How I can draw a tile from a texture tilemap in an OpenGL shader?

ellipticaldoor

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);
}
Rabbid76

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
    );
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How Can I Prevent Texture Bleeding When Using drawImage To Draw Multiple Images From A Tile Sheet, Sprite Sheet, Or Texture Atlas?

OpenGL : How can I pass multiple texture to a shader with one variable?

How can I get the x,y coordinate location of a tile in the tilemap?

Libgdx: Draw a texture with tilemap

How do I pass texture data to this openGL vertex shader from an Objective C client?

How do I scale the specific tile on the tilemap?

How to select a tile from multi tilemap?

How can I load bufferedImage as opengl texture?

How to correctly draw other texture in fragment shader?

How can I map a texture image onto a shader geometry?

In SceneKit, how can one tile a texture on differently sized objects while keeping draw calls minimal?

Passing texture to shader in OpenGL

Rendering texture in OpenGL shader

How to draw texture circle opengl-es?

How I can create a texture atlas in OpenGL with other existing textures?

How can I texture with vertex position coordinates? openGL,c++

How can i fix this texture rendering issue in this basic OpenGL demo?

Tunnel shader (converted from WebGL to OpenGL) - texture coords do not match?

How to stop a Shader from distorting a texture

OpenGL texture black in fragment shader

Compute Shader OpenGL Write to Texture

Why can't I draw a texture in pyOpenGL

Can I skip implementing tessellation shader in OpenGL?

How can I use a compute shader to calculate values and store them in a 3D texture?

How can I keep the main texture of the character when adding a new shader?

How can I make OpenGL draw something using VBOs in XCODE?

How to change collider in individual tile in tilemap

OpenGL - How to draw to a multisample framebuffer and then use the result as a normal texture?

OpenGL: Framebuffer - draw to texture - glClearColor