2010-11-26 9 views
5

Estoy trabajando en un proyecto que requiere que cree una interfaz 2D renderizada "en la parte superior" o en un mundo 3D. En algunos otros foros, leí que podría usar "GluOrtho2D()" para el trabajo, y volver a GluPerspective() una vez que haya terminado. Lo único es que mi código de prueba que escribí solo muestra el mundo 3D, no el quad 2D. Sin embargo, cuando deshabilito el código de representación 3D, el quad aparece donde debería estar. Recorté el código para abrir solo las declaraciones, que anoté a continuación. El código está escrito en Python usando la biblioteca Pyglet.Problema al crear una interfaz 2D en un mundo 3D OpenGL


El código de inicialización escena: código de representación

glViewport(0, 0, width, height) 
glMatrixMode(GL_PROJECTION) 
glLoadIdentity() 
gluPerspective(60.0, float(width)/height, .1, 10000.) 
glMatrixMode(GL_MODELVIEW) 
glClearDepth(1.0) 
glShadeModel(GL_FLAT) 
glEnable(GL_DEPTH_TEST) 
glDepthFunc(GL_LEQUAL) 
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) 

El bastidor. La llamada a builder.buildMap() crea la escena 3D, y el par glBegin-glEnd dibuja un quad 2D:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT) 
stage.set3DMode(window.width, window.height) 
builder.buildMap() 
stage.set2DMode(window.width, window.height) 
glBegin (GL_QUADS) 
glVertex2i(0, 0) 
glVertex2i(0, 200) 
glVertex2i(200, 200) 
glVertex2i(200, 0) 
glEnd() 

la función stage.set3DMode:

glDisable(GL_DEPTH_TEST) 
glMatrixMode(GL_PROJECTION) 
glLoadIdentity() 
gluOrtho2D(0, width, 0, height) 
glMatrixMode(GL_MODELVIEW) 

y la función stage.set3DMode :

glEnable(GL_DEPTH_TEST) 
glMatrixMode(GL_PROJECTION) 
glLoadIdentity() 
gluPerspective(60.0, float(width)/float(height), .1, 10000.) 
glMatrixMode(GL_MODELVIEW) 

¡Realmente espero que alguien pueda señalar mi error! Y gracias por ayudarme :)

Respuesta

3

Parece que usted no llama glLoadIdentity() después de cambiar a GL_MODELVIEW. La forma más sencilla de recordar es llamarlo cada vez que cambie la matriz de proyección, cambie a GL_MODELVIEW y llame al glLoadIdentity() (a menos que realmente desee conservar el anterior).

+0

¡Perfecto! Eso lo solucionó ¡Muchas gracias! – Bartvbl

1

utilizo las siguientes matrices de renderizado 2D:

glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
glOrtho(0, w, h, 0, 0, 1); 
glViewport(0, 0, w, h); 

glMatrixMode(GL_MODELVIEW); 
glLoadIdentity(); 
glTranslatef(0.375f, 0.375f, 0); 
2

Mucha gente se equivoca al usar glViewport y similares. Siempre lo colocan en la devolución de llamada de remodelación, que simplemente está mal.

Usted siempre a la configuración completa en su función de representación, justo antes de que necesite esas configuraciones. Entonces su código debería ser (pesudocode):

render_scene(): 
    // first clear the whole window 
    glViewport(0, 0, window.width, window.height) 
    glClearDepth(1.0) 
    glClearColor(1., 1., 1., 1.); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT) 

    glViewport(3Dstuff.x, 3Dstuff.y, 3Dstuff.w, 3Dstuff.h) 
    // maybe also set scissor to clip 
    glMatrixMode(GL_PROJECTION) 
    glLoadIdentity() 
    gluPerspective(60.0, float(width)/height, .1, 10000.) 
    glMatrixMode(GL_MODELVIEW) 
    setup3DstuffModelview() 
    glDepthFunc(...) 
    glEnable(GL_DEPTH_TEST) 
    // other state stuff 
    render3Dstuff() 

    // now the 2D stuff 
    glViewport(2Dstuff.x, 2Dstuff.y, 2Dstuff.w, 2Dstuff.h) 
    // clear depth and stencil -- if you need parts of the 3D depth/stencil 
    // for some algorithm retain it or save and restore by FBO renderbuffers or 
    // glReadPixels, glDrawPixels 
    glClearDepth(1.0) 
    glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT) 

    glMatrixMode(GL_PROJECTION) 
    glLoadIdentity() 
    glOrtho(...) 
    glMatrixMode(GL_MODELVIEW) 
    glLoadIdentity() 
    setup2DstuffModelview() 
    glDepthFunc(...) 
    glDisable(GL_DEPTH_TEST) 
    // other state stuff 
    render2Dstuff() 

    // repeat for all the layers you need 

Esto es realmente importante: OpenGL es una máquina de estado. A menos que pueda probar que no ocurren cambios de estado indeterminados, siempre vuelve a establecer todos los estados que necesita antes de representar una determinada geometría.

Cuestiones relacionadas