theory behind 3d engine

BlitzMax Forums/OpenGL Module/theory behind 3d engine

slenkar(Posted 2008) [#1]
when using openGL do you have to simulate a camera turning?

I have an idea how to put objects in front of the camera and translate the camera,
I also can use the rotate command to turn the objects but how do you turn the camera?


ImaginaryHuman(Posted 2008) [#2]
Don't.

I have read in a few places that any lighting calculations that you might want to do later would be incorrectly calculated if you try to translate or move the camera. I might be wrong, but they suggest it is better to move the model itself and put it in front of the camera, leaving the camera in its default position.

You select the modelview matrix. Then you translate and rotate the model to show whichever part you want - it's sort of the inverse of the movement you'd make to the camera.


nawi(Posted 2008) [#3]
Some parts of my 3d engine (I might release the sources sometime, it's a frozen project anyway)..

'Position camera
glRotatef(NCam.Pitch,1.0,0,0)
glRotatef(NCam.Yaw,0,1.0,0)
glRotatef(NCam.Roll,0,0,1.0)
glTranslatef(NCam.X,NCam.Y,NCam.Z)

...

'Loop entities
For Local NEnt:NEntity = EachIn NEntity.NEntityList
    If NEnt.Parent = Null Then RenderEntity(NEnt,NCam)
Next

...
Method RenderEntity(NEnt:NEntity,NCam:NCamera)
	glPushMatrix()
	glTranslatef(NEnt.X,NEnt.Y,NEnt.Z)
	glRotatef(NEnt.Pitch,1.0,0,0)
	glRotatef(NEnt.Yaw,0,1.0,0)
	glRotatef(NEnt.Roll,0,0,1.0)
	glCallList(NEnt.DisplayList[NEnt.Frame])
        For Local Chi:NEntity = EachIn NEnt.ChildList
	     RenderEntity(Chi,NCam)
	Next
	glPopMatrix()
End Method


Of course, I left out most of the code in the render routine etc, but should give you the idea. That also shows how parent/child system works.


DavidSimon(Posted 2008) [#4]
A camera is just a virtual construct that holds a local matrix. There is no camera entity, as such, in GL. You do get a few helper functions, to simulate the operation of a camera. gluLookAT for example. But those are just for quick testing/newbies really.

Simpliest way to put it.

You have 3 matrices.

1. Model Matrix
2. Camera Matrix
3. Projection Matrix.

To get the final matrix your transform your vertices by, you,

FinalMatrix = ModelMatrix*CameraMatrix*ProjectionMatrix


it's often useful to pass each matrix individually to a shader, then you can do model or projection space calcs, for things like temporal motion blur.

In short, use matrices. The first thing you ever do when writing a 3d engine, is write a solid vector3/matrix class(With interactivity between the two) then build your scenegraph around those.


Or if you're feeling lazy, just refactor some xna matrix code like i did this one time in band-camp. ah those late nights playing sax.....