copenglglu

Object not being fulfilled with material


I am starting to write code for materials in my class project, this is what i have as a initial test:

glEnable(GL_COLOR_MATERIAL);
        glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,_selected_object->material.ambient);
        glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,_selected_object->material.diffuse);
        glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,_selected_object->material.specular);
        _selected_object->material.shininess=128.0f;
        glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,_selected_object->material.shininess);

And this is the result in the object

Why aren't the faces being fulfiled completely by the material?

I do have a light source with this values:

    l->A1[0]=0.0;
    l->A1[1]=0.0;
    l->A1[2]=0.0;
    l->A1[3]=1.0;
    l->RDifusa[0]=1.0;
    l->RDifusa[1]=1.0;
    l->RDifusa[2]=1.0;
    l->RDifusa[3]=1.0;
    l->REspecular[0]=1.0;
    l->REspecular[1]=1.0;
    l->REspecular[2]=1.0;
    l->REspecular[3]=1.0;
    l->position[0]=1.0;
    l->position[1]=1.0;
    l->position[2]=1.0;
    l->position[3]=0.0;

Which i display here (in my init function):

    glEnable(GL_LIGHTING);
    glLightfv(GL_LIGHT0, GL_AMBIENT, l->A1);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, l->RDifusa);
    glLightfv(GL_LIGHT0, GL_SPECULAR, l->REspecular);
    glLightfv(GL_LIGHT0, GL_POSITION, l->position);
    glEnable(GL_LIGHT0);

This is the code part that draws the object:

while (aux_obj != 0) {

        glPushMatrix();
       // glLoadIdentity();
        glMultMatrixd(aux_obj->mtptr->M);

        /* Select the color, depending on whether the current object is the selected one or not */
        if (aux_obj == _selected_object){
            glColor3f(KG_COL_SELECTED_R,KG_COL_SELECTED_G,KG_COL_SELECTED_B);
        }else{
            glColor3f(KG_COL_NONSELECTED_R,KG_COL_NONSELECTED_G,KG_COL_NONSELECTED_B);
        }
    

        /* Draw the object; for each face create a new polygon with the corresponding vertices */
        //glLoadIdentity();
        for (f = 0; f < aux_obj->num_faces; f++) {
            glBegin(GL_POLYGON);
            vector3 N,A,B,C,ABAC;

            for (v = 0; v < aux_obj->face_table[f].num_vertices; v++) {
                v_index = aux_obj->face_table[f].vertex_table[v];
                glVertex3d(aux_obj->vertex_table[v_index].coord.x,
                aux_obj->vertex_table[v_index].coord.y,
                aux_obj->vertex_table[v_index].coord.z);
            }
        }
        glEnd();
        glEnable(GL_COLOR_MATERIAL);
        glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,_selected_object->material.ambient);
        glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,_selected_object->material.diffuse);
        glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,_selected_object->material.specular);
        



        //Draw vertex normal vectors->
        for(v=0;v<aux_obj->num_vertices;v++){
            glBegin(GL_LINE);
            glVertex3d(aux_obj->vertex_table[v].coord.x,aux_obj->vertex_table[v].coord.y,aux_obj->vertex_table[v].coord.z);
            //Point A= vertex
            //Point B = A - 10000*D
            glVertex3d(aux_obj->vertex_table[v].coord.x+(aux_obj->vertex_table[v].normal.x),aux_obj->vertex_table[v].coord.y+(aux_obj->vertex_table[v].normal.y),aux_obj->vertex_table[v].coord.z+(aux_obj->vertex_table[v].normal.z));
            glEnd();
        } 
        


        //Set object material values->
        
        glPopMatrix();
        aux_obj = aux_obj->next;
    }


Solution

  • The primitive GL_POLYGON connects all vertices of the primitive to one single polygon. You create a polygon for each face and must glEnd() each individual polygon. glEnd() must be called in the loop that traverses all faces, but not after the loop:

    for (f = 0; f < aux_obj->num_faces; f++) {
        glBegin(GL_POLYGON);
        vector3 N,A,B,C,ABAC;
    
        for (v = 0; v < aux_obj->face_table[f].num_vertices; v++) {
            v_index = aux_obj->face_table[f].vertex_table[v];
            glVertex3d(aux_obj->vertex_table[v_index].coord.x,
            aux_obj->vertex_table[v_index].coord.y,
            aux_obj->vertex_table[v_index].coord.z);
        }
        glEnd();                                             // <--- INSERT
    }
    //glEnd();                                                  <--- DELETE