texture trouble :/

BlitzMax Forums/BlitzMax OpenGL Programming/texture trouble :/

Nope, still doesnt work on my engine. Really weird.

If anyone wants to try it:

library.bmx
'Application: opengl lib
'Author: nawi
'Description: the main library 

Global TexCount:Int = 0


'Constants
Const MDL_CUBE = 1


Extern "win32"
	Function GetActiveWindow() = "GetActiveWindow@0"
	Function SetCursorPos(x,y) = "SetCursorPos@8"
	Function GetWindowRect(hWnd,lpRect:Rect) = "GetWindowRect@8"
End Extern

Type Rect
	Field X,Y,W,H
End type

Type Entity
	Field Name$
	Field X#,Y#,Z#
	Field RotX#,RotY#,RotZ#
	
	Field Wireframe
	
	Field Alpha#
	
	Field Txt:Texture
	
	Global EntityList:Tlist
	
	Field PolygonList:TList
	Field Counter
	Field Cp#[6],Cc[6]
	
	Function Create:Entity(NewX#=0,NewY#=0,NewZ#=0)
		Local Ent:Entity = New Entity
		Ent.X# = NewX#
		Ent.Y# = NewY#
		Ent.Z# = NewZ#
		Ent.Alpha# = 1.0
		Return Ent
	End Function
	
	Method New()
		If EntityList = Null Then EntityList = CreateList()
		If PolygonList = Null Then PolygonList = CreateList()
		EntityList.AddLast Self
	End Method
	
	Method ApplyTexture(NewTxt:Texture)
		Txt = NewTxt
	End Method
	
	
	Method Add(X#,Y#,Z#,U#=0.0,V#=0.0,R=255,G=255,B=255)
		If Counter = 2 Then
			Counter = 0
			Local Poly:Polygon = New Polygon
			PolygonList.AddLast Poly
			
			Poly.P[0] = New Point
			Poly.P[0].X# = Cp#[0]; Poly.P[0].Y# = Cp#[1]; Poly.P[0].Z# = Cp#[2];
			Poly.P[0].R = Cc[0]; Poly.P[0].G = Cc[1]; Poly.P[0].B = Cc[2];
			Poly.P[0].U# = U#; Poly.P[0].V# = V#
			
			Poly.P[1] = New Point
			Poly.P[1].X# = Cp#[3]; Poly.P[1].Y# = Cp#[4]; Poly.P[1].Z# = Cp#[5];
			Poly.P[1].R = Cc[3]; Poly.P[1].G = Cc[4]; Poly.P[1].B = Cc[5];
			Poly.P[1].U# = U#; Poly.P[1].V# = V#
			
			Poly.P[2] = New Point
			Poly.P[2].X# = X#; Poly.P[2].Y# = Y#; Poly.P[2].Z# = Z#;
			Poly.P[2].R = R; Poly.P[2].G = G; Poly.P[2].B = B;
			Poly.P[2].U# = U#; Poly.P[2].V# = V#
		Else
			Cp#[Counter*3] = X#
			Cp#[Counter*3+1] = Y#
			Cp#[Counter*3+2] = Z#
			Cc[Counter*3] = R
			Cc[Counter*3+1] = G
			Cc[Counter*3+2] = B
			Counter = Counter + 1
		EndIf
		
	End Method
	
	Method Turn(AddX#=0,AddY#=0,AddZ#=0)
		RotX# = RotX# + AddX#
		RotY# = RotY# + AddY#
		RotZ# = RotZ# + AddZ#
		
		If RotX# > 360 Then RotX# = 0
		If RotY# > 360 Then RotY# = 0
		If RotZ# > 360 Then RotZ# = 0			

		If RotX# < 0 Then RotX# = 360
		If RotY# < 0 Then RotY# = 360
		If RotZ# < 0 Then RotZ# = 360					
	End Method
	
	Method Rotate(NewX#=0,NewY#=0,NewZ#=0)
		RotX# = NewX#
		RotY# = NewY#
		RotZ# = NewZ#
		
		If RotX# > 360 Then RotX# = 0
		If RotY# > 360 Then RotY# = 0
		If RotZ# > 360 Then RotZ# = 0			

		If RotX# < 0 Then RotX# = 360
		If RotY# < 0 Then RotY# = 360
		If RotZ# < 0 Then RotZ# = 360					
	End Method
	
	Method SetColor(NewR,NewG,NewB)
		For Local Pol:Polygon = EachIn PolygonList
			Pol.P[0].R = NewR
			Pol.P[0].G = NewG
			Pol.P[0].B = NewB
			Pol.P[1].R = NewR
			Pol.P[1].G = NewG
			Pol.P[1].B = NewB
			Pol.P[2].R = NewR
			Pol.P[2].G = NewG
			Pol.P[2].B = NewB			
		Next
	End Method
	
	Method AddPrimitive(ModelType)
		If ModelType = MDL_CUBE Then
			'FRONT TRI 1
			Self.Add(-1,1,1,0,1)
			Self.Add(-1,-1,1,0,0)
			Self.Add(1,1,1,1,1)
			
			'FRONT TRI2
			Self.Add(-1,-1,1,0,0)
			Self.Add(1,-1,1,1,0)
			Self.Add(1,1,1,1,1)
			
			'RIGHT TRI1
			Self.Add(1,1,1)
			Self.Add(1,-1,1)
			Self.Add(1,-1,-1)
			
			'RIGHT TRI2
			Self.Add(1,1,1)
			Self.Add(1,-1,-1)
			Self.Add(1,1,-1)
			
			'BACK TRI1
			Self.Add(1,1,-1)
			Self.Add(1,-1,-1)
			Self.Add(-1,-1,-1)
			
			'BACK TRI2
			Self.Add(1,1,-1)
			Self.Add(-1,-1,-1)
			Self.Add(-1,1,-1)
			
			'LEFT TRI1
			Self.Add(-1,1,-1)
			Self.Add(-1,-1,-1)
			Self.Add(-1,-1,1)
			
			'LEFT TRI2
			Self.Add(-1,1,-1)
			Self.Add(-1,-1,1)
			Self.Add(-1,1,1)
		
			'BOTTOM TRI1
			Self.Add(-1,-1,1)
			Self.Add(-1,-1,-1)
			Self.Add(1,-1,-1)
			
			'BOTTOM TRI2
			Self.Add(-1,-1,1)
			Self.Add(1,-1,-1)
			Self.Add(1,-1,1)
			
			'TOP TRI1
			Self.Add(1,1,-1)
			Self.Add(-1,1,-1)	
			Self.Add(-1,1,1)
					
			'TOP TRI2
			Self.Add(1,1,1)			
			Self.Add(1,1,-1)
			Self.Add(-1,1,1)
								
		EndIf
	End Method
	
	Method AddModel(Filename$) 'not done
		'.3DS
		If Lower(Right$(FileName$,4) = ".3ds") Then
			Local Chunk_ID:Short,Chunk_Length,Chunk_Quantiny,Chunk_Flags,i
			Local File:TStream = OpenFile(FileName$,1,0)
			If File Then
				While Not Eof(File)	
					Chunk_ID = ReadShort(File)
					Chunk_Length = ReadInt(File)
					
					'Print Chunk_ID
					
					Select Chunk_ID
						Case $4d4d 'Main chunk
						Print "Found main chunk!"
						
						Case $3d3d '3d editor chunk
						Print "Found 3d editor chunk!"
						
						Case $4000 'Object chunk
						Print "Found object chunk!"
						'Read model name
						Repeat
							i = ReadByte(File)
							Name$ = Name$ + Asc(i)
						Until Eof(File) Or (Asc(i) = 0) Or (i = 13)
						
						Print Name$
					
					End Select
					
					'Seek
					File.Seek(File.Pos()+Chunk_Length-6)
					
				Wend
			EndIf
		EndIF
	End Method
	
	
	Method Move(AddX#=0,AddY#=0,AddZ#=0)
		X# = X# + Cos(RotY#) * AddX#
		Y# = Y# + Sin(RotZ#) * AddX#
		Z# = Z# + Sin(RotY#) * AddX#
		
		Y# = Y# + Cos(RotX#) * -AddY#

		X# = X# + Sin(-RotY#) * AddZ#
		Y# = Y# + Sin(RotX#) * AddZ#
		Z# = Z# + Cos(-RotY#) * AddZ#
	End Method
	
End type

Type Polygon
	Field P:Point[3]
End type

Type Point
	Field X#,Y#,Z#
	Field U#,V#
	Field R,G,B
End type

Type Graphics
	Field Width,Height,Depth,Screen
	Field Window,GetRect:Rect
	
	Field FPSTimer,FPSCounter,FPSShow

	Function Start:Graphics(NewWidth,NewHeight,NewDepth=32,NewScreen=0)
		Local Gfx:Graphics = New Graphics
		Gfx.Width = NewWidth
		Gfx.Height = NewHeight
		Gfx.Depth = NewDepth
		Gfx.Screen = NewScreen
		
		Local Flags = BGL_BACKBUFFER|BGL_DEPTHBUFFER
		If NewScreen = 0 Then Flags = Flags + BGL_FULLSCREEN
		bglCreateContext(Gfx.Width,Gfx.Height,Gfx.Depth,0,Flags)
		
		glViewport(0,0,Gfx.Width,Gfx.Height)
		
		Gfx.Window = GetActiveWindow()


		glShadeModel(GL_SMOOTH)
		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
		glClearColor(0,0,0, 0.0)
		glColor3f(1.0, 1.0, 1.0)
		glClearDepth(1.0)
		glEnable(GL_CULL_FACE)
		glEnable(GL_DEPTH_TEST) 
		glDepthFunc(GL_LEQUAL) 
		glMatrixMode(GL_PROJECTION)
		glLoadIdentity()
		gluPerspective(45.0, Float(Gfx.Width)/Float(Gfx.Height), 0.1, 100.0)
		glMatrixMode(GL_MODELVIEW)
		glLoadIdentity()
		
		Gfx.GetRect = New Rect
		
		Return Gfx
	End Function
	
	Method Viewport(NewX,NewY,NewWidth,NewHeight)
		glViewport(NewX,NewY,NewWidth,NewHeight)
	End Method
	
	
	Method ClearColor(R,G,B)
		glClearColor(R/255.0,G/255.0,B/255.0, 0.0)
	End Method
	
	
	Method Render(Cam:Camera)
		glLoadIdentity()
		
		'Position camera
		glRotatef(Cam.RotX#,1.0,0,0)
		glRotatef(Cam.RotY#,0,1.0,0)
		glRotatef(Cam.RotZ#,0,0,1.0)
		glTranslatef(Cam.X#,Cam.Y#,Cam.Z#)
		glPopMatrix()
		
		'Entities
		For Local Ent:Entity = EachIn Entity.EntityList	
			For Local Pol:Polygon = EachIn Ent.PolygonList
				glPushMatrix()
				
				'Texture
				If Ent.Txt <> Null Then
					glEnable(GL_TEXTURE_2D)
					glBindTexture(GL_TEXTURE_2D,Ent.Txt.GLID)
				EndIf
				
				'Alpha
				If Ent.Alpha# <> 1.0 Then
					glEnable(GL_BLEND)
				EndIf
				
				'WireFrame
				If Ent.Wireframe then
					glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
					glDisable(GL_CULL_FACE)
				EndIf
				
				
				'Position entity
				glTranslatef(Ent.X#,Ent.Y#,Ent.Z#)
				glRotatef(Ent.RotX#,1.0,0,0)
				glRotatef(Ent.RotY#,0,1.0,0)
				glRotatef(Ent.RotZ#,0,0,1.0)

				
				
				'Draw
				glBegin(GL_TRIANGLES)
				
				'Tri1
				If Ent.Alpha# <> 1.0 Then
					glColor4f(Pol.P[0].R/255.0,Pol.P[0].G/255.0,Pol.P[0].B/255.0,Ent.Alpha#)
				Else
					glColor3f(Pol.P[0].R/255.0,Pol.P[0].G/255.0,Pol.P[0].B/255.0)
				EndIf
				glTexCoord2f(Pol.P[0].U#,Pol.P[0].V#)
				glVertex3f(Pol.P[0].X#,Pol.P[0].Y#,Pol.P[0].Z#)

				'Tri2
				If Ent.Alpha# <> 1.0 Then
					glColor4f(Pol.P[1].R/255.0,Pol.P[1].G/255.0,Pol.P[1].B/255.0,Ent.Alpha#)
				Else
					glColor3f(Pol.P[1].R/255.0,Pol.P[1].G/255.0,Pol.P[1].B/255.0)
				EndIf
				glTexCoord2f(Pol.P[1].U#,Pol.P[1].V#)
				glVertex3f(Pol.P[1].X#,Pol.P[1].Y#,Pol.P[1].Z#)				

				'Tri3
				If Ent.Alpha# <> 1.0 Then
					glColor4f(Pol.P[2].R/255.0,Pol.P[2].G/255.0,Pol.P[2].B/255.0,Ent.Alpha#)
				Else
					glColor3f(Pol.P[2].R/255.0,Pol.P[2].G/255.0,Pol.P[2].B/255.0)
				EndIf
				glTexCoord2f(Pol.P[2].U#,Pol.P[2].V#)
				glVertex3f(Pol.P[2].X#,Pol.P[2].Y#,Pol.P[2].Z#)	
				
				glEnd()
				
				'Alpha
				If Ent.Alpha# <> 1.0 Then
					glDisable(GL_BLEND)
				EndIf
				
				'Texture
				If Ent.Txt <> Null Then
					glDisable(GL_TEXTURE_2D)	
				EndIf
				
				'WireFrame
				If Ent.Wireframe then
					glPolygonMode(GL_FRONT, GL_FILL)
					glEnable(GL_CULL_FACE)
				EndIf		
				
				glPopMatrix()
			Next
		Next
		
		'FPS Counter
		FPSCounter = FPSCounter + 1
		If MilliSecs() > FPSTimer + 999 Then
			FPSTimer = MilliSecs()
			FPSShow = FPSCounter
			FPSCounter = 0
		EndIf
	End Method
	
	Method Cls()
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
	End Method
	
	Method Flip()
		bglSwapBuffers()
	End Method
	
	Method CenterMouse()
		GetWindowRect(Window,GetRect)
		SetCursorPos(GetRect.X-Width/2,GetRect.Y-Height/2)
	End Method
	
	Method FPS()
		Return FPSShow
	End Method
End type

Type Texture
	Field GLID
	Field Priority#

	Function Create:Texture(Filename$)
		Local Txt:Texture = New Texture
		
		Local Pixmap:TPixmap = LoadPixmap(Filename$)
		If Not Pixmap Then
			Print "Could not load texture!"
			Return Null
		EndIf

		Txt.GLID = bglTexFromPixmap(Pixmap)
		If Not Txt.GLID Then
			Print "Could not apply texture to opengl!"
			Return Null
		EndIf
		
		
		
		
		glBindTexture(GL_TEXTURE_2D, Txt.GLID)
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

		Pixmap = Null
				
		
		Return Txt
	End Function
	
End type


Type Camera
	Field X#,Y#,Z#
	Field RotX#,RotY#,RotZ#
	
	Function Create:Camera(NewX#=0,NewY#=0,NewZ#=0)
		Local Cam:Camera = New Camera
		Cam.X# = NewX#
		Cam.Y# = NewY#
		Cam.Z# = NewZ#
		
		Return Cam
	End Function
	
	Method Turn(AddX#=0,AddY#=0,AddZ#=0)
		RotX# = RotX# + AddX#
		RotY# = RotY# + AddY#
		RotZ# = RotZ# + AddZ#
		
		If RotX# > 360 Then RotX# = 0
		If RotY# > 360 Then RotY# = 0
		If RotZ# > 360 Then RotZ# = 0			

		If RotX# < 0 Then RotX# = 360
		If RotY# < 0 Then RotY# = 360
		If RotZ# < 0 Then RotZ# = 360					
	End Method
	
	Method Rotate(NewX#=0,NewY#=0,NewZ#=0)
		RotX# = NewX#
		RotY# = NewY#
		RotZ# = NewZ#
		
		If RotX# > 360 Then RotX# = 0
		If RotY# > 360 Then RotY# = 0
		If RotZ# > 360 Then RotZ# = 0			

		If RotX# < 0 Then RotX# = 360
		If RotY# < 0 Then RotY# = 360
		If RotZ# < 0 Then RotZ# = 360					
	End Method
	
	Method Move(AddX#=0,AddY#=0,AddZ#=0)
		X# = X# + Cos(RotY#) * AddX#
		Y# = Y# + Sin(RotZ#) * AddX#
		Z# = Z# + Sin(RotY#) * AddX#
		
		Y# = Y# + Cos(RotX#) * -AddY#

		X# = X# + Sin(-RotY#) * AddZ#
		Y# = Y# + Sin(RotX#) * AddZ#
		Z# = Z# + Cos(-RotY#) * AddZ#
	End Method
	
	
End type


opengl.bmx
'Application: opengl test
'Author: nawi
'Description: test the opengl lib
'
Strict


'for mouselook
Local MX,MY



Include "library.bmx"

'Initialize
Local System:Graphics = Graphics.Start(800,600,32,1)

'Create entity
Local Cube:Entity = Entity.Create()
'Cube.AddPrimitive(MDL_CUBE)
Cube.Add(-1,1,-1,0,0)
Cube.Add(-1,-1,-1,0,1)
Cube.Add(1,-1,-1,1,1)

Cube.Add(1,-1,-1,0,0)
Cube.Add(1,1,-1,1,0)
Cube.Add(-1,1,-1,1,1)

Cube.SetColor(107,230,255)

Local Cube2:Entity = Entity.Create(-4)
Cube2.AddPrimitive(MDL_CUBE)
Cube2.Alpha# = 0.5
Cube2.SetColor(255,245,68)

Local Fighter:Entity = Entity.Create(7)
'Fighter.AddModel("fighter.3ds")
Fighter.AddPrimitive(MDL_CUBE)
Fighter.Wireframe = True

'Create camera
Local Cam:Camera = Camera.Create(0,0,-10)

'Load texture
Local Txt:Texture = Texture.Create("tex.bmp")

Cube.ApplyTexture(Txt)

Repeat
	System.Cls()
	



	'Mouselook
	Cam.Turn(Float(MouseY() - MY)/10.0,Float(MouseX() - MX)/10.0,0)
	System.CenterMouse()
	MX = MouseX()
	MY = MouseY()
	
	'move camera
	If KeyDown(KEY_UP) Then Cam.Move(0,0,0.07)
	If KeyDown(KEY_DOWN) Then Cam.Move(0,0,-0.07)
	If KeyDown(KEY_LEFT) Then Cam.Move(0.07)
	If KeyDown(KEY_RIGHT) Then Cam.Move(-0.07)
	If KeyDown(KEY_Q) Then Cam.Move(0,0.07)
	If KeyDOWN(KEY_A) Then Cam.Move(0,-0.07)
	
	
	
	'rotate cube
	Cube2.Turn(0.2,0.2,0.1)
		
	System.Render(Cam)
	System.Flip()
	Print "FPS " + System.FPS() + " MEM " + MemAlloced()
	FlushMem()
Until KeyHit(KEY_ESCAPE)