Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does my translation matrix needs to be transposed?
    primarykey
    data
    text
    <p>I'm working on a small graphics engine using OpenGL and I'm having some issues with my translation matrix. I'm using OpenGL 3.3, GLSL and C++. The situation is this: I have defined a small cube which I want to render on screen. The cube uses it's own coordinate system, so I created a model matrix to be able to transform the cube. To make it myself a bit easier I started out with just a translation matrix as the cube's model matrix and after a bit of coding I've managed to make everything work and the cube appears on the screen. Nothing all to special, but there is one thing about my translation matrix that I find a bit odd.</p> <p>Now as far as I know, a translation matrix is defined as follows:</p> <pre><code>1, 0, 0, x 0, 1, 0, y 0, 0, 1, z 0, 0, 0, 1 </code></pre> <p>However, this does not work for me. When I define my translation matrix this way, nothing appears on the screen. It only works when I define my translation matrix like this:</p> <pre><code>1, 0, 0, 0 0, 1, 0, 0 0, 0, 1, 0 x, y, z, 1 </code></pre> <p>Now I've been over my code several times to find out why this is the case, but I can't seem to find out why or am I just wrong and does a translation matrix needs to be defined like the transposed one here above?</p> <p>My matrices are defined as a one-dimensional array going from left to right, top to bottom.</p> <p>Here is some of my code that might help:</p> <pre><code>//this is called just before cube is being rendered void DisplayObject::updateMatrices() { modelMatrix = identityMatrix(); modelMatrix = modelMatrix * translateMatrix( xPos, yPos, zPos ); /* update modelview-projection matrix */ mvpMatrix = modelMatrix * (*projMatrix); } //this creates my translation matrix which causes the cube to disappear const Matrix4 translateMatrix( float x, float y, float z ) { Matrix4 tranMatrix = identityMatrix(); tranMatrix.data[3] = x; tranMatrix.data[7] = y; tranMatrix.data[11] = z; return Matrix4(tranMatrix); } </code></pre> <p>This is my simple test vertex shader:</p> <pre><code>#version 150 core in vec3 vPos; uniform mat4 mvpMatrix; void main() { gl_Position = mvpMatrix * vec4(vPos, 1.0); } </code></pre> <p>I've also did tests to check if my matrix multiplication works and it does. I * randomMatrix is still just randomMatrix</p> <p>I hope you guys can help. Thanks</p> <p>EDIT:</p> <p>This is how I send the matrix data to OpenGL:</p> <pre><code>void DisplayObject::render() { updateMatrices(); glBindVertexArray(vaoID); glUseProgram(progID); glUniformMatrix4fv( glGetUniformLocation(progID, "mvpMatrix"), 1, GL_FALSE, &amp;mvpMatrix.data[0] ); glDrawElements(GL_TRIANGLES, bufferSize[index], GL_UNSIGNED_INT, 0); } </code></pre> <p>mvpMatrix.data is a std::vector:</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload