Pixels are not being drawn over a box

Started by poo2thegeek, June 07, 2013, 19:31:12

Previous topic - Next topic

poo2thegeek

For a simple game I am making I have got a developer mode. In here there are things such as fps, and a graph that shows used ram/total ram. The logic of drawing and calculating this will be simple. But I have a problem. My graph has a background that is a black to red color so that the graph itself stands out from the background program.

glBegin(GL_QUADS);{
			glColor3f(0.7f,0,0);
			glVertex2f(0,400);
			glVertex2f(400,400);
			glColor3f(0,0,0);
			glVertex2f(400,600);
			glVertex2f(0,600);
		}glEnd();


This draws absolutely fine. I then have an array list of Points, which will be the points that I will use for my graph later on.
for(Point i: p){
				glVertex2f(i.x,350);
			}


This will work as well, so long as the line is above the background box. But as soon as the y co-ords of the line are over the box (for example, y=450) then the line disappears. It seems to be rendering behind the box for some reason. If I cange the order of the code (so the points are drawn before the box) the line gets drawn just fine. But then my 3d background game just disappears! Even though there is no change to the code other than a small bit of ordering.

Changing the method from this:
private static void drawGraph(){
		glDisable(GL_TEXTURE_2D);
		glEnable(GL_DEPTH_TEST);
		
		glBegin(GL_QUADS);{
			glColor3f(0.7f,0,0);
			glVertex2f(0,400);
			glVertex2f(400,400);
			glColor3f(0,0,0);
			glVertex2f(400,600);
			glVertex2f(0,600);
		}glEnd();
		
		glColor3d(1,1,1);
		glBegin(GL_POINTS);{
			for(Point i: p){
				glVertex2f(i.x+50,450);
			}
		}glEnd();
	}

http://imageshack.us/a/img845/8029/example1i.png

To this:
private static void drawGraph(){
		glDisable(GL_TEXTURE_2D);
		glEnable(GL_DEPTH_TEST);
		
		glColor3d(1,1,1);
		glBegin(GL_POINTS);{
			for(Point i: p){
				glVertex2f(i.x+50,450);
			}
		}glEnd();
		
		glBegin(GL_QUADS);{
			glColor3f(0.7f,0,0);
			glVertex2f(0,400);
			glVertex2f(400,400);
			glColor3f(0,0,0);
			glVertex2f(400,600);
			glVertex2f(0,600);
		}glEnd();
	}


http://imageshack.us/a/img7/6329/example2x.png

I have absolutly NO idea why.
Any help is great! Thanks!

quew8

Why are you enabling the depth test? You want to be disabling it so that the geometry is always rendered. Then draw the background box then draw the line.

poo2thegeek

Disabling the GL_DEPTH_TEST stops everything from being drawn.

quew8

Then you're probably drawing the graph stuff before you draw the actual game. Bad idea. Whenever you want an overlay type thing, disable the depth test and draw it AFTER everything else. This ensures it is drawn on top. Also make sure you are clearing the depth buffer as well as the colour buffer at the start of the frame.:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

poo2thegeek

Here is my "loop" method:
private static void loop(){
		delta=1;
		long lastTime=System.currentTimeMillis();
		while(!Display.isCloseRequested()){
			delta=(float)(System.currentTimeMillis()-lastTime);
			lastTime=System.currentTimeMillis();
			controlls(delta+1);
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			draw3D();
                        draw2D();
			Display.update();
			Display.sync(100);
		}
	}


draw3D does this:
private static void draw3D(){
		
		cam.applyPerspectiveMatrix();
		glDisable(GL_BLEND);

		glLoadIdentity();
		cam.applyTranslations();{
			
			Wall.render();
			
		}		
		glPopMatrix();
	}


and draw 2d does this:

private static void draw2D(){
		glEnable(GL_TEXTURE_2D);
		glMatrixMode(GL_PROJECTION);
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glLoadIdentity();
		glOrtho(0, getWidth(), getHeight(), 0, 1, -1);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		Developer.update(cam,room,delta);
	}


The developer.update() method is a version oft he same as before:
private static void drawGraph(){
		glDisable(GL_TEXTURE_2D);
		glEnable(GL_DEPTH_TEST);
		
		glBegin(GL_QUADS);{
			glColor3f(0.7f,0,0);
			glVertex2f(0,400);
			glVertex2f(400,400);
			glColor3f(0,0,0);
			glVertex2f(400,600);
			glVertex2f(0,600);
		}glEnd();
		
		glColor3d(1,1,1);
		glBegin(GL_POINTS);{
			for(Point i: p){
				glVertex2f(i.x+50,450);
			}
		}glEnd();
	}

quew8

OK:
1) You are not DISABLING the depth test in drawGraph like I told you to.
2) You aren't disabling it anywhere else.
3) The pictures you gave show lines and yet none are drawn in the code snippet so I guess this isn't actually what you're using?

In draw graph, disable the depth test, draw the box, then draw the lines / points, then enable the depth test.

I just assumed you were enabling it later on before.

poo2thegeek

OK:
2) You aren't disabling it anywhere else.
In draw graph, disable the depth test, draw the box, then draw the lines / points, then enable the depth test.

I just assumed you were enabling it later on before.

Quote1) You are not DISABLING the depth test in drawGraph like I told you to.

Ah right, my bad sorry. I did so originally, but in the wrong place so it didn't work so I removed it. It now works.

Quote3) The pictures you gave show lines and yet none are drawn in the code snippet so I guess this isn't actually what you're using?

The lines in the screen shots are this:
for(Point i: p){
		glVertex2f(i.x,350);
	}

The "p" is an array list of Points that is 200 points long. The 350 was just for testing, and has now been changed to a set value.

So now, with your help, I have got it working. But my 3d object in the background has just lost all its color and gone white. Ill try and fix it, its just a minor bug. Thanks a lot!

quew8

Just be careful when you change a state in OpenGL because it is such an important thing. Another example of this is the GL_BLEND. You enable it and set the function in draw3D() but don't disable it anywhere or change the function so both of those lines can be moved to the initialization / setup part of your code.

When you have two distinct modes of drawing, it would probably be best to use glPushAttrib and glPopAttrib to "save" and "load" the state for each operation. It would probably be quicker as well as less error prone. I'm not sure about the exact arguments you would want to use but here are the docs for it: http://www.opengl.org/sdk/docs/man2/xhtml/glPushAttrib.xml.