OpenGL + GLU + C++ 不绘制任何东西

OpenGL + GLU + C++ not drawing anything

我真的被这个问题搞得焦头烂额了。我正在尝试创建一个简单的游戏,玩家可以在游戏区域周围滚动球。

我正在使用 WinAPI 进行 window 管理和输入处理。

我也尝试渲染一些简单的四边形,而不是 GLU 球体,但这也不起作用。

我已将代码分开 类。我在下面提供相关代码。此代码在我的 WinMain 中:

while (running) {

    PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);

    if (msg.message == WM_QUIT)
        running = false;

    else {

        // handle key presses

        // update
        gameWorld->update(getDirections());

        // render
        gameWorld->render(deviceContext);

        // I added this block of code for testing, still does not work
        glColor4f(1, 1, 1, 1);
        glBegin(GL_QUADS);
        glVertex3f(10, 10, 0);
        glVertex3f(10, -10, 0);
        glVertex3f(-10, -10, 0);
        glVertex3f(-10, 10, 0);
        glEnd();

        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

这里是GameWorld.cpp:

GameWorld::GameWorld()
{
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    this->ball = new Ball(1, 10, 10);
    this->camera = new Camera(ball);
}

GameWorld::~GameWorld()
{
    delete this->ball;
}

void GameWorld::render(HDC deviceContext) {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    this->ball->draw();

    SwapBuffers(deviceContext);
}

void GameWorld::update(Directions dirs) {

    glLoadIdentity();

    this->ball->handleInput(dirs);
    this->ball->update();

    this->camera->update();
}

这是相机 update 方法:

void Camera::update() {

    GLdouble ballX = ball->getLocation()->getX();
    GLdouble ballY = ball->getLocation()->getY();
    GLdouble ballZ = ball->getLocation()->getZ();

    GLdouble x = ballX + cos(90) * this->distanceFromBall;
    GLdouble y = ballY + cos(90) * this->distanceFromBall;
    GLdouble z = ballZ + cos(90) * this->distanceFromBall;

    gluLookAt(
        x, y, z,
        ballX, ballY, ballZ, 
        0, 1, 0
    );
}

这是球 draw 方法:

void Ball::draw() {

    glPushMatrix();
    this->quadric = gluNewQuadric();

    glTranslated(this->location->getX(), this->location->getY(), this->location->getZ());

    gluQuadricDrawStyle(this->quadric, GLU_FILL);
    glColor4f(1, 1, 1, 1);

    gluSphere(this->quadric, this->radius, this->slices, this->stacks);
    gluDeleteQuadric(this->quadric);

    glPopMatrix();
}

这段代码 #!@% 有什么问题?我应该在一周内完成这件事,所以我真的需要一些帮助...

我不得不使用 gluPerspective() 函数来完成这项工作。我的 GameWorld 构造函数现在看起来像这样:

GameWorld::GameWorld()
{
    glViewport(0, 0, WIDTH, HEIGHT);        // reset the viewport to new dimensions
    glMatrixMode(GL_PROJECTION);            // set projection matrix current matrix
    glLoadIdentity();                       // reset projection matrix

                                            // calculate aspect ratio of window
    gluPerspective(54.0f, (GLfloat)WIDTH / (GLfloat)HEIGHT, 1.0f, 1000.0f);

    glMatrixMode(GL_MODELVIEW);             // set modelview matrix
    glLoadIdentity();

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    this->ball = new Ball(1, 20, 20);
    this->camera = new Camera(ball);
}

代码是从Dave Astle的书"OpenGL Game Programming".

中复制的示例代码