Some help for you-find face normal function

BlitzMax Forums/OpenGL Module/Some help for you-find face normal function

Ryan Burnside(Posted 2007) [#1]
This little function allows the user to find normals for faces of 3 points. It has a last parameter that may be 1 or 0 depending on the way the verticies are wound.

If you are using your own primatives this should be somewhat usefull. Please note that this function is for face normals not vertex normals.

A pic from the demo:


the demo
GLGraphics 640,480
glEnable GL_DEPTH_TEST								'Enables Depth Testing
glEnable GL_LIGHTING
glEnable GL_LIGHT0
glMatrixMode GL_PROJECTION							'Select The Projection Matrix
glLoadIdentity										'Reset The Projection Matrix

glFrustum -0.1, 0.1,-0.1, 0.1, 0.1, 100.0				'Setup The Projection Matrix Frustum

glMatrixMode GL_MODELVIEW							'Select The ModelView Matrix
glLoadIdentity										'Reset The ModelView Matrix
Local rquad:Float

Type surface_normal
Field x:Float, y:Float, z:Float
 
EndType ' something to hold data for surface normal
Function create_surface_normal:surface_normal(p1x:Float, p1y:Float, p1z:Float, p2x:Float, p2y:Float, p2z:Float, p3x:Float, p3y:Float, p3z:Float, bool_side = 0) 
		Local nx:Float = ((p3y - p2y) * (p2z - p1z)) - ((p3z - p2z) * (p2y - p1y)) 
		Local ny:Float = ((p3z - p2z) * (p2x - p1x)) - ((p3x - p2x) * (p2z - p1z)) 
		Local nz:Float = ((p3x - p2x) * (p2y - p1y)) - ((p3y - p2y) * (p2x - p1x)) 
		Local N:Float = Sqr((nx * nx) + (ny * ny) + (nz * nz)) 
			
			If bool_side = 1
				N = n * -1.0
			EndIf
			
		Local a:Float = (1.0 / N) * nx
		Local b:Float = (1.0 / N) * ny
		Local c:Float = (1.0 / N) * nz
		Local v:surface_normal = New surface_normal
		
		v.x = a
		v.y = b
		v.z = c
		Return v
endfunction 
									

While Not KeyHit( KEY_ESCAPE )

	glClear GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT		'Clear The Screen And The Depth Buffer
					'Rotate The Triangle On The Y axis ( New )

	
	glLoadIdentity()								'Reset The Current Modelview Matrix
	glTranslatef 0.0, 0.0, - 1.0						'Move Right 1.5 Units and Into The Screen 7.0
	glRotatef rquad, 1.0, 1.0, 0.0						'Rotate The Quad On The X, Y and Z axis ( New )
	
	
	' note: lights disable glcolor3f() so we must use glmaterial instead
	Local mcolor:Float[]=[0.0, 0.3, 1.0, 1.0] ' set an array for a nice blue color
	glMaterialfv(GL_FRONT, GL_AMBIENT, mcolor)     ' Set the material from the array as the Current material
	Local diffuse:Float[]=[1.0, 0.0, 1.0, 1.0]
	glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse) 
	Local specular:Float[]=[0.0, 0.3, 1.0, 1.0]
	glMaterialfv(GL_FRONT, GL_SPECULAR, specular) 
	glMateriali(GL_FRONT, GL_SHININESS, 128) 
	
	

	glBegin(GL_TRIANGLES) 
	
		Local v:surface_normal = New surface_normal ' just Save a vertex To compute Values
		Local topy:Float = -.5 ' the y coordinate of the top point
		Local bottomy:Float =.5 ' the y coordinate of the bottom point
		Local centery:Float = 0.0 ' this is the coordinate For the y of the origen
		Local centerx:Float = 0.0 'this is the coordinate For x at the origen or 0
		Local centerz:Float = 0.0 ' this is the coordinate For z at the origen
		
		For Local i = 0 To 35
			Local currentx:Float = Cos(i * 10.0) *.5 ' current value of x at i
			Local currentz:Float = Sin(i * 10.0) *.5 ' current value of z at i
			Local oldx:Float = Cos((i - 1) * 10.0) *.5 ' previous value of x at i
			Local oldz:Float = Sin((i - 1) * 10.0) *.5 ' previous value of z at i
			' this is the top trianle belt "winding" the vertices in this direction
			' means that we must set the last parameter of "create_surface_normal" to 0
			v = create_surface_normal(centerx, topy, centerz, currentx, centery, currentz, oldx, centery, oldz, 0) 
			glNormal3f(v.x, v.y, v.z) 
			glColor3f(1.0, 0.0, 0.0) 
			glVertex3f(centerx, topy, centerz) 
			glVertex3f(currentx, centery, currentz) 
			glVertex3f(oldx, centery, oldz) 
			
			' this is the bottom belt this time we wind the triangle verticies in the opposite direction
			' so "bool_side" must be 1 instead of 0
			v = create_surface_normal(currentx, centery, currentz, oldx, centery, oldz, centerx, bottomy, centerz, 1) 
			glNormal3f(v.x, v.y, v.z) 
			glColor3f(0.0, 0.0, 1.0) 
			glVertex3f(currentx, centery, currentz) 
			glVertex3f(oldx, centery, oldz) 
			glVertex3f(centerx, bottomy, centerz) 
		Next
	glEnd										'Finished Drawing The Box										'Increase The Rotation Variable For The Triangle ( New )
	rquad:-0.85									'Decrease The Rotation Variable For The Quad     ( New )

	Flip

Wend