基本的OpenGL渲染问题?

时间:2013-12-29 01:35:32

标签: c++ opengl render glut freeglut

我是OpenGL的初学者,只是想学习(暂时从freeglut开始)。到目前为止,我有以下代码,应该绘制一些基本的3D对象。问题在于,无论我放入渲染函数(尽管它确实执行),它只显示一个空白窗口。

#include "stdafx.h"
#include <iostream>
#include "dependente\glew\glew.h"
#include "dependente\freeglut\glut.h"

void render()
{
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glEnable(GL_DEPTH_TEST);

    glTranslatef(-1.5f, 1.0f, -6.0f);                                     // Translate back and to the left

    glPushMatrix();                                                         // Push the current modelview matrix on the matrix                         // Rotate on all 3 axis
    glBegin(GL_TRIANGLES);                                                // Draw a pyramid
    glColor3f(1.0f, 0.0f, 0.0f);                                      // Red
    glVertex3f(0.0f, 1.0f, 0.0f);                                     // Top of front face
    glColor3f(0.0f, 1.0f, 0.0f);                                      // Green
    glVertex3f(-1.0f, -1.0f, 1.0f);                                   // Left of front face
    glColor3f(0.0f, 0.0f, 1.0f);                                      // Blue
    glVertex3f(1.0f, -1.0f, 1.0f);                                    // Right of front face

    glColor3f(1.0f, 0.0f, 0.0f);                                      // Red
    glVertex3f(0.0f, 1.0f, 0.0f);                                     // Top of right face
    glColor3f(0.0f, 0.0f, 1.0f);                                      // Blue
    glVertex3f(1.0f, -1.0f, 1.0f);                                    // Left of right face
    glColor3f(0.0f, 1.0f, 0.0f);                                      // Green
    glVertex3f(1.0f, -1.0f, -1.0f);                                   // Right of right face

    glColor3f(1.0f, 0.0f, 0.0f);                                      // Red
    glVertex3f(0.0f, 1.0f, 0.0f);                                     // Top of back face
    glColor3f(0.0f, 1.0f, 0.0f);                                      // Green
    glVertex3f(1.0f, -1.0f, -1.0f);                                   // Left of back face
    glColor3f(0.0f, 0.0f, 1.0f);                                      // Blue
    glVertex3f(-1.0f, -1.0f, -1.0f);                                  // Right of back face

    glColor3f(1.0f, 0.0f, 0.0f);                                      // Red
    glVertex3f(0.0f, 1.0f, 0.0f);                                     // Top of left face
    glColor3f(0.0f, 0.0f, 1.0f);                                      // Blue
    glVertex3f(-1.0f, -1.0f, -1.0f);                                  // Left of left face
    glColor3f(0.0f, 1.0f, 0.0f);                                      // Green
    glVertex3f(-1.0f, -1.0f, 1.0f);                                   // Right of left face
    glEnd();

    // Render a quad for the bottom of our pyramid
    glBegin(GL_QUADS);
    glColor3f(0.0f, 1.0f, 0.0f);                                      // Green
    glVertex3f(-1.0f, -1.0f, 1.0f);                                   // Left/right of front/left face
    glColor3f(0.0f, 0.0f, 1.0f);                                      // Blue
    glVertex3f(1.0f, -1.0f, 1.0f);                                    // Right/left of front/right face
    glColor3f(0.0f, 1.0f, 0.0f);                                      // Green
    glVertex3f(1.0f, -1.0f, -1.0f);                                   // Right/left of right/back face
    glColor3f(0.0f, 0.0f, 1.0f);                                      // Blue
    glVertex3f(-1.0f, -1.0f, -1.0f);                                  // Left/right of right/back face
    glEnd();
    glPopMatrix();

    glTranslatef(3.0f, 0.0f, 0.0f);                                        // Translate right
    glPushMatrix();                                                         // Push the current modelview matrix on the matrix stack                         // Rotate the primitive on all 3 axis
    glBegin(GL_QUADS);
    // Top face
    glColor3f(0.0f, 1.0f, 0.0f);                                   // Green
    glVertex3f(1.0f, 1.0f, -1.0f);                                   // Top-right of top face
    glVertex3f(-1.0f, 1.0f, -1.0f);                                   // Top-left of top face
    glVertex3f(-1.0f, 1.0f, 1.0f);                                   // Bottom-left of top face
    glVertex3f(1.0f, 1.0f, 1.0f);                                   // Bottom-right of top face

    // Bottom face
    glColor3f(1.0f, 0.5f, 0.0f);                                  // Orange
    glVertex3f(1.0f, -1.0f, -1.0f);                                  // Top-right of bottom face
    glVertex3f(-1.0f, -1.0f, -1.0f);                                  // Top-left of bottom face
    glVertex3f(-1.0f, -1.0f, 1.0f);                                  // Bottom-left of bottom face
    glVertex3f(1.0f, -1.0f, 1.0f);                                  // Bottom-right of bottom face

    // Front face
    glColor3f(1.0f, 0.0f, 0.0f);                                  // Red
    glVertex3f(1.0f, 1.0f, 1.0f);                                  // Top-Right of front face
    glVertex3f(-1.0f, 1.0f, 1.0f);                                  // Top-left of front face
    glVertex3f(-1.0f, -1.0f, 1.0f);                                  // Bottom-left of front face
    glVertex3f(1.0f, -1.0f, 1.0f);                                  // Bottom-right of front face

    // Back face
    glColor3f(1.0f, 1.0f, 0.0f);                                 // Yellow
    glVertex3f(1.0f, -1.0f, -1.0f);                                 // Bottom-Left of back face
    glVertex3f(-1.0f, -1.0f, -1.0f);                                 // Bottom-Right of back face
    glVertex3f(-1.0f, 1.0f, -1.0f);                                 // Top-Right of back face
    glVertex3f(1.0f, 1.0f, -1.0f);                                 // Top-Left of back face

    // Left face
    glColor3f(0.0f, 0.0f, 1.0f);                                   // Blue
    glVertex3f(-1.0f, 1.0f, 1.0f);                                   // Top-Right of left face
    glVertex3f(-1.0f, 1.0f, -1.0f);                                   // Top-Left of left face
    glVertex3f(-1.0f, -1.0f, -1.0f);                                   // Bottom-Left of left face
    glVertex3f(-1.0f, -1.0f, 1.0f);                                   // Bottom-Right of left face

    // Right face
    glColor3f(1.0f, 0.0f, 1.0f);                                   // Violet
    glVertex3f(1.0f, 1.0f, 1.0f);                                   // Top-Right of left face
    glVertex3f(1.0f, 1.0f, -1.0f);                                   // Top-Left of left face
    glVertex3f(1.0f, -1.0f, -1.0f);                                   // Bottom-Left of left face
    glVertex3f(1.0f, -1.0f, 1.0f);                                   // Bottom-Right of left face
    glEnd();
    glPopMatrix();

    glTranslatef(-1.5f, -3.0f, 0.0f);                                     // Back to center and lower screen
    glPushMatrix();
    glColor3f(1.0f, 1.0f, 0.0f);                                          // Yellow
    glutSolidSphere(1.0f, 16, 16);                                        // Use GLUT to draw a solid sphere
    glScalef(1.01f, 1.01f, 1.01f);
    glColor3f(1.0f, 0.0f, 0.0f);                                          // Red
    glutWireSphere(1.0f, 16, 16);                                         // Use GLUT to draw a wireframe sphere
    glPopMatrix();
}

void initGlut(int argc, char* argv[]) {
    std::cout << "Initialise OpenGL..." << std::endl;

    glutInit(&argc, argv);
    int iScreenWidth = glutGet(GLUT_SCREEN_WIDTH);
    int iScreenHeight = glutGet(GLUT_SCREEN_HEIGHT);

    glutInitDisplayMode(GLUT_RGBA | GLUT_ALPHA | GLUT_DOUBLE | GLUT_DEPTH);

    glutInitWindowPosition(120, 120);
    glutInitWindowSize(600, 600);

    glutCreateWindow("OpenGL");

    // Register GLUT callbacks
    glutDisplayFunc(render);

    // Setup initial GL State
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClearDepth(1.0f);

    glShadeModel(GL_SMOOTH);
    glutMainLoop();
    std::cout << "Initialise OpenGL: Success!" << std::endl;
}

int _tmain(int argc, char* argv[])
{
    initGlut(argc, argv);
    return 0;
}

希望有经验的人会让我知道我错过了哪些显而易见的东西。

1 个答案:

答案 0 :(得分:3)

以下是我如何调试问题“OpenGL没有绘制任何东西”:

  1. 将此代码添加到render()函数的开头:glClearColor (1, 1, 0, 1); glClear (GL_COLOR_BUFFER_BIT);如果输出变为黄色,则调用render()函数并正确清除输出。然后,您可以删除该代码或将其注释掉。如果输出没有变黄,那么你的render()函数没有被调用,或者它被调用,但你的OpenGL状态被设置为不抽取到屏幕。 (也许当时错误的背景是当前的,或者当前FBO的颜色附件不是你想象的那样。)

  2. 尝试绘制一个没有纹理或着色器的白色三角形,以原点为中心。如果它出现,那么您尝试绘制的其他几何图形可能是错误的。如果它没有显示,问题可能是你的矩阵计算(投影或模型视图矩阵)。 (您是否将“相机”指向您认为的位置?您的物体是在您想到的地方绘制的吗?)它也可以是照明,混合或深度测试。为了确保这一点,我把所有这些都关掉了。 (请参阅glEnable() / glDisable()了解如何开启和关闭它们。)

  3. 如果这些东西有效,我会开始打开上面关闭的东西:纹理,着色器,灯光,混合,深度测试。我一次打开一个,直到出现问题。

  4. 如果没有出错,那么我的对象的几何图形可能是错误的。