PDA

View Full Version : OpenGL depth problem/question

openglbeginner123
09-04-2014, 03:47 PM
Hello,

I keep running into a problem with depth in the viewport (i assume) when trying to draw just simple lines. I want to draw a set of lines parallel to each other and I am using a loop to do that. The problem that arises though, is when I use glRotatef() function to rotate my "camera", which is technically just rotating everything drawn, the lines that I drew are only appearing to be extended to a certain value, I assume it's 1 but I have no idea if that's what's happening... :confused: So basically, what is happening, I think, is the lines just stop being drawn at that point, and when I rotate the view, I get an unpleasant effect of the lines just cut-off there, when I would like to see them go further down, hence creating an effect of further depth.. unfortunately this is not happening. As you can see from the piece of code, all I am doing is drawing a bunch of horizontal lines parallel to each other..

I would greatly appreciate if somebody could explain why this is happening and even more if there is a solution to this and how I should fix it

I assume this has to do something with the viewport and the min and max depth ranges but I am really not sure

I also hope I explained this enough and clearly enough I realize the explanation might be confusing

glBegin(GL_LINES);

glColor3f(0.5, 0.5, 0.5);
for(float i = -100; i < 100; i+=0.005){
glVertex3f(i, 0, 1);
glVertex3f(i, 0, -1);
}

glEnd();

Thank you very much in advance

Yandersen
09-05-2014, 07:38 AM
Seems like you have just discovered that the cameras in computer graphics have a far clipping plane against which all the geometry is clipped (not drawn). Check your projection matrix. Here are the functions used to construct such matrices:
http://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml
http://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml
The legacy OpenGL maintains two matrices, modelview and projection, so ensure you switch between them depending which one you want to modify (see glMatrixMode).

openglbeginner123
09-07-2014, 12:07 PM
Thanks for your reply. I read over even more tutorials and documentation on the two matrices and the two modes. I tried to modify my code to construct such matrices but all I keep getting is a black screen every time... I think I may be missing something important here. As I mentioned before, my graphics are "destroyed" / "hidden" when I rotate using glRotatef() and I would like them to display appropriately. I would appreciate if you could post a code snippet of the order of method calls that I need to do to properly set up my matrices so that my geometry is clipped the right way.

Thanks a lot

Yandersen
09-08-2014, 02:47 AM
You first. :)
Do not take away from us a nice opportunity to laugh at the nooby code! :D
Whoops, I mean, show the codes, we will help you to correct it to make it work. :)

openglbeginner123
09-26-2014, 06:56 PM
Haha :) Thanks for your replies. Sure I can post some code. Sorry it's been a while since I logged in last time, but here is the code that I have so far:

for(float theta = 0; theta < 360; theta +=((float)rand() / RAND_MAX) * 10){
for(float phi = 0; phi < 360; phi +=((float)rand() / RAND_MAX) * 10){
float x = 1.1 * sinf(theta * 3.14 / 180) * cosf(phi * 3.14 / 180);
float y = 1.1 * sinf(theta * 3.14 / 180) * sinf(phi * 3.14 / 180);
float z = 1.1 * cosf(theta * 3.14 / 180);
Point3D *point = [[Point3D alloc] initWithX:x Y:y Z:z];
}
}

So I am trying to draw something like a sphere but only with a little 'randomized' coordinates meaning its not completely filled but has some 'gaps'. This is my code and it works so I do get what I want to draw. I generate a bunch of vertices and store them in an array. The problem though, is once this is done, I have the following code where I try to retrieve the vertices and draw them. That works too except that when I try to use glScale() to 'zoom in' a little or glRotatef() to rotate the view, i get this black cutoff thing in a middle of the viewport that messes up the graphics.

glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glScalef(0.5, 0.5, 0.5);

glRotatef(rotationX,1,0,0);
glRotatef(rotationY, 0, 1, 0);

glBegin(GL_POINTS);

for(int i = 0; i < points.count; i++){
Point3D *point = [points objectAtIndex:i];
glVertex3f(point.x, point.y, point.z);
}

glEnd();
glFlush();

The sphere just cuts off and there is like a hole. I don't know how to fix this, but I think it has something to do with my projection matrix...then again maybe not... As you said though, I would greatly appreciate and explanation from you guys:)