# gluLookAt and GL_DEPTH_TEST?!?

This topic is 3659 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, I'm still having a major problem doing what I need to do for my project, even after posting a few threads I still wasn't able to get it right. Ok... so here I go again. What I'm trying to do is to draw a bunch of identical objects (gluCylinders), 8 of them lined up horizontally and 4 rows of that which comes out to 32 of them. I need to have 4 sets of it as well, which adds up to a total of 128 cylinders. I also need to rotate them by +-25 degrees every other set, creating kind of a crossing effect. Now, when I draw them normally without enabling GL_DEPTH_TEST, the first 8 tubes (top layer) would be drawn correctly like so... and when I move down to draw the next 8 tubes, it would cover up the top 8 as shown... That's not what I need, I need the top layer to be above the rest and 2nd layer to be above 3 and 4 and 3rd layer to be above 4th like this... I created the correct image by drawing bot to top, which is not correct by design since I need it to be top to bot for the other part of the program to work. Sorry for such a lengthy post, but I've been struggling with this for weeks and I'd really appreciate it if I can get some help on this >< Mahalo, Aiea edit: broken images

##### Share on other sites
This is a job for the depth buffer, so enable depth testing. Just make sure you actually ask for a depth buffer when defining the pixel format for the window, and that the near and far clip planes are set to reasonable values. I have quickly looked at your previous posts, and it sounds to me the problem is in how you manage the depth buffers and settings.

##### Share on other sites
Brother,

Thanks for the suggestion, I've read up on the redbook and other materials, but I'm not sure if I understand what they are saying about the depth test.

So depth testing tests the old pixel and the new pixel to figure out which is closer to the camera, and if it is, it replaces it. But how are you suppose to set the depth of a specific item that you are drawing?

The way I'm understanding this is that first thing drawn is always on the "top" when depth testing is enabled. I know that doesn't sound right but that's what I got out of it.

Care to elaborate a bit more?

Thanks

##### Share on other sites
The depth of a pixel is automatically calculated from the geometry. When the cylinder approaches the back, the distance from the viewpoint to the cylinder increases, and so the depth increases.

##### Share on other sites
    for (int j = 0; j <= 3; j++)    {        float temp_y_trans = 0.7;        temp_y_trans = temp_y_trans - j * VERT_TUBE_CENT_DIST_CM * CONVERSION_UNIT;        for (int i = 0; i <= 7; i++)        {            glClear(GL_DEPTH_BUFFER_BIT);            glLoadIdentity();            glPushMatrix();            glTranslatef(0, 0, -1);            glRotatef(25, 0.3, -1, 0);            glTranslatef(HORI_TUBE_CENT_DIST_CM * CONVERSION_UNIT * i + RADIUS_CM * CONVERSION_UNIT + HORI_GAP_CM * CONVERSION_UNIT / 2, temp_y_trans, -1);            glColor3f(1.0f, 0.0f, 0.0f);            GLUquadric* tube_quad;tube_quad = gluNewQuadric();            gluCylinder(tube_quad, RADIUS_CM * CONVERSION_UNIT, RADIUS_CM * CONVERSION_UNIT, 2, 32, 32);            glBegin(GL_TRIANGLES);            glColor3f(0.8f, 0.0f, 0.0f);            float xc = 0.0;            float yc = 0.0;            float xp1 = 0.0;            float yp1 = 0.0;            for(double angle = 0.0f; angle <= (2.1f*3.14159); angle += 0.01f)            {                glVertex3f(0, 0, 2.00); //first point                double xp2 = xc + (RADIUS_CM * CONVERSION_UNIT * (float)sin((double)angle));                double yp2 = yc + (RADIUS_CM * CONVERSION_UNIT * (float)cos((double)angle));                glVertex3f(xp1, yp1, 2.00);                glVertex3f(xp2, yp2, 2.00);                xp1 = xp2;                yp1 = yp2;            }            glEnd();            glPopMatrix();        }

That is my code for drawing the cylinders and capping them off. As far as I know, I'm drawing them all at the same depth z and capping them off at the same depth as well, but it still produces the same result even after setting the gl_depthfunc to less only. Am I doing something wrong here?

##### Share on other sites
Of course the depth buffer isn't working properly when you clear it for every cylinder. If you clear it, it will not contain the depth for previous cylinders and you cannot expect to get anything useful out of that. Clear it once when you clear the color buffer.

##### Share on other sites
My mistake, you are right about me clearing it for every cylinder. However even after I took out the clear buffer it still produces the same result, and the only other time that I clear it its during init.

I was thinking about this, so I should clear it only once per 32 tubes and somehow make each horizontal tube higher depth than the tube before and each vertical tube less than the one above it. Does that sound right? If so how would I approach it?

I'm still quite new at opengl, sorry about these questions.

##### Share on other sites
You should clear it before drawing your image. You must clear the color buffer somewhere, and you should clear the depth buffer when you clear the color buffer. Almost without exceptions, that means the very first thing you do before drawing each new frame.

But as I said in my first post, it is probably a problem with the depth buffer settings, and you still haven't said anything about the points I mentioned. What about the pixel format, do you even have a depth buffer to begin with? What about the near and far clip planes, are they reasonable values?

##### Share on other sites
Brother

I don't think I know how to do any of those things to be honest ><

I'm using wxWidgets to write my program, and after googling on pixel formats, I have a feeling that's for win32 programming?? I believe my depth buffer is at default which is 1?? and I don't really understand clip planes =(

Thanks alot for all the help, I didn't think drawing a few simple cylinders would be this difficult ><

##### Share on other sites
The pixel format is the format of the frame buffer, and you need to specify it somewhere. If you don't, then you either get some default settings (becuase you don't specify it, or simply becuase you don't have the ability to; depends on API you use). Check the documentation, any decent windowing API supporting OpenGL should provide you with some way to enable depth buffering and/or specify depth buffer resolution.

Stepping back from actually trying to solve this specific problem, the reason you're having problem drawing some cylinders is simply becuase you're not familliar with the very basic concepts of OpenGL. At this point, I would strongly suggest you step back from your application for a while and take some time to read som tutorials. For example, study the way of setting up the rendering environment (creating a window and specifying its pixel format) and the view setup (in which you will know about the clip planes).

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5

• 12
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632991
• Total Posts
3009749
• ### Who's Online (See full list)

There are no registered users currently online

×