As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
Options

OpenGL/GLSL question - Passing uniforms?

12gauge12gauge Registered User regular
edited September 2007 in Help / Advice Forum
Hi, I want to recreate a nvidia shader in glsl but I just can't seem to get the uniform variables in my shader. Here is my vertex shader:
varying vec3 velocity;
uniform mat4 prevModelView;

void main() {	
	float blurScale = 1.0;
	gl_TexCoord[0] = gl_MultiTexCoord0;
	//transforming the positions to eyespace
	vec3 hWS = vec3(256.0, 256.0, 0.0);//halfWindowSize;

	vec4 P = gl_ModelViewMatrix * gl_Vertex;
	vec4 Pprev = prevModelView * gl_Vertex;
	
	//transfrom the normals
	vec3 normal = normalize(gl_NormalMatrix * gl_Normal);
	
	//now, the motion vector
	vec3 motionVector = P.xyz - Pprev.xyz;


	//calculate  the previous MODELVIEW_PROJECTION_MATRIX
	mat4 prevModelViewProj =  gl_ProjectionMatrix * prevModelView;

	//get the clip space motion vector
	P = gl_ModelViewProjectionMatrix * gl_Vertex;
	Pprev = prevModelViewProj * gl_Vertex;
	
	Pprev = mix(P, Pprev, 1.0);

	//choose current or previous position
	//bool flag = dot(motionVector, normal) > 0.0;
	vec4 Pstretch;
	float flag = sign(dot(motionVector, normal));
	if (flag > 0.0)
	{
		Pstretch = P;
	}
	else{
		Pstretch = Pprev;
	}
	P.xyz = P.xyz / P.w;
	Pprev.xyz = Pprev.xyz / Pprev.w;
	//Pstretch.xyz = Pstretch.xyz / Pstretch.w;

	vec3 dP = (P.xyz - Pprev.xyz) * hWS;
	velocity = dP;

	gl_Position =  Pstretch;
	//gl_ClipVertex = Pstretch;
}

And here is my code to update the prevModelView (in opengl it is an float[16] array, same as modelView)
void renderScene(void) {
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glTranslatef(0.0, 0.0, -30.0);
	//glRotatef(yrot, 1.0, 0.0, 0.0);
	glRotatef(yrot, 0.0, 1.0, 0.0);
	//glRotatef(yrot, 0.0, 0.0, 1.0);
	glGetFloatv(GL_MODELVIEW_MATRIX, modelView);
	//drawModel();
	
	drawScene();
	drawBlurredScene();
	yrot+=1.0f;
	for(int i = 0; i < 16; i++)
	{
		prevModelView[i] = modelView[i];
	}
	GLfloat tester[20];
	glutSwapBuffers();
	glGetUniformfv(mblur_p, glGetUniformLocation(mblur_p, "prevModelView"), tester);
	for(int i = 0; i < 20; i++)
	{
		printf("Number &#37;i :%f\n",i, tester[i]);
	}
	glutSwapBuffers();
}

and in the drawBlurredScene()
[...]
glUniformMatrix4fv(getUniLoc(mblur_p, "prevModelView"), 0, GL_TRUE, prevModelView);
glUseProgram(mblur_p);
drawModel();
glUseProgram(0);

}


I can see from the result, that all vertices affected by the prevModelView-Matrix in the shader get translated to 0,0,0 modelspace and when I query the variable, I also only get zeros on all 16 positions. What the fuck am I doing wrong?

Edit: Here is the getUniLoc-Function
GLint getUniLoc(GLuint program, const GLchar *name)
{
	GLint loc;

	loc = glGetUniformLocation(program, name);

	if (loc == -1)
		printf("No such uniform name \"%s\"\n", name);

	//printOpenGLError();
	return loc;
}

davidoc0.jpg
12gauge on

Posts

  • Options
    zilozilo Registered User regular
    edited September 2007
    Your call to glUniformMatrix4fv should have parameters (thing, 1, thing, thing) not (thing, 0, thing, thing).

    Let me know if that helps. I'll be mostly away from the internet for the next week or so (moving to California) but I'll try to poke my head in when I find some time.

    zilo on
  • Options
    12gauge12gauge Registered User regular
    edited September 2007
    zilo wrote: »
    Your call to glUniformMatrix4fv should have parameters (thing, 1, thing, thing) not (thing, 0, thing, thing).

    Let me know if that helps. I'll be mostly away from the internet for the next week or so (moving to California) but I'll try to poke my head in when I find some time.

    Yeah, the glUniformMatrix4fv parameters where me just trying around to see if I can get different results - good catch with the linking, got me on the right lead - uniforms have not to be initialized before linking, but after the shader program is used - as you can see, I defined the variable *before* I used the shader:|

    Thanks a lot and good luck on your way to california!

    12gauge on
    davidoc0.jpg
  • Options
    zilozilo Registered User regular
    edited September 2007
    Bah, of course. I can never remember the order in which that stuff needs to happen.

    Glad to see it works. Post screens ;-)

    zilo on
  • Options
    12gauge12gauge Registered User regular
    edited September 2007
    zilo wrote: »
    Bah, of course. I can never remember the order in which that stuff needs to happen.

    Glad to see it works. Post screens ;-)

    Well, after some more work on the fragment part, here you can see a comparison - guess which one has the motion blur... :P
    comparisonve0.jpg

    12gauge on
    davidoc0.jpg
  • Options
    zilozilo Registered User regular
    edited September 2007
    Sorry for the semi-necropost, I was moving this week.

    That looks great. How's the framerate hit?

    zilo on
Sign In or Register to comment.