Question

I have a program in which users can create boxes with different widths and heights.

I also have a texture (512 x 512 Pixel) which should lay on every box.

Now I have the problem that on 90x90 Box (Mesh.Box) i can lay the texture 4x4 times.

So I thought that i could use the ratio 4/90 so texture the boxes. But that does not workenter image description here

The box has a width by 28.723 and a height by 56.43661

I use the following code to calculate the texture coords:

float tex_coords_w = 4.0f / 90;
float tex_coords_h = 4.0f / 90;

mesh = Mesh.Box(d3dDevice, width, height, 0);

        Mesh tempMesh = mesh.Clone(mesh.Options.Value, VertexFormats.PositionNormal | VertexFormats.Texture1, d3dDevice);
        CustomVertex.PositionNormalTextured[] vertData = (CustomVertex.PositionNormalTextured[])tempMesh.VertexBuffer.Lock(0, typeof(CustomVertex.PositionNormalTextured), LockFlags.None, tempMesh.NumberVertices);
        for (int i = vertData.Length / 2; i < vertData.Length; i += 4)
        {
            vertData[i + 1].Tu = Convert.ToSingle(i) + 0.0f;
            vertData[i + 1].Tv = Convert.ToSingle(i) + 0.0f;
            vertData[i + 2].Tu = Convert.ToSingle(i) + tex_coords_w * width;
            vertData[i + 2].Tv = Convert.ToSingle(i) + 0.0f;
            vertData[i + 3].Tu = Convert.ToSingle(i) + tex_coords_w * width;
            vertData[i + 3].Tv = Convert.ToSingle(i) + tex_coords_h * height;
            vertData[i + 0].Tu = Convert.ToSingle(i) + 0.0f;
            vertData[i + 0].Tv = Convert.ToSingle(i) + tex_coords_h * height;
        }
        tempMesh.VertexBuffer.Unlock();



        mesh.Dispose();
        mesh = tempMesh;

Can someone help me please?!

Was it helpful?

Solution

If you want to repeat the texture n times on each face.

  1. Set the texture addressing mode to the WRAP mode

  2. Set the texture coordinates as below for each face of the box.

        A(0, 0)                    B(n, 0)
               -------------------
               |                 |
               |                 |
               |                 |
               |                 |
               |                 |
               |                 |
               |                 |
        D(0, n) ------------------- C(n, n)
    

Here is the code example, I am using native DirectX, take vertex B for example, it's position is (5.0, 5.0, 0), and the texture coordinate is(0, 4.0f), so the texture repeat 4 times across u direction.

// Vertex layout
struct Vertex
{
    float x, y, z ; // Position
    float u, v ;    // Texture coordinates
};

Vertex FrontFace[] = 
{
    {-5.0f,  5.0f, 0,    0,    0},  // Vertex A
    { 5.0f,  5.0f, 0, 4.0f,    0},  // B
    { 5.0f, -5.0f, 0, 4.0f, 4.0f},  // C
    { 5.0f, -5.0f, 0, 4.0f,    0},  // D
} ;

Typically, the u- and v-texture coordinates that you assign to a vertex are in the range of 0.0 to 1.0 inclusive. However, by assigning texture coordinates outside that range, you can create certain special texturing effects.

see Texture Addressing Mode for details. this is for native DirectX, but you can simply apply it in C#.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top