Reputation: 1288
Let's say I have an untextured plane made of 4 vertices. The UV coordinates of the vertices are the following:
(0,0)------------------(1,0)
| |
| |
| |
| |
(0,1)------------------(1,1)
I also have a texture and I want this texture to occupy X% of horizontal space on the plane and Y% of vertical space. How can I calculate the uv coordinate in the vertex shader to allow these arbitrary numbers?
I understand that if I want the texture to be (50%,50%) of the plane I need to make uvCoords * 2f
, and if I want (200%,200%) then I'll need uvCoords / 2f
, but how to do that for any number?
Upvotes: 1
Views: 3239
Reputation: 43319
You can always scale your coordinates this way:
uvCoords / vec2 (X/100.0, Y/100.0)
This takes X and Y in terms of percentage and then divides the individual components of your uvCoords
by the fractional representation for X and Y.
However, it is not clear exactly what version of OpenGL you are using here, whether it is the fixed-function pipeline or GLSL. If it is fixed-function, there is another thing you can consider called the texture matrix. You can use glScalef (...)
to setup a scaling matrix while the matrix mode is GL_TEXTURE
rather than performing this calculation when you setup your vertices. Just be sure to reset that matrix or you will scale everything's texture coordinates.
User texture matrices can also be used to transform texture coordinates in GLSL, though are probably more tedious to setup than doing what I suggested originally. If you wanted to rotate or translate these coordinates in addition to scaling, then I might suggest using a texture matrix.
Upvotes: 1