Files
CDAG/Research/core/Voxelizer/TriangleMeshVoxelizer.h

154 lines
5.7 KiB
C++

#pragma once
#ifndef GLM_FORCE_RADIANS
#define GLM_FORCE_RADIANS
#endif
#include <map>
#include <vector>
#include <unordered_map>
#include <stdio.h>
#include "../../inc/glm/glm.hpp"
#include "../../inc/gl/glew.h"
#include "../../inc/gl/glfw3.h"
#include "../../inc/glm/gtc/matrix_transform.hpp"
#include "../../core/Defines.h"
#include "../../scene/Scene.h"
#include "BaseVoxelizer.h"
#include "../Hashers.h"
class ObjLoader;
class ShaderLoader;
class PropertyLoader;
struct VoxelInfo;
enum Direction
{
Top = 0, Side = 1, Front = 2
};
class TriangleMeshVoxelizer : public BaseVoxelizer
{
public:
TriangleMeshVoxelizer();
virtual ~TriangleMeshVoxelizer() override;
bool Initialize() override;
bool LoadScene(const std::string& sceneFileName) override;
bool UnloadScene() override;
std::vector<glm::uvec3> GetValidCoords(unsigned8 scale) override;
void Voxelize(unsigned8 scale, unsigned8 partScale, glm::uvec3 partCoord, const std::function<void(const VoxelInfo&, bool best)>& nodeAdder,
bool colors = true, bool normals = true, bool reflectivity = true) override;
// Keep other voxelize overloads from the base
using BaseVoxelizer::Voxelize;
size_t Load2DTexture(std::string filename, bool interpolation);
// Settings:
// If render scene is set to true, the actual scene is rendered instead of the image used for depth peeling
bool renderScene;
// If manual is set to true, the L and Space buttons should be used to manually advance a layer each time.
bool manual;
// If verbose mode is turned on, some details on progress will be output
bool verbose;
// If "interpolate colors" is on, mipmaps will be used to interpolate the colors. This means that the final octree looks more smooth, but might be harder to compress
bool interpolateColors;
// Boolean indicating if the tree should be regenerated or loaded from cache if available.
bool useCache;
protected:
private:
bool Reinitialize(bool fullscreen);
bool InitializeGLFW();
bool textureLoaded(std::string filename);
std::string mMissingTexture;
std::map<std::string, unsigned> mTextures;
// Loads the scene data into GPU memory
void LoadScene(Scene& scene);
// Compiles and loads the shaders. Binds values
void ReloadShaders(const Scene& scene);
void LoadShaders(const Scene& scene);
// Calculates an axis-aligned bounding box around the scene. Stores results in sceneLeft, sceneRight, sceneTop, sceneBottom, sceneNear, sceneFar.
void CalculateBoundingBox(Scene& scene);
// Calculates the size and position of the root node based on the current bounding box
void CalculateRootNode();
// Calculates the coordinates of non-empty subtrees at the given level that need to be evaluated in order to build te tree
std::vector<glm::uvec3> CalculateValidSubtrees(unsigned8 level);
void InitDepthPeel();
void DepthPeel(glm::uvec3 coord, const std::function<void(const VoxelInfo&, bool best)>& nodeAdder);
void TerminateDepthPeel();
// Given the depth values in the scene, marks the corresponding voxels in the grid as true
void SetGridData(Direction dir, const std::function<void(const VoxelInfo&, bool best)>& nodeAdder);
void SetDirection(Direction dir, unsigned oldDepthTexture);
void Transform(Direction dir);
// Transforms a world coordinate to a coordinate in the 3D grid
glm::ivec3 WorldToGrid(glm::vec3 world);
void RenderScene(Direction dir, unsigned oldDepthTexture, float viewPortWidth, float viewPortHeight);
void RenderDebugImage(GLuint curDepthTexture);
// GLFW window properties
unsigned mWidth, mHeight;
GLFWwindow* mWindow;
// Pointers to some singletons used in the application
ObjLoader* mObjLoader;
ShaderLoader* mShaderLoader;
PropertyLoader* mPropertyLoader;
GLuint mVertexArrayID, mVertexBuffer, mTextureBuffer, mVertexColorBuffer, mNormalBuffer, mElementBuffer, mDefaultProgramID;
GLuint mFramebufferName, mColorTexture, mNormalTexture, mDepthTexture1, mDepthTexture2, mAngleTexture, mReflectivityTexture, mDepthRenderbuffer;
GLuint mQuadVertexbuffer, mQuadProgram, mImageRenderDepthTexID, mImageRenderColorTexID, mImageRenderUseDepthID;
// Buffers that are filled using glReadPixels
std::vector<GLfloat>* depthData;
std::vector<GLubyte>* colorData;
std::vector<GLfloat>* normalData;
std::vector<GLfloat>* angleData;
std::vector<GLfloat>* reflectivityData;
// The scene
Scene mScene;
// Variables to store details about the size of the scene
float sceneLeft, sceneRight, sceneTop, sceneBottom, sceneNear, sceneFar;
// The center of the root node in world space (e.g. the center of the object)
glm::vec3 mRootCenter;
// The edge length of the root node
float mRootSize;
// Data used for calculating the current pass
bool mReadColors, mReadNormals, mReadReflectivity;
glm::vec3 mCurPassRootCenter;
float mCurPassRootSize;
unsigned mCurPassGridSize;
unsigned8 mCurPassTreeDepth;
// Variables to store progress for the current direction
unsigned startTexX, startTexY, texWidth, texHeight, colorTexWidth;
bool firstPass, pixelsLeft;
// Model, view, project matrices (calculated in Transform method)
glm::mat4 mModelMatrix, mViewMatrix, mProjectionMatrix, mMVP;
// OpenGL bindings
GLuint mTextureSampler; // for binding the texture sampler
GLuint mReflectivity;
GLuint mOldDepthBufferSampler; // For binding the old depth map sampler
GLuint mFirstPass; // For binding the bool detailing if this is the first pass
GLuint mDepthmargin; // For binding the float detailing the gridsize
GLuint mCameraDir; // For binding the camera direction (used for calculating angles)
bool mStaticUniformsSet; // flag that static uniforms have been set
std::unordered_map<glm::uvec3, VoxelInfo> mMissingNodes;
};