Reflecting local textures on object

Hello all!

Hope someone can give me a pointer. I need to reflect a texture on a CAD model.

I have a simple rectangular quad with a texture on it. This quad is not infinitely far away (like in cube mapping), it has a local position and orientation. The texture contains a pattern to be reflected on all other models in the scene.

Currently I’m taking the ray from the camera eye to each model vertex, reflect it at it’s normal, and find the point where the reflected ray hits the quad (if it does). The hit position is transformed to texture coords.

This works well for the vertices, but since texture fragments are always mapped linearly on the triangles, the appearance gets distorted. The effect gets worse when the camera comes nearer to the model, of course. But my camera must be near the model and it must be a perspective view (orthogonal projection would work fine since mapping is always linear then).

My question is:
Is there a way to get rid of this distortion?

My opinion: It’s not possible with “normal” texturing methods. The solution would be to apply the reflection per fragment, not per vertex. Is this resolvable using GLSL fragment shading? (Don’t have a clue about shaders… yet…)

Thanks for your help and hints!


Are you sure you can’t use cube mapping ?

Originally posted by jide:
Are you sure you can’t use cube mapping ?
Hm… thinking… yes!

Since cube mapping (just like all other methods of environment mapping) assumes the emitting source to be infinitely far away. This (nearly) turns the perspective projection from reflector to emitter to an orthogonal projection. That’s the trick of all environment mapping, as I did understand it (?).

In my case, the emitter is placed inside of the scene. Think of a television screen (quad with texture) and a teapot placed directly before it. I need a good (correct!) reflection of the TV screen on the teapot while rotating my camera around both objects.

Maybe I’m wrong with my assumptions about environment mapping?


I think what you are missing is that the easiest way to achieve what you want is to project the objects you want onto a cube map and then use that for your reflection.

Google for “dynamic cube mapping”.

Originally posted by rgpc:
Google for “dynamic cube mapping”.
Done. Thanks for your answer!

Let me see, if I got that right (retaining my TV-teapot-example):

  1. The TV-Screen is rendered from the teapot’s point of view onto a cube map.
  2. This cube map is used as environment map when rendering the teapot itself.

The problem here is the “teapot’s point of view”. The reflection is only correct at this point. For all other vertices, it’s an approximation, which btw looks very good (found some tutorials about this topic), if the reflecting objects aren’t too concave or streched. But in fact, it’s fake, isn’t it?

Meanwhile, I’m beginning to see that my wishes can only be fulfilled in raytracing… sigh… or I’m just blockheaded.


You can do localized cubemapping - which is still an approximation - we prefer that term to “fake” :wink: . Nvidia has a presentation on this from GDC a couple of years ago:


@Mikkel: Good stuff!!!

Assimilated that GDC presentation and accordingly had two hours of funny playing with FX Composer.

Oh boy, if only that “card reflection mapping” wasn’t a fa… approximation. :slight_smile:

See that shot? The “D” matched perfectly from this point of view, but the “n” is completly beaten up. Of course, if the texture doesn’t touch the model, it looks perfect.

Suppose that’s the best I can get in real time rendering, hm?


(Btw your six letter word… amazing!!)

of course you could do this perfectly in a fragment shader.
just interpolate the world position and normal using varying’s, and do the full reflection calculation (does it intersect with the quad, if so, calc the texcoord and look it up using a 2D sampler).

Ran to the bookstore and bought Rosts “OpenGL Shading Language” to see what exactly knackered was talking about. Varyings? Samplers? Never cared about shaders - thought they were only useful for gaming effects.

Oh, ignorance.

Downloaded Render Monkey yesterday and got my reflections working using a fragment shader, exacly the way knackered explained:


[EDIT] Updated shot! Last one was crap.

Thank you all for pointing me to the right direction! See you in the shader forum! :slight_smile:


Glad I helped, but I’m surprised you thought so little of shaders before-hand. Man, are you going to enjoy shader programming, the mind boggles at what’s possible with them.
BTW, that reflection in the screenshot doesn’t look right. there’s only one grid cell in front of the base of the teapot, but the reflection shows 3 grid cells!

basically what the OP wants to do is impossible (unless they raytrace or someit)
a cubemap will only give the correct reflections from one position only

That looks pretty good. It does seem like you flipped a sign somewhere.

I just looked at the “card reflection mapping” video, and I get the impression that their mehod is conceptually the same as what you did. From the video, it seems they just computed the view reflection’s intersection with the plane and did a texture look up. They probably just did not handle the case where the object intersects the plane.

If that’s the case, it seems a little silly (and confusing) that someone would try to coin the term “card reflection mapping” to basically mean “ray-plane intersection”…

zed, the original poster wanted to reflect a single local quad, not an environment map. Cube mapping was suggested as a possible solution, but as you say, it’s an approximation.
A single local quad can be reflected perfectly over the faces of any object using fragment shaders.
canuckle, yes they’re just using six quads with a cube map texture mapped on to them to give perfect reflection.

Originally posted by knackered:
I’m surprised you thought so little of shaders before-hand.
The problem is my intended audience. I’m doing CAD stuff and simulation. My app needs to run not only on latest hardware but also on notebooks. I need to be GL1.2 compliant.

Adding features that need >1.2 can therefore not be sold as standard application feature. Hence, until now, my focus was far away from shaders. But this will change…

The second reason is, that in game programming nearly everything is fake (though people prefer to call it approximation). Goal is to show cool effects with high framerate. In CAD this is insufficient - you run the risk of loosing seriousness.

Reflecting per fragment is serious, because it’s correct. And there are applications for it, especially in robotics.

I made a new shot with a correct texture. On the old one the clamping was wrong.

And btw, take a look at the cool idea that HellKnight posted! I will keep it in mind, if I ever need better-than-normal environment mapping.


knackered: Yeah, the “local cube maps” talks about using 6 quads, but later he talks about “card reflection mapping” which is only using 1 quad. IMO, the slides kind of make it seem a lot more confusing than it actually is…

CatDog: that screen shot looks real sweet! But for some reason, the reflection looks a little bright… one idea would be to also simulate the reflection’s quad’s lighting. for example, instead of using the raw texture value, actually modulate that value by the reflection quad’s (i.e. the tiles’) diffuse lighting at the intersection point.

Also, if you want to go overboard, consider a Fresnel effect on the pot to make it look less metallic =).

Originally posted by canuckle:
But for some reason, the reflection looks a little bright…
Well, I added some simple diffuse ligthing to the teapot to emphazise the reflection over the texture quad. The texture quad itself is not lit at all.

My only intention was to proof (to myself), that true reflections can be done using shaders. But of course, spending a hole day on gold plating the effect would be much fun. :wink:


CatDog: Haha, right on. Keep us posted on any updates =).