That is my first time utilizing this web site and I am a newbie too, so sorry if what I am asking does not make sense.
I’m utilizing C++ and OpenGL to make a fundamental 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 area. Then I rework them into pixel by area by multiplying them by the tile’s measurement in pixels, uSize
. After having the coordinates in pixel area I rework them to NDC. Then the fragment shader is executed, however for some motive the fragment shader is utilized to each pixel on display. 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. The pixel sampling is working tremendous, 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):
And that is a picture of the end result.
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 coordinates
vec2 screenPos = vec2(vec2(ivec2(aPos)) * float(uSize));
// Convert from display coordinates to NDC [-1,1]
// Additionally account for side 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 area (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 scale 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 colour
return texture(atlas, uv).rgb;
}
void foremost() {
vec3 colour = sampleAtlasPixel(uAtlas, fTile_ID, uSize);
FragColor = vec4(colour, 1.0);
}
Thanks upfront to whoever solutions!