2016-07-27 62 views
0

我使用OpenGL創建3D遊戲,並且想在窗口頂部製作一個工具欄。爲此,我嘗試使用SDL繪製按鈕和OpenGL來繪製實際的遊戲。這裏是我的代碼的相關部分:在同一個OpenGL窗口中繪製2D和3D

void openMainWindow(){ 
    SDL_Surface *screen; 
    SDL_Event event; 
    SDL_Rect position; 
    SDL_Init(SDL_INIT_VIDEO); 
    putenv("SDL_VIDEO_CENTERED=center"); 
    SDL_WM_SetCaption("Example",NULL); 
    SDL_WM_SetIcon(IMG_Load("icon.png"),NULL); 
    screen = SDL_SetVideoMode(832,487,32,SDL_HWSURFACE | SDL_OPENGL); 
    glLoadIdentity(); 
    gluPerspective(70,(double)832/487,1,1000); 
    //Some things to initialize the window 
    int continue = 1; 
    while(continue){ 
     SDL_PollEvent(&event); 
     switch(event.type){ 
      //Events 
     } 
     glMatrixMode(GL_MODELVIEW); 
     glLoadIdentity(); 
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     glBegin(GL_QUADS); 
      //Draw a square 
     glEnd(); 
     //Draw more things the same way 
     glFlush(); 
     SDL_GL_SwapBuffers(); 
     SDL_Surface *button1 = SDL_CreateRGBSurface(SDL_HWSURFACE,50,50,32,0,0,0,0); 
     SDL_FillRect(button1,NULL,SDL_MapRGB(screen->format,50,50,50); 
     position.x = 8; 
     position.y = 8; 
     SDL_BlitSurface(button1,NULL,screen,&position); 
     SDL_Flip(screen); 
    } 
    SDL_Quit(); 
} 

與此問題是,當這個功能被調用時,該過程結束,並返回圖3(這意味着有一個錯誤)。於是,我就用OpenGL繪製按鈕這樣的:

void openMainWindow(){ 
    //Everything before the while loop is the same as in the other code 
    int continue = 1; 
    while(continue){ 
     SDL_PollEvent(&event); 
     switch(event.type){ 
      //Events 
     } 
     glMatrixMode(GL_MODELVIEW); 
     glLoadIdentity(); 
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     glBegin(GL_QUADS); 
      //Draw a square 
     glEnd(); 
     //Draw more things the same way 
     glDisable(GL_DEPTH_TEST); 
     glLoadIdentity(); 
     glBegin(GL_QUADS); //Draw the button 
      glColor3ub(50,50,50); 
      glVertex2d(-0.5,-0.5); 
      glVertex2d(-0.5,0.5); 
      glVertex2d(0.5,0.5); 
      glVertex2d(0.5,-0.5); 
     glEnd(); 
     glEnable(GL_DEPTH_TEST); 
     glFlush(); 
     SDL_GL_SwapBuffers(); 
    } 
    SDL_Quit(); 
} 

我知道,第二個代碼應集中在該窗口的按鈕,但我用這個代碼只是爲了測試它是否工作(它不這就是我發佈這個問題的原因)。

隨着第二個代碼,3D的東西出現在他們應該的窗口,但我看不到任何按鈕。如何在3D OpenGL窗口中放置2D按鈕?

+0

你使用什麼類型的投影?我會切換到用於ui的拼字。 – BDL

+0

@BDL因爲我是OpenGL的初學者,所以我不太清楚你的投影是什麼意思,但是你可能會看到我在代碼中使用了什麼類型的投影。如果沒有,你可以告訴我如何找出答案。 –

+0

有一個身份投影(透視proj用'gluPerspective'設置,但隨後立即用'glLoadIdentity'丟棄)。我在第二個代碼中看不到任何明顯的錯誤(第一個代碼是另一回事 - 它沒有辦法工作),但是由於GL是一個狀態機,在給定代碼之前可能會發生很多狀態改變並影響其執行(像背面剔除)。我建議發佈說明你的問題的最小完整的例子。更好的解決方案是使用例如apitrace來調試你的問題。 – keltar

回答

0

第二個代碼的工作原理是繪製2D按鈕之前加入以下權利:

glLoadIdentity(); 
glMatrixMode(GL_PROJECTION); 
glDisable(GL_DEPTH_TEST); 
glLoadIdentity(); 

和下面的代碼繪製2D按鈕後,右:

glEnable(GL_DEPTH_TEST); 
gluPerspective(70,(double)640/480,0.5,INFINITE); //These parameters have to be the same as the ones used for gluPerspective when initializing the 3D 
gluLookAt(0,0,3,1,0,3,0,0,0.01); //Where you want to position the camera and where you want to look at 
glMatrixMode(GL_MODELVIEW); 
glLoadIdentity(); 

下面是完整的代碼,作品:

void openMainWindow(){ 
    //Everything before the while loop is the same as in the other code 
    int continue = 1; 
    while(continue){ 
     SDL_PollEvent(&event); 
     switch(event.type){ 
      //Events 
     } 
     glMatrixMode(GL_MODELVIEW); 
     glLoadIdentity(); 
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     glBegin(GL_QUADS); 
      //Draw a square 
     glEnd(); 
     //Draw more things the same way 
     glLoadIdentity(); 
     glMatrixMode(GL_PROJECTION); 
     glDisable(GL_DEPTH_TEST); 
     glLoadIdentity(); 
     glBegin(GL_QUADS); //Draw the button 
      glColor3ub(50,50,50); 
      glVertex2d(-0.5,-0.5); 
      glVertex2d(-0.5,0.5); 
      glVertex2d(0.5,0.5); 
      glVertex2d(0.5,-0.5); 
     glEnd(); 
     glEnable(GL_DEPTH_TEST); 
     gluPerspective(70,(double)640/480,0.5,INFINITE); //These parameters have to be the same as the ones used for gluPerspective when initializing the 3D 
     gluLookAt(0,0,3,1,0,3,0,0,0.01); //Where you want to position the camera and where you want to look at 
     glMatrixMode(GL_MODELVIEW); 
     glLoadIdentity(); 
     glFlush(); 
     SDL_GL_SwapBuffers(); 
    } 
    SDL_Quit(); 
}