I'm stuck...

Blitz3D Forums/Blitz3D Programming/I'm stuck...

Chroma(Posted 2006) [#1]
Been trying to get this function to load a heightmap and it works ok. But...

The bottom and right vertices aren't effected by the heighmap and stay at 0,0,0. I've tried all kinds of stuff and probably spent too long on it. Can a fresh set of eyes see what the problem is?

Function CreateSegQuad(hmap$)

	temp = LoadImage(hmap$)
	If temp = 0 Then End
	tdim = ImageWidth(temp)
	seg=tdim

	mesh = CreateMesh()
	surf = CreateSurface(mesh)
	; vertices
	For y = 0 To seg
		For x = 0 To seg
			AddVertex surf,x,0,y-1,0,x,-y
			VertexTexCoords surf,x+((seg+1)*y),x,-y
		Next
	Next
	; triangles	
	For y = 0 To seg-1
		For x = 0 To seg-1
			v=x+s
			AddTriangle surf,v,v+seg+1,v+seg+2
			AddTriangle surf,v,v+seg+2,v+1
		Next
	s=s+seg+1
	Next
	; center quad
	PositionEntity mesh,-seg/2.0,0,-seg/2.0
	
	; alter vertice height to match the heightmap red channel
	SetBuffer ImageBuffer(temp)
	For lx = 0 To x
		For ly = 0 To y
			GetColor lx,y-ly
			index = lx + ((x+1)*ly)
			VertexCoords surf, index , VertexX(surf,index), ColorRed()/20.0,VertexZ(surf,index)
		Next
	Next
	SetBuffer BackBuffer()

	UpdateNormals mesh
	Return mesh
End Function



Stevie G(Posted 2006) [#2]
I think this is your issue is ...

If your heightmap is 64 x 64 pixels you can only have 0 ... 63 height points rather than 0-64 which you seem to be doing.

In your getcolor section your also going from 0-64 so the last heightpoint cannot be read as it's outwith the image.

I would also recomment using unwelded quads to build up the terrain so that it can be textured per quad .. it also makes the terrain easier to build.

Stevie


Chroma(Posted 2006) [#3]
Thanks Stevie.

All I had to do was change
seg=tdim


to
seg=tdim-1


It appears to be working correctly now. :)


Sir Gak(Posted 2006) [#4]
Yah, it's easy to forget that computers start counting at zero (0), so when you specify any number 'n', it's 0 to 'n-1'. Typically, this comes into effect when using a DIM statement, such as:
 Dim myarray(100) ;for 100 positions.

Problem is, that's actually 101 positions, ie 0 to 100, inclusive.


Ross C(Posted 2006) [#5]
I dunno if the same applies to vertex numbers and surface numbers. I can't remember. They confuse me anyway :D


(tu) sinu(Posted 2006) [#6]
vertices start at 0 and surfaces at 1 iirc.