Converting Orthogonal Camera to Perspective OpenGL - c++

I'm having some troubles converting a orthogonal camera to a perspective one, using OpenGL.
I currently have my orthogonal camera following a middle point of two objects, using:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,_winWidth,0,_winHeight,150,-150);
glTranslated(-_middlePoint[0]+_winWidth/2, -_middlePoint[1]+_winHeight/2, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
The above code works perfectly, now i'm trying to use it like this:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, _winWidth/_winHeight, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 800, _middlePoint[0], _middlePoint[1], 50, 0, 0, 1);
glLoadIdentity();
And I simply get a black screen. Any thoughts? I've tried changing the up vector from 0,0,1 to 0,1,0 and it stays the same.
Any help appreciated.

If you have code working using glOrtho already, you can normally switch to a perspective projection by simply changing that to glFrustum. If you're writing new code, gluPerspective and gluLookat may be easier, but for code that already works using an orthographic projection, it's easy to switch to perspective by just calling glFrustum with the same parameters.

Related

Why is this object drawn different if I use glFrustum?

I'm doing this OpenGL project for my Computer Graphics class, where I display an object and I rotate it and stuff, the thing is that at the beginning of the project we used glOrtho() and it looked really great.
But now the teacher said that we have to use glFrustum() for perspective and if I use that function, the object is drawn like this and I really don't know why does this happens:
This is my code from the init() function where everything changes:
void init (void)
{
/* select clearing (background) color */
glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100.0, 100.0, -60.0, 160.0, -100.0, 100.0);
//glFrustum(-100, 100 ,-100 ,100 ,1 , 40);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(90,0,1,0);
}
I'd appreciate your help.
EDIT: If I use glFrustum(-100, 100, -100, 100, 20, 200) it looks like this, like I'm getting closer but what about the left, right, top and bottom parameters? Are they okay with that values?
It's hard to be certain without more information. Perhaps the model could give some insight. But I suspect it may have to do with your clipping planes (nearVal and farVal as described here) arguments passed to glFrustum (1, 40). Perhaps try setting them to a broader range like your glOrtho call: 1, 150 (Note: neither nearVal or farVal can be negative when passed to glFrustum).
This all depends on the scale of the model and how it is positioned relative to the camera. If part of the model falls outside of the clipping planes, then it will be, well... clipped.

OpenGL 2D hud in 3D application

I have been trying to create a hud in my OpenGL application. Having looked around, it seems the way to do it is with an ortho projection, but so far I have not been able to get the program to render correctly. What is happening is instead of rendering on top of my display, I'm getting odd graphical glitches as seen here:
If I comment out the hud code, everything renders perfectly.
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
//Set up projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Using gluPerspective. It's pretty easy and looks nice.
gluPerspective(fov, aspect, zNear, zFar);
//Set up modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//3D rendering
glDepthMask(GL_FALSE);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,window_width,0,window_height); //left,right,bottom,top
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0,0.0,1.0);
glBegin(GL_QUADS);
glVertex2f(50,50);
glVertex2f(50,100);
glVertex2f(100,100);
glVertex2f(100,50);
glEnd();
Once you're done rendering the HUD, you need to re-enable the depth writes
glDepthMask(GL_TRUE);
When you clear the buffers when rendering your HUD, all that has been
drawn so far (your 3D scene) will also be cleared. So don't clear the buffer twice.

glortho zox plane projection

I need to make an opengl projection on a plane paralel with XOZ plane (perpendicular on OY).
Can you explain me what parameters I should use with glOrtho to make this projection.
Thanks,
The wording of your question is not entirely clear to me, but if you want to display things in a "2D" fashion, this is how to do it:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, window_x_size, window_y_size, 0, 0, 1);

opengl perspective view clipping glutSolidTeapot too early

i got following code:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, (GLint)w, (GLint)h); //got w and h from QT4.7.1's QGLWidget
gluPerspective(90,1,0.1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//me.position is 0;0;0
gluLookAt(me.position.x(), me.position.y(), me.position.z(),
me.position.x(), me.position.y(), me.position.z()+1.0f,
me.position.x(), me.position.y()+1.0f, me.position.z());
glTranslatef(-0.2,0.5,2);
glRotated(180,0,1,0);
glRotated(45,1,0,0);
glRotated(45,0,0,1);
glFrontFace(GL_CW);
glutSolidTeapot(0.5f);
glFrontFace(GL_CCW);
now, the problem is, opengl is clipping almost the complete teapot, and I don't understand why.
I think something is going wrong w/ the zNear/zFar planes, but I don't see why.
if anyone can tell me what I'm doing wrong/how to fix it, please do.
thanks in advance

Why is my glutWireCube not placed in origin?

I have the following OpenGL code in the display function:
glLoadIdentity();
gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz);
// called as: gluLookAt(20, 5, 5, -20, 5, 5, 0, 1, 0);
axis();
glutWireCube (1.);
glFlush ();
axis() draws lines from (0,0,0) to (10,0,0), (0,10,0) and (0,10,0), plus a line from (1,0,0) to (1,3,0).
My reshape function contains the following:
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(45.0, (GLsizei) w / (GLsizei) h, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
This image shows the result of running the program with 1. as the argument to glutWireCube:
As you can see, the cube isn't centered around (0,0,0) as the documentation says it should be:
The cube is centered at the modeling
coordinates origin (...) (source)
If I run the program with 5. as the argument, the cube is displaced even further:
Why is that, and how do I place the cubes around (0,0,0)?
FURTHER INFORMATION
It doesn't matter if I switch the order of axis() and glutWireCube. Surrounding axis() with glPushMatrix() and glPopMatrix() doesn't fix it either.
SOLUTION
I modified gluPerspective to start looking further away from the camera, and now the Z-buffering works properly, so it is clear that the cubes are placed around the origin.
Are you sure axis does not mess with the view matrix ?
What happens if you call it after the drawing of the cube ?
Edit to add:
Actually... Looking at the picture closer, it looks like it might be centered at the origin.
The center of the cube seems to align exactly with the intersection of the 3 axes. The only thing that looks suspicious is that the red line does not write over the white edge. do you have Z-buffering properly set up ?
It might be right, I think it's hard to determine due to the perspective ... But I guess it isn't from staring a bit more at it.
To quickly rule out that axis() isn't modifying the model view matrix, surround the call with matrix push/pops:
glPushMatrix();
axis();
glPopMatrix();
Things to investigate/check:
Is this the entire window? It seems odd that the view is down in one corner.
Does it help if you add an increasing rotation before the rendering? That can make it easier to determine the perspective, by giving more clues.
You can also try moving the "camera" around, by changing the arguments to gluLookAt() dynamically.