I’m making a basic game in openGL with GLUT, and I’ve come across a small hitch.
When I attempt to draw an object from a class into my world, it gives everything a tint of colour which was last created with the function makeMaterial (see below).
Does anyone know how I could restrict these changes to being applied to the object only?
I’ve tried push/pop, but it doesn’t seem to limit anything.
Additionally, if the last colour drawn is black, I see nothing at all, except for my snowman.
Here are the relevant code:
makeMaterial :-
makeMaterial( float r,float g,float b,float a,float s )
{
float color[4];
float white[4];
GLfloat surfshine[1] ;
surfshine[0]=s*128.0 ;
color[0]=r;color[1]=g;color[2]=b;color[3]=a;
white[0]=0.5;white[1]=0.5;white[2]=0.5;white[3]=1.0;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT,color);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,color);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,white);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS,surfshine);
}
makeMaterial in use :-
makeMaterial(0.05f,0.05f,0.05f,1.00f, 0.4f);
glPushMatrix();
glutSolidCube(10.0);
glPopMatrix();
my draw function :-
void drawToScene(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
uniformFPS.setStartTime(glutGet(GLUT_ELAPSED_TIME));
camera.updateCamera();
lvl1.drawLevel();
enemy.drawSnowman(350.0f,6.0f, 200.0f);
crosshair.show();
glutSwapBuffers();
uniformFPS.enforceChecks();
}
If you really want to ensure you are restricting changes to a specific section, you should be able to use
glPushAttrib(GL_LIGHTING_BIT)before you do yourmakeMaterialcall, andglPopAttribafter your drawing of the object is done. The reference forglPushAttribhere shows what properties are push’d by each of the possible bits you can supply. I don’t know that this is a particularly well regarded way to handle your state however.A more general solution would be to make a call to
makeMaterialwith appropriate values before each set of drawing commands, i.e. assuming you have just one material for your snowman and one material for your world: