Skip to content

Commit

Permalink
directional mapping for single directional light source
Browse files Browse the repository at this point in the history
  • Loading branch information
laudominik committed Jun 20, 2024
1 parent 7188938 commit c54221f
Show file tree
Hide file tree
Showing 18 changed files with 265 additions and 46 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ See [examples](examples) for example usage.
Core features:
1. creating meshes/models
2. loading meshes from wavefront .obj files
3. multiple ambient/point/directional lights using PHONG
3. multiple ambient/point/directional lights using PHONG + bloom
4. skyboxes
5. materials
6. camera
7. dynamic shader library (to avoid ubershader approach)
8. viewport postprocessing (filters, grayscale etc.)

More to be done.
For now the engine comes as a standalone library (tricore and triutils).
57 changes: 37 additions & 20 deletions examples/scene3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ int main(){
.setTranslation({0, 4.0, -4.0})
.setScaleXYZ({0.3, 0.3, 0.3});

auto dirLight = light::make_dir({
.direction = {-1, -1, 0},
.color = {1, 1, 1},
.intensity = 1
});

renderer
// .setPostprocessing<postprocess::InvertPostprocess>()
.setSkybox(Cubemap({
Expand All @@ -48,24 +54,30 @@ int main(){
auto cubeModel = std::make_shared<Model>();
cubeModel->setMesh(VertexCube())
.setMaterial<SolidMaterial>(glm::vec3{1.0, 0.0, 0.0})
.setTranslation({-7, -0.5, -10});
.setTranslation({2, 0.5, 1})
.enableCastShadow()
.enableReceiveShadow()
;
return cubeModel;
}())
.add([](){
auto capsule = std::make_shared<Model>();
capsule->setMesh(Mesh::fromFile("examples/data/mesh/textured_with_normals.obj"))
.setMaterial<SolidMaterial>(glm::vec3{0.0, 0.0, 1.0}, 1.0, 0.3, 1.0)
.setTranslation({-5, -0.5, -5});
.setTranslation({0.0f, 1.5f, 0.0})
.enableCastShadow()
.enableReceiveShadow()
;
return capsule;
}())
.add([](){
auto lightModel = std::make_shared<Model>();
lightModel->setMesh(Sphere())
.setMaterial<LightMaterial>(glm::vec3{1.0, 1.0, 1.0})
.setTranslation(secondLightPos)
.setScaleXYZ({0.3, 0.3, 0.3});
return lightModel;
}())
// .add([](){
// auto lightModel = std::make_shared<Model>();
// lightModel->setMesh(Cube())
// .setMaterial<LightMaterial>(glm::vec3{1.0, 1.0, 1.0})
// .setTranslation(secondLightPos)
// .setScaleXYZ({0.3, 0.3, 0.3});
// return lightModel;
// }())
.add(lightModel)
.add([](){
auto floor = std::make_shared<Model>();
Expand All @@ -76,19 +88,23 @@ int main(){
.setNormalMap(std::move(Texture("examples/data/textures/brickwall_normal.jpg")))
.build()
).setScaleXYZ({20, 20, 20})
.setTranslation({-5, -2, -10});
.setTranslation({0, 0, 0})
.enableCastShadow()
.enableReceiveShadow()
;
return floor;
}())
.addLightSource(light)
.addLightSource(light::make_point({
.pos = secondLightPos,
.color = {1, 1, 1},
.attentuation = {
.constant = 1.0,
.linear = 0.1,
.quadratic = 0.0
}
}));
// .addLightSource(light::make_point({
// .pos = secondLightPos,
// .color = {1, 1, 1},
// .attentuation = {
// .constant = 1.0,
// .linear = 0.1,
// .quadratic = 0.0
// }
// }))
.addLightSource(dirLight);

static constexpr auto R = 5.0;
while (!glfwWindowShouldClose(window.get()))
Expand All @@ -97,6 +113,7 @@ int main(){
camera.poll(window.get());
auto theta = glfwGetTime();
glm::vec3 pos = glm::vec3{R * sin(theta), 3.0, R * cos(theta)};
dirLight->direction = glm::vec3({sin(theta), -1.0, cos(theta)});
light->pos = pos;
lightModel->setTranslation(pos);
}
Expand Down
13 changes: 13 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once


// TODO: change from static to editable during runtime
namespace tri::config {
static constexpr auto CAMERA_FAR_PLANE = 300.f;
static constexpr auto CAMERA_NEAR_PLANE = 0.1f;
static constexpr struct {
unsigned int w = 1024;
unsigned int h = 1024;
} SHADOW_RESOLUTION;
}

4 changes: 3 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/*
small TODO list:
> add shadows
> add shadows from multiple sources
> add shadows from point lights
> make shadows less hardcoded
> add sprites
> add cel shader material
...
Expand Down
3 changes: 3 additions & 0 deletions src/shaders/depth/fs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
void main()
{
}
12 changes: 12 additions & 0 deletions src/shaders/depth/vs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
uniform mat4 rotation;
uniform mat4 transform;
uniform mat4 projection;
in vec3 vPos;
//in vec3 vCol;
//out vec3 pos;



void main(){
gl_Position = projection * transform * rotation * vec4(vPos, 1.0);
}
38 changes: 37 additions & 1 deletion src/shaders/solid/fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ uniform int hasHeightMap;
uniform sampler2D heightMap;
uniform float height_scale;


uniform int hasShadow;
uniform sampler2D shadowMap;

// ------ textures end ----

uniform int uNumPointLights;
Expand All @@ -40,10 +44,34 @@ in vec4 worldPos;
in vec2 texPos;
in vec3 tangent;
in mat3 TBN;
in vec4 ogPos;
in vec4 shadowSpacePos;


out vec4 FragColor;
out vec4 BrightColor;

float shadowCalculation(vec4 sPos, sampler2D shadow){
vec3 projCoords = sPos.xyz / sPos.w;
projCoords = projCoords * 0.5 + 0.5;
float closestDepth = texture(shadow, projCoords.xy).r;
float currentDepth = projCoords.z;

float bias = 0.005;
float sha = 0.0;
vec2 texelSize = 1.0 / textureSize(shadow, 0);
for(int x = -1; x <= 1; ++x)
{
for(int y = -1; y <= 1; ++y)
{
float pcfDepth = texture(shadowMap, projCoords.xy + vec2(x, y) * texelSize).r;
sha += currentDepth - bias > pcfDepth ? 1.0 : 0.0;
}
}
sha /= 9.0;
return sha;
}

void main()
{
if(flatColor == 1){
Expand Down Expand Up @@ -78,7 +106,11 @@ void main()
for(int i = 0; i < uNumDirLights; i++){
outColor += calcDirLight(uDirectionalLights[i], normal_val, viewDir, uShininess, specular_val, uDiffuse);
}


if(hasShadow == 1){
outColor -= outColor * shadowCalculation(shadowSpacePos, shadowMap);
}

if (hasTexture == 1){
outColor *= texture(texture0, texCoords_val).xyz;
} else {
Expand Down Expand Up @@ -107,4 +139,8 @@ void main()
} else {
BrightColor = vec4(0, 0, 0,alpha);
}


//FragColor = texture(shadowMap, texPos);
//FragColor = vec4(shad, shad, shad, 1.0);
}
4 changes: 4 additions & 0 deletions src/shaders/solid/vs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ uniform mat4 rotation;
uniform mat4 transform;
uniform mat4 projection;
uniform vec3 uColor;
uniform mat4 shadowSpaceMatrix;

in vec3 vPos;
in vec3 vNormal;
Expand All @@ -15,6 +16,7 @@ out vec2 texPos;
out vec3 tangent;
out mat3 TBN;
out vec4 ogPos;
out vec4 shadowSpacePos;

void main(){
ogPos = vec4(vPos, 1.0);
Expand All @@ -31,5 +33,7 @@ void main(){
vec3 B = normalize(vec3(rotation * vec4(cross(vTangent, vNormal), 0.0)));
vec3 N = normalize(normal);

shadowSpacePos = shadowSpaceMatrix * transform * rotation * ogPos;

TBN = mat3(T, B, N);
}
5 changes: 3 additions & 2 deletions src/tricore/Camera.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Camera.h"
#include "config.h"

#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtc/matrix_transform.hpp>
Expand All @@ -8,8 +9,8 @@

using namespace tri::core;

glm::mat4 Camera::view(float zFar) const{
return glm::perspective(glm::radians(90.0f), w/h, 0.1f, zFar) * glm::lookAt(pos, pos + dir, UP);
glm::mat4 Camera::view() const{
return glm::perspective(glm::radians(90.0f), w/h, config::CAMERA_NEAR_PLANE, config::CAMERA_FAR_PLANE) * glm::lookAt(pos, pos + dir, UP);
}

glm::vec3 Camera::getDir(){
Expand Down
2 changes: 1 addition & 1 deletion src/tricore/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace tri::core {
void poll(GLFWwindow* window);
glm::vec3 getDir();
glm::vec3 getPos();
glm::mat4 view(float zFar = 300.0f) const;
glm::mat4 view() const;

private:
glm::vec3 pos;
Expand Down
Loading

0 comments on commit c54221f

Please sign in to comment.