1
votes

So I have an AtlasTexture that contains all the tiles I need to draw a tile map.

Right now I pass the AtlasTexture through a uniform, and the idea is to change the texture coordinate to select just the portion I need from the atlas.

The issue is that I can only specify on the fragment shader to cut the texture from the zero origin, is it possible to specify an offsetX to tell to the shader where I want to start drawing?

float vertices[] = {
    // aPosition     // aTextureCoordinate
    0.0f,   0.0f,    0.0f, 0.0f,
    100.0f, 0.0f,    1.0f, 0.0f,
    0.0f,   100.0f,  0.0f, 1.0f,
    100.0f, 100.0f,  1.0f, 1.0f,
};

uint32_t indices[] = {0, 1, 2, 2, 3, 1};

Vertex shader

#version 330 core

layout(location = 0) in vec2 aPosition;
layout(location = 1) in vec2 aTextureCoordinate;
out vec2 textureCoordinate;

void main() {
    gl_Position = vec4( aPosition.x, aPosition.y, 1.0f, 1.0f);
    textureCoordinate = vec2(
    aTextureCoordinate.x / 3.0f, // This selects the first tile in the uAtlasTexture
    aTextureCoordinate.y
    );
}

Fragment shader

#version 330 core

in vec2 textureCoordinate;
uniform sampler2D uAtlasTexture; // Has 3 tiles
out vec4 color;

void main() {
    color = texture(uAtlasTexture, textureCoordinate);
}
1
Use an Uniform variable for the offset.Rabbid76
but how I apply the offset? I can only select a portion of the texture from the leftellipticaldoor
vec2(1.0/3.0 + aTextureCoordinate.x / 3.0f, aTextureCoordinate.y); selects the 2nd tile. Use a uniform instead of 1.0/3.0Rabbid76
By the way. If all your tiles have the same size, you may find array textures (not to be confused with arrays of textures) more convenient.Andrea
thanks @Andrea , yes all my tiles are the same size. how I can use array textures?ellipticaldoor

1 Answers

0
votes

Use an Uniform variable for the offset. `vec2(1.0/3.0 + aTextureCoordinate.x / 3.0f, aTextureCoordinate.y); "selects2 the 2nd tile. Use a uniform instead of 1.0/3.0:

#version 330 core

layout(location = 0) in vec2 aPosition;
layout(location = 1) in vec2 aTextureCoordinate;
out vec2 textureCoordinate;

uniform float textureOffsetX;

void main() {
    gl_Position = vec4( aPosition.x, aPosition.y, 1.0f, 1.0f);
    textureCoordinate = vec2(
        textureOffsetX + aTextureCoordinate.x / 3.0f, 
        aTextureCoordinate.y
    );
}