#Include Once "gl/gl.bi"
#Include Once "gl/glu.bi"
#Include Once "fbgfx.bi"
#Include Once "skybox.bi"

Using FB

Declare Sub LoadTexture(tex As UInteger Ptr, filename As String)

Const ScrW = 1024
Const ScrH = 768
ScreenRes scrW,scrH,32,,FB.GFX_OPENGL

'' Initialise OpenGL
glMatrixMode (GL_PROJECTION)
glLoadIdentity()
gluPerspective (45.0, ScrW/ScrH, .1, 1000)
glMatrixMode (GL_MODELVIEW)
glLoadIdentity()
glEnable (GL_DEPTH_TEST)

'' Load textures
Dim Shared skybox(6) As GLuint
LoadTexture(@skybox(0), "skyup.bmp")
LoadTexture(@skybox(1), "skydn.bmp")
LoadTexture(@skybox(2), "skyrt.bmp")
LoadTexture(@skybox(3), "skylf.bmp")
LoadTexture(@skybox(4), "skyft.bmp")
LoadTexture(@skybox(5), "skybk.bmp")

CreateSkyBox(skybox()) ' create the box

Dim As Integer MouseX, MouseY, mdx, mdy
Dim As Integer CamLookX=1, CamLookY=0, CamLookZ=0

SetMouse scrW\2, scrH\2, 0
Do
	GetMouse MouseX, MouseY
	mdx = MouseX - scrW\2
	mdy = MouseY - scrH\2
	SetMouse scrW\2, scrH\2, 0
	
	CamLookY += mdx
	CamLookX += mdy
	
	'' Rendering
	glClear (GL_DEPTH_BUFFER_BIT Or GL_COLOR_BUFFER_BIT)
	glLoadIdentity
	DoSkyBox CamLookX, CamLookY, CamLookZ
	
	' other stuff here '
	
	Flip
Loop While Not MultiKey(1)
End


Const TEX_MASKED = &h1
Const TEX_MIPMAP = &h2
Const TEX_NOFILTER = &h4
Const TEX_HASALPHA = &h8

'------------------------------------------------------------------------
'' Create texture creates textures from BLOAD buffer
Private Function CreateTexture( ByVal buffer As Any Ptr, ByVal flags As Integer = 0 ) As UInteger
	
	Dim p As UInteger Ptr
	Dim As Integer w, h, x, y, col
	Dim tex As UInteger
	Dim As GLenum Format, minfilter, magfilter
	Dim As FB.PUT_HEADER Ptr header = buffer

	Function = 0
    
    If header->Type = FB.PUT_HEADER_NEW Then
		w = header->Width
		h = header->height
    Else
		w = header->old.width
		h = header->old.height
    End If
    

	If( (w < 64) Or (h < 64) ) Then
		Exit Function
	End If
	If( (w And (w-1)) Or (h And (h-1)) ) Then
		'' Width/height not powers of 2
		Exit Function
	End If

	ReDim dat(0 To (w * h) - 1) As UInteger
	p = @dat(0)

	glGenTextures 1, @tex
	glBindTexture GL_TEXTURE_2D, tex

	For y = h-1 To 0 Step -1
		For x = 0 To w-1
			col = Point(x, y, buffer)
			'' Swap R and B so we can use the GL_RGBA texture format
			col = RGB(col And &hFF, _
				(col Shr 8) And &hFF, _
				(col Shr 16) And &hFF)
			If( (flags And TEX_MASKED) And (col = &hFF00FF) ) Then
				*p = 0
			Else
				*p = col Or &hFF000000
			End If
			p += 1
		Next x
	Next y

	If (flags And (TEX_MASKED Or TEX_HASALPHA)) Then
		Format = GL_RGBA
	Else
		Format = GL_RGB
	End If

	If (flags And TEX_NOFILTER) Then
		magfilter = GL_NEAREST
	Else
		magfilter = GL_LINEAR
	End If

	If( flags And TEX_MIPMAP) Then
		gluBuild2DMipmaps GL_TEXTURE_2D, Format, w, h, GL_RGBA, _
			GL_UNSIGNED_BYTE, @dat(0)

		If (flags And TEX_NOFILTER) Then
			minfilter = GL_LINEAR_MIPMAP_NEAREST
		Else
			minfilter = GL_LINEAR_MIPMAP_LINEAR
		End If
	Else
		glTexImage2D GL_TEXTURE_2D, 0, Format, w, h, 0, GL_RGBA, _
			GL_UNSIGNED_BYTE, @dat(0)
		minfilter = magfilter
	End If

	glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter
	glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter

	Function = tex

End Function


Sub LoadTexture(tex As UInteger Ptr, filename As String)
	Dim image As Any Ptr
	image = ImageCreate(512,512)
	BLoad filename, image
	'create and bind the texture ( courtesy of lillo )
	*tex = CreateTexture(image, TEX_MIPMAP)
    glTexParameterf GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE
    glTexParameterf GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE
End Sub

