Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenGL switching between ortho and perspective
    primarykey
    data
    text
    <p>I have a tile engine using orthographic projection in immediate mode and I'm just trying to draw a 3d cube on top of my tile scene, in hopes that I can eventually incorporate 3d models into my engine instead of just sprites / textured quads. I would also like to make the tiles 3d for that slight bit of extra eye candy.. Hopefully I can eventually convert this to use modern OpenGL so I can take advantage of those extra features. Not on the top of my priority list at the moment. So on to the question.</p> <p>I'm initializing OpenGL with this:</p> <pre><code>void initGL() { glDisable(GL_DEPTH_TEST); glViewport( 0,0,SCREEN_WIDTH,SCREEN_HEIGHT); //Initialize Projection Matrix glMatrixMode( GL_PROJECTION ); glPushMatrix(); glLoadIdentity(); //Initialize Modelview Matrix glMatrixMode( GL_MODELVIEW ); glPushMatrix(); glLoadIdentity(); glOrtho(0,SCREEN_WIDTH,SCREEN_HEIGHT,0,0,1); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); //...setting some various other attributes, omitted for brevity... glEnable(GL_TEXTURE_2D); glClearColor( 0, 0, 0, 0 ); } </code></pre> <p>I have a function for drawing a cube that works.</p> <pre><code>void draw_cube() { /* position object */ glRotatef(30.0F, 1.0F, 0.0F, 0.0F); glRotatef(30.0F, 0.0F, 1.0F, 0.0F); /* draw six faces of a cube */ glBegin(GL_QUADS); ...vertices... (removed for brevity) glEnd(); } </code></pre> <p>I made 2 functions for setting the 2d and 3d projection modes.</p> <pre><code>void set3d() { glEnable(GL_DEPTH_TEST); glClear(GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(0,SCREEN_WIDTH,0,SCREEN_HEIGHT, 0,1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void set2d() { glClear(GL_COLOR_BUFFER_BIT); glDisable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0,SCREEN_WIDTH,SCREEN_HEIGHT,0,0,1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } </code></pre> <p>For the sake of completeness, some <strong><em>pseudocode</em></strong> of my textured quad drawing..</p> <pre><code>pushmatrix(); translate(); rotate(); -translate(); glbegin(); ...vertices glend(); popmatrix(); </code></pre> <p>So far I have managed to get the cube to draw on top of the tiles, but it disappears after approximately 1 second to never be seen again until I run the program again.</p> <p>Basically what I'm doing in the main loop is this:</p> <pre><code>move objects around, process collisions, etc. set2d(); draw textured quads, lines, text, etc. in 2d mode. set3d(); draw_cube(); </code></pre> <p>My question is: Why is the cube disappearing, and are there any errors in my set2d() and set3d() functions?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload