I am trying to to take a given geometry and through multiple render passes, render to a 3D texture in such a way that I can construct a voxelization of my scene.
So far the way I am attempting to render to the texture is:
Creating and loading a texture:
Voxel_Map::Voxel_Map(float width, float depth, float height)
{
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_3D, textureID);
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, width, height, depth, 0, GL_RGBA,
GL_INT, NULL);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Clean up
glBindTexture(GL_TEXTURE_3D, 0);
}
void Voxel_Map::load_to_GPU(GLuint program)
{
glUseProgram(program);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_3D, textureID);
GLint loc = glGetUniformLocation(program, "voxel_map");
if(loc == GL_INVALID_VALUE || loc==GL_INVALID_OPERATION)
{
cerr << "Error returned when trying to find texture uniform."
<< "
uniform: vocel_map"
<< "Error num: " << loc
<< endl;
return;
}
glUniform1i(loc,1);
}
Rendering to the texture:
current_program = shading_programs[SHADER_VOXELIZER].programID;
glUseProgram(current_program);
glViewport(0, 0, 7*16, 7*16);
glBindFramebuffer(GL_FRAMEBUFFER, FBOs[FBO_TEXTURE]);
glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D,
vMap->textureID, 0, 0);
glClearColor(0, 0.f, 0.f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw();
glViewport(0, 0, cam->getWidth(), cam->getHeight());
glBindFramebuffer(GL_FRAMEBUFFER, FBOs[FBO_DEFAULT]);
current_program = shading_programs[SHADER_3D].programID;
glUseProgram(current_program);
vMap->load_to_GPU(current_program);
draw();
openGLerror();
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);
}
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);
}
However when I try to sample the 3D texture through:
outColor = texture(voxel_map, vertexPos*vec3(1.f/(7*16), 1.f/(7*16), 1.f/(4*16)));
In the final shader, I get black instead of green, which seems to indicate I am either drawing or sampling from the texture in a wrong way.
What is the issue?
Alternatively if anyone has access to a tutorial series on 3D textures that would work too.
Or worst case scneario if soemone could explain to me how to use renderdoc to see if the texture is getting rendered through properly that would be good enough as well.