Sunday, May 4, 2025
HomeGame DevelopmentMy fragment shader is being run on each pixel of the display...

My fragment shader is being run on each pixel of the display screen


That is my first time utilizing this web site and I am a newbie too, so sorry if what I am asking would not make sense.

I’m utilizing C++ and OpenGL to make a primary platformer.
My subject is that the fragment shader is being run on each pixel of the display screen as a substitute of the rasterized geometry. I’m attempting to make a platformer with tiles. I’ve a tile that’s being rendered. At first the tile coordinates are in what I name tile house. Then I rework them into pixel by house by multiplying them by the tile’s measurement in pixels, uSize. After having the coordinates in pixel house I rework them to NDC. Then the fragment shader is executed, however for some cause the fragment shader is utilized to each pixel on display screen. I believe it might be linked to my use of gl_FragCoord.xy within the fragment shader. This results in the picture tiling throughout the display screen. The pixel sampling is working high-quality, as the feel atlas is an array of uSize by uSize textures I simply want to determine what the tile’s starting coordinates within the atlas could be then add the coordinates of the place we’re within the tile.

That is the feel atlas (the bizarre pixels had been used for debugging):

My fragment shader is being run on each pixel of the display screen

And that is a picture of the end result.
enter image description here

The vertex shader:

#model 330 core
structure(location = 0) in uvec2 aPos;
structure(location = 1) in uint aBlock_id;

flat out uint fTile_ID;

uniform uint uSize;            
uniform vec2 uScreenSize;     
uniform uvec2 uPlayerCoords;

void foremost() {
    fTile_ID = aBlock_id;

    // Add perspective
    ivec2 perspectiveCoords = ivec2(aPos) - ivec2(uPlayerCoords);

    // Convert from tile coordinates to display screen coordinates
    vec2 screenPos = vec2(vec2(ivec2(aPos)) * float(uSize));

    // Convert from display screen coordinates to NDC [-1,1]
    // Additionally account for facet ratio
    vec2 ndcPos = vec2(
       (screenPos.x / uScreenSize.x) * 2.0 - 1.0,
       (screenPos.y / uScreenSize.y) * 2.0 - 1.0
    );

    gl_Position = vec4(ndcPos, 0.0, 1.0);
}

That is the fragment shader:

#model 330 core

out vec4 FragColor;

uniform uint uSize;   
uniform sampler2D uAtlas;  // Texture atlas containing tiles
flat in uint fTile_ID;      // Tile ID for the present fragment

vec3 sampleAtlasPixel(sampler2D atlas, uint tileID, uint tileSize) {
    // Fragment place in display screen house (in pixels)
    ivec2 fragCoord = ivec2(gl_FragCoord.xy); 

    // the place we're within the tile vary 0:tileSize
    ivec2 tilePos = fragCoord % ivec2(tileSize); 
    // Because the atlas is an array begin y will at all times be 0 
    ivec2 tileStartPos = ivec2(tileID * tileSize, 0);

    ivec2 samplePos = tileStartPos + tilePos;

    // Get the dimensions of the atlas utilizing textureSize
    ivec2 atlasSize = textureSize(atlas, 0); // Stage 0 for the bottom mipmap degree

    // Convert the pixel place to normalised texture coordinates.
    vec2 uv = vec2(samplePos) / vec2(atlasSize);

    // Pattern and return the atlas shade
    return texture(atlas, uv).rgb;
}

void foremost() {
    vec3 shade = sampleAtlasPixel(uAtlas, fTile_ID, uSize);
    FragColor = vec4(shade, 1.0);
}

Thanks upfront to whoever solutions!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments