mardi 27 décembre 2016

How to use material color when drawing different objects in OpenGL?

I am trying to figure out a way to use a material specified with a color along with lights to color different objects. The exact problem I am dealing with is as follows.

There are quite many components involved so I will try to be as clear and concise as possible. I have a scene file in which there are several meshes each containing a number of triangles and those triangles correspond to vertices in 3d space. Each mesh is also related to a material which has diffuse, ambient, specular color reflectance triplets specified. There are also light sources with specified positions and light intensities. So far everything has worked out well, except the part where I try to color each object pertaining to a mesh. This is an overview of the code shown below.

void display(){
    ....
    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_specular);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);

    ....
    for each mesh{

        glMaterialfv(GL_FRONT,GL_AMBIENT,ambient);
        glMaterialfv(GL_FRONT,GL_DIFFUSE,diffuse);
        glMaterialfv(GL_FRONT,GL_SPECULAR,specular);
        glMaterialfv(GL_FRONT,GL_SHININESS,pong);

    /// These preceding statements don't seem to have an effect in changing the color of the vertices

        glDrawElements(GL_TRIANGLES,current_triCount*3,GL_UNSIGNED_INT, (void*)(sizeof(GLuint)*index));
        index+= current_triCount*3;
    }

}
int main(int argc, char** argv)
{
     glutInit(&argc, argv);
     init();     // enabling lights and other stuff
     ...
     glutDisplayFunc(display);

     ...
     glutMainLoop();
}

Aucun commentaire:

Enregistrer un commentaire