What's with the texturing here?

Blitz3D Forums/Blitz3D Beginners Area/What's with the texturing here?

WillKoh(Posted 2004) [#1]
I've made this flat square of two traingles, and the textures are simply 24-bit bmps with no black in them ( except a few pixs possibly in smuida). They become either transparent when alpha flag is set, or invisible if I don't set it. The outcommented read/write pix was an attempt to set full opacity.. It's also supposed to rotate but it don't...

	
	cam = CreateCamera()
	
	AmbientLight 200,200,200
	;light = CreateLight(2,cam)
	
	mesh = CreateMesh()
	surf = CreateSurface(mesh)

	v0 = AddVertex(surf,0,0,0)
	v1 = AddVertex(surf,10,0,0)
	v2 = AddVertex(surf,0,-10,0)
	
	tri = AddTriangle (surf,V0,v1,v2)
	
	txflags=2
	
	mytex = LoadTexture("tex.bmp",txflags)
	sumida = LoadTexture("c:\windows\sumida.bmp",txflags)
	stucco = LoadTexture("c:\windows\stucco.bmp",txflags)
	
		EntityAlpha mesh,1.0
		
	SetBuffer TextureBuffer(mytex)
		For x = 0 To TextureWidth(mytex) - 1
		For y = 0 To TextureWidth(mytex) - 1
			;ritePixel x,y,ReadPixel(x,y) Or $FF000000
		Next
		Next
	
	SetBuffer BackBuffer()

	ScaleMesh mesh, 2, 2, 2
	EntityTexture Mesh, stucco
			
	mesh2 = CopyEntity(mesh,mesh)
	RotateEntity mesh2,0,0,180
	
	plane=CreatePlane()
	
	EntityTexture mesh2,mytex
	EntityTexture plane,sumida
	
	MoveEntity cam,0,+10,-25
	PositionEntity mesh2,+20,-20,0
	
rotams=10

While Not KeyHit(1)

	If KeyDown (200)
		MoveEntity mesh,0,0,+0.2
	ElseIf KeyDown(205)
		MoveEntity mesh,0.2,0,0
	ElseIf KeyDown(203)
		MoveEntity mesh,-0.2,0,0
	ElseIf KeyDown(208)
		MoveEntity mesh,0,0,-0.2
	End If
	If rotams => Abs(MilliSecs()-msces)
		msecs =MilliSecs()
		RotateEntity Mesh, 0.7,0.7,0.7
	End If
	UpdateWorld()
	RenderWorld()
		Flip
Wend



Andy(Posted 2004) [#2]
You're not setting the UV coordinates...

AddVertex(surf,0,0,0)

http://www.blitzbasic.com/b3ddocs/command.php?name=AddVertex&ref=3d_cat

Andy


Damien Sturdy(Posted 2004) [#3]
basic Vertex UV values per triangle, if your new. I remember forgetting this!
(0,1) (1,1)
o o



o o
(0,0) (1,0)

Addvertex(surf x,y,z,u,v,w) is the way to use it. Currently, W doesnt do anything, so you set the UV coordinates like this:

bottom left:
addvertex(surf,x,y,z,0,0)

bottom right:
addvertex(surf,x,y,z,1,0)

top left:
addvertex(surf,x,y,z,0,1)

top right:
addvertex(surf,x,y,z,1,1)

(0,0) (1,0), (0,1), (1,1) make the entire texture fit onto the quad. you can use floating point values to only make part of the texture fit onto the quad, like this:
(.25,.25) (.75,.25) (.25,.75) (.75,.75).. which would take a quarter of the texture (actually, the middle bit) and stretch it across the quad


Forgive me if im terrible explaining, Will Someone clean up after me? I know you lot can do much better explanations than me!!!

Cheers.


WillKoh(Posted 2004) [#4]
OK, here's an example implementing those U,V:s. The pixel loop is about checking whether custom alpha works on a texure without the alpha flag. (Without the alpha flag I can use 2d commands on it, according to Paul Gerfen, but I don't want to to it unless the alpha byte is still in effect which 'may or may not' be the case). What's wrong now? It doesn't even become a square now...

Graphics3D 640,480


	tex = LoadTexture("c:\windows\stucco.bmp",2)
	
	buf = TextureBuffer(tex)
	
	wid = TextureWidth(tex)
	hig = TextureHeight(tex)
	
	SetBuffer buf: LockBuffer()
	
For x = 0 To wid - 1
	For y = 0 To hig - 1
		pixel = ReadPixelFast(x,y)
	  WritePixelFast X,Y, x Shl 24 Or (pixel And $00ffffff)
	Next
Next

	UnlockBuffer()

	
	cam = CreateCamera()
	
	mesh = CreateMesh()
	surf = CreateSurface(mesh)
		
	v1 = AddVertex(surf, 0,0, 0,1)
	v2 = AddVertex(surf, 1,0, 1,1)
	v3 = AddVertex(surf, 0,-1, 1,0)
	v4 = AddVertex(surf, 1,-1, 0,0)
	
	AddTriangle surf, v1,v2,v3
	AddTriangle surf, v1,v3,v4
	
	EntityTexture mesh, tex
	
	
	
While Not KeyHit(1)


	If KeyDown(200) Then 
		MoveEntity mesh, 0,0,+0.1
	ElseIf KeyDown(208) Then
		MoveEntity mesh, 0,0,-0.1
	ElseIf KeyDown(30) Then
		MoveEntity cam, 0,+0.1,0
	ElseIf KeyDown(44)
		MoveEntity cam, 0,-0.1,0
	End If
	
	UpdateWorld()
	RenderWorld()
	
	Flip
Wend



I know it's only me that keeps windows xp's "Santa Fe Stucco" image as "C:\windows\stucco.bmp" so it's always at hand. I don't remember its original exact path. But it's just a beige 256x256 bitmap...


jfk EO-11110(Posted 2004) [#5]
As far as I see you load the texture with flag 2 and then write some values to the pixels, but leave the most significant byte (the alpha byte) to be zero. Of course everything will be invisible if that byte is zero all the time. The alphabyte at $FF000000 will define the transparency of a texel. If you load a texture with flag 2, that byte will be set by the brightness of the texel automaticly (unless you load an image that already has an alpha channel, such as a 32 Bit TGA).

But if you once start writing values to the texture, you need to set the alphabyte yourself. You could use alpha=(red+green+blue)/3, or "if rgb=$FF00FF" then alpha=0, anyway, but it's up to you to set the alpha byte.

Personally I prefere to use 32 Bit TGAs with predefined Alpha Channel. This also allows to use fake shadows that are opaque in ther black center and fade to transparent at their white borders, for example.

EDIT - I have just realized that "x Shl 24" does set the alpha, so forget about that.


DJWoodgate(Posted 2004) [#6]
You missed the z coord in addvertex. Triangles should have a consistent winding order. That is to say, from a given viewpoint, you link the verts in a clockwise order otherwise they will not be facing that viewpoint (they will be backfacing). Below I used a counterclockwise order because I then decided I wanted the triangles to face in a positive direction along the local Z axis. I adjusted the UV's accordingly so the texture is oriented correctly and centred the quad on the origin.