opengl 3/GLM matrix help

Started by
2 comments, last by TheChubu 11 years, 4 months ago
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);
//glLoadIdentity();
}
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
{
coLoadIdentity();
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));
}


vertex shader:

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?
Advertisement
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


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.
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

This topic is closed to new replies.

Advertisement