-1

I am recently trying to voxelize 3D polygon meshes with fixed function opengl.

void VoxelizeWithOpenGL(const Mesh &in, uint32_t width, uint32_t height, 
    uint32_t depth, uint8_t *out)
{
    GLuint texture_id, fbo_id;

    glGenTextures(1, &texture_id);
    glEnable(GL_TEXTURE_3D);
    glBindTexture(GL_TEXTURE_3D, texture_id);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB8, width, height, depth, 0, GL_RGB, 
         GL_UNSIGNED_BYTE, (void *)0);

    glGenFramebuffers(1, &fbo_id);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo_id);
    glFramebufferTexture3D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, texture_id, 0, 0);
    GLenum drawBuffers[] = { GL_COLOR_ATTACHMENT0 };
    glDrawBuffers( 1, drawBuffers );

    GLenum status = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
    if (status != GL_FRAMEBUFFER_COMPLETE)
        throw std::runtime_error("Cannot create 3D framebuffer.");

    glViewport(0, 0, width, height);

    const AABB &aabb = FindAABB(in);
    const glm::mat4 &proj = glm::ortho(aabb.min_x, aabb.max_x, aabb.min_y, aabb.max_y,
        aabb.min_z, aabb.max_z);

    glMatrixMode(GL_PROJECTION);
    glLoadMatrixf(glm::value_ptr(proj));
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    //glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    /*glBegin(GL_TRIANGLES);
    for(const Face &c: in.faces)
    {
        const glm::vec3 &o=c.o.pos;
        const glm::vec3 &a=c.a.pos;
        const glm::vec3 &b=c.b.pos;

        glVertex3fv(glm::value_ptr(o));
        glVertex3fv(glm::value_ptr(a));
        glVertex3fv(glm::value_ptr(b));
    }
    glEnd();*/

    //Read from GPU to RAM
    glBindTexture(GL_TEXTURE_3D, texture_id);
    glGetTexImage(GL_TEXTURE_3D, 0, GL_RGB, GL_UNSIGNED_BYTE, out);

    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    glDeleteFramebuffers(1, &fbo_id);
    glDeleteTextures(1, &texture_id);
}

I am pretty sure FindAABB and mesh loader is correct, however it keeps giving me weird result, even if I comment the drawing part.(please ignore the random cube color)

enter image description here

Tim Hsu
  • 402
  • 5
  • 19

1 Answers1

1

What you are trying to do there, it doesn't work like that. When you bind a 3D texture as a FBO's color attachment, you're actually binding only a single layer of the texture. For this to work you also have to "cut away" all the parts of your mesh outside a certain depth span along that layer. However triangulated meshes are not solid, they're just an outline. so this does not really work by applying clip planes. You'll have to "solidify" your mesh. And that is easier done without OpenGL, by putting the mesh data into a spatial subdivision structure (like an octree). The mesh has to be orientable; if this constraint is met, then you can read out the voxels from the spatial subdivision structure directly.

If you really insist to do this using an image based method using OpenGL, the result of clipping your mesh by two clip planes will be a set of outlines; you can then apply a flood fill to "solidify" them, which is then what goes into a 3D texture.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • i see, so opengl doesnot work like that. but I can still achieve an hollow voxel object if i used self made software renderer right? – Tim Hsu Apr 08 '15 at 01:55