Lighting problem.

Started by
5 comments, last by Ahl 16 years, 2 months ago
I'm having trouble implementing lighting. It works in that it illuminates the objects I have on the screen. My problem is that it doesn't help apply the "3D" look of my 3D objects. I have a couple 4 subdivision icospheres floating around the screen, no texture maps just solid colors, and they stay that solid uniform color. No light variation from light intensity, position or distance and I can't figure out what I'm doing wrong. I'm using the following code: LightAmbient = (GLfloat*4)(0.5, 0.5, 0.5, 1.0) LightDiffuse = (GLfloat*4)(0.2, 0.2, 0.2, 1.0) LightPosition = (GLfloat*4)(0.0, 0.0, 20.0, 1.0) diffuseMaterial = (GLfloat*4)(0.1, 0.1, 0.1, 1.0) mat_specular = (GLfloat*4)(0.0, 0.0, 0.0, 0.0) light_position = (GLfloat*4)(0.0, 0.0, 2.0, 0.0) glLightfv( GL_LIGHT1, GL_AMBIENT, LightAmbient ) glLightfv( GL_LIGHT1, GL_DIFFUSE, LightDiffuse ) glLightfv( GL_LIGHT1, GL_POSITION, LightPosition ) glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial) glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular) glMaterialfv(GL_FRONT, GL_SHININESS, GLfloat(25.0)) glEnable(GL_COLOR_MATERIAL) glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) glShadeModel(GL_SMOOTH) glEnable( GL_LIGHTING ) glEnable( GL_LIGHT1 ) Any help would be appreciated.
Advertisement
It sounds like you're not correctly calculating or defining the objects' vertex normals. If the whole sphere has the same normal then it will all be lit the same. You need to specify each vertex's normal with glnormal3f() when you define the geometry. Are you making the geometry in code or loading it from a file? If you're loading it then the normals may not be loading properly or you may not be using a format which stores normals.
Hey Perth...I've been there.

Anyways: Normals you say? I'm importing my spheres from a .raw file generated using Blender. All the file includes are the point coords for the triangles making up the sphere.

Should I be using a different format? Or is there a way to determine the normals after the importing?

Oooooooh I see I see. Looking at Nehe lesson 7 I don't know how I missed that the first time. Alright. So the question remains though: Is there a way to calculate normals once the sphere is generated or do I need to find a new format to export my Blender stuff into?
It appears that the .map format has precisely what im looking for. At least I guess it does. It's giving me 5 lines per triangle. I'm guessing the first is the normal as it's not labled. What the other 4 are I don't know.

Thoughts?
The normals, at least for spheres, are the *easiest* thing to generate yourself, just normalize the direction from the sphere center to each vertex. As for a file format, wavefront obj is extremely simple for basic uses, and you can find lots of stuff about it.
I don't know if im just doing it wrong or what but going with the Wavefront obj gives me a butt ton of .obj and .mtl files. I dont think thats what im looking for.
I figured it out.

struct vector a, b, n;
double l;
a.x = p2.x - p1.x; a.y = p2.y - p1.y; a.z = p2.z - p1.z;
b.x = p3.x - p1.x; b.y = p3.y - p1.y; b.z = p3.z - p1.z;

n.x = (a.y * b.z) - (a.z * b.y);
n.y = (a.z * b.x) - (a.x * b.z);
n.z = (a.x * b.y) - (a.y * b.x);

// Normalize (divide by root of dot product)
l = sqrt(n.x * n.x + n.y * n.y + n.z * n.z);
n.x /= l; n.y /= l; n.z /= l;

// Now pass n.x, n.y, n.z to glNormal3f

Was what I was looking for. For beginners anyways. My spheres take on a some-what golf ball look but thats cool. At least you can tell they're 3D now. LOL.

This topic is closed to new replies.

Advertisement