OpenGL - Terrain Problem or Perspective code wrong

BlitzMax Forums/BlitzMax Programming/OpenGL - Terrain Problem or Perspective code wrong

FBEpyon(Posted 2005) [#1]
Im trying to figure out why there isn't any terrain showing in this code its really give me problem :( I can see anything Im asumming that its building the terrain because I have read thru 3 terrain tutorials and they all point out to do this same thing... look at my terrain system first and if you see thats wrong let me know, and if Im setting up the modes wrong i would like to know..

Main.bb

Framework brl.blitzgl
Import brl.pngloader
Import brl.system
Import brl.linkedlist
Import brl.random

Include "terrain.bmx"

Global TWidth:Int
Global THeight:Int

Global SWidth:Int = 800
Global SHeight:Int = 600
Global SDepth:Int = 32


glSetup()

Global Test = TTerrain.Create(0,0)

Function glSetup()
	
	bglCreateContext( SWidth, SHeight, SDepth, 0, BGL_BACKBUFFER | BGL_DEPTHBUFFER )


	glViewport(0,0,Width,SHeight)
	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()

	gluPerspective(45.0,Float(SWidth)/Float(SHeight),0.1,100.0)

	glMatrixMode(GL_MODELVIEW)
	glLoadIdentity()
	
	glShadeModel(GL_NICEST)
	glClearColor(0.0,0.0,0.0,0.0)
	glClearDepth(1.0)
	glEnable(GL_DEPTH_TEST)
	glDepthFunc(GL_LEQUAL)
	glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST)	
	
	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)	
	
	Return True
	
End Function

Function glRender()

	Repeat

		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
		glLoadIdentity()
		
			For Terrain:TTerrain = EachIn TerrainList
				Terrain.Render()
			Next

		FlushMem
		bglSwapBuffers

	Until KeyHit(KEY_ESCAPE)
	End

End Function

glRender()



terrain.bb

Global Terrain:TTerrain
Global TerrainList:TList = CreateList()
Const TERRAIN_STEP = 16
Const TERRAIN_SIZE = 255
Const TERRAIN_SCALE = 16

Type TTerrain

	Field X:Float, Z:Float
	Field Height:Float[255,255]
	Field ID:Int
	Field HeightMap:TPixmap

	Method Render()
	
			glTranslatef(Terrain.X, 1.0, Terrain.Z)
		
			For Local xx = 0 To (TERRAIN_SIZE-TERRAIN_SCALE)-1 Step TERRAIN_STEP
			glBegin(GL_TRIANGLE_STRIP)
				For Local zz = 0 To (TERRAIN_SIZE-TERRAIN_SCALE)-1 Step TERRAIN_STEP
					glVertex3f(xx+TTERRAIN_SCALE,Terrain.Height[xx+TERRAIN_SCALE,zz],zz)
					glVertex3f(xx,Terrain.Height[xx,zz],zz)
				Next
			glEnd()
			Next
	
	End Method
	
	Function Create:Int(xx:Int, zz:Int)
	
		Terrain:TTerrain = New TTerrain
		Terrain.X = xx
		Terrain.Z = zz
		Terrain.ID = Terrain.ID + 1
		
		For Local xxx = 0 To TERRAIN_SIZE-1
			For Local zzz = 0 To TERRAIN_SIZE-1	
				Terrain.Height[xxx,zzz] = Rnd(255)
			Next
		Next
		
		ListAddLast(TerrainList,Terrain)
		
		Return Terrain.ID
	
	End Function
	
	Function Load:Int(file:String, xx:Int, zz:Int)
	
		Terrain:TTerrain = New TTerrain
		Terrain.X = xx
		Terrain.Z = zz
		Terrain.ID = Terrain.ID + 1
		
		Terrain.HeightMap = LoadPixmap(file)
		
		For Local xxx = 0 To TERRAIN_SIZE-1
			For Local zzz = 0 To TERRAIN_SIZE-1
				Terrain.Height[xxx,zzz] = ReadPixel(Terrain.HeightMap,xxx,zzz)
			Next
		Next
		
		ListAddLast(TerrainList,Terrain)
		
		Return Terrain.ID
	
	End Function

End Type




Odds On(Posted 2005) [#2]
Hi FBEpyon,

I found a few problems just by adding Strict at the top of main.bb.

You have used Width instead of SWidth in the glViewport( ) function, and you have used TTERRAINSCALE instead of TERRAINSCALE in your Render( ) function.

Apart from that, I think the only problem is that you're not translating the terrain into a position where it is in view of the camera..

If you want the terrain to be centered you will need to do something like this:

glTranslatef(Terrain.X-TERRAIN_SIZE/2.0,-60.0,Terrain.Z-TERRAIN_SIZE/2.0)
This should make the terrain visible at least, but I would also lower the maximum height of the terrain to something less than 50.

Best Regards,
Chris


FBEpyon(Posted 2005) [#3]
thanks man,

I still don't have anything displaying in the screen, I tried making a basic quad cube and there was nothing still I think it has something to do with my glsetup, but I see nothing wrong :(


Odds On(Posted 2005) [#4]
This is the code I ended up with after making the modifications I mentioned above:

main.bmx
Strict

Framework brl.blitzgl
Import brl.pngloader
Import brl.system
Import brl.linkedlist
Import brl.random

Include "terrain.bmx"

Global TWidth:Int
Global THeight:Int

Global SWidth:Int = 800
Global SHeight:Int = 600
Global SDepth:Int = 32


glSetup()

Global Test = TTerrain.Create(0,0)

Function glSetup()
	
	bglCreateContext( SWidth, SHeight, SDepth, 0, BGL_BACKBUFFER | BGL_DEPTHBUFFER )


	glViewport(0,0,SWidth,SHeight)
	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()

	gluPerspective(45.0,Float(SWidth)/Float(SHeight),0.1,100.0)

	glMatrixMode(GL_MODELVIEW)
	glLoadIdentity()
	
	glShadeModel(GL_NICEST)
	glClearColor(0.0,0.0,0.0,0.0)
	glClearDepth(1.0)
	glEnable(GL_DEPTH_TEST)
	glDepthFunc(GL_LEQUAL)
	glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST)	
	
	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)	
	
	Return True
	
End Function

Function glRender()

	Repeat

		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
		glLoadIdentity()
		
			For Terrain:TTerrain = EachIn TerrainList
				Terrain.Render()
			Next

		FlushMem
		bglSwapBuffers

	Until KeyHit(KEY_ESCAPE)
	End

End Function

glRender()
terrain.bmx
Global Terrain:TTerrain
Global TerrainList:TList = CreateList()
Const TERRAIN_STEP = 16
Const TERRAIN_SIZE = 255
Const TERRAIN_SCALE = 16

Type TTerrain

	Field X:Float, Z:Float
	Field Height:Float[255,255]
	Field ID:Int
	Field HeightMap:TPixmap

	Method Render()
	
			'glTranslatef(Terrain.X, 1.0, Terrain.Z)
			glTranslatef(Terrain.X-TERRAIN_SIZE/2.0,-60.0,Terrain.Z-TERRAIN_SIZE/2.0)
		
			For Local xx = 0 To (TERRAIN_SIZE-TERRAIN_SCALE)-1 Step TERRAIN_STEP
			glBegin(GL_TRIANGLE_STRIP)
				For Local zz = 0 To (TERRAIN_SIZE-TERRAIN_SCALE)-1 Step TERRAIN_STEP
					glVertex3f(xx+TERRAIN_SCALE,Terrain.Height[xx+TERRAIN_SCALE,zz],zz)
					glVertex3f(xx,Terrain.Height[xx,zz],zz)
				Next
			glEnd()
			Next
	
	End Method
	
	Function Create:Int(xx:Int, zz:Int)
	
		Terrain:TTerrain = New TTerrain
		Terrain.X = xx
		Terrain.Z = zz
		Terrain.ID = Terrain.ID + 1
		
		For Local xxx = 0 To TERRAIN_SIZE-1
			For Local zzz = 0 To TERRAIN_SIZE-1	
				Terrain.Height[xxx,zzz] = Rnd(50)
			Next
		Next
		
		ListAddLast(TerrainList,Terrain)
		
		Return Terrain.ID
	
	End Function
	
	Function Load:Int(file:String, xx:Int, zz:Int)
	
		Terrain:TTerrain = New TTerrain
		Terrain.X = xx
		Terrain.Z = zz
		Terrain.ID = Terrain.ID + 1
		
		Terrain.HeightMap = LoadPixmap(file)
		
		For Local xxx = 0 To TERRAIN_SIZE-1
			For Local zzz = 0 To TERRAIN_SIZE-1
				Terrain.Height[xxx,zzz] = ReadPixel(Terrain.HeightMap,xxx,zzz)
			Next
		Next
		
		ListAddLast(TerrainList,Terrain)
		
		Return Terrain.ID
	
	End Function

End Type
Hope that helps,
Chris


FBEpyon(Posted 2005) [#5]
Well wtf lol,

THanks this code works great and there wasn't much that wass changed I don't get it but hey its works noww a that all matter to me :P


FBEpyon(Posted 2005) [#6]
okay this maybe stupid part of me, but im trying to load a Heighmap from a png or whatnot..

can someone show me the correct way to load a heightmap using a pixmap and readpixel


Function Load:Int(file:String, xx:Int, zz:Int)
	
		Terrain:TTerrain = New TTerrain
		Terrain.X = xx
		Terrain.Z = zz
		Terrain.ID = Terrain.ID + 1
		
		Terrain.HeightMap = LoadPixmap(file)
		
		For Local xxx = 0 To TERRAIN_SIZE-1
			For Local zzz = 0 To TERRAIN_SIZE-1
				Terrain.Height[xxx,zzz] = ReadPixel(Terrain.HeightMap,xxx,zzz)
			Next
		Next
		
		ListAddLast(TerrainList,Terrain)
		
		Return Terrain.ID
	
	End Function



this doesn't work at all :(


Odds On(Posted 2005) [#7]
The value returned by ReadPixel is in Integer RGB format. Meaning the Red, Green and Blue values need to be extracted from the value returned by ReadPixel and averaged to create the terrain height.

For Local xxx = 0 To TERRAIN_SIZE-1
	For Local zzz = 0 To TERRAIN_SIZE-1
		Local pix:Int = ReadPixel(Terrain.HeightMap,xxx,zzz)
		Local red:Int = ( pix Shr 16 ) & $FF
		local green:Int = ( pix Shr 8 ) & $FF
		Local blue:int = pix & $FF
		Local height:Int = (red+green+blue)/3
		Terrain.Height[xxx,zzz] = height
 	Next
Next
You may need to reposition the terrain a bit to be able to see it better.


FBEpyon(Posted 2005) [#8]
Thank,

I had figured that out, but I couldn't think of how to shift things :( I never done any shifting commands before


FBEpyon(Posted 2005) [#9]
Sorry for the DP

Im looking for a different way to do the terrain rendering maybe using GL_TRIANGLES instead of GL_TRIANGLE_STRIPS

if any of you have a idea how I can go about doing this let me know please thanks..

also sense this is going ot be a editor, im looking for ideas on how to keep track of the y of the terrain so I can edit it.. thanks..