Dark Bit Factory & Gravity
PROGRAMMING => General coding questions => Topic started by: Pixel_Outlaw on January 31, 2009
-
I have a project that has been done entirely in OpenGL's orthographic projection mode. It has now become necessary to draw some perspective distorted shapes in the background after the orthographic objects are drawn. Since the coordinate systems are different between the two modes, is it possible do render objects without perspective then render perspective corrected objects in the same scene without too much hassle? Basically I have foreground objects that must be rendered without perspective correction, then I want objects in the background to be rendered with perspective correction to imply a scene. The program already is compleatly coded for the orthographic projection and it would be pure hell to recode for the other coordinate system just to include some perspective distorted objects. This is a really odd request I know but I want some scrolling buildings in the background that are distorted while the important foreground interactive objects remain undistorted.
The main problem between the ortho and perspective mode lies in how the origin is positioned. In ortho it is in the corner while in perspective it must lie in the center.
-
You can just switch from orthographic mode to perspective mode and vice versa.
orthographic mode (800x600):
glViewport(0, 0, 800, 600);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0f, 800.0f, 600.0f,0.0f);
perspective mode (800x600):
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,800.0f/600.0f,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
-
What rbz said, in fact I am doing something similar in something at the moment, only the other way around to you :)
-
Thanks so much. I will try when I can get home.
-
It seems that now there are two issues.
In perspective mode, my y value seems to be reversed.
The screen in perspective mode is only 1 unit across.
Also the lighting model direction is now different between shapes drawn in both modes.
This is kind of what I meant earlier.
'render loop
while 1
glLoadIdentity()
set_perspective()
'render background objects
set_ortho()
'render foreground objects
flip
wend
Function set_ortho()
glViewport(0, 0, 800, 600) ;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 800.0, 600.0, 0.0) ;
End Function
Function set_perspective()
glMatrixMode(GL_PROJECTION) ;
glLoadIdentity();
gluPerspective(30.0, 800.0 / 600.0, 0.1, 100.0) ;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
End Function
-
The screen in perspective mode is only 1 unit across.
That's at z=-1 (and a field of view of 45 degrees).
Objects further away have more units to travel; that's the concept of perspective (see theorem on intersecting lines (http://en.wikipedia.org/wiki/Intercept_theorem)).
If your foreground objects are sprites (textured quads) it's actually pretty comfortable to set their position and size in a resolution independant manner in perspective projection at z=-1 and fov=90 in a range between -1..+1.
In perspective mode, my y value seems to be reversed.
Just try to swap top and bottom.
Better use glOrtho to have identical z-clipping-planes in perspective and orthographic projection.
Also the lighting model direction is now different between shapes drawn in both modes.
Make sure the current transformation matrix is set to identity before setting the light-position.
-
Hmm I've had some time to tinker with this a bit and there are still some concepts that are escaping me. I sort of don't know what I don't know with OpenGL. Since I've read the responses here and am still feeling a bit lost perhaps I could post what I do have.
Here is my initial setup.
' initiates Opengl
AppTitle = "Gun Ranger X Recode"
GLGraphics 800, 600
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glMatrixMode(GL_PROJECTION)
glOrtho(0, 800, 600, 0, 1000, - 1000)
glMatrixMode(GL_MODELVIEW)
glClearColor(color_1.r, color_1.g, color_1.b, 1.0)
Global light_position:Float[] =[- .5, - .5, - .5, 0.0]
glLightfv(GL_LIGHT0, GL_POSITION, light_position)
glShadeModel(GL_SMOOTH)
Please feel free to correct me on anything in this.
This is my rendering loop.
Method draw()
glLoadIdentity()
' enable lights for rendering
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
' set perspective to draw sky scrapers with perspective
set_perspective()
update_scrapers()
' attempt to return to ortho mode for rendeing shapes without perspective
set_ortho()
cell_material.set_material()
update_cells()
'disable lighting for our flat 2d elements
glDisable(GL_LIGHTING)
update_ship_shots()
player.update()
update_cell_shots()
glColor3f(color_3.r, color_3.g, color_3.b)
glTranslatef(0.0, 0.0, 0.0)
GLDrawRect(0, 0, 200, 600)
GLDrawRect(600, 0, 200, 600)
glColor3f(color_2.r, color_2.g, color_2.b)
glTranslatef(0.0, 0.0, - 0.1)
GLDrawRect(192, 0, 4, 600)
GLDrawRect(604, 0, 4, 600)
GLDrawRect(200, 400, 400, 1)
GLDrawText("Score: " + score, 16, 16)
GLDrawText("Level: " + level, 16, 32)
' draw lives
For Local i:Int = 0 To lives - 1
GLDrawRect(8 + (i * 16), 64, 14, 14)
Next
glTranslatef(0.0, 0.0, - 0.2)
glColor3f(color_1.r, color_1.g, color_1.b)
For Local i:Int = 0 To lives - 1
GLDrawRect(9 + (i * 16), 65, 12, 12)
Next
GLDrawText("Bomb: " + Int(player.scrape_percent * 100) + "%", 200, 600 - 14)
End Method
' functions for switching modes
Function set_ortho()
glViewport(0, 0, 800, 600) ;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 800.0, 600.0, 0.0) ;
glTranslatef(0.0, 0.0, 0.0)
End Function
Function set_perspective()
glMatrixMode(GL_PROJECTION) ;
gluPerspective(10.0, 800.0 / 600.0, 0.1, 100.0) ;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
End Function
As soon as the perspective part finishes it refuses to draw my 3D shapes.
My 3D shapes are drawn like this maybe the problem lies there.
glloadidentity() ' reset any rotations and scaling
gltranslatef( position_x,position_y,position,z) ' move to desired location
glrotate(direction_of_travel) ' rotate to direction of travel
draw_shape() ' call the shape
Sorry, I don't want it to seem that I'm not listening but I am reading the provided links and trying to grasp what is going on. I thought maybe the information clashes with something that I am doing wrong.
-
You probably want to go back to the modelview-matrix at the end of set_ortho.
-
Thank you so MUCH!
Finally got it working as needed. You are a great help to the forum.