Newbie question

Hi!

I recently started to read “the orange book” and tried implementing the first example, which is a brick wall.
I had to modify the code since the book is written for GLSL 1.40 and the mac driver for GeForce GTX285 only supports GLSL 1.20.

If I draw a rectangle it looks OK. However if I draw a teapot it only renders parts of it, especially if depth-test is enabled.
I suspect it might be due to the fact that the translate-call isn’t properly taken into account in all the shading calculations.

Any help is much appreciated.

Vertex shader:


uniform vec3 LightPosition;

const float SpecularContribution = 0.3;
const float DiffuseContribution = 1.0 - SpecularContribution;

varying float LightIntensity;
varying vec2 MCposition;

void main()
{
	vec3 ecPosition = vec3(gl_ModelViewProjectionMatrix * gl_Vertex);
	vec3 tnorm = normalize(gl_NormalMatrix * gl_Normal);
	vec3 lightVec = normalize(LightPosition - ecPosition);
	vec3 reflectVec = reflect(-lightVec, tnorm);
	vec3 viewVec = normalize(-ecPosition);
	float diffuse = max(dot(lightVec, tnorm), 0.0);
	float spec = 0.0;

	if(diffuse > 0.0)
	{
		spec = max(dot(reflectVec,viewVec), 0.0);
		spec = pow(spec, 16.0);
	}

	LightIntensity = DiffuseContribution * diffuse + SpecularContribution * spec;
	MCposition = gl_Vertex.xy;
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
	
}

Fragment shader:


uniform vec3 LightPosition;

const float SpecularContribution = 0.3;
const float DiffuseContribution = 1.0 - SpecularContribution;

varying float LightIntensity;
varying vec2 MCposition;

void main()
{
	vec3 ecPosition = vec3(gl_ModelViewProjectionMatrix * gl_Vertex);
	vec3 tnorm = normalize(gl_NormalMatrix * gl_Normal);
	vec3 lightVec = normalize(LightPosition - ecPosition);
	vec3 reflectVec = reflect(-lightVec, tnorm);
	vec3 viewVec = normalize(-ecPosition);
	float diffuse = max(dot(lightVec, tnorm), 0.0);
	float spec = 0.0;

	if(diffuse > 0.0)
	{
		spec = max(dot(reflectVec,viewVec), 0.0);
		spec = pow(spec, 16.0);
	}

	LightIntensity = DiffuseContribution * diffuse + SpecularContribution * spec;
	MCposition = gl_Vertex.xy;
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
	
}

OpenGL (parts of init-code)


glUniform3f(getUniLoc(g_program, "BrickColor"), 1.0, 0.3, 0.2);
	glUniform3f(getUniLoc(g_program, "MortarColor"), 0.85, 0.86, 0.84);
	g_brickSize = getUniLoc(g_program, "BrickSize");
	glUniform2f(g_brickSize, 0.30, 0.15);
	
	glUniform2f(getUniLoc(g_program, "BrickPct"), 0.90, 0.85);
	g_lighPos = getUniLoc(g_program, "LightPosition");
	glUniform3f(g_lighPos,2.0, 2.0, -1.5);

OpenGL (main part of display handler)


glClearColor(0, 0.0, 0.0, 1.0);

	glClear(GL_COLOR_BUFFER_BIT);
	
	glViewport(0,0,k_dxWINDOW,k_dyWINDOW);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
	
	glFrustum (-1, 1.0, -1, 1.0,
			   1.5, 20.0); 
	
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
	
	glPushMatrix();
	
	glTranslated(0, 0, -5);
	glRotated(g_angle*360/(2*M_PI), 0, 1, 0);
	glutSolidTeapot(1);
	
	glPopMatrix();

I found the problem. In the vertex shader the line


vec3 ecPosition = vec3(gl_ModelViewProjectionMatrix * gl_Vertex);

shoud’ve been


vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Vertex);

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.