Hello,
I have a 3D object.
I draw it by drawing all its surfaces.The camera location
can be choosen randomly.(And the light sourse is place in rthe camera place)
How do I draw the surfaces correctly ?
Is the order of surfaces drawing important?
need draw them back to from probably according to the camera position or it is not important…?
The order is really relevant if your object is very complex and you are using fragment shaders that require significant amount of GPU shading unit time. Considering HyperZ or similar technologies, rendering should be done front to back (the opposite way you should expect).
But in your case, I guess that rendering order is totally irrelevant. Just enable depth test and everything will work fine.
In the scene preparation function just add
glEnable(GL_DEPTH_TEST);
and at the beginning of the drawing function clear depth buffer (along with frame buffer) with
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
After that you can freely draw in the order you want.
P.S. Drawing may require polygon sorting if you want to implement some kind of transparency.
The problem can be in light position, but also in normals calculation.
If you want light source to be at the camera position, just set light source position at coordinates (0,0,0) after switching to modelview matrix and before the viewing transformation (for example, before gluLookAt).
OK! We have eliminated the problem with the light position. Now it is at the right position. But there is still the problem with the normals, perhaps.
Take a look at the model. Some faces oriented “up” as well as some faces oriented “down” are equally lighted. Check the functions that calculate normals.
I’ve been fooling around with your code by drawing a teapot instead of your object. I got the same results you did when I tried moving the glLightfv(…, light_position) command around. It didn’t fix the lighting problem. What works for me is to put the light at the same location as the camera (first 3 values of LookAt). This only requires one line of code -
I am afraid that may be the problem is in the
incorrect hidden surfaces removal and I have to use stencil test /or dot product of the vector from camera with the normal of surface plane /or the way I try to examine the depth is incorrect and I have to add some additional code…(whatever )
Any case,thank you very much for the answer,I`ll try as you said!!!
I have found the reason for the error in the rendering image-the normals!!!
The way I was calculating them is by Martin Newell method.
But the problem is in the directions.So I tried to define correct directions by finding center of mass of the 3D object and the center of mass of each surface and findig the dot product between vector leading from center of mass of the surface with the normal to the surface.If it is negative the normal direction is corret else it has to be negated.
But this way is not optimal…Doesnt work good for not convex objects... Will be very thankfull for some advices (I don want to use cross product …)