Skip to content

Commit

Permalink
setup for postprocessing
Browse files Browse the repository at this point in the history
  • Loading branch information
laudominik committed Jun 19, 2024
1 parent d689356 commit 5ac7fb6
Show file tree
Hide file tree
Showing 17 changed files with 336 additions and 94 deletions.
5 changes: 4 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ add_executable(tri_scene1 scene1.cpp)
target_link_libraries(tri_scene1 PUBLIC tricore triutil glm glad glfw)

add_executable(tri_scene2 scene2.cpp)
target_link_libraries(tri_scene2 PUBLIC tricore triutil glm glad glfw)
target_link_libraries(tri_scene2 PUBLIC tricore triutil glm glad glfw)

add_executable(tri_scene3 scene3.cpp)
target_link_libraries(tri_scene3 PUBLIC tricore triutil glm glad glfw)
33 changes: 3 additions & 30 deletions examples/scene1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,8 @@
> outlines
> skybox
*/

#include <glad/glad.h>
#include "glfwinclude.h"
#include <glm/mat4x4.hpp>
#include <glm/vec3.hpp>
#include <glm/vec2.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <stdlib.h>
#include <stdio.h>
#include <tricore/Renderer.h>
#include <tricore/program/Program.h>
#include <tricore/program/materials/SolidMaterial.h>
#include <tricore/program/materials/LightMaterial.h>
#include <tricore/program/materials/SkyboxMaterial.h>
#include <tricore/program/materials/DebugMaterial.h>
#include <tricore/model/meshes/Cube.h>
#include <tricore/model/meshes/Plane.h>
#include <tricore/model/meshes/Sphere.h>

#include <tricore/model/meshes/VertexCube.h>
#include <triutil/files.h>
#include <iostream>
#include <tricore/model/Mesh.h>
#include <tricore/Camera.h>
#include <tricore/model/Model.h>
#include <triutil/window_init.h>
#include <tricore/texture/Texture.h>
#include <tricore/program/materials/TextureMaterial.h>
#include <tricore/texture/Cubemap.h>
#include <tricore/tricore.h>
#include <triutil/triutil.h>

using namespace tri::core;
using namespace tri::core::materials;
Expand All @@ -47,7 +20,7 @@ int main(void){
static constexpr auto width = 640;
static constexpr auto height = 480;

Window window("example", width, height);
Window window("scene1", width, height);

Camera camera(width, height, {0, 0, 3.0}, {0, 0, -1.0f});
auto model = std::make_shared<Model>();
Expand Down
26 changes: 3 additions & 23 deletions examples/scene2.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
/*
this scene shows tesselation
*/
#include <glad/glad.h>
#include "glfwinclude.h"
#include <glm/mat4x4.hpp>
#include <glm/vec3.hpp>
#include <glm/vec2.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <tricore/Renderer.h>
#include <tricore/program/Program.h>
#include <tricore/program/materials/LightMaterial.h>
#include <tricore/program/materials/SkyboxMaterial.h>
#include <tricore/model/meshes/Cube.h>
#include <tricore/model/meshes/Sphere.h>

#include <triutil/files.h>
#include <tricore/model/Mesh.h>
#include <tricore/Camera.h>
#include <tricore/model/Model.h>
#include <triutil/window_init.h>
#include <tricore/texture/Texture.h>
#include <tricore/program/materials/TextureMaterial.h>
#include <tricore/texture/Cubemap.h>
#include <tricore/model/HeightmapModel.h>
#include <tricore/tricore.h>
#include <triutil/triutil.h>

using namespace tri::core;
using namespace tri::core::materials;
Expand Down Expand Up @@ -56,7 +36,7 @@ int main(void){
}))
.add([](){
auto heightmapModel = std::make_shared<HeightmapModel>(std::move(Texture("examples/data/textures/iceland_heightmap.png")), 0.5);
heightmapModel->setScaleXYZ({30, 30, 30}).setTranslation({0, -4.0, 0.0});
heightmapModel->setScaleXYZ({100, 100, 100}).setTranslation({0, -4.0, 0.0});

return heightmapModel;
}())
Expand Down
102 changes: 102 additions & 0 deletions examples/scene3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <tricore/tricore.h>
#include <triutil/triutil.h>


using namespace tri::core;
using namespace tri::core::materials;
using namespace tri::core::meshes;
using namespace tri::util;

static constexpr auto width = 480;
static constexpr auto height = 480;

int main(){
Window window("scene3", width, height);
Camera camera(width, height, {0, 0, 3.0}, {0, 0, -1.0f});

Renderer renderer(*window.get(), camera);

auto light = light::make_point({
.pos = {0, 0, -4.0},
.color = {1, 1, 1},
.attentuation = {
.constant = 1.0,
.linear = 0.0,
.quadratic = 0.0
}
});

static constexpr glm::vec3 secondLightPos = {-10, 1, -10};

auto lightModel = std::make_shared<Model>();
lightModel->setMesh(Sphere())
.setMaterial<LightMaterial>(glm::vec3{1.0, 1.0, 1.0})
.setTranslation({0, 4.0, -4.0})
.setScaleXYZ({0.3, 0.3, 0.3});

renderer
.setSkybox(Cubemap({
.right = "examples/data/skybox/space/right.png",
.left = "examples/data/skybox/space/left.png",
.top = "examples/data/skybox/space/top.png",
.bottom = "examples/data/skybox/space/bottom.png",
.front = "examples/data/skybox/space/front.png",
.back = "examples/data/skybox/space/back.png"
}))
.add([](){
auto cubeModel = std::make_shared<Model>();
cubeModel->setMesh(VertexCube())
.setMaterial<SolidMaterial>(glm::vec3{1.0, 0.0, 0.0})
.setTranslation({-7, -0.5, -10});
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});
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(lightModel)
.add([](){
auto floor = std::make_shared<Model>();
floor->setMesh(Plane())
.setMaterial(TextureMaterialBuilder()
.setShininess(1024)
.setTexture(std::move(Texture("examples/data/textures/brickwall.jpg")))
.setNormalMap(std::move(Texture("examples/data/textures/brickwall_normal.jpg")))
.build()
).setScaleXYZ({20, 20, 20})
.setTranslation({-5, -2, -10});
return floor;
}())
.addLightSource(light)
.addLightSource(light::make_point({
.pos = secondLightPos,
.color = {1, 1, 1},
.attentuation = {
.constant = 1.0,
.linear = 0.1,
.quadratic = 0.0
}
}));

static constexpr auto R = 5.0;
while (!glfwWindowShouldClose(window.get()))
{
renderer.render();
camera.poll(window.get());
auto theta = glfwGetTime();
glm::vec3 pos = glm::vec3{R * sin(theta), 3.0, R * cos(theta)};
light->pos = pos;
lightModel->setTranslation(pos);
}
}
41 changes: 7 additions & 34 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,46 +1,19 @@
#include <glad/glad.h>
#include "glfwinclude.h"
#include <glm/mat4x4.hpp>
#include <glm/vec3.hpp>
#include <glm/vec2.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <stdlib.h>
#include <stdio.h>
#include <tricore/Renderer.h>
#include <tricore/program/Program.h>
#include <tricore/program/materials/SolidMaterial.h>
#include <tricore/program/materials/LightMaterial.h>
#include <tricore/program/materials/SkyboxMaterial.h>
#include <tricore/program/materials/DebugMaterial.h>
#include <tricore/model/meshes/Cube.h>
#include <tricore/model/meshes/Plane.h>
#include <tricore/model/meshes/Sphere.h>

#include <tricore/model/meshes/VertexCube.h>
#include <triutil/files.h>
#include <iostream>
#include <tricore/model/Mesh.h>
#include <tricore/Camera.h>
#include <tricore/model/Model.h>
#include <triutil/window_init.h>
#include <tricore/texture/Texture.h>
#include <tricore/program/materials/TextureMaterial.h>
#include <tricore/texture/Cubemap.h>

/*
small TODO list:
> add heightmap model
> add postprocessing operation
> add sprites
> add shadows
> add hdr
> add bloom
> split to examples
> add sprites
> add cel shader material
...
> add gui
> add entity tree
*/
> add hdr
>dynamic lib for scenes (in gui) ?
*/

int main(void){
/*
Expand Down
10 changes: 10 additions & 0 deletions src/shaders/postprocess/identity/fs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
out vec4 FragColor;

in vec2 TexCoords;

uniform sampler2D screenTexture;

void main()
{
FragColor = texture(screenTexture, TexCoords);
}
10 changes: 10 additions & 0 deletions src/shaders/postprocess/identity/vs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
in vec2 aPos;
in vec2 aTexCoords;

out vec2 TexCoords;

void main()
{
gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
TexCoords = aTexCoords;
}
1 change: 1 addition & 0 deletions src/tricore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ ${SOURCE_DIR}/tricore/Renderer.cpp
${SOURCE_DIR}/tricore/texture/Texture.cpp
${SOURCE_DIR}/tricore/texture/Cubemap.cpp
${SOURCE_DIR}/tricore/program/shaderError.cpp
${SOURCE_DIR}/tricore/frame/Frame.cpp
)
target_link_libraries(tricore PUBLIC glad triutil fmt glm glfw stb Boost::filesystem)
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 = 100.0f) const;
glm::mat4 view(float zFar = 300.0f) const;

private:
glm::vec3 pos;
Expand Down
60 changes: 57 additions & 3 deletions src/tricore/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <fmt/format.h>

#include <tricore/program/materials/SkyboxMaterial.h>
#include <tricore/model/meshes/Plane.h>
#include <algorithm>
#include <limits>
#include <boost/range/adaptor/reversed.hpp>
Expand All @@ -14,6 +15,12 @@ void Renderer::render(){
int width, height;
glfwGetFramebufferSize(&window, &width, &height);
glViewport(0, 0, width, height);

setupFBs(width, height);
windowWidth = width;
windowHeight = height;

glBindFramebuffer(GL_FRAMEBUFFER, 0);

glClearColor(0, 0, 0, 1.0f);
glStencilMask(0xFF);
Expand All @@ -25,25 +32,72 @@ void Renderer::render(){

glDepthFunc(GL_LESS);

// blending requirement
/*
* TODO: shadow mapping
*/
// renderShadows()

renderModels();
renderModelsWithAlpha();

postprocess();

glfwSwapBuffers(&window);
glfwPollEvents();
}

void Renderer::setupFBs(int width, int height){
if(width != windowWidth || height != windowHeight){
renderFrame.setup(width, height);
}

renderFrame.bind();
}

void Renderer::cleanupFBs(){

}

void Renderer::postprocess(){
glDisable(GL_DEPTH_TEST);
//glClear(GL_COLOR_BUFFER_BIT);
renderFrame.unbind();

postprocessOp->setup(renderFrame);
framePlane.draw(camera, *postprocessOp);

glEnable(GL_DEPTH_TEST);


}


Renderer::~Renderer(){
cleanupFBs();
}

Renderer& Renderer::wireframe(){
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
return *this;
}

Renderer& Renderer::culling(){
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
glFrontFace(GL_CW);
return *this;
}



void Renderer::setupLights(const Program& material){
material.uniformVec3("viewDir", camera.getDir());
material.uniformVec3("ambientColor", ambientLight->color);
material.uniformFloat("ambientIntensity", ambientLight->intensity);

if(ambientLight){
material.uniformVec3("ambientColor", ambientLight->color);
material.uniformFloat("ambientIntensity", ambientLight->intensity);
}

material.uniformInt("uNumPointLights", pointLights.size());
for(auto i = 0u; i < pointLights.size(); i++){
const auto& light = pointLights[i];
Expand Down
Loading

0 comments on commit 5ac7fb6

Please sign in to comment.