FreshRSS

Zobrazení pro čtení

Jsou dostupné nové články, klikněte pro obnovení stránky.

Why does reading my depth texture in GLSL return less than one?

I've created a depth texture in OpenGL (using C#) as follows:

// Create the framebuffer.
var framebuffer = 0u;

glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);

// Create the depth texture.
var depthTexture = 0u;

glGenTextures(1, &depthTexture);
glBindTexture(GL_TEXTURE_2D, depthTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, 800, 600, 0, GL_DEPTH_COMPONENT, GL_FLOAT, null);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0);

Later, I sample from the depth texture as follows:

float depth = texture(depthTexture, texCoords).r;

But even when no geometry has been rendered to that pixel, the depth value coming back is less than 1 (seems to be very slightly above 0.5). This is confusing to me since, per the documentation on glClearDepth, the default value is 1. Note that this is not a problem of linearizing depth since I'm attemping to compare depth directly (using the same near and far planes), not convert that depth back to world space.

Why is my depth texture sample returning <1 when no geometry has been rendered?

Depth sorting issue

I've been working on a custom tile-based map editor for a while now, and everything works as expected, or so I thought until I've tried rendering the actual map including the dynamic objects as well.

As it currently works, it's saving data into a buffer in a format of: [ground layer] [on-ground layer] (flowers/rocks) [wall layer] [on-wall layer] (windows/torches/bookshelves)

So basically there's a total of 4 layers. The tiles are rendered from top left corner to bottom right corner in the following order: [ground layer] -> All tiles [on-ground layer] -> All tiles [wall layer] Does an instance exists on that spot? in case it does the instance gets rendered first, and then the tile itself. [on-wall layer] renders normally right after that.

This system seems perfectly fine it the game has a grid-based movement, as well as if all the dynamic sprites are the same size like the tile size. Why? Well, you'd just walk anywhere and get covered with a certain tile or be drawn over a certain tile. On the other hand, if any of the dynamic instances has let's say a sprite height which is bigger than the actual tile size the depth sorting issues appear.

Example: We've got a pillar which is 16x48 in size. The very bottom of the pillar has a collider of 16x16 and it can't be stepped on, but the mid and top part don't have any colliders. Now if a player for example steps on the topmost tile of the pillar everything gets rendered normally. On the other hand if the steps on the mid tile, the "player's head" would be rendered over the topmost tile..

Screenshot of tile map sorting problem

I'm wondering if there's an actual solution for depth ordering in a case like this.

❌