First creation + question vertex-list

BlitzMax Forums/BlitzMax OpenGL Programming/First creation + question vertex-list

ok, so i finally made something that actually does what i want it to do. arrows move, a + q go op and down, r resets everything (i think it crashes if you leave it growing too long)



i use a TList to keep track of all passed positions in space, and every frame i eachin-through them all and redraw the whole scene. my question: is this the right way to do this? i wanted to use vertexarrays but couldn't find how to use TLists with them.

(edit - you have to move back a little at the beginning to see something)

Global ScreenWidth:Int=1024
Global ScreenHeight:Int=768
Global ScreenDepth:Int=32



GLGraphics ScreenWidth,ScreenHeight,ScreenDepth,120
'GLGraphics ScreenWidth,ScreenHeight

HideMouse()
init()

Global pos_x# = 0.5
Global pos_y# = 1
Global pos_z# = 2



Global mvr:Mover = New Mover
mvr.init()


Global tel = 0


Global vertexList:TList=CreateList()

Global timer# = 0

Global vertices:Float[] =  [-1.0,0.0, 0.0, 1.0,0.0, 0.0,..
							-1.0,0.0,-1.0, 1.0,0.10,-1.0,..
							-0.5,0.0,-2.0, 1.5,0.20,-2.0,..
							-0.5,0.0,-3.0, 1.5,0.30,-3.0,..
							 0.0,0.0,-4.0, 2.0,0.40,-4.0,..
						 	 0.0,0.0,-5.0, 2.0,0.50,-5.0]


While Not KeyHit( KEY_ESCAPE )

	mouse_x# = MouseX() / Float(ScreenWidth)
	mouse_y# = MouseY() / Float(ScreenHeight)

	

	mvr.move()
	
	' vastleggen vertex-posities
	'	ListAddLast vertexList, createVector(mvr.pos.x#,mvr.pos.y#,mvr.pos.z#)
		
	
	glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT 
'	glClear GL_DEPTH_BUFFER_BIT 


	glLoadIdentity
	
	checkKeys()


	' eigen positie 
	xtrans# = -pos_x#
	ytrans# = -pos_y#
	ztrans# = -pos_z#		
	
	glTranslatef xtrans#, ytrans#, ztrans#

'	gluLookAt -2,0.5,2, mvr.pos.x#,mvr.pos.y#,mvr.pos.z#, 0,1,0
	gluLookAt 2,0.5,3, 10,0,0, 0,1,0

	
	' teken hulplijnen
		' axis
'		drawAxis()		
		' snijpunt met vlak y=0
		glBegin GL_POINTS
'			glColor4f 0,0,0,0.1
'			glVertex3f mvr.pos.x#, 0, mvr.pos.z#
		glEnd
		' verticale lijn
		glBegin GL_LINES
'			glColor4f 0,0,0,0.1
'			glVertex3f mvr.pos.x#, 0, mvr.pos.z#
'			glVertex3f mvr.pos.x#, mvr.pos.y#, mvr.pos.z#
		glEnd
	
		
	

'		glDisable(GL_LINE_STIPPLE)
'		' tekenen richtingsvectoren
'		glBegin GL_LINES
'			mp# = 10.0
'			glColor3f 1,0,0
'	
			' direction
	'			glVertex3f mvr.pos.x#, mvr.pos.y#, mvr.pos.z#
	'			glVertex3f mvr.pos.x# + mp# * (mvr.direction.x#), mvr.pos.y# + mp# * (mvr.direction.y#), mvr.pos.z# + mp# * (mvr.direction.z#)
	'			
	'			mp# = 1.0
	'			glColor3f 0,0,0
	
			' crossprod
'	
'			glVertex3f mvr.pos.x#, mvr.pos.y#, mvr.pos.z#
'			glVertex3f mvr.pos.x# + mp# * (mvr.crossprod.x#), mvr.pos.y# + mp# * (mvr.crossprod.y#), mvr.pos.z# + mp# * (mvr.crossprod.z#)
'	
'			mp# = -1.0
'			glVertex3f mvr.pos.x#, mvr.pos.y#, mvr.pos.z#
'			glVertex3f mvr.pos.x# + mp# * (mvr.crossprod.x#), mvr.pos.y# + mp# * (mvr.crossprod.y#), mvr.pos.z# + mp# * (mvr.crossprod.z#)
		
	mp# = 0.5*Sin(tel*2)
	ListAddLast vertexList, createVector(mvr.pos.x# + mp# * (mvr.crossprod.x#), mvr.pos.y# + mp# * (mvr.crossprod.y#), mvr.pos.z# + mp# * (mvr.crossprod.z#) )
	mp# = -mp#		
	ListAddLast vertexList, createVector(mvr.pos.x# + mp# * (mvr.crossprod.x#), mvr.pos.y# + mp# * (mvr.crossprod.y#), mvr.pos.z# + mp# * (mvr.crossprod.z#) )


	glEnd

	' kubus
	glBegin GL_POINTS
		glColor4f 0,0,1.0,1
		If mvr.pos.y# < 0
			glColor4f 0,0,1,0.5
		End If		
'		glVertex3f mvr.pos.x#, mvr.pos.y#, mvr.pos.z#
	glEnd
	
	' lint!!
	
	glRotatef 360 * mouse_x#, 0,1,0
	glRotatef 360 * mouse_y#, 0,0,1
	

	glBegin GL_QUAD_STRIP
		glColor4f 1,1,1,0.2
		n = 0
		For pos:Vector = EachIn vertexList
			n = n + 1
			glVertex3f pos.x#, pos.y#, pos.z#
			'glVertex3f pos.x#, pos.y#, pos.z#

	'		DebugLog n +"    "+poos.x# +"    "+poos.y# +"    "+poos.z#

		Next
	glEnd
	
'	DebugLog "- - - -"	

	' draw strip
'	glColor4f(1.0,0,0,0.2)

'	glVertexPointer( 3 , GL_FLOAT , 0 , vertices)
'	glDrawArrays( GL_QUAD_STRIP, 0, 12 )

	Flip



	tel = tel + 1
Wend

Function init()
	glClearColor(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),1.0,100.0)
	glMatrixMode(GL_MODELVIEW)
'	glEnable(GL_POLYGON_SMOOTH)

	glEnableClientState(GL_VERTEX_ARRAY)
	

	glPointsize(15)
	glLineWidth(1)
	
	GlEnable GL_LINE_SMOOTH
	GlEnable GL_POLYGON_SMOOTH
	
	GlEnable GL_BLEND
	glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
	glHint GL_LINE_SMOOTH_HINT, GL_NICEST
	glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
'    glBlendFunc (GL_SRC_ALPHA_SATURATE, GL_ONE_MINUS_SRC_ALPHA);


'	DebugLog String.FromCString( glGetString( GL_VENDOR ) ) + " | " ..
'	+  String.FromCString( glGetString( GL_VERSION ) )  + " | " ..
'	+  String.FromCString( glGetString( GL_RENDERER ) )  + " | " ..
'	+  String.FromCString( glGetString( GL_EXTENSIONS ) ) 


	
	
	
End Function

Function checkKeys()

	speed# = .2
	If KeyDown (KEY_LSHIFT)
		speed# = 0.5

	End If
	
	' MOVEMENT
	
	If KeyDown (KEY_UP)
		pos_z# = pos_z# - speed#
	End If
	If KeyDown (KEY_DOWN)
		pos_z# = pos_z# + speed#
	End If
	If KeyDown (KEY_LEFT)
		pos_x# = pos_x# - speed#
	End If
	If KeyDown (KEY_RIGHT)
		pos_x# = pos_x# + speed#
	End If
	If KeyDown (KEY_Q)
		pos_y# = pos_y# + speed#
	End If
	If KeyDown (KEY_A)
		pos_y# = pos_y# - speed#
	End If
	
	If KeyDown (KEY_R)
		ClearList vertexList
		mvr.init()
		
	End If



	'DebugLog (pos_x#+"  "+pos_y#+"  "+pos_z#)
End Function

Function drawAxis()
	length# = 30
	
	glLineStipple(1,HexToInt("0x00FF"))
	glEnable(GL_LINE_STIPPLE)
	
	glBegin GL_LINES
		glColor4f(0,0,0,0.2)
		' x-as
		glVertex3f -length, 0,0
		glVertex3f length, 0,0		
		' y-as		
		glVertex3f 0,-length, 0
		glVertex3f 0,length, 0
		' z-as		
		glVertex3f 0,0,-length
		glVertex3f 0,0,length
	glEnd
	
	length# = 10
	full# = 0.1
	glBegin GL_QUADS
		
		' y=0 plane
		' Q1
		glColor4f(0,0,0,full)
		glVertex3f(0,0,0)

		glColor4f(0,0,0,0)
		glVertex3f(length,0,0)
		glVertex3f(length,0,-length)		
		glVertex3f(0,0,-length)		

		' Q2
		glColor4f(0,0,0,full)
		glVertex3f(0,0,0)

		glColor4f(0,0,0,0)
		glVertex3f(-length,0,0)
		glVertex3f(-length,0,-length)		
		glVertex3f(0,0,-length)		
		
		' x=0 plane
		' Q1
		glColor4f(0,0,0,full)
		glVertex3f(0,0,0)

		glColor4f(0,0,0,0)
		glVertex3f(0,0,-length)
		glVertex3f(0,length,-length)		
		glVertex3f(0,length,0)		

		' Q2
'		glColor4f(0,0,0,full)
'		glVertex3f(0,0,0)
'
'		glColor4f(0,0,0,0)
'		glVertex3f(-length,0,0)
'		glVertex3f(-length,0,-length)		
'		glVertex3f(0,0,-length)		

	
	
	glEnd
End Function

' *********************************************************************************

Type Mover

	Field pos:Vector
	Field last_pos:Vector
	Field pre_last_pos:Vector
	
	Field direction:Vector
	Field last_direction:Vector
	
	Field crossprod:Vector
	
	
	Field counter#
	
	Field radius#
	
	Method init()
		pos:Vector = New Vector
		pos.x# = 0	
		pos.y# = 0
		pos.z# = -1
		
		last_pos:Vector = New Vector
		last_pos.x# = 0	
		last_pos.y# = 0
		last_pos.z# = 0
				
		pre_last_pos:Vector = New Vector
		pre_last_pos.x# = 0	
		pre_last_pos.y# = 0
		pre_last_pos.z# = 0

		
		direction:Vector = New Vector
		direction.x# = 0	
		direction.y# = 0
		direction.z# = 0
		
		last_direction:Vector = New Vector
		last_direction.x# = 0	
		last_direction.y# = 0
		last_direction.z# = 0
		
		crossprod:Vector = New Vector
		crossprod.x# = 0	
		crossprod.y# = 0
		crossprod.z# = 0
		
		
		
		
		radius# = 2.5
	End Method
	
	Method goBack()
		posX# = 0
		posY# = 0		
		posZ# = 0
	End Method

	Method update_direction:Vector()
		
		last_direction.x# = direction.x# 
		last_direction.y# = direction.y# 
		last_direction.z# = direction.z# 
		
		
		direction.x# = (pos.x# - last_pos.x#)
		direction.y# = (pos.y# - last_pos.y#)
		direction.z# = (pos.z# - last_pos.z#)		
		
'		DebugLog direction.x# +", "+ direction.y# +", "+ direction.z#
		
		Return direction
	End Method
	
	Method update_crossprod()
'	u cross v	=	x(u_y x v_z - u_z x v_y), y(u_z x v_x - u_x x v_z), z(u_x x v_y - u_y x v_x)


		crossprod.x# = ( direction.y# * last_direction.z# ) - ( direction.z# * last_direction.y# )
		crossprod.y# = ( direction.z# * last_direction.x# ) - ( direction.x# * last_direction.z# )
		crossprod.z# = ( direction.x# * last_direction.y# ) - ( direction.y# * last_direction.x# )
		
		crossprod = vector_normalize(crossprod)
		
'		DebugLog crossprod.x#
	
	End Method
	
	Method move()
		' huidige pos naar lastpos en prelastpos
		
		pre_last_pos.x# = last_pos.x#
		pre_last_pos.y# = last_pos.y#
		pre_last_pos.z# = last_pos.z#
		
		last_pos.x# = pos.x#
		last_pos.y# = pos.y#
		last_pos.z# = pos.z#
		
		' record
'		New recorded_pos.Create(1.0,2.0,3.0)


		' positie updaten
		pos.z# = Sin(counter) * radius#
		pos.y# = Cos(counter) * radius#
'		pos.x# = pos.x# + 0.03
		pos.x# =  Cos(counter/2) * radius#

		
		' richtingsvector		
		update_direction()
		
		'loodrechte ding
		update_crossprod()


		radius = radius - 0.01
		counter = counter + 10

	
	End Method
	
End Type


Type recorded_pos
	Field pos:Vector
	
	Function Create(x#,y#,z#)
		pos:Vector = New Vector
		pos.x# = x#
		pos.y# = y#
		pos.z# = z#				
	End Function
End Type




Type Vector
	Field x#, y#, z#
End Type

Function HexToInt( HexStr:String )
	Local n
	HexStr = HexStr.Trim().Replace("#","").Replace("$","").Replace("0x","").ToUpper()
	For Local d:Byte = 0 Until HexStr.length
		If HexStr[d]>64 And HexStr[d]<77 Then n = (n Shl 4) + HexStr[d]-55 ElseIf HexStr[d]>47 And HexStr[d]<58 Then n = (n Shl 4) + HexStr[d]-48 Else Return n
	Next
	Return n 
End Function

Function vector_length:Float (vec:Vector)
	Return Sqr ((vec.x#^2) + (vec.y#^2) + (vec.z#^2))
End Function

Function vector_normalize:Vector(vec:Vector)
	length# = vector_length(vec)
	vec.x# = vec.x# / length#
	vec.y# = vec.y# / length#
	vec.z# = vec.z# / length#		
	
	Return vec
End Function


Function createVector:Vector(x#,y#,z#)
		v:Vector = New Vector
		v.x# = x#
		v.y# = y#
		v.z# = z#
		
		Return v
End Function