3

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?

Makogan
  • 1,706
  • 12
  • 28

1 Answers1

2

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).

Calvin
  • 506
  • 2
  • 7