I am trying to calculate texture coordinates based on the coordinates of an incoming vertex in Vertex Shader. This is a stripped down version of my attempt:
varying vec4 color;
uniform sampler2D heightmap;
uniform ivec2 heightmapSize;
void main(void)
{
vec2 fHeightmapSize = vec2(heightmapSize);
vec2 pos = gl_Vertex.zx + vec2(0.5f, 0.5f);
vec2 offset = floor(fHeightmapSize * pos) + vec2(0.5f, 0.5f);
if (fract(offset.x) > 0.45f && fract(offset.x) < 0.55f
&& fract(offset.y) > 0.45f && fract(offset.y) < 0.55f)
color = vec4(0.0f, 1.0f, 0.0f, 1.0f);
else
color = vec4(1.0f, 0.0f, 0.0f, 1.0f);
// gl_Position = ...
// ...
}
gl_Vertex is in [-0.5, 0.5]^2, on the XZ plane. So what I'm basically trying to do is
- first create a float vec2 from heightmapSize ivec2 that contains the width and height of the sample height of the map.
- Then I convert the coordinates of the vertices to an interval
[0, 1]^2. Then I calculate the offset in the texture coordinates, multiplying the vertex position by the height size. The left side (using floor()) should return the number of texels in each direction.
: Texure 4x4, (0.55, 0.5). , 2 , → (2.0, 2.0).
(0.5, 0.5), . (2.0, 2.0) (2.5, 2.5). :, , 0.5 .
. , . "" 0.5, , . .

, fract() ( - ), vec2(0.5, 0.5) ? - ?
slash source
share