Texturing a mesh

Blitz3D Forums/Blitz3D Programming/Texturing a mesh

SoggyP(Posted 2004) [#1]
Hi Folks,

A nice simple question, I suspect.

I've created a shape - in this case an elongated cube - from scratch, ie, AddVertex, AddTriangle, etc, and I want to texture it with a basic wall like texture(possibly a different texture on top of the wall?). Can someone show me the sequence of commands needed to do this? I have no problem with primitives but my own meshes are causing me problems.

I suspect this has to do with UVs, right?

Anyway help much appreciated.

Later,

Jes


Rob Farley(Posted 2004) [#2]
You set the UV when you create the vertex.

A U or V co-ord goes from 0 to 1. Zero being the left hand side or top and 1 being the right hand side or bottom depending on if you're dealing with the U or V respectivly.


SoggyP(Posted 2004) [#3]
Hi Folks,

Thanks Rob, but that's not helped much. I understand what you've said but that still hasn't helped me put the texture onto my slab.

Later,

Jes


big10p(Posted 2004) [#4]
Well, if you've got your UV's set correctly, you just need to:

tex = LoadTexture("wall_texture.bmp") ;or whatever name it is.
EntityTexture my_mesh,tex


Or am I missing something? :)


WolRon(Posted 2004) [#5]
You need to decide how you want your texture to wrap around your mesh before you assign the U,V coordinates.

For example, if you only want the 'front' side of the cube to be textured, then you could just assign the 'front' vertices values of
0,0
0,1
1,0
and 1,1.
All other vertices could just remain at 0,0. Then you could apply a texture with an image that is basically square and only the 'front' of the cube would be covered.


But if you wanted to texture every side of the cube with a different image then you would have to assign the vertices U,V coordinates something like:
.333,0 .666,0
0,.25 .333,.25 .666,.25 1,.25
0,.5 .333,.5 .666,.5 1,.5
.333,.75 .666,.75
.333,1 .666,1
and then create a texture with an image that is arranged something like an unfolded box
.1.
234
.5.
.6.
The 1,2,3,4,5,6 represent the different parts of your image that will cover different sides of the cube. The . represent empty areas of the image.
(sorry this couldn't be more clear without images)

Note that this is just one way to do it.

Edit: Putting more thought into this, I realize that the example I showed above actually would use more than 4 vertices. It would require 14 vertices.

I'm sure it can be done using only 4 vertices but maybe this will give you an idea how to go about it.


SoggyP(Posted 2004) [#6]
Hi Folks,

Ok, think I've got it, I'll give it a go.

Later,

Jes


SoggyP(Posted 2004) [#7]
Hi Folks,

Ok, here is the code I'm using at the moment. This builds a cube using vertices and triangles and then applies a texture to it. Simple so far. However, the texture goes well odd across different parts of the faces.



Whoops, premature posting ;0)

So, the question is, do I have to create multiple vertices at the same location to correctly map the texture to? I don't think that very likely but...? How else can I map the texture presented(a square texture) on each face?

Thanks,

Jes


AntonyWells(Posted 2004) [#8]
No need to create multiple verts, you share verts across a face normally.

Just consider each face a flat plane, as that's all it is. And each vert has a uv range of 0 to 1. (Think of this as a percentage of the texture's width/height)

e.g, u=0.5,v=0.5 = Centre of texture.

Now for each face, considering it's just a flat plane, just consider it on a whole, rather than a series of polys.

So for the front face. Two triangles.

Top Left corner(On tri 1) would be 0,0. Top Right Corner(Shared between tri 1 and 2) would be 1,0

Bottom left corner(Shared by tri 1 and 2) would be 0,1

And finally bottom right corner(tri 2) would be 1,1

Now just repeat the same uvs for each face, because they're just flat planes and rotation doesn't affect texture mapping. It can warp it in blitz, in some cases if you're not using even surfaces though.

Also if you want to scale a texture down so it appears smaller yet still tiles across the surface, still use the 0,1 range, and use TextureScale with a number within 0 to 1. and it'll tile it across the face.


SoggyP(Posted 2004) [#9]
Hi Folks,

Yep, I've got that, thanks. The problem are those faces that share verts, so for example, the front face is going to share it's top left vertex with the bottom left vertex of the top face. Where the vertices don't 'clash' like that it's no problem, only where there are multiple faces. Am I making sense or talking baps?

As an example, a di. I have facing me number 3, the top face is number 1. The top left vertex is going to be the top left of the 3 image and at the same time is going to be the bottom left of the 1 image. Does that help or are my baps floured but undercooked?

Later,

Jes


SoggyP(Posted 2004) [#10]
Hi Folks,

Can anyone help me with this?

Later,

Jes