Frage

glModelMatrix function is used to modify either projection or model matrices and compute vertices positions properly. However, since i use VAO, VBO and shader program the magic doesn't happens anymore. (same issues with gluPerspective or gluOrtho)

Should i compute model and projection matrices on my own and send them to shader ?

War es hilfreich?

Lösung

Yes, the general idea is to perform matrix computations on your own. Actually no serious 3D program ever made use of the old OpenGL matrix stack. There's no benefit in using the old OpenGL matrix functions, except that they make things a little bit more convenient for beginners (no, there's also no GPU acceleration for that). Quite frankly, OpenGL should never have had them in the first place, and GLU should have provided the basic matrix functions.

Any matrix math library that can work with 4×4 matrices will do. GLM is very popular among the c++ folks, and there's a Java conversion called jGLM at https://github.com/jroyalty/jglm

Andere Tipps

Should i compute model and projection matrices on my own and send them to shader?

Yes!

(If you are using C++ GLM as the library to help with that and it works great. For Java with LWJGL read on...)

Quick LWJGL example:

//-- Update matrices
// Reset view and model matrices
viewMatrix = new Matrix4f();
modelMatrix = new Matrix4f();

// Translate camera
Matrix4f.translate(cameraPos, viewMatrix, viewMatrix);

// Scale, translate and rotate model
Matrix4f.scale(modelScale, modelMatrix, modelMatrix);
Matrix4f.translate(modelPos, modelMatrix, modelMatrix);
Matrix4f.rotate(this.degreesToRadians(modelAngle.z), new Vector3f(0, 0, 1), modelMatrix, modelMatrix);
Matrix4f.rotate(this.degreesToRadians(modelAngle.y), new Vector3f(0, 1, 0), modelMatrix, modelMatrix);
Matrix4f.rotate(this.degreesToRadians(modelAngle.x), new Vector3f(1, 0, 0), modelMatrix, modelMatrix);

// Upload matrices to the uniform variables
GL20.glUseProgram(pId);

projectionMatrix.store(matrix44Buffer); matrix44Buffer.flip();
GL20.glUniformMatrix4(projectionMatrixLocation, false, matrix44Buffer);
viewMatrix.store(matrix44Buffer); matrix44Buffer.flip();
GL20.glUniformMatrix4(viewMatrixLocation, false, matrix44Buffer);
modelMatrix.store(matrix44Buffer); matrix44Buffer.flip();
GL20.glUniformMatrix4(modelMatrixLocation, false, matrix44Buffer);

GL20.glUseProgram(0);

The Vertex Shader:

#version 150 core

uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;

in vec4 in_Position;
in vec4 in_Color;
in vec2 in_TextureCoord;

out vec4 pass_Color;
out vec2 pass_TextureCoord;

void main(void) {
    gl_Position = in_Position;
    // Override gl_Position with our new calculated position
    gl_Position = projectionMatrix * viewMatrix * modelMatrix * in_Position;

    pass_Color = in_Color;
    pass_TextureCoord = in_TextureCoord;
}

Full source - http://lwjgl.org/wiki/index.php?title=The_Quad_with_Projection,_View_and_Model_matrices

Why is self compute of model and projection matrices better?

In short, it is more flexible and allows you to optimize based on your use case. OpenGL 1.x uses fixed function render pipeline, is vendor/driver implementation dependent and cannot be optimized to suit a use case better.

A quick example is, lets say you just want to render a whole lot of quads 2D, you can use a 3x3 matrix for vertex transform saving GPU cycles.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top