OpenGL/glut background polygons appear to overlap foreground plygons - c

I am new to OpenGL, and I am having trouble displaying a simple cube on my screen. The problem is that sides of the cube that should be hidden in the background still appear. I feel that the answer should be that I have to enable GL_DEPTH_TEST, but this causes the screen to display a complete white canvas with nothing on it. Here's a sample from a run I have done:
Each side is just a random color.
Here is a snippet of my code:
glutInit(&argc, argv);
glutInitWindowSize(600, 400);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50.0, 1.5, 1.0f, 100.0);
gluLookAt(10.0, 5.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
//glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glClearColor(1.0, 1.0, 1.0, 1.0); /* white */
I have commented out glEnable(GL_DEPTH_TEST) for now.
What else should I be doing so that there is no overlapping on this cube?
Thank you for any help!

Enable GL_DEPTH_TEST, and clear the depth buffer before rendering with glClear(GL_DEPTH_BUFFER_BIT);.
This can be combined with glClear(GL_COLOR_BUFFER_BIT); if you're using that, as glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

In addition to what #immibis said about depth buffers, you can also turn on back-face culling and make sure that you draw your primitives facing the correct direction. To do that:
glEnable (GL_CULL_FACE);
glCullFace (GL_BACK);
glFrontFace (GL_CW); // This says you define your primitives in clockwise order
That causes faces that are pointing away from the camera not to be drawn which can improve performance, and can eliminate this particular problem as well. (But you probably want to use a depth buffer, too.)

Related

Drawing the intersection between glut objects in C

I am trying to draw the intersection between two glut objects, I managed to draw each object separately but I was wondering if I can draw only the intersection between the two objects?
My Code below draws a solid cube and sphere:
/* draw a cube */
glTranslatef( 0.0, 0.0, 30.0 );
glutSolidSphere(30,12,6);
/* draw a wire sphere */
glTranslatef( 0.0, 0.0, 30.0 );
glutSolidCube(30);
Since OpenGL is not a scene graph (i.e. it doesn't maintain some kind of scene representation) but only draws simple primitives (points, line, triangles), one at a time, this is not immediately possible. There are methods to do this in image space, using multipass stencil buffer trickery. Here's a nice explanation: ftp://ftp.sgi.com/opengl/contrib/blythe/advanced99/notes/node22.html

gluLookAt in openGL never changes the view

I have been googling this for awhile and haven't been able to fix my code. I am just trying to get gluLookAt() to look at a certain point on a surface.
I have an init() function that should set the starting point to look at when the program runs, but it never changes the view.
Here is my init():
void init(void){
glClearColor(0.0,0.0,0.0,1.0);
glColor3f(1.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0,1.0,1.0,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,5.0,0.0,0.0,6.0,0.0,1.0,0.0);
}

why dont glut cubes or spheres show up?

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glBegin(GL_TRIANGLES);
// glVertex3f(-0.5,-0.5,0.0);
// glVertex3f(0.5,0.0,0.0);
// glVertex3f(0.0,0.5,0.0);
//glEnd();
glutSolidSphere(200,10,10);
glutSwapBuffers();
the triangle shows up but not the sphere
why?
I just get a black window
Your radius is probably too big and you are culling back faces.
glutSolidSphere(1,10,10);
Glut is close-sourced and unmaintained. Consider something else, like SDL.

Simplest way to set up a 3D OpenGL perspective projection

There have been many tutorials where each suggests using gluPerspective or glFrustum with a combination of other things, yet I've had difficulties setting up the right matrix. What code do I need to set up a 45˚ perspective view looking down the +z axis?
So far I have:
glShadeModel(GL_SMOOTH);
glClearColor(0,0,0,0);
glClearDepth(1);
glDepthFunc(GL_LEQUAL);
glViewport(0,0,width,height);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,1,0.1,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
But that doesn't seem to work. All I get is a black screen when I attempt to draw things.
EDIT: Here's the minimal drawing code:
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3ub(255,255,255);
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(20,20,20);
glVertex3f(20,30,20);
glVertex3f(30,20,20);
glVertex3f(30,30,20);
glEnd();
Things such as points on (1,1,1) and (2,50,23). They do not appear.
Well there's your problem. The default OpenGL camera has the +Z axis pointing towards the camera. And since the camera is at Z=0, any position who's Z position is >0 is behind the camera.
Move your points in front of the camera. They need to at least have a -Z position.
EDIT: Here's the minimal drawing code:
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3ub(255,255,255);
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(20,20,20);
glVertex3f(20,30,20);
glVertex3f(30,20,20);
glVertex3f(30,30,20);
glEnd();
Your vertex coordinates lie way outside the viewing volume. First, OpenGL by default "looks" down the negative Z axis, so your Z coordinates must be -100 < z < -0.1 for your choosen near and far clip plane.
But even if you flipped the sign on the Z coordinate, your vertices still would lie outside the 45° FOV. (20, 0, 20) is 45° from the viewing axis, and (30, 0, 20) even farther. Try centering your vertex coodinates around (0,0,-5) like (+/-1, +/-1, -5)

Opengl drawing a 2d overlay on a 3d scene problem

I have a moving 3d scene set up, and I want to make a stationary 2d GUI overlay that is always on top, when I try making 2d shapes I don't see anything. When I call: glMatrixMode(GL_PROJECTION); my 3d scene disappears and I'm left with a blank window...
here is the code I'm using for the overlay
EDIT: updated code
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glColor3f(1, 1, 1);
glPushMatrix();
glBegin(GL_QUADS);
glVertex3f(-5.0f, 5.0f, 0.0f);
glVertex3f(-5.0f, -5.0f, 0.0f);
glVertex3f(5.0f, -5.0f, 0.0f);
glVertex3f(5.0f, 5.0f, 0.0f);
glEnd();
glPopMatrix();
glEnable(GL_DEPTH_TEST);
glutSwapBuffers();
Hmm... Basing on the fragment of code you posted, I believe that your scene disappears because of what you're doing with your matrices - looks a bit chaotic to me. The approach should look like this:
clean the screen
3D:
enable lighting, z-test, etc
set active matrix mode to projection
load identity and establish a perspective projection
set active matrix mode back to modelview
draw everything 3D
2D:
disable lighting, z-test, etc
set active matrix mode to projection
load identity and establish an ortogonal projection
set active matrix mode back to modelview
draw everything 2D
swap buffers
Also, consider switching to shaders (and to a modern OpenGL version in general) if you want to make your life even easier :).
You must draw your quad in the other order. By default, OpenGL use counterclockwise front facing polygons. That means that you don't see your polygon because you see only its back face.
You might take a look at glFrontFace.
EDIT:
Also, if that doesn't work, you could try to disable the following states:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glDisable(GL_BLENDING);
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
You might want use glPushAttrib and glPopAttrib in order not to mess your state.
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(20.0f, 20.0f, 0.0f);
glVertex3f(20.0f, -20.0f, 0.0f);
glVertex3f(-20.0f, -20.0f, 0.0f);
glVertex3f(-20.0f, 20.0f, 0.0f);
glEnd();
/// Now swap buffers
In addition, I also use a separate FBO for these kind of things. Usually the overlay doesn't have to be redrawn all the time, so render it on demand to a FBO and just render it as a fullscreen quad each frame. It wastes some fillrate but in general I find it is usually faster anyway and makes the code so much cleaner.
Make sure your geometry ( specifically the z coordinates of your geometry, in terms of your 2d UI ) is greater than the near plane ( behind the near plane on the z-axis ), otherwise, any rendering which takes place in front of the near-plane will not be seen. I'm assuming you have defined your view frustum somewhere else in the code ( this is where the near-plane is defined ).
If the near-plane is 0.01f, then your vertex definitions could be
glVertex3f(-5.0f, 5.0f, -0.02f);
glVertex3f(-5.0f, -5.0f, -0.02f);
glVertex3f(5.0f, -5.0f, -0.02f);
glVertex3f(5.0f, 5.0f, -0.02f);
I believe in the MatrixMode( GL_MODELVIEW ) you are always looking into the -Z Axis.
I hope this helps.
I may be wrong but i think the DEPTH_TEST refers to the z-buffering of your final rendered object, i don't think it disables the near-plane value.
' glGetBooleanv(GL_BLEND, &m_origin_blend);
glGetBooleanv(GL_DEPTH_TEST,&m_origin_depth);
glGetBooleanv(GL_CULL_FACE, &m_origin_cull);
setAlphaBlending(true);
setDepthTest(false);
setCullFace(false); //by stone
//ur draw core()
setAlphaBlending(m_origin_blend>0?true:false);
setDepthTest(m_origin_depth>0?true:false);
setCullFace(m_origin_cull>0?true:false); //by stone
'

Resources