VertexTexCoords - Help needed

Blitz3D Forums/Blitz3D Programming/VertexTexCoords - Help needed

.rIKmAN.(Posted 2012) [#1]
I have looked through the forums, read thread after thread and tried for about 2hrs to get my head around this, and I just can't do it!

Could anyone please explain what values I need to use with the VertexTexCoords command to display each of the four 32x32 squares from the texture on the face of the quad in full, and what it is I am doing wrong.

I just cannot fathom it out for some reason and am starting to feel completely dumb, no matter what I do the texture just distorts in all manner of ways I don't want it to - I 'm just not grasping it at all!

As usual I'm sure it's something simple.

Thanks in advance.


; Graphics
Graphics3D 640,480,16,2
SetBuffer BackBuffer()

; Create quad
mesh = CreateMesh()
surf = CreateSurface(mesh)

v0 = AddVertex (surf,  -1, +1, 0,  0  , 0)
v1 = AddVertex (surf,  +1, +1, 0,  1  , 0)
v2 = AddVertex (surf,  +1, -1, 0,  1  , 1)
v3 = AddVertex (surf,  -1, -1, 0,  0  , 1)

AddTriangle surf,v0,v1,v2
AddTriangle surf,v0,v2,v3

; Create cube
cube = CreateCube()
PositionEntity cube,-4,0,0

; Create texture
tex = CreateTexture(64,64)
SetBuffer TextureBuffer(tex)

ClsColor 255,255,255
Cls

Color 255,0,0
Rect 0,0,32,32,1
Color 0,255,0
Rect 32,0,32,32,1
Color 255,255,0
Rect 0,32,32,32,1
Color 0,0,255
Rect 32,32,32,32,1

SetBuffer BackBuffer()

;
; *** NEED HELP HERE ***
;

;VertexTexCoords surf,



; Apply textures
EntityTexture cube,tex
EntityTexture mesh,tex

; Creat camera
cam = CreateCamera()
MoveEntity cam, 0,0,-7

; Main loop
While Not KeyDown(1)
	
	Cls 
	
	RenderWorld
	
	Text 0,0,"Surfaces: "+CountSurfaces(mesh)
	
	Flip
	
Wend
End



Yasha(Posted 2012) [#2]
I think you might have VertexTexCoords confused in your mind with ScaleTexture and PositionTexture (which could be used for this specific task, but are obviously less flexible in the general case).

VertexTexCoords adjusts the U and V coordinates of one vertex (it has no effect on the W coordinate, despite what the documentation says, because the W coordinate isn't actually used in B3D and doesn't mean anything). All that means is that for that one vertex, the U and V will now be whatever you passed to VertexTexCoords instead of whatever you originally set them to in the fifth and sixth parameters of AddVertex.

So, to "move" a quad, you need to adjust all of the vertices of that quad, not just use one command. To display the red zone only on the quad mesh in your example, you could add this:

VertexTexCoords surf, v0, 0  , 0
VertexTexCoords surf, v1, 0.5, 0
VertexTexCoords surf, v2, 0.5, 0.5
VertexTexCoords surf, v3, 0  , 0.5



This is now exactly the same as if you'd created the quad using the AddVertex command with different parameters:

v0 = AddVertex (surf,  -1, +1, 0,  0  , 0)
v1 = AddVertex (surf,  +1, +1, 0,  0.5  , 0)
v2 = AddVertex (surf,  +1, -1, 0,  0.5  , 0.5)
v3 = AddVertex (surf,  -1, -1, 0,  0  , 0.5)



That's all it does!

(I assume you're comfortable with U and V coordinates and that they are a float between 0, the top/left, and 1, the maximum extreme to the bottom/right, yes?)


.rIKmAN.(Posted 2012) [#3]
Yasha - THANK YOU!

I did have 4 VertexTexCoords commands but somehow chopped them out when re-arranging the code so it didn't need the texture atlas I was using and would work with just code.

I had totally mixed it all up in my head because I had been messing around for so long with it and just couldn't seem to get the coords right.

*bangs head off table repeatedly*

I wouldn't say I am "comfortable" with UVs, but yes I knew it was between 0-1 for the offset in the atlas, I just had a 2hr brain fart and couldn't seem to work it out!

Thanks again, much appreciated :)


.rIKmAN.(Posted 2012) [#4]
Just another quick question.

If you don't create the vertices yourself with AddVertex, what is the best way to find the right value for the index required by VertexTexCoords and make sure you have correct ones relating to the face you intend to change? (ie CreateCube())

Naming them v0/v1/v2 etc is fine for my simple example, but if you then use AddMesh to try and create a single surface mesh from say 100 of them, finding the correct vertices/coords to texture with the relevant part of the texture atlas seems like it would be trial and error?

Iteration through the vertices wouldn't work as I still wouldn't know which was which, but I'm sure there might be some clever maths involved for finding the right part of the texture (texture and tile width/height etc)

Last edited 2012


Yasha(Posted 2012) [#5]
I'm not sure there is a general-purpose way of doing that. You can get the vertices for a particular triangle using TriangleVertex, but that still leaves you to 1) get the triangle somehow (pick?); and 2) work out which of the three triverts os the one you want to modify (which will depend on things like orientation vs. the camera).

This is one of those areas where "it depends what you want to do"; you'll need to establish a few mesh-handling conventions of your own, that reflect whatever it is you actually want to do with the mesh.


Side note with regard to the first question again: if you try to use every single pixel as part of your atlas, you may experience some "bleeding" as the texture filters surrounding regions into the bit you're actually using. You might want to investigate some combination of making your subtextures slightly smaller (e.g. a 1px border), or the Clamp UV texture flags, to prevent this side effect.


.rIKmAN.(Posted 2012) [#6]
Yeah I did find a few threads regarding the texture bleeding thing, so that is something I will keep in mind once I get the basics sorted.

With regards to what I'm trying to achieve, it's a simple Minecraft style world built with blocks though it won't be Minecraft gameplay - that's just the method I decided to use to build the world as I'm artistically retarded.

I have each block and it's associated data stored in type and thought it would be easy to reference this to find what I needed to know to allow texturing, placement, movement, deformation, triggers etc - but all this is lost when they are all AddMesh'ed into a single surface to get a decent FPS, which I didn't think of before I started coding.