OpenGL 项目 - 对象在移动时不保持填充颜色

OpenGL Project - objects not keeping filling color on movement

当我执行代码时,我得到了一个由三个元素组成的热气球。我的问题是,当我从键盘上移动对象时,对象颜色变淡,变得更像电线而不是实体。

从我发现到现在,我的麻烦来自这个函数调用:

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)

但我需要它来制作绳索...

/*
This program shows a hot air balloon rotating around its own axe to the left and to the right
*/
#include "glos.h"
#include<math.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>

void myinit(void);
void CALLBACK display(void);
void CALLBACK myReshape(GLsizei w, GLsizei h);
void CALLBACK rotateRight(void);
void CALLBACK rotateLeft(void);

static GLfloat  x = 0;
static GLfloat  y = 0;
static GLfloat  z = 0;
static GLfloat alfa = 0;
double PI = 3.14159265;

void myinit (void) {
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
}
void CALLBACK rotateLeft(void)  {   y -=  5; }

void CALLBACK rotateRight(void) {   y += 5;  }

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

     glRotatef(x, 1, 0, 0);
     glRotatef(y, 0, 1, 0);
     glTranslatef(0, -80, 0);
       //cube = basket
        glColor3f(1, 1, 0);
        auxSolidCube(50);
        
       //full sphere = baloon
        glPushMatrix();
        glColor3f(1.0, 0.0, 0.0);
        glTranslatef(0,200,0);
        glRotatef(-90, 1, 0, 0);
        auxSolidSphere(130.0);
        glPopMatrix();

        //polygon cylinder = ropes
        glPushMatrix();
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
            glBegin(GL_QUAD_STRIP);
            glColor3f(0.0, 1.0, 1.0);
            for (alfa = 0; alfa <= 360; alfa+=30) {
                glVertex3f(65 * sin((PI * alfa) / 180), 100, 65 * cos((PI * alfa) / 180));//top of the cylinder
                glVertex3f(15 * sin((PI * alfa) / 180),0, 15 * cos((PI * alfa) / 180));//base of the cylinder     
            }
            glEnd();        
        glPopMatrix();
        
    glPopMatrix();
    glFlush();
}

void CALLBACK myReshape(GLsizei w, GLsizei h)   
{
    if (!h) return;
    glViewport(0, 0, w, h); 
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (w <= h)     
    glOrtho (-400,400, -400 *(GLfloat)h / (GLfloat)w, +400.0*(GLfloat)h/(GLfloat)w, -1000.0, 1000.0);
    else 
    glOrtho (-400*(GLfloat)w / (GLfloat)h, 400.0*(GLfloat)w/(GLfloat)h, -400, 400.0, -1000.0, 1000.0);
    glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv)
{
    auxInitDisplayMode (AUX_SINGLE | AUX_RGB | AUX_DEPTH);
    auxInitPosition (100, 0, 600, 400);
    auxInitWindow ("Hot air balloon");
    myinit ();
    auxKeyFunc(AUX_RIGHT, rotateRight);
    auxKeyFunc(AUX_LEFT, rotateLeft);
    auxReshapeFunc (myReshape);
    auxMainLoop(display);
    return(0);
}

OpenGL 是一个状态引擎。一旦设置了状态,它就会一直保留直到再次更改,甚至超出帧。因此,在渲染实体几何之前需要设置多边形模式GL_FILL

void CALLBACK display (void)
{
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

    // render solid geometry
    // [...]
    
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

    // render wireframe geometry
    // [...]
}