Nove repo opengl-learning

This commit is contained in:
TheRetikGM
2021-02-12 15:43:03 +01:00
commit 63e3c0bc8b
1046 changed files with 358397 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/../../../include",
"${workspaceFolder}/build/src",
"${workspaceFolder}/src/include"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++14",
"intelliSenseMode": "clang-x64",
"compileCommands": "${workspaceFolder}/build/compile_commands.json"
}
],
"version": 4
}

View File

@@ -0,0 +1,64 @@
cmake_minimum_required(VERSION 3.13.4)
project("main")
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Configs" FORCE)
if (NOT DEFINED CMAKE_BUILD_TYPE)
message(STATUS "Setting build type to 'Debug' as none was specified.")
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${CMAKE_CONFIGURATION_TYPES})
endif()
set(CMAKE_DEBUG_POSTFIX "d")
add_subdirectory("src")
target_include_directories("${PROJECT_NAME}"
PUBLIC "${CMAKE_SOURCE_DIR}/../../../include"
)
target_link_directories("${PROJECT_NAME}"
PRIVATE "${CMAKE_SOURCE_DIR}/../../../lib"
PRIVATE "${CMAKE_SOURCE_DIR}/../../../lib/Release"
PRIVATE "${CMAKE_SOURCE_DIR}/../../../lib/Debug"
)
target_link_libraries("${PROJECT_NAME}"
camera
shader
glfbo
glml
)
if (MSVC)
set(AssimpLibs
debug Debug/assimp-vc142-mtd optimized Release/assimp-vc142-mt
debug Debug/IrrXMLd optimized Release/IrrXML
debug Debug/zlibstaticd optimized Release/zlibstatic)
set (glfw3Lib debug Debug/glfw3d optimized Release/glfw3)
target_link_libraries("${PROJECT_NAME}"
opengl32
${AssimpLibs}
${glfw3Lib}
)
elseif (UNIX)
set(ASSIMP
debug assimpd optimized assimp
debug IrrXMLd optimized IrrXML
debug zlibstaticd optimized zlibstatic)
set(GLFW debug glfw3d optimized glfw3)
target_link_libraries("${PROJECT_NAME}"
${GLFW}
${ASSIMP}
dl
m
Xi
pthread
Xrandr
X11
GL
GLU
)
endif()

View File

@@ -0,0 +1,3 @@
#!/bin/bash
cmake --build build

View File

@@ -0,0 +1,9 @@
#!/bin/bash
if [[ $1 == "-d" ]]; then
echo "Debug configuration"
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
else
echo "Release configuration"
cmake -S . -B build
fi

View File

@@ -0,0 +1,5 @@
#!/bin/bash
cd build/src/
exec ./main
#cd ../../

View File

@@ -0,0 +1,40 @@
set(REPO_ROOT "${CMAKE_SOURCE_DIR}/../../..")
set(PROJECT_DIR "${CMAKE_SOURCE_DIR}")
set(SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(SOURCE_INCLUDE "${SOURCE_DIR}/include")
set(BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
include_directories(
"${CMAKE_CURRENT_BINARY_DIR}"
"${REPO_ROOT}/include"
"${SOURCE_INCLUDE}"
)
add_library(camera Camera.cpp include/Camera.h)
add_library(shader Shader.cpp include/Shader.h)
add_subdirectory("glml")
add_subdirectory("glfbo")
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(DEBUG 1)
endif()
configure_file(config.h.in config.h @ONLY)
file(GLOB SHADERS
"shaders/*.frag"
"shaders/*.vert"
"shaders/*.geom"
)
add_executable("${PROJECT_NAME}"
main.cpp
"${REPO_ROOT}/glad.c"
"${REPO_ROOT}/stb_image.cpp"
"${REPO_ROOT}/stb_image_write.cpp"
include/DebugColors.h
"${BINARY_DIR}/config.h"
"${SHADERS}"
)
source_group(TREE ${SOURCE_DIR} PREFIX "" FILES ${SHADERS})

View File

@@ -0,0 +1,83 @@
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
Camera::Camera(glm::vec3 position, glm::vec3 worldUp, glm::vec3 worldFront, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(2.5f), MouseSensitivity(0.1f), FOV(45.0f), Mode(CAM_FLOAT)
{
this->Position = position;
this->WorldUp = worldUp;
this->Yaw = yaw;
this->Pitch = pitch;
this->WorldFront = worldFront;
this->WorldRight = glm::normalize(glm::cross(this->WorldFront, this->WorldUp));
updateCameraVectors();
}
void Camera::updateCameraVectors()
{
glm::vec3 front;
front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
front.y = sin(glm::radians(Pitch));
front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
Front = glm::normalize(front);
Right = glm::normalize(glm::cross(Front, WorldUp));
Up = glm::normalize(glm::cross(Right, Front));
}
glm::mat4 Camera::getViewMatrix()
{
return glm::lookAt(Position, Position + Front, WorldUp);
}
void Camera::ProccessKeyboard(camera_movement direction, float deltaTime)
{
switch (direction)
{
case CAM_FORWARD:
Position += deltaTime * MovementSpeed * Front;
break;
case CAM_BACKWARD:
Position -= deltaTime * MovementSpeed * Front;
break;
case CAM_LEFT:
Position -= deltaTime * MovementSpeed * Right;
break;
case CAM_RIGHT:
Position += deltaTime * MovementSpeed * Right;
break;
case CAM_UP:
Position += deltaTime * MovementSpeed * WorldUp;
break;
case CAM_DOWN:
Position -= deltaTime * MovementSpeed * WorldUp;
break;
}
}
void Camera::ProccessMouse(float xoffset, float yoffset, bool constrainPitch)
{
xoffset *= MouseSensitivity;
yoffset *= MouseSensitivity;
Yaw = fmodf(Yaw + xoffset, 360.0f);
Pitch += yoffset;
if (constrainPitch)
{
if (Pitch > 89.0f)
Pitch = 89.0f;
else if (Pitch < -89.0f)
Pitch = -89.0f;
}
updateCameraVectors();
}
void Camera::ProccessScroll(float yoffset)
{
FOV -= yoffset;
if (FOV < 1.0f)
FOV = 1.0f;
else if (FOV > 45.0f)
FOV = 45.0f;
}
void Camera::SetCameraMode(camera_mode mode)
{
Mode = mode;
}

View File

@@ -0,0 +1,179 @@
#include "Shader.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <glad/glad.h>
#include <glm/gtc/type_ptr.hpp>
#include "DebugColors.h"
Shader::Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath, const char* geometryShaderSourcePath, bool directSource)
{
const char* vertexSource;
const char* fragmentSource;
const char* geometrySource;
std::string v, f, g;
if (!directSource)
{
v = readFile(vertexShaderSourcePath);
f = readFile(fragmentShaderSourcePath);
if (v == "" || f == "") {
this->Program = 0;
return;
}
vertexSource = v.c_str();
fragmentSource = f.c_str();
if (geometryShaderSourcePath != NULL)
{
g = readFile(geometryShaderSourcePath);
if (g == "") {
this->Program = 0;
return;
}
geometrySource = g.c_str();
}
else
geometrySource = NULL;
}
else
{
vertexSource = vertexShaderSourcePath;
fragmentSource = fragmentShaderSourcePath;
geometrySource = geometryShaderSourcePath;
}
int success;
char infoLog[512];
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, NULL);
glCompileShader(vertexShader);
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cerr << "Shader.h: " DC_WARNING " Vertex shader compilation failed!\n";
std::cerr << "shader.h: Path: " << vertexShaderSourcePath << '\n' << infoLog << std::endl;
this->Program = 0;
return;
}
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cerr << "Shader.h: " DC_WARNING " Fragment shader compilation failed!\n";
std::cerr << "shader.h: Path: " << fragmentShaderSourcePath << '\n' << infoLog << std::endl;
this->Program = 0;
return;
}
unsigned int geometryShader;
if (geometrySource)
{
geometryShader = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(geometryShader, 1, &geometrySource, NULL);
glCompileShader(geometryShader);
glGetShaderiv(geometryShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(geometryShader, 512, NULL, infoLog);
std::cerr << "Shader.h: " DC_WARNING " Geometry shader compilation failed!\n";
std::cerr << "Shader.h: Path: " << geometryShaderSourcePath;
std::cerr << std::endl << infoLog << std::endl;
this->Program = 0;
return;
}
}
Program = glCreateProgram();
glAttachShader(Program, vertexShader);
glAttachShader(Program, fragmentShader);
if (geometrySource)
glAttachShader(Program, geometryShader);
glLinkProgram(Program);
glGetProgramiv(Program, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(Program, 512, NULL, infoLog);
std::cerr << "shader.h: " DC_ERROR " Program linking failed\n" << infoLog << std::endl;
this->Program = 0;
return;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
Shader::Shader(std::string vertexShaderSourcePath,
std::string fragmentShaderSourcePath,
std::string geometryShaderSource,
bool directSource) : Shader(vertexShaderSourcePath.c_str(), fragmentShaderSourcePath.c_str(), geometryShaderSource.c_str(), directSource)
{
}
Shader::~Shader()
{
glDeleteProgram(Program);
}
std::string Shader::readFile(const char* path)
{
std::ifstream ifs(path); // input file stream
if (ifs.fail())
{
std::cout << "shader.h: " DC_ERROR " Could not open file '" << path << "'\n";
return "";
}
std::stringstream buffer;
buffer << ifs.rdbuf();
std::string out = buffer.str();
ifs.close();
return out;
}
void Shader::Use() const
{
glUseProgram(Program);
}
void Shader::setBool(const std::string name, bool value) const
{
glUniform1i(glGetUniformLocation(Program, name.c_str()), value ? 1 : 0);
}
void Shader::setFloat(const std::string name, float value) const
{
glUniform1f(glGetUniformLocation(Program, name.c_str()), value);
}
void Shader::setInt(const std::string name, int value) const
{
glUniform1i(glGetUniformLocation(Program, name.c_str()), value);
}
void Shader::setMat4(const std::string name, glm::mat4 value) const
{
glUniformMatrix4fv(glGetUniformLocation(Program, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
}
void Shader::setVec3(const std::string name, glm::vec3 value) const
{
setVec3(name, value.x, value.y, value.z);
}
void Shader::setVec3(const std::string name, float v0, float v1, float v2) const
{
glUniform3f(glGetUniformLocation(Program, name.c_str()), v0, v1, v2);
}
void Shader::setMat3(const std::string name, glm::mat3 value) const
{
glUniformMatrix3fv(glGetUniformLocation(Program, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
}
void Shader::setVec2(const std::string name, float v0, float v1) const
{
glUniform2f(glGetUniformLocation(Program, name.c_str()), v0, v1);
}
void Shader::setVec2(const std::string name, glm::vec2 v) const
{
setVec2(name, v.x, v.y);
}

View File

@@ -0,0 +1,6 @@
#pragma once
#cmakedefine REPO_ROOT "@REPO_ROOT@"
#cmakedefine PROJECT_DIR "@PROJECT_DIR@"
#cmakedefine SOURCE_DIR "@SOURCE_DIR@"
#cmakedefine BINARY_DIR "@BINARY_DIR@"
#cmakedefine DEBUG

View File

@@ -0,0 +1,10 @@
add_library(glfbo
ColorBuffer.cpp
DepthBuffer.cpp
StencilBuffer.cpp
DepthStencilBuffer.cpp
glfbo.cpp
Framebuffer.cpp
MSFramebuffer.cpp
${SOURCE_INCLUDE}/glfbo.h
)

View File

@@ -0,0 +1,106 @@
#include "glfbo.h"
#include "config.h"
#include "DebugColors.h"
#include <iostream>
using namespace glfbo;
ColorBuffer::ColorBuffer(unsigned int width, unsigned int height,
BufferType type,
GLint internal_format,
unsigned int samples,
GLint wrap_s,
GLint wrap_t,
GLint wrap_r,
vector3 bordercolor) : Width(width), Height(height), Type(type), Samples(samples), int_format(internal_format)
{
#ifdef DEBUG
if (samples == 1)
std::cout << DC_WARNING << " ColorBuffer: samples == 1 will not create multisampled color buffer.\n";
#endif
if (type == BufferType::Texture) {
glGenTextures(1, &ID);
GLint target;
if (samples > 1) {
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, ID);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, internal_format, width, height, GL_TRUE);
target = GL_TEXTURE_2D_MULTISAMPLE;
}
else {
Samples = 0;
glBindTexture(GL_TEXTURE_2D, ID);
glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, GL_RGBA, GL_FLOAT, 0);
target = GL_TEXTURE_2D;
}
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, (float *)&bordercolor);
glBindTexture(target, 0);
}
else if (type == BufferType::Renderbuffer) {
glGenRenderbuffers(1, &ID);
glBindRenderbuffer(GL_RENDERBUFFER, ID);
if (samples <= 1) {
samples = 0;
}
glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, internal_format, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
}
else if (type == BufferType::Cubemap) {
glGenTextures(GL_TEXTURE_CUBE_MAP, &ID);
glBindTexture(GL_TEXTURE_CUBE_MAP, ID);
for (int i = 0; i < 6; i++)
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internal_format, width , height, 0, GL_RGBA, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, wrap_s);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, wrap_t);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, wrap_r);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}
else {
ID = 0;
}
}
ColorBuffer::ColorBuffer(unsigned int width, unsigned int height,
BufferType type,
GLint internal_format,
unsigned int samples,
GLint wrap_str,
vector3 bordercolor)
: ColorBuffer(width, height, type, internal_format, samples, wrap_str, wrap_str, wrap_str, bordercolor) {}
ColorBuffer::~ColorBuffer()
{
if (Type == BufferType::Cubemap || Type == BufferType::Texture)
glDeleteTextures(1, &ID);
else
glDeleteRenderbuffers(1, &ID);
}
GLenum ColorBuffer::BindTarget()
{
if (Type == BufferType::Cubemap) {
return GL_TEXTURE_CUBE_MAP;
}
else if (Type == BufferType::Texture) {
if (Samples > 1)
return GL_TEXTURE_2D_MULTISAMPLE;
else
return GL_TEXTURE_2D;
}
else {
return GL_RENDERBUFFER;
}
}
GLint ColorBuffer::Get_internal_format()
{
return int_format;
}

View File

@@ -0,0 +1,104 @@
#include "glfbo.h"
#include "config.h"
#include "DebugColors.h"
#include <iostream>
using namespace glfbo;
DepthBuffer::DepthBuffer(unsigned int width, unsigned int height,
BufferType type,
GLint internal_format,
unsigned int samples,
GLint wrap_s,
GLint wrap_t,
GLint wrap_r,
vector3 bordercolor) : Width(width), Height(height), Samples(samples), Type(type), int_format(internal_format)
{
#ifdef DEBUG
if (samples == 1)
std::cout << DC_WARNING << " DepthBuffer: samples == 1 will not create multisampled depth buffer.\n";
#endif
if (type == BufferType::Texture) {
glGenTextures(1, &ID);
GLint target;
if (samples > 1) {
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, ID);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, internal_format, width, height, GL_TRUE);
target = GL_TEXTURE_2D_MULTISAMPLE;
}
else {
Samples = 0;
glBindTexture(GL_TEXTURE_2D, ID);
glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width , height, 0, GL_RGBA, GL_FLOAT, 0);
target = GL_TEXTURE_2D;
}
glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, (float*)&bordercolor);
glBindTexture(target, 0);
}
else if (type == BufferType::Renderbuffer) {
glGenRenderbuffers(1, &ID);
glBindRenderbuffer(GL_RENDERBUFFER, ID);
if (samples <= 1) {
samples = 0;
}
glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, internal_format, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
}
else if (type == BufferType::Cubemap) {
glGenTextures(1, &ID);
glBindTexture(GL_TEXTURE_CUBE_MAP, ID);
for (unsigned int i = 0; i < 6; i++)
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internal_format, Width, Height, 0,
GL_RGBA, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}
else {
ID = 0;
}
}
DepthBuffer::DepthBuffer(unsigned int width, unsigned int height,
BufferType type,
GLint internal_format,
unsigned int samples,
GLint wrap_str,
vector3 bordercolor)
: DepthBuffer(width, height, type, internal_format, samples, wrap_str, wrap_str, wrap_str, bordercolor) {}
DepthBuffer::~DepthBuffer()
{
if (Type == BufferType::Cubemap || Type == BufferType::Texture)
glDeleteTextures(1, &ID);
else
glDeleteRenderbuffers(1, &ID);
}
GLenum DepthBuffer::BindTarget()
{
if (Type == BufferType::Cubemap) {
return GL_TEXTURE_CUBE_MAP;
}
else if (Type == BufferType::Texture) {
if (Samples > 1)
return GL_TEXTURE_2D_MULTISAMPLE;
else
return GL_TEXTURE_2D;
}
else {
return GL_RENDERBUFFER;
}
}
GLint DepthBuffer::Get_internal_format()
{
return int_format;
}

View File

@@ -0,0 +1,98 @@
#include "glfbo.h"
#include "config.h"
#include "DebugColors.h"
#include <iostream>
using namespace glfbo;
DepthStencilBuffer::DepthStencilBuffer(unsigned int width, unsigned int height,
BufferType type,
GLint internal_format,
unsigned int samples,
GLint wrap_s,
GLint wrap_t,
GLint wrap_r,
vector3 bordercolor) : Width(width), Height(height), Type(type), Samples(samples), int_format(internal_format)
{
#ifdef DEBUG
if (samples == 1)
std::cout << DC_WARNING << "DepthBuffer: samples == 1 will not create multisampled depth buffer.\n";
#endif
if (type == BufferType::Cubemap)
{
#ifdef DEBUG
std::cout << DC_WARNING << " DepthStencilBuffer: Bad buffer type. Using BufferType::Renderbuffer.\n";
#endif
type = Type = BufferType::Renderbuffer;
}
if (type == BufferType::Renderbuffer) {
glGenRenderbuffers(1, &ID);
glBindRenderbuffer(GL_RENDERBUFFER, ID);
if (samples <= 1) {
samples = 0;
}
glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, internal_format, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
}
else {
glGenTextures(1, &ID);
GLint target;
if (samples > 1) {
target = GL_TEXTURE_2D_MULTISAMPLE;
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, internal_format, width, height, GL_TRUE);
}
else {
target = GL_TEXTURE_2D;
glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, GL_RGBA, GL_FLOAT, 0);
}
glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, (float*)&bordercolor);
glBindTexture(target, 0);
}
}
DepthStencilBuffer::DepthStencilBuffer(unsigned int width, unsigned int height,
BufferType type,
GLint internal_format,
unsigned int samples,
GLint wrap_str,
vector3 bordercolor)
: DepthStencilBuffer(width, height, type, internal_format, samples, wrap_str, wrap_str, wrap_str, bordercolor) {}
DepthStencilBuffer::~DepthStencilBuffer()
{
if (Type == BufferType::Renderbuffer)
glDeleteRenderbuffers(1, &ID);
else
glDeleteTextures(1, &ID);
}
GLenum DepthStencilBuffer::BindTarget()
{
if (Type == BufferType::Renderbuffer)
return GL_RENDERBUFFER;
else if (Type == BufferType::Texture)
{
if (Samples > 1)
return GL_TEXTURE_2D_MULTISAMPLE;
else
return GL_TEXTURE_2D;
}
else {
#ifdef DEBUG
std::cout << DC_WARNING << " DepthStencilBuffer::BindTarget(): Bad buffer type. Returning GL_RENDERBUFFER.\n";
#endif
return GL_RENDERBUFFER;
}
}
GLint DepthStencilBuffer::Get_internal_format()
{
return int_format;
}

View File

@@ -0,0 +1,337 @@
#include <iostream>
#include "glfbo.h"
#include "DebugColors.h"
#include "config.h"
#include <stb_image_write.h>
using namespace glfbo;
Framebuffer::Framebuffer(std::unordered_map<unsigned int, ColorBuffer*> cBufs, DepthBuffer* dBuf, StencilBuffer* sBuf) : DepthBuf(NULL), StencilBuf(NULL), DepthStencilBuf(NULL), ID(0)
{
glGenFramebuffers(1, &ID);
glBindFramebuffer(GL_FRAMEBUFFER, ID);
if (cBufs.size() == 0 && !dBuf && !sBuf) {
#ifdef DEBUG
std::cout << DC_WARNING << " Framebuffer::Framebuffer: No buffers specified. Framebuffer should have at least 1 buffer attached.\n";
#endif
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return;
}
for (auto i : cBufs)
Attach_ColorBuffer(i.first, i.second);
if (dBuf)
Attach_DepthBuffer(dBuf);
if (sBuf)
Attach_StencilBuffer(sBuf);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
Framebuffer::Framebuffer(unsigned int attachment, ColorBuffer* cBuf, DepthBuffer* dBuf, StencilBuffer* sBuf) : DepthBuf(NULL), StencilBuf(NULL), DepthStencilBuf(NULL), ID(0)
{
glGenFramebuffers(1, &ID);
glBindFramebuffer(GL_FRAMEBUFFER, ID);
if (!cBuf && !dBuf && !sBuf) {
#ifdef DEBUG
std::cout << DC_WARNING << " Framebuffer::Framebuffer: No buffers specified. Framebuffer should have at least 1 buffer attached.\n";
#endif
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return;
}
if (cBuf)
Attach_ColorBuffer(attachment, cBuf);
if (dBuf)
Attach_DepthBuffer(dBuf);
if (sBuf)
Attach_StencilBuffer(sBuf);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
Framebuffer::Framebuffer(std::unordered_map<unsigned int, ColorBuffer*> cBufs, DepthStencilBuffer* dsBuf) : DepthBuf(NULL), StencilBuf(NULL), DepthStencilBuf(NULL), ID(0)
{
glGenFramebuffers(1, &ID);
glBindFramebuffer(GL_FRAMEBUFFER, ID);
if (cBufs.size() == 0 && !dsBuf) {
#ifdef DEBUG
std::cout << DC_WARNING << " Framebuffer::Framebuffer: No buffers specified. Framebuffer should have at least 1 buffer attached.\n";
#endif
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return;
}
for (auto i : cBufs)
Attach_ColorBuffer(i.first, i.second);
if (dsBuf)
Attach_DepthStencilBuffer(dsBuf);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
Framebuffer::Framebuffer(unsigned int attachment, ColorBuffer* cBuf, DepthStencilBuffer* dsBuf) : DepthBuf(NULL), StencilBuf(NULL), DepthStencilBuf(NULL), ID(0)
{
glGenFramebuffers(1, &ID);
glBindFramebuffer(GL_FRAMEBUFFER, ID);
if (!cBuf && !dsBuf) {
#ifdef DEBUG
std::cout << DC_WARNING << " Framebuffer::Framebuffer: No buffers specified. Framebuffer should have at least 1 buffer attached.\n";
#endif
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return;
}
if (cBuf)
Attach_ColorBuffer(attachment, cBuf);
if (dsBuf)
Attach_DepthStencilBuffer(dsBuf);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
Framebuffer::Framebuffer() : DepthBuf(NULL), StencilBuf(NULL), DepthStencilBuf(NULL), ID(0)
{
glGenFramebuffers(1, &ID);
}
Framebuffer::~Framebuffer()
{
glDeleteFramebuffers(1, &ID);
}
int Framebuffer::CheckStatus()
{
int status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
switch (status)
{
case GL_FRAMEBUFFER_COMPLETE:
return 0;
break;
default:
#ifdef DEBUG
fprintf(stderr, "Framebuffer::Complete(): " DC_ERROR " code: %#04x\n", (int)status);
#endif
return status;
}
}
void Framebuffer::Set_ReadDrawBuffer(GLenum mode)
{
glBindFramebuffer(GL_FRAMEBUFFER, ID);
glDrawBuffer(mode);
glReadBuffer(mode);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void Framebuffer::Set_DrawBuffers(std::initializer_list<unsigned int> indexes)
{
GLint maxDrawBuf = 0;
glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuf);
if (indexes.size() > maxDrawBuf) {
#ifdef DEBUG
std::cout << DC_ERROR << " Framebuffer::Set_DrawBuffers(): Couldn't set glDrawBuffers. Number of indexes > GL_MAX_DRAW_BUFFERS.\n";
#endif
}
else {
GLenum *arr = new GLenum[indexes.size()];
auto j = indexes.begin();
for (int i = 0; i < indexes.size(); i++, j++)
arr[i] = GL_COLOR_ATTACHMENT0 + *j;
glBindFramebuffer(GL_FRAMEBUFFER, ID);
glDrawBuffers(indexes.size(), (const GLenum*) arr);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
delete[] arr;
}
}
void Framebuffer::Attach_ColorBuffer(unsigned int attachment, ColorBuffer* buffer)
{
GLint maxAttach = 0;
glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &maxAttach);
if (attachment > maxAttach) {
#ifdef DEBUG
std::cout << DC_ERROR << " Framebuffer::Attach_ColorBuffer(): Color attachment " << attachment << " > GL_MAX_COLOR_ATTACHMENTS. Color buffer wasn't attached.\n";
#endif
return;
}
else if (buffer != NULL) {
if (buffer->ID == 0) {
#ifdef DEBUG
std::cout << DC_ERROR << " Cannot attach incomplete ColorBuffer at color attachment " << ColorBufs.size() << ".\n";
#endif
return;
}
if (buffer->Samples != 0) {
#ifdef DEBUG
std::cout << DC_ERROR << " Framebuffer::Attach_ColorBuffer(): Could not attach multisampled color buffer at color attachment " << ColorBufs.size() << "\n";
#endif
}
ColorBufs[attachment] = buffer;
glBindFramebuffer(GL_FRAMEBUFFER, ID);
switch (buffer->Type)
{
case BufferType::Texture:
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + attachment, GL_TEXTURE_2D, buffer->ID, 0);
break;
case BufferType::Renderbuffer:
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + attachment, GL_RENDERBUFFER, buffer->ID);
break;
case BufferType::Cubemap:
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + attachment, buffer->ID, 0);
break;
}
}
else {
#ifdef DEBUG
std::cout << DC_WARNING << " Framebuffer::Attach_ColorBuffer(): ColorBuffer wasn't attached. (buffer == NULL)\n";
#endif
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void Framebuffer::Attach_DepthBuffer(DepthBuffer* buffer)
{
DepthBuf = buffer;
if (DepthBuf != NULL)
{
if (DepthBuf->ID == 0) {
#ifdef DEBUG
std::cout << DC_ERROR << " Cannot attach uncomplete DepthBuffer. ( DepthBuffer::ID == 0 )\n";
#endif
return;
}
if (DepthBuf->Samples == 0)
{
glBindFramebuffer(GL_FRAMEBUFFER, ID);
if (DepthBuf->Type == BufferType::Texture)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, DepthBuf->ID, 0);
else if (DepthBuf->Type == BufferType::Renderbuffer)
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, DepthBuf->ID);
else if (DepthBuf->Type == BufferType::Cubemap)
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, DepthBuf->ID, 0);
}
else {
#ifdef DEBUG
std::cout << DC_ERROR << " Framebuffer::Attach_DepthBuffer(): Cannot attach multisampled depth buffer to Framebuffer. For multisampled framebuffers use MSFramebuffer.\n";
#endif
}
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void Framebuffer::Attach_StencilBuffer(StencilBuffer* buffer)
{
glBindFramebuffer(GL_FRAMEBUFFER, ID);
StencilBuf = buffer;
if (StencilBuf != NULL)
{
if (StencilBuf->ID == 0) {
#ifdef DEBUG
std::cout << DC_ERROR << " Cannot attach uncomplete StencilBuffer. ( StencilBuffer::ID == 0 )\n";
#endif
return;
}
if (StencilBuf->Samples == 0) {
if (StencilBuf->Type == BufferType::Renderbuffer)
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, StencilBuf->ID);
else if (StencilBuf->Type == BufferType::Texture)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, StencilBuf->ID, 0);
else {
#ifdef DEBUG
std::cout << DC_ERROR << " Framebuffer::Attach_StencilBuffer(): StencilBuffer not attached. Wrong buffer type.\n";
#endif
}
}
else {
#ifdef DEBUG
std::cout << DC_ERROR " Framebuffer::Attach_StencilBuffer(): Cannot attach multisampled stencil buffer to Framebuffer. For multisampled framebuffer use MSFramebuffer.\n";
#endif
}
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void Framebuffer::Attach_DepthStencilBuffer(DepthStencilBuffer* buffer)
{
glBindFramebuffer(GL_FRAMEBUFFER, ID);
DepthStencilBuf = buffer;
if (DepthStencilBuf != NULL)
{
if (DepthStencilBuf->ID == 0) {
#ifdef DEBUG
std::cout << DC_ERROR << " Cannot attach uncomplete DepthStencilBuffer. ( DepthStencilBuffer::ID == 0 )\n";
#endif
return;
}
if (DepthStencilBuf->Samples == 0) {
if (DepthStencilBuf->Type == BufferType::Texture)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, DepthStencilBuf->BindTarget(), DepthStencilBuf->ID, 0);
else if (DepthStencilBuf->Type == BufferType::Renderbuffer)
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, DepthStencilBuf->ID);
else {
#ifdef DEBUG
std::cout << DC_ERROR << " Framebuffer::Attach_DepthStencilBuffer(): DepthStencilBuffer not attached. Wrong buffer type.\n";
#endif
}
}
else {
#ifdef DEBUG
std::cout << DC_ERROR " Framebuffer::Attach_DepthStencilBuffer(): Cannot attach multisampled depth and stencil buffer to Framebuffer. For multisampled framebuffer use MSFramebuffer.\n";
#endif
}
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
unsigned int Framebuffer::Get_ColorBuffer(unsigned int attachment)
{
return ColorBufs[attachment]->ID;
}
unsigned int Framebuffer::Get_ColorBufferWidth(unsigned int attachment)
{
return ColorBufs[attachment]->Width;
}
unsigned int Framebuffer::Get_ColorBufferHeight(unsigned int attachment)
{
return ColorBufs[attachment]->Height;
}
std::string Framebuffer::Export(std::string dir, ImageType type, unsigned int attachment)
{
uint8_t channels = 0;
GLenum format;
switch (type) {
case ImageType::PNG: channels = 4; format = GL_RGBA; break;
case ImageType::JPG: channels = 3; format = GL_RGB; break;
}
unsigned int width = Get_ColorBufferWidth(attachment);
unsigned int height = Get_ColorBufferHeight(attachment);
GLint binding = GetFramebufferBinding();
glBindFramebuffer(GL_FRAMEBUFFER, ID);
unsigned char *data = new unsigned char[width * height * channels];
glReadBuffer(GL_COLOR_ATTACHMENT0 + attachment);
glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, data);
stbi_flip_vertically_on_write(1);
if (dir.length() != 0 && *(dir.end() - 1) != '/')
dir = dir + "/";
std::string img_name = dir + GetScreenshotName(type);
switch (type) {
case ImageType::PNG: stbi_write_png(img_name.c_str(), width, height, channels, (const void*)data, width * channels);
case ImageType::JPG: stbi_write_jpg(img_name.c_str(), width, height, channels, (const void*)data, jpg_export_quality);
}
delete[] data;
glBindFramebuffer(GL_FRAMEBUFFER, binding);
return img_name;
}
void Framebuffer::Set_JpgExportQuality(uint8_t quality)
{
jpg_export_quality = quality;
}
uint8_t Framebuffer::Get_JpgExportQuality()
{
return jpg_export_quality;
}

View File

@@ -0,0 +1,349 @@
#include <glad/glad.h>
#include "glfbo.h"
#include "config.h"
#include "DebugColors.h"
#include <stb_image_write.h>
#define WARN_EMPTY_BUFFERS DC_WARNING " MSFramebuffer::MSFramebuffer: No buffers specified. Framebuffer should have at least 1 buffer attached."
#define WARN_SAMPLES_LESS_TWO DC_WARNING " MSFramebuffer::MSFramebuffer: Cannot create multisamples framebuffer with samples < 2. Using samples = 2"
#define ERR_ATTACH_BUFFER_NULL(type) DC_ERROR " MSFramebuffer::Attach_" type "(): " type " wasn't attached. (buffer == NULL)"
#define ERR_ATTACH_SAMPLES_NOT_EQ(type) DC_ERROR " MSFramebuffer::Attach_" type "(): " type " wasn't attached. Samples aren't equal to samples of MSFramebuffer."
#define ERR_ATTACH_BUF_INCOMPLETE(type) DC_ERROR " MSFramebuffer::Attach_" type "(): Cannot attach incomplete " type "."
#define ERR_ATTACH_WRONG_BUF_TYPE(type) DC_ERROR " MSFramebuffer::Attach_" type "(): " type " wasn't attached. Wrong buffer type."
#define ERR_ATTACH_TOO_MANY_ATTACHMENTS DC_ERROR " MSFramebuffer::Attach_ColorBuffer(): ColorBuffer wasn't attached. Given number of color attachment > GL_MAX_COLOR_ATTACHMENTS."
#define ERR_COL_ATTACH_NOT_FOUND(func) DC_ERROR " MSFramebuffer::" func "(): Could not find color attachment."
#define ERR_INT_FBO_NOT_FOUND(func) DC_ERROR " MSFramebuffer::" func "(): Could not find intermediate framebuffer for specified color attachment. Did you forget to call Create_intFBO() ?"
#define ERR_EXPORT DC_ERROR " MSFramebuffer::Export(): Could not export ColorBuffer."Framebuffer* ifbo =
using namespace glfbo;
void MSFramebuffer::handleSamples(unsigned int s)
{
if (s < 2) {
#ifdef DEBUG
std::cout << WARN_SAMPLES_LESS_TWO << std::endl;
#endif
this->samples = s = 2;
}
else
this->samples = s;
}
MSFramebuffer::MSFramebuffer(unsigned int samples, std::unordered_map<unsigned int, ColorBuffer*> cBufs, DepthBuffer* dBuf, StencilBuffer* sBuf)
{
glGenFramebuffers(1, &ID);
glBindFramebuffer(GL_FRAMEBUFFER, ID);
handleSamples(samples);
if (cBufs.size() != 0) {
bool all_null = true;
for (auto i : cBufs) {
if (i.second != NULL) {
all_null = false;
break;
}
}
if (all_null && !dBuf && !sBuf) {
#ifdef DEBUG
std::cout << WARN_EMPTY_BUFFERS << std::endl;
#endif
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return;
}
}
else if (!dBuf && !sBuf) {
#ifdef DEBUG
std::cout << WARN_EMPTY_BUFFERS << std::endl;
#endif
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return;
}
for (auto i : cBufs)
Attach_ColorBuffer(i.first, i.second);
if (dBuf)
Attach_DepthBuffer(dBuf);
if (sBuf)
Attach_StencilBuffer(sBuf);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
MSFramebuffer::MSFramebuffer(unsigned int samples, unsigned int attachment, ColorBuffer* cBuf, DepthBuffer* dBuf, StencilBuffer* sBuf)
: MSFramebuffer(samples, {{attachment, cBuf}}, dBuf, sBuf) {}
MSFramebuffer::MSFramebuffer(unsigned int samples, std::unordered_map<unsigned int, ColorBuffer*> cBufs, DepthStencilBuffer* dsBuf)
{
glGenFramebuffers(1, &ID);
glBindFramebuffer(GL_FRAMEBUFFER, ID);
handleSamples(samples);
if (cBufs.size() != 0) {
bool all_null = true;
for (auto i : cBufs) {
if (i.second != NULL) {
all_null = false;
break;
}
}
if (all_null && !dsBuf) {
#ifdef DEBUG
std::cout << WARN_EMPTY_BUFFERS << std::endl;
#endif
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return;
}
}
else if (!dsBuf) {
#ifdef DEBUG
std::cout << WARN_EMPTY_BUFFERS << std::endl;
#endif
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return;
}
for (auto i : cBufs)
Attach_ColorBuffer(i.first, i.second);
if (dsBuf)
Attach_DepthStencilBuffer(dsBuf);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
MSFramebuffer::MSFramebuffer(unsigned int samples, unsigned int attachment, ColorBuffer* cBuf, DepthStencilBuffer* dsBuf)
: MSFramebuffer(samples, {{attachment, cBuf}}, dsBuf) {}
MSFramebuffer::MSFramebuffer(unsigned int samples)
{
glGenFramebuffers(1, &ID);
handleSamples(samples);
}
MSFramebuffer::~MSFramebuffer()
{
for (auto i : intFBOs) {
delete i.second->ColorBufs[0];
delete i.second;
}
glDeleteFramebuffers(1, &ID);
}
void MSFramebuffer::Attach_ColorBuffer(unsigned int attachment, ColorBuffer* buffer)
{
GLint maxAttach = 0;
glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &maxAttach);
if (attachment > maxAttach) {
#ifdef DEBUG
std::cout << ERR_ATTACH_TOO_MANY_ATTACHMENTS << std::endl;
#endif
return;
}
if (buffer != NULL) {
if (buffer->Samples != this->samples) {
#ifdef DEBUG
std::cout << ERR_ATTACH_SAMPLES_NOT_EQ("ColorBuffer") << std::endl;
#endif
return;
}
if (buffer->ID == 0) {
#ifdef DEBUG
std::cout << ERR_ATTACH_BUF_INCOMPLETE("ColorBuffer") << " At index " << ColorBufs.size() << std::endl;
#endif
return;
}
ColorBufs[attachment] = buffer;
glBindFramebuffer(GL_FRAMEBUFFER, ID);
switch (buffer->Type)
{
case BufferType::Texture:
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + attachment, GL_TEXTURE_2D_MULTISAMPLE, buffer->ID, 0);
break;
case BufferType::Renderbuffer:
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + attachment, GL_RENDERBUFFER, buffer->ID);
break;
case BufferType::Cubemap:
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + attachment, buffer->ID, 0);
break;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
else {
#ifdef DEBUG
std::cout << ERR_ATTACH_BUFFER_NULL("ColorBuffer") << std::endl;
#endif
}
}
void MSFramebuffer::Attach_DepthBuffer(DepthBuffer* buffer)
{
DepthBuf = buffer;
if (buffer != NULL) {
if (buffer->Samples != this->samples) {
#ifdef DEBUG
std::cout << ERR_ATTACH_SAMPLES_NOT_EQ("DepthBuffer") << std::endl;
#endif
return;
}
if (buffer->ID == 0) {
#ifdef DEBUG
std::cout << ERR_ATTACH_BUF_INCOMPLETE("DepthBuffer") << std::endl;
#endif
return;
}
glBindFramebuffer(GL_FRAMEBUFFER, ID);
if (DepthBuf->Type == BufferType::Texture)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, DepthBuf->ID, 0);
else if (DepthBuf->Type == BufferType::Renderbuffer)
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, DepthBuf->ID);
else if (DepthBuf->Type == BufferType::Cubemap)
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, DepthBuf->ID, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
else {
#ifdef DEBUG
std::cout << ERR_ATTACH_BUFFER_NULL("DepthBuffer") << std::endl;
#endif
}
}
void MSFramebuffer::Attach_StencilBuffer(StencilBuffer* buffer)
{
StencilBuf = buffer;
if (buffer != NULL) {
if (buffer->Samples != this->samples) {
#ifdef DEBUG
std::cout << ERR_ATTACH_SAMPLES_NOT_EQ("StencilBuffer") << std::endl;
#endif
return;
}
if (buffer->ID == 0) {
#ifdef DEBUG
std::cout << ERR_ATTACH_BUF_INCOMPLETE("StencilBuffer") << std::endl;
#endif
return;
}
glBindFramebuffer(GL_FRAMEBUFFER, ID);
if (StencilBuf->Type == BufferType::Renderbuffer)
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, StencilBuf->ID);
else if (StencilBuf->Type == BufferType::Texture)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, StencilBuf->ID, 0);
else {
#ifdef DEBUG
std::cout << ERR_ATTACH_WRONG_BUF_TYPE("StencilBuffer") << std::endl;
#endif
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
else {
#ifdef DEBUG
std::cout << ERR_ATTACH_BUFFER_NULL("StencilBuffer") << std::endl;
#endif
}
}
void MSFramebuffer::Attach_DepthStencilBuffer(DepthStencilBuffer* buffer)
{
DepthStencilBuf = buffer;
if (buffer != NULL) {
if (buffer->Samples != this->samples) {
#ifdef DEBUG
std::cout << ERR_ATTACH_SAMPLES_NOT_EQ("DepthStencilBuffer") << std::endl;
#endif
return;
}
if (buffer->ID == 0) {
#ifdef DEBUG
std::cout << ERR_ATTACH_BUF_INCOMPLETE("DepthStencilBuffer") << std::endl;
#endif
return;
}
glBindFramebuffer(GL_FRAMEBUFFER, ID);
if (DepthStencilBuf->Type == BufferType::Texture)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, DepthStencilBuf->ID, 0);
else if (DepthStencilBuf->Type == BufferType::Renderbuffer)
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, DepthStencilBuf->ID);
else {
#ifdef DEBUG
std::cout << ERR_ATTACH_WRONG_BUF_TYPE("DepthStencilBuffer") << std::endl;
#endif
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
else {
#ifdef DEBUG
std::cout << ERR_ATTACH_BUFFER_NULL("DepthStencilBuffer") << std::endl;
#endif
}
}
unsigned int MSFramebuffer::Get_Samples()
{
return samples;
}
void MSFramebuffer::Create_intFBO(unsigned int attachment)
{
if (ColorBufs.find(attachment) == ColorBufs.end()) {
#ifdef DEBUG
std::cout << ERR_COL_ATTACH_NOT_FOUND("Create_intFBO") << " (attachment == " << attachment << ")" << std::endl;
#endif
return;
}
if (intFBOs.find(attachment) != intFBOs.end()) {
if (intFBOs[attachment] != NULL) {
delete intFBOs[attachment]->ColorBufs[0];
delete intFBOs[attachment];
}
}
ColorBuffer *cb = new ColorBuffer(ColorBufs[attachment]->Width, ColorBufs[attachment]->Height,
ColorBufs[attachment]->Type, ColorBufs[attachment]->Get_internal_format(),
0, GL_CLAMP_TO_EDGE);
Framebuffer *f = new Framebuffer();
f->Attach_ColorBuffer(0, cb);
f->Set_DrawBuffers({0});
intFBOs[attachment] = f;
}
unsigned int MSFramebuffer::Get_ResolvedColorBuffer(unsigned int attachment)
{
if (intFBOs.find(attachment) == intFBOs.end()) {
#ifdef DEBUG
std::cout << ERR_INT_FBO_NOT_FOUND("Get_ResolvedColorBuffer") << " (attachment == " << attachment << ")" << std::endl;
#endif
return 0;
}
GLint binding;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &binding);
glBindFramebuffer(GL_READ_FRAMEBUFFER, ID);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, intFBOs[attachment]->ID);
glReadBuffer(GL_COLOR_ATTACHMENT0 + attachment);
glDrawBuffer(GL_COLOR_ATTACHMENT0);
glBlitFramebuffer(0, 0, ColorBufs[attachment]->Width, ColorBufs[attachment]->Height,
0, 0, ColorBufs[attachment]->Width, ColorBufs[attachment]->Height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBindFramebuffer(GL_FRAMEBUFFER, binding);
return intFBOs[attachment]->ColorBufs[0]->ID;
}
std::string MSFramebuffer::Export(std::string dir, ImageType type, unsigned int attachment)
{
Framebuffer* ifbo = NULL;
auto it = intFBOs.find(attachment);
if (it != intFBOs.end())
ifbo = it->second;
else {
#ifdef DEBUG
std::cerr << ERR_INT_FBO_NOT_FOUND("Export") << " (attachment == " << attachment << ")\n";
#endif
return "";
}
return ifbo->Export(dir, type, 0);
}
Framebuffer* MSFramebuffer::Get_intFBO(unsigned int attachment)
{
if (intFBOs.find(attachment) == intFBOs.end())
{
#ifdef DEBUG
std::cerr << ERR_INT_FBO_NOT_FOUND("Get_intFBOs") << " (attachment == " << attachment << ")\n";
#endif
return NULL;
}
return intFBOs[attachment];
}

View File

@@ -0,0 +1,80 @@
#include "glfbo.h"
#include <iostream>
#include "config.h"
#include "DebugColors.h"
using namespace glfbo;
StencilBuffer::StencilBuffer(unsigned int width, unsigned int height,
BufferType type,
unsigned int samples) : Width(width), Height(height), Samples(samples), Type(type)
{
if (type == BufferType::Cubemap) {
#ifdef DEBUG
std::cout << DC_WARNING << " StencilBuffer: Bad buffer type. Using BufferType::Renderbuffer.\n";
#endif
type = Type = BufferType::Renderbuffer;
}
#ifdef DEBUG
if (samples == 1)
std::cout << DC_WARNING << " StencilBuffer: samples == 1 will not create multisampled stencil buffer.\n";
#endif
if (type == BufferType::Renderbuffer) {
glGenRenderbuffers(1, &ID);
glBindRenderbuffer(GL_RENDERBUFFER, ID);
if (samples <= 1) {
samples = 0;
}
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 0, GL_STENCIL_INDEX8, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
}
else {
glGenTextures(1, &ID);
GLint target;
if (samples > 1) {
target = GL_TEXTURE_2D_MULTISAMPLE;
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_STENCIL_INDEX8, width, height, GL_TRUE);
}
else {
Samples = 0;
target = GL_TEXTURE_2D;
glTexImage2D(GL_TEXTURE_2D, 0, GL_STENCIL_INDEX8, width, height, 0, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, 0);
}
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(target, 0);
}
}
StencilBuffer::~StencilBuffer()
{
if (Type == BufferType::Texture)
glDeleteTextures(1, &ID);
else if (Type == BufferType::Renderbuffer)
glDeleteRenderbuffers(1, &ID);
}
GLenum StencilBuffer::BindTarget()
{
if (Type == BufferType::Renderbuffer)
return GL_RENDERBUFFER;
else if (Type == BufferType::Texture) {
if (Samples > 1)
return GL_TEXTURE_2D_MULTISAMPLE;
else
return GL_TEXTURE_2D;
}
else
{
#ifdef DEBUG
std::cout << DC_ERROR << " StencilBuffer::BindTarget(): Bad buffer type. Returning GL_RENDERBUFFER.\n";
#endif
return GL_RENDERBUFFER;
}
}

View File

@@ -0,0 +1,19 @@
#include "glfbo.h"
#include <stb_image_write.h>
GLint glfbo::GetFramebufferBinding()
{
GLint bind = 0;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &bind);
return bind;
}
std::string glfbo::GetScreenshotName(glfbo::ImageType type)
{
time_t tim = time(NULL);
struct tm *ltm = localtime(&tim);
char buf[50];
snprintf(buf, sizeof(buf), ("screenshot_%i%02i%02i_%02i%02i%02i." + ImageTypeStrings[(size_t)type]).c_str(),
ltm->tm_year + 1900, ltm->tm_mon + 1, ltm->tm_mday, ltm->tm_hour, ltm->tm_min, ltm->tm_sec);
return std::string(buf);
}

View File

@@ -0,0 +1,2 @@
add_library(glml Model.cpp ${SOURCE_INCLUDE}/glml/Model.h
Mesh.cpp ${SOURCE_INCLUDE}/glml/Mesh.h)

View File

@@ -0,0 +1,67 @@
#include <glad/glad.h>
#include <glml/Mesh.h>
using namespace glml;
Mesh::Mesh(const std::vector<Vertex>& vertices,
const std::vector<unsigned int>& indices,
const std::vector<Texture>& textures) : vertices(vertices), indices(indices), textures(textures)
{
setupMesh();
}
void Mesh::setupMesh()
{
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, Position));
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, Normal));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, TexCoord));
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, Tangent));
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, Bitangent));
for (int i = 0; i < 5; i++)
glEnableVertexAttribArray(i);
glBindVertexArray(0);
}
void Mesh::Draw(const Shader& shader) const
{
unsigned int diffNr = 0;
unsigned int specNr = 0;
unsigned int normNr = 0;
unsigned int heigNr = 0;
for (int i = 0; i < textures.size(); i++)
{
std::string name = TextureTypeStrings[static_cast<size_t>(textures[i].type)];
if (textures[i].type == TextureType::diffuse)
name += std::to_string(diffNr++);
else if (textures[i].type == TextureType::specular)
name += std::to_string(specNr++);
else if (textures[i].type == TextureType::normal)
name += std::to_string(normNr++);
else if (textures[i].type == TextureType::height)
name += std::to_string(heigNr++);
glActiveTexture(GL_TEXTURE0 + i);
shader.setInt("material." + name, i);
glBindTexture(GL_TEXTURE_2D, textures[i].id);
}
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}

View File

@@ -0,0 +1,200 @@
#include <glad/glad.h>
#include <assimp/Importer.hpp>
#include <assimp/postprocess.h>
#include <iostream>
#include <stb_image.h>
#include "glml/Model.h"
#include "DebugColors.h"
using namespace glml;
Model::Model(const std::string path, bool flip_textures, bool sRGB_diffuse) :
sRGB_diffuse(sRGB_diffuse), flip_textures(flip_textures)
{
loadModel(path);
}
void Model::Draw(const Shader& shader) const
{
for (int i = 0; i < meshes.size(); i++)
meshes[i].Draw(shader);
}
void Model::loadModel(const std::string path)
{
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_CalcTangentSpace);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
{
std::cerr << DC_ERROR "[ASSIMP] Model '" << path << "' not loaded.\n" << importer.GetErrorString() << std::endl;
return;
}
directory = path.substr(0, path.find_last_of('/'));
processNode(scene->mRootNode, scene);
}
void Model::processNode(aiNode* node, const aiScene* scene)
{
unsigned int i;
for (i = 0; i < node->mNumMeshes; i++)
{
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(processMesh(mesh, scene));
}
for (i = 0; i < node->mNumChildren; i++)
processNode(node->mChildren[i], scene);
}
Mesh Model::processMesh(aiMesh* mesh, const aiScene* scene)
{
std::vector<Vertex> vertices;
std::vector<unsigned int> indices;
std::vector<Texture> textures;
unsigned int i;
for (i = 0; i < mesh->mNumVertices; i++)
{
Vertex vertex;
glm::vec3 v;
v.x = mesh->mVertices[i].x;
v.y = mesh->mVertices[i].y;
v.z = mesh->mVertices[i].z;
vertex.Position = v;
if (mesh->mNormals != NULL)
{
v.x = mesh->mNormals[i].x;
v.y = mesh->mNormals[i].y;
v.z = mesh->mNormals[i].z;
vertex.Normal = v;
}
if (mesh->mTangents != NULL)
{
v.x = mesh->mTangents[i].x;
v.y = mesh->mTangents[i].y;
v.z = mesh->mTangents[i].z;
vertex.Tangent = v;
}
if (mesh->mBitangents != NULL)
{
v.x = mesh->mBitangents[i].x;
v.y = mesh->mBitangents[i].y;
v.z = mesh->mBitangents[i].z;
vertex.Bitangent = v;
}
if (mesh->mTextureCoords[0]) { // does the mesh contains texture coordinates?
glm::vec2 vec;
vec.x = mesh->mTextureCoords[0][i].x;
vec.y = mesh->mTextureCoords[0][i].y;
vertex.TexCoord = vec;
}
else {
vertex.TexCoord = glm::vec2(0.0f);
}
vertices.push_back(vertex);
}
for (i = 0; i < mesh->mNumFaces; i++)
{
aiFace face = mesh->mFaces[i];
for (unsigned int j = 0; j < face.mNumIndices; j++)
indices.push_back(face.mIndices[j]);
}
if (mesh->mMaterialIndex >= 0)
{
aiMaterial* mat = scene->mMaterials[mesh->mMaterialIndex];
std::vector<Texture> diffuseMaps = loadMaterialTextures(mat, aiTextureType_DIFFUSE, TextureType::diffuse);
textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
std::vector<Texture> specularMaps = loadMaterialTextures(mat, aiTextureType_SPECULAR, TextureType::specular);
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
std::vector<Texture> normalMaps = loadMaterialTextures(mat, aiTextureType_NORMALS, TextureType::normal);
textures.insert(textures.end(), normalMaps.begin(), normalMaps.end());
std::vector<Texture> heightMaps = loadMaterialTextures(mat, aiTextureType_HEIGHT, TextureType::height);
textures.insert(textures.end(), heightMaps.begin(), heightMaps.end());
}
return Mesh(vertices, indices, textures);
}
std::vector<Texture> Model::loadMaterialTextures(aiMaterial* mat, aiTextureType type, TextureType glmlType)
{
std::vector<Texture> textures;
bool sRGB = false;
if (sRGB_diffuse && glmlType == TextureType::diffuse)
sRGB = true;
for (unsigned int i = 0; i < mat->GetTextureCount(type); i++)
{
aiString str;
mat->GetTexture(type, i, &str);
bool loaded = false;
for (int j = 0; j < textures_loaded.size(); j++)
{
if (std::strcmp(textures_loaded[j].path.data(), str.C_Str()) == 0)
{
textures.push_back(textures_loaded[j]);
loaded = true;
break;
}
}
if (!loaded)
{
Texture texture;
texture.id = load_texture(directory + '/' + str.C_Str(), flip_textures, sRGB);
texture.type = glmlType;
texture.path = str.C_Str();
textures.push_back(texture);
textures_loaded.push_back(texture);
}
}
return textures;
}
static unsigned int glml::load_texture(std::string path, bool flip, bool sRGB, GLint wrapS, GLint wrapT)
{
return glml::load_texture(path.c_str(), flip, sRGB, wrapS, wrapT);
}
static unsigned int glml::load_texture(const char* path, bool flip, bool sRGB, GLint wrapS, GLint wrapT)
{
unsigned int texture = 0;
int width, height, nrChannels, format, intFormat;
stbi_set_flip_vertically_on_load(flip);
unsigned char* data = stbi_load(path, &width, &height, &nrChannels, 0); // load image to the array of bytes (char = 1 byte)
switch (nrChannels)
{
case 4: format = GL_RGBA;
if (sRGB) intFormat = GL_SRGB_ALPHA;
else intFormat = GL_RGBA;
break;
case 3: format = GL_RGB;
if (sRGB) intFormat = GL_SRGB;
else intFormat = GL_RGB;
break;
case 2: format = intFormat = GL_RED; break;
default: format = intFormat = GL_RGB; break;
}
if (data) // data != NULL
{
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, GL_UNSIGNED_BYTE, data); // generates texture
glGenerateMipmap(GL_TEXTURE_2D);
}
else {
std::cerr << DC_WARNING " Could not load texture '" << path << "'\n";
}
stbi_image_free(data);
glBindTexture(GL_TEXTURE_2D, 0);
return texture;
}

View File

@@ -0,0 +1,54 @@
#pragma once
#include <glm/glm.hpp>
enum camera_movement {
CAM_FORWARD,
CAM_BACKWARD,
CAM_LEFT,
CAM_RIGHT,
CAM_UP,
CAM_DOWN
};
enum camera_mode {
CAM_FLOAT,
CAM_MINECRAFT
};
class Camera
{
public:
float FOV;
float MovementSpeed;
float MouseSensitivity;
float Yaw; // vlevo vpravo
float Pitch; // nahoru dolu
camera_mode Mode;
glm::vec3 Position;
glm::vec3 Front;
glm::vec3 Right;
glm::vec3 Up;
glm::vec3 WorldUp;
glm::vec3 WorldRight;
glm::vec3 WorldFront;
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3 worldUp = glm::vec3(0.0f, 1.0f, 0.0f),
glm::vec3 worldFront = glm::vec3(0.0f, 0.0f, -1.0),
float yaw = -90.0f, float pitch = 0);
glm::mat4 getViewMatrix();
void ProccessKeyboard(camera_movement direction, float deltaTime);
void ProccessMouse(float xoffset, float yoffset, bool constrainPitch = true);
void ProccessScroll(float yoffset);
void SetCameraMode(camera_mode mode);
private:
float lastY;
float lastX;
void updateCameraVectors();
};

View File

@@ -0,0 +1,17 @@
#pragma once
#define DC_RED "\x1b[31m"
#define DC_GREEN "\x1b[32m"
#define DC_YELLOW "\x1b[33m"
#define DC_CYAN "\x1b[36m"
#define DC_MAGNETA "\x1b[35m"
#define DC_DEFAULT "\x1b[0m"
#define DC_ERROR "[" DC_RED "ERROR" DC_DEFAULT "]"
#define DC_SUCCESS "[" DC_GREEN "SUCCESS" DC_DEFAULT "]"
#define DC_INFO "[" DC_CYAN "INFO" DC_DEFAULT "]"
#define DC_WARNING "[" DC_YELLOW "WARNING" DC_DEFAULT "]"
#define DC_CURSOR_MOVE(y, x) std::cout << "\033[" << (y + 1) << ";" << (x + 1) << "H"
#define DC_CURSOR_SAVE() std::cout << "\033[s"
#define DC_CURSOR_RESTORE() std::cout << "\033[u"
#define DC_CLRTOEOL() std::cout << "\033[K"
#define DC_CLRSCR() std::cout << "\033[2J"

View File

@@ -0,0 +1,29 @@
#pragma once
#include <string>
#include <glm/glm.hpp>
class Shader
{
public:
unsigned int Program;
Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath, const char* geometryShaderSource = NULL, bool directSource = false);
Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath, std::string geometryShaderSource = NULL, bool directSource = false);
~Shader();
void Use() const;
void setBool(const std::string name, bool value) const;
void setFloat(const std::string name, float value) const;
void setInt(const std::string name, int value) const;
void setMat4(const std::string name, glm::mat4 value) const;
void setVec3(const std::string name, glm::vec3 value) const;
void setVec3(const std::string name, float v0, float v1, float v2) const;
void setVec2(const std::string name, float v0, float v1) const;
void setVec2(const std::string name, glm::vec2 v) const;
void setMat3(const std::string name, glm::mat3 value) const;
static std::string readFile(const char* path);
};

View File

@@ -0,0 +1,204 @@
#pragma once
#include <iostream>
#include <glad/glad.h>
#include <vector>
#include <initializer_list>
#include <unordered_map>
#include <string>
namespace glfbo
{
enum class BufferType : uint8_t
{
Texture = 0,
Renderbuffer = 1,
Cubemap = 2
};
struct vector3
{
float x, y, z;
};
enum class ImageType : size_t
{
PNG = 0,
JPG = 1
};
static const std::string ImageTypeStrings[] = { "png", "jpg" };
std::string GetScreenshotName(ImageType type);
GLint GetFramebufferBinding();
class ColorBuffer
{
public:
unsigned int ID;
const unsigned int Width, Height;
unsigned int Samples;
BufferType Type;
ColorBuffer(unsigned int width, unsigned int height,
BufferType type,
GLint internal_format,
unsigned int samples,
GLint wrap_s,
GLint wrap_t,
GLint wrap_r,
vector3 bordercolor);
ColorBuffer(unsigned int width, unsigned int height,
BufferType type = BufferType::Texture,
GLint internal_format = GL_RGB,
unsigned int samples = 0,
GLint wrap_str = GL_CLAMP_TO_EDGE,
vector3 bordercolor = vector3{1.0f, 1.0f, 1.0f});
~ColorBuffer();
GLenum BindTarget();
GLint Get_internal_format();
private:
GLint int_format;
};
class DepthBuffer
{
public:
unsigned int ID;
const unsigned int Width, Height;
unsigned int Samples;
BufferType Type;
DepthBuffer(unsigned int width, unsigned int height,
BufferType type,
GLint internal_format,
unsigned int samples,
GLint wrap_s,
GLint wrap_t,
GLint wrap_r,
vector3 bordercolor);
DepthBuffer(unsigned int width, unsigned int height,
BufferType type = BufferType::Renderbuffer,
GLint internal_format = GL_DEPTH_COMPONENT24,
unsigned int samples = 0,
GLint wrap_str = GL_CLAMP_TO_EDGE,
vector3 border_color = vector3{1.0f, 1.0f, 1.0f});
~DepthBuffer();
GLenum BindTarget();
GLint Get_internal_format();
private:
GLint int_format;
};
class StencilBuffer
{
public:
unsigned int ID;
const unsigned int Width, Height;
unsigned int Samples;
BufferType Type;
StencilBuffer(unsigned int width, unsigned int height,
BufferType type = BufferType::Renderbuffer,
unsigned int samples = 0);
~StencilBuffer();
GLenum BindTarget();
};
class DepthStencilBuffer
{
public:
unsigned int ID;
const unsigned int Width, Height;
const unsigned int Samples;
BufferType Type;
DepthStencilBuffer(unsigned int width, unsigned int height,
BufferType type,
GLint internal_format,
unsigned int samples,
GLint wrap_s,
GLint wrap_t,
GLint wrap_r,
vector3 bordercolor);
DepthStencilBuffer(unsigned int width, unsigned int height,
BufferType type = BufferType::Renderbuffer,
GLint internal_format = GL_DEPTH24_STENCIL8,
unsigned int samples = 0,
GLint wrap_str = GL_CLAMP_TO_EDGE,
vector3 bordercolor = vector3{1.0f, 1.0f, 1.0f});
~DepthStencilBuffer();
GLenum BindTarget();
GLint Get_internal_format();
private:
GLint int_format;
};
class Framebuffer
{
protected:
uint8_t jpg_export_quality = 90;
public:
unsigned int ID;
std::unordered_map<unsigned int, ColorBuffer*> ColorBufs;
DepthBuffer *DepthBuf;
StencilBuffer *StencilBuf;
DepthStencilBuffer *DepthStencilBuf;
Framebuffer(std::unordered_map<unsigned int, ColorBuffer*> cBufs, DepthBuffer* dBuf, StencilBuffer* sBuf);
Framebuffer(unsigned int attachment, ColorBuffer* cBuf, DepthBuffer* dBuf, StencilBuffer* sBuf);
Framebuffer(unsigned int attachment, ColorBuffer* cBuf, DepthStencilBuffer* dsBuf);
Framebuffer(std::unordered_map<unsigned int, ColorBuffer*> cBufs, DepthStencilBuffer* dsBuf);
Framebuffer();
~Framebuffer();
int CheckStatus();
void Set_ReadDrawBuffer(GLenum mode);
/* Specify color attachments for drawing */
void Set_DrawBuffers(std::initializer_list<unsigned int> indexes);
/* Returns color buffer ID which is binded to specified color attachment number */
unsigned int Get_ColorBuffer(unsigned int attachment);
unsigned int Get_ColorBufferWidth(unsigned int attachment);
unsigned int Get_ColorBufferHeight(unsigned int attachment);
void Set_JpgExportQuality(uint8_t quality);
uint8_t Get_JpgExportQuality();
virtual void Attach_ColorBuffer(unsigned int attachment, ColorBuffer* buffer);
virtual void Attach_DepthBuffer(DepthBuffer* buffer);
virtual void Attach_StencilBuffer(StencilBuffer* buffer);
virtual void Attach_DepthStencilBuffer(DepthStencilBuffer* buffer);
virtual std::string Export(std::string dir, ImageType type, unsigned int attachment);
};
class MSFramebuffer : public Framebuffer
{
public:
MSFramebuffer(unsigned int samples, std::unordered_map<unsigned int, ColorBuffer*> cBufs, DepthBuffer* dBuf, StencilBuffer* sBuf);
MSFramebuffer(unsigned int samples, unsigned int attachment, ColorBuffer* cBuf, DepthBuffer* dBuf, StencilBuffer* sBuf);
MSFramebuffer(unsigned int samples, unsigned int attachment, ColorBuffer* cBuf, DepthStencilBuffer* dsBuf);
MSFramebuffer(unsigned int samples, std::unordered_map<unsigned int, ColorBuffer*> cBufs, DepthStencilBuffer* dsBuf);
MSFramebuffer(unsigned int samples);
~MSFramebuffer();
/* Returns number of samples */
unsigned int Get_Samples();
/* Resolve color buffer binded to specified attachment and returns resolved color buffer ID. */
unsigned int Get_ResolvedColorBuffer(unsigned int attachment);
/* Creates intermediate Framebuffer object for specified color attachment. */
void Create_intFBO(unsigned int attachment);
/* Returns pointer to intermediate framebuffer of specified color attachment. */
Framebuffer* Get_intFBO(unsigned int attachment);
void Attach_ColorBuffer(unsigned int attachment, ColorBuffer* buffer) override;
void Attach_DepthBuffer(DepthBuffer* buffer) override;
void Attach_StencilBuffer(StencilBuffer* buffer) override;
void Attach_DepthStencilBuffer(DepthStencilBuffer* buffer) override;
std::string Export(std::string dir, ImageType type, unsigned int attachment) override;
private:
std::unordered_map<unsigned int, Framebuffer*> intFBOs;
unsigned int samples;
void handleSamples(unsigned int s);
};
}

View File

@@ -0,0 +1,53 @@
#pragma once
#include <glm/glm.hpp>
#include <string>
#include <vector>
#include "Shader.h"
namespace glml
{
static const std::string TextureTypeStrings[] = {
"texture_diffuse",
"texture_specular",
"texture_normal",
"texture_height"
};
enum class TextureType : size_t
{
diffuse = 0,
specular = 1,
normal = 2,
height = 3
};
struct Vertex {
glm::vec3 Position;
glm::vec3 Normal;
glm::vec2 TexCoord;
glm::vec3 Tangent;
glm::vec3 Bitangent;
};
struct Texture {
unsigned int id;
TextureType type;
std::string path;
};
class Mesh {
public:
unsigned int VAO;
std::vector<Vertex> vertices;
std::vector<unsigned int> indices;
std::vector<Texture> textures;
Mesh(const std::vector<Vertex>& vertices, const std::vector<unsigned int>& indices, const std::vector<Texture>& textures);
void Draw(const Shader& shader) const;
private:
unsigned int VBO, EBO;
void setupMesh();
};
};

View File

@@ -0,0 +1,48 @@
#pragma once
#include <string>
#include "Mesh.h"
#include <vector>
#include "Shader.h"
#include <assimp/scene.h>
/*
Vertex attributes are expected as follows:
0. Vertex position
1. Vertex normal
2. Vertex texture coordinate
3. Vertex tangent
4. Vertex bitangent
In the Fragment Shader there must be at least
struct Material
{
sampler2D texture_diffuse0;
}
uniform Material material;
*/
namespace glml
{
class Model {
public:
std::vector<Mesh> meshes;
std::vector<Texture> textures_loaded;
/* sRGB_diffuse: when true, save diffuse textures as sRGB textures*/
Model(const std::string path, bool flip_textures = false, bool sRGB_diffuse = false);
void Draw(const Shader& shader) const;
private:
// model data
std::string directory;
const bool sRGB_diffuse;
const bool flip_textures;
void loadModel(const std::string path);
void processNode(aiNode* node, const aiScene* scene);
Mesh processMesh(aiMesh* mesh, const aiScene* scene);
std::vector<Texture> loadMaterialTextures(aiMaterial* mat, aiTextureType type, TextureType glmlType);
};
static unsigned int load_texture(const char* path, bool flip = true, bool sRGB = true, GLint wrapS = GL_REPEAT, GLint wrapT = GL_REPEAT);
static unsigned int load_texture(std::string path, bool flip = true, bool sRGB = true, GLint wrapS = GL_REPEAT, GLint wrapT = GL_REPEAT);
}

View File

@@ -0,0 +1,646 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <stb_image.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <vector>
#include <algorithm>
#include <stb_image_write.h>
#include <ctime>
#include <thread>
#include <chrono>
#include "Shader.h"
#include "Camera.h"
#include "glml/Mesh.h"
#include "DebugColors.h"
#include "glfbo.h"
#include "config.h" // in cmake build directory (need to run configure.sh first)
#if _WIN32
#include <Windows.h>
#endif
#define WINDOW_NAME "8. Bloom"
#define TEXTURES_DIR REPO_ROOT "/textures/"
#define MODELS_DIR REPO_ROOT "/models/"
#define SHADERS_DIR SOURCE_DIR "/shaders/"
#define _IsUnused __attribute__((__unused__)) // linux only
using namespace std;
using namespace glfbo;
typedef unsigned int uint;
struct Light {
glm::vec3 Position, Color, Ambient, Diffuse, Specular;
};
// Global Constants
// ...
// Global Variables
GLFWwindow* window;
int WINDOW_WIDTH = 800;
int WINDOW_HEIGHT = 600;
Camera myCamera(glm::vec3(0.0f, 0.0f, 3.0f));
float deltaTime = 0.0f;
float lastFrame = 0.0f;
float lastX = 400;
float lastY = 300;
bool firstmouse = true;
float shader_gamma = 2.2f;
float exposure = 1.0f;
uint8_t init(void);
void framebuffersize_callback(GLFWwindow* window, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void proccessInput(GLFWwindow* window);
void printDebugInfo();
unsigned int load_texture(const char* path, bool flip = true, bool sRGB = true, GLint wrapS = GL_REPEAT, GLint wrapT = GL_REPEAT);
unsigned int load_cubemap(vector<string> faces);
void draw_cube(void);
void draw_floor(void);
void draw_quad(void);
void draw_scene(const Shader* shader, bool lightCube);
void countAvgFPS();
Shader* shader = NULL;
Shader* quadShader = NULL;
Shader* basicShader = NULL;
Shader* blurShader;
bool cullFaces = false;
GLint polygonMode = GL_FILL;
glfbo::Framebuffer *printscreen_FBO = NULL;
int main(int argc, char** argv)
{
#if _WIN32
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
#endif
if (init() != 0)
return -1;
shader = new Shader(SHADERS_DIR "shader.vert", SHADERS_DIR "shader.frag");
quadShader = new Shader(SHADERS_DIR "quadShader.vert", SHADERS_DIR "quadShader.frag");
basicShader = new Shader(SHADERS_DIR "basicShader.vert", SHADERS_DIR "basicShader.frag");
blurShader = new Shader(SHADERS_DIR "quadShader.vert", SHADERS_DIR "blurShader.frag");
if (!shader->Program || !quadShader->Program || !basicShader->Program || !blurShader->Program)
{
std::cerr << DC_ERROR " Could not create shader programs!" << std::endl;
std::cerr << DC_INFO " Exiting...\n";
return 1;
}
myCamera.MovementSpeed = 5.0f;
MSFramebuffer fbOffScreen(4);
ColorBuffer colBuf0(WINDOW_WIDTH, WINDOW_HEIGHT, BufferType::Texture, GL_RGB16F, 4);
ColorBuffer colBuf1(WINDOW_WIDTH, WINDOW_HEIGHT, BufferType::Texture, GL_RGB16F, 4);
fbOffScreen.Attach_ColorBuffer(0, &colBuf0);
fbOffScreen.Attach_ColorBuffer(1, &colBuf1);
DepthStencilBuffer dsBuf(WINDOW_WIDTH, WINDOW_HEIGHT, BufferType::Renderbuffer, GL_DEPTH24_STENCIL8, 4);
fbOffScreen.Attach_DepthStencilBuffer(&dsBuf);
fbOffScreen.Set_DrawBuffers({0, 1});
fbOffScreen.Create_intFBO(0);
fbOffScreen.Create_intFBO(1);
if (fbOffScreen.CheckStatus() != 0) {
std::cout << DC_ERROR << " Could not create fbOffScreen glfbo::MSFramebuffer.\n";
}
Framebuffer pingpongFBOs[2];
for (int i = 0; i < 2; i++)
{
ColorBuffer *testFBO_Color0 = new ColorBuffer(WINDOW_WIDTH, WINDOW_HEIGHT, BufferType::Texture, GL_RGBA16F, 0);
pingpongFBOs[i].Attach_ColorBuffer(0, testFBO_Color0);
pingpongFBOs[i].Set_DrawBuffers({0});
}
//MSFramebuffer fbOffScreen(WINDOW_WIDTH, WINDOW_HEIGHT, 4, GL_RGB16F);
//if (fbOffScreen.ID == 0)
// return -1;
printscreen_FBO = &pingpongFBOs[0];
glEnable(GL_DEPTH_TEST);
cullFaces = false;
//glEnable(GL_MULTISAMPLE);
/* ------------------ MAIN loop ------------------ */
while (!glfwWindowShouldClose(window)) {
float currentFrame = (float)glfwGetTime();
deltaTime = currentFrame - lastFrame; // cas jak dlouho trval posledni frame v sekundach
lastFrame = currentFrame; // cas kdy zacal tento frame
// input ...
proccessInput(window);
// rendering commands here ...
glEnable(GL_DEPTH_TEST);
if (cullFaces)
glEnable(GL_CULL_FACE);
// 1. Render pass: render to fbOffscreen framebuffer
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glPolygonMode(GL_FRONT_AND_BACK, polygonMode);
glBindFramebuffer(GL_FRAMEBUFFER, fbOffScreen.ID);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
shader->Use();
draw_scene(shader, true);
/* 2. Render pass: blur the texture */
bool horizontal = true, first_iteration = true;
int amount = 10;
for (int i = 0; i < amount; i++)
{
glBindFramebuffer(GL_FRAMEBUFFER, pingpongFBOs[horizontal].ID);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glDisable(GL_CULL_FACE);
glClear(GL_COLOR_BUFFER_BIT);
blurShader->Use();
blurShader->setInt("texture0", 0);
blurShader->setInt("horizontal", horizontal);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, (first_iteration) ? fbOffScreen.Get_ResolvedColorBuffer(1) : pingpongFBOs[!horizontal].Get_ColorBuffer(0));
draw_quad();
horizontal = !horizontal;
if (first_iteration)
first_iteration = false;
}
// 3. Render pass: render quad on screen
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
quadShader->Use();
quadShader->setInt("texture0", 0);
glActiveTexture(GL_TEXTURE0);
uint bufID = fbOffScreen.Get_ResolvedColorBuffer(0);
if (bufID == 0)
break;
glBindTexture(GL_TEXTURE_2D, bufID);
quadShader->setInt("bloomBlur", 1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, pingpongFBOs[0].Get_ColorBuffer(0));
quadShader->setFloat("gamma", shader_gamma);
quadShader->setFloat("exposure", exposure);
draw_quad();
glDisable(GL_FRAMEBUFFER_SRGB);
// check and calls events and swap the buffers
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
delete(shader);
delete(quadShader);
delete(basicShader);
delete(blurShader);
return 0;
}
unsigned int wood_texture = 0, container_texture = 0;
void draw_scene(const Shader* shader, bool lightCube)
{
if (wood_texture == 0) // flipV sRGB
wood_texture = load_texture(TEXTURES_DIR "wood/wood.png", true, true);
if (container_texture == 0)
container_texture = load_texture(TEXTURES_DIR "container2.png", true, true);
glm::mat4 model(1.0f);
glm::mat4 view = myCamera.getViewMatrix();
glm::mat4 projection = glm::perspective(glm::radians(myCamera.FOV),
WINDOW_WIDTH / (float)WINDOW_HEIGHT, 0.1f, 300.0f);
shader->Use();
shader->setFloat("gamma", shader_gamma);
shader->setMat4("projection", projection);
shader->setMat4("view", view);
/* Lighting */
static std::vector<Light> lights = {
// Position Color Ambient Diffuse Specular
{glm::vec3(0.0f, 0.5f, 1.5f), glm::vec3(5.0f, 5.0f, 5.0f), glm::vec3(0.005f), glm::vec3(0.1f), glm::vec3(0.01f)},
{glm::vec3(-4.0f, 0.5f, -3.0f), glm::vec3(10.0f, 0.0f, 0.0f), glm::vec3(0.005f), glm::vec3(0.5f), glm::vec3(0.1f)},
{glm::vec3(3.0f, 0.5f, 1.0f), glm::vec3(0.0f, 0.0f, 15.0f), glm::vec3(0.005f), glm::vec3(0.5f), glm::vec3(0.1f)},
{glm::vec3(-.8f, 2.4f, -1.0f), glm::vec3(0.0f, 5.0f, 0.0f), glm::vec3(0.005f), glm::vec3(0.5f), glm::vec3(0.1f)},
};
shader->setVec3("viewPos", myCamera.Position);
shader->setInt("lights_num", lights.size());
for (int i = 0; i < lights.size(); i++)
{
shader->setVec3("lights[" + std::to_string(i) + "].position", lights[i].Position);
shader->setVec3("lights[" + std::to_string(i) + "].ambient", lights[i].Ambient);
shader->setVec3("lights[" + std::to_string(i) + "].diffuse", lights[i].Diffuse * lights[i].Color);
shader->setVec3("lights[" + std::to_string(i) + "].specular", lights[i].Specular * lights[i].Color);
}
shader->setInt("material.texture_diffuse0", 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, wood_texture);
if (cullFaces)
glEnable(GL_CULL_FACE);
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(0.0f, -1.0f, 0.0));
model = glm::scale(model, glm::vec3(12.5f, 0.5f, 12.5f));
shader->setMat4("model", model);
shader->setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
draw_cube();
// then create multiple cubes as the scenery
shader->setInt("material.texture_diffuse0", 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, container_texture);
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(0.0f, 1.5f, 0.0));
model = glm::scale(model, glm::vec3(0.5f));
shader->setMat4("model", model);
shader->setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
draw_cube();
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(2.0f, 0.0f, 1.0));
model = glm::scale(model, glm::vec3(0.5f));
shader->setMat4("model", model);
shader->setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
draw_cube();
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(-1.0f, -1.0f, 2.0));
model = glm::rotate(model, glm::radians(60.0f), glm::normalize(glm::vec3(1.0, 0.0, 1.0)));
shader->setMat4("model", model);
shader->setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
draw_cube();
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(0.0f, 2.7f, 4.0));
model = glm::rotate(model, glm::radians(23.0f), glm::normalize(glm::vec3(1.0, 0.0, 1.0)));
model = glm::scale(model, glm::vec3(1.25));
shader->setMat4("model", model);
shader->setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
draw_cube();
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(-2.0f, 1.0f, -3.0));
model = glm::rotate(model, glm::radians(124.0f), glm::normalize(glm::vec3(1.0, 0.0, 1.0)));
shader->setMat4("model", model);
shader->setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
draw_cube();
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(-3.0f, 0.0f, 0.0));
model = glm::scale(model, glm::vec3(0.5f));
shader->setMat4("model", model);
shader->setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
draw_cube();
if (lightCube)
{
for (int i = 0; i < lights.size(); i++)
{
basicShader->Use();
model = glm::translate(glm::mat4(1.0f), lights[i].Position);
model = glm::scale(model, glm::vec3(0.1f));
basicShader->setMat4("PVM", projection * view * model);
basicShader->setVec3("Color", lights[i].Color);
draw_cube();
}
}
}
uint8_t init(void) {
// init of glfw window
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_NAME, NULL, NULL);
if (window == NULL) {
cerr << DC_ERROR " Failed to create glfw window" << endl;
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(window);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// init of GLAD
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
cerr << DC_ERROR " Failed to initialize GLAD" << endl;
}
glfwSetFramebufferSizeCallback(window, framebuffersize_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
// glfwSwapInterval(2);
return 0;
}
void framebuffersize_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
WINDOW_HEIGHT = height;
WINDOW_WIDTH = width;
}
void proccessInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) // Escape
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_PRESS) // F1
{
if (polygonMode != GL_FILL)
{
polygonMode = GL_FILL;
}
}
else if (glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS) // F2
{
if (polygonMode != GL_LINE)
{
polygonMode = GL_LINE;
}
}
if (glfwGetKey(window, GLFW_KEY_F3) == GLFW_PRESS) // F3
{
if (printscreen_FBO)
{
string s = (printscreen_FBO)->Export("", ImageType::PNG, 0);
if (s.length() != 0)
std::cout << DC_INFO << " Screenshot saved to '" DC_MAGNETA BINARY_DIR "/" << s << DC_DEFAULT "'\n";
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
if (glfwGetKey(window, GLFW_KEY_F4) == GLFW_PRESS) // F4
{
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
}
else if (glfwGetKey(window, GLFW_KEY_F5) == GLFW_PRESS)
{
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
float exposure_gain_speed = 1.0f; // gain 1.0 per second
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
{
exposure += exposure_gain_speed * deltaTime;
}
else if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
{
if (exposure > 0.0f) {
exposure -= exposure_gain_speed * deltaTime;
}
}
/* -------------------- Movement -------------------- */
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) // w
myCamera.ProccessKeyboard(CAM_FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) // S
myCamera.ProccessKeyboard(CAM_BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) // D
myCamera.ProccessKeyboard(CAM_RIGHT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) // A
myCamera.ProccessKeyboard(CAM_LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) // Left-shift
myCamera.ProccessKeyboard(CAM_DOWN, deltaTime);
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) // Space
myCamera.ProccessKeyboard(CAM_UP, deltaTime);
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
if (firstmouse) {
lastX = (float)xpos;
lastY = (float)ypos;
firstmouse = false;
}
float xoffset = (float)xpos - lastX;
float yoffset = lastY - (float)ypos;
lastX = (float)xpos;
lastY = (float)ypos;
myCamera.ProccessMouse(xoffset, yoffset);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
myCamera.ProccessScroll((float)yoffset);
}
unsigned int load_texture(const char* path, bool flip, bool sRGB, GLint wrapS, GLint wrapT)
{
unsigned int texture = 0;
int width, height, nrChannels, format, intFormat;
stbi_set_flip_vertically_on_load(flip);
unsigned char* data = stbi_load(path, &width, &height, &nrChannels, 0); // load image to the array of bytes (char = 1 byte)
switch (nrChannels)
{
case 4: format = GL_RGBA;
if (sRGB) intFormat = GL_SRGB_ALPHA;
else intFormat = GL_RGBA;
break;
case 3: format = GL_RGB;
if (sRGB) intFormat = GL_SRGB;
else intFormat = GL_RGB;
break;
case 2: format = intFormat = GL_RED; break;
default: format = intFormat = GL_RGB; break;
}
if (data) // data != NULL
{
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, GL_UNSIGNED_BYTE, data); // generates texture
glGenerateMipmap(GL_TEXTURE_2D);
}
else {
cerr << DC_WARNING " Could not load texture '" << path << "'\n";
}
stbi_image_free(data);
glBindTexture(GL_TEXTURE_2D, 0);
return texture;
}
unsigned int load_cubemap(vector<string> faces)
{
unsigned int textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
int width, height, nrChannels, format;
for (size_t i = 0; i < faces.size(); i++)
{
unsigned char* data = stbi_load(faces[i].c_str(), &width, &height, &nrChannels, 0);
switch (nrChannels)
{
case 4: format = GL_RGBA; break;
case 3: format = GL_RGB; break;
case 2: format = GL_RED; break;
default: format = GL_RGB; break;
}
if (data)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + static_cast<int>(i), 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
}
else {
std::cerr << DC_INFO " Failed to load cubemap texture at " << faces[i] << std::endl;
}
stbi_image_free(data);
};
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
return textureID;
}
unsigned int cubeVAO = 0, cubeVBO = 0;
void draw_cube(void)
{
if (cubeVBO == 0 || cubeVAO == 0)
{
float cubeVertices[] = {
// back face
-1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, // bottom-left
1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, // top-right
1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, // bottom-right
1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, // top-right
-1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, // bottom-left
-1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, // top-left
// front face
-1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom-left
1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, // bottom-right
1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // top-right
1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // top-right
-1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, // top-left
-1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom-left
// left face
-1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-right
-1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top-left
-1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-left
-1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-left
-1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // bottom-right
-1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-right
// right face
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-left
1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-right
1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top-right
1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-right
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-left
1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // bottom-left
// bottom face
-1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, // top-right
1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f, // top-left
1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, // bottom-left
1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, // bottom-left
-1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, // bottom-right
-1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, // top-right
// top face
-1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // top-left
1.0f, 1.0f , 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom-right
1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // top-right
1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom-right
-1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // top-left
-1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f // bottom-left
};
glGenVertexArrays(1, &cubeVAO);
glGenBuffers(1, &cubeVBO);
glBindVertexArray(cubeVAO);
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)(3 * sizeof(float)));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)(6 * sizeof(float)));
for (int i = 0; i < 3; i++)
glEnableVertexAttribArray(i);
glBindVertexArray(0);
}
glBindVertexArray(cubeVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
}
unsigned int floorVAO = 0, floorVBO = 0;
void draw_floor(void)
{
if (floorVAO == 0 || floorVBO == 0)
{
float floorVertices[] = {
// positions // normals // texcoords
25.0f, -0.5f, 25.0f, 0.0f, 1.0f, 0.0f, 25.0f, 0.0f,
-25.0f, -0.5f, 25.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-25.0f, -0.5f, -25.0f, 0.0f, 1.0f, 0.0f, 0.0f, 25.0f,
25.0f, -0.5f, 25.0f, 0.0f, 1.0f, 0.0f, 25.0f, 0.0f,
-25.0f, -0.5f, -25.0f, 0.0f, 1.0f, 0.0f, 0.0f, 25.0f,
25.0f, -0.5f, -25.0f, 0.0f, 1.0f, 0.0f, 25.0f, 25.0f
};
glGenVertexArrays(1, &floorVAO);
glGenBuffers(1, &floorVBO);
glBindVertexArray(floorVAO);
glBindBuffer(GL_ARRAY_BUFFER, floorVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(floorVertices), floorVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0); // aPos
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)0);
glEnableVertexAttribArray(1); // aTexCoord
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2); // aNormal
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)(3 * sizeof(float)));
glBindVertexArray(0);
}
glBindVertexArray(floorVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
}
unsigned int quadVAO = 0, quadVBO = 0;
void draw_quad(void)
{
if (quadVAO == 0 || quadVBO == 0)
{
float quadVertices[] = {
// positions // normal // texcoords
-1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.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, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f
};
// configure plane VAO
glGenVertexArrays(1, &quadVAO);
glGenBuffers(1, &quadVBO);
glBindVertexArray(quadVAO);
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0); // Vertex positions
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1); // Vertex normals
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(2); // Vertex texture coordinates
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
}
glBindVertexArray(quadVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
}

View File

@@ -0,0 +1,19 @@
#version 330 core
layout (location = 0) out vec4 FragColor;
layout (location = 1) out vec4 BrightColor;
uniform vec3 Color;
void main()
{
//FragColor = vec4(Color.rgb, 1.0);
// Gamma corrected color
FragColor = vec4(Color.rgb, 1.0);
float brightness = dot(FragColor.rgb, vec3(0.2126, 0.7152, 0.0722));
if (brightness > 1.0)
BrightColor = vec4(FragColor.rgb, 1.0);
else
BrightColor = vec4(0.0);
}

View File

@@ -0,0 +1,10 @@
#version 330 core
layout (location = 0) in vec3 aPos;
uniform mat4 PVM;
void main()
{
gl_Position = PVM * vec4(aPos, 1.0);
}

View File

@@ -0,0 +1,30 @@
#version 330 core
layout (location = 0) out vec4 FragColor;
//out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D texture0;
uniform bool horizontal;
uniform float weight[5] = float[] (0.227027, 0.1945946, 0.1216216, 0.054054, 0.016216);
void main()
{
vec2 tex_offset = 1.0 / textureSize(texture0, 0);
vec3 result = texture(texture0, TexCoords).rgb * weight[0];
if (horizontal) {
for (int i = 0; i < 5; i++) {
result += texture(texture0, TexCoords + vec2(i * tex_offset.x, 0.0)).rgb * weight[i];
result += texture(texture0, TexCoords - vec2(i * tex_offset.x, 0.0)).rgb * weight[i];
}
} else {
for (int i = 0; i < 5; i++) {
result += texture(texture0, TexCoords + vec2(0.0, i * tex_offset.y)).rgb * weight[i];
result += texture(texture0, TexCoords - vec2(0.0, i * tex_offset.y)).rgb * weight[i];
}
}
FragColor = vec4(result.rgb, 1.0);
//FragColor = texture(texture0, TexCoords);
//FragColor = vec4(0.0, 1.0, 1.0, 1.0);
}

View File

@@ -0,0 +1,46 @@
#version 330 core
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D texture0;
uniform sampler2D bloomBlur;
uniform float gamma;
uniform float exposure;
float offset = 1.0 / 300.0;
void main()
{
vec2 offsets[9] = vec2[](
vec2(-offset, offset), // top-left
vec2(0.0, offset), // top-center
vec2(offset, offset), // top-right
vec2(-offset, 0.0), // center-right
vec2(0.0, 0.0), // center-center
vec2(offset, 0.0), // center-right
vec2(-offset, -offset), // bottom-left
vec2(0.0, -offset), // bottom-center
vec2(offset, -offset) // bottom-right
);
float kernel[9] = float[](
0, 0, 0,
0, 1, 0,
0, 0, 0
);
vec3 col = vec3(0.0);
for (int i = 0; i < 9; i++)
{
col += vec3(texture(texture0, TexCoords.st + offsets[i])) * kernel[i];
}
vec3 hdrColor = col;
vec3 bloomColor = texture(bloomBlur, TexCoords).rgb;
hdrColor += bloomColor;
vec3 mapped = vec3(1.0) - exp(-hdrColor * exposure);
mapped = pow(mapped, vec3(1.0 / gamma));
FragColor = vec4(mapped, 1.0f);
}

View File

@@ -0,0 +1,14 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 2) in vec2 aTexCoord;
uniform mat4 PV, model;
out vec2 TexCoords;
void main()
{
TexCoords = aTexCoord;
gl_Position = vec4(aPos, 1.0);
}

View File

@@ -0,0 +1,73 @@
#version 330 core
layout (location = 0) out vec4 FragColor;
layout (location = 1) out vec4 BrightColor;
struct Light {
vec3 position;
vec3 diffuse, specular, ambient;
};
struct Material {
sampler2D texture_diffuse0;
sampler2D texture_normal0;
sampler2D texture_height0;
sampler2D texture_specular0;
};
in VS_OUT
{
vec3 Normal;
vec2 TexCoords;
vec3 FragPos;
} fs_in; // fragment shader in
uniform Material material;
uniform Light lights[16];
uniform vec3 viewPos;
uniform bool inverse_normals;
uniform int lights_num;
uniform float gamma;
vec3 CalcPointLight(Light light, vec3 color, vec3 normal, vec3 viewDir);
void main()
{
vec3 color = texture(material.texture_diffuse0, fs_in.TexCoords).rgb;
vec3 normal = normalize(fs_in.Normal) * ((inverse_normals) ? -1.0 : 1.0);
vec3 viewDir = normalize(viewPos - fs_in.FragPos);
vec3 pointlight = vec3(0.0);
for (int i = 0; i < lights_num; i++)
pointlight += CalcPointLight(lights[i], color, normal, viewDir);
pointlight += 0.0025 * color; // ambient
FragColor = vec4(pointlight, 1.0);
float brightness = dot(FragColor.rgb, vec3(0.2126, 0.7152, 0.0722));
if (brightness > 1.0)
BrightColor = vec4(FragColor.rgb, 1.0);
else
BrightColor = vec4(0.0);
}
vec3 CalcPointLight(Light light, vec3 color, vec3 normal, vec3 viewDir)
{
vec3 lightDir = normalize(light.position - fs_in.FragPos);
float fragDist = length(light.position - fs_in.FragPos);
//float attenuation = 1.0 / (1.0 + 0.14 * fragDist + 0.07 * fragDist * fragDist);
float attenuation = 1.0 / (fragDist * fragDist);
// ambient
vec3 ambient = light.ambient * color;
// diffuse
float diff = max(dot(lightDir, normal), 0.0);
vec3 diffuse = diff * light.diffuse * color;
// specular
vec3 halfwayDir = normalize(lightDir + viewDir);
float spec = pow(max(dot(halfwayDir, normal), 0.0), 128.0);
vec3 specular = spec * light.specular; //* texture(material.texture_specular0, texCoords);
return (diffuse + specular) * attenuation;
}

View File

@@ -0,0 +1,27 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
out VS_OUT
{
vec3 Normal;
vec2 TexCoords;
vec3 FragPos;
};
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
uniform mat3 normalMatrix;
void main()
{
FragPos = vec3(model * vec4(aPos, 1.0)); // Fragment position in world space
Normal = normalMatrix * aNormal;
TexCoords = aTexCoords;
gl_Position = projection * view * model * vec4(aPos, 1.0);
}