PDA

View Full Version : Ver 130 -> 140. Uniform & Transposition Question.



Red_Riot
02-27-2011, 01:30 PM
Howdy, was working on GLSL 140 and trying to ditch legacy code but I'm having problems. I'm trying to make the transition in steps. This is basic demo code to test for functionality:

This is my old 130 code, which works fine, it's a vertex shader GLSL program.

#version 130
in vec4 MCVertex;
void main() {
gl_Position = gl_ModelViewProjectionMatrix * MCVertex;
}

MCVertex gets passed automatically every time glDrawElements() is called correct? there's no API binding to do?

Here's the code I'm attempting to go to:

#version 140
uniform mat4 MVPmat;
in vec4 MCVertex;
void main() {
gl_Position = MVPmat * MCVertex;
}

The trick here is that you have to generate your own Project Model View matrix, which is (Projection Matrix * ModelViewMatrix = MVPmat)

Which leads me to think that I'm running into an API issue. I'm still using legacy glTranslatef to generate a Modelview matrix and gluPerspective to generate a Perspective matrix before I impliment my own. I use glGetFloatv(GL_PROJECTION_MATRIX , PMatrix);
glGetFloatv(GL_MODELVIEW_MATRIX , MVMatrix);
to pull these values to generate my own ProjModelView matrix and pass it to GLSL.
C++ OpenGL Code:

float PMatrix[16], MVMatrix[16], MVPMatrix[16];
GLuint MVPmat;
void InitCode(){
glGetFloatv(GL_PROJECTION_MATRIX , PMatrix);
glGetFloatv(GL_MODELVIEW_MATRIX , MVMatrix);
MatMult4(MVMatrix,PMatrix,MVPMatrix);
MVPmat = glGetUniformLocation(shader, "MVPmat");
glUniformMatrix4fv(MVPmat,1,GL_FALSE,MVPMatrix);
}

However this does NOT yield the same results as the 130 version of GLSL using glProjectionModelView * MCVertex. I've tried passing PMatrix and MVMatrix in as their own uniforms as well and doing PMatrix * MVMatrix * MCVertex to get my position as well but it doesn't work.

No compiler warnings or errors from the 140 compiler. (130 shows me a warning that glProjectionModelView is depreciated)

Any Ideas?



MatMult4 function is as follows:
void MatMult4(float *matA, float *matB, float *matX)
{
for (int x = 0; x < 16; x += 4)
{
for (int y = 0; y < 4; y++)
{
matX[x+y] = matA[x]*matB[y]+matA[x+1]*matB[y+4]+matA[x+2]*matB[y+8]+matA[x+3]*matB[y+12];
}
}
}

mobeen
02-27-2011, 10:17 PM
You should get ur matrices(MV and P) before the drawelements call in the render function and not in the init function. So in code it would be somethign like this,


void render() {
glClear(...);
//setup modelview and proj. matrices
//get the modelview proj matrices and create
//the concatenated matrix MVPMatrix

glUseProgram(progID);
//pass the matrix to the shader
glUniformMatrix4fv(MVPmat,1,GL_FALSE,MVPMatrix);
//bind ur vao/vbos
glDrawElements(...);
//unbind vao/vbo
glUseProgram(0);
glutSwapBuffers();
}

Red_Riot
03-09-2011, 07:46 PM
Thank you.