English 中文(简体)
In OpenGL, what is the simplest way to create a perspective view using only OpenGL3+ methods?
原标题:

This might sound lazy at first, but I ve been researching it for 2 days.

I have an SDL+GLEW app that draws a primitive. I wanted to make a few viewports in different perspectives. I see the four viewports but I can t change the the perspectives.

Assume you have

draw();
swapbuffers();

What is the simplest way - in OpenGL3+ spec - to create a perspective viewport (or ideally several ones)?

最佳回答

The simplest is the only way. Calculate yourself (with some lib, plain math, etc) a projection matrix, and upload it to a uniform in your vertex shader, and do the transformation there.

问题回答

Here s GLSL 1.50 code for doing the same thing as gluPerspective. You can easily convert it to your language of choice, and upload it via glUniformMatrix4fv instead.

mat4 CreatePerspectiveMatrix(in float fov, in float aspect,
    in float near, in float far)
{
    mat4 m = mat4(0.0);

    float angle = (fov / 180.0f) * PI;
    float f = 1.0f / tan( angle * 0.5f );

    /* Note, matrices are accessed like 2D arrays in C.
       They are column major, i.e m[y][x] */

    m[0][0] = f / aspect;
    m[1][1] = f;
    m[2][2] = (far + near) / (near - far);
    m[2][3] = -1.0f;
    m[3][2] = (2.0f * far*near) / (near - far);

    return m;
}

Then you do:

mat4 worldMatrix = CreateSomeWorldMatrix();
mat4 clipMatrix = CreatePerspectiveMatrix(90.0, 4.0/3.0, 1.0, 128.0);

gl_Position = worldMatrix * clipMatrix * myvertex;
some_varying1 = worldMatrix * myvertex; 
some_varying2 = worldMatrix * vec4(mynormal.xyz, 0.0);

For perspective matrix: gluPerspective, then glGetFLoatv(GL_PROJECTION_MATRIX, ...), then glUniform or glUniformMatrix4fv

For viewports: glViewport and glScissor. Or use framebuffer object, render to texture, then render texture.

P.S. If you don t want to use deprecated features, grab matrix formula from gluPerspective documentation, calculate matrix yourself, then use glUniformMatrix4fv. Or work in compatibility profile.





相关问题
OpenGL 3D Selection

I am trying to create a 3D robot that should perform certain actions when certain body parts are clicked. I have successfully (sort of) implemented picking in that if you click on any x-plane part, it ...

CVDisplayLink instead of NSTimer

I have started to implement cvDisplayLink to drive the render loop instead of nstimer, as detailed in this technical note https://developer.apple.com/library/archive/qa/qa1385/_index.html Is it ...

Can the iPhone simulator handle PVR textures?

I have a really weird problem with PVR textures on the iPhone simulator- the framerate falls through the floor on the iPhone simulator, but on the iPhone itself it works just fine. Has anyone had any ...

Calculate fps (frames per second) for iphone app

I am using an opengl es iphone application. What is the most accurate way to calculate the frames per second of my application for performance tuning?

Java - Zoom / 3D Data Visualization Libraries

What are the best libraries/frameworks for doing 3D and/or Zoom interfaces in Java? I d like to be able to do some prototyping of creating new types of interfaces for navigating within data and ...

FLTK in Cygwin using Eclipse (Linking errors)

I have this assignment due that requires the usage of FLTK. The code is given to us and it should compile straight off of the bat, but I am having linking errors and do not know which other libraries ...

热门标签