2016-09-23 09:06:46 +02:00
|
|
|
#include "RuntimeModule.h"
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "RenderModule.h"
|
2018-02-13 14:27:16 +01:00
|
|
|
#include <GLFW/glfw3.h>
|
2018-02-17 23:49:56 +01:00
|
|
|
#include "Serializer.h"
|
2017-07-02 20:18:34 +02:00
|
|
|
|
2018-02-16 22:43:40 +01:00
|
|
|
#include "imgui/imgui.h"
|
|
|
|
#include "imgui_dock.h"
|
|
|
|
|
2018-02-16 11:35:09 +01:00
|
|
|
using namespace SimpleMath::GL;
|
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
struct Renderer;
|
|
|
|
|
2018-02-17 23:49:56 +01:00
|
|
|
struct RendererSettings {
|
|
|
|
bool DrawDepth = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
static RendererSettings sRendererSettings;
|
|
|
|
|
2018-02-13 12:05:07 +01:00
|
|
|
static const GLfloat g_vertex_buffer_data[] = {
|
2018-02-16 22:43:40 +01:00
|
|
|
-0.9f, -0.9f, 0.0f,
|
2018-02-16 22:06:09 +01:00
|
|
|
0.9f, -0.9f, 0.0f,
|
2018-02-17 23:49:56 +01:00
|
|
|
0.0f, 0.9f, 4.0f
|
2018-02-13 12:05:07 +01:00
|
|
|
};
|
|
|
|
|
2018-02-13 14:27:16 +01:00
|
|
|
static const GLfloat g_quad_vertex_buffer_data[] = {
|
|
|
|
-1.0f, -1.0f, 0.0f,
|
|
|
|
1.0f, -1.0f, 0.0f,
|
|
|
|
-1.0f, 1.0f, 0.0f,
|
|
|
|
-1.0f, 1.0f, 0.0f,
|
|
|
|
1.0f, -1.0f, 0.0f,
|
|
|
|
1.0f, 1.0f, 0.0f
|
|
|
|
};
|
|
|
|
|
2018-02-19 14:33:29 +01:00
|
|
|
static const GLfloat g_textured_quad_vertex_buffer_data[] = {
|
|
|
|
-1.0f, -1.0f, 0.0f, 0.0f, 1.0f,
|
|
|
|
1.0f, -1.0f, 0.0f, 1.0f, 1.0f,
|
|
|
|
-1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
|
|
|
|
-1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
|
|
|
|
1.0f, -1.0f, 0.0f, 1.0f, 1.0f,
|
|
|
|
1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
|
|
|
|
};
|
|
|
|
|
2018-02-27 23:15:07 +01:00
|
|
|
static const GLfloat g_coordinate_system_vertex_buffer_data[] = {
|
2018-02-28 10:01:39 +01:00
|
|
|
0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
|
|
|
1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
|
|
|
|
0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
|
2018-02-27 23:15:07 +01:00
|
|
|
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
|
|
|
|
0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
|
|
|
|
};
|
|
|
|
|
2018-02-28 12:39:12 +01:00
|
|
|
VertexArray gVertexArray;
|
|
|
|
VertexArrayMesh gVertexArrayMesh;
|
|
|
|
|
2018-02-12 21:35:44 +01:00
|
|
|
//
|
|
|
|
// Module
|
|
|
|
//
|
2016-09-23 09:06:46 +02:00
|
|
|
struct module_state {
|
|
|
|
Renderer *renderer;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct module_state *module_init() {
|
2018-02-12 13:05:19 +01:00
|
|
|
gLog ("%s %s called", __FILE__, __FUNCTION__);
|
2016-09-23 09:06:46 +02:00
|
|
|
assert (gWindow != nullptr && "Cannot initialize renderer module without gWindow!");
|
|
|
|
|
|
|
|
module_state *state = (module_state*) malloc(sizeof(*state));
|
|
|
|
state->renderer = new Renderer();
|
|
|
|
assert (state->renderer != nullptr);
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2017-01-09 21:24:20 +01:00
|
|
|
template <typename Serializer>
|
|
|
|
static void module_serialize (
|
|
|
|
struct module_state *state,
|
|
|
|
Serializer* serializer) {
|
2018-02-17 23:49:56 +01:00
|
|
|
SerializeBool(*serializer, "protot.RenderModule.DrawDepth", sRendererSettings.DrawDepth);
|
2018-02-19 14:33:29 +01:00
|
|
|
SerializeBool(*serializer, "protot.RenderModule.Camera.mIsOrthographic", gRenderer->mCamera.mIsOrthographic);
|
2018-02-27 21:32:29 +01:00
|
|
|
SerializeFloat(*serializer, "protot.RenderModule.Camera.mFov", gRenderer->mCamera.mFov);
|
|
|
|
SerializeVec3(*serializer, "protot.RenderModule.Camera.mEye", gRenderer->mCamera.mEye);
|
|
|
|
SerializeVec3(*serializer, "protot.RenderModule.Camera.mPoi", gRenderer->mCamera.mPoi);
|
|
|
|
SerializeVec3(*serializer, "protot.RenderModule.Camera.mUp", gRenderer->mCamera.mUp);
|
2018-02-19 14:33:29 +01:00
|
|
|
SerializeFloat(*serializer, "protot.RenderModule.Camera.mNear", gRenderer->mCamera.mNear);
|
|
|
|
SerializeFloat(*serializer, "protot.RenderModule.Camera.mFar", gRenderer->mCamera.mFar);
|
2017-01-09 21:24:20 +01:00
|
|
|
|
2018-02-12 21:35:44 +01:00
|
|
|
// SerializeBool (*serializer, "protot.RenderModule.draw_floor", gRenderer->drawFloor);
|
|
|
|
// SerializeBool (*serializer, "protot.RenderModule.draw_skybox", gRenderer->drawSkybox);
|
|
|
|
// SerializeBool (*serializer, "protot.RenderModule.debug_enabled", gRenderer->drawDebug);
|
|
|
|
// SerializeVec3 (*serializer, "protot.RenderModule.camera.eye", camera->eye);
|
|
|
|
// SerializeVec3 (*serializer, "protot.RenderModule.camera.poi", camera->poi);
|
2017-01-09 21:24:20 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
static void module_finalize(struct module_state *state) {
|
2018-02-12 13:05:19 +01:00
|
|
|
gLog ("%s %s called (state %p)", __FILE__, __FUNCTION__, state);
|
2016-09-23 09:06:46 +02:00
|
|
|
|
|
|
|
assert (state->renderer != nullptr);
|
|
|
|
delete state->renderer;
|
|
|
|
|
|
|
|
free(state);
|
|
|
|
}
|
|
|
|
|
2017-01-09 21:24:20 +01:00
|
|
|
static void module_reload(struct module_state *state, void *read_serializer) {
|
2018-02-12 13:05:19 +01:00
|
|
|
gLog ("%s %s called (state %p)", __FILE__, __FUNCTION__, state);
|
2016-09-23 09:06:46 +02:00
|
|
|
assert (gWindow != nullptr);
|
|
|
|
|
2017-02-05 10:36:37 +01:00
|
|
|
gLog ("Renderer initialize");
|
2016-09-23 09:06:46 +02:00
|
|
|
assert (state != nullptr);
|
2018-02-12 21:35:44 +01:00
|
|
|
state->renderer->Initialize(100, 100);
|
2018-02-17 23:49:56 +01:00
|
|
|
state->renderer->mSettings = &sRendererSettings;
|
2018-02-13 12:05:07 +01:00
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
gRenderer = state->renderer;
|
2016-11-20 21:50:53 +01:00
|
|
|
|
2017-01-09 21:24:20 +01:00
|
|
|
// load the state of the module
|
|
|
|
if (read_serializer != nullptr) {
|
|
|
|
module_serialize(state, static_cast<ReadSerializer*>(read_serializer));
|
|
|
|
}
|
2016-09-23 09:06:46 +02:00
|
|
|
}
|
|
|
|
|
2017-01-09 21:24:20 +01:00
|
|
|
static void module_unload(struct module_state *state, void* write_serializer) {
|
|
|
|
// serialize the state of the module
|
|
|
|
if (write_serializer != nullptr) {
|
|
|
|
module_serialize(state, static_cast<WriteSerializer*>(write_serializer));
|
|
|
|
}
|
2016-11-20 21:50:53 +01:00
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
gRenderer = nullptr;
|
2018-02-12 21:35:44 +01:00
|
|
|
state->renderer->Shutdown();
|
2016-11-20 21:50:53 +01:00
|
|
|
|
2017-02-05 10:36:37 +01:00
|
|
|
gLog ("RenderModule unload called");
|
2016-09-23 09:06:46 +02:00
|
|
|
}
|
|
|
|
|
2016-11-07 21:34:18 +01:00
|
|
|
static bool module_step(struct module_state *state, float dt) {
|
2016-09-23 09:06:46 +02:00
|
|
|
int width, height;
|
|
|
|
assert (gWindow != nullptr);
|
2018-02-12 21:35:44 +01:00
|
|
|
state->renderer->RenderGui();
|
|
|
|
state->renderer->RenderGl();
|
2017-02-18 17:22:21 +01:00
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
const struct module_api MODULE_API = {
|
|
|
|
.init = module_init,
|
|
|
|
.reload = module_reload,
|
|
|
|
.step = module_step,
|
|
|
|
.unload = module_unload,
|
|
|
|
.finalize = module_finalize
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-17 23:04:50 +01:00
|
|
|
|
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
//
|
2018-02-12 21:35:44 +01:00
|
|
|
// Camera
|
2016-09-23 09:06:46 +02:00
|
|
|
//
|
2018-02-16 22:06:09 +01:00
|
|
|
void Camera::UpdateMatrices() {
|
2018-02-19 14:33:29 +01:00
|
|
|
mViewMatrix = LookAt(mEye, mPoi, mUp);
|
2018-02-16 22:06:09 +01:00
|
|
|
|
2018-02-19 14:33:29 +01:00
|
|
|
if (mIsOrthographic) {
|
|
|
|
mProjectionMatrix = Ortho(-1.0f, 1.0f, -1.0f, 1.0f, mNear, mFar);
|
|
|
|
} else {
|
2018-02-27 21:32:29 +01:00
|
|
|
mProjectionMatrix = Perspective(mFov, mWidth / mHeight, mNear, mFar);
|
2018-02-16 22:06:09 +01:00
|
|
|
}
|
2016-09-23 09:06:46 +02:00
|
|
|
}
|
|
|
|
|
2018-02-19 14:33:29 +01:00
|
|
|
void Camera::DrawGui() {
|
2018-02-27 21:32:29 +01:00
|
|
|
ImGui::SliderFloat3("Eye", mEye.data(), -10.0f, 10.0f);
|
|
|
|
ImGui::SliderFloat3("Poi", mPoi.data(), -10.0f, 10.0f);
|
|
|
|
ImGui::SliderFloat3("Up", mUp.data(), -10.0f, 10.0f);
|
2018-02-19 14:33:29 +01:00
|
|
|
ImGui::Checkbox("Orthographic", &mIsOrthographic);
|
2018-02-27 21:32:29 +01:00
|
|
|
ImGui::SliderFloat("Fov", &mFov, 5, 160);
|
2018-02-19 14:33:29 +01:00
|
|
|
ImGui::SliderFloat("Near", &mNear, -10, 10);
|
|
|
|
ImGui::SliderFloat("Far", &mFar, -10, 10);
|
2018-02-27 21:32:29 +01:00
|
|
|
if (ImGui::Button("Reset")) {
|
|
|
|
*this = Camera();
|
|
|
|
}
|
2018-02-19 14:33:29 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
|
2018-02-12 13:05:19 +01:00
|
|
|
//
|
2018-02-19 14:33:29 +01:00
|
|
|
// Renderer
|
2018-02-12 21:35:44 +01:00
|
|
|
//
|
|
|
|
void Renderer::Initialize(int width, int height) {
|
2018-02-19 14:33:29 +01:00
|
|
|
mDefaultTexture.MakeGrid(128, Vector3f (0.8, 0.8f, 0.8f), Vector3f (0.2f, 0.2f, 0.2f));
|
2018-02-28 12:39:12 +01:00
|
|
|
|
|
|
|
gVertexArray.Initialize(1000, GL_STATIC_DRAW);
|
|
|
|
gVertexArrayMesh.Initialize(gVertexArray, 6);
|
|
|
|
|
|
|
|
VertexArray::VertexData vertex_data[] = {
|
|
|
|
{0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 255, 0, 0, 255 },
|
|
|
|
{1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 255, 0, 0, 255 },
|
|
|
|
|
|
|
|
{0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, 255, 0, 255},
|
|
|
|
{0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, 255, 0, 255},
|
|
|
|
|
|
|
|
{0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 255, 255},
|
|
|
|
{0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 255, 255}
|
|
|
|
};
|
|
|
|
|
|
|
|
gVertexArrayMesh.SetData(vertex_data, 6);
|
|
|
|
|
2018-02-19 14:33:29 +01:00
|
|
|
// Mesh
|
2018-02-13 12:05:07 +01:00
|
|
|
glGenVertexArrays(1, &mMesh.mVertexArrayId);
|
|
|
|
glBindVertexArray(mMesh.mVertexArrayId);
|
|
|
|
glGenBuffers(1, &mMesh.mVertexBuffer);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, mMesh.mVertexBuffer);
|
2018-02-19 14:33:29 +01:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
// Plane
|
|
|
|
glGenVertexArrays(1, &mPlane.mVertexArrayId);
|
|
|
|
glBindVertexArray(mPlane.mVertexArrayId);
|
|
|
|
glGenBuffers(1, &mPlane.mVertexBuffer);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, mPlane.mVertexBuffer);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
|
2018-02-13 12:05:07 +01:00
|
|
|
|
2018-02-27 23:15:07 +01:00
|
|
|
// Coordinate System
|
|
|
|
glGenVertexArrays(1, &mCoordinateSystem.mVertexArrayId);
|
|
|
|
glBindVertexArray(mCoordinateSystem.mVertexArrayId);
|
|
|
|
glGenBuffers(1, &mCoordinateSystem.mVertexBuffer);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, mCoordinateSystem.mVertexBuffer);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(g_coordinate_system_vertex_buffer_data), g_coordinate_system_vertex_buffer_data, GL_STATIC_DRAW);
|
|
|
|
|
2018-02-16 21:22:34 +01:00
|
|
|
// Simple Shader
|
|
|
|
mDefaultProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl");
|
|
|
|
bool load_result = mDefaultProgram.Load();
|
2018-02-13 12:05:07 +01:00
|
|
|
assert(load_result);
|
2018-02-16 21:22:34 +01:00
|
|
|
muDefaultModelViewProjection = mDefaultProgram.GetUniformLocation("uModelViewProj");
|
|
|
|
muDefaultColor = mDefaultProgram.GetUniformLocation("uColor");
|
2018-02-13 12:44:34 +01:00
|
|
|
|
2018-02-13 14:27:16 +01:00
|
|
|
// Render Target
|
2018-02-17 23:49:56 +01:00
|
|
|
mRenderTarget = RenderTarget (width, height,
|
|
|
|
RenderTarget::EnableColor
|
|
|
|
| RenderTarget::EnableDepthTexture
|
|
|
|
| RenderTarget::EnableLinearizedDepthTexture);
|
2018-02-13 14:27:16 +01:00
|
|
|
|
|
|
|
// Render Target Quad
|
|
|
|
glGenVertexArrays(1, &mRenderQuadVertexArrayId);
|
|
|
|
glBindVertexArray(mRenderQuadVertexArrayId);
|
|
|
|
|
|
|
|
glGenBuffers(1, &mRenderQuadVertexBufferId);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, mRenderQuadVertexBufferId);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(g_quad_vertex_buffer_data), g_quad_vertex_buffer_data, GL_STATIC_DRAW);
|
2018-02-15 09:59:38 +01:00
|
|
|
|
|
|
|
// Program for color texture rendering
|
2018-02-13 14:27:16 +01:00
|
|
|
mRenderQuadProgramColor = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_simpletexture.glsl");
|
|
|
|
load_result = mRenderQuadProgramColor.Load();
|
|
|
|
assert(load_result);
|
2018-02-16 21:22:34 +01:00
|
|
|
muRenderQuadModelViewProj = mRenderQuadProgramColor.GetUniformLocation("uModelViewProj");
|
|
|
|
|
|
|
|
muRenderQuadTexture = mRenderQuadProgramColor.GetUniformLocation("uTexture");
|
|
|
|
muRenderQuadTime = mRenderQuadProgramColor.GetUniformLocation("uTime");
|
2018-02-15 09:59:38 +01:00
|
|
|
|
|
|
|
// Program for depth texture rendering
|
|
|
|
mRenderQuadProgramDepth = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_depthbuffer.glsl");
|
|
|
|
load_result = mRenderQuadProgramDepth.Load();
|
|
|
|
assert(load_result);
|
2018-02-16 21:22:34 +01:00
|
|
|
muRenderQuadDepthModelViewProj = mRenderQuadProgramDepth.GetUniformLocation( "uModelViewProj");
|
|
|
|
muRenderQuadDepthNear = mRenderQuadProgramDepth.GetUniformLocation("uNear");
|
|
|
|
muRenderQuadDepthFar = mRenderQuadProgramDepth.GetUniformLocation("uFar");
|
2016-11-06 22:30:09 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 21:35:44 +01:00
|
|
|
void Renderer::Shutdown() {
|
2018-02-13 12:05:07 +01:00
|
|
|
glDeleteVertexArrays(1, &mMesh.mVertexArrayId);
|
2018-02-19 14:33:29 +01:00
|
|
|
glDeleteBuffers(1, &mMesh.mVertexBuffer);
|
|
|
|
glDeleteVertexArrays(1, &mPlane.mVertexArrayId);
|
|
|
|
glDeleteBuffers(1, &mPlane.mVertexBuffer);
|
2018-02-27 23:15:07 +01:00
|
|
|
glDeleteVertexArrays(1, &mCoordinateSystem.mVertexArrayId);
|
|
|
|
glDeleteBuffers(1, &mCoordinateSystem.mVertexBuffer);
|
2016-11-27 22:02:17 +01:00
|
|
|
}
|
2018-02-13 14:27:16 +01:00
|
|
|
|
|
|
|
|
2018-02-12 21:35:44 +01:00
|
|
|
void Renderer::RenderGl() {
|
2018-02-13 14:27:16 +01:00
|
|
|
int width, height;
|
|
|
|
glfwGetWindowSize(gWindow, &width, &height);
|
2018-02-13 17:46:31 +01:00
|
|
|
if (width != mWidth || height != mHeight)
|
|
|
|
Resize(width, height);
|
2018-02-13 14:27:16 +01:00
|
|
|
|
2018-02-16 22:06:09 +01:00
|
|
|
mCamera.UpdateMatrices();
|
2018-02-16 21:22:34 +01:00
|
|
|
|
2018-02-16 22:06:09 +01:00
|
|
|
Matrix44f model_matrix = TranslateMat44(0.0f, 0.0f, 0.0f);
|
|
|
|
Matrix44f model_view_projection =
|
|
|
|
model_matrix
|
|
|
|
* mCamera.mViewMatrix
|
|
|
|
* mCamera.mProjectionMatrix;
|
2018-02-16 21:22:34 +01:00
|
|
|
|
2018-02-13 12:44:34 +01:00
|
|
|
// enable the render target
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, mRenderTarget.mFrameBufferId);
|
|
|
|
GLenum DrawBuffers[1] = { GL_COLOR_ATTACHMENT0 };
|
|
|
|
glDrawBuffers(1, DrawBuffers);
|
|
|
|
|
|
|
|
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
|
|
|
gLog ("Cannot render: frame buffer invalid!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear color and depth
|
2018-02-13 12:05:07 +01:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2018-02-14 13:14:10 +01:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
2018-02-13 12:05:07 +01:00
|
|
|
|
2018-02-16 21:22:34 +01:00
|
|
|
glUseProgram(mDefaultProgram.mProgramId);
|
|
|
|
glUniformMatrix4fv(muDefaultModelViewProjection, 1, GL_FALSE, model_view_projection.data());
|
|
|
|
glUniform4fv(muDefaultColor, 1, Vector4f(1.0f, 0.0f, 0.0f, 1.0f).data());
|
2018-02-13 12:05:07 +01:00
|
|
|
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, mMesh.mVertexBuffer);
|
|
|
|
glVertexAttribPointer(
|
|
|
|
0, // attribute 0
|
|
|
|
3, // size
|
|
|
|
GL_FLOAT, // type
|
|
|
|
GL_FALSE, // normalized?
|
|
|
|
0, // stride
|
|
|
|
(void*)0 // offset
|
|
|
|
);
|
|
|
|
|
2018-02-27 23:15:07 +01:00
|
|
|
// glDrawArrays(GL_TRIANGLES, 0, 3); // starting from vertex 0; 3 vertices total
|
2018-02-28 12:39:12 +01:00
|
|
|
// // Coordinate system
|
2018-02-27 23:15:07 +01:00
|
|
|
glEnableVertexAttribArray(0);
|
2018-02-28 10:01:39 +01:00
|
|
|
glBindVertexArray(mCoordinateSystem.mVertexArrayId);
|
|
|
|
glUniform4fv(muDefaultColor, 1, Vector4f(0.0f, 0.0f, 0.0f, 1.0f).data());
|
2018-02-27 23:15:07 +01:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, mCoordinateSystem.mVertexBuffer);
|
|
|
|
glVertexAttribPointer(
|
|
|
|
0,
|
|
|
|
3,
|
|
|
|
GL_FLOAT,
|
|
|
|
GL_FALSE,
|
2018-02-28 10:01:39 +01:00
|
|
|
(sizeof(float)*6),
|
2018-02-27 23:15:07 +01:00
|
|
|
(void*)0
|
|
|
|
);
|
2018-02-28 10:01:39 +01:00
|
|
|
|
|
|
|
glEnableVertexAttribArray(1);
|
2018-02-27 23:15:07 +01:00
|
|
|
glVertexAttribPointer(
|
|
|
|
1,
|
|
|
|
3,
|
|
|
|
GL_FLOAT,
|
|
|
|
GL_FALSE,
|
2018-02-28 10:01:39 +01:00
|
|
|
(sizeof(float)*6),
|
|
|
|
(void*)(sizeof(float) * 3)
|
2018-02-27 23:15:07 +01:00
|
|
|
);
|
|
|
|
glDrawArrays(GL_LINES, 0, 6);
|
2018-02-17 23:49:56 +01:00
|
|
|
|
2018-02-28 12:39:12 +01:00
|
|
|
// Coordinate System: VertexArrayMesh
|
|
|
|
model_view_projection =
|
|
|
|
TranslateMat44(1.25f, 0.0f, 0.0f)
|
|
|
|
* mCamera.mViewMatrix
|
|
|
|
* mCamera.mProjectionMatrix;
|
|
|
|
glUniformMatrix4fv(muDefaultModelViewProjection, 1, GL_FALSE, model_view_projection.data());
|
|
|
|
glUniform4fv(muDefaultColor, 1, Vector4f(1.0f, 0.0f, 0.0f, 1.0f).data());
|
|
|
|
glBindAttribLocation(mDefaultProgram.mProgramId, 0, "inCoord");
|
|
|
|
glBindAttribLocation(mDefaultProgram.mProgramId, 1, "inNormal");
|
|
|
|
glBindAttribLocation(mDefaultProgram.mProgramId, 2, "inUV");
|
|
|
|
glBindAttribLocation(mDefaultProgram.mProgramId, 3, "inColor");
|
|
|
|
gVertexArray.Bind();
|
|
|
|
gVertexArrayMesh.Draw(GL_LINES);
|
|
|
|
|
2018-02-17 23:49:56 +01:00
|
|
|
if (mSettings->DrawDepth) {
|
|
|
|
mRenderTarget.RenderToLinearizedDepth(true);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
|
|
|
|
Matrix44f model_view_projection = Matrix44f::Identity();
|
|
|
|
|
|
|
|
// render depth texture
|
|
|
|
glUseProgram(mRenderQuadProgramDepth.mProgramId);
|
|
|
|
glUniformMatrix4fv(muRenderQuadModelViewProj, 1, GL_FALSE, model_view_projection.data());
|
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mDepthTexture);
|
|
|
|
glUniform1i(muRenderQuadTexture, 0);
|
|
|
|
|
|
|
|
// TODO: adjust for perspective
|
2018-02-19 14:33:29 +01:00
|
|
|
glUniform1f(muRenderQuadDepthNear, mCamera.mNear);
|
2018-02-17 23:49:56 +01:00
|
|
|
// TODO: why do I have to divide by depth range?
|
2018-02-19 14:33:29 +01:00
|
|
|
glUniform1f(muRenderQuadDepthFar, mCamera.mFar / (mCamera.mFar - mCamera.mNear));
|
2018-02-17 23:49:56 +01:00
|
|
|
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, mRenderQuadVertexBufferId);
|
|
|
|
glVertexAttribPointer(
|
|
|
|
0, // attribute 0
|
|
|
|
3, // size
|
|
|
|
GL_FLOAT, // type
|
|
|
|
GL_FALSE, // normalized?
|
|
|
|
0, // stride
|
|
|
|
(void*)0 // offset
|
|
|
|
);
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6); // starting from vertex 0; 3 vertices total
|
|
|
|
|
|
|
|
mRenderTarget.RenderToLinearizedDepth(false);
|
|
|
|
}
|
|
|
|
|
2018-02-13 12:05:07 +01:00
|
|
|
glDisableVertexAttribArray(0);
|
2018-02-16 22:43:40 +01:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
2016-11-28 21:29:19 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 21:35:44 +01:00
|
|
|
void Renderer::RenderGui() {
|
2018-02-17 23:49:56 +01:00
|
|
|
if (ImGui::BeginDock("Scene")) {
|
|
|
|
ImGui::Checkbox("Draw Depth", &mSettings->DrawDepth);
|
2018-02-16 22:43:40 +01:00
|
|
|
|
2018-02-17 23:49:56 +01:00
|
|
|
GLuint texture;
|
|
|
|
if (mSettings->DrawDepth) {
|
|
|
|
texture = mRenderTarget.mLinearizedDepthTexture;
|
|
|
|
} else {
|
|
|
|
texture = mRenderTarget.mColorTexture;
|
|
|
|
}
|
2018-02-16 22:43:40 +01:00
|
|
|
|
|
|
|
ImGui::Text("Scene");
|
|
|
|
const ImVec2 content_avail = ImGui::GetContentRegionAvail();
|
2018-02-17 23:49:56 +01:00
|
|
|
|
2018-02-16 22:43:40 +01:00
|
|
|
ImGui::Image((void*) texture,
|
|
|
|
content_avail,
|
|
|
|
ImVec2(0.0f, 1.0f),
|
|
|
|
ImVec2(1.0f, 0.0f)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
ImGui::EndDock();
|
2018-02-19 14:33:29 +01:00
|
|
|
|
|
|
|
if (ImGui::BeginDock("Render Settings")) {
|
2018-02-27 21:32:29 +01:00
|
|
|
ImGui::Text("Camera");
|
2018-02-19 14:33:29 +01:00
|
|
|
mCamera.DrawGui();
|
|
|
|
|
|
|
|
ImGui::Text("Default Texture");
|
|
|
|
const ImVec2 content_avail = ImGui::GetContentRegionAvail();
|
|
|
|
ImGui::Image((void*) mDefaultTexture.mTextureId,
|
|
|
|
ImVec2(content_avail.x, content_avail.x),
|
|
|
|
ImVec2(0.0f, 1.0f),
|
|
|
|
ImVec2(1.0f, 0.0f)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
ImGui::EndDock();
|
2017-01-21 23:22:32 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 21:35:44 +01:00
|
|
|
void Renderer::Resize (int width, int height) {
|
2018-02-13 17:46:31 +01:00
|
|
|
mWidth = width;
|
|
|
|
mHeight = height;
|
|
|
|
mRenderTarget.Resize(mWidth, mHeight);
|
2018-02-27 21:32:29 +01:00
|
|
|
mCamera.mWidth = mWidth;
|
|
|
|
mCamera.mHeight = mHeight;
|
2018-02-13 17:46:31 +01:00
|
|
|
glViewport(0, 0, mWidth, mHeight);
|
2017-01-22 20:51:12 +01:00
|
|
|
}
|