<p>You need to set your texture coordinates appropriately. Let's look at one side of your cube. Let's say it's 100 x 100. If the user stretches the cube to be 100 x 150, you need to set the texture coordinates of the corner vertices to be:</p>

<pre><code>lower left = &lt;0.0, 0.0&gt;
lower right = &lt;1.5, 0.0&gt;
upper right = &lt;1.5, 1.0&gt;
upper left = &lt;0.0, 1.0&gt;
</code></pre>

<p>That's the easiest way to handle it. Another way to handle it would be to pass either as scale factor or scale matrix into your fragment shader and apply that to your texture coordinates before doing any sampling.</p>

<p>To elaborate on the texture matrix idea, it's fairly simple. In your fragment shader, you'd have a uniform, say <code>textureMatrix</code> that's a <code>mat4</code>. It can contain any sort of transform you need - scale, rotation, translation, shear, etc. Before you sample any texture, you run the uv coordinates through the texture matrix, and then use the resulting values for your lookup. Something like this:</p>

<pre><code>vec4 uv = gl_TexCoord[ 0 ]; // or whatever you called it coming from your vert. shader
vec4 newUV = textureMatrix * uv;
vec4 pixel = texture2D(texture, newUV.xy);
</code></pre>

<p>To do your non-uniform scaling, your matrix would be a scale matrix. For example if you wanted to scale the Y coordinate, you could use a matrix like this:</p>

<pre><code>| 1 0 0 0 |
| 0 2 0 0 |
| 0 0 0 0 |
| 0 0 0 1 |
</code></pre>

CC BY-SA 3.0
2017-03-06T07:55:50.450Z
But the scale factor is 3 dimensional and the tex coordinate is 2 dimensional.
Tudvari
CC BY-SA 3.0
2017-03-06T08:03:08.383Z
For example if the scaling factor is 1, 10, 1, then the texture coordinates of a face will be scaled based on which face we render currently. Like if we render the top quad it will be scaled x = 1 and z = 1. If we look at a side quad it will scaled by x = 1 and y = 10 or z = 1 and y = 10. How can I check which face are working on?
Or do I overcomplicate it?
Tudvari
CC BY-SA 3.0
2017-03-06T17:17:20.910Z
Without knowing more about your architecture and seeing more of your code, I don't have any way to answer that question. I would assume that you know which faces you're drawing when. You do need to know which face you're drawing and what the scale factors are in order to do this correctly.
user1118321
CC BY-SA 3.0
2017-03-07T13:59:25.173Z
@user1118321 Could you expand on the part about passing a scale matrix into the fragment shader? How do you use that to adjust U,V coords for non-uniform scaling? Is it possible?
Dan
CC BY-SA 3.0
2017-03-07T17:20:05.297Z
OK, I've added an example above.
user1118321
CC BY-SA 3.0
2017-03-08T09:38:08.610Z
I couldn't figure out how could I know which face I am drawing. The drawing part of my architecture is simple: Load the vertices, normals, indices, uv-s from an .obj file, then just push it to the shader. The vertex shader will multiple it with MVP, then the fragment shader samples from teh texture based on the UV coord. Somehow I have to get that which face looks towards which axis before the MVP multiple. (because for example a "rotate around Y axis 90 degree" would make this very hard, if not impossible)
Maybe I could something with the indices?
Tudvari
CC BY-SA 3.0
2017-03-08T16:52:12.200Z
Can you modify the .obj file? If so, you can group sets of polygons in meaningful ways using [the `g` command](https://en.wikipedia.org/wiki/Wavefront_.obj_file#Referencing_materials). This will allow you to make 2 triangles into a single cube face, for example. Then, when reading it in, you can know which face is which.
user1118321
CC BY-SA 3.0
2017-03-06T04:28:53.877Z
2017-03-07T17:19:56.697Z
2017-03-07T17:19:56.697Z
user1118321
<p>I have a simple <strong>cube</strong> mesh, which I <strong>scale</strong> with modifying the <strong>model</strong> matrix.</p>

<p><strong>Texture settings:</strong></p>

<pre><code>glGenTextures(1, &amp;m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
</code></pre>

<p>I set <code>GL_REPEAT</code> as wrap mode, but in my case it doesn't help, because the texture coordinates don't exceed <code>[0...1]</code>.</p>

<p><strong>For example</strong> from the unit cube I want to make a column whose dimensions are (1,10,1).</p>

<p>Just scaling this doesn't work because it textures the cube and then applies the scaling, which means that the texture will be stretched, instead of repeated along the y axis.</p>

<p><strong>How can I implement this so I could set the length, width and depth of a cube dynamically and the texture will be repeated on it instead of stretched?</strong></p>

<opengl><texture>
Stretch the cube mesh but repeat the texture?
Tudvari
# Stretch the cube mesh but repeat the texture?

(Asked by: username_0 on 2017-03-05)

I have a simple **cube** mesh, which I **scale** with modifying the **model** matrix.

**Texture settings:**

```
glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

```

I set `GL_REPEAT` as wrap mode, but in my case it doesn't help, because the texture coordinates don't exceed `[0...1]`.

**For example** from the unit cube I want to make a column whose dimensions are (1,10,1).

Just scaling this doesn't work because it textures the cube and then applies the scaling, which means that the texture will be stretched, instead of repeated along the y axis.

**How can I implement this so I could set the length, width and depth of a cube dynamically and the texture will be repeated on it instead of stretched?**

**Tags:** opengl, texture


---

## 1 answers

### Answer by username_1 on 2017-03-06. Score: 4

You need to set your texture coordinates appropriately. Let's look at one side of your cube. Let's say it's 100 x 100. If the user stretches the cube to be 100 x 150, you need to set the texture coordinates of the corner vertices to be:

```
lower left = <0.0, 0.0>
lower right = <1.5, 0.0>
upper right = <1.5, 1.0>
upper left = <0.0, 1.0>

```

That's the easiest way to handle it. Another way to handle it would be to pass either as scale factor or scale matrix into your fragment shader and apply that to your texture coordinates before doing any sampling.

To elaborate on the texture matrix idea, it's fairly simple. In your fragment shader, you'd have a uniform, say `textureMatrix` that's a `mat4`. It can contain any sort of transform you need - scale, rotation, translation, shear, etc. Before you sample any texture, you run the uv coordinates through the texture matrix, and then use the resulting values for your lookup. Something like this:

```
vec4 uv = gl_TexCoord[ 0 ]; // or whatever you called it coming from your vert. shader
vec4 newUV = textureMatrix * uv;
vec4 pixel = texture2D(texture, newUV.xy);

```

To do your non-uniform scaling, your matrix would be a scale matrix. For example if you wanted to scale the Y coordinate, you could use a matrix like this:

```
| 1 0 0 0 |
| 0 2 0 0 |
| 0 0 0 0 |
| 0 0 0 1 |

```

> **Comments:**
> **username_0**: But the scale factor is 3 dimensional and the tex coordinate is 2 dimensional.  
> **username_0**: For example if the scaling factor is 1, 10, 1, then the texture coordinates of a face will be scaled based on which face we render currently. Like if we render the top quad it will be scaled x = 1 and z = 1. If we look at a side quad it will scaled by x = 1 and y = 10 or z = 1 and y = 10. How can I check which face are working on? Or do I overcomplicate it?  
> **username_1**: Without knowing more about your architecture and seeing more of your code, I don't have any way to answer that question. I would assume that you know which faces you're drawing when. You do need to know which face you're drawing and what the scale factors are in order to do this correctly.  
> **username_2**: @username_1 Could you expand on the part about passing a scale matrix into the fragment shader? How do you use that to adjust U,V coords for non-uniform scaling? Is it possible?  
> **username_1**: OK, I've added an example above.  
> **username_0**: I couldn't figure out how could I know which face I am drawing. The drawing part of my architecture is simple: Load the vertices, normals, indices, uv-s from an .obj file, then just push it to the shader. The vertex shader will multiple it with MVP, then the fragment shader samples from teh texture based on the UV coord. Somehow I have to get that which face looks towards which axis before the MVP multiple. (because for example a "rotate around Y axis 90 degree" would make this very hard, if not impossible) Maybe I could something with the indices?  
> **username_1**: Can you modify the .obj file? If so, you can group sets of polygons in meaningful ways using [the `g` command](https://en.wikipedia.org/wiki/Wavefront_.obj_file#Referencing_materials). This will allow you to make 2 triangles into a single cube face, for example. Then, when reading it in, you can know which face is which.  

---

