0

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;
    }

martqqqqq
  • 13
  • 3
  • *"Why aren't the faces being fulfiled completely by the material?"* - What is the OpenGL [primitive](https://www.khronos.org/opengl/wiki/Primitive) type used to draw the geometry? You have to use `GL_QUADS`, `GL_POLYGON` or one of the `TRIANGLE` primitive types and the [`glPolygonMode`](https://registry.khronos.org/OpenGL-Refpages/gl4/html/glPolygonMode.xhtml) must be `GL_FILL`. – Rabbid76 Jan 05 '23 at 10:17
  • @Rabbid76 I am using GL_POLYGON when drawing the object, is that what you are saying? – martqqqqq Jan 05 '23 at 10:22
  • @Rabbid76 already completed with the drawing part. – martqqqqq Jan 05 '23 at 10:29

1 Answers1

0

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
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Ok that was why i wasn't getting the vertex normal vectors drawn correctly, still doesn't but now the object draws completely. Thank you. After changing that the material still doesn't aply correctly (same as in the question picture) – martqqqqq Jan 05 '23 at 10:46
  • @martqqqqq So there is another problem. However, this is not reproducible with the code in the question. Have you set `glPolygonMode` anywhere? – Rabbid76 Jan 05 '23 at 10:53
  • not really, the drawing part of the code is the base the proffesor gave us to work with, and i assume it is ok like that. – martqqqqq Jan 05 '23 at 10:56
  • @martqqqqq Sorry, but the problem is not reproducible. Try `glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);` before drawing the polygons. – Rabbid76 Jan 05 '23 at 10:57