Camera Position Switch

Quick question:

I have an object with a camera pointed at it. I want to move the camera to the object’s location, point it at the old camera position, render the image into a buffer for use as a texture, and then put the camera back where it was. Can someone give help me out with the specifics? For example, how can I store the first cam position so that it’ll know where to point, and where to return to? How do I select a certain buffer to render to, which one should I use, and how can I turn it into something I can make a texture from? (I know how to load a texture from an RGB file, but this is a little complicated for me.) I’d really appreciate any help, cause I can’t seem to get started.

Matrix can be easily stored with glPushMatrix, looking to an object - by gluLookAt. You can obtain camera location from matrix(four “bottom” values are 4d vector).
As for buffers… It seems to me that there are no buffers except front and back in Macrohard implementation. Try glDrawBuffer with aux buffer, but it will not work probably. So better render to backbuffer as usual and then clear it again.
You can obtain texture by glReadPixels - it will be in needed format. But it is not very fast.
Are you creating smth like mirror?

Yeah, it’s for a mirror. So what’s the syntax for pulling values out of the modelview matrix like that? And when I was talking about buffers, I meant color buffer and depth buffer and all that. Our prof was talking about something like this the other day, and he said something about rendering it to the color buffer and not the depth buffer, or the other way around or something. I didn’t really follow it. Speed doesn’t matter, just generating a static image.


Matrix can be get by glGetdoublev(GL_MODELVIEW_MATRIX)
Mirror can be made without textures. By Stencil.
1)CLear stencil, render mirror and mark it with stencil
2)clear depth buffer, restrict rendering to stenciled region and create clipping plane
3)multiply matrix by mirroring matrix and ,change polygon face to CW render scene again
4) change mode back to CCW
You can find sources(for Delphi, but they are pretty clear) in
unzip, you’ll find glclAux18. In it procedure is called tglReflector.hidecolor
If something is still unclear ask again

If you need to do it with textures, you can do this:

Set the viewing matrix so that it will be square.

Set the viewport to something like 256*256

Store the camera position in an array of 3 floats.
Store the object’s position in an array.

Use gluLookAt to set the camera position to the object position, and the look at position to the previous camera position

Render the image.

Use glCopyPixels to get the image from the back buffer.

Load the image information to a texture.

Set the camera position, viewport, and viewing matrix back to whatever they were before.

Draw the image using the texture you just got.

By the way, how did you make out with that reflection/refraction thing?


For some more information on this, try the OpenGL Programming guide:

Chapters 8 and 10 should be helpful to you.


Thanks for all the responses guys. Sorry I didn’t check this sooner, but I was crashed out asleep after a 40 hour programming session. I ended up using the exact same method that J outlines above. I stored it in a texture, and mapped it onto my fishbowl - looked pretty good after I set a decent transparency. Then, to get the refraction, I did the same texture trick, but instead of the original cam position, I looked at that position’s inverse from the bowl (so along the same line of sight). So the two textures made semi-transparent and blended together made a pretty decent effect. Not perfect, because I don’t really have a refracting angle calculated anywhere, but the final image looks pretty much like you would expect a fish bowl to look. Using those methods is pretty damn slow though - I’d never recommend it for real-time. Thanks again for the help!

Thanks for all the responses guys. Sorry I didn’t check this sooner, but I was crashed out asleep after a 40 hour programming session.

40 HOURS! Woah. That’s pretty incredible.

I’m glad to hear that you got it working in the end.