2016-07-27 14 views
0

に2Dと3Dを描画します。そのために、SDLを使ってボタンを描き、OpenGLを使って実際のゲームを描こうとしました。私は、OpenGLで3Dゲームを作成していると私は、ウィンドウの上部にツールバーをしたいと思い、同じOpenGLのウィンドウ

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

私は2番目のコードは、ウィンドウ内のボタンを中央にすべきであることを知っているが、私はそれが動作するかどうかだけで、それがない(テストするには、このコードを使用します、それが私がこの質問を投稿している理由です)。第二の符号付き

、彼らが必要として、3Dのものがウィンドウに表示されますが、私はいずれかのボタンを見ることができません。 3Dボタンを3D OpenGLウィンドウに配置するにはどうすればよいですか?

+0

あなたは投影のどのようなタイプを使用していますか?私はあなたのために正書法に切り替えます。 – BDL

+0

@BDL私はOpenGLの初心者なので、プロジェクションで何を意味するのかは分かりませんが、コードでどのようなプロジェクションを使用しているのかは分かります。そうでない場合は、私にどのように見つけ出すか教えてください。 –

+0

IDプロジェクションがあります(プロジェクションprojは 'gluPerspective'で設定されましたが、すぐに' glLoadIdentity'でドロップされました)。私は2番目のコードではっきりと間違いがないことを見ています(最初のものは別の話です - 動作する方法はありません).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(); 
} 
関連する問題