Reputation: 1397
My understanding of OpenGL rendering was that without a depth test, objects would be drawn in sequential order, so that the last render function you call will draw its shape on top of whatever has already been drawn. I'm finding that this seems to not be the case however:
glDisable(GL_DEPTH_TEST);
glDisable(GL_ALPHA_TEST);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_QUADS);
{
glVertex2f(50, 50);
glVertex2f(100, 50);
glVertex2f(100, 100);
glVertex2f(50, 100);
}
glEnd();
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_QUADS);
{
glVertex2f(30, 60);
glVertex2f(110, 60);
glVertex2f(110, 90);
glVertex2f(30, 90);
}
glEnd();
Rather then the red square under the blue square, which I would expect, these calls give me a red square over a blue square!
Do I not understand how OpenGL Rendering works? Or am I just missing some kind of option or configuration thats not letting the blue square render over? I am using a custom (but very simple) shader, would that change anything?
The non accepted answer to this question seems to imply that sequential rendering doesn't in fact occur and is implementation specific, but its accepted answer implies it is in fact the case.
Whats the best way of making the second call on top? I don't really want to have to have some kind of global z value that I keep on incrementing throughout the program.
Upvotes: 1
Views: 2088
Reputation: 26439
render function you call will draw its shape on top of whatever has already been drawn.
That's right.
I'm finding that this seems to not be the case however:
This works for me:
#include <SDL/SDL.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>
static int scrWidth = 640;
static int scrHeight = 480;
int main(int argc, char** argv){
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);
if (SDL_SetVideoMode(scrWidth, scrHeight, 32, SDL_OPENGL) == 0){
fprintf(stderr, "couldn't set mode %dx%d!\n", 640, 480);
SDL_Quit();
return -1;
}
glewInit();
SDL_ShowCursor(SDL_DISABLE);
glClearColor(0.2, 0.2, 0.2, 0.2);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
bool running = true;
while (running){
SDL_Event event;
if (SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
running = false;
break;
};
}
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, scrWidth, scrHeight, 0);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex2f(50, 50);
glVertex2f(100, 50);
glVertex2f(100, 100);
glVertex2f(50, 100);
glEnd();
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_QUADS);
glVertex2f(30, 60);
glVertex2f(110, 60);
glVertex2f(110, 90);
glVertex2f(30, 90);
glEnd();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glFlush();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}
Compare with your code and see if there's a difference. Perhaps you didn't really disable depthtest. You could also get the same problem if you use stencil buffer in your code with certain combinations of stencil buffer parameters (something like glStencilFunc(GL_NOTEQUAL, 0, 0xffffffff); glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
). You could also get same effect if you forget to use glClear and insert "swap buffers" call between first and second polygon.
am using a custom (but very simple) shader, would that change anything?
Yes it could change everything. However, in this case it means that you haven't provided enough information to help you with your problem. When you want help with your code you should post complete and functional, but minimal example that illustrates your problem. "Forgetting" to post this example is a bad idea.
Upvotes: 5