Write to terrain texture

Blitz3D Forums/Blitz3D Programming/Write to terrain texture

abacadabad(Posted 2003) [#1]
Hi there,
Can anyone help me with this bit of code;

;////////////
terrain2=LoadTerrain ("Heightmap.bmp")
ScaleEntity terrain2, 1,12,1
PositionEntity terrain2,0,7,0
EntityTexture terrain2,tex1

texbuf=TextureBuffer(tex1)
LockBuffer db

For x=0 To 256
For y=0 To 256
height=TerrainHeight# ( terrain2,x,y )
If height>0.5
Color 255,0,0
WritePixel x,y,(Color),texbuf
EndIf
Next
Next
UnlockBuffer texbuf
;///////////

When the program runs, a large patch of the terrain texture (seemingly unrelated to height) is painted black, and the onscreen text goes red.
I would actually like the texture to write depending on the height of the returned terrain, which could make lots of cool effects (snow on mountains changing with seasons etc), but this deosnt seem to work?

Thanks, :-)


makakoman(Posted 2003) [#2]
You need to get the rgb value of the color for your WritePixel command.

For 32 bit RGB values:

alpha = bits 24 to 31
red = bits 16 to 23
green = bits 8 to 15
blue = bits 0 to 7

argb = (red shl 16) + (green shl 8) + blue

or in your case

argb = (255 shl 16) ; you don't use green and blue

or you could just use $FF0000 for red.

Then:

WritePixel x, y, argb, texbuf

and if you are going to bother to lock the buffer then use:

WritePixelFast x,y,argb, texbuf

You might also want to check your LockBuffer db.

Shouldn't that be LockBuffer texbuf?

One more thing, I think your loops should be 0 to 255, not 0 to 256.


;//////////// 
terrain2=LoadTerrain ("Heightmap.bmp") 
ScaleEntity terrain2, 1,12,1 
PositionEntity terrain2,0,7,0 
EntityTexture terrain2,tex1 

texbuf=TextureBuffer(tex1) 
LockBuffer texbuf

For x=0 To 255
For y=0 To 255 
height=TerrainHeight# ( terrain2,x,y ) 
argb = $FF0000
If height>0.5 
WritePixelFast x,y,argb,texbuf 
EndIf 
Next 
Next 
UnlockBuffer texbuf 
;/////////// 


Hope this helps.
MK


abacadabad(Posted 2003) [#3]
Hi!
Thankyou for your reply :-) Fortunately the code paints the colour red, but unfortuanelty the area painted doesnt match up to the height. It seems to paint it correctly, just offset in the wrong place. Any ideas?

Thanks :-)


abacadabad(Posted 2003) [#4]
Hi again,
After mucking around it seems that the texture is painted correctly, but just reversed onto the terrain. Why do you think this would happen and how would you fix it? I tried putting in a "RotateTexture" command, but that just clears the texture of the red completely :-(

Thanks again


Bolo_Loco(Posted 2003) [#5]
hi

try this :

WritePixelFast x,255-y,argb,texbuf

Bolo


makakoman(Posted 2003) [#6]
abacadabad,

The reason the texture looks like it paints upside down is because the 2D origin is at the upper left of the texture, and the 3D origin is at the lower left. The easiest way to fix this that I know of is to just scale the texture with a negative value on the Y axis.
For instance: ScaleTexture(1, -1) or ScaleTexture(256, -256)

Although the method Bolo_Loco suggest will work just as well!

Good Luck,
MK


DrakeX(Posted 2003) [#7]
i'm not sure why B3D uses the lower-left as UV (0,0), when every other 3d language (and DirectX itself) uses the top left as UV (0,0). very confusing and annoying.


abacadabad(Posted 2003) [#8]
Hi,
It works!! :-) Thankyou very much! Ill get cracking and post some screenshots later on on how it hoes...thanks again!