2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ make texture coordinates to be bigger than 1, for example 2 and calculate that by using the sideratio of the rectangle. \$\endgroup\$ – HenrikD Feb 15 '18 at 22:25
  • \$\begingroup\$ 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? \$\endgroup\$ – DMGregory Feb 15 '18 at 23:33
  • \$\begingroup\$ @DMGregory I'm setting all the data (position + uv) manually, in the code. \$\endgroup\$ – K.K. Slider Feb 15 '18 at 23:36
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – K.K. Slider Feb 15 '18 at 23:01
  • \$\begingroup\$ 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 \$\endgroup\$ – CobaltHex Feb 16 '18 at 21:19
  • \$\begingroup\$ khronos.org/registry/OpenGL-Refpages/gl4/html/… - the default is GL_REPEAT so it doesn't need to be explicitly set. \$\endgroup\$ – Maximus Minimus Apr 26 '18 at 18:42
0
\$\begingroup\$

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!

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.