opengl perspective view clipping glutSolidTeapot too early - c++

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

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.

Trouble with partially rendering viewport

I use the following code trying to render a partial frame of a viewport.
Start and end is a part of the frame, which is correct.
glViewport(start,0,end,SAVE_HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0f, 1.0f, NEAR, FAR);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
The code seems out of order; I had it working before. From what I could find of sparse documentation, this seems the correct order however.
The visual output is that I do not see some parts, and the parts that are rendered do not have the correct perspective. As a note, I am rendering to the same FBO each time.
The expected output is that each time the code is called a part of the viewport is rendered to the same fbo.
My question: how can I render to an FBO partially each iteration?
I was looking for glScissor apparently.

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

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.