Commit 6602b012 authored by Bastian Krayer's avatar Bastian Krayer

cg2 commit

parent a8e5094f
......@@ -72,7 +72,7 @@ void charCallback(GLFWwindow* window, unsigned int key) {
void resizeCallback(GLFWwindow* window, int w, int h) {
cam_trackball->setWidthHeight(w, h);
cam_trackball->getProjection()->updateRatio(w / (float)h);
cam_trackball->getProjection().updateRatio(w / (float)h);
glViewport(0, 0, w, h);
}
......@@ -80,7 +80,7 @@ void init_camera() {
// Camera
cam_trackball = new CVK::Trackball(window, WIDTH, HEIGHT, &projection);
glm::vec3 center(0.0f, 0.0f, 0.0f);
cam_trackball->setCenter(&center);
cam_trackball->setCenter(center);
cam_trackball->setRadius(5.0f);
CVK::State::getInstance()->setCamera(cam_trackball);
}
......
cmake_minimum_required(VERSION 2.8)
include(${CMAKE_MODULE_PATH}/DefaultExecutable.cmake)
\ No newline at end of file
#include "ShaderCubeMapReflection.h"
ShaderCubeMapReflection::ShaderCubeMapReflection(
GLuint shaderMask, const std::vector<std::string>& shaderNames)
: CVK::ShaderMinimal(shaderMask, shaderNames) {
// Skybox
m_cubeMapID = glGetUniformLocation(m_ProgramID, "cubeMap");
// Color
m_diffuseColorID = glGetUniformLocation(m_ProgramID, "mat.diffuse");
m_specularID = glGetUniformLocation(m_ProgramID, "mat.specular");
m_shininessID = glGetUniformLocation(m_ProgramID, "mat.shininess");
m_reflectionStrengthID =
glGetUniformLocation(m_ProgramID, "mat.reflectionStrength");
// Texture
m_useColorTexture = glGetUniformLocation(m_ProgramID, "useColorTexture");
m_colorTextureID = glGetUniformLocation(m_ProgramID, "colorTexture");
// Light
m_lightposID = glGetUniformLocation(m_ProgramID, "light.pos");
m_lightcolID = glGetUniformLocation(m_ProgramID, "light.col");
m_lightsdirID = glGetUniformLocation(m_ProgramID, "light.spot_direction");
m_lightsexpID = glGetUniformLocation(m_ProgramID, "light.spot_exponent");
m_lightscutID = glGetUniformLocation(m_ProgramID, "light.spot_cutoff");
}
void ShaderCubeMapReflection::update() {
ShaderMinimal::update();
CVK::Light& light = CVK::State::getInstance()->getLights()->at(0);
glUniform4fv(m_lightposID, 1, glm::value_ptr(light.getPosition()));
glUniform3fv(m_lightcolID, 1, glm::value_ptr(light.getColor()));
glUniform3fv(m_lightsdirID, 1, glm::value_ptr(light.getSpotDirection()));
glUniform1f(m_lightsexpID, light.getSpotExponent());
glUniform1f(m_lightscutID, light.getSpotCutoff());
glUniform3fv(m_lightambID, 1,
glm::value_ptr(CVK::State::getInstance()->getLightAmbient()));
/////////////////////////////////////////////////////
//////////////// Cube Map Reflection ////////////////
/////////////////////////////////////////////////////
glUniform1i(m_cubeMapID, 3);
glActiveTexture(CUBE_MAP_TEXTURE_UNIT);
CVK::CubeMapTexture* cubeMap =
CVK::State::getInstance()->getCubeMapTexture();
if (cubeMap != 0) {
cubeMap->bind();
}
}
void ShaderCubeMapReflection::update(CVK::Node* node) {
ShaderMinimal::update(node);
if (node->hasMaterial()) {
CVK::Material* mat = node->getMaterial();
CVK::Texture* color_texture;
glUniform3fv(m_diffuseColorID, 1, glm::value_ptr(mat->getDiffColor()));
glUniform3fv(m_specularID, 1, glm::value_ptr(mat->getSpecColor()));
glUniform1f(m_shininessID, mat->getShininess());
glUniform1f(m_reflectionStrengthID, mat->getKs());
bool colorTexture = mat->hasTexture(CVK::COLOR_TEXTURE);
glUniform1i(m_useColorTexture, colorTexture);
if (colorTexture) {
glUniform1i(m_colorTextureID, 0);
glActiveTexture(COLOR_TEXTURE_UNIT);
color_texture = mat->getTexture(CVK::COLOR_TEXTURE);
color_texture->bind();
}
}
}
#ifndef __SHADER_CUBE_MAP_REFLECTION_H
#define __SHADER_CUBE_MAP_REFLECTION_H
#include <CVK_2/CVK_Defs.h>
#include <CVK_2/CVK_Material.h>
#include <CVK_2/CVK_ShaderMinimal.h>
#include <CVK_2/CVK_Texture.h>
class ShaderCubeMapReflection : public CVK::ShaderMinimal {
public:
ShaderCubeMapReflection(GLuint shaderMask,
const std::vector<std::string>& shaderNames);
void update();
void update(CVK::Node* node);
private:
GLuint m_cubeMapID;
GLuint m_diffuseColorID, m_reflectionStrengthID;
GLuint m_specularID, m_shininessID;
GLuint m_colorTextureID, m_useColorTexture;
GLuint m_lightambID;
GLuint m_lightposID, m_lightcolID, m_lightsdirID, m_lightsexpID,
m_lightscutID;
};
#endif /*__SHADER_CUBE_MAP_REFLECTION_H*/
\ No newline at end of file
#include "ShaderSkyBox.h"
ShaderSkyBox::ShaderSkyBox(GLuint shaderMask,
const std::vector<std::string>& shaderNames)
: CVK::ShaderMinimal(shaderMask, shaderNames) {
m_cubeMapID = glGetUniformLocation(m_ProgramID, "cubeMap");
}
void ShaderSkyBox::update() {
ShaderMinimal::update();
glUniform1i(m_cubeMapID, 0);
glActiveTexture(COLOR_TEXTURE_UNIT);
CVK::CubeMapTexture* cubeMap =
CVK::State::getInstance()->getCubeMapTexture();
if (cubeMap != 0) {
cubeMap->bind();
}
}
#ifndef __SHADER_SKY_BOX_H
#define __SHADER_SKY_BOX_H
#include <CVK_2/CVK_Defs.h>
#include <CVK_2/CVK_ShaderMinimal.h>
#include <string>
#include <vector>
class ShaderSkyBox : public CVK::ShaderMinimal {
public:
ShaderSkyBox(GLuint shaderMask,
const std::vector<std::string>& ShaderNames);
void update() override;
private:
GLuint m_cubeMapID;
};
#endif /*__SHADER_SKY_BOX_H*/
\ No newline at end of file
CVK_2
\ No newline at end of file
#include "ShaderCubeMapReflection.h"
#include "ShaderSkyBox.h"
#include <CVK_2/CVK_Framework.h>
const int WIDTH = 800;
const int HEIGHT = 800;
// Window
GLFWwindow* window = nullptr;
int initWindow() {
glfwInit();
CVK::useOpenGL33CoreProfile();
window = glfwCreateWindow(WIDTH, HEIGHT, "Cube Mapping", 0, 0);
glfwSetWindowPos(window, 100, 50);
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize OpenGL context" << std::endl;
return -1;
}
return 0;
}
// Camera
CVK::Perspective projection(glm::radians(60.0f), WIDTH / (float)HEIGHT, 0.1f,
10.f);
CVK::Trackball* cam_trackball;
void resizeCallback(GLFWwindow* window, int w, int h) {
cam_trackball->setWidthHeight(w, h);
cam_trackball->getProjection().updateRatio(w / (float)h);
glViewport(0, 0, w, h);
}
void initCamera() {
cam_trackball = new CVK::Trackball(window, WIDTH, HEIGHT, &projection);
glm::vec3 center(0.0f, 0.0f, 0.0f);
cam_trackball->setCenter(center);
cam_trackball->setRadius(5.0f);
CVK::State::getInstance()->setCamera(cam_trackball);
glfwSetWindowSizeCallback(window, resizeCallback);
}
// Shaders
CVK::ShaderMinimal *skyboxShader, *reflectionShader;
void initOpenGL() {
// Shaders
skyboxShader =
new ShaderSkyBox(VERTEX_SHADER_BIT | FRAGMENT_SHADER_BIT,
{CVK::State::getInstance()->getShaderPath() +
"/CubeMapping/SkyBox.vert",
CVK::State::getInstance()->getShaderPath() +
"/CubeMapping/SkyBox.frag"});
reflectionShader = new ShaderCubeMapReflection(
VERTEX_SHADER_BIT | FRAGMENT_SHADER_BIT,
{CVK::State::getInstance()->getShaderPath() +
"/CubeMapping/CubeMapReflection.vert",
CVK::State::getInstance()->getShaderPath() +
"/CubeMapping/CubeMapReflection.frag"});
// OpenGL parameters
glClearColor(1.0, 1.0, 1.0, 0.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
// Scene
CVK::CubeMapTexture* cubeMap;
CVK::Node *earthNode, *moonNode;
CVK::Geometry *teapot, *sphere, *cube;
CVK::Material *mat_blue, *mat_red;
CVK::Light* plight;
void initScene() {
// Textures
std::vector<std::string> textureNames = {
CVK::State::getInstance()->getResourcesPath() + "/cubeMap/posx.jpg",
CVK::State::getInstance()->getResourcesPath() + "/cubeMap/negx.jpg",
CVK::State::getInstance()->getResourcesPath() + "/cubeMap/posy.jpg",
CVK::State::getInstance()->getResourcesPath() + "/cubeMap/negy.jpg",
CVK::State::getInstance()->getResourcesPath() + "/cubeMap/posz.jpg",
CVK::State::getInstance()->getResourcesPath() + "/cubeMap/negz.jpg"};
cubeMap = new CVK::CubeMapTexture(textureNames);
// Geometries
teapot = new CVK::Teapot;
sphere = new CVK::Sphere(0.3f);
cube = new CVK::Cube(1.0f);
// Materials
mat_blue = new CVK::Material(BLUE, WHITE, 100.0f);
mat_red = new CVK::Material(RED, WHITE, 100.0f);
// Nodes
earthNode = new CVK::Node("Earth");
earthNode->setGeometry(teapot);
earthNode->setMaterial(mat_blue);
moonNode = new CVK::Node("Moon");
moonNode->setGeometry(sphere);
moonNode->setMaterial(mat_red);
moonNode->setModelMatrix(
glm::translate(glm::mat4(1.0f), glm::vec3(3, 0, 0)));
earthNode->addChild(moonNode);
// Lights
plight = new CVK::Light(glm::vec4(0, 1, 1, 0), GREY, glm::vec3(0, 0, 0),
1.0f, 0.0f);
CVK::State::getInstance()->addLight(plight);
// Settings (ambient and fog)
CVK::State::getInstance()->updateSceneSettings(DARKGREY, 0, WHITE, 1, 10,
1);
}
void animate(float deltaTime) {
// rotate in relation to time
earthNode->setModelMatrix(glm::rotate(*earthNode->getModelMatrix(),
glm::radians(30.0f) * deltaTime,
glm::vec3(0.0, 1.0, 0.0)));
moonNode->setModelMatrix(glm::rotate(*moonNode->getModelMatrix(),
glm::radians(90.0f) * deltaTime,
glm::vec3(1.0, 1.0, 1.0)));
}
void cleanup() {
delete earthNode;
delete moonNode;
delete teapot;
delete sphere;
delete mat_blue;
delete mat_red;
delete plight;
delete skyboxShader;
delete reflectionShader;
delete cam_trackball;
glfwDestroyWindow(window);
glfwTerminate();
}
// Settings
float reflection = 0.7f;
bool showSkyBox = true;
void charCallback(GLFWwindow* window, unsigned int key) {
switch (key) {
case 'W':
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
break;
case 'w':
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
break;
case '+':
reflection += 0.1f;
if (reflection > 1)
reflection = 1;
break;
case '-':
reflection -= 0.1f;
if (reflection < 0)
reflection = 0;
break;
case 's':
showSkyBox = !showSkyBox;
break;
}
}
int main() {
initWindow();
initCamera();
initOpenGL();
initScene();
glfwSetWindowSizeCallback(window, resizeCallback);
glfwSetCharCallback(window, charCallback);
CVK::State::getInstance()->setCubeMapTexture(cubeMap);
double startTime = glfwGetTime();
while (!glfwWindowShouldClose(window)) {
float deltaT = glfwGetTime() - startTime;
startTime = glfwGetTime();
// update
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
animate(deltaT);
mat_blue->setKs(reflection);
mat_red->setKs(reflection);
cam_trackball->update(deltaT);
if (showSkyBox) {
glDisable(GL_DEPTH_TEST);
CVK::State::getInstance()->setShader(skyboxShader);
skyboxShader->update();
cube->render();
glEnable(GL_DEPTH_TEST);
}
// update shader and render
CVK::State::getInstance()->setShader(reflectionShader);
reflectionShader->update();
earthNode->render();
glfwSwapBuffers(window);
glfwPollEvents();
}
cleanup();
return 0;
}
cmake_minimum_required(VERSION 2.8)
include(${CMAKE_MODULE_PATH}/DefaultExecutable.cmake)
\ No newline at end of file
#include "ShaderCompositing.h"
ShaderCompositing::ShaderCompositing(
GLuint shaderMask, const std::vector<std::string>& shaderPaths)
: CVK::ShaderPostProcessing(shaderMask, shaderPaths) {
m_lightMapID = glGetUniformLocation(m_ProgramID, "lightMap");
m_colorMapID = glGetUniformLocation(m_ProgramID, "colorMap");
}
void ShaderCompositing::update() {
if (m_textures.size() > 0) {
glUniform1i(m_lightMapID, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_textures[0]);
}
if (m_textures.size() > 1) {
glUniform1i(m_colorMapID, 1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_textures[1]);
}
}
#ifndef __SHADER_COMPOSITING_H
#define __SHADER_COMPOSITING_H
#include <CVK_2/CVK_Defs.h>
#include <CVK_2/CVK_ShaderPostProcessing.h>
#include <CVK_2/CVK_State.h>
class ShaderCompositing : public CVK::ShaderPostProcessing {
public:
ShaderCompositing(GLuint shaderMask,
const std::vector<std::string>& shaderPaths);
void update() override;
private:
GLuint m_lightMapID, m_colorMapID;
};
#endif /*__SHADER_COMPOSITING_H*/
\ No newline at end of file
#include "ShaderGBuffer.h"
ShaderGBuffer::ShaderGBuffer(GLuint shaderMask,
const std::vector<std::string>& shaderNames)
: CVK::ShaderMinimal(shaderMask, shaderNames) {
m_useColorTextureID = glGetUniformLocation(m_ProgramID, "useColorTexture");
m_colorTexID = glGetUniformLocation(m_ProgramID, "colortexture");
m_diffuseColorID = glGetUniformLocation(m_ProgramID, "diffuseColor");
m_alphaID = glGetUniformLocation(m_ProgramID, "alpha");
}
void ShaderGBuffer::update() { ShaderMinimal::update(); }
void ShaderGBuffer::update(CVK::Node* node) {
// COLOR_TEXTURE_UNIT
glUniform1i(m_colorTexID, 0);
glActiveTexture(COLOR_TEXTURE_UNIT);
glBindTexture(GL_TEXTURE_2D, 0);
if (node->hasMaterial()) {
CVK::Material* mat = node->getMaterial();
glm::vec3 diffuseColor = mat->getDiffColor();
glUniform3f(m_diffuseColorID, diffuseColor.r, diffuseColor.g,
diffuseColor.b);
bool colorTexture = mat->hasTexture(CVK::COLOR_TEXTURE);
glUniform1i(m_useColorTextureID, colorTexture);
if (mat->hasTexture(CVK::COLOR_TEXTURE)) {
mat->getTexture(CVK::COLOR_TEXTURE)->bind();
}
}
}
#ifndef __SHADER_GBUFFER_H
#define __SHADER_GBUFFER_H
#include <CVK_2/CVK_Camera.h>
#include <CVK_2/CVK_Defs.h>
#include <CVK_2/CVK_ShaderMinimal.h>
#include <CVK_2/CVK_Texture.h>
class ShaderGBuffer : public CVK::ShaderMinimal {
public:
ShaderGBuffer(GLuint shaderMask,
const std::vector<std::string>& shaderNames);
void update() override;
void update(CVK::Node* node) override;
private:
GLuint m_useColorTextureID, m_colorTexID;
GLuint m_diffuseColorID, m_alphaID;
};
#endif /*__SHADER_GBUFFER_H*/
\ No newline at end of file
#include "ShaderLighting.h"
ShaderLighting::ShaderLighting(GLuint shaderMask,
const std::vector<std::string>& shaderPaths)
: CVK::ShaderMinimal(shaderMask, shaderPaths) {
m_positionMapID = glGetUniformLocation(m_ProgramID, "positionMap");
m_normalMapID = glGetUniformLocation(m_ProgramID, "normalMap");
m_colorMapID = glGetUniformLocation(m_ProgramID, "colorMap");
}
void ShaderLighting::update() {
ShaderMinimal::update();
if (m_textures.size() > 0) {
glUniform1i(m_positionMapID, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_textures[0]);
}
if (m_textures.size() > 1) {
glUniform1i(m_normalMapID, 1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_textures[1]);
}
if (m_textures.size() > 2) {
glUniform1i(m_colorMapID, 2);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, m_textures[2]);
}
}
void ShaderLighting::update(CVK::Node* node) {}
#ifndef __SHADER_LIGHTING_H
#define __SHADER_LIGHTING_H
#include <CVK_2/CVK_Defs.h>
#include <CVK_2/CVK_ShaderMinimal.h>
#include <CVK_2/CVK_State.h>
class ShaderLighting : public CVK::ShaderMinimal {
public:
ShaderLighting(GLuint shaderMask,
const std::vector<std::string>& shaderPaths);
void update() override;
void update(CVK::Node* node) override;
private:
GLuint m_positionMapID, m_normalMapID, m_colorMapID;
};
#endif /*__SHADER_LIGHTING_H*/
\ No newline at end of file
#include <CVK_2/CVK_Framework.h>
#include "ShaderCompositing.h"
#include "ShaderGBuffer.h"
#include "ShaderLighting.h"
const int WIDTH = 800;
const int HEIGHT = 800;
// Window
GLFWwindow* window = nullptr;
int initWindow() {
glfwInit();
CVK::useOpenGL33CoreProfile();
window = glfwCreateWindow(WIDTH, HEIGHT, "Deferred Shading", 0, 0);
glfwSetWindowPos(window, 100, 50);
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize OpenGL context" << std::endl;
return -1;
}
return 0;
}
// Camera
CVK::Perspective projection(glm::radians(60.0f), WIDTH / (float)HEIGHT, 0.1f,
100.f);
CVK::Trackball* cam_trackball;
void resizeCallback(GLFWwindow* window, int w, int h) {
cam_trackball->setWidthHeight(w, h);
cam_trackball->getProjection().updateRatio(w / (float)h);
glViewport(0, 0, w, h);
}
void initCamera() {
cam_trackball = new CVK::Trackball(window, WIDTH, HEIGHT, &projection);
glm::vec3 center(0.0f, 0.0f, 0.0f);
cam_trackball->setCenter(center);
cam_trackball->setRadius(5.0f);
CVK::State::getInstance()->setCamera(cam_trackball);
}
// Shaders
CVK::ShaderMinimal *gBufferShader, *lightingShader;
CVK::ShaderPostProcessing *compositingShader, *simpleTextureShader;
CVK::FBO *fbo, *fboLighting;
void initOpenGL() {
// FBOs
fbo = new CVK::FBO(WIDTH, HEIGHT, 3, true);
fboLighting = new CVK::FBO(WIDTH, HEIGHT, 1, true);
// Shaders
gBufferShader =
new ShaderGBuffer(VERTEX_SHADER_BIT | FRAGMENT_SHADER_BIT,
{CVK::State::getInstance()->getShaderPath() +
"/DeferredShading/GBuffer.vert",
CVK::State::getInstance()->getShaderPath() +
"/DeferredShading/GBuffer.frag"});
lightingShader =
new ShaderLighting(VERTEX_SHADER_BIT | FRAGMENT_SHADER_BIT,
{CVK::State::getInstance()->getShaderPath() +
"/DeferredShading/lighting.vert",
CVK::State::getInstance()->getShaderPath() +
"/DeferredShading/lighting.frag"});
lightingShader->setTextureInput(0, fbo->getColorTexture(0));
lightingShader->setTextureInput(1, fbo->getColorTexture(1));
lightingShader->setTextureInput(2, fbo->getColorTexture(2));
compositingShader =
new ShaderCompositing(VERTEX_SHADER_BIT | FRAGMENT_SHADER_BIT,
{CVK::State::getInstance()->getShaderPath() +
"/DeferredShading/screenFill.vert",
CVK::State::getInstance()->getShaderPath() +
"/DeferredShading/finalCompositing.frag"});
compositingShader->setTextureInput(0, fboLighting->getColorTexture(0));