LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Making fast reflections in openGL (https://www.linuxquestions.org/questions/programming-9/making-fast-reflections-in-opengl-245950/)

cjp 10-22-2004 09:36 AM

Making fast reflections in openGL
 
I'm making a racing game with openGL, but real-time updated reflections make it really slow. On the same computer, NFS underground seems to do exactly the same thing much faster. I don't think it can be the difference between directX and openGL, because ATI also provides good openGL support for my Radeon 9200 card, and DirectX 8+ and openGL aren't that much different. Of course it could be my code that's inefficient, but I don't think that that is the case, because of this:

Reflections are done by applying a texture to an object that describes its environment (the "environment map"). To update the environment map, you need to render the scene from the point of view of the object 6 times (left, right, top etc.), and put the results in the environment map. So after every rendering step you need at least one glCopyTexImage2D call to put the result in a texture. I've measured that these calls take approximately 2 ms, so it takes at least 6x2 = 12 ms to update a reflection.

Let's say that 25 fps is reasonable. That gives us 1/25 = 0.04 s = 40 ms for every frame. So with 12 ms per reflection, you can only update 3 reflections per frame. In reality it will be even worse, because you also need to render the scene 1 time + 6 times per reflection. But in a game like NFS underground I can see many cars and even the road having reflections, while the frame rate is still good. So how is that possible?

avarus 10-22-2004 01:08 PM

Hi,

It's been a while since I did OpenGL at university, but I remeber the way that we were told to do reflections is as described here:

http://www.opengl.org/resources/code...s/Reflect.html

I won't say any more since this guy puts it much better than I can.

TIM

deiussum 10-22-2004 01:30 PM

I haven't done much with cubical environment maps, which is what I am assuming you are using since you said you render the scene 6 times from different view, but there are a couple things you might be able to try.

First, you could try to use glCopyTexSubImage2D instead of glCopyTexImage2D. I believe that glCopyTExSubImage2D is generally a bit faster.

Second, when rendering for your environment map, you can try to use some tricks to make those renders go faster. Maybe turn off lighting for those, render into as small an area as possible, etc...

Third, if that none of that works, you could try and fall back to normal environment mapping, which only uses a single distorted texture instead of 6 textures from different directions...

Edit:
I briefly looked at the link that was posted. If you just are just reflecting off of a plane (like a mirror), that method should work pretty well. If you are trying to make an irregularly shaped object reflect its environment, environment mapping will probably give you a better result.

cjp 10-26-2004 03:31 AM

I already did a lot of these things. Turn out lighting, depth testing, blending etc.

BTW currently I'm using sphere mapping, but for updating your sphere map you still need several rendering steps. In the future, when I'm going to support cube mapping, I'll be able to skip the final compilation of the sphere map, but that's not the thing that takes most of the time.

I know about the option to use a "static" environment map, but that's not what I'm talking about. I know that environment maps in NFS are dynamic (at least for cars close to the camera).

Thanks for the glCopyTexSubImage2D hint, I'll try it. Why would it be faster than glCopyTexImage2D?

worldmagic 10-26-2004 06:02 AM

I know very little about OpenGL, so I guess my words are light on the subject. But maybe you can change the Field Of View to render all sides of your "box" at one time? And then develop a static alorithm that transforms the result image into 6 images (abit streched and such, but might work?). Im thinking of gluPerspective. Im guessing you already do this when you use Spheric mapping, but from what I see you only need one render.

deiussum 10-26-2004 08:29 AM

Quote:

Originally posted by cjp
Thanks for the glCopyTexSubImage2D hint, I'll try it. Why would it be faster than glCopyTexImage2D?
That I'm not exactly sure of myself. I seem to remember people recommending that on the OpenGL forums a lot, though. BTW, if you don't get a satisfactory answer here, there are a lot of very knowledgeable people over at the opengl.org forums.

cjp 10-28-2004 04:58 PM

I tried glCopyTexSubImage2D, and it really works. On my system, it takes 0,41 ms on average, compared to 2 ms for glCopyTexImage2D. Now I should only optimise the rendering of the scene, which is still 1,5 ms, but that's my problem :D


All times are GMT -5. The time now is 11:50 PM.