2016-11-22 109 views
-1

我真的把我的頭髮拉出這個問題。我試圖創建一個簡單的遊戲,玩家在遊戲區域周圍擲球。OpenGL + GLU + C++不繪任何東西

我使用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 
    ); 
} 

這裏的球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(); 
} 

這個代碼中#!@%是錯誤的嗎?我應該在一個星期內完成這件事情,所以我真的可以使用一些幫助...

回答

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); 
} 

的代碼是從戴夫阿瑟爾的書「的OpenGL遊戲編程」的示例代碼複製。