I loaded in a wavefront *.OBJ-file and made a routine that returns the bounding box / limits of the obj-file using min/max on all the vertices… My bounding box function returns 6 numbers:
- min/max of all X-vertices => I calculate dx as the width/span in X-direction…
- min/max of all Y-vertices => I calculate dy as the width/span in Y-direction…
- min/max of all Z-vertices => I calculate dz as the width/span in Z-direction…
I then initialize my rotation center ( rotCenter ) to 0,0,0 and translation: tx = ty = tz = 0 and rotation around two axes: rx = ry = 0.
Incomplete code (python) - to give you an idea:
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(90.0, width/float(height), zNear, zFar) # width = 800, height = 600 pixels (as an example)
glMatrixMode(GL_MODELVIEW)
while running:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
# Move to center of object (consider special add/subtract in Z-dir!):
glTranslate(-rotCenter[0], -rotCenter[1], -rotCenter[2]) # not sure if plus/minus, however not an issue now
# add user-defined viewpoint:
glTranslate( tx, ty, tz ) # tx and ty is ok (halfways). tz should NOT be halfways into the bounding box/object...
glRotate(ry, 1, 0, 0)
glRotate(rx, 0, 1, 0)
# Below here I display my wavefront *.OBJ file etc...
# ........ not too relevant code follows below ....
This code means that I start in the middle of my *.OBJ file and I have to zoom out before I can see my whole object / *.OBJ-file (have to start somewhere).
Now, I have x-axis right, y-axis up and z-axis from my screen and pointing out of the monitor. I want to initialize tz to a value to be a “good value” so when I start the program, I’ll immediately see the object in front of me - maybe taking up 75% of the vertical screen space (in the y-direction) - instead of now, where the initial position is in the middle of the bounding box of the object… How should I change my gluPerspective(90.0, width/float(height), zNear, zFar)-function ? It could also be that I wanted my object to take up 60% of the horizontal screen width, maybe ?
So I have dx = the width of my object and dy = the height of my object…
Basically I’m asking you: Which value should I initialize tz to ?
Take an example: The bounding box has dx=2000, dy=1000, dz=200 (something very wide, half as tall and 1/10 of the width in the depth-direction)… Which formula could I use to get a good initial tz-value and how to implement it in my program code ? Must be some frustrum/viewport/viewpoint equation…?
Sorry, but I cannot seem to figure out, what is the best/nicest way of solving this problem… I’m a bit of a noob, although I now can solve the most basic things/problems myself. Here I need a good idea/suggestion to continue my program. I hope you understand… I highly appreciate ANY ideas/suggestions/references.
Thank you (and please ask, if something is unclear, I hope you get the idea of what I’m trying to solve here)!