2011-08-30 81 views
2

如何繪製2D & OpenGL中的3D材質?如果可能,請給我看一些C++代碼。 請同時顯示如何在3D對象前面繪製&。如何在OpenGL中一起繪製2D和3D材質?

+0

我試過gluPerspective畫事情3D,然後用glOrtho畫的東西在2D,但隨後3D的東西似乎覆蓋所有的二維圖紙,從而使這些2D東西閃爍! – jondinham

+0

任何解決方案?.. – jondinham

+3

請提供您現有的代碼,我們需要查看您所嘗試的內容,以便我們可以進一步幫助您。 –

回答

6

關於這樣:

void render_perspective_scene(void); 

void render_ortho_scene(void); 

void render_HUD(); 

void display() 
{ 
    float const aspect = (float)win_width/(float)win_height; 

    glViewport(0,0,win_width,win_height); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glFrustum(-aspect*near/lens, aspect*near/lens, -near/lens, near/lens, near, far); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    render_perspective_scene(); 

    // just clear the depth buffer, so that everything that's 
    // drawn next will overlay the previously rendered scene. 
    glClear(GL_DEPTH_BUFFER_BIT); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glOrtho(-aspect*scale, aspect*scale, -scale, scale, 0, 1); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    render_ortho_scene(); 

    // Same for the HUD, only that we render 
    // that one in pixel coordinates. 
    glClear(GL_DEPTH_BUFFER_BIT); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glOrtho(0, win_width, 0, win_height, 0, 1); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    render_HUD(); 
} 
5

你在正確的軌道上,但是您繪製2D圖形時,還必須禁用深度測試:glDisable(GL_DEPTH_TEST);

否則可能會被你的3D圖形變成隱藏。

+0

只有當我們在3D場景背後繪製東西時,diable depth test纔有意義? – jondinham