<p>A fragment shader executes for a 2D rasterized position. When you output the normalized position it's still being projected onto the 2D xy image plane onto a render target. The render target is 2D, so you can't use the fragment shader output to achieve this.</p>

<p>You need to bind a 3D texture as a read/write texture in order to store values. </p>

<p>The other thing is how you're expecting to voxelize the geometry, are you trying to voxelize the surfaces? If so you can use a pixel shader but you need to render all faces (disable back face culling).</p>

CC BY-SA 4.0
2018-12-02T04:55:26.397Z
2018-12-02T04:55:26.397Z
Calvin
<p>I am trying to fetch a given geometry and, through (potentially multiple) render passes, create a 3D texture that stores the color value of said geometry into an associated texel.</p>

<p>In other words there is a 3D box that i am trying to color based on the geometry inside of that box.</p>

<p>Currently I am attempting to do so with the following vertex shader:</p>

<pre><code>#version 450

#define PI 3.1415926535897932384626433832795

layout(location = 0) in vec3 position; //(x,y,z) coordinates of a vertex
layout(location = 1) in vec3 norm; //a 3D vertex representing the normal to the vertex 
layout(location = 2) in vec2 texture_coordinate; // texture coordinates

layout(std430, binding = 3) buffer instance_buffer
{
    vec4 cubes_info[];//first 3 values are position of object 
};

out vec3 normalized_pos;
out vec3 normal; 
out vec2 texture_coord;

uniform float width = 128;
uniform float depth = 128;
uniform float height = 128;

uniform float voxel_size = 1;
uniform vec3 origin = vec3(0,0,0);

uniform float level=0;

void main()
{
    texture_coord = texture_coordinate; 
    vec4 pos = (vec4(position, 1.0) + vec4(vec3(cubes_info[gl_InstanceID]),0));

    pos-=vec4(origin, 0);

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

    gl_Position = pos;

    normalized_pos = vec3(pos);

    normal = normalize(norm);
}
</code></pre>

<p>And the following fragment shader:</p>

<pre><code>#version 450

in vec3 normalized_pos;
in vec3 normal;//Normal to the vertex
in vec2 texture_coord;

out vec4 outColor;//Final color of the pixel

uniform sampler2D text;

void main()
{
    outColor = vec4(0,1,0,1);
}
</code></pre>

<p>Currently the color is ahrd set to green, just to test whether I am doing things in the correct fashion.</p>

<p>But I seem to have failed.</p>

<p>How exactly would you design a shading program that voxelizes a 3D grid of a space?</p>

CC BY-SA 4.0
2019-09-30T08:34:01.273Z
What leads you to say "But I seem to have failed."?
Tiago Peres
<opengl><shader><voxelization>
How to voxelize geometry using textures and shaders?
Makogan
# How to voxelize geometry using textures and shaders?

(Asked by: username_0 on 2018-03-03)

I am trying to fetch a given geometry and, through (potentially multiple) render passes, create a 3D texture that stores the color value of said geometry into an associated texel.

In other words there is a 3D box that i am trying to color based on the geometry inside of that box.

Currently I am attempting to do so with the following vertex shader:

```
#version 450

#define PI 3.1415926535897932384626433832795

layout(location = 0) in vec3 position; //(x,y,z) coordinates of a vertex
layout(location = 1) in vec3 norm; //a 3D vertex representing the normal to the vertex 
layout(location = 2) in vec2 texture_coordinate; // texture coordinates

layout(std430, binding = 3) buffer instance_buffer
{
    vec4 cubes_info[];//first 3 values are position of object 
};

out vec3 normalized_pos;
out vec3 normal; 
out vec2 texture_coord;

uniform float width = 128;
uniform float depth = 128;
uniform float height = 128;

uniform float voxel_size = 1;
uniform vec3 origin = vec3(0,0,0);

uniform float level=0;

void main()
{
    texture_coord = texture_coordinate; 
    vec4 pos = (vec4(position, 1.0) + vec4(vec3(cubes_info[gl_InstanceID]),0));

    pos-=vec4(origin, 0);

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

    gl_Position = pos;

    normalized_pos = vec3(pos);

    normal = normalize(norm);
}

```

And the following fragment shader:

```
#version 450

in vec3 normalized_pos;
in vec3 normal;//Normal to the vertex
in vec2 texture_coord;

out vec4 outColor;//Final color of the pixel

uniform sampler2D text;

void main()
{
    outColor = vec4(0,1,0,1);
}

```

Currently the color is ahrd set to green, just to test whether I am doing things in the correct fashion.

But I seem to have failed.

How exactly would you design a shading program that voxelizes a 3D grid of a space?

**Tags:** opengl, shader, voxelization

> **Comments:**
> **username_1**: What leads you to say "But I seem to have failed."?  

---

## 1 answers

### Answer by username_2 on 2018-12-02. Score: 2 | Accepted answer

A fragment shader executes for a 2D rasterized position. When you output the normalized position it's still being projected onto the 2D xy image plane onto a render target. The render target is 2D, so you can't use the fragment shader output to achieve this.

You need to bind a 3D texture as a read/write texture in order to store values.

The other thing is how you're expecting to voxelize the geometry, are you trying to voxelize the surfaces? If so you can use a pixel shader but you need to render all faces (disable back face culling).


---

