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 as a substitute of the rasterized geometry. I’m making an attempt 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 remodel them into pixel by house by multiplying them by the tile’s measurement in pixels, uSize
. After having the coordinates in pixel house I remodel them to NDC. Then the fragment shader is executed, however for some cause the fragment shader is utilized to each pixel on display. I feel it could be linked to my use of gl_FragCoord.xy
within the fragment shader. This results in the picture tiling throughout the display. 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 have been used for debugging):
And that is a picture of the end result.
The vertex shader:
#model 330 core
format(location = 0) in uvec2 aPos;
format(location = 1) in uint aBlock_id;
flat out uint fTile_ID;
uniform uint uSize;
uniform vec2 uScreenSize;
uniform uvec2 uPlayerCoords;
void principal() {
fTile_ID = aBlock_id;
// Add perspective
ivec2 perspectiveCoords = ivec2(aPos) - ivec2(uPlayerCoords);
// Convert from tile coordinates to display coordinates
vec2 screenPos = vec2(vec2(ivec2(aPos)) * float(uSize));
// Convert from display 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 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); // Degree 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 coloration
return texture(atlas, uv).rgb;
}
void principal() {
vec3 coloration = sampleAtlasPixel(uAtlas, fTile_ID, uSize);
FragColor = vec4(coloration, 1.0);
}
Thanks upfront to whoever solutions!