• Advertisement

#### Archived

This topic is now archived and is closed to further replies.

# Lighting troubles...

This topic is 5321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

hello, i am having lighting problems in the rendering of my 3D model. The lighting just doesn''t give a good 3D look to it. I''d highly appreciate if anyone would be willing to run my program and see the results to give me feedback on what''s missing or what''s wrong in the lighting. So if anyone is helpful enough then please give me some way - like ur email - to send you my program (its a small 82K compressed file that i''ll have to send). [my email is rizvi1981@hotmail.com if u would like to tell me on email] thanks in advance.

#### Share this post

##### Share on other sites
Advertisement
We could help you more if you

Posted your code (in [ source] [ /source] tags}

or

Uploaded the exe (Angelfire.com host for free)

#### Share this post

##### Share on other sites
ok i''ve uploaded the compressed file here:
http://www.angelfire.com/un/shangskykes/

please have a look at it and tell me whats wrong.
thanks.

#### Share this post

##### Share on other sites
ok since no one seems to be interested in helping me by downloading my project file and having a look at the results, i''ll now try getting help by posting my code here for the lighting... please have a look at it and tell me why isn''t my lighting good enough for a good 3D effect.

Here is how I setup my light for the scene:
	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);	GLfloat lmodel_ambient[] = { 0.0, 1.0, 0.0, 1.0 };	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);	    GLfloat light_position[] = { cameraPosX, cameraPosY, 0.0, 1.0 };	GLfloat light_ambient[] = { 0.0, 0.0, 1.0, 1.0};	GLfloat light_diffuse[] = { 0.0, 0.0, 1.0, 1.0};	GLfloat light_specular[] = { 1.0, 1.0, 0.0, 1.0};	    glLightfv(GL_LIGHT0, GL_POSITION, light_position);	glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);	glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);	glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);    glEnable(GL_LIGHTING);    glEnable(GL_LIGHT0);

[cameraPosX and cameraPosY are the coordinates used for the position and view vectors of the camera such that the 3D object always stays in central view of the camera. So I''ve kept the light positioned at whatever place the camera is positioned]

Here''s how i set the material properties and don''t change them anywhere else.
void setDefaultMaterial(){	GLfloat defmat_ambient[] = { 0.2, 0.2, 0.2, 1.0 };	GLfloat defmat_diffuse[] = { 0.8, 0.8, 0.8, 1.0 };	GLfloat defmat_specular[] = { 0.0, 0.0, 0.0, 1.0 };	GLfloat defmat_shininess[] = { 0.0 };	GLfloat defmat_emission[] = {0.0, 0.0, 0.0, 1.0};	glMaterialfv(GL_FRONT, GL_AMBIENT, defmat_ambient);	glMaterialfv(GL_FRONT, GL_DIFFUSE, defmat_diffuse);	glMaterialfv(GL_FRONT, GL_SPECULAR, defmat_specular);	glMaterialfv(GL_FRONT, GL_SHININESS, defmat_shininess);	glMaterialfv(GL_FRONT, GL_EMISSION, defmat_emission);}

My 3D model is made up of lots of triangles and i build a display list of them in the beginning after computing all the triangle vertices and normals:
triangleCount = 0;		linklist = glGenLists(1);		glNewList(linklist, GL_COMPILE);			glColor3ub(255, 255, 255);			while(head->next!=NULL)			{	glBegin(GL_TRIANGLES); // Drawing Using Triangles				glNormal3f( dummy.getNorm00(), dummy.getNorm01(), dummy.getNorm02());				glVertex3f( dummy.getVal00(), dummy.getVal01(), dummy.getVal02());			//	glNormal3f(dummy.getNorm10(),dummy.getNorm11(), dummy.getNorm12());				glVertex3f(dummy.getVal10(),dummy.getVal11(), dummy.getVal12());			//	glNormal3f( dummy.getNorm20(),dummy.getNorm21(), dummy.getNorm22());				glVertex3f( dummy.getVal20(),dummy.getVal21(), dummy.getVal22());				glEnd(); // Finished Drawing The Triangle				triangleCount++;				dummy.eraseOne();			}		glEndList();

And here''s how i compute a normal vector for each triangle:
[the input is the x,y,z coordinates for the 3 vertices of the triangle and the output is the normal for that triangle]
XYZ generateNormal (double x1, double y1, double z1,					double x2, double y2, double z2,					double x3, double y3, double z3){	XYZ n, v1, v2;	// compute two vectors	v1.x = x1 - x2;	v1.y = y1 - y2;	v1.z = z1 - z2;	v2.x = x2 - x3;	v2.y = y2 - y3;	v2.z = z2 - z3;	// take their cross product to get the normal	n.x = ( (v1.y * v2.z) - (v1.z * v2.y) );	n.y = ( (v1.z * v2.x) - (v1.x * v2.z) );	n.z = ( (v1.x * v2.y) - (v1.y * v2.x) );	double l = sqrt( (n.x * n.x) + (n.y * n.y) + (n.z * n.z) );	// normalize	n.x /= l;	n.y /= l;	n.z /= l;	return n;}

#### Share this post

##### Share on other sites
What look are you trying to achieve? As far as I can tell you''ve set up a bright blue ambient (and diffuse) light and that''s what your scene looks like, so there''s nothing wrong with your code. You say you want a good 3D look...when your model is rotating it looks 3D to me!

Maybe you could consider having no (or low) ambient light and instead use a diffuse light with some sort of attenuation(e.g. linear attenuation), so one area of your model is lit more brightly. It''s also more usual to colour your model and shine a white light on it if you want your model to be blue, rather than have a white model with a blue light, but maybe you meant to do it this way round...

Alternatively you could move the light around to give a more 3D feel, if this is acceptable in your project...Maybe multiple light sources? Ages ago I wrote a quick prog to display some text using an outline font and 2 diffuse light sources (with linear attenuation to make them more spotlight-like), 1 red 1 green, flying around the text. I was just learning how to use lighting so this was my first practice. The result looked very "3D" so maybe something like that is what you are looking for?

These are just a few thoughts, but maybe they will inspire someone else to help...I''ll check back tomorrow if I can. I might even be able to look out the program I described above.

#### Share this post

##### Share on other sites
Oh...just noticed one more thing (not really about lighting, but might help u anyway!)...

Don''t use:

for loop
glBegin(GL_LINES)
draw lines
glEnd(GL_LINES)
end loop

Many GLBegin and Ends are slow...hits frame rate hard. Instead use:

glBegin(GL_LINES)
for loop
draw lines
end loop
glEnd(GL_LINES)

I noticed that in your draw3DSgrid procedure, but u may have done it elsewhere as well! Just a tip for the future! ;-)

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### Popular Tags

• Advertisement
• ### Popular Now

• 9
• 11
• 21
• 9
• 14
• Advertisement