Weird VertexTexCoords behaviour?

Blitz3D Forums/Blitz3D Programming/Weird VertexTexCoords behaviour?

Midnight(Posted 2004) [#1]
I might have this completely wrong, but I'm just starting to experiment with 2 UV coordinate sets.

Here's what I want to do: Create a square with a smaller square in its center. Texture the whole square with some texture (Woodfloor), then blend a second texture (Light) only in the smaller square.

Here's my code:

Graphics3D 640,480,16,2

Camera = CreateCamera()
PositionEntity Camera,0,1,-5
AmbientLight 255,255,255

tex=LoadTexture("woodfloor1.jpg")
tex2=LoadTexture("light.jpg")

TextureCoords tex2,1

LevelMesh=CreateMesh()

Surface1=CreateSurface(LevelMesh)
AddVertex (Surface1,-1,1,0,0,0)
AddVertex (Surface1,1,1,0,1,0)
AddVertex (Surface1,-1,-1,0,0,1)
AddVertex (Surface1,1,-1,0,1,1)
VertexTexCoords Surface1,0,0,0,0,0
VertexTexCoords Surface1,1,1,0,0,0
VertexTexCoords Surface1,2,0,1,0,0
VertexTexCoords Surface1,3,1,1,0,0

AddTriangle (Surface1,0,1,2)
AddTriangle (Surface1,1,3,2)

AddVertex (Surface1,-.5,.5,0,0,0)
AddVertex (Surface1,.5,.5,0,1,0)
AddVertex (Surface1,-.5,-.5,0,0,1)
AddVertex (Surface1,.5,-.5,0,1,1)
VertexTexCoords Surface1,4,0,0,0,1
VertexTexCoords Surface1,5,1,0,0,1
VertexTexCoords Surface1,6,0,1,0,1
VertexTexCoords Surface1,7,1,1,0,1

AddTriangle (Surface1,4,5,6)
AddTriangle (Surface1,5,7,6)

EntityTexture LevelMesh,tex,0,0
EntityTexture LevelMesh,tex2,0,1

UpdateNormals LevelMesh

Repeat
	UpdateWorld()
	RenderWorld()
	
	Flip
	
Until KeyDown(1)


Now, two things happen that I don't understand:

1) Instead of the desired result, the two textures are blended together and then applied to both the larger and the smaller square (i.e. the smaller square is a carbon copy of the larger square)- how do I get the desired result?

2) If in the second set of AddVertex's (for the smaller square) I remove the U,V values at the end, I get a completely different result - why would this even affect my result?

Any thoughts?

Thanks in advance,
Patrick


Shambler(Posted 2004) [#2]
Two textures requires two surfaces,



Graphics3D 640,480,16,2


Camera = CreateCamera()

PositionEntity Camera,0,1,-5

AmbientLight 255,255,255


tex=LoadTexture("woodfloor1.jpg") 
tex2=LoadTexture("light.jpg") 


TextureCoords tex2,1


LevelMesh=CreateMesh()


Surface1=CreateSurface(LevelMesh)

AddVertex (Surface1,-1,1,0,0,0)

AddVertex (Surface1,1,1,0,1,0)

AddVertex (Surface1,-1,-1,0,0,1)

AddVertex (Surface1,1,-1,0,1,1)

AddTriangle (Surface1,0,1,2)

AddTriangle (Surface1,1,3,2)


Surface2=CreateSurface(LevelMesh)

AddVertex (Surface2,-.5,.5,0,0,0)

AddVertex (Surface2,.5,.5,0,1,0)

AddVertex (Surface2,-.5,-.5,0,0,1)

AddVertex (Surface2,.5,-.5,0,1,1)

AddTriangle (Surface2,4,5,6)

AddTriangle (Surface2,5,7,6)


EntityTexture LevelMesh,tex,0,0

EntityTexture LevelMesh,tex2,0,1


UpdateNormals LevelMesh


Repeat

	UpdateWorld()

	RenderWorld()

	

	Flip

	

Until KeyDown(1)


2) The addvertex command can't affect the second UV coord set, thats why you get different results when you remove your vertextexcoords commands.


jhocking(Posted 2004) [#3]
You don't need two surfaces to do blending. A given surface can have up to 8 layers of textures (although video hardware only supports a couple layers in a single pass, so 8 layers would be excessive.)


Shambler(Posted 2004) [#4]
Hmmm...time to read that manual lol


Shambler(Posted 2004) [#5]
The problem is that all triangles will have tex applied to uv coords 0 and tex2 applied to uv coords 1.

This is why the light texture has a square edge around it which looks like the wood texture, you can't tell Blitz that the 2 small triangles should only have tex2 applied to them unless you use 2 different surfaces.

The alternative is to just use 2 triangles and position the light texture using a combination of scaleing and uv offsetting remembering to load the texture with clampU and clampV flags.


Graphics3D 640,480,16,2

Camera = CreateCamera()

PositionEntity Camera,0,1,-5

AmbientLight 255,255,255

tex=LoadTexture("woodfloor1.jpg") 
tex2=LoadTexture("light.jpg",16+32) 

TextureCoords tex2,1

LevelMesh=CreateMesh()


Surface1=CreateSurface(LevelMesh)

AddVertex (Surface1,-1,1,0,0,0)
AddVertex (Surface1,1,1,0,1,0)
AddVertex (Surface1,-1,-1,0,0,1)
AddVertex (Surface1,1,-1,0,1,1)


VertexTexCoords Surface1,0,0,0,0,1 
VertexTexCoords Surface1,1,1,0,0,1 
VertexTexCoords Surface1,2,0,1,0,1 
VertexTexCoords Surface1,3,1,1,0,1
 


AddTriangle (Surface1,0,1,2) 
AddTriangle (Surface1,1,3,2) 




TextureBlend tex2,2 ;or 3 
 
EntityTexture LevelMesh,tex,0,0

EntityTexture LevelMesh,tex2,0,1

UpdateNormals LevelMesh

u#=0.0
v#=0.0

Repeat

If KeyDown(205) Then u#=u#+0.01
If KeyDown(203) Then u#=u#-0.01
If KeyDown(208) Then v#=v#+0.01
If KeyDown(200) Then v#=v#-0.01

PositionTexture tex2,u#,v#

	UpdateWorld()

	RenderWorld()


	Flip


Until KeyDown(1)




Midnight(Posted 2004) [#6]
Thanks Shambler - your second solution isn't really what I'm looking for, since it doesn't use a second independent set of texture coordinates.

Basically I want to do a very simply example of light/shadow. I thought I could use one set of vertices for one texture (using VertexTextureCoords) and a second set of vertices for light/shadow blending (a second set of coordinates is required so I can do this anywhere on the model). This was my understanding how a basic lightmapped model would be displayed - perhaps I'm wrong?


Ross C(Posted 2004) [#7]
I wouldn't use two sets of uv co-ords for that. Because, when you load a texture in, you can move the texture anyways. So just do:


<createvertexs/triangles>

tex=loadtexture("firsttex.bmp")
tex2=loadtexture("shadow.bmp")

EntityTexture model,tex,0,0
EntityTexture model,tex2,0,1

MoveTexture tex2,0.5,0.2 ;<<<<line for moving texture.



Make sure that you use the two clamp flags, so the texture doesn't get tiled. No need to use 2 sets of UV Co-ords :)