PDA

View Full Version : Strange light behaviour (Fixed pipeline)



jafrbc
03-17-2014, 09:30 AM
Hi everyone,
I'm trying to render an object (say cube) with OpenGL 1.1 (I know that doesn't makes sense nowadays, but I've to use this). Everything works fine until I try some lighting.

First I'll show you the strange behaviour:




The geometry is fine as if I render wireframe or with another meshviewer everything looks fine. The problem is clearly in the light interaction. The normals however are fine (here I've rendered them):




The code related to this is:


// GLOBAL VARIABLES
static GLfloat light_position[] = {1.0, 1.0, 2*cZ.x , 0.0}; //cZ.x is the minimum z of the mesh. I know this is at infinity, but don't work also with w=1.0






// While drawing mesh k
GLfloat light_ambient[] = {COLOUR[k][0], COLOUR[k][1], COLOUR[k][2], 1.0};
GLfloat light_diffuse[] = {COLOUR[k][0], COLOUR[k][1], COLOUR[k][2], 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);

....
//This is a mesh, so will be drawn using triangles
glBegin(GL_TRIANGLES);
//Triangles will be defined by vertex indices in faces
for (unsigned int i = 0; i<mesh->faces.size(); i++){

int index1 = mesh->faces.at(i).x;
int index2 = mesh->faces.at(i).y;
int index3 = mesh->faces.at(i).z;
glNormal3f(mesh->normals.at(i).x,mesh->normals.at(i).y,mesh->normals.at(i).z);
glVertex3f(mesh->vertices.at(index1).x, mesh->vertices.at(index1).y, mesh->vertices.at(index1).z);
glVertex3f(mesh->vertices.at(index2).x, mesh->vertices.at(index2).y, mesh->vertices.at(index2).z);
glVertex3f(mesh->vertices.at(index3).x, mesh->vertices.at(index3).y, mesh->vertices.at(index3).z);
}
glEnd();

....




In the main function


...
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH); // Enable Smooth Shading

....




What may be the problem?
Thank you very much

jafrbc
03-17-2014, 09:34 AM
Sorry for double posting, but it didn't allowed me to post screenshots or URL of any sort.
The behaviour is something like holes in the cube.

How can I post screenshot in this forum?

Dark Photon
03-18-2014, 04:55 PM
After you've made a few posts (to verify you're not a spammer), it'll let you. For now, just post a URL to the picture, possibly putting spaces in the URL or removing the leading http:// to post it, and we'll fix it up.

jafrbc
03-20-2014, 05:30 AM
Oh yeah, thanks!

imgur.com/ autDE0q,FVjRLpd#1

. Sorry about that.

jafrbc
03-22-2014, 11:25 AM
Oh yeah, thanks!

imgur.com/ autDE0q,FVjRLpd#1

. Sorry about that.

Sorry for upping the topic, but I still can't find the reason why and it's really puzzling me

jafrbc
03-24-2014, 03:00 PM
It's the last time I up the topic because I believe is not a desirable behaviour here on the forum, but I still struggle and I really need to understand what's wrong asap.

Thank you again.

Aleksandar
03-25-2014, 09:55 AM
Your problem is in the for-loop, and the way you are displaying triangles. Most of the normals are not correct, too.
I really have no time to analyze what you are actually doing, but try to set

glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
and/or constant normals, like (0,0,1).

The first will eliminate the influence of wrong winding order, and the second wrong normals for a front side (I hope).
What I suggested is not a solution, but a way to find whats wrong.

jafrbc
03-25-2014, 01:17 PM
Your problem is in the for-loop, and the way you are displaying triangles. Most of the normals are not correct, too.
I really have no time to analyze what you are actually doing, but try to set

glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
and/or constant normals, like (0,0,1).

The first will eliminate the influence of wrong winding order, and the second wrong normals for a front side (I hope).
What I suggested is not a solution, but a way to find whats wrong.

First of all thank you for the reply.
I calculate the normals as follow:



glm::vec3 currFace = m->faces.at(faceIndex);
glm::vec3 vert1 = m->vertices.at(currFace.x);
glm::vec3 vert2 = m->vertices.at(currFace.y);
glm::vec3 vert3 = m->vertices.at(currFace.z);

glm::vec3 side1 = (vert2 - vert1);
glm::vec3 side2 = (vert3 - vert1);

glm::vec3 normal = glm::cross(side1, side2);

normal = glm::normalize(normal);

return normal;


How can they be wrong? (is the transparency and orientation of the cube that make them look skewed, but printing them out looks ok (0,0,1), (0,0,-1), (1,0,0) and so on.)

Enabling what you said doesn't change nothing either :(