Reflection in Opengl in C?

Asked

Viewed 522 times

7

Hi, I’m with a college computer graphics project and I need help. I’m trying to create a rectangle that works like a mirror, for the objects of my scene in Opengl in C.

I found some tutorials that draw the scene twice to fit the reflection. I wonder if there is any simpler way to create a reflection. Could someone help me?

Thanks in advance.

  • 1

    As far as I know this is even the simplest form (for reference, this technique is called "render to Texture"). Unless you are using raytracing - which besides being much more complex is impracticable for use in real time - I don’t know any other way.

  • I understand, :/ it is Porq in my case, the scene is a sphere that Uica on the surface and enters a box, and in case I think it would give me trouble to redesign. In case it is all within a single display() function, do you think q would be better separate into other methods? thanks for responding

  • It is not a problem, because in render to Texture the result of the first rendering (the one that will create the reflection, made from the point of view of the mirrored object and that includes everything in the scene but it) stays in the GPU itself, as texture, so it is much faster than the final rendering (which goes back to the CPU and ends up being drawn on the screen). You can do several additional renderings per frame, and this is used not only for reflection but also for shadows and some other interesting effects. Then I will try to give an answer explaining better.

  • Thank you Victor, I’ve been searching for the tutorial I mentioned and almost worked, but the figure was upside down in the reflection and the disproportionate size tbm, the best would be I have posted the code, but as it is in opengl the code itself is immense and I think I would confuse equal :/, thanks for answering, I’ll keep trying.

  • Yuri, I think you were wanting to address @mgibsonbr to say thank you. However, if you have already managed to put a reflex, only upside down and disproportionate, it may be the case to only do a scale operation on the image of the reflex, where the scale factor X is different from Y and at least one of them has the negative signal to straighten the image.

  • @Victorstafusa It is not really a matter of scale, but of adjusting the texture coordinates (commonly called u and v). This reversing is a fairly common problem...

Show 1 more comment

1 answer

5

The way you described it is in fact the simplest. When rendering a scene, you can use two lighting models: the local and global.

inserir a descrição da imagem aqui

Local model

In the local model each object is rendered independently of the others. This allows each of them - as well as each of their faces, vertices, etc - to be rendered in parallel, which in modern Gpus is done massively (thousands of floating point operations are done simultaneously). This allows quite complex scenes to be painted in real time (approximately), which is necessary when interactivity is required (games, for example).

The price paid is that each object is "ignorant" of others. There is no interaction between them, be it reflection (light hits the first object, then the second, and ends in the camera), refraction (light hits the first, is deflected by the second and ends in the camera), shadows (the light was going to hit the object, but is blocked by the second, and does not reach the camera), etc. This creates unrealistic scenes, and it is necessary to use "tricks" to improve the appearance of them.

One of these tricks is to remove the reflective object from the scene, render it from several angles (forming a square known as "Environment map" or sometimes "skybox") and use the rendering result as texture for the reflective object. Then, when this object is processed in the final rendering (which the user will actually see) it will be a reflection of the rest of the scene, although at that moment it is being rendered without any "consciousness" of the other objects of the scene.

This technique is known as "render to Texture". If you only have one reflective object in the scene, the thing is quite simple, and these additional renderings do not consume so much tamp (largely because the result of this rendering is a texture in the GPU itself, without the overhead to transmit it to and from the main memory). So it is often used in racing games for example, where the player’s car is the one that is more in focus and deserves a more elaborate rendering than the rest of the scene (including the other cars). But if you have more than one reflective object, the thing complicates, and you end up having to use an approximate model that looks like ok at first glance, but if you look carefully you can see the inaccuracies (example; requires modern GPU; does not work on my).

And if there are many objects, there is no way out but to ignore each other and just reflect the skybox generic. In this example you can see that the balls only reflect the scenery around, and not each other, which is far from being a faithful representation of reality...

Global model

In the global model each object takes into account all others when determining its appearance, resulting in a much more realistic scene. The main technique used is the raytracing, that roughly picks up each ray of light that came out of each light source, observes which object(s) hit, whether it was reflected or not, refracted or not (and in more advanced applications whether it is dispersed or not), etc., until finally it reaches the camera, contributing to the color of that pixel on the screen.

(This is a simplification; in practice the raytracing reverse, where one part of the camera and one tries to reach some light source, but this is a little more difficult to explain)

The price you pay is time greater good rendering (example), in addition to the difficulty of making good use of the GPU resources (which is more optimized for real-time rendering). That’s why this technique is more used when time is not important, for example in static images (very used in architecture, for example) or in movies (it can take hours to render a single frame, but then just put it all together and display).

Note: I’m sorry I don’t present examples in Opengl and C, but as you yourself commented they would be quite extensive, and you say in the question that you already found the relevant code in tutorials, so I’m only giving an overview here.

  • Thanks, it really gets difficult without showing an example, but I can show the code I used as a tutorial at least. It is very simple, but when increasing in my project or several problems, I used this example link&#What would make me easier would be to leave the 'mirror', vertically with the object, but I’m having work even to manipulate the tutorial.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.