Back to basic - Just a backpack rendering

This commit is contained in:
Nigel Barink 2023-05-19 20:48:24 +02:00
parent 3acc8525b0
commit 8890b4d973
19 changed files with 210 additions and 409 deletions

View File

@ -12,18 +12,15 @@ void main(){
// Inversion filter
//FragColor = vec4( vec3 (1.0 - texture(screenTexure, TexCoords) ), 1.0) ;
//grayscale
/*
FragColor = texture(screenTexure, TexCoords);
float average = (FragColor.r + FragColor.g + FragColor.b) / 3.0;
FragColor = vec4(average, average, average, 1.0);
*/
// Simple Grayscale
//FragColor = texture(screenTexure, TexCoords);
//float average = (FragColor.r + FragColor.g + FragColor.b) / 3.0;
//FragColor = vec4(average, average, average, 1.0);
// physically accurate grayscale
/* FragColor = texture(screenTexure, TexCoords);
float average = 0.2126 * FragColor.r + 0.7152 * FragColor.g + 0.0722 * FragColor.b;
FragColor = vec4(average,average,average, 1.0);*/
//FragColor = texture(screenTexure, TexCoords);
//float average = 0.2126 * FragColor.r + 0.7152 * FragColor.g + 0.0722 * FragColor.b;
//FragColor = vec4(average,average,average, 1.0);*/
vec2 offset[9] = vec2[] (
vec2(-offset, offset), // top-left
@ -38,38 +35,36 @@ void main(){
);
// sharpen
/*float kernel[9] = float[](
float sharpen_kernel[9] = float[](
-1, -1, -1,
-1, 9, -1,
-1, -1, -1
);*/
);
// blur
/* float kernel[9] = float[](
float blur_kernel[9] = float[](
1.0 /16, 2.0 /16, 1.0 /16,
2.0 /16, 4.0 /16, 2.0 /16,
1.0 /16, 2.0 /16, 1.0 /16
);*/
);
// Edge-detection
/* float kernel[9] = float[] (
float edgeDetection_kernel[9] = float[] (
1, 1, 1,
1, -8, 1,
1, 1, 1
);
vec3 sampleTex[9];
for (int i= 0; i < 9; i++) {
sampleTex[i] = vec3(texture(screenTexure, TexCoords.st + offset[i]));
}
//vec3 sampleTex[9];
//for (int i= 0; i < 9; i++) {
// sampleTex[i] = vec3(texture(screenTexure, TexCoords.st + offset[i]));
//}
vec3 col = vec3(0.0);
for (int i = 0; i < 9; i++) {
col += sampleTex[i] * kernel[i];
}
FragColor = vec4(col, 1.0);
*/
//vec3 col = vec3(0.0);
//for (int i = 0; i < 9; i++) {
// col += sampleTex[i] * kernel[i];
//}
//FragColor = vec4(col, 1.0);
}

View File

@ -2,7 +2,6 @@
layout (location = 0) in vec2 aPos;
layout (location = 1) in vec2 aTexCoords;
out vec2 TexCoords;
void main()

View File

@ -60,4 +60,3 @@ project "LearnOpenGL"
filter "configurations:Release"
defines{"NDEBUG"}
optimize "On"

View File

@ -0,0 +1,9 @@
#pragma once
#include "glm/glm.hpp"
struct Material {
glm::vec3 ambient;
glm::vec3 diffuse;
glm::vec3 specular;
float shininess;
};

14
src/Primitives/Scene.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include <vector>
#include "../model.h"
#include "camera.h"
class Scene {
public:
Camera cameras;
std::vector <Model> entities;
// std::vector<Material> materials;
};

View File

@ -1,7 +1,7 @@
#include "shader.h"
Shader::Shader(const char* vertextPath, const char* fragmentPath)
{
void Shader::Load(const char* vertexPath, const char* fragmentPath) {
// retrieve the vertex / fragment source code from filepath
std::string vertexCode;
std::string fragmentCode;
@ -13,8 +13,8 @@ Shader::Shader(const char* vertextPath, const char* fragmentPath)
try {
std::cout << "Opening VertexShader.. " << vertextPath << std::endl;
vShaderFile.open(vertextPath);
std::cout << "Opening VertexShader.. " << vertexPath << std::endl;
vShaderFile.open(vertexPath);
std::cout << "Opening FragmentShader.. " << fragmentPath << std::endl;
fShaderFile.open(fragmentPath);
@ -89,7 +89,6 @@ Shader::Shader(const char* vertextPath, const char* fragmentPath)
glDeleteShader(vertex);
glDeleteShader(fragment);
}
void Shader::use(){

View File

@ -13,8 +13,8 @@ class Shader
unsigned int ID;// Program ID
// Read and build the shader upon construction
Shader(const char* vertexPath, const char* fragmentPath);
Shader() = default;
void Load(const char* vertexPath, const char* fragmentPath);
// Activate the shader
void use();

View File

@ -1,84 +0,0 @@
#include "OutlinePass.h"
#include "Renderer.h"
#include "../../CheckErrors.h"
OutlinePass::OutlinePass(Model& sceneObject, glm::mat4& model, glm::mat4& projection, Camera& camera)
: RenderPass("Renderpass - Outline", "../Shaders/shader.vs", "../Shaders/outlineshader.fs" ) , m_model(sceneObject), camera(camera), projection(projection), model(model)
{
}
void OutlinePass::Create() {
// Framebuffer outline effect
std::cout << "Create Framebuffer" << std::endl;
m_outlineFrameBuffer = new FrameBuffer();
glCheckError();
m_outlineFrameBuffer->Bind();
glCheckError();
// Create a texture attachment (colour attachment)
std::cout << "Create Texture" << std::endl;
m_colourTexture = CreateTexture(800, 600);
glCheckError();
// attach texture to the frame buffer as a colour attachment
m_outlineFrameBuffer->Attach(*m_colourTexture);
glCheckError();
// Add depth buffer attachment
std::cout << "Create RenderBuffer" << std::endl;
m_outlineRenderBuffer = new RenderBuffer();
glCheckError();
m_outlineRenderBuffer->Bind();
glCheckError();
m_outlineRenderBuffer->UseDepthAndStencil();
glCheckError();
m_outlineRenderBuffer->Unbind();
glCheckError();
// attach depth buffer to our framebuffer
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_outlineRenderBuffer->id);
glCheckError();
m_outlineFrameBuffer->Unbind();
glCheckError();
std::cout << "Success!" << std::endl;
}
OutlinePass::~OutlinePass(){
delete m_colourTexture;
delete m_outlineRenderBuffer;
}
void OutlinePass::Render()
{
std::cout << "Start outline render" << std::endl;
glStencilFunc(GL_NOTEQUAL, 1, 0xFF);
glStencilMask(0x00);
glDisable(GL_DEPTH_TEST);
m_shader.use();
m_shader.setMat4("model", model);
m_shader.setMat4("view", camera.GetViewMatrix());
m_shader.setMat4("projection", projection);
m_shader.setVec3("outlineColor", glm::vec3(0.28, 0.10, 0.26));
model = glm::scale(model, glm::vec3(1.05f,1.05f, 1.05f));
m_shader.setMat4("model", model);
std::cout << "Drawing model" << std::endl;
m_model.Draw(m_shader);
glStencilFunc(GL_ALWAYS, 1, 0xff);
glStencilMask(0xFF);
std::cout << "Finished outline render" << std::endl;
}

View File

@ -1,30 +0,0 @@
#pragma once
#include <glm/glm.hpp>
#include "RenderPass.h"
#include "FrameBuffer.h"
#include "RenderBuffer.h"
#include "../model.h"
#include "../Primitives/texture.h"
#include "../Primitives/camera.h"
class OutlinePass : RenderPass {
public:
void Render() override;
OutlinePass (Model& sceneObject, glm::mat4& model , glm::mat4& projection, Camera& camera) ;
void Create();
~OutlinePass();
private:
FrameBuffer* m_outlineFrameBuffer;
RenderBuffer* m_outlineRenderBuffer;
Texture* m_colourTexture;
glm::mat4 model;
glm::mat4 projection;
Model& m_model;
Camera& camera;
};

View File

@ -1,7 +1,7 @@
#include "RenderBuffer.h"
void RenderBuffer::UseDepthAndStencil (){
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 800, 600); /// NOTE: should be part of creating but kept seperate for now!
void RenderBuffer::UseDepthAndStencil (int width, int height){
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); /// NOTE: should be part of creating but kept seperate for now!
}
void RenderBuffer::Bind()

View File

@ -10,7 +10,7 @@ public:
void Bind();
void Unbind();
void UseDepthAndStencil();
void UseDepthAndStencil(int width, int height);
RenderBuffer();
~RenderBuffer();

View File

@ -1,19 +0,0 @@
#pragma once
#include <string>
#include "../Primitives/shader.h"
class RenderPass {
public :
virtual void Render() = 0;
RenderPass(const std::string& name,const std::string& VertexShaderFile, const std::string& FragmentShaderFile) : m_shader(VertexShaderFile.c_str(), FragmentShaderFile.c_str())
{
std::cout << "Created " << name << std::endl;
}
~RenderPass(){}
protected:
Shader m_shader;
};

View File

@ -9,110 +9,11 @@
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "../Primitives/Scene.h"
void Renderer::Setup()
{
framebuffer.Bind();
shader.Load("../Shaders/Framebuffers.vs", "../Shaders/Framebuffers.fs");
ColourBuffer = CreateTexture(800,600);
framebuffer.Attach(*ColourBuffer);
renderbufferObject.Bind();
renderbufferObject.UseDepthAndStencil();
renderbufferObject.Unbind();
framebuffer.Attach(renderbufferObject);
if(framebuffer.IsComplete() == false ){
std::cout << "ERROR::FRAMEBUFFER::Framebuffer is not complete! " << std::endl;
}
framebuffer.Unbind();
}
void Renderer::Render( )
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Recalculate the Camera's view matrix
view = camera.GetViewMatrix();
// Reset the model matrix to an identity matrix
model = glm::mat4(1.0f);
framebuffer.Bind();
// 1. Skybox pass
skyboxpass.Render();
// 2. Scene pass
scenepass.Render();
// 3. outline pass
outlinepass.Render();
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 4. draw result to screen
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDisable(GL_DEPTH_TEST);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
m_shader.use();
m_shader.setInt("screenTexture",0);
glBindVertexArray(ScreenVAO);
glBindTexture(GL_TEXTURE_2D, ColourBuffer->id);
glDrawArrays(GL_TRIANGLES, 0, 6);
// Reset stencil
glStencilMask(0xFF);
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glEnable(GL_DEPTH_TEST);
}
Renderer::Renderer(Camera& camera, Model& scene) :
camera(camera),
renderbufferObject(),
framebuffer(),
skybox(),
skyboxpass(skybox, camera, projection),
scenepass(skybox, scene,camera,projection, model),
outlinepass(scene, model, projection, camera),
m_shader("../Shaders/Framebuffers.vs", "../Shaders/Framebuffers.fs")
{
outlinepass.Create();
std::vector<std::string> faces = {
"../Textures/skybox/right.jpg",
"../Textures/skybox/left.jpg",
"../Textures/skybox/top.jpg",
"../Textures/skybox/bottom.jpg",
"../Textures/skybox/front.jpg",
"../Textures/skybox/back.jpg"
};
skybox.loadCubeTextures(faces);
// auto outlinepass = OutlinePass(scene , model, projection, camera);
// Create ScreenVAO
glGenVertexArrays(1, &ScreenVAO);
glBindVertexArray(ScreenVAO);
@ -135,6 +36,139 @@ Renderer::Renderer(Camera& camera, Model& scene) :
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glEnable(GL_MULTISAMPLE);
}
void Renderer::resize(int width, int height ) {
framebuffer = FrameBuffer();
framebuffer.Bind();
ColourBuffer = CreateTexture(width, height);
framebuffer.Attach(*ColourBuffer);
renderbufferObject = RenderBuffer();
renderbufferObject.Bind();
renderbufferObject.UseDepthAndStencil(width, height);
framebuffer.Attach(renderbufferObject);
if (framebuffer.IsComplete() == false) {
std::cout << "ERROR::FRAMEBUFFER::Framebuffer is not complete! " << std::endl;
}
renderbufferObject.Unbind();
}
void Renderer::Render(Scene& scene)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glm::mat4 projection = glm::perspective(glm::radians(scene.cameras.Zoom), (float)800 / (float)600, 0.1f, 100.0f);
auto view = scene.cameras.GetViewMatrix();
auto model = glm::mat4(1.0f);
// 1. Skybox pass
/*
Shader cubemap;
cubemap.Load("../Shaders/skybox.vs", "../Shaders/Cubemap.fs");
glDepthMask(GL_FALSE);
cubemap.use();
cubemap.setMat4("projection", projection);
auto centeredView = glm::mat4(glm::mat3(scene.cameras.GetViewMatrix()));
cubemap.setMat4("view", centeredView);
skybox.Bind();
glDrawArrays(GL_TRIANGLES, 0, 36);
skybox.Unbind();
glDepthMask(GL_TRUE);
*/
// 2. Scene pass
Shader blinnPhong;
blinnPhong.Load("../Shaders/shader.vs", "../Shaders/shader.fs");
blinnPhong.use();
blinnPhong.setVec3("cameraPos", scene.cameras.Position);
/*
glActiveTexture(GL_TEXTURE11);
m_skybox.Bind(); // Segmentation Fault HERE!!
glActiveTexture(GL_TEXTURE0);
*/
blinnPhong.setInt("skybox", 11);
blinnPhong.setMat4("model", model);
blinnPhong.setMat4("view", view);
blinnPhong.setMat4("projection", projection);
for (auto entity : scene.entities) {
entity.Draw(blinnPhong);
}
// m_skybox.Unbind();
// 3. outline pass
//Shader OutlineShader;
//OutlineShader.Load("../Shaders/shader.vs", "../Shaders/outlineshader.fs");
/*
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
*/
// 4. draw result to screen
/*glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDisable(GL_DEPTH_TEST);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
shader.use();
shader.setInt("screenTexture",0);
glBindVertexArray(ScreenVAO);
glBindTexture(GL_TEXTURE_2D, ColourBuffer->id);
glDrawArrays(GL_TRIANGLES, 0, 6);
// Reset stencil
glStencilMask(0xFF);
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glEnable(GL_DEPTH_TEST);
*/
}
void Renderer::Shutdown() {
framebuffer.Unbind();
}
Renderer::Renderer() {
// auto outlinepass = OutlinePass(scene , model, projection, camera);
}
Renderer::~Renderer()

View File

@ -5,38 +5,25 @@
#include "../Primitives/shader.h"
#include "FrameBuffer.h"
#include "RenderBuffer.h"
#include "ScenePass.h"
#include "OutlinePass.h"
#include "SkyboxPass.h"
#include "../Primitives/Scene.h"
class Renderer
{
public:
void Render();
void Setup();
Renderer(Camera& camera,Model& scene);
Renderer();
~Renderer();
void Setup();
void resize(int width, int height);
void Render(Scene& scene);
void Shutdown();
private:
ScenePass scenepass;
SkyboxPass skyboxpass;
OutlinePass outlinepass;
Camera& camera;
GLuint ScreenVAO, VBO;
FrameBuffer framebuffer;
RenderBuffer renderbufferObject;
Texture* ColourBuffer;
Skybox skybox;
Shader m_shader;
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 view;
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)800 / (float)600, 0.1f, 100.0f);
Shader shader;
glm::vec3 lightpos = glm::vec3(-0.2f, -1.0f, -0.3f);

View File

@ -1,34 +0,0 @@
#include "ScenePass.h"
void ScenePass::Render()
{
m_shader.use();
m_shader.setVec3("cameraPos", camera.Position);
glActiveTexture(GL_TEXTURE11);
m_skybox.Bind(); // Segmentation Fault HERE!!
glActiveTexture(GL_TEXTURE0);
m_shader.setInt("skybox",11);
m_shader.setMat4("model", model);
m_shader.setMat4("view", camera.GetViewMatrix());
m_shader.setMat4("projection", projection);
m_scene.Draw(m_shader);
m_skybox.Unbind();
}
void ScenePass::SetSkybox(Skybox& skybox)
{
m_skybox = skybox;
}

View File

@ -1,33 +0,0 @@
#pragma once
#include "RenderPass.h"
#include "../Primitives/Skybox.h"
#include "../Primitives/shader.h"
#include "../Primitives/camera.h"
#include "../model.h"
class ScenePass : public RenderPass {
public:
void Render() override;
ScenePass(Skybox& skybox, Model& scene, Camera& camera, glm::mat4 projection , glm::mat4 model)
: RenderPass("RenderPass - Scene", "../Shaders/shader.vs", "../Shaders/shader.fs" ),
m_scene(scene), camera(camera), projection(projection), model(model), m_skybox(skybox)
{
}
~ScenePass() = default;
void SetSkybox(Skybox& skybox);
private:
Skybox& m_skybox;
Model& m_scene;
Camera& camera;
glm::mat4 projection;
glm::mat4 model;
};

View File

@ -1,32 +0,0 @@
#include "SkyboxPass.h"
SkyboxPass::SkyboxPass(Skybox& skybox, Camera& camera, glm::mat4& projection) : RenderPass("Render Pass - Skybox", "../Shaders/skybox.vs", "../Shaders/Cubemap.fs"),
skybox(skybox),
camera(camera),
projection(projection)
{
}
void SkyboxPass::Render()
{
glDepthMask(GL_FALSE);
m_shader.use();
m_shader.setMat4("projection", projection);
glm::mat4 centeredView = glm::mat4(glm::mat3(camera.GetViewMatrix()));
m_shader.setMat4("view", centeredView);
skybox.Bind();
glDrawArrays(GL_TRIANGLES, 0, 36);
skybox.Unbind();
glDepthMask(GL_TRUE);
}

View File

@ -1,20 +0,0 @@
#pragma once
#include "RenderPass.h"
#include "../Primitives/Skybox.h"
#include "../Primitives/shader.h"
#include "../Primitives/camera.h"
class SkyboxPass : public RenderPass {
public:
void Render() override;
SkyboxPass( Skybox& skybox, Camera& camera, glm::mat4& projection);
~SkyboxPass() = default;
private:
Skybox& skybox;
Camera& camera;
glm::mat4& projection;
};

View File

@ -14,14 +14,14 @@
#include <stb_image.h>
#include "Application.h"
#include "Primitives/Scene.h"
void processInput( GLFWwindow* window);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
const int WIDTH = 800, HEIGHT = 600;
float deltaTime = 0.0f; ; // Time between current frame and last frame
float lastFrame = 0.0f; // Time of last frame
bool firstMouse = true;
@ -35,7 +35,7 @@ public:
void Run () override
{
// Create a window
Window window(800,600, "LearnOpenGL");
Window window(WIDTH,HEIGHT, "LearnOpenGL");
IMGUI_CHECKVERSION();
ImGui::CreateContext();
@ -48,10 +48,27 @@ public:
ImGui_ImplOpenGL3_Init("#version 460");
stbi_set_flip_vertically_on_load(true);
Model backpack("../Models/backpack.obj");
Renderer renderer = Renderer(camera, backpack);
Scene scene;
scene.entities.push_back(backpack);
scene.cameras = camera;
std::vector<std::string> faces = {
"../Textures/skybox/right.jpg",
"../Textures/skybox/left.jpg",
"../Textures/skybox/top.jpg",
"../Textures/skybox/bottom.jpg",
"../Textures/skybox/front.jpg",
"../Textures/skybox/back.jpg"
};
Skybox skybox = Skybox();
skybox.loadCubeTextures(faces);
Renderer renderer = Renderer();
renderer.resize(WIDTH, HEIGHT);
renderer.Setup();
@ -65,7 +82,7 @@ public:
glfwPollEvents();
processInput(window.ptr());
renderer.Render();
renderer.Render(scene);
ImGui_ImplOpenGL3_NewFrame();