Question

I would like to be able to dynamically repeat textures based on the scale size of a object (cube).

I have tried going through VerticesCube3D structure but get a crash when trying to change the values. I have my textures setup on repeat but currently it stretches the texture (I need to dynamically change TEX_COORD_MAX)

 Vertex VerticesCube3D[] = {
        // Front
        {{1, -1, 0}, {1, 1, 1, 1}, {TEX_COORD_MAX, 0}},
        {{1, 1, 0}, {1, 1, 1, 1}, {TEX_COORD_MAX, TEX_COORD_MAX}},
        {{-1, 1, 0}, {1, 1, 1, 1}, {0, TEX_COORD_MAX}},
        {{-1, -1, 0}, {1, 1, 1, 1}, {0, 0}},
        // Back
        {{1, 1, -2}, {1, 1, 1, 1}, {TEX_COORD_MAX, 0}},
        {{-1, -1, -2}, {1, 1, 1, 1}, {TEX_COORD_MAX, TEX_COORD_MAX}},
        {{1, -1, -2}, {1, 1, 1, 1}, {0, TEX_COORD_MAX}},
        {{-1, 1, -2}, {1, 1, 1, 1}, {0, 0}},
        // Left
        {{-1, -1, 0}, {1, 1, 1, 1}, {TEX_COORD_MAX, 0}},
        {{-1, 1, 0}, {1, 1, 1, 1}, {TEX_COORD_MAX, TEX_COORD_MAX}},
        {{-1, 1, -2}, {1, 1, 1, 1}, {0, TEX_COORD_MAX}},
        {{-1, -1, -2}, {1, 1, 1, 1}, {0, 0}},
        // Right
        {{1, -1, -2}, {1, 1, 1, 1}, {TEX_COORD_MAX, 0}},
        {{1, 1, -2}, {1, 1, 1, 1}, {TEX_COORD_MAX, TEX_COORD_MAX}},
        {{1, 1, 0}, {1, 1, 1, 1}, {0, TEX_COORD_MAX}},
        {{1, -1, 0}, {1, 1, 1, 1}, {0, 0}},
        // Top
        {{1, 1, 0}, {1, 1, 1, 1}, {TEX_COORD_MAX, 0}},
        {{1, 1, -2}, {1, 1, 1, 1}, {TEX_COORD_MAX, TEX_COORD_MAX}},
        {{-1, 1, -2}, {1, 1, 1, 1}, {0, TEX_COORD_MAX}},
        {{-1, 1, 0}, {1, 1, 1, 1}, {0, 0}},
        // Bottom
        {{1, -1, -2}, {1, 1, 1, 1}, {TEX_COORD_MAX, 0}},
        {{1, -1, 0}, {1, 1, 1, 1}, {TEX_COORD_MAX, TEX_COORD_MAX}},
        {{-1, -1, 0}, {1, 1, 1, 1}, {0, TEX_COORD_MAX}},
        {{-1, -1, -2}, {1, 1, 1, 1}, {0, 0}}
    };
Was it helpful?

Solution

You don't want to change the texture coordinates in your vertex data (because then you'll have to re-upload it to the hardware). Instead, you want to transform the texture coordinates on the GPU, probably in your vertex shader.

Read about Projective Texture Mapping. This is a pretty common technique, but surprisingly I can't find much in the way of good tutorials about it. Here's a university programming assignment that discusses it a bit.

The gist of it: you define a texture matrix to transform texture coordinate -- this is a transformation just like your model, view, and projection matrices, so on iOS you can create it with GLKMatrix4 functions just like those. Then provide that matrix to your vertex shader using a uniform variable. In the vertex shader, multiply the input texture coordinate (a vertex attribute) by the texture matrix to transform it before outputting it to a varying for use in the fragment shader.

If you define texture coordinates in your vertex data such that a texture covers an object exactly once, you can then use a texture matrix to scale the texture down. If it's scaled down, and your texture wrap parameters are set to repeat, your texture will tile across the face of the object.

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