• Create Account

## opengl 3/GLM matrix help

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1ic0de  Members

954
Like
0Likes
Like

Posted 12 December 2012 - 10:21 PM

I'm updating some Opengl 2 stuff to Opengl 3+ and I'm having some trouble with the absence of the matrix stack. I'm using GLM (opengl mathematics) to do my matrix operations. I wrote a small function to replace each of the opengl transformation functions with their GLM alternative and replaced all calls to those functions with my own. All that works in theory but when I run my program I get a whole bunch of garbage wonky polygons making no sense. Here is my code:

glm::mat4 modelMatrix;
glm::mat4 modelViewMatrix;
glm::mat4 modelViewProjectionMatrix;
glm::mat3 normalMatrix;
glm::mat4 ProjectionMatrix;
glm::mat4 viewMatrix;

ProjectionMatrix = glm::perspective(60.0f, ratio, 0.1f, 4096.0f); //setup projection
viewMatrix = glm::lookAt(glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 0.0, 1.0), glm::vec3(0.0, 1.0, 0.0)); //set view camera at 0,0,0 looking forward into the z-axis

void coLoadIdentity()
{
modelMatrix = glm::mat4(1.0f);
}
void coRotatef(float angle, float x, float y, float z)
{
modelMatrix = glm::rotate(modelMatrix, angle, glm::vec3(x, y, z));
//glRotatef(angle, x, y, z);
}
void coTranslatef(float x, float y, float z)
{
modelMatrix = glm::translate(modelMatrix, glm::vec3(x, y, z));
//glTranslatef(x, y, z);
}
inline void reApplyTransform(vec viewer, float yaw, float pitch) //used to apply two operations with one function for simplicity
{
coRotatef(pitch, 1.0, 0.0, 0.0);
coRotatef(yaw, 0.0, 1.0, 0.0);
coTranslatef(viewer.x, viewer.y, viewer.z);
}

void updateMatrices() //called before something is drawn and after transformations have been applied
{
modelViewMatrix = modelMatrix * viewMatrix;
modelViewProjectionMatrix = modelViewMatrix * ProjectionMatrix;
normalMatrix = glm::inverseTranspose(glm::mat3(modelViewMatrix));
glUniformMatrix4fv(mvmat_loc, 1, false, glm::value_ptr(modelViewMatrix));
glUniformMatrix4fv(mvpmat_loc, 1, false, glm::value_ptr(modelViewProjectionMatrix));
glUniformMatrix3fv(nmat_loc, 1, false, glm::value_ptr(normalMatrix));
}


void main(void)
{
vnormal = normalize(normalMatrix * normal);
vec3 ntangent = normalize(normalMatrix * tangent.xyz);

vec3 bitangent = cross(vnormal, ntangent) * -tangent.w;

TBN = mat3(ntangent, bitangent, vnormal);

v = vec3(modelViewMatrix * vec4(position, 1.0));
VaryingTexCoord0 = vec4(vec3(texCoord, 0), 0);
gl_Position = modelViewProjectionMatrix * vec4(position, 1.0);
}


anybody spot anything obviously wrong?

Edited by ic0de, 12 December 2012 - 10:25 PM.

you know you program too much when you start ending sentences with semicolons;

### #2beans222  Members

1248
Like
2Likes
Like

Posted 13 December 2012 - 12:56 AM

Check your ordering of multiplying the matrices together. The destination space of the combined matrix needs to be on the left of the multiply.
(both the multiplies in the first two lines of updateMatrices are backwards)

If you haven't already, do yourself a huge favor and read arcsynthesis.org/gltut all the way through. He explains all the math from the beginning and uses GLM for all the examples.

New C/C++ Build Tool 'Stir' (doesn't just generate Makefiles, it does the build): https://github.com/space222/stir

### #3ic0de  Members

954
Like
0Likes
Like

Posted 13 December 2012 - 02:21 PM

Check your ordering of multiplying the matrices together. The destination space of the combined matrix needs to be on the left of the multiply.
(both the multiplies in the first two lines of updateMatrices are backwards)

If you haven't already, do yourself a huge favor and read arcsynthesis.org/gltut all the way through. He explains all the math from the beginning and uses GLM for all the examples.

I reversed the order of multiplication and everything works great, thanks alot.

you know you program too much when you start ending sentences with semicolons;

### #4TheChubu  Members

8945
Like
0Likes
Like

Posted 13 December 2012 - 03:19 PM

Matrix multiplication has a few more rules than common multiplication . It'd be good if you understood that well before dealing with matrices http://en.wikipedia.org/wiki/Matrix_multiplication

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.