• Advertisement
Sign in to follow this  

GLSL and Materials

This topic is 4415 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working through some basic tutorials on shaders, and right now I'm modifying my cel shader to use the object's material to determine the color. I'm rendering a .OBJ model, and when I go to change the material, the material doesn't change. That is, my entire object is one color. It's always the color of the first material set. When I turn off the shader before changing materials and turn it back on afterward, the model displays as expected. However, on my card, this gives a huge performance hit (ATI Radeon Mobility X300). Does GLSL intercept calls to glMaterialfv or something? I couldn't imagine it would, but perhaps my understanding of shaders is a bit shoddy. Here's my fragment shader:
varying vec3 normal;

void main()
{
	vec3 n = normalize(normal);
	float intensity = dot(vec3(gl_LightSource[0].position),n);
		
	if (intensity > 0.95) intensity = 1.0;
	else if (intensity > 0.75) intensity = 0.75;
	else if (intensity > 0.5) intensity = 0.5;
	else if (intensity > 0.25) intensity = 0.25;
	else intensity = 0.10;

	gl_FragColor = gl_FrontMaterial.diffuse * intensity;
} 

And the relevant section of code:
for (int faceGroup = 0; faceGroup < mFaceGroups.size(); faceGroup++)
		{
			//glLinkProgramARB(0);
			//glUseProgramObjectARB(0);

			glMaterialfv(GL_FRONT, GL_AMBIENT, mMaterials[mFaceGroups[faceGroup].materialID].ambient);
			glMaterialfv(GL_FRONT, GL_DIFFUSE, mMaterials[mFaceGroups[faceGroup].materialID].diffuse);
			glMaterialfv(GL_FRONT, GL_SPECULAR, mMaterials[mFaceGroups[faceGroup].materialID].specular);

			//glLinkProgramARB(shaderProg);
			//glUseProgramObjectARB(shaderProg);

			glBegin(GL_TRIANGLES);
			for (int face = 0; face < mFaceGroups[faceGroup].faces.size(); face++)
			{
				Face f = mFaceGroups[faceGroup].faces[face];

						
				glTexCoord2fv(mTexCoords[f.indtex[0]].uv);
				glNormal3fv(mNormals[f.indnorm[0]].v);
				glVertex3fv(mVertices[f.indvert[0]].v);
				glTexCoord2fv(mTexCoords[f.indtex[1]].uv);
				glNormal3fv(mNormals[f.indnorm[1]].v);
				glVertex3fv(mVertices[f.indvert[1]].v);
				glTexCoord2fv(mTexCoords[f.indtex[2]].uv);
				glNormal3fv(mNormals[f.indnorm[2]].v);
				glVertex3fv(mVertices[f.indvert[2]].v);
			}
			glEnd();
		}

Cheers, --Brian

Share this post


Link to post
Share on other sites
Advertisement
changinf materials with glMaterialfv(GL_FRONT, GL_AMBIENT,
is very slow , the faster method is to use GL_COLOR_MATERIAL (ch3ck the spec)
though since youre glsl i wouldnt use the fixed pipeline material stuff at all
but instead either supply the material color data as either uniforms or attributes

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement