OpenGL + GLU + C ++没有绘制任何内容

时间:2016-11-22 00:06:46

标签: c++ opengl glu

我真的因为这个问题而把头发拉了出来。我试图创建一个简单的游戏,玩家在游戏区域内滚动球。

我使用WinAPI进行窗口管理和输入处理。

我试图渲染一些简单的四边形,而不是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
    );
}

这是Ball 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();
}

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

1 个答案:

答案 0 :(得分:0)

我不得不使用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游戏编程"的示例代码中复制的。