MipMap OpenGL

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
JohnB
Posts: 236
Joined: Jul 22, 2005 3:53
Location: Minnesota Arizona

MipMap OpenGL

Post by JohnB »

The link to source, exe, texture. (No external libraries required, I hope.)

http://www.mediafire.com/?j2m1zyldyyn

Use the w-key to rotate, then the pageup, pagedown to to position the square. Colors on the square change depending on the texture used.

JohnB

Code: Select all

'
'
' MipMapping - 20080313 - JohnB
' FreeBASIC 18.3 - OpenGL - FBGFX
'
' http://www.mediafire.com/?j2m1zyldyyn
'
'
#Include Once "GL/gl.bi"
#Include Once "GL/glu.bi"
#Include Once "fbgfx.bi"
'
' bmpload.bi is located in \FreeBASIC\EXAMPLES\libraries\GL\NeHe
' copy bmpload.bi to the compile/exe dir or add path to include 
'
#Include Once "bmpload.bi"

#ifndef FALSE
#define FALSE 0
#define TRUE (Not FALSE)
#EndIf

#ifndef NULL
#define NULL 0
#endif

#Define scr_width  1024
#Define scr_height  768
#Define scr_bpp		32

Dim Shared As GLuint textureBase
	
Dim As Integer i,j
Dim Shared As Double x, y, z, rotx, roty, rotz

ScreenRes scr_width, scr_height, scr_bpp, 2, FB.GFX_OPENGL

glViewport 0, 0, scr_width, scr_height
glMatrixMode GL_PROJECTION

glLoadIdentity
gluPerspective 80/2, scr_width/scr_height, 1.0, 500.0    
glMatrixMode GL_MODELVIEW

glLoadIdentity
glShadeModel GL_SMOOTH
glClearColor 0.0, 0.0, 0.0, 1.0
glClearDepth 1.0
glEnable GL_DEPTH_TEST
glDepthFunc GL_LEQUAL
glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
'
' display black screen while loading textures
'
glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
Flip

Dim As Integer done = 0
Dim As Integer status = TRUE
Dim As Any Ptr img
Dim As Uinteger w, h
Dim As String file_name
Dim TextureImage(0) as BITMAP_RGBImageRec ptr

file_name = ExePath + "\textures\texbase256.bmp" 
TextureImage(0) = LoadBMP(file_name)
If TextureImage(0) <> NULL Then  
	status And= TRUE
	glGenTextures 1, @textureBase
	glBindTexture GL_TEXTURE_2D, textureBase
'	glTexImage2D GL_TEXTURE_2D, 0, 3,TextureImage(0)->sizeX, TextureImage(0)->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage(0)->buffer
'	glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST_MIPMAP_NEAREST
'	glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST_MIPMAP_NEAREST
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST)
	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage(0)->sizeX, TextureImage(0)->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage(0)->buffer)

	If TextureImage(0) <> NULL Then 
		If TextureImage(0)->buffer Then
			DeAllocate(TextureImage(0)->buffer)
		End If
		DeAllocate(TextureImage(0))
	End If
Else
	status And= FALSE
End If

file_name = ExePath + "\textures\tex256.bmp" 
TextureImage(0) = LoadBMP(file_name)
If TextureImage(0) <> NULL Then  
	status And= TRUE
	glTexImage2D GL_TEXTURE_2D, 0, 3, TextureImage(0)->sizeX, TextureImage(0)->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage(0)->buffer
	If TextureImage(0) <> NULL Then 
		If TextureImage(0)->buffer Then
			DeAllocate(TextureImage(0)->buffer)
		End If
		DeAllocate(TextureImage(0))
	End If
Else
	status And= FALSE
End If

file_name = ExePath + "\textures\tex128.bmp" 
TextureImage(0) = LoadBMP(file_name)
If TextureImage(0) <> NULL Then  
	status And= TRUE
	glTexImage2D GL_TEXTURE_2D, 1, 3, TextureImage(0)->sizeX, TextureImage(0)->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage(0)->buffer
	If TextureImage(0) <> NULL Then 
		If TextureImage(0)->buffer Then
			DeAllocate(TextureImage(0)->buffer)
		End If
		DeAllocate(TextureImage(0))
	End If
Else
	status And= FALSE
End If

file_name = ExePath + "\textures\tex64.bmp" 
TextureImage(0) = LoadBMP(file_name)
If TextureImage(0) <> NULL Then  
	status And= TRUE
	glTexImage2D GL_TEXTURE_2D, 2, 3, TextureImage(0)->sizeX, TextureImage(0)->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage(0)->buffer
	If TextureImage(0) <> NULL Then 
		If TextureImage(0)->buffer Then
			DeAllocate(TextureImage(0)->buffer)
		End If
		DeAllocate(TextureImage(0))
	End If
Else
	status And= FALSE
End If

file_name = ExePath + "\textures\tex32.bmp" 
TextureImage(0) = LoadBMP(file_name)
If TextureImage(0) <> NULL Then  
	status And= TRUE
	glTexImage2D GL_TEXTURE_2D, 3, 3, TextureImage(0)->sizeX, TextureImage(0)->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage(0)->buffer
	If TextureImage(0) <> NULL Then 
		If TextureImage(0)->buffer Then
			DeAllocate(TextureImage(0)->buffer)
		End If
		DeAllocate(TextureImage(0))
	End If
Else
	status And= FALSE
End If

file_name = ExePath + "\textures\tex16.bmp" 
TextureImage(0) = LoadBMP(file_name)
If TextureImage(0) <> NULL Then  
	status And= TRUE
	glTexImage2D GL_TEXTURE_2D, 4, 3, TextureImage(0)->sizeX, TextureImage(0)->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage(0)->buffer
	If TextureImage(0) <> NULL Then 
		If TextureImage(0)->buffer Then
			DeAllocate(TextureImage(0)->buffer)
		End If
		DeAllocate(TextureImage(0))
	End If
Else
	status And= FALSE
End If

file_name = ExePath + "\textures\tex8.bmp" 
TextureImage(0) = LoadBMP(file_name)
If TextureImage(0) <> NULL Then  
	status And= TRUE
	glTexImage2D GL_TEXTURE_2D, 5, 3, TextureImage(0)->sizeX, TextureImage(0)->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage(0)->buffer
	If TextureImage(0) <> NULL Then 
		If TextureImage(0)->buffer Then
			DeAllocate(TextureImage(0)->buffer)
		End If
		DeAllocate(TextureImage(0))
	End If
Else
	status And= FALSE
End If

If status = FALSE Then glDeleteTextures 1, @textureBase : End

z = 100.0 : rotx = 0.0 : roty = 0.0
Do While (done = 0)
	glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
	If z < 1.1 Then z = 1.1	

	glMatrixMode GL_MODELVIEW
   glLoadIdentity
	gluLookAt 0, 0, z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0

	glRotatef rotz, 0.0, 0.0, 1.0	
	glRotatef roty, 0.0, 1.0, 0.0	
	glRotatef rotx, 1.0, 0.0, 0.0	

	glEnable GL_TEXTURE_2D
	glColor3f 1.0, 1.0, 1.0

' 256x256	base = white
' 256x256	 256 = red
' 128x128	 128 = blue
'  64x64		  64 = green
'  32x32		  32 = yellow
'  16x16		  16 = purple
'   8x8			8 = light blue

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST)

	glBindTexture GL_TEXTURE_2D, textureBase
	x = -25.0 : y = -25.0
	For i = 0 To 24
		For j = 0 To 24
			glPushMatrix()
			glTranslatef x, y, 0.0
			glBegin(GL_QUADS)
				glNormal3f(0.0f,0.0f,1.0f)
				glTexCoord2f(0.0, 0.0) : glVertex3f(-1.0, -1.0, 0.0)
				glTexCoord2f(1.0, 0.0) : glVertex3f( 1.0, -1.0, 0.0)
				glTexCoord2f(1.0, 1.0) : glVertex3f( 1.0,  1.0, 0.0)
				glTexCoord2f(0.0, 1.0) : glVertex3f(-1.0,  1.0, 0.0)
			glEnd()
			glPopMatrix()
			x += 2.0			
		Next
		x = -25.0
		y += 2.0
	Next
	
	If MultiKey(FB.SC_ESCAPE) Then done = 1
	
	If MultiKey(FB.SC_PAGEUP)   Then z += 0.1
	If MultiKey(FB.SC_PAGEDOWN) Then z -= 0.1

	If MultiKey(FB.SC_W) Then rotx -= 0.1
	If MultiKey(FB.SC_S) Then rotx += 0.1
		
	If MultiKey(FB.SC_A) Then roty -= 0.1
	If MultiKey(FB.SC_D) Then roty += 0.1
		
	If MultiKey(FB.SC_Q) Then rotz += 0.1
	If MultiKey(FB.SC_E) Then rotz -= 0.1

	If MultiKey(FB.SC_X) Then End 

	If MultiKey(FB.SC_R) Then z = 100.0: rotx = 0.0 : roty = 0.0 : rotz = 0.0

	Flip 
Loop

' cleanup
glDeleteTextures 1, @textureBase

while inkey <> "": wend
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

the link is down
JohnB
Posts: 236
Joined: Jul 22, 2005 3:53
Location: Minnesota Arizona

Post by JohnB »

I just upload the zip again, then downloaded, link should be ok.

JohnB
Post Reply