Nehe tutorial

BlitzMax Forums/BlitzMax Tutorials/Nehe tutorial

NeheTutorials for BMax 1.22

Tutorial 6/7:

'begin tut6

Import brl.bmploader
Import brl.max2d

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

Global xrot:Float=0
Global yrot:Float=0
Global zrot:Float=0

Global Checkimage:Byte[256,256,4]
Global Texname:Int

GLGraphics ScreenWidth,ScreenHeight,ScreenDepth,0,BGL_BACKBUFFER | BGL_DEPTHBUFFER

InitGl()
HideMouse

While Not KeyHit( KEY_ESCAPE )
glClear GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT		'Clear The Screen And The Depth Buffer
	nehe6()
	glColor3f(1.0,1.0,1.0)
	'Framecounter--------------------------------------------
	Framecounter_counter=Framecounter_counter+1
	If Framecounter_time=0 Then Framecounter_time=MilliSecs()
	If Framecounter_time+1001 <MilliSecs() Then
		Framecounter_framerate=Framecounter_counter
		Framecounter_counter=0
		Framecounter_time=MilliSecs()
	EndIf
	GLDrawText("FPS : "+Framecounter_framerate,ScreenWidth-(8*12),ScreenHeight-16-8)
	'--------------------------------------------------------
	GLDrawText("Nehe lesson 6",10,24)
	Flip
Wend
End

Function InitGl()
	LoadGlTextures()
	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),1.0,100.0)
	glMatrixMode(GL_MODELVIEW)
End Function

Function LoadGlTextures()
	Local PointeurImg:Byte Ptr
	Local TexWidth
	Local TexHeight
	tex01:TPixmap=LoadPixmap("data\NeHe.bmp")
	TexWidth=tex01.Width
	TexHeight=tex01.Height
	PointeurImg=PixmapPixelPtr(tex01,0,0)
	pp=0
	For y=TexHeight-1 To 0 Step -1
		For x=0 To TexWidth-1
			Checkimage[y,x,0]=PointeurImg[pp]
			Checkimage[y,x,1]=PointeurImg[pp+1]
			Checkimage[y,x,2]=PointeurImg[pp+2]
			Checkimage[y,x,3]=100
			pp=pp+3
		Next
	Next
	tex01=Null
	glPixelStorei(GL_UNPACK_ALIGNMENT,1)
	glGenTextures(1, Varptr Texname)
	glBindTexture(GL_TEXTURE_2D, Texname)
	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)
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TexWidth, TexHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, Checkimage)
End Function

Function nehe6()
	glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
   	glEnable(GL_TEXTURE_2D)
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)
	glBindTexture GL_TEXTURE_2D,Texname
	
	glLoadIdentity
	glTranslatef 0.0,0.0,-5.0
	glRotatef xrot,1.0,0.0,0.0                         ' Rotate On The X Axis
	glRotatef yrot,0.0,1.0,0.0                         ' Rotate On The Y Axis
	glRotatef zrot,0.0,0.0,1.0                         ' Rotate On The Z Axis

	glBegin GL_QUADS
		' Front Face
		glTexCoord2f 0.0, 0.0; glVertex3f -1.0, -1.0, 1.0       ' Bottom Left Of The Texture And Quad
		glTexCoord2f 1.0, 0.0; glVertex3f  1.0, -1.0, 1.0       ' Bottom Right Of The Texture And Quad
		glTexCoord2f 1.0, 1.0; glVertex3f  1.0,  1.0, 1.0       ' Top Right Of The Texture And Quad
		glTexCoord2f 0.0, 1.0; glVertex3f -1.0,  1.0, 1.0       ' Top Left Of The Texture And Quad
		' Back Face
		glTexCoord2f 1.0, 0.0; glVertex3f -1.0, -1.0, -1.0      ' Bottom Right Of The Texture And Quad
		glTexCoord2f 1.0, 1.0; glVertex3f -1.0,  1.0, -1.0      ' Top Right Of The Texture And Quad
		glTexCoord2f 0.0, 1.0; glVertex3f  1.0,  1.0, -1.0      ' Top Left Of The Texture And Quad
		glTexCoord2f 0.0, 0.0; glVertex3f  1.0, -1.0, -1.0      ' Bottom Left Of The Texture And Quad
		' Top Face
		glTexCoord2f 0.0, 1.0; glVertex3f -1.0,  1.0, -1.0      ' Top Left Of The Texture And Quad
		glTexCoord2f 0.0, 0.0; glVertex3f -1.0,  1.0,  1.0      ' Bottom Left Of The Texture And Quad
		glTexCoord2f 1.0, 0.0; glVertex3f  1.0,  1.0,  1.0      ' Bottom Right Of The Texture And Quad
		glTexCoord2f 1.0, 1.0; glVertex3f  1.0,  1.0, -1.0      ' Top Right Of The Texture And Quad
		' Bottom Face
		glTexCoord2f 1.0, 1.0; glVertex3f -1.0, -1.0, -1.0      ' Top Right Of The Texture And Quad
		glTexCoord2f 0.0, 1.0; glVertex3f  1.0, -1.0, -1.0      ' Top Left Of The Texture And Quad
		glTexCoord2f 0.0, 0.0; glVertex3f  1.0, -1.0,  1.0      ' Bottom Left Of The Texture And Quad
		glTexCoord2f 1.0, 0.0; glVertex3f -1.0, -1.0,  1.0      ' Bottom Right Of The Texture And Quad
		' Right face
		glTexCoord2f 1.0, 0.0; glVertex3f  1.0, -1.0, -1.0      ' Bottom Right Of The Texture And Quad
		glTexCoord2f 1.0, 1.0; glVertex3f  1.0,  1.0, -1.0      ' Top Right Of The Texture And Quad
		glTexCoord2f 0.0, 1.0; glVertex3f  1.0,  1.0,  1.0      ' Top Left Of The Texture And Quad
		glTexCoord2f 0.0, 0.0; glVertex3f  1.0, -1.0,  1.0      ' Bottom Left Of The Texture And Quad
		' Left Face
		glTexCoord2f 0.0, 0.0; glVertex3f -1.0, -1.0, -1.0      ' Bottom Left Of The Texture And Quad
		glTexCoord2f 1.0, 0.0; glVertex3f -1.0, -1.0,  1.0      ' Bottom Right Of The Texture And Quad
		glTexCoord2f 1.0, 1.0; glVertex3f -1.0,  1.0,  1.0      ' Top Right Of The Texture And Quad
		glTexCoord2f 0.0, 1.0; glVertex3f -1.0,  1.0, -1.0      ' Top Left Of The Texture And Quad
	glEnd

	xrot = xrot + 0.3                     ' X Axis Rotation
	yrot = yrot + 0.2                     ' Y Axis Rotation
	zrot = zrot + 0.4                     ' Z Axis Rotation
	glDisable(GL_TEXTURE_2D)
End Function

'end tut6

########################################################################################################################

'begin tut7

Import brl.bmploader
Import brl.system

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

Global xrot:Float=0
Global yrot:Float=0
Global zrot:Float=0
Global Filter:Int=0
Global z:Float=-5.0
Global xspeed:Float=0.0
Global yspeed:Float=0.0
Global Lp:Byte=0

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

Global LightAmbient:Float[]=[0.5, 0.5, 0.5, 1.0]
Global LightDiffuse:Float[]=[1.0, 1.0, 1.0, 1.0]
Global LightPosition:Float[]=[0.0, 0.0, 2.0, 1.0]

GLGraphics ScreenWidth,ScreenHeight,ScreenDepth,0,BGL_BACKBUFFER | BGL_DEPTHBUFFER


InitGl()
HideMouse

While Not KeyHit( KEY_ESCAPE )
	nehe7()
	glDisable(GL_LIGHTING)
	glColor3f(1.0,1.0,1.0)
	'Framecounter--------------------------------------------
	Framecounter_counter=Framecounter_counter+1
	If Framecounter_time=0 Then Framecounter_time=MilliSecs()
	If Framecounter_time+1001 <MilliSecs() Then
		Framecounter_framerate=Framecounter_counter
		Framecounter_counter=0
		Framecounter_time=MilliSecs()
	EndIf
	GLDrawText("FPS : "+Framecounter_framerate,ScreenWidth-(8*12),ScreenHeight-16-8)
	'--------------------------------------------------------
	GLDrawText("Nehe lesson 7",10,24)
	glEnable(GL_LIGHTING)
	Flip
Wend
End

Function InitGl()
	LoadGlTextures()
	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),1.0,100.0)
	glMatrixMode(GL_MODELVIEW)
	glLightfv GL_LIGHT1, GL_AMBIENT, Float Ptr LightAmbient
	glLightfv GL_LIGHT1, GL_DIFFUSE, Float Ptr LightDiffuse
	glLightfv GL_LIGHT1, GL_POSITION, Float Ptr LightPosition
	glEnable GL_LIGHT1
End Function

Function LoadGlTextures()
	Local PointeurImg:Byte Ptr
	Local TexWidth
	Local TexHeight
	tex01:TPixmap=LoadPixmap("data\crate.bmp")
	TexWidth=tex01.Width
	TexHeight=tex01.Height
	PointeurImg=PixmapPixelPtr(tex01,0,0)
	pp=0
	For y=TexHeight-1 To 0 Step -1
		For x=0 To TexWidth-1
			Checkimage[y,x,0]=PointeurImg[pp]
			Checkimage[y,x,1]=PointeurImg[pp+1]
			Checkimage[y,x,2]=PointeurImg[pp+2]
			pp=pp+3
		Next
	Next
	tex01=Null
	' Create Nearest Filtered Texture
	glGenTextures 3, Varptr Texname[0]
	glBindTexture GL_TEXTURE_2D,Texname[0]
	glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST
	glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST
	glTexImage2D GL_TEXTURE_2D, 0, 3, TexWidth, TexHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, Checkimage
	' Create Linear Filtered Texture
	glBindTexture GL_TEXTURE_2D, Texname[1]
	glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR
	glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR
	glTexImage2D GL_TEXTURE_2D, 0, 3, TexWidth, TexHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, Checkimage
	' Create MipMapped Texture
	glBindTexture GL_TEXTURE_2D, Texname[2]
	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, TexWidth, TexHeight, GL_RGB, GL_UNSIGNED_BYTE, Checkimage
End Function

Function nehe7()
	If  KeyHit(KEY_L) Then
		lp = Not lp
	EndIf
	If  lp=0 Then
		glDisable(GL_LIGHTING)
	Else
		glEnable(GL_LIGHTING)
	EndIf
	If KeyHit(KEY_F)
		Filter:+1
		If Filter>2 Then Filter=0
	EndIf
	If KeyDown(KEY_PAGEUP) Then z:-0.02
	If KeyDown(KEY_PAGEDOWN) Then z:+ 0.02
	If KeyHit(KEY_UP) Then xspeed:-0.001
	If KeyHit(KEY_DOWN) Then xspeed:+0.001
	If KeyHit(KEY_RIGHT) Then yspeed:+0.001
	If KeyHit(KEY_LEFT) Then yspeed:-0.001
	
	glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
	glEnable GL_LIGHT1
	glEnable(GL_TEXTURE_2D)
	glBindTexture GL_TEXTURE_2D, Texname[Filter]
	
	glLoadIdentity
	glTranslatef 0.0,0.0,z
	glRotatef xrot,1.0,0.0,0.0
	glRotatef yrot,0.0,1.0,0.0
	glBegin GL_QUADS
		' Front Face
		glNormal3f  0.0, 0.0, 1.0
		glTexCoord2f 0.0, 0.0; glVertex3f -1.0, -1.0,  1.0
		glTexCoord2f 1.0, 0.0; glVertex3f  1.0, -1.0,  1.0
		glTexCoord2f 1.0, 1.0; glVertex3f  1.0,  1.0,  1.0
		glTexCoord2f 0.0, 1.0; glVertex3f -1.0,  1.0,  1.0
		' Back Face
		glNormal3f  0.0, 0.0,-1.0
		glTexCoord2f 1.0, 0.0; glVertex3f -1.0, -1.0, -1.0
		glTexCoord2f 1.0, 1.0; glVertex3f -1.0,  1.0, -1.0
		glTexCoord2f 0.0, 1.0; glVertex3f  1.0,  1.0, -1.0
		glTexCoord2f 0.0, 0.0; glVertex3f  1.0, -1.0, -1.0
		' Top Face
		glNormal3f  0.0, 1.0, 0.0
		glTexCoord2f 0.0, 1.0; glVertex3f -1.0,  1.0, -1.0
		glTexCoord2f 0.0, 0.0; glVertex3f -1.0,  1.0,  1.0
		glTexCoord2f 1.0, 0.0; glVertex3f  1.0,  1.0,  1.0
		glTexCoord2f 1.0, 1.0; glVertex3f  1.0,  1.0, -1.0
		' Bottom Face
		glNormal3f  0.0,-1.0, 0.0
		glTexCoord2f 1.0, 1.0; glVertex3f -1.0, -1.0, -1.0
		glTexCoord2f 0.0, 1.0; glVertex3f  1.0, -1.0, -1.0
		glTexCoord2f 0.0, 0.0; glVertex3f  1.0, -1.0,  1.0
		glTexCoord2f 1.0, 0.0; glVertex3f -1.0, -1.0,  1.0
		' Right face
		glNormal3f  1.0, 0.0, 0.0
		glTexCoord2f 1.0, 0.0; glVertex3f  1.0, -1.0, -1.0
		glTexCoord2f 1.0, 1.0; glVertex3f  1.0,  1.0, -1.0
		glTexCoord2f 0.0, 1.0; glVertex3f  1.0,  1.0,  1.0
		glTexCoord2f 0.0, 0.0; glVertex3f  1.0, -1.0,  1.0
		' Left Face
		glNormal3f -1.0, 0.0, 0.0
		glTexCoord2f 0.0, 0.0; glVertex3f -1.0, -1.0, -1.0
		glTexCoord2f 1.0, 0.0; glVertex3f -1.0, -1.0,  1.0
		glTexCoord2f 1.0, 1.0; glVertex3f -1.0,  1.0,  1.0
		glTexCoord2f 0.0, 1.0; glVertex3f -1.0,  1.0, -1.0
	glEnd

	xrot = xrot + xspeed
	yrot = yrot + yspeed
	glDisable(GL_TEXTURE_2D)
	glDisable GL_LIGHT1
End Function

'end tut7