2

I'm not asking about how to map the texture itself. I'm just wondering how I could go about evenly mapping a texture to an object so that it repeats itself instead of stretching to the surface.

For example: A rectangle with the texture stretching to each corner, and a rectangle with the texture repeated without losing its original aspect ratio.

Thanks.

K.K. Slider
  • 103
  • 1
  • 5
  • 1
    make texture coordinates to be bigger than 1, for example 2 and calculate that by using the sideratio of the rectangle. – HenrikD Feb 15 '18 at 22:25
  • How are you creating this object and assigning textures to it? Is it made by hand in a 3D tool where you can manually adjust the UV coordinates? Or is it generated/modified at runtime in a way that your texture mapping needs to react to? – DMGregory Feb 15 '18 at 23:33
  • @DMGregory I'm setting all the data (position + uv) manually, in the code. – K.K. Slider Feb 15 '18 at 23:36

3 Answers3

1

If you know you're spawning a quad with width and height

and you want your texture to spawn its entire height exactly once

and you want the width to truncate/repeat accordingly to preserve the texture aspect ratio

then you want your UV x values to run from 0 to width/height

and your UV y values to run from 0 to 1

This way if width is twice as large as height, your texture will tile two times horizontally. And correspondingly for other sizes.

DMGregory
  • 134,153
  • 22
  • 242
  • 357
0

You want to set the wrapping mode:

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

You can also do

GL_CLAMP
GL_REPEAT
GL_MIRRORED_REPEAT
GL_CLAMP_TO_EDGE
GL_CLAMP_TO_BORDR
GL_MIRROR_CLAMP_TO_EDGE

See What does changing GL_TEXTURE_WRAP)_(S/T) do? for more info

CobaltHex
  • 2,238
  • 12
  • 18
  • I'm aware of the various wrap modes. My question was about how to set the texture coordinates in such a way that the image would retain its aspect ratio, instead of stretching to the shape of what I'm drawing. – K.K. Slider Feb 15 '18 at 23:01
  • I believe you would have to get the dimensions of the triangle being rendered (in 3d space) and then scale that with the dimensions of your texture – CobaltHex Feb 16 '18 at 21:19
  • https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexParameter.xhtml - the default is GL_REPEAT so it doesn't need to be explicitly set. – Maximus Minimus Apr 26 '18 at 18:42
0

First set the wrap mode:

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

Then you just need to make your texture coordinates bigger than 1.

Instead of putting 1, 1, you need to put 2, 2 and it will repeat itself twice. Good luck!

Joza100
  • 259
  • 1
  • 2
  • 9