nehe 10 with picking

BlitzMax Forums/BlitzMax OpenGL Programming/nehe 10 with picking

a modified version of the nehe 10 "move around a world"
demo, with polygon picking and mouse movement

You will need a texture (file name specified in last
function) and the world.txt file from nehe 10 tut.

mouse to look lmb forwards

tris in pick list red, nearest green
space bar toggles poly flashing

Enjoy

Strict 
Global ScreenWidth:Float=800
Global ScreenHeight:Float=600
Global ScreenDepth:Int=32

'Global Checkimage:Byte[256,256,3]
Global Texname

Type VERTEX
  ' 3D Coordinates
  Field x:Float, y:Float, z:Float
  ' Texture Coordinates
  Field u:Float, v:Float
End Type


Global numtriangles:Int							' Number Of Triangles In Sector
Global mytriangles:VERTEX[]						' Array Of Triangles
Global hilight:Int[]
Global state:Int[]
Global lastspace

Global dist:Int[]
Global worldfile:String="world.txt"
Global light:Int									' Lighting ON/OFF
Global blend:Int									' Blending ON/OFF
Global xrot:Float									' X Rotation
Global yrot:Float									' Y Rotation
Global walkbias:Float
Global walkbiasangle:Float
Global lookupdown:Float=0.0
Global heading:Float=0.0
Global xpos:Float=0.0
Global zpos:Float=0.0
Global filter:Int=0								' Which Filter To Use

Global LightAmbient:Float[]=[0.5, 0.5, 0.5, 1.0]		' Ambient Light
Global LightDiffuse:Float[]=[1.0, 1.0, 1.0, 1.0]		' Diffuse Light
Global LightPosition:Float[]=[0.0, 0.0, 2.0, 1.0]	' Light Position
Global m:Int,fc
Global s:String=""
HideMouse

bglCreateContext(ScreenWidth,ScreenHeight,ScreenDepth,0,BGL_BACKBUFFER | BGL_DEPTHBUFFER | BGL_FULLSCREEN)
bglSetSwapInterval(1)

InitGl()
SetupWorld()
ShowMouse

Global selectbuffer:Int[512],hits

Local Framecounter_counter:Int,Framecounter_time,Framecounter_framerate
Global viewport:Int[4]
While Not KeyHit( KEY_ESCAPE )
		glenable(GL_TEXTURE_2D)
		nehe10()
		glDisable(GL_TEXTURE_2D)
		glcolor3f 1.0,1.0,1.0
		
		bglDrawText("Nehe lesson 10",10,24)
		bglDrawText("hits "+hits,10,34)
		
		Local y=44

		s:+"#"
		
		Local f=Instr(s,"#",1)
		Repeat 
			Local l:String= Left(s,f-1)
			s= Right(s,Len(s)-f)
			bglDrawText(l,10,y)
			y:+12
			f=Instr(s,"#",1)
		Until f=0
		
		
		FlushMem
		bglSwapBuffers
		fc:+1
Wend
End

Function InitGl()
	LoadGlTextures()
	glEnable GL_TEXTURE_2D
	glBlendFunc GL_SRC_ALPHA, GL_ONE
	glClearColor(0.0, 0.0, 0.0, 0.0)
	glClearDepth 1.0
	glDepthFunc GL_LESS
	glEnable GL_DEPTH_TEST
	glShadeModel(GL_SMOOTH)
	

	
	
	glViewport(0,0,ScreenWidth,ScreenHeight)
	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()
	gluPerspective(45.0,Float(ScreenWidth)/Float(ScreenHeight),0.1,100.0)
	glMatrixMode(GL_MODELVIEW)
	
	glLightfv GL_LIGHT0, GL_AMBIENT, Float Ptr LightAmbient
	glLightfv GL_LIGHT0, GL_DIFFUSE, Float Ptr LightDiffuse
	glLightfv GL_LIGHT0, GL_POSITION, Float Ptr LightPosition
	glEnable GL_LIGHT0
End Function




Function nehe10()
	Local x_m:Float; Local y_m:Float; Local z_m:Float; Local u_m:Float ; Local v_m:Float
	Local xtrans:Float; Local ztrans:Float; Local ytrans:Float
	Local sceneroty:Float
	Local loop_m:Int

	If KeyHit(KEY_B) Then
		blend = Not blend
	EndIf
	If blend = 0 Then
		glDisable GL_BLEND
		glEnable GL_DEPTH_TEST
	Else
		glEnable GL_BLEND
'		glDisable GL_DEPTH_TEST
	End If
	

	
	If KeyHit(KEY_L) Then
		light = Not light
	EndIf
	If light=0 Then
		glDisable(GL_LIGHTING)
	Else
		glEnable(GL_LIGHTING)
	EndIf





 
	If KeyDown(KEY_PAGEUP) Then
		lookupdown = lookupdown - 0.1
	End If

	If KeyDown(KEY_PAGEDOWN) Then
		lookupdown = lookupdown + 0.1
	End If

	If KeyDown(KEY_UP) Or MouseDown(1) Then
		xpos = xpos - Float(Sin(heading)) * 0.1
		zpos = zpos - Float(Cos(heading)) * 0.1

	End If

	If KeyDown(KEY_DOWN) Or MouseDown(2) Then
		xpos = xpos + Float(Sin(heading)) * 0.1
		zpos = zpos + Float(Cos(heading)) * 0.1

	End If


	If KeyDown(KEY_RIGHT) Then
		heading = heading - 1
	End If

	If KeyDown(KEY_LEFT) Then
		heading = heading + 1
	End If
	
	yrot = heading
		
	xtrans=-xpos
	ztrans=-zpos
	ytrans= - 0.5
	sceneroty = 360.0 - yrot
	glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT


	lookupdown=lookupdown+(MouseY()-ScreenHeight/2.0)/8
	heading=heading-(MouseX()-Screenwidth/2.0)/8
	MoveMouse screenwidth/2,screenheight/2
	
'''''''''''''''''''''''''''''''''''''''''''''''picking


	glSelectBuffer(512,Varptr selectBuffer[0]);

	glRenderMode(GL_SELECT)	
	glInitNames();	
	glGetIntegerv(GL_VIEWPORT, viewport);

	glMatrixMode(GL_PROJECTION);					'			// Selects The Projection Matrix
	glPushMatrix();								'				// Push The Projection Matrix
	glLoadIdentity();
	gluPickMatrix(ScreenWidth/2.0, ScreenHeight/2.0, 1, 1, viewport);

	gluPerspective(45.0,Float(ScreenWidth)/Float(ScreenHeight),0.1,100.0)
	glMatrixMode(GL_MODELVIEW);


	glLoadIdentity
	glRotatef lookupdown, 1.0, 0, 0
	glRotatef sceneroty, 0, 1.0, 0
	glTranslatef xtrans, ytrans, ztrans
	glBindTexture GL_TEXTURE_2D, Texname
	'	gldisable GL_DEPTH_TEST
	' Process Each Triangle

	For loop_m = 0 To (numtriangles - 1)*3 Step 3
	glPushName(loop_m/3);
		glBegin GL_TRIANGLES

			glNormal3f 0.0, 0.0, 1.0
			x_m = mytriangles[loop_m+0].x
			y_m = mytriangles[loop_m+0].y
			z_m = mytriangles[loop_m+0].z
			u_m = mytriangles[loop_m+0].u
			v_m = mytriangles[loop_m+0].v
			glTexCoord2f u_m,v_m ; glVertex3f x_m,y_m,z_m

			x_m = mytriangles[loop_m+1].x
			y_m = mytriangles[loop_m+1].y
			z_m = mytriangles[loop_m+1].z
			u_m = mytriangles[loop_m+1].u
			v_m = mytriangles[loop_m+1].v
			glTexCoord2f u_m,v_m ; glVertex3f x_m,y_m,z_m

			x_m = mytriangles[loop_m+2].x
			y_m = mytriangles[loop_m+2].y
			z_m = mytriangles[loop_m+2].z
			u_m = mytriangles[loop_m+2].u
			v_m = mytriangles[loop_m+2].v
			glTexCoord2f u_m,v_m ; glVertex3f x_m,y_m,z_m
		glEnd
		glpopname()
	Next
	
	glFlush();				'				// Select The Modelview Matrix
	hits=glRenderMode(GL_RENDER);

	glMatrixMode(GL_PROJECTION);					'			// Select The Projection Matrix
	glPopMatrix();									'			// Pop The Projection Matrix
	glMatrixMode(GL_MODELVIEW);	
	glcolor4f 1.0,1.0,1.0,1.0

	glEnable GL_DEPTH_TEST



	If hits>0 Then processhits(hits,Varptr selectBuffer[0])



''''''''									'''''''''' end picking


	glLoadIdentity
	glRotatef lookupdown, 1.0, 0, 0
	glRotatef sceneroty, 0, 1.0, 0
	glTranslatef xtrans, ytrans, ztrans
	glBindTexture GL_TEXTURE_2D, Texname
	' Process Each Triangle
	Local n:Int=0
	For loop_m = 0 To (numtriangles - 1)*3 Step 3
		glBegin GL_TRIANGLES

	
			If hilight[n]=	0 Then 	glcolor3f 1.0,1.0,1.0
			If hilight[n]=1 Then
			 	glcolor3f 1.0,0.0,0.0
				hilight[n]=0
			EndIf
			If hilight[n]=	2 Then 
				glcolor3f 0.0,1.0,0.0
				hilight[n]=0
			endif
			
			If state[n] And (fc Mod 60)>30 Then
				glcolor3f 0.5,0.5,1.0
			End If


			n:+1

	

			glNormal3f 0.0, 0.0, 1.0
			x_m = mytriangles[loop_m+0].x
			y_m = mytriangles[loop_m+0].y
			z_m = mytriangles[loop_m+0].z
			u_m = mytriangles[loop_m+0].u
			v_m = mytriangles[loop_m+0].v
			glTexCoord2f u_m,v_m ; glVertex3f x_m,y_m,z_m

			x_m = mytriangles[loop_m+1].x
			y_m = mytriangles[loop_m+1].y
			z_m = mytriangles[loop_m+1].z
			u_m = mytriangles[loop_m+1].u
			v_m = mytriangles[loop_m+1].v
			glTexCoord2f u_m,v_m ; glVertex3f x_m,y_m,z_m

			x_m = mytriangles[loop_m+2].x
			y_m = mytriangles[loop_m+2].y
			z_m = mytriangles[loop_m+2].z
			u_m = mytriangles[loop_m+2].u
			v_m = mytriangles[loop_m+2].v
			glTexCoord2f u_m,v_m ; glVertex3f x_m,y_m,z_m
			glEnd


	Next
	
	
	
	
End Function

Function processhits:string(hits:Int,sb:Int Ptr)

s="hits="+hits
Local low:Float
low=Var (sb+1)
Local ply:Int=Var (sb+3)


For Local x=0 To hits-1
s=s+"#polys "+Var(sb)
Local p=Var sb
sb:+1
 
Local d1:Float=Var sb
s:+"  d1="+d1
sb:+1

Local d2:Float=Var sb
s:+"  d2="+d2
sb:+1

If ((d1+d2)/2) < low Then 
	low=((d1+d2)/2)
	ply=Var sb
End if
s:+"#"
For Local n=1 To p
s:+"   name=" + Var sb
hilight[Var sb]=1
sb:+1
Next
Next

hilight[ply]=2

If KeyDown(key_space) And lastspace Then 

	state[ply]=Not state[ply]
	lastspace=false
endif
If Not KeyDown(key_space) Then lastspace=true

 

s:+"#low ply="+ply

End Function


Function SetupWorld()
	Local i:Int
	Local c:Int
	Local boom:Int
	Local vert:Int
	Local oneline:String
	Local sstring:String

	Local file=OpenFile(worldfile)
	Repeat  
		oneline=ReadLine(file)
		If Left(oneline, 11) = "NUMPOLLIES " Then
			sstring=Mid(oneline, 12)
			numtriangles=sstring.ToInt()
			Exit
		End If
	Until Eof(file)

	mytriangles=mytriangles:VERTEX[..numtriangles*3]
	
	
	hilight=hilight:Int[..numtriangles]
	state=state:Int[..numtriangles]
	
	
	For c = 0 To numtriangles - 1
		For vert = 0 To 2
			boom = 0
			Repeat
				oneline=ReadLine(file)
				oneline = Trim(oneline)
				If oneline <> "" Then
					If Left(oneline, 2) <> "//" Then
						mytriangles[c*3+vert]=New VERTEX
						mytriangles[c*3+vert].x = oneline.ToFloat()
						i = Instr(oneline, " ")
						oneline = Trim(Mid(oneline, i))
						mytriangles[c*3+vert].y = oneline.ToFloat()
						i = Instr(oneline, " ")
						oneline = Trim(Mid(oneline, i))
						mytriangles[c*3+vert].z = oneline.ToFloat()
						i = Instr(oneline, " ")
						oneline = Trim(Mid(oneline, i))
						mytriangles[c*3+vert].u = oneline.ToFloat()
						i = Instr(oneline, " ")
						oneline = Trim(Mid(oneline, i))
						mytriangles[c*3+vert].v = oneline.ToFloat()
						boom = 1
					End If
				End If
			Until boom = 1
		Next
	Next
	CloseStream file
End Function


Function LoadGlTextures()
	Local PointeurImg:Byte Ptr
	Local TexWidth
	Local TexHeight
	Local tex:TPixmap=LoadPixmap("metal.bmp")
	texname=bglTexFromPixmap(tex)

	' Create MipMapped Texture
	glBindTexture GL_TEXTURE_2D, Texname
	glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR
	glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST

End Function
 


Greatest, im now onto Lesson 10. However, I cant for the life of me find the media and text documents. anywhere. Where are they please?