OpenGL Lighting problems

Started by
13 comments, last by Etwinox 22 years, 5 months ago
quote:Original post by Etwinox
Uhh, hmm.

Ok, you guys are really being nice and putting lots of time into this but, all this info is really confusing...how do I "smooth shade" a cube? I thought you had to give a "normal" for each vertex...



Smooth shading works fine with face normals. With a cube, specify the same normal for each vertex in a face. The ''smoothness'' of smooth shading will come from the changing angle to the light source at the different vertexes (which is computed by openGL for you) rather than from differing normals.
Advertisement
Why would you smooth shade a cube? All of the normals on a particular face share the same normal. Even worse, if you''re only using 8 vertices for the cube, because each vertex is used more than once (4 vertices per face, each vertex shares 3 faces), there is not a particular "vertex normal" or "surface normal" that relates to each vertex in a 1 to 1 manner. Each vertex will use 3 different normals, one for each face it''s rendering. This is because each vertex lies on a sharp edge.

If you were to use a single "surface" normal for each vertex, the cube would not look like a cube, but like a sphere that is very under tesselated, like it doesn''t use enough polygons to approximate the surface of the sphere.

Now, if you were talking about using more than 8 vertices, like having vertices internal to a particular plane (face), then each vertex on a particular face shares the same normal.

If you''re looking to explore smooth shading, try a different primitive that doesn''t have sharp edges, such as a sphere, cone, or cylinder. I learned how to describe these shapes in 11th grade trig class - it''s all sines and cosines.

If you''re interested in some code for them post here about it and I can send you some or post some here. My advice, though, is to "borrow" some of the math books you use in high school trig and calculus, and get your hands on some college level texts, if you can. I still have my high school trig book, two different high school calc books, and all of my college math books. Every once in a while, I still have to go and double check something from that high school trig book!
-- Succinct(Don't listen to me)
Aww...
I didn''t think of it that way. I thought you "had" to smooth shade an object...how do games get a "spotlight" effect then if
the surface consists of only one normal? I didn''t think it would
work considering the lighting is only a brightness level when its one normal (or so it seems).

Course, now I know why my attempts at smooth shading a cube were
not exactly correct looking.


P.S.
Thank god, I thought I would of been stuck on chapter five of that book forever. ^_^

Oh, almost forgot, I would be happy to see that source code you were talking about if you have the free time, but its upto you.
By a ''spotlight'' effect I''m going to assume that you''re talking about a specular reflection. (The white patch you see when looking at an apple or a shiny ball.)

The specular reflection occurs when the vector of light ''mirroring'' off of the object happens to line up well with the vector to the eye. Think of the light bouncing off of the surface at the same angle it hit. If the bounce points right at your eye or camera, you get a specular hightlight. This happens at some points of a flat surface but not others, since the reflected light rays won''t match up when reflected from various points, even if they are reflected at nearly the same angle.

OpenGL''s shading model (gourand shading) won''t do specular highlighting within a face. To get this you need to tesselate your flat surfaces into larger groups of triangles. However, with a flat surface these tesselated faces will still all have the same normal.

If you want this to happen on one big quad, you need phong shading, which isn''t built into OpenGL.

If you want a normal for a triangle, this is the code:

struct GLVECTOR
{
float x,y,z;
}

GLVECTOR triangle[3];
glVECTOR normal;
GLVECTOR temp[2];

//move the triangle temporarily to the origin.
temp[0] = triangle[1] - triangle[0];
temp[1] = triangle[2] - triangle[0];
//No real code, i know, but you know how to do that.

normal.x = triangle[1].y*triangle[2].z - triangle[2].y*triangle[1].z;
normal.y = triangle[1].z*triangle[2].x - triangle[2].z*triangle[1].x;
normal.z = triangle[1].x*triangle[2].y - triangle[2].x*triangle[1].y;


Now you got a good normal. (Needs to be normalized, though, but you can use glEnable(GL_NORMALIZE), i think..)
--Electron"The truth can be changed simply by the way you accept it.""'General failure trying to read from file' - who is General Failure, and why is he reading my file??"

This topic is closed to new replies.

Advertisement