PDA

View Full Version : Applying Material to glDrawElements Object



Brian Brookwell
08-10-2012, 10:45 AM
I've created an index vertex array (positions & surface normals). I'm trying to change the material properties of the object. Unfortunately, the surface being rendered seems to be a flat grey no matter what I do.

Lighting & display data is set up in initialization this way:

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer( 3, GL_FLOAT, sizeof(Node), site);
glNormalPointer(GL_FLOAT, sizeof(Node), &site[0][0].normVec);
glPointSize(2.0);
glClearColor(0.0,0.0,0.0,0.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_DEPTH_TEST);
glLightfv(GL_LIGHT1, GL_AMBIENT, lightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightDiffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, lightSpecular);
glLightfv(GL_LIGHT1, GL_POSITION, lightPosition);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
glFrontFace(GL_CCW);
glShadeModel(GL_SMOOTH);

The actual display is done this way:

void drawScene() {
// Other stuff might go here

glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_DIFFUSE);
glColor3f(1.0,0,1.0);
glMaterialf(GL_FRONT,GL_SHININESS,50.0);
glColorMaterial(GL_FRONT,GL_SPECULAR);
glColor3f(1.0,1.0,1.0);

glDrawElements(GL_TRIANGLES, //mode
numIndices, //count, ie. how many indices
GL_UNSIGNED_INT, //type of the index array
indexArray);
glDisable(GL_COLOR_MATERIAL);
}

void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

glLightfv(GL_LIGHT0,GL_POSITION,lightPosition);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);

camRender(camera);

drawScene();

glFlush();
glutSwapBuffers();
}

V-man
08-12-2012, 02:33 PM
Try it without glEnable(GL_COLOR_MATERIAL) and just use glMaterial function calls. Does it work?

Brian Brookwell
08-13-2012, 09:03 AM
Try it without glEnable(GL_COLOR_MATERIAL) and just use glMaterial function calls. Does it work?

Okay. I tried that and at first it didn't work. I found a problem in how I was specifying the materials though and that looks like it fixed the problem.

glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, waterColor);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, waterAmbient);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, waterShiny); <-- I had a single value here
glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 50.0);<-- I didn't have this one at all

I'd confused GL_SHININESS with GL_SPECULAR. Thanks for the help!