• Advertisement
Sign in to follow this  

RESOLVED Can anyone tell me what is wrong here? (lighting + vertex normals)

This topic is 3900 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

Here's a screenshot: Image Hosted by ImageShack.us
(This is supposed to be a simulation of water waves) I'm trying to do lighting with vertex normals calculated by averaging the face normals of all adjacent triangles and this is what I'm getting. I swear I've been over every permutation of different winding directions and points used to calculate the cross-products, but I can't get anything but a mess like this. Can anybody think of a common mistake I might be making here or something, or should I just tear it down and start over? SOLUTION: Okay well I'm an idiot. The vertex.Normalize() function wasn't taking the square root of the length of the vector, so it was dividing by r^2 not r. Everything is fine now. [Edited by - My_Mind_Is_Going on June 23, 2007 10:25:42 PM]

Share this post


Link to post
Share on other sites
Advertisement
Okay, here it is. As a disclaimer, I should say I haven't attempted to make this work efficiently I just want to see it work period. Currently I'm making three passes through all the points to do this which probably can be improved upon, but here's the current sequence of events:

There's a 2D array of lattice points called Lattice that is handling all the physics and then there's a 2D array of vertices, that are used to actually draw the triangles.

1. Loop through all lattice points and set the position of the corresponding vertex to the position of the lattice point.

2. Loop through the vertices and calculate the normal vector for each triangle and add this normal vector to the normal vector each vertex is carrying around. You can see that the loop runs from like 0 to SIZE-1 in both directions, but the triangles are calculated by accessing elements [j], [i+1][j], [j+1], etc so vertices that are involved in more than one triangle should be getting the contribution from the normal of each triangle they are a part of.

3. Loop through vertices again now that normals are all calculated and draw triangles.

Here's the main DrawLattice() function:

void DrawLattice (void) 
{

for( int i = 0 ; i < LENGTH ; i++ )
{
for( int j = 0 ; j < LENGTH ; j++ )
{
vertices[j].ClearNormal() ;
vertices[j].SetPosition( Lattice[j].position ) ;

}

}

for( i = 0 ; i < LENGTH-1 ; i++ )
{
for( int j = 0 ; j < LENGTH-1 ; j++ )
{
float face_normal[3] ;

// Upper triangle

CalculateNormal( vertices[i+1][j].position,
vertices[j].position,
vertices[i+1][j+1].position,
face_normal) ;

vertices[i+1][j].normal[0] += face_normal[0] ;
vertices[i+1][j].normal[1] += face_normal[1] ;
vertices[i+1][j].normal[2] += face_normal[2] ;

vertices[j].normal[0] += face_normal[0] ;
vertices[j].normal[1] += face_normal[1] ;
vertices[j].normal[2] += face_normal[2] ;

vertices[i+1][j+1].normal[0] += face_normal[0] ;
vertices[i+1][j+1].normal[1] += face_normal[1] ;
vertices[i+1][j+1].normal[2] += face_normal[2] ;


// Lower triangle

CalculateNormal( vertices[j].position, vertices[j+1].position, vertices[i+1][j+1].position,
face_normal ) ;

vertices[j].normal[0] += face_normal[0] ;
vertices[j].normal[1] += face_normal[1] ;
vertices[j].normal[2] += face_normal[2] ;

vertices[j+1].normal[0] += face_normal[0] ;
vertices[j+1].normal[1] += face_normal[1] ;
vertices[j+1].normal[2] += face_normal[2] ;

vertices[i+1][j+1].normal[0] += face_normal[0] ;
vertices[i+1][j+1].normal[1] += face_normal[1] ;
vertices[i+1][j+1].normal[2] += face_normal[2] ;

}
}



glBegin(GL_TRIANGLES) ;

for( int j = 0 ; j < LENGTH-1 ; j++ )
{

for( int i = 0 ; i < LENGTH-1 ; i++ )
{

// Upper triangle

vertices[i+1][j].Normalize() ;
glNormal3fv( vertices[i+1][j].normal ) ;
glVertex3fv( vertices[i+1][j].position ) ;

vertices[j].Normalize() ;
glNormal3fv( vertices[j].normal ) ;
glVertex3fv( vertices[j].position ) ;

vertices[i+1][j+1].Normalize() ;
glNormal3fv( vertices[i+1][j+1].normal ) ;
glVertex3fv( vertices[i+1][j+1].position ) ;

// Lower triangle

vertices[j].Normalize() ;
glNormal3fv( vertices[j].normal ) ;
glVertex3fv( vertices[j].position ) ;

vertices[j+1].Normalize() ;
glNormal3fv( vertices[j+1].normal ) ;
glVertex3fv( vertices[j+1].position ) ;

vertices[i+1][j+1].Normalize() ;
glNormal3fv( vertices[i+1][j+1].normal ) ;
glVertex3fv( vertices[i+1][j+1].position ) ;

}

}

glEnd() ;

}





and the CalculateNormal() function as well:


void CalculateNormal( float *p1, float *p2, float *p3, float *normal )
{

float a[3] ;
float b[3] ;

a[0] = p2[0] - p1[0] ;
a[1] = p2[1] - p1[1] ;
a[2] = p2[2] - p1[2] ;

b[0] = p3[0] - p1[0] ;
b[1] = p3[1] - p1[1] ;
b[2] = p3[2] - p1[2] ;

normal[0] = a[1]*b[2] - a[2]*b[1] ;
normal[1] = a[2]*b[0] - a[0]*b[2] ;
normal[2] = a[0]*b[1] - a[1]*b[0] ;

float length = sqrt( normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2] ) ;

normal[0] /= length ;
normal[1] /= length ;
normal[2] /= length ;

}




I think that should be all the relevant parts, let me know if anything is not clear and thanks to anyone who takes a stab at this.

[Edited by - My_Mind_Is_Going on July 24, 2007 8:09:16 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement