glColor3f 不适用于 gluSphere

glColor3f not working on gluSphere

我目前正在我的 3d 中渲染一个太阳系的行星 space 但是即使光线照射到球体上,每个行星都是黑色的。球体在我的渲染函数中最后渲染。如果球体单独渲染时颜色有效,但现在我添加了我的天空盒和其他四边形,所有球体都拒绝着色。

    #include "Scene.h"

float rotation;
float rotation2;
int direction;
int speed;




Scene::Scene(Input *in)
{
    // Initialise variables
    rotation = 20;
    rotation2 = 0;
    direction = 1;
    speed = 5;
    myTexture = 0;
    skyBox = 0;

    // Store pointer for input class
    input = in;

    // OpenGL settings
    glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
    glClearColor(0.39f, 0.58f, 93.0f, 1.0f);            // Cornflour Blue Background
    glClearDepth(1.0f);                                 // Depth Buffer Setup
    glClearStencil(0);                                  // Clear stencil buffer
    glEnable(GL_DEPTH_TEST);                            // Enables Depth Testing
    glEnable(GL_LIGHTING);                              // Enables Lighting
    glDepthFunc(GL_LEQUAL);                             // The Type Of Depth Testing To Do
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Really Nice Perspective Calculations

    // Other OpenGL / render setting should be applied here.
    myTexture = SOIL_load_OGL_texture
        (
            "gfx/neongrid.png",
            SOIL_LOAD_AUTO,
            SOIL_CREATE_NEW_ID,
            SOIL_FLAG_MIPMAPS | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
            );
    skyBox = SOIL_load_OGL_texture
        (
            "gfx/starField.png",
            SOIL_LOAD_AUTO,
            SOIL_CREATE_NEW_ID,
            SOIL_FLAG_MIPMAPS | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
            );
    glEnable(GL_TEXTURE_2D);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
        GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
        GL_NEAREST);

    camera = new Camera();
}

void Scene::update(float dt)
{
    // Update camera position
    camera->update(input, dt);

    // Handle user input
        if (input->isKeyDown(43))
        {
            direction = 1;
            input->SetKeyUp(43);
        }
        else if (input->isKeyDown(45))
        {
            direction = -1;
            input->SetKeyUp(45);
        }

    // Update scene related variables
        rotation += speed * dt;
        rotation2 += (speed *2) * dt;

        if (input->isKeyDown('p') && WF == false)
        {
            WF = true;
            input->SetKeyUp('p');
            glPolygonMode(GL_FRONT, GL_LINE);
        }
        if (input->isKeyDown('p') && WF == true)
        {
            WF = false;
            input->SetKeyUp('p');
            glPolygonMode(GL_FRONT, GL_FILL);
        }

    // Calculate FPS for output
    calculateFPS();
}

void Scene::render() {


    // Clear Color and Depth Buffers
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Reset transformations
    glLoadIdentity();
    // Set the camera
    gluLookAt(camera->getPosition().x, camera->getPosition().y, camera->getPosition().z,
        camera->getLookAt().x, camera->getLookAt().y, camera->getLookAt().z,
        camera->getUp().x, camera->getUp().y, camera->getUp().z);

    glutWarpPointer(400, 300);
    glutSetCursor(GLUT_CURSOR_FULL_CROSSHAIR);

    // Lighting
    GLfloat Light_Ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
    GLfloat Light_Diffuse[] = { 9.0f, 9.0f, 9.0f, 1.0f };
    GLfloat Light_Position[] = { 2.0f, 2.0f, 2.0f, 1.0f };


    glLightfv(GL_LIGHT0, GL_AMBIENT, Light_Ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, Light_Diffuse);
    glLightfv(GL_LIGHT0, GL_POSITION, Light_Position);


    glEnable(GL_LIGHT0);
    glDisable(GL_DEPTH_TEST);

#pragma region skybox 
    glPushMatrix();

    glTranslatef(camera->getPosition().x, camera->getPosition().y, camera->getPosition().z);
    glBindTexture(GL_TEXTURE_2D, skyBox);
    glBegin(GL_QUADS);
    //Back
    glNormal3f(0.0f, 0.0f, 1.0f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);

    glNormal3f(0.0f, 0.0f, 1.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);

    glNormal3f(0.0f, 0.0f, 1.0f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);

    glNormal3f(0.0f, 0.0f, 1.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);

    //Right 
    glNormal3f(1.0f, 0.0f, 0.0f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, -1.0f);

    glNormal3f(1.0f, 0.0f, 0.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);

    glNormal3f(1.0f, 0.0f, 0.0f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);

    glNormal3f(1.0f, 0.0f, 0.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);

    //front
    glNormal3f(0.0f, 0.0f, -1.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, -1.0f);

    glNormal3f(0.0f, 0.0f, -1.0f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);

    glNormal3f(0.0f, 0.0f, -1.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-1.0f, 1.0f, -1.0f);

    glNormal3f(0.0f, 0.0f, -1.0f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);

    //left
    glNormal3f(-1.0f, 0.0f, 0.0f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);

    glNormal3f(-1.0f, 0.0f, 0.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);

    glNormal3f(-1.0f, 0.0f, 0.0f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);

    glNormal3f(-1.0f, 0.0f, 0.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-1.0f, 1.0f, -1.0f);

    //top
    glNormal3f(0.0f, 1.0f, 0.0f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);

    glNormal3f(0.0f, 1.0f, 0.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);

    glNormal3f(0.0f, 1.0f, 0.0f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);

    glNormal3f(0.0f, 1.0f, 0.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-1.0f, 1.0f, -1.0f);

    //bottom
    glNormal3f(0.0f, -1.0f, 0.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);

    glNormal3f(0.0f, -1.0f, 0.0f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, -1.0f);

    glNormal3f(0.0f, -1.0f, 0.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);

    glNormal3f(0.0f, -1.0f, 0.0f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);

    glEnd();
#pragma endregion

    glEnable(GL_DEPTH_TEST);

    glPopMatrix();
    glPushMatrix();
#pragma region wall
    glBindTexture(GL_TEXTURE_2D, myTexture);
    glTranslatef(0.0, 0.0, 0.0);
    glScalef(5.0f, 5.0f, 5.0f);
    glBegin(GL_QUADS);
    // first face
    glNormal3f(1.0f, 0.0f, 0.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
    // second face
    glNormal3f(1.0f, 0.0f, 0.0f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(1.0f, 1.0f, 0.0f);
    // third face
    glNormal3f(1.0f, 0.0f, 0.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(1.0f, 0.0f, 0.0f);
    // fourth face
    glNormal3f(1.0f, 0.0f, 0.0f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glEnd();

    glPopMatrix();
    glPushMatrix();
    glBindTexture(GL_TEXTURE_2D, myTexture);
    glScalef(5.0f, 5.0f, 5.0f);
    glBegin(GL_QUADS);
    // first face
    glNormal3f(1.0f, 0.0f, 0.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(0.0f, 0.0f, 0.0f);
    // second face
    glNormal3f(1.0f, 0.0f, 0.0f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(1.0f, 0.0f, 0.0f);
    // third face
    glNormal3f(1.0f, 0.0f, 0.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(1.0f, 0.0f, -1.0f);
    // fourth face
    glNormal3f(1.0f, 0.0f, 0.0f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(0.0f, 0.0f, -1.0f);
    glEnd();
#pragma endregion
    glPopMatrix();
    glPushMatrix();

    // Render sun
    glEnable(GL_TEXTURE_2D);
    glColor3f(1.0f, 1.0f, 1.0f);
    glBindTexture(GL_TEXTURE_2D, NULL);
    glTranslatef(0.5, 0.5, -0.5);
    glColor3f(1.0f, 0.0f, 0.0f);
    gluSphere(gluNewQuadric(), 0.20, 20, 20);
    glPushMatrix(); // Push for default matrix

                    // Render Planet 1
    glRotatef(rotation, 0.5, 0.5, 0);
    glTranslatef(0, 0, 1);
    glScalef(1, 1, 1);
    glColor3f(0.0f, 2.0f, 0.0f);
    gluSphere(gluNewQuadric(), 0.20, 10, 10);
    glPopMatrix(); // Pop off stack back to sun matrix
    glPushMatrix(); // Push for default matrix

                    // Render Planet 2
    glRotatef(rotation2, 0, 1, 0);
    glTranslatef(2, 0, 0);
    glScalef(0.5, 0.5, 0.5);
    glColor3f(0.0f, 0.0f, 1.0f);
    gluSphere(gluNewQuadric(), 0.20, 5, 5);
    glPushMatrix(); // Pop back to sun

                    // Render a moon around Planet 2
    glRotatef((rotation*2.0), 0, 1, 0);
    glTranslatef(1.5, 0, 0);
    glScalef(0.3, 0.3, 0.3);
    glColor3f(0.0f, 0.0f, 1.0f);
    gluSphere(gluNewQuadric(), 0.20, 20, 20);
    glPopMatrix(); // Pop to planet 2
    glPushMatrix(); // Push for default matrix

                    // Render a SECOND moon around Planet 2
    glRotatef((rotation * 2), 0, 0, 1);
    glTranslatef(1.5, 0, 0);
    glScalef(0.3, 0.3, 0.3);
    glColor3f(0.0f, 0.0f, 1.0f);
    gluSphere(gluNewQuadric(), 0.20, 20, 20);
    glPopMatrix();
    //glPopMatrix();
    glPopMatrix(); // Go back to sun!
    glPushMatrix();

    glTranslatef(2, 2, 2);
    glColor3f(1, 0, 0);
    gluSphere(gluNewQuadric(), 0.5, 20, 20);

    glPopMatrix();

    // End render geometry --------------------------------------

    // Render text, should be last object rendered.
    renderTextOutput();

    // Swap buffers, after all objects are rendered.
    glutSwapBuffers();
}

// Handles the resize of the window. If the window changes size the perspective matrix requires re-calculation to match new window size.
void Scene::resize(int w, int h) 
{
    width = w;
    height = h;
    // Prevent a divide by zero, when window is too short
    // (you cant make a window of zero width).
    if (h == 0)
        h = 1;

    float ratio = (float)w / (float)h;
    fov = 45.0f;
    nearPlane = 0.1f;
    farPlane = 100.0f;

    // Use the Projection Matrix
    glMatrixMode(GL_PROJECTION);

    // Reset Matrix
    glLoadIdentity();

    // Set the viewport to be the entire window
    glViewport(0, 0, w, h);

    // Set the correct perspective.
    gluPerspective(fov, ratio, nearPlane, farPlane);

    // Get Back to the Modelview
    glMatrixMode(GL_MODELVIEW);


}

// Calculates FPS
void Scene::calculateFPS()
{

    frame++;
    time = glutGet(GLUT_ELAPSED_TIME);

    if (time - timebase > 1000) {
        sprintf_s(fps, "FPS: %4.2f", frame*1000.0 / (time - timebase));
        timebase = time;
        frame = 0;
    }
}

// Compiles standard output text including FPS and current mouse position.
void Scene::renderTextOutput()
{
    // Render current mouse position and frames per second.
    sprintf_s(mouseText, "Mouse: %i, %i", input->getMouseX(), input->getMouseY());
    displayText(-1.f, 0.96f, 1.f, 0.f, 0.f, mouseText);
    displayText(-1.f, 0.90f, 1.f, 0.f, 0.f, fps);
}

// Renders text to screen. Must be called last in render function (before swap buffers)
void Scene::displayText(float x, float y, float r, float g, float b, char* string) {
    // Get Lenth of string
    int j = strlen(string);

    // Swap to 2D rendering
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0, 1.0, -1.0, 1.0, 5, 100);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    // Orthographic lookAt (along the z-axis).
    gluLookAt(0.0f, 0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

    // Set text colour and position.
    glColor3f(r, g, b);
    glRasterPos2f(x, y);
    // Render text.
    for (int i = 0; i < j; i++) {
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, string[i]);
    }
    // Reset colour to white.
    glColor3f(1.f, 1.f, 1.f);

    // Swap back to 3D rendering.
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(fov, ((float)width/(float)height), nearPlane, farPlane);
    glMatrixMode(GL_MODELVIEW);
}

更新太阳代码:

// Render sun
glTranslatef(0.5, 0.5, -0.5);
glColor3f(1.0f, 0.0f, 0.0f);
gluSphere(gluNewQuadric(), 0.20, 20, 20);
glPushMatrix(); // Push for default matrix
                // Render Planet 1
glRotatef(rotation, 0.5, 0.5, 0);
glTranslatef(0, 0, 1);
glScalef(1, 1, 1);
glColor3f(0.0f, 2.0f, 0.0f);
gluSphere(gluNewQuadric(), 0.20, 10, 10);
glPopMatrix(); // Pop off stack back to sun matrix
glPushMatrix(); // Push for default matrix
  1. 尝试结束glEnable(GL_TEXTURE_2D);与 glDisable(GL_TEXTURE_2D);在渲染太阳之后。
  2. 如果添加天空盒后出现问题。检查顶点的顺序是否正确。

发现我缺少 OpenGL 设置

glEnable(GL_COLOR_MATERIAL);

在场景开始处插入