2012-03-28 66 views
-2

我在OpenGL中使用C++在dev-C++ IDE中。 當我畫一個圓圈時,沒有任何反應,什麼可能是錯的?OpenGL沒有繪圖發生

int WINAPI WinMain (HINSTANCE hInstance, 
        HINSTANCE hPrevInstance, 
        LPSTR lpCmdLine, 
        int iCmdShow) 
{ 
    WNDCLASS wc; 
    HWND hWnd; 
    HDC hDC; 
    HGLRC hRC;   
    MSG msg; 
    BOOL bQuit = FALSE; 
    float theta = 0.0f; 

    /* register window class */ 
    wc.style = CS_OWNDC; 
    wc.lpfnWndProc = WndProc; 
    wc.cbClsExtra = 0; 
    wc.cbWndExtra = 0; 
    wc.hInstance = hInstance; 
    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION); 
    wc.hCursor = LoadCursor (NULL, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH); 
    wc.lpszMenuName = NULL; 
    wc.lpszClassName = "GLSample"; 
    RegisterClass (&wc); 

    /* create main window */ 
    hWnd = CreateWindow (
     "GLSample", "OpenGL Sample", 
     WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE, 
     0, 0, 768, 512, 
     NULL, NULL, hInstance, NULL); 

    /* enable OpenGL for the window */ 
    EnableOpenGL (hWnd, &hDC, &hRC); 

    /* program main loop */ 
    while (!bQuit) 
    { 
     /* check for messages */ 
     if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) 
     { 
      /* handle or dispatch messages */ 
      if (msg.message == WM_QUIT) 
      { 
       bQuit = FALSE; 
      } 
      else 
      { 
       TranslateMessage (&msg); 
       DispatchMessage (&msg); 
      } 
     } 
     else 
     { 
      /* OpenGL animation code goes here */ 

      glClearColor (0.0f, 0.0f, 0.0f, 0.0f); 
      glClear (GL_COLOR_BUFFER_BIT); 

      glPushMatrix(); 

      circle(30,30,20,3); 


      glPopMatrix(); 

      SwapBuffers (hDC); 

     } 
    } 

    /* shutdown OpenGL */ 
    DisableOpenGL (hWnd, hDC, hRC); 

    /* destroy the window explicitly */ 
    DestroyWindow (hWnd); 

    return msg.wParam; 
} 
void circle(float x, float y, float r, int segments) 
{ 
    glBegin(GL_TRIANGLE_FAN); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); 
     glVertex2f(x, y); 
     for(int n = 0; n <= segments; ++n) { 
      float const t = 2*M_PI*(float)n/(float)segments; 
      glVertex2f(x + sin(t)*r, y + cos(t)*r); 
     } 
    glEnd(); 
} 

回答

2

首先,您不能將受限制的調用放入glBegin()塊中。 (glMatrixMode,glLoadIdentity,glOrtho等)。你應該開始檢查代碼中幾處glGetError的opengl錯誤。如果你有任何錯誤報告(你當然會這麼做),那應該是任何調試過程中的第一步。

而且熟悉的手冊頁每個命令,他們可以幫助你瞭解如何使用它們:

http://www.opengl.org/sdk/docs/man/xhtml/glBegin.xml