|
Author |
Topic: why beginCamera() and endCamera()? (Read 1994 times) |
|
arielm
|
why beginCamera() and endCamera()?
« on: Apr 25th, 2003, 6:40pm » |
|
after looking at the "opengl man pages", i think i understand better perspective() and lookat(), but i didn't found there anything similar to beginCamera() / endCamera()... my own theory for why these statements are needed is: - if, within loop(), processing is not finding any beginCamera(), it uses it's own perspective() and lookat() settings... make sense? another question in the same field: i understood from the opengl literacy that i read that gluLookat() is doing nothing else than translate() and rotate() stuff... and that it's possible to implement custom viewing transformations instead: so i'm asking if theses custom transformations should also take place within begin...endCamera or it doesn't matter? ... actually, after digging a bit more: lookat() and any other custom viewing transformation are doing okay outside begin...endCamera... it seems that only perspective() needs to be "inside"...
|
Ariel Malka | www.chronotext.org
|
|
|
fry
|
Re: why beginCamera() and endCamera()?
« Reply #1 on: May 1st, 2003, 5:17am » |
|
beginCamera and endCamera are so that you can avoid the glMatrixMode-style calls. MatrixMode seems like a weird concept (i've had a tough time explaining it to people in the past) so we get around it by doing an internal call to something equivalent by doign the begin/end thing, since that fits the mental model behind our other begin/end commands. p5 does a beginCamera() with its own perspective as a one-time thing, and it gets wiped out if someone else calls beginCamera. (no attempts to auto-detect or sense if your code contains beginCamera) that make sense?
|
|
|
|
arielm
|
Re: why beginCamera() and endCamera()?
« Reply #2 on: May 1st, 2003, 1:51pm » |
|
got you... so to recap, internally, at the beginning of loop(), p5 is using something like: fov = 60; eyeX = width / 2.0f; eyeY = height / 2.0f; eyeDist = eyeY / ((float)tan(PI * fov / 360f)); nearDist = eyeDist / 10.0f; farDist = eyeDist * 10.0f; aspect = (float)width / (float)height; beginCamera(); perspective(fov, aspect, nearDist, farDist); lookat(eyeX, eyeY, eyeDist, eyeX, eyeY, 0, 0, 1, 0); // this lookat statement is quite similar to translate(-eyeX, -eyeY, -eyeDist), no? endCamera(); then... if a beginCamera() statement with at least one perspective() statement inside is encountered: the original perspective() and lookat() are "canceled", so you'd better use a new lookat() statement or your own custom viewing transformations, otherwise your "eye" will be at (0, 0, 0), looking downwards... concerning the cancelation of the original lookat(), i guess it is done by something like translate(eyeX, eyeY, eyeDist), or even by resetting the transformation matrix, so i guess this is the reason why beginCamera() stuff should always be placed at the beginning of loop()... now, if i try to transpose the things to opengl, it's like p5 is doing internally, at the beginning of loop(), something like: glMatrixMode(GL_PROJECTION); gluPerspective(fov, aspect, nearDist, farDist); glMatrixMode(GL_MODELVIEW); gluLookAt(eyeX, eyeY, eyeDist, eyeX, eyeY, 0, 0, 1, 0); finally, a question: is there something similar to glLoadIdentity() in p5, or should we only rely on push() and pop()? thanks!..
|
Ariel Malka | www.chronotext.org
|
|
|
fry
|
Re: why beginCamera() and endCamera()?
« Reply #3 on: May 1st, 2003, 5:04pm » |
|
yeah, though you also load identity after switching the mode to get things cleared. and the lookat should be happening in the projection, not MODELVIEW since that will affect your objects in weird ways. it'll mostly work, but it's not the correct way to do it. Code:glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(fov, aspect, nearDist, farDist); // probably a few more lines in here gluLookAt(eyeX, eyeY, eyeDist, eyeX, eyeY, 0, 0, 1, 0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // rotate/translate/etc here to affect your shapes |
| which in p5 looks like Code:beginCamera(); // no need for loadIdentity, it's done behind the scenes perspective(fov, aspect, nearDist, farDist); lookat( ... ); endCamera(); |
| glLoadIdentity in p5 is called "resetMatrix". (and fwiw, glMultMatrix is applyMatrix) beginCamera() calls resetMatrix() to clear things out. endCamera() grabs the current 16 matrix values (the 4x4 that's getting set up, stored as g.m00, g.m01, etc) and copies them to the internal 'perspective' matrix (g.p00, g.p01, etc). then resetMatrix() clears out mNN again, since perspective() and the rest are calculated inside g.mNN. when transforming a point, the perspective matrix (p00 etc) is multiplied by the model matrix (m00 and friends). that matrix is applied to the xyz point to be rendered, and the result gets transferred to the screen. phew!
|
|
|
|
|