1

I'm having a light problem in OpenGL.

I load in my C++ an eyeglasses OBJ containing the faces, normals e uvmapping exported with blender and I want to visualize the object in the user's face. The obj is this: https:https://drive.google.com/file/d/0B8ba1lz8BBHAUEVQX1hkYmtqYlE/view?usp=sharing

So, I solve a linear system that finds a transformation matrix to transform all the points of object from 3D to 2D. I transpose this matrix and load as my modelview matrix in opengl. This part is ok.

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho (0, 512, 512, 0, 100,-100); //Define a ortho
glMatrixMode (GL_MODELVIEW);
glLoadMatrixd(&mViewMatrix(0,0));

Now I want to add light effects in my glasses, so, I configure the light like this in opengl:

GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { camXV2, camYV2, camZV2, 0.0 };
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);

And I render the object and set its respective normals, but the results is something like this:

Wrong light effect

Blue is the real texture color, but as we can see the result is very far from the expected. Anyone help

[Edit by Spektre] obj preview with normals

Normals from OBJ: preview Computed normals from faces (ignoring vn entries) preview

To test your engine try this Wavefront OBJ:

v -1.0 -1.0 -1.0 
v +1.0 -1.0 -1.0 
v +1.0 +1.0 -1.0 
v -1.0 +1.0 -1.0 
v -1.0 -1.0 +1.0 
v +1.0 -1.0 +1.0 
v +1.0 +1.0 +1.0 
v -1.0 +1.0 +1.0 

vn -0.333 -0.333 -0.333 
vn +0.333 -0.333 -0.333 
vn +0.333 +0.333 -0.333 
vn -0.333 +0.333 -0.333 
vn -0.333 -0.333 +0.333 
vn +0.333 -0.333 +0.333 
vn +0.333 +0.333 +0.333 
vn -0.333 +0.333 +0.333 

f 1 2 3 4 
f 5 6 7 8 
f 1 2 6 5 
f 2 3 7 6 
f 3 4 8 7 
f 4 1 5 8 

It is simple cube with normals set to shade it as a sphere.

Spektre
  • 49,595
  • 11
  • 110
  • 380
  • 1
    where is the obj, where is the code ? What kind of GL (oldstyle,VBO/VAO, GLSL ?) what kind of lighting (fixed function, your own ...)? have you setted the lights properly ? Are the normals OK for your material front face settings and CULL_FACE mode? etc ... we are not oracles from vague description without any actual usable info no one can help you – Spektre Jul 23 '16 at 09:19
  • see [Understanding lighting in OpenGL](http://stackoverflow.com/a/29004546/2521214) , [Normal mapping gone horribly wrong](http://stackoverflow.com/a/28541305/2521214) and [OpenGL - vertex normals in OBJ](http://stackoverflow.com/a/31913542/2521214) – Spektre Jul 23 '16 at 09:34
  • Spektre, I added some details. If you need something more please tell me. – Pedro Azevedo Jul 23 '16 at 19:10
  • @PedroAzevedo to notify user `nick` (present in current thread) you need to add `@nick` to your comment and SO will notify him. I look at your OBJ and it looks like the normals nor faces winding rule are not consistent. Some parts are outwards and some are inwards (`CW/CCW` winding). There are also few singularities. See the images I add to your Question. You can try to disable `CULL_FACE` and use double sided materials to avoid problems but you would need to recompute the normals as It looks like the normals provided in OBJ file are wrong/buggy. – Spektre Jul 24 '16 at 11:29
  • @Spektre I tried to recalculate the normals in Blender but the results still the same. About the CULL_FACE, i read hear [link](https://www.opengl.org/sdk/docs/man/docbook4/xhtml/glCullFace.xml) that is disabled by default. – Pedro Azevedo Jul 25 '16 at 16:26
  • not sure about what your GL init and or engine used does so just to be sure before obj rendering try to use: `glDisable(GL_CULL_FACE);` and use `GL_FRONT_AND_BACK` instead of `GL_FRONT` – Spektre Jul 26 '16 at 07:01
  • and I forgot to add also `glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1); ` – Spektre Jul 26 '16 at 07:27

0 Answers0