getting the perspective i want

dave05
Unregistered
 
Post: #1
hi... another question about OpenGL here. I've been using glOrtho and gluPerspective together recently, and I assumed they were doing what I expected them to do, but apparently that is not the case.

I set zNear and zFar to 10 and 1000 using both glOrtho and gluPerspective, and... Clipping seems to occur for objects with z-coordinates greater than 25! I guess there must be some sort of matrix multiplication that occurs that screws this up.


Anyhow: each frame before I do any drawing I make the following calls

Code:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-400, 400, -300, 300, 10, 1000);
gluPerspective(30, 1.33f, 10, 1000);

now, what I want to happen is-
1. vertices get clipped at z < 10.
2. at z = 10, the field of view is [-400->400] on the x axis, and [-300->300] on the y axis
3. at z = 1000, the field of view is roughly double. so [-800->800], [-600->600].


I'd appreciate some kind of explanation of how this can be done, or, maybe, a link to some source with an explanation of some kind. Wacko

quite busy today... again, you guys have been a great help
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
you don't want to be doing both glOrtho and glPerspective... who knows what that will do o_O

pick one and stick to it Rasp
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #3
I'm pretty sure that you don't want to do both the call to glOrtho() and gluPerspective(). Those matrices get multiplied together and will do something really bizarre. I'm pretty sure that that's not what you want, just do the call to gluPerspective(). You'll need to change your FOV to get what you want though.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
dave05
Unregistered
 
Post: #4
the problem is, when i only use glOrtho(....,10,1000), it still clips strangely.

How do you set the X and Y axes using gluPerspective?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
use a gluLookAt on your modelview matrix, after you're done messing with the projection matrix.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #6
OneSadCookie Wrote:use a gluLookAt on your modelview matrix, after you're done messing with the projection matrix.

Wouldn't you want to keep that in the projection matrix so you don't need to reapply it to the modelview matrix every time you reset it to transform an object?

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #7
do this instead,
Code:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(-400, 400, -300, 300, 10, 1000);
gluPerspective(30, 1.33f, 10, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

if vertices get clipped at z > 25, check you are clearing the depth buffer, there's nothing in the way (disable depth testing to check this).

Skorche Wrote:Wouldn't you want to keep that in the projection matrix so you don't need to reapply it to the modelview matrix every time you reset it to transform an object?
IMO its best to put it in the modelview matrix because its not part of the projection, also you only need to keep applying it if you do load identity somewhere.

dave05 Wrote:How do you set the X and Y axes using gluPerspective?
Eh?

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #8
Unknown: But don't you reset the modelview matrix all the time when placing objects? Maybe I'm just doing things in a really stupid way, but I reset, translate and and rotate for placing every object.

Also, I think he meant rotating the world by "setting the x and y axes." Which would be covered by gluLookAt().

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #9
I dont reset the modelview matrix as often as I push and pop it though.

like I'd set the set the camera (with gluLookAt for example)
then glPushMatrix, translate, rotate, draw object, glPopMatrix for each object.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #10
Things like fog and lighting don't work correctly if the viewing transformation (gluLookAt) is in the projection matrix as opposed to the modelview.

If you're ignoring those (not using them, or implementing them yourself with shaders) then you can put your gluLookAt wherever you like Smile
Quote this message in a reply
dave05
Unregistered
 
Post: #11
yes, OSC, I am implementing fog and lighting myself.

So.....

if i use gluPerspective and gluLookAt (NOT glOrtho. haha) like so:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50.0, 1.0, 3.0, 7.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyex, eyey, eyez, ctx, cty, ctz, upx, upy, upz);

1. should my eye location be at or before znear? since my znear is 10, I'm thinking maybe eye_pos = {0,0,0} (wouldn't the area of the FOV at 0 distance from the "eye" be 0?)

2. obviously up = {0,1,0}

3. should centre be the centre of the BACK of my scene? or somewhat closer than that? Right now I'm gonna test using the above settings and centre = {0,0,1000}


PS. I love you guys.

update: using the settings above, openGL still displays nothing when i try to draw a radius 75 sphere at {0,0,100}, {0,0,200} ... {0,0,1000}, as well as {0,0,0} which i tested due to what worked before. I hate the camera! Should I reduce all my coordinates and try znear = 3, zfar = 10 or something???
Quote this message in a reply
dave05
Unregistered
 
Post: #12
just fyi, here's my full glitchy pre-drawing initialization. I've commented out gluLookAt for now.

Code:
aglSetCurrentContext(gAGLContext);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Set up the camera's lens
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30, 1.33f, 10, 1000);
// Set up the camera's aim
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(0, 0, 10, 0, 0, 1000, 0, 1, 0);

// DRAW THE USER INTERFACE !!!
UI_DrawScreen();

as a side note:
#include <Carbon/Carbon.h>
#include <AGL/agl.h>
#include <OpenGL/glext.h>
#include <IOKit/hid/IOHIDLib.h>
#include <ppc_intrinsics.h>

...is this enough to include glu Methods?
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #13
OneSadCookie Wrote:Things like fog and lighting don't work correctly if the viewing transformation (gluLookAt) is in the projection matrix as opposed to the modelview.

If you're ignoring those (not using them, or implementing them yourself with shaders) then you can put your gluLookAt wherever you like Smile

Oh, I didn't know that. Also never used them.

Using the matrix stack makes sense I guess. I've always just used that for sub pieces.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
dave05
Unregistered
 
Post: #14
lower Z are farther from the camera?!?! My entire perceptiton of reality is thrown into question.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #15
The positive Z axis is facing the camera, in which case positive Z is behind you, and negative Z is in front of you.

BTW, for just OpenGL and AGL, all you need is
#include <AGL/agl.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>

Add more as needed. You likely won't want to use ppc_intrinsics since, as its name implies, you can only use it for PPC.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL Perspective Screen to Obj Coordinates Mapping Question WhatMeWorry 5 9,170 Feb 17, 2011 05:40 PM
Last Post: Holmes
  Perspective View Question, (GL_PROJECTION Matrix), and GUI hangt5 3 5,236 Sep 11, 2005 11:17 PM
Last Post: hangt5
  dim3 Perspective - How does Quake/UT do it? ggadwa 3 2,962 Jun 16, 2005 08:26 AM
Last Post: PAD Games
  2D to 3D and back (Ortho and Perspective) Phate 5 5,507 Nov 30, 2003 10:56 AM
Last Post: AnotherJake
  OpenGL - mixing perspective &amp; orthographic projections mars 5 5,703 Jul 2, 2003 06:34 PM
Last Post: Iceman