glortho zox plane projection - c++

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);

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 screen-space translation of perspective camera view

I am using a perspective projection camera in OpenGL with an eye-target-up approach along the lines of gluLookAt()
I would like to move the entire 3d view to a different location within the 2d window, as shown in the image below:
I know this can be done with glViewport(), but I would ultimately like to be able to rotate the 3d view in addition to translating it, which I understand won't work with a glViewport approach.
I also understand that taking a non-glViewport approach will require the additional step of stenciling. I'll leave that step for later.
For now, I'm hoping to simply translate the 3d view as shown in the image above (ignoring the stenciling issue).
It seems like translating the projection matrix is the correct approach, however the translation extent is not what I expect. Perhaps I need to scale the parameter I'm using? In any case, I'm hoping someone can explain how to (more-or-less) simulate a translatable/rotatable glViewport for a 3d perspective view.
Thanks!
You use glViewport(). For each view port, you set the viewport, setup model view matrix and draw scene.
For first viewport
glViewport (0, 0, window_width/2, window_height/2);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
//do something with projection matrix
drawScene();
For second viewport
glViewport (window_width/2, 0, window_width/2, window_height/2);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
//do something different with projection matrix
drawScene();

modelview and projection - which code is more common?

1.which code is more common and why?
2. How will you describe the meaning of this code?
A.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluLookAt(1,1,1,0,0,0,1,1,0);
B.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1,1,1,0,0,0,1,1,0);
B. It correctly separates the modelview/"camera"-type transforms from the projection matrix.
There are some concerns with putting "camera"-type transforms in the projection matrix:
Lighting: OpenGL has to transform vertex normals into world coordinate
space - that is to say WITHOUT the effects of perspective - but WITH
the effects of the camera position. Hence, only the GL_MODELVIEW
matrix is applied to the normals for lighting. If you put the camera
transform into the GL_PROJECTION matrix then your lighting will be
wrong. However, some people do their own lighting - and in any case,
it can be a subtle error that you might not have noticed previously.
Fog: OpenGL has to figure out how far each vertex is from the camera.
Once again, perspective effects are not relevent to this calculation -
so the GL_PROJECTION matrix is not used. If you put the camera
transform into the GL_PROJECTION matrix then your fogging will be
wrong. Since few people use fog, many people have the error and don't
know it.
TexGen: Since OpenGL uses the eyepoint to figure out some of the
TexGen'ed texture coordinates, if the camera position is all mixed up
with the projection information then you'll end up with some pretty
strange texture coordinates. (Thanks to Brian Sharp at 3Dfx for
pointing this one out)

Converting Orthogonal Camera to Perspective OpenGL

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.

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.