2016-09-23 09:06:46 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-11-20 21:50:53 +01:00
|
|
|
#include "SimpleMath/SimpleMath.h"
|
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
#include <bgfx/bgfx.h>
|
|
|
|
|
|
|
|
#include "RenderUtils.h"
|
|
|
|
|
|
|
|
struct Entity;
|
|
|
|
|
|
|
|
struct InputState {
|
|
|
|
int32_t mousedX;
|
|
|
|
int32_t mousedY;
|
|
|
|
int32_t mouseX;
|
|
|
|
int32_t mouseY;
|
|
|
|
uint8_t mouseButton;
|
|
|
|
int32_t mouseScroll;
|
|
|
|
char key;
|
|
|
|
|
|
|
|
InputState() :
|
|
|
|
mouseX(0),
|
|
|
|
mouseY(0),
|
|
|
|
mouseButton(0),
|
|
|
|
mouseScroll(0),
|
|
|
|
key(0) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Camera {
|
2016-11-20 21:50:53 +01:00
|
|
|
SimpleMath::Vector3f eye;
|
|
|
|
SimpleMath::Vector3f poi;
|
|
|
|
SimpleMath::Vector3f up;
|
2016-09-23 09:06:46 +02:00
|
|
|
|
|
|
|
float near;
|
|
|
|
float far;
|
|
|
|
float fov;
|
|
|
|
bool orthographic;
|
|
|
|
float width;
|
|
|
|
float height;
|
|
|
|
|
|
|
|
float mtxProj[16];
|
|
|
|
float mtxView[16];
|
2016-10-21 22:07:23 +02:00
|
|
|
float mtxEnv[16];
|
2016-09-23 09:06:46 +02:00
|
|
|
|
|
|
|
Camera() :
|
|
|
|
eye {5.f, 4.f, 5.f},
|
|
|
|
poi {0.f, 2.f, 0.f},
|
|
|
|
up {0.f, 1.f, 0.f},
|
2016-10-21 22:07:23 +02:00
|
|
|
near (0.1f),
|
|
|
|
far (150.f),
|
2016-11-30 22:11:08 +01:00
|
|
|
fov (60.f),
|
2016-09-23 09:06:46 +02:00
|
|
|
orthographic (false),
|
|
|
|
width (-1.f),
|
|
|
|
height (-1.f),
|
|
|
|
|
|
|
|
mtxProj {
|
|
|
|
1.f, 0.f, 0.f, 0.f,
|
|
|
|
0.f, 1.f, 0.f, 0.f,
|
|
|
|
0.f, 0.f, 1.f, 0.f,
|
|
|
|
0.f, 0.f, 0.f, 1.f},
|
|
|
|
mtxView {
|
2016-10-21 22:07:23 +02:00
|
|
|
1.f, 0.f, 0.f, 0.f,
|
|
|
|
0.f, 1.f, 0.f, 0.f,
|
|
|
|
0.f, 0.f, 1.f, 0.f,
|
|
|
|
0.f, 0.f, 0.f, 1.f},
|
|
|
|
mtxEnv {
|
2016-09-23 09:06:46 +02:00
|
|
|
1.f, 0.f, 0.f, 0.f,
|
|
|
|
0.f, 1.f, 0.f, 0.f,
|
|
|
|
0.f, 0.f, 1.f, 0.f,
|
|
|
|
0.f, 0.f, 0.f, 1.f}
|
|
|
|
{}
|
|
|
|
|
|
|
|
void updateMatrices();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Light {
|
|
|
|
bgfx::UniformHandle u_shadowMap;
|
|
|
|
bgfx::UniformHandle u_shadowMapParams;
|
|
|
|
bgfx::UniformHandle u_lightPos;
|
|
|
|
bgfx::UniformHandle u_lightMtx;
|
|
|
|
|
|
|
|
bgfx::TextureHandle shadowMapTexture;
|
|
|
|
bgfx::FrameBufferHandle shadowMapFB;
|
2016-11-30 22:11:08 +01:00
|
|
|
float pos[4];
|
2016-09-23 09:06:46 +02:00
|
|
|
float dir[3];
|
|
|
|
|
|
|
|
float mtxView[16];
|
|
|
|
float mtxProj[16];
|
|
|
|
float mtxLight[16];
|
|
|
|
float mtxShadow[16];
|
|
|
|
|
|
|
|
float shadowMapBias;
|
|
|
|
uint16_t shadowMapSize;
|
|
|
|
|
|
|
|
bool enabled;
|
|
|
|
float near;
|
|
|
|
float far;
|
|
|
|
float area;
|
|
|
|
|
|
|
|
Light() :
|
|
|
|
u_shadowMap (BGFX_INVALID_HANDLE),
|
|
|
|
u_lightPos (BGFX_INVALID_HANDLE),
|
|
|
|
u_lightMtx (BGFX_INVALID_HANDLE),
|
|
|
|
shadowMapTexture (BGFX_INVALID_HANDLE),
|
|
|
|
shadowMapFB (BGFX_INVALID_HANDLE),
|
2016-11-30 22:11:08 +01:00
|
|
|
pos {0.f, 10.f, 10.f, 1.0f},
|
2016-09-23 09:06:46 +02:00
|
|
|
dir {-1.f, -1.f, -1.f},
|
|
|
|
mtxView {
|
|
|
|
1.f, 0.f, 0.f, 0.f,
|
|
|
|
0.f, 1.f, 0.f, 0.f,
|
|
|
|
0.f, 0.f, 1.f, 0.f,
|
|
|
|
0.f, 0.f, 0.f, 1.f
|
|
|
|
},
|
|
|
|
mtxProj {
|
|
|
|
1.f, 0.f, 0.f, 0.f,
|
|
|
|
0.f, 1.f, 0.f, 0.f,
|
|
|
|
0.f, 0.f, 1.f, 0.f,
|
|
|
|
0.f, 0.f, 0.f, 1.f
|
|
|
|
},
|
|
|
|
mtxShadow {
|
|
|
|
1.f, 0.f, 0.f, 0.f,
|
|
|
|
0.f, 1.f, 0.f, 0.f,
|
|
|
|
0.f, 0.f, 1.f, 0.f,
|
|
|
|
0.f, 0.f, 0.f, 1.f
|
|
|
|
},
|
2016-11-10 21:29:47 +01:00
|
|
|
shadowMapBias (0.004f),
|
|
|
|
shadowMapSize (2048),
|
|
|
|
near (0.1f),
|
2016-09-23 09:06:46 +02:00
|
|
|
far (100.f),
|
|
|
|
area (10.f),
|
|
|
|
enabled (false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Entity {
|
|
|
|
float transform[16];
|
|
|
|
float color[4];
|
|
|
|
bgfxutils::Mesh* mesh;
|
|
|
|
|
|
|
|
Entity() :
|
|
|
|
transform {
|
|
|
|
1.f, 0.f, 0.f, 0.f,
|
|
|
|
0.f, 1.f, 0.f, 0.f,
|
|
|
|
0.f, 0.f, 1.f, 0.f,
|
|
|
|
0.f, 0.0, 0.f, 1.f
|
|
|
|
},
|
|
|
|
color { 1.f, 1.f, 1.f, 1.f },
|
|
|
|
mesh (NULL) {};
|
|
|
|
};
|
|
|
|
|
2016-10-21 22:07:23 +02:00
|
|
|
struct LightProbe
|
|
|
|
{
|
|
|
|
enum Enum
|
|
|
|
{
|
|
|
|
Bolonga,
|
|
|
|
Kyoto,
|
|
|
|
|
|
|
|
Count
|
|
|
|
};
|
|
|
|
|
|
|
|
void load(const char* _name);
|
|
|
|
|
|
|
|
void destroy()
|
|
|
|
{
|
|
|
|
bgfx::destroyTexture(m_tex);
|
|
|
|
bgfx::destroyTexture(m_texIrr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bgfx::TextureHandle m_tex;
|
|
|
|
bgfx::TextureHandle m_texIrr;
|
|
|
|
};
|
|
|
|
|
2016-12-19 22:15:29 +01:00
|
|
|
struct Path {
|
|
|
|
std::vector<SimpleMath::Vector3f> points;
|
|
|
|
};
|
|
|
|
|
2016-11-27 22:02:17 +01:00
|
|
|
struct DebugCommand {
|
|
|
|
enum CommandType {
|
|
|
|
Line,
|
2016-11-28 21:29:19 +01:00
|
|
|
Axes,
|
2016-11-27 22:02:17 +01:00
|
|
|
Arrow
|
|
|
|
};
|
|
|
|
|
|
|
|
CommandType type;
|
2016-10-21 22:07:23 +02:00
|
|
|
|
2016-11-27 22:02:17 +01:00
|
|
|
SimpleMath::Vector3f from;
|
|
|
|
SimpleMath::Vector3f to;
|
|
|
|
SimpleMath::Vector3f color;
|
|
|
|
};
|
2016-10-21 22:07:23 +02:00
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
struct Renderer {
|
|
|
|
bool initialized;
|
|
|
|
bool drawDebug;
|
2016-12-17 23:04:50 +01:00
|
|
|
bool drawFloor = true;
|
2016-12-19 22:15:29 +01:00
|
|
|
bool drawSkybox = true;
|
2016-09-23 09:06:46 +02:00
|
|
|
uint32_t width;
|
|
|
|
uint32_t height;
|
|
|
|
|
|
|
|
bgfx::UniformHandle sceneDefaultTextureSampler;
|
|
|
|
bgfx::TextureHandle sceneDefaultTexture;
|
|
|
|
|
|
|
|
std::vector<bgfxutils::Mesh*> meshes;
|
|
|
|
typedef std::map<std::string, unsigned int> MeshIdMap;
|
|
|
|
MeshIdMap meshIdMap;
|
|
|
|
|
2016-10-21 22:07:23 +02:00
|
|
|
LightProbe mLightProbes[LightProbe::Count];
|
|
|
|
LightProbe::Enum mCurrentLightProbe;
|
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
std::vector<Entity*> entities;
|
|
|
|
|
|
|
|
std::vector<Camera> cameras;
|
|
|
|
std::vector<Light> lights;
|
2016-11-27 22:02:17 +01:00
|
|
|
std::vector<DebugCommand> debugCommands;
|
2016-09-23 09:06:46 +02:00
|
|
|
|
|
|
|
uint16_t activeCameraIndex;
|
|
|
|
|
|
|
|
// needed to forward inputs to IMGUI
|
|
|
|
InputState inputState;
|
|
|
|
|
|
|
|
Renderer() :
|
|
|
|
initialized(false),
|
|
|
|
drawDebug(false),
|
|
|
|
width (0),
|
|
|
|
height (0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// initialize simple geometries (cube, sphere, ...)
|
|
|
|
void createGeometries();
|
|
|
|
// create uniforms, load shaders, and create render targets
|
|
|
|
void setupShaders();
|
|
|
|
// setup renderpasses and wire up render targets
|
|
|
|
void setupRenderPasses();
|
|
|
|
|
|
|
|
void initialize(int width, int height);
|
|
|
|
void shutdown();
|
|
|
|
void paintGL();
|
|
|
|
void paintGLSimple();
|
|
|
|
void resize (int width, int height);
|
|
|
|
|
2016-12-17 23:04:50 +01:00
|
|
|
// check whether shader files were modified and reload them. Returns
|
|
|
|
// true on success, otherwise false
|
|
|
|
bool updateShaders();
|
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
Entity* createEntity();
|
2016-11-06 22:30:09 +01:00
|
|
|
bool destroyEntity (Entity* entity);
|
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
bgfxutils::Mesh* loadMesh(const char* filename);
|
2016-11-27 22:02:17 +01:00
|
|
|
|
|
|
|
// debug commands
|
|
|
|
void drawDebugLine (
|
|
|
|
const SimpleMath::Vector3f &from,
|
|
|
|
const SimpleMath::Vector3f &to,
|
|
|
|
const SimpleMath::Vector3f &color);
|
2016-11-28 21:29:19 +01:00
|
|
|
|
|
|
|
void drawDebugAxes (
|
|
|
|
const SimpleMath::Vector3f &pos,
|
|
|
|
const SimpleMath::Matrix33f &orientation,
|
|
|
|
const float &scale);
|
2016-09-23 09:06:46 +02:00
|
|
|
};
|
|
|
|
|
2016-12-17 23:04:50 +01:00
|
|
|
struct RenderProgram {
|
|
|
|
bgfx::ProgramHandle program;
|
|
|
|
std::string vertexShaderFileName;
|
|
|
|
int vertexShaderFileModTime;
|
|
|
|
std::string fragmentShaderFileName;
|
|
|
|
int fragmentShaderFileModTime;
|
|
|
|
|
|
|
|
RenderProgram () :
|
|
|
|
vertexShaderFileName(""),
|
|
|
|
vertexShaderFileModTime(-1),
|
|
|
|
fragmentShaderFileName(""),
|
|
|
|
fragmentShaderFileModTime(-1)
|
|
|
|
{
|
|
|
|
program = BGFX_INVALID_HANDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderProgram (
|
|
|
|
const char* vertex_shader_file_name,
|
|
|
|
const char* fragment_shader_file_name
|
|
|
|
)
|
|
|
|
:
|
|
|
|
vertexShaderFileName(vertex_shader_file_name),
|
|
|
|
vertexShaderFileModTime(-1),
|
|
|
|
fragmentShaderFileName(fragment_shader_file_name),
|
|
|
|
fragmentShaderFileModTime(-1)
|
|
|
|
{
|
|
|
|
program = BGFX_INVALID_HANDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool reload();
|
|
|
|
bool checkModified() const;
|
|
|
|
bool valid() const {
|
|
|
|
return bgfx::isValid(program);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
struct RenderState {
|
|
|
|
enum {
|
2016-10-21 22:07:23 +02:00
|
|
|
Skybox,
|
2016-09-23 09:06:46 +02:00
|
|
|
ShadowMap,
|
|
|
|
Scene,
|
|
|
|
SceneTextured,
|
2016-12-17 23:04:50 +01:00
|
|
|
Lines,
|
2016-09-23 09:06:46 +02:00
|
|
|
Debug,
|
|
|
|
Count
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Texture {
|
|
|
|
uint32_t m_flags;
|
|
|
|
bgfx::UniformHandle m_sampler;
|
|
|
|
bgfx::TextureHandle m_texture;
|
|
|
|
uint8_t m_stage;
|
|
|
|
};
|
|
|
|
|
|
|
|
uint64_t m_state;
|
|
|
|
uint8_t m_numTextures;
|
2016-12-17 23:04:50 +01:00
|
|
|
RenderProgram m_program;
|
2016-09-23 09:06:46 +02:00
|
|
|
uint8_t m_viewId;
|
|
|
|
Texture m_textures[4];
|
|
|
|
};
|
|
|
|
|