简体   繁体   中英

openGL gl_Polygon mix color issue

I want to draw two rectangle one on left and one on right with 2 different colors, but after I draw with two GL_Polygon . the image turn out to be this, can anyone help me out? Thank You.

{
    GLfloat rectangleB[5][3]={{10,0,0},{10,30,0},{5,30,0},{1,30,0},{1,0,0}};
    GLfloat rectangleW[5][3]={{0,0,0},{0,30,0},{-5,30,0},{-10,30,0},{-10,0,0}};
    GLfloat rectangleY[5][3]={{8,2,0},{5,28,0},{0,28,0},{-8,28,0},{-8,2,0}};

    void draw_Rectangle(void){

    glBegin(GL_POLYGON);
    for(int i=0;i<5;i++){
        glVertex3fv(rectangleB[i]);
        glColor3f(0.0, 0.0, 1.0);
    }
    glEnd();

    glBegin(GL_POLYGON);
    for(int i=0;i<5;i++){
        glVertex3fv(rectangleW[i]);
        glColor3f(1.0, 1.0, 1.0);
    }
    glEnd();


    glFlush();
}

this is output image

this is the image I want

First of all note, that drawing by glBegin / glEnd sequences is deprecated since several years. Read about Fixed Function Pipeline and see Vertex Specification and Shader for a state of the art way of rendering.


Anyway, glVertex is used to specifies the next vertex coordinate within a glBegin / glEnd sequence.
When glVertex is called, then the current color attribute ( glColor ) is associated with the vertex coordinate.

This means you have to set the color before you specify the vertex coordinate. Swap glColor3f and glVertex3fv to solve your issue:

glBegin(GL_POLYGON);
for(int i=0;i<5;i++){
    glColor3f(0.0, 0.0, 1.0);
    glVertex3fv(rectangleB[i]);
}
glEnd();

glBegin(GL_POLYGON);
for(int i=0;i<5;i++){
    glColor3f(1.0, 1.0, 1.0);
    glVertex3fv(rectangleW[i]);
}
glEnd();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM