2

I am attempting to write and read from a 3D texture, but it seems my mapping is wrong. I have used Render doc to check the textures and they look ok.

A random layer of this voluemtric texture looks like:

enter image description here

So just some blue to denote absence and some green values to denote pressence.

The coordinates I calculate when I write to each layer are calculated in the vertex shader as:

pos.x = (2.f*pos.x-width+2)/(width-2);
pos.y = (2.f*pos.y-depth+2)/(depth-2);

pos.z -= level;
pos.z *= 1.f/voxel_size;

gl_Position = pos;

Since the texture itself looks ok it seems these coordinates are good to achieve my goal.

It's important to note that right now voxel_size is 1 and the scale of the texture is supposed to be 1 to 1 with the scene dimensions. In essence, each pixel in the texture represents a 1x1x1 voxel in the scene.

Next I attempt to fetch the texture values as follows:

vec3 pos = vertexPos;
pos.x = (2.f*pos.x-width+2)/(width-2);
pos.y = (2.f*pos.y-depth+2)/(depth-2);

pos.z *= 1.f/(4*16);

outColor = texture(voxel_map, pos);

Where vertexPos is the global vertex position in the scene. The z coordinate may be completely wrong however (i am not sure if I am supposed to normalize the depth component or not) but that is not the only issue. If you look at the final result:

enter image description here

There is a horizontal sclae component problem. Since each texel represents a voxel, the color of a cube should always be a fixed color. But as you can see I am getting multiple colors for a single cube on the top faces. So my horizontal scale is wrong.

What am i doing wrong when fetching the texels from the texture?

Makogan
  • 8,208
  • 7
  • 44
  • 112
  • 2
    If pos.x is the position of the voxel in the world and width is the total size of your voxel world, I would have thought to normalise it to (0 - 1) for the texture coord would be Xcoord = pos.x - (-width / 2) – Zebrafish Mar 04 '18 at 22:09
  • I thought the coordinates have to be from -1 to 1 – Makogan Mar 04 '18 at 22:26
  • 3
    Texture coordinates should be from (0 - 1), screen coordinates are from -1 to 1, yes. These are called NDC, (normalised device coordinates) in OpenGL. I'm not sure what you're trying to do though. – Zebrafish Mar 04 '18 at 22:31
  • I am trying to voxelize my scene. i.e I want to create a 3D texture that is a voxel approximation of the true geometry in the scene – Makogan Mar 04 '18 at 22:37
  • what if this is just Z-fighting do you change fragment depth according to z coordinate in screen space? if not then depth test will not work correctly. There are 2 ways to remedy this. 1. use ray casting like order of render or compute the real depth of each fragment and set it ... take a look at my [GLSL volumetric back ray tracer](https://stackoverflow.com/a/48092685/2521214) it might get you some ideas ... – Spektre Mar 05 '18 at 08:58
  • People who voted to close - I want to understand something, the category called "why isn't this code working" clearly says that "must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself" .And I can't see how this question doesn't align with that rule! Stop closing question for the sake of closing questions. That makes SO a nasty place to ask. This question is 100% alright and it is clear that the user made his best to depict the problem. – Michael IV Mar 05 '18 at 10:28
  • 1
    I didn't vote to close, but for me there is a lot of code missing. For example, it is unclear if "when I write to each layer" uses render-to-texture with layered rendering or imageLoad/Store or whatever else. But depending on the method the coordinate system might defer (imageLoad/Store: texel coordinates, layered rendering: normalized coordinates). – BDL Mar 05 '18 at 17:03
  • vertexPos is just the global, unmodified position of the vertex in the world. – Makogan Mar 06 '18 at 02:38

0 Answers0