Working on shadow maps

simple_math_single_header
Martin Felis 2018-03-15 11:01:55 +01:00
parent ef4479ba71
commit cb13324846
12 changed files with 181 additions and 144 deletions

View File

@ -1090,7 +1090,13 @@ struct DockContext
} }
Dock* getDockByIndex(int idx) { return idx < 0 ? nullptr : m_docks[(int)idx]; } Dock* getDockByIndex(int idx) {
if (idx >= 0 && idx >= m_docks.size()) {
fprintf(stderr, "Error loading docks: trying to get dock with id %d but only have %d docks", idx, m_docks.size());
IM_ASSERT(idx < m_docks.size());
}
return idx < 0 ? nullptr : m_docks[(int)idx];
}
void load() void load()

View File

@ -3,23 +3,44 @@
uniform vec4 uColor; uniform vec4 uColor;
uniform vec3 uLightDirection; uniform vec3 uLightDirection;
uniform vec3 uViewPosition; uniform vec3 uViewPosition;
uniform sampler2D uAlbedoTexture;
uniform sampler2D uShadowMap;
smooth in vec4 ioFragColor;
in vec3 ioNormal;
in vec3 ioFragPosition; in vec3 ioFragPosition;
in vec3 ioFragNormal;
in vec2 ioFragTexCoords;
smooth in vec4 ioFragColor;
in vec4 ioFragPosLightSpace;
out vec4 outColor; out vec4 outColor;
float ShadowCalculation(vec4 frag_pos_light_space) {
vec3 projected_coordinates = frag_pos_light_space.xyz / frag_pos_light_space.w;
projected_coordinates = projected_coordinates * 0.5 + 0.5;
float closest_depth = texture(uShadowMap, projected_coordinates.xy).r;
// float closest_depth = texture(uAlbedoTexture, projected_coordinates.xy).r;
float current_depth = projected_coordinates.z;
// return current_depth;
return closest_depth;
// float bias = 0.005;
// return current_depth - bias > closest_depth ? 1.0 : 0.0;
// return current_depth;
}
void main() { void main() {
vec4 albedo_color = texture(uAlbedoTexture, ioFragTexCoords) * ioFragColor * uColor;
// ambient lighting // ambient lighting
float ambient_strength = 0.1; float ambient_strength = 0.1;
vec4 ambient = ambient_strength * ioFragColor; vec4 ambient = ambient_strength * albedo_color;
// diffuse lighting // diffuse lighting
vec3 normal_dir = normalize(ioNormal); vec3 normal_dir = normalize(ioFragNormal);
vec3 light_dir = normalize(uLightDirection); vec3 light_dir = normalize(uLightDirection);
float diff = max(dot(normal_dir, light_dir), 0.0); float diff = max(dot(normal_dir, light_dir), 0.0);
vec4 diffuse = diff * ioFragColor; vec4 diffuse = diff * albedo_color;
// specular lighting // specular lighting
vec3 view_dir = normalize(uViewPosition - ioFragPosition); vec3 view_dir = normalize(uViewPosition - ioFragPosition);
@ -28,5 +49,19 @@ void main() {
float spec = pow(max(dot(normal_dir, halfway_dir), 0.0), 32); float spec = pow(max(dot(normal_dir, halfway_dir), 0.0), 32);
vec4 specular = spec * vec4(0.5); vec4 specular = spec * vec4(0.5);
outColor = ambient + diffuse + specular; // shadow
float shadow = ShadowCalculation(ioFragPosLightSpace);
outColor = ambient + (1.0 - shadow) * (diffuse + specular);
//
//
// vec3 projected_coordinates = ioFragPosLightSpace.xyz / ioFragPosLightSpace.w;
// projected_coordinates = projected_coordinates * 0.5 + 0.5;
// float shadow_map_value = texture(uShadowMap, projected_coordinates.xy).r;
// outColor = shadow_map_value * vec4(1.0, 1.0, 1.0, 1.0);
//
// outColor = vec4(vec3(1.0f - shadow_map_value), 1.0);
// outColor = (shadow) * vec4(1.0, 1.0, 1.0, 1.0);
// outColor = ioFragPosLightSpace / ioFragPosLightSpace.w;
// outColor = ambient + diffuse + specular;
} }

View File

@ -1,4 +1,4 @@
#version 330 core #version 150 core
in vec2 ioUV; in vec2 ioUV;

View File

@ -1,32 +1,13 @@
#version 150 core #version 150 core
uniform vec4 uColor;
uniform vec3 uLightDirection;
uniform vec3 uViewPosition;
smooth in vec4 ioFragColor; smooth in vec4 ioFragColor;
in vec3 ioNormal;
in vec3 ioFragPosition; in vec3 ioFragPosition;
out vec4 outColor; out vec4 outColor;
void main() { void main() {
// ambient lighting gl_FragDepth = gl_FragCoord.z;
float ambient_strength = 0.1;
vec4 ambient = ambient_strength * ioFragColor;
// diffuse lighting outColor = ioFragColor;
vec3 normal_dir = normalize(ioNormal);
vec3 light_dir = normalize(uLightDirection);
float diff = max(dot(normal_dir, light_dir), 0.0);
vec4 diffuse = diff * ioFragColor;
// specular lighting
vec3 view_dir = normalize(uViewPosition - ioFragPosition);
vec3 halfway_dir = normalize(light_dir + view_dir);
float spec = pow(max(dot(normal_dir, halfway_dir), 0.0), 32);
vec4 specular = spec * vec4(0.5);
outColor = ambient + diffuse + specular;
} }

View File

@ -1,18 +1,11 @@
#version 330 core #version 150 core
in vec2 ioUV; in vec2 ioUV;
out vec3 outColor; out vec4 outColor;
uniform sampler2D uTexture; uniform sampler2D uTexture;
uniform float uTime;
void main() { void main() {
outColor = texture(uTexture, outColor = texture(uTexture, ioUV);
ioUV
+ 0.01 * vec2(
sin(uTime + 1024.0 * ioUV.x),
cos(uTime + 768.0 * ioUV.y)
)
).xyz;
} }

View File

@ -9,17 +9,23 @@ in vec4 inColor;
uniform mat4 uModelMatrix; uniform mat4 uModelMatrix;
uniform mat4 uViewMatrix; uniform mat4 uViewMatrix;
uniform mat4 uProjectionMatrix; uniform mat4 uProjectionMatrix;
uniform mat4 uLightSpaceMatrix;
uniform mat3 uNormalMatrix; uniform mat3 uNormalMatrix;
uniform vec3 uLightDirection; uniform vec3 uLightDirection;
uniform vec3 uViewPosition; uniform vec3 uViewPosition;
smooth out vec4 ioFragColor;
out vec3 ioNormal;
out vec3 ioFragPosition; out vec3 ioFragPosition;
out vec3 ioFragNormal;
out vec2 ioFragTexCoords;
smooth out vec4 ioFragColor;
out vec4 ioFragPosLightSpace;
void main() { void main() {
gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * inCoord;
ioFragColor = inColor;
ioNormal = uNormalMatrix * inNormal;
ioFragPosition = (uModelMatrix * inCoord).xyz; ioFragPosition = (uModelMatrix * inCoord).xyz;
ioFragNormal = uNormalMatrix * inNormal;
ioFragTexCoords = inUV;
ioFragColor = inColor;
ioFragPosLightSpace = uLightSpaceMatrix * vec4(ioFragPosition, 1.0);
gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * inCoord;
} }

View File

@ -1,13 +1,11 @@
#version 330 core #version 150 core
in vec3 inVertex; in vec4 inCoord;
in vec2 inUV;
uniform mat4 uModelViewProj;
uniform float uTime;
out vec2 ioUV; out vec2 ioUV;
void main() { void main() {
gl_Position = uModelViewProj * vec4(inVertex, 1); ioUV = inUV;
ioUV = (inVertex.xy + vec2(1,1)) / 2.0; gl_Position = inCoord;
} }

View File

@ -1,25 +1,14 @@
#version 150 core #version 150 core
#extension GL_ARB_explicit_attrib_location : require
in vec4 inCoord; in vec4 inCoord;
in vec3 inNormal;
in vec2 inUV;
in vec4 inColor; in vec4 inColor;
uniform mat4 uModelMatrix; uniform mat4 uModelMatrix;
uniform mat4 uViewMatrix; uniform mat4 uLightSpaceMatrix;
uniform mat4 uProjectionMatrix;
uniform mat3 uNormalMatrix;
uniform vec3 uLightDirection;
uniform vec3 uViewPosition;
smooth out vec4 ioFragColor; smooth out vec4 ioFragColor;
out vec3 ioNormal;
out vec3 ioFragPosition;
void main() { void main() {
gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * inCoord; gl_Position = uLightSpaceMatrix * uModelMatrix * inCoord;
ioFragColor = inColor; ioFragColor = inColor;
ioNormal = uNormalMatrix * inNormal;
ioFragPosition = (uModelMatrix * inCoord).xyz;
} }

View File

@ -53,8 +53,10 @@ static const GLfloat g_coordinate_system_vertex_buffer_data[] = {
VertexArray gVertexArray; VertexArray gVertexArray;
VertexArrayMesh gCoordinateFrameMesh; VertexArrayMesh gCoordinateFrameMesh;
VertexArrayMesh gXZPlaneGrid;
VertexArrayMesh gXZPlaneMesh; VertexArrayMesh gXZPlaneMesh;
VertexArrayMesh gUnitCubeMesh; VertexArrayMesh gUnitCubeMesh;
VertexArrayMesh gScreenQuad;
// //
// Module // Module
@ -141,6 +143,7 @@ static bool module_step(struct module_state *state, float dt) {
assert (gWindow != nullptr); assert (gWindow != nullptr);
state->renderer->RenderGui(); state->renderer->RenderGui();
state->renderer->RenderGl(); state->renderer->RenderGl();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return true; return true;
} }
@ -175,13 +178,13 @@ void Light::Initialize() {
void Light::UpdateMatrices() { void Light::UpdateMatrices() {
mCamera.mIsOrthographic = true; mCamera.mIsOrthographic = true;
mCamera.mWidth = mShadowMapSize; mCamera.mWidth = 20.0f;
mCamera.mHeight = mShadowMapSize; mCamera.mHeight = 20.0f;
mCamera.mNear = mNear; mCamera.mNear = mNear;
mCamera.mFar = mFar; mCamera.mFar = mFar;
mCamera.mEye = mDirection * mBBoxSize * 0.5; mCamera.mEye = Vector3f (10.0f, 10.0f, 10.0f);
mCamera.mPoi = mCamera.mEye - mDirection; mCamera.mPoi = Vector3f (0.0f, 0.0f, 0.0f);
mCamera.UpdateMatrices(); mCamera.UpdateMatrices();
@ -210,9 +213,9 @@ void Renderer::Initialize(int width, int height) {
gCoordinateFrameMesh.SetData(vertex_data, 6); gCoordinateFrameMesh.SetData(vertex_data, 6);
// Plane // Plane Grid
const int plane_grid_size = 20; const int plane_grid_size = 20;
gXZPlaneMesh.Initialize(gVertexArray, (plane_grid_size + 1) * 4); gXZPlaneGrid.Initialize(gVertexArray, (plane_grid_size + 1) * 4);
std::vector<VertexArray::VertexData> plane_data((plane_grid_size + 1) * 4); std::vector<VertexArray::VertexData> plane_data((plane_grid_size + 1) * 4);
for (int i = 0, n = plane_grid_size + 1; i < n; ++i) { for (int i = 0, n = plane_grid_size + 1; i < n; ++i) {
@ -240,7 +243,22 @@ void Renderer::Initialize(int width, int height) {
-1.0f * i, plane_grid_size * 0.5f, -1.0f * i, plane_grid_size * 0.5f,
255, 255, 255, 255); 255, 255, 255, 255);
} }
gXZPlaneMesh.SetData(plane_data.data(), plane_data.size()); gXZPlaneGrid.SetData(plane_data.data(), plane_data.size());
// Plane Mesh
gXZPlaneMesh.Initialize(gVertexArray, 4);
VertexArray::VertexData plane_mesh_vertex_data[] = {
{-10.0f, 0.0f, -10.0f, 1.0f, 0.0f, 1.0f, 0.0f, -10.0f, -10.0f, 255, 255, 255, 255},
{-10.0f, 0.0f, 10.0f, 1.0f, 0.0f, 1.0f, 0.0f, -10.0f, 10.0f, 255, 255, 255, 255},
{ 10.0f, 0.0f, 10.0f, 1.0f, 0.0f, 1.0f, 0.0f, 10.0f, 10.0f, 255, 255, 255, 255},
{ 10.0f, 0.0f, -10.0f, 1.0f, 0.0f, 1.0f, 0.0f, 10.0f, -10.0f, 255, 255, 255, 255}
};
gXZPlaneMesh.SetData(plane_mesh_vertex_data, 4);
GLuint xz_plane_index_data[] = {
0, 1, 2,
2, 3, 0
};
gXZPlaneMesh.SetIndexData(xz_plane_index_data, 6);
// Unit Cube // Unit Cube
gUnitCubeMesh.Initialize(gVertexArray, 4 * 6); gUnitCubeMesh.Initialize(gVertexArray, 4 * 6);
@ -292,6 +310,21 @@ void Renderer::Initialize(int width, int height) {
}; };
gUnitCubeMesh.SetIndexData(unit_cube_index_data, 36); gUnitCubeMesh.SetIndexData(unit_cube_index_data, 36);
// Screen Quad
gScreenQuad.Initialize(gVertexArray, 4);
VertexArray::VertexData screen_quad_data[] = {
{-1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 255, 255, 255, 255},
{ 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 255, 255, 255, 255},
{ 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 255, 255, 255, 255},
{-1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 255, 255, 255, 255}
};
gScreenQuad.SetData(screen_quad_data, 4);
GLuint screen_quad_indices[] = {
0, 1, 2,
2, 3, 0
};
gScreenQuad.SetIndexData(screen_quad_indices, 6);
// Simple Shader // Simple Shader
mSimpleProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl"); mSimpleProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl");
bool load_result = mSimpleProgram.Load(); bool load_result = mSimpleProgram.Load();
@ -308,19 +341,12 @@ void Renderer::Initialize(int width, int height) {
load_result = mRenderQuadProgramColor.Load(); load_result = mRenderQuadProgramColor.Load();
mRenderQuadProgramColor.RegisterFileModification(); mRenderQuadProgramColor.RegisterFileModification();
assert(load_result); assert(load_result);
muRenderQuadModelViewProj = mRenderQuadProgramColor.GetUniformLocation("uModelViewProj");
muRenderQuadTexture = mRenderQuadProgramColor.GetUniformLocation("uTexture");
muRenderQuadTime = mRenderQuadProgramColor.GetUniformLocation("uTime");
// Program for depth texture rendering // Program for depth texture rendering
mRenderQuadProgramDepth = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_depthbuffer.glsl"); mRenderQuadProgramDepth = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_depthbuffer.glsl");
load_result = mRenderQuadProgramDepth.Load(); load_result = mRenderQuadProgramDepth.Load();
mRenderQuadProgramDepth.RegisterFileModification(); mRenderQuadProgramDepth.RegisterFileModification();
assert(load_result); assert(load_result);
muRenderQuadDepthModelViewProj = mRenderQuadProgramDepth.GetUniformLocation( "uModelViewProj");
muRenderQuadDepthNear = mRenderQuadProgramDepth.GetUniformLocation("uNear");
muRenderQuadDepthFar = mRenderQuadProgramDepth.GetUniformLocation("uFar");
// Render Target // Render Target
gLog("Initializing main render target size: %d,%d", width, height); gLog("Initializing main render target size: %d,%d", width, height);
@ -329,23 +355,15 @@ void Renderer::Initialize(int width, int height) {
| RenderTarget::EnableDepthTexture | RenderTarget::EnableDepthTexture
| RenderTarget::EnableLinearizedDepthTexture); | RenderTarget::EnableLinearizedDepthTexture);
// Render Target Quad mRenderTarget.mVertexArray = &gVertexArray;
glGenVertexArrays(1, &mRenderQuadVertexArrayId); mRenderTarget.mQuadMesh = &gScreenQuad;
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);
mRenderTarget.mQuadVertexArray = mRenderQuadVertexArrayId;
mRenderTarget.mQuadVertexBuffer = mRenderQuadVertexBufferId;
mRenderTarget.mLinearizeDepthProgram = mRenderQuadProgramDepth; mRenderTarget.mLinearizeDepthProgram = mRenderQuadProgramDepth;
mRenderTarget.mLinearizeDepthProgram.RegisterFileModification(); mRenderTarget.mLinearizeDepthProgram.RegisterFileModification();
// Light // Light
mLight.Initialize(); mLight.Initialize();
mLight.mShadowMapTarget.mQuadVertexArray = mRenderQuadVertexArrayId; mLight.mShadowMapTarget.mVertexArray = &gVertexArray;
mLight.mShadowMapTarget.mQuadVertexBuffer = mRenderQuadVertexBufferId; mLight.mShadowMapTarget.mQuadMesh = &gScreenQuad;
mLight.mShadowMapTarget.mLinearizeDepthProgram = mRenderQuadProgramDepth; mLight.mShadowMapTarget.mLinearizeDepthProgram = mRenderQuadProgramDepth;
mLight.mShadowMapTarget.mLinearizeDepthProgram.RegisterFileModification(); mLight.mShadowMapTarget.mLinearizeDepthProgram.RegisterFileModification();
} }
@ -372,8 +390,10 @@ void Renderer::RenderGl() {
glViewport(0, 0, mLight.mShadowMapSize, mLight.mShadowMapSize); glViewport(0, 0, mLight.mShadowMapSize, mLight.mShadowMapSize);
mLight.UpdateMatrices(); mLight.UpdateMatrices();
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glUseProgram(mLight.mShadowMapProgram.mProgramId); glUseProgram(mLight.mShadowMapProgram.mProgramId);
if (mLight.mShadowMapProgram.SetMat44("uLightSpaceMatrix", mLight.mLightSpaceMatrix) == -1) {
gLog ("Warning: Uniform %s not found!", "uLightSpaceMatrix");
}
RenderScene(mLight.mShadowMapProgram, mLight.mCamera); RenderScene(mLight.mShadowMapProgram, mLight.mCamera);
mLight.mShadowMapTarget.RenderToLinearizedDepth(mLight.mCamera.mNear, mLight.mCamera.mFar, mLight.mCamera.mIsOrthographic); mLight.mShadowMapTarget.RenderToLinearizedDepth(mLight.mCamera.mNear, mLight.mCamera.mFar, mLight.mCamera.mIsOrthographic);
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
@ -404,7 +424,7 @@ void Renderer::RenderGl() {
// clear color and depth // clear color and depth
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glUseProgram(mSimpleProgram.mProgramId); glUseProgram(mSimpleProgram.mProgramId);
mSimpleProgram.SetMat44("uModelViewProj", model_view_projection); mSimpleProgram.SetMat44("uModelViewProj", model_view_projection);
@ -412,7 +432,7 @@ void Renderer::RenderGl() {
// Coordinate System: VertexArrayMesh // Coordinate System: VertexArrayMesh
model_view_projection = model_view_projection =
TranslateMat44(0.0f, 0.0f, 0.0f) TranslateMat44(0.0f, 0.002f, 0.0f)
* mCamera.mViewMatrix * mCamera.mViewMatrix
* mCamera.mProjectionMatrix; * mCamera.mProjectionMatrix;
mSimpleProgram.SetMat44("uModelViewProj", model_view_projection); mSimpleProgram.SetMat44("uModelViewProj", model_view_projection);
@ -422,27 +442,28 @@ void Renderer::RenderGl() {
// Plane // Plane
model_view_projection = model_view_projection =
TranslateMat44(0.0f, 0.0f, 0.0f) TranslateMat44(0.0f, 0.001f, 0.0f)
* mCamera.mViewMatrix * mCamera.mViewMatrix
* mCamera.mProjectionMatrix; * mCamera.mProjectionMatrix;
mSimpleProgram.SetMat44("uModelViewProj", model_view_projection); mSimpleProgram.SetMat44("uModelViewProj", model_view_projection);
mSimpleProgram.SetVec4("uColor", Vector4f (1.0f, 0.0f, 0.0f, 1.0f)); mSimpleProgram.SetVec4("uColor", Vector4f (1.0f, 0.0f, 0.0f, 1.0f));
gVertexArray.Bind(); gVertexArray.Bind();
gXZPlaneMesh.Draw(GL_LINES); gXZPlaneGrid.Draw(GL_LINES);
// Scene // Scene
glUseProgram(mDefaultProgram.mProgramId); glUseProgram(mDefaultProgram.mProgramId);
glBindTexture(GL_TEXTURE_2D, mLight.mShadowMapTarget.mDepthTexture);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
if (mDefaultProgram.SetMat44("uLightSpaceMatrix", mLight.mLightSpaceMatrix) == -1) {
gLog ("Warning: Uniform %s not found!", "uLightSpaceMatrix");
}
RenderScene(mDefaultProgram, mCamera); RenderScene(mDefaultProgram, mCamera);
if (mSettings->DrawDepth) { if (mSettings->DrawDepth) {
mRenderTarget.RenderToLinearizedDepth(mCamera.mNear, mCamera.mFar, mCamera.mIsOrthographic); mRenderTarget.RenderToLinearizedDepth(mCamera.mNear, mCamera.mFar, mCamera.mIsOrthographic);
} }
glDisableVertexAttribArray(0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }
void Renderer::RenderScene(RenderProgram &program, const Camera& camera) { void Renderer::RenderScene(RenderProgram &program, const Camera& camera) {
@ -452,16 +473,34 @@ void Renderer::RenderScene(RenderProgram &program, const Camera& camera) {
Matrix33f normal_matrix = model_matrix.block<3,3>(0,0).transpose(); Matrix33f normal_matrix = model_matrix.block<3,3>(0,0).transpose();
normal_matrix = normal_matrix.inverse(); normal_matrix = normal_matrix.inverse();
program.SetMat44("uModelMatrix", model_matrix); if (program.SetMat44("uModelMatrix", model_matrix) == -1) {
gLog ("Warning: could not find uModelMatrix");
}
program.SetMat44("uViewMatrix", camera.mViewMatrix); program.SetMat44("uViewMatrix", camera.mViewMatrix);
program.SetMat44("uProjectionMatrix", camera.mProjectionMatrix); program.SetMat44("uProjectionMatrix", camera.mProjectionMatrix);
program.SetMat33("uNormalMatrix", normal_matrix); program.SetMat33("uNormalMatrix", normal_matrix);
program.SetVec4("uColor", Vector4f (1.0f, 0.0f, 0.0f, 1.0f)); program.SetVec4("uColor", Vector4f (1.0f, 1.0f, 1.0f, 1.0f));
program.SetVec3("uLightDirection", mLight.mDirection); program.SetVec3("uLightDirection", mLight.mDirection);
program.SetVec3("uViewPosition", camera.mEye); program.SetVec3("uViewPosition", camera.mEye);
program.SetMat44("uLightSpaceMatrix", mLight.mLightSpaceMatrix);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mLight.mShadowMapTarget.mDepthTexture);
program.SetInt("uShadowMap", 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, mDefaultTexture.mTextureId);
program.SetInt("uAlbedoTexture", 1);
gVertexArray.Bind(); gVertexArray.Bind();
program.SetMat44("uModelMatrix", TranslateMat44(3.0, 2.0, 0.0));
gUnitCubeMesh.Draw(GL_TRIANGLES); gUnitCubeMesh.Draw(GL_TRIANGLES);
program.SetMat44("uModelMatrix", Matrix44f::Identity());
program.SetVec4("uColor", Vector4f (1.0f, 1.0f, 1.0f, 1.0f));
gXZPlaneMesh.Draw(GL_TRIANGLES);
} }
void Renderer::RenderGui() { void Renderer::RenderGui() {
@ -494,7 +533,7 @@ void Renderer::RenderGui() {
ImGui::SliderFloat3("Direction", mLight.mDirection.data(), -10.0f, 10.0f); ImGui::SliderFloat3("Direction", mLight.mDirection.data(), -10.0f, 10.0f);
ImVec2 content_avail = ImGui::GetContentRegionAvail(); ImVec2 content_avail = ImGui::GetContentRegionAvail();
ImGui::SliderFloat("Light Near", &mLight.mNear, -10.0f, 10.0f); ImGui::SliderFloat("Light Near", &mLight.mNear, -10.0f, 50.0f);
ImGui::SliderFloat("Light Far", &mLight.mFar, -10.0f, 50.0f); ImGui::SliderFloat("Light Far", &mLight.mFar, -10.0f, 50.0f);
ImGui::Checkbox("Draw Light Depth", &mSettings->DrawLightDepth); ImGui::Checkbox("Draw Light Depth", &mSettings->DrawLightDepth);
void* texture; void* texture;

View File

@ -65,27 +65,13 @@ struct Renderer {
Texture mDefaultTexture; Texture mDefaultTexture;
RenderProgram mSimpleProgram; RenderProgram mSimpleProgram;
RenderProgram mDefaultProgram; RenderProgram mDefaultProgram;
GLuint muDefaultModelViewProjection; RenderProgram mRenderQuadProgramColor;
GLuint muDefaultColor; RenderProgram mRenderQuadProgramDepth;
RenderTarget mRenderTarget; RenderTarget mRenderTarget;
GLTextureRef mRenderTextureRef = { (int)0xbadface }; GLTextureRef mRenderTextureRef = { (int)0xbadface };
GLuint mRenderQuadVertexArrayId;
GLuint mRenderQuadVertexBufferId;
RenderProgram mRenderQuadProgramColor;
GLuint muRenderQuadModelViewProj;
GLuint muRenderQuadTexture;
GLuint muRenderQuadTime;
RenderProgram mRenderQuadProgramDepth;
GLuint muRenderQuadDepthModelViewProj;
GLuint muRenderQuadDepthNear;
GLuint muRenderQuadDepthFar;
Renderer() : Renderer() :
mInitialized(false), mInitialized(false),
mWidth (0), mWidth (0),

View File

@ -29,6 +29,9 @@ void Camera::UpdateMatrices() {
float width = mWidth * 0.5f * (mFar - mNear * 0.5f) * 0.001f; float width = mWidth * 0.5f * (mFar - mNear * 0.5f) * 0.001f;
float height = width * mHeight / mWidth; float height = width * mHeight / mWidth;
// width = mWidth;
// height = mHeight;
mProjectionMatrix = Ortho(-width * 0.5f, width * 0.5f, -height * 0.5f, height * 0.5f, mNear, mFar); mProjectionMatrix = Ortho(-width * 0.5f, width * 0.5f, -height * 0.5f, height * 0.5f, mNear, mFar);
} else { } else {
mProjectionMatrix = Perspective(mFov, mWidth / mHeight, mNear, mFar); mProjectionMatrix = Perspective(mFov, mWidth / mHeight, mNear, mFar);
@ -355,6 +358,8 @@ void RenderTarget::Resize(int width, int height) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, mDepthTexture, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, mDepthTexture, 0);
@ -397,8 +402,8 @@ void RenderTarget::Resize(int width, int height) {
void RenderTarget::RenderToLinearizedDepth(const float& near, const float& far, bool is_orthographic) { void RenderTarget::RenderToLinearizedDepth(const float& near, const float& far, bool is_orthographic) {
assert(mFlags & EnableLinearizedDepthTexture); assert(mFlags & EnableLinearizedDepthTexture);
assert(mLinearizedDepthTexture != -1); assert(mLinearizedDepthTexture != -1);
assert(mQuadVertexArray != -1); assert(mVertexArray != nullptr);
assert(mQuadVertexBuffer != -1); assert(mQuadMesh != nullptr);
glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferId); glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferId);
GLenum draw_attachment_1[] = { GL_COLOR_ATTACHMENT1 }; GLenum draw_attachment_1[] = { GL_COLOR_ATTACHMENT1 };
@ -420,24 +425,14 @@ void RenderTarget::RenderToLinearizedDepth(const float& near, const float& far,
mLinearizeDepthProgram.SetFloat("uIsOrthographic", is_orthographic ? 1.0f : 0.0f); mLinearizeDepthProgram.SetFloat("uIsOrthographic", is_orthographic ? 1.0f : 0.0f);
mLinearizeDepthProgram.SetInt("uDepthTexture", 0); mLinearizeDepthProgram.SetInt("uDepthTexture", 0);
glEnableVertexAttribArray(0); mVertexArray->Bind();
glBindBuffer(GL_ARRAY_BUFFER, mQuadVertexBuffer); mQuadMesh->Draw(GL_TRIANGLES);
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
if (mFlags & EnableColor) { if (mFlags & EnableColor) {
GLenum draw_attachment_0[] = { GL_COLOR_ATTACHMENT1 }; GLenum draw_attachment_0[] = { GL_COLOR_ATTACHMENT1 };
glDrawBuffers(1, draw_attachment_0); glDrawBuffers(1, draw_attachment_0);
glEnable(GL_DEPTH_TEST);
} }
glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }
// //

View File

@ -9,6 +9,10 @@
#include <vector> #include <vector>
// Forward declarations
struct VertexArray;
struct VertexArrayMesh;
struct Transform { struct Transform {
Quaternion rotation = Quaternion (0.0f, 0.0f, 0.0f, 1.0f); Quaternion rotation = Quaternion (0.0f, 0.0f, 0.0f, 1.0f);
Vector3f translation = Vector3f (0.0f, 0.0f, 0.0f); Vector3f translation = Vector3f (0.0f, 0.0f, 0.0f);
@ -197,33 +201,40 @@ struct RenderProgram : AFileModificationListener {
GLuint CompileFragmentShader(); GLuint CompileFragmentShader();
GLuint LinkProgram(GLuint vertex_shader, GLuint fragment_shader); GLuint LinkProgram(GLuint vertex_shader, GLuint fragment_shader);
void SetInt(const char* name, const GLint& val) { GLint SetInt(const char* name, const GLint& val) {
GLint location = glGetUniformLocation(mProgramId, name); GLint location = glGetUniformLocation(mProgramId, name);
glUniform1i(location, val); glUniform1i(location, val);
return location;
} }
void SetFloat(const char* name, const float& val) { GLint SetFloat(const char* name, const float& val) {
GLint location = glGetUniformLocation(mProgramId, name); GLint location = glGetUniformLocation(mProgramId, name);
glUniform1f(location, val); glUniform1f(location, val);
return location;
} }
void SetVec3(const char* name, const Vector3f& vec) { GLint SetVec3(const char* name, const Vector3f& vec) {
GLint location = glGetUniformLocation(mProgramId, name); GLint location = glGetUniformLocation(mProgramId, name);
glUniform3fv(location, 1, vec.data()); glUniform3fv(location, 1, vec.data());
return location;
} }
void SetVec4(const char* name, const Vector3f& vec, float w = 1.0f) { GLint SetVec4(const char* name, const Vector3f& vec, float w = 1.0f) {
GLint location = glGetUniformLocation(mProgramId, name); GLint location = glGetUniformLocation(mProgramId, name);
glUniform4f(location, vec[0], vec[1], vec[2], w); glUniform4f(location, vec[0], vec[1], vec[2], w);
return location;
} }
void SetVec4(const char* name, const Vector4f& vec) { GLint SetVec4(const char* name, const Vector4f& vec) {
GLint location = glGetUniformLocation(mProgramId, name); GLint location = glGetUniformLocation(mProgramId, name);
glUniform4fv(location, 1, vec.data()); glUniform4fv(location, 1, vec.data());
return location;
} }
void SetMat44 (const char* name, const Matrix44f& mat) { GLint SetMat44 (const char* name, const Matrix44f& mat) {
GLint location = glGetUniformLocation(mProgramId, name); GLint location = glGetUniformLocation(mProgramId, name);
glUniformMatrix4fv(location, 1, GL_FALSE, mat.data()); glUniformMatrix4fv(location, 1, GL_FALSE, mat.data());
return location;
} }
void SetMat33 (const char* name, const Matrix33f& mat) { GLint SetMat33 (const char* name, const Matrix33f& mat) {
GLint location = glGetUniformLocation(mProgramId, name); GLint location = glGetUniformLocation(mProgramId, name);
glUniformMatrix3fv(location, 1, GL_FALSE, mat.data()); glUniformMatrix3fv(location, 1, GL_FALSE, mat.data());
return location;
} }
void RegisterFileModification(); void RegisterFileModification();
@ -252,8 +263,8 @@ struct RenderTarget {
int mFlags = 0; int mFlags = 0;
RenderProgram mLinearizeDepthProgram; RenderProgram mLinearizeDepthProgram;
GLuint mQuadVertexArray = -1; VertexArray* mVertexArray = nullptr;
GLuint mQuadVertexBuffer = -1; VertexArrayMesh* mQuadMesh = nullptr;
RenderTarget() {}; RenderTarget() {};
~RenderTarget(); ~RenderTarget();
@ -276,8 +287,6 @@ struct Texture {
bool Load(const char* path, int num_components = 3); bool Load(const char* path, int num_components = 3);
}; };
struct VertexArrayMesh;
/** /**
* Multiple VertexArrayMeshes can be stored in a single VertexArray. * Multiple VertexArrayMeshes can be stored in a single VertexArray.
* Storage order is: * Storage order is: