|
|
|
@@ -1,118 +1,101 @@
|
|
|
|
// GLEW
|
|
|
|
#include <glad/glad.h>
|
|
|
|
#define GLEW_STATIC
|
|
|
|
|
|
|
|
#include <GL/glew.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GLFW
|
|
|
|
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
|
|
|
|
|
|
|
// GL includes
|
|
|
|
|
|
|
|
#include <learnopengl/shader.h>
|
|
|
|
|
|
|
|
#include <learnopengl/camera.h>
|
|
|
|
|
|
|
|
#include <learnopengl/model.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GLM Mathemtics
|
|
|
|
|
|
|
|
#include <glm/glm.hpp>
|
|
|
|
#include <glm/glm.hpp>
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
// Other Libs
|
|
|
|
|
|
|
|
#include <SOIL.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <random> // necessary for generation of random floats (for sample kernel and noise texture)
|
|
|
|
|
|
|
|
#include <learnopengl/filesystem.h>
|
|
|
|
#include <learnopengl/filesystem.h>
|
|
|
|
|
|
|
|
#include <learnopengl/shader.h>
|
|
|
|
|
|
|
|
#include <learnopengl/camera.h>
|
|
|
|
|
|
|
|
#include <learnopengl/model.h>
|
|
|
|
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
#include <iostream>
|
|
|
|
const GLuint SCR_WIDTH = 1280, SCR_HEIGHT = 720;
|
|
|
|
#include <random>
|
|
|
|
|
|
|
|
|
|
|
|
// Function prototypes
|
|
|
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
|
|
|
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
|
|
|
|
|
|
|
|
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
|
|
|
|
|
|
|
|
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
|
|
|
|
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
|
|
|
|
void Do_Movement();
|
|
|
|
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
|
|
|
|
void RenderCube();
|
|
|
|
void processInput(GLFWwindow *window);
|
|
|
|
void RenderQuad();
|
|
|
|
unsigned int loadTexture(const char *path, bool gammaCorrection);
|
|
|
|
|
|
|
|
void renderQuad();
|
|
|
|
|
|
|
|
void renderCube();
|
|
|
|
|
|
|
|
|
|
|
|
// Camera
|
|
|
|
// settings
|
|
|
|
|
|
|
|
const unsigned int SCR_WIDTH = 1280;
|
|
|
|
|
|
|
|
const unsigned int SCR_HEIGHT = 720;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// camera
|
|
|
|
Camera camera(glm::vec3(0.0f, 0.0f, 5.0f));
|
|
|
|
Camera camera(glm::vec3(0.0f, 0.0f, 5.0f));
|
|
|
|
|
|
|
|
float lastX = (float)SCR_WIDTH / 2.0;
|
|
|
|
|
|
|
|
float lastY = (float)SCR_HEIGHT / 2.0;
|
|
|
|
|
|
|
|
bool firstMouse = true;
|
|
|
|
|
|
|
|
|
|
|
|
// Delta
|
|
|
|
// timing
|
|
|
|
GLfloat deltaTime = 0.0f;
|
|
|
|
float deltaTime = 0.0f;
|
|
|
|
GLfloat lastFrame = 0.0f;
|
|
|
|
float lastFrame = 0.0f;
|
|
|
|
|
|
|
|
|
|
|
|
// Options
|
|
|
|
float lerp(float a, float b, float f)
|
|
|
|
GLuint draw_mode = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GLfloat lerp(GLfloat a, GLfloat b, GLfloat f)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return a + f * (b - a);
|
|
|
|
return a + f * (b - a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// The MAIN function, from here we start our application and run our Game loop
|
|
|
|
|
|
|
|
int main()
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Init GLFW
|
|
|
|
// glfw: initialize and configure
|
|
|
|
|
|
|
|
// ------------------------------
|
|
|
|
glfwInit();
|
|
|
|
glfwInit();
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", nullptr, nullptr); // Windowed
|
|
|
|
// glfw window creation
|
|
|
|
|
|
|
|
// --------------------
|
|
|
|
|
|
|
|
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
|
|
|
|
glfwMakeContextCurrent(window);
|
|
|
|
glfwMakeContextCurrent(window);
|
|
|
|
|
|
|
|
if (window == NULL)
|
|
|
|
// Set the required callback functions
|
|
|
|
{
|
|
|
|
glfwSetKeyCallback(window, key_callback);
|
|
|
|
std::cout << "Failed to create GLFW window" << std::endl;
|
|
|
|
|
|
|
|
glfwTerminate();
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
|
|
|
glfwSetCursorPosCallback(window, mouse_callback);
|
|
|
|
glfwSetCursorPosCallback(window, mouse_callback);
|
|
|
|
glfwSetScrollCallback(window, scroll_callback);
|
|
|
|
glfwSetScrollCallback(window, scroll_callback);
|
|
|
|
|
|
|
|
|
|
|
|
// Options
|
|
|
|
// tell GLFW to capture our mouse
|
|
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
|
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize GLEW to setup the OpenGL Function pointers
|
|
|
|
// glad: load all OpenGL function pointers
|
|
|
|
glewExperimental = GL_TRUE;
|
|
|
|
// ---------------------------------------
|
|
|
|
glewInit();
|
|
|
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
|
|
|
glGetError();
|
|
|
|
{
|
|
|
|
|
|
|
|
std::cout << "Failed to initialize GLAD" << std::endl;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Define the viewport dimensions
|
|
|
|
// configure global opengl state
|
|
|
|
glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
|
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
// Setup some OpenGL options
|
|
|
|
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
|
|
|
|
|
|
|
|
// Setup and compile our shaders
|
|
|
|
// build and compile shaders
|
|
|
|
Shader shaderGeometryPass("ssao_geometry.vs", "ssao_geometry.frag");
|
|
|
|
// -------------------------
|
|
|
|
Shader shaderLightingPass("ssao.vs", "ssao_lighting.frag");
|
|
|
|
Shader shaderGeometryPass("9.ssao_geometry.vs", "9.ssao_geometry.fs");
|
|
|
|
Shader shaderSSAO("ssao.vs", "ssao.frag");
|
|
|
|
Shader shaderLightingPass("9.ssao.vs", "9.ssao_lighting.fs");
|
|
|
|
Shader shaderSSAOBlur("ssao.vs", "ssao_blur.frag");
|
|
|
|
Shader shaderSSAO("9.ssao.vs", "9.ssao.fs");
|
|
|
|
|
|
|
|
Shader shaderSSAOBlur("9.ssao.vs", "9.ssao_blur.fs");
|
|
|
|
|
|
|
|
|
|
|
|
// Set samplers
|
|
|
|
// load models
|
|
|
|
shaderLightingPass.Use();
|
|
|
|
// -----------
|
|
|
|
glUniform1i(glGetUniformLocation(shaderLightingPass.Program, "gPosition"), 0);
|
|
|
|
Model nanosuit(FileSystem::getPath("resources/objects/nanosuit/nanosuit.obj"));
|
|
|
|
glUniform1i(glGetUniformLocation(shaderLightingPass.Program, "gNormal"), 1);
|
|
|
|
|
|
|
|
glUniform1i(glGetUniformLocation(shaderLightingPass.Program, "gAlbedo"), 2);
|
|
|
|
|
|
|
|
glUniform1i(glGetUniformLocation(shaderLightingPass.Program, "ssao"), 3);
|
|
|
|
|
|
|
|
shaderSSAO.Use();
|
|
|
|
|
|
|
|
glUniform1i(glGetUniformLocation(shaderSSAO.Program, "gPosition"), 0);
|
|
|
|
|
|
|
|
glUniform1i(glGetUniformLocation(shaderSSAO.Program, "gNormal"), 1);
|
|
|
|
|
|
|
|
glUniform1i(glGetUniformLocation(shaderSSAO.Program, "texNoise"), 2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Objects
|
|
|
|
// configure g-buffer framebuffer
|
|
|
|
Model nanosuit(FileSystem::getPath("resources/objects/nanosuit/nanosuit.obj").c_str());
|
|
|
|
// ------------------------------
|
|
|
|
|
|
|
|
unsigned int gBuffer;
|
|
|
|
// Lights
|
|
|
|
|
|
|
|
glm::vec3 lightPos = glm::vec3(2.0, 4.0, -2.0);
|
|
|
|
|
|
|
|
glm::vec3 lightColor = glm::vec3(0.2, 0.2, 0.7);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set up G-Buffer
|
|
|
|
|
|
|
|
// 3 textures:
|
|
|
|
|
|
|
|
// 1. Positions (RGB)
|
|
|
|
|
|
|
|
// 2. Color (RGB)
|
|
|
|
|
|
|
|
// 3. Normals (RGB)
|
|
|
|
|
|
|
|
GLuint gBuffer;
|
|
|
|
|
|
|
|
glGenFramebuffers(1, &gBuffer);
|
|
|
|
glGenFramebuffers(1, &gBuffer);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);
|
|
|
|
GLuint gPosition, gNormal, gAlbedo;
|
|
|
|
unsigned int gPosition, gNormal, gAlbedo;
|
|
|
|
// - Position buffer
|
|
|
|
// position color buffer
|
|
|
|
glGenTextures(1, &gPosition);
|
|
|
|
glGenTextures(1, &gPosition);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gPosition);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gPosition);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGB, GL_FLOAT, NULL);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGB, GL_FLOAT, NULL);
|
|
|
|
@@ -121,39 +104,41 @@ int main()
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gPosition, 0);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gPosition, 0);
|
|
|
|
// - Normal color buffer
|
|
|
|
// normal color buffer
|
|
|
|
glGenTextures(1, &gNormal);
|
|
|
|
glGenTextures(1, &gNormal);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gNormal);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gNormal);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGB, GL_FLOAT, NULL);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGB, GL_FLOAT, NULL);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, gNormal, 0);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, gNormal, 0);
|
|
|
|
// - Albedo color buffer
|
|
|
|
// color + specular color buffer
|
|
|
|
glGenTextures(1, &gAlbedo);
|
|
|
|
glGenTextures(1, &gAlbedo);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gAlbedo);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gAlbedo);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, gAlbedo, 0);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, gAlbedo, 0);
|
|
|
|
// - Tell OpenGL which color attachments we'll use (of this framebuffer) for rendering
|
|
|
|
// tell OpenGL which color attachments we'll use (of this framebuffer) for rendering
|
|
|
|
GLuint attachments[3] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
|
|
|
|
unsigned int attachments[3] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
|
|
|
|
glDrawBuffers(3, attachments);
|
|
|
|
glDrawBuffers(3, attachments);
|
|
|
|
// - Create and attach depth buffer (renderbuffer)
|
|
|
|
// create and attach depth buffer (renderbuffer)
|
|
|
|
GLuint rboDepth;
|
|
|
|
unsigned int rboDepth;
|
|
|
|
glGenRenderbuffers(1, &rboDepth);
|
|
|
|
glGenRenderbuffers(1, &rboDepth);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, rboDepth);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, rboDepth);
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, SCR_WIDTH, SCR_HEIGHT);
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, SCR_WIDTH, SCR_HEIGHT);
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboDepth);
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboDepth);
|
|
|
|
// - Finally check if framebuffer is complete
|
|
|
|
// finally check if framebuffer is complete
|
|
|
|
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
|
|
|
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
|
|
|
std::cout << "GBuffer Framebuffer not complete!" << std::endl;
|
|
|
|
std::cout << "Framebuffer not complete!" << std::endl;
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
|
|
|
|
|
|
|
|
// Also create framebuffer to hold SSAO processing stage
|
|
|
|
// also create framebuffer to hold SSAO processing stage
|
|
|
|
GLuint ssaoFBO, ssaoBlurFBO;
|
|
|
|
// -----------------------------------------------------
|
|
|
|
|
|
|
|
unsigned int ssaoFBO, ssaoBlurFBO;
|
|
|
|
glGenFramebuffers(1, &ssaoFBO); glGenFramebuffers(1, &ssaoBlurFBO);
|
|
|
|
glGenFramebuffers(1, &ssaoFBO); glGenFramebuffers(1, &ssaoBlurFBO);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, ssaoFBO);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, ssaoFBO);
|
|
|
|
GLuint ssaoColorBuffer, ssaoColorBufferBlur;
|
|
|
|
unsigned int ssaoColorBuffer, ssaoColorBufferBlur;
|
|
|
|
// - SSAO color buffer
|
|
|
|
// SSAO color buffer
|
|
|
|
glGenTextures(1, &ssaoColorBuffer);
|
|
|
|
glGenTextures(1, &ssaoColorBuffer);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, ssaoColorBuffer);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, ssaoColorBuffer);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGB, GL_FLOAT, NULL);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGB, GL_FLOAT, NULL);
|
|
|
|
@@ -162,7 +147,7 @@ int main()
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ssaoColorBuffer, 0);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ssaoColorBuffer, 0);
|
|
|
|
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
|
|
|
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
|
|
|
std::cout << "SSAO Framebuffer not complete!" << std::endl;
|
|
|
|
std::cout << "SSAO Framebuffer not complete!" << std::endl;
|
|
|
|
// - and blur stage
|
|
|
|
// and blur stage
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, ssaoBlurFBO);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, ssaoBlurFBO);
|
|
|
|
glGenTextures(1, &ssaoColorBufferBlur);
|
|
|
|
glGenTextures(1, &ssaoColorBufferBlur);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, ssaoColorBufferBlur);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, ssaoColorBufferBlur);
|
|
|
|
@@ -174,31 +159,33 @@ int main()
|
|
|
|
std::cout << "SSAO Blur Framebuffer not complete!" << std::endl;
|
|
|
|
std::cout << "SSAO Blur Framebuffer not complete!" << std::endl;
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
|
|
|
|
|
|
|
|
// Sample kernel
|
|
|
|
// generate sample kernel
|
|
|
|
|
|
|
|
// ----------------------
|
|
|
|
std::uniform_real_distribution<GLfloat> randomFloats(0.0, 1.0); // generates random floats between 0.0 and 1.0
|
|
|
|
std::uniform_real_distribution<GLfloat> randomFloats(0.0, 1.0); // generates random floats between 0.0 and 1.0
|
|
|
|
std::default_random_engine generator;
|
|
|
|
std::default_random_engine generator;
|
|
|
|
std::vector<glm::vec3> ssaoKernel;
|
|
|
|
std::vector<glm::vec3> ssaoKernel;
|
|
|
|
for (GLuint i = 0; i < 64; ++i)
|
|
|
|
for (unsigned int i = 0; i < 64; ++i)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
glm::vec3 sample(randomFloats(generator) * 2.0 - 1.0, randomFloats(generator) * 2.0 - 1.0, randomFloats(generator));
|
|
|
|
glm::vec3 sample(randomFloats(generator) * 2.0 - 1.0, randomFloats(generator) * 2.0 - 1.0, randomFloats(generator));
|
|
|
|
sample = glm::normalize(sample);
|
|
|
|
sample = glm::normalize(sample);
|
|
|
|
sample *= randomFloats(generator);
|
|
|
|
sample *= randomFloats(generator);
|
|
|
|
GLfloat scale = GLfloat(i) / 64.0;
|
|
|
|
float scale = float(i) / 64.0;
|
|
|
|
|
|
|
|
|
|
|
|
// Scale samples s.t. they're more aligned to center of kernel
|
|
|
|
// scale samples s.t. they're more aligned to center of kernel
|
|
|
|
scale = lerp(0.1f, 1.0f, scale * scale);
|
|
|
|
scale = lerp(0.1f, 1.0f, scale * scale);
|
|
|
|
sample *= scale;
|
|
|
|
sample *= scale;
|
|
|
|
ssaoKernel.push_back(sample);
|
|
|
|
ssaoKernel.push_back(sample);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Noise texture
|
|
|
|
// generate noise texture
|
|
|
|
|
|
|
|
// ----------------------
|
|
|
|
std::vector<glm::vec3> ssaoNoise;
|
|
|
|
std::vector<glm::vec3> ssaoNoise;
|
|
|
|
for (GLuint i = 0; i < 16; i++)
|
|
|
|
for (unsigned int i = 0; i < 16; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
glm::vec3 noise(randomFloats(generator) * 2.0 - 1.0, randomFloats(generator) * 2.0 - 1.0, 0.0f); // rotate around z-axis (in tangent space)
|
|
|
|
glm::vec3 noise(randomFloats(generator) * 2.0 - 1.0, randomFloats(generator) * 2.0 - 1.0, 0.0f); // rotate around z-axis (in tangent space)
|
|
|
|
ssaoNoise.push_back(noise);
|
|
|
|
ssaoNoise.push_back(noise);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GLuint noiseTexture; glGenTextures(1, &noiseTexture);
|
|
|
|
unsigned int noiseTexture; glGenTextures(1, &noiseTexture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, noiseTexture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, noiseTexture);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, 4, 4, 0, GL_RGB, GL_FLOAT, &ssaoNoise[0]);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, 4, 4, 0, GL_RGB, GL_FLOAT, &ssaoNoise[0]);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
@@ -206,285 +193,274 @@ int main()
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// lighting info
|
|
|
|
|
|
|
|
// -------------
|
|
|
|
|
|
|
|
glm::vec3 lightPos = glm::vec3(2.0, 4.0, -2.0);
|
|
|
|
|
|
|
|
glm::vec3 lightColor = glm::vec3(0.2, 0.2, 0.7);
|
|
|
|
|
|
|
|
|
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
// shader configuration
|
|
|
|
|
|
|
|
// --------------------
|
|
|
|
|
|
|
|
shaderLightingPass.use();
|
|
|
|
|
|
|
|
shaderLightingPass.setInt("gPosition", 0);
|
|
|
|
|
|
|
|
shaderLightingPass.setInt("gNormal", 1);
|
|
|
|
|
|
|
|
shaderLightingPass.setInt("gAlbedo", 2);
|
|
|
|
|
|
|
|
shaderLightingPass.setInt("ssao", 3);
|
|
|
|
|
|
|
|
shaderSSAO.use();
|
|
|
|
|
|
|
|
shaderSSAO.setInt("gPosition", 0);
|
|
|
|
|
|
|
|
shaderSSAO.setInt("gNormal", 1);
|
|
|
|
|
|
|
|
shaderSSAO.setInt("texNoise", 2);
|
|
|
|
|
|
|
|
shaderSSAOBlur.use();
|
|
|
|
|
|
|
|
shaderSSAOBlur.setInt("ssaoInput", 0);
|
|
|
|
|
|
|
|
|
|
|
|
// Game loop
|
|
|
|
// render loop
|
|
|
|
|
|
|
|
// -----------
|
|
|
|
while (!glfwWindowShouldClose(window))
|
|
|
|
while (!glfwWindowShouldClose(window))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Set frame time
|
|
|
|
// per-frame time logic
|
|
|
|
GLfloat currentFrame = glfwGetTime();
|
|
|
|
// --------------------
|
|
|
|
|
|
|
|
float currentFrame = glfwGetTime();
|
|
|
|
deltaTime = currentFrame - lastFrame;
|
|
|
|
deltaTime = currentFrame - lastFrame;
|
|
|
|
lastFrame = currentFrame;
|
|
|
|
lastFrame = currentFrame;
|
|
|
|
|
|
|
|
|
|
|
|
// Check and call events
|
|
|
|
// input
|
|
|
|
glfwPollEvents();
|
|
|
|
// -----
|
|
|
|
Do_Movement();
|
|
|
|
processInput(window);
|
|
|
|
|
|
|
|
|
|
|
|
// 1. Geometry Pass: render scene's geometry/color data into gbuffer
|
|
|
|
// render
|
|
|
|
|
|
|
|
// ------
|
|
|
|
|
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 1. geometry pass: render scene's geometry/color data into gbuffer
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (GLfloat)SCR_WIDTH / (GLfloat)SCR_HEIGHT, 0.1f, 50.0f);
|
|
|
|
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 50.0f);
|
|
|
|
glm::mat4 view = camera.GetViewMatrix();
|
|
|
|
glm::mat4 view = camera.GetViewMatrix();
|
|
|
|
glm::mat4 model;
|
|
|
|
glm::mat4 model;
|
|
|
|
shaderGeometryPass.Use();
|
|
|
|
shaderGeometryPass.use();
|
|
|
|
glUniformMatrix4fv(glGetUniformLocation(shaderGeometryPass.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
|
|
|
|
shaderGeometryPass.setMat4("projection", projection);
|
|
|
|
glUniformMatrix4fv(glGetUniformLocation(shaderGeometryPass.Program, "view"), 1, GL_FALSE, glm::value_ptr(view));
|
|
|
|
shaderGeometryPass.setMat4("view", view);
|
|
|
|
// Background cubes
|
|
|
|
// room cube
|
|
|
|
// Note that AO doesn't work too well on flat surfaces so simply scaling the cube as the background room wouldn't work
|
|
|
|
|
|
|
|
// as the resulting faces of the cube are completely flat.
|
|
|
|
|
|
|
|
model = glm::translate(model, glm::vec3(10.0f, 0.0f, 0.0f));
|
|
|
|
|
|
|
|
model = glm::scale(model, glm::vec3(1.0f, 20.0f, 20.0f));
|
|
|
|
|
|
|
|
glUniformMatrix4fv(glGetUniformLocation(shaderGeometryPass.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
|
|
|
|
|
|
|
|
RenderCube();
|
|
|
|
|
|
|
|
model = glm::mat4();
|
|
|
|
model = glm::mat4();
|
|
|
|
model = glm::translate(model, glm::vec3(-10.0f, 0.0f, 0.0f));
|
|
|
|
model = glm::translate(model, glm::vec3(0.0, 7.0f, 0.0f));
|
|
|
|
model = glm::scale(model, glm::vec3(1.0f, 20.0f, 20.0f));
|
|
|
|
model = glm::scale(model, glm::vec3(7.5f, 7.5f, 7.5f));
|
|
|
|
glUniformMatrix4fv(glGetUniformLocation(shaderGeometryPass.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
|
|
|
|
shaderGeometryPass.setMat4("model", model);
|
|
|
|
RenderCube();
|
|
|
|
shaderGeometryPass.setInt("invertedNormals", 1); // invert normals as we're inside the cube
|
|
|
|
model = glm::mat4();
|
|
|
|
renderCube();
|
|
|
|
model = glm::translate(model, glm::vec3(0.0f, 0.0f, 10.0f));
|
|
|
|
shaderGeometryPass.setInt("invertedNormals", 0);
|
|
|
|
model = glm::scale(model, glm::vec3(20.0f, 20.0f, 1.0f));
|
|
|
|
// nanosuit model on the floor
|
|
|
|
glUniformMatrix4fv(glGetUniformLocation(shaderGeometryPass.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
|
|
|
|
|
|
|
|
RenderCube();
|
|
|
|
|
|
|
|
model = glm::mat4();
|
|
|
|
|
|
|
|
model = glm::translate(model, glm::vec3(0.0f, 0.0f, -10.0f));
|
|
|
|
|
|
|
|
model = glm::scale(model, glm::vec3(20.0f, 20.0f, 1.0f));
|
|
|
|
|
|
|
|
glUniformMatrix4fv(glGetUniformLocation(shaderGeometryPass.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
|
|
|
|
|
|
|
|
RenderCube();
|
|
|
|
|
|
|
|
model = glm::mat4();
|
|
|
|
|
|
|
|
model = glm::translate(model, glm::vec3(0.0f, 10.0f, 0.0f));
|
|
|
|
|
|
|
|
model = glm::scale(model, glm::vec3(20.0f, 1.0f, 20.0f));
|
|
|
|
|
|
|
|
glUniformMatrix4fv(glGetUniformLocation(shaderGeometryPass.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
|
|
|
|
|
|
|
|
RenderCube();
|
|
|
|
|
|
|
|
// Floor cube
|
|
|
|
|
|
|
|
model = glm::mat4();
|
|
|
|
|
|
|
|
model = glm::translate(model, glm::vec3(0.0, -1.0f, 0.0f));
|
|
|
|
|
|
|
|
model = glm::scale(model, glm::vec3(20.0f, 1.0f, 20.0f));
|
|
|
|
|
|
|
|
glUniformMatrix4fv(glGetUniformLocation(shaderGeometryPass.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
|
|
|
|
|
|
|
|
RenderCube();
|
|
|
|
|
|
|
|
// Nanosuit model on the floor
|
|
|
|
|
|
|
|
model = glm::mat4();
|
|
|
|
model = glm::mat4();
|
|
|
|
model = glm::translate(model, glm::vec3(0.0f, 0.0f, 5.0));
|
|
|
|
model = glm::translate(model, glm::vec3(0.0f, 0.0f, 5.0));
|
|
|
|
model = glm::rotate(model, glm::radians(-90.0f), glm::vec3(1.0, 0.0, 0.0));
|
|
|
|
model = glm::rotate(model, glm::radians(-90.0f), glm::vec3(1.0, 0.0, 0.0));
|
|
|
|
model = glm::scale(model, glm::vec3(0.5f));
|
|
|
|
model = glm::scale(model, glm::vec3(0.5f));
|
|
|
|
glUniformMatrix4fv(glGetUniformLocation(shaderGeometryPass.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
|
|
|
|
shaderGeometryPass.setMat4("model", model);
|
|
|
|
nanosuit.Draw(shaderGeometryPass);
|
|
|
|
nanosuit.Draw(shaderGeometryPass);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Create SSAO texture
|
|
|
|
// 2. generate SSAO texture
|
|
|
|
|
|
|
|
// ------------------------
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, ssaoFBO);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, ssaoFBO);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
shaderSSAO.Use();
|
|
|
|
shaderSSAO.use();
|
|
|
|
|
|
|
|
// Send kernel + rotation
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < 64; ++i)
|
|
|
|
|
|
|
|
shaderSSAO.setVec3("samples[" + std::to_string(i) + "]", ssaoKernel[i]);
|
|
|
|
|
|
|
|
shaderSSAO.setMat4("projection", projection);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gPosition);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gPosition);
|
|
|
|
glActiveTexture(GL_TEXTURE1);
|
|
|
|
glActiveTexture(GL_TEXTURE1);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gNormal);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gNormal);
|
|
|
|
glActiveTexture(GL_TEXTURE2);
|
|
|
|
glActiveTexture(GL_TEXTURE2);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, noiseTexture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, noiseTexture);
|
|
|
|
// Send kernel + rotation
|
|
|
|
renderQuad();
|
|
|
|
for (GLuint i = 0; i < 64; ++i)
|
|
|
|
|
|
|
|
glUniform3fv(glGetUniformLocation(shaderSSAO.Program, ("samples[" + std::to_string(i) + "]").c_str()), 1, &ssaoKernel[i][0]);
|
|
|
|
|
|
|
|
glUniformMatrix4fv(glGetUniformLocation(shaderSSAO.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
|
|
|
|
|
|
|
|
RenderQuad();
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Blur SSAO texture to remove noise
|
|
|
|
// 3. blur SSAO texture to remove noise
|
|
|
|
|
|
|
|
// ------------------------------------
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, ssaoBlurFBO);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, ssaoBlurFBO);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
shaderSSAOBlur.Use();
|
|
|
|
shaderSSAOBlur.use();
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, ssaoColorBuffer);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, ssaoColorBuffer);
|
|
|
|
RenderQuad();
|
|
|
|
renderQuad();
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 4. Lighting Pass: traditional deferred Blinn-Phong lighting now with added screen-space ambient occlusion
|
|
|
|
// 4. lighting pass: traditional deferred Blinn-Phong lighting with added screen-space ambient occlusion
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
shaderLightingPass.Use();
|
|
|
|
shaderLightingPass.use();
|
|
|
|
|
|
|
|
// send light relevant uniforms
|
|
|
|
|
|
|
|
glm::vec3 lightPosView = glm::vec3(camera.GetViewMatrix() * glm::vec4(lightPos, 1.0));
|
|
|
|
|
|
|
|
shaderLightingPass.setVec3("light.Position", lightPosView);
|
|
|
|
|
|
|
|
shaderLightingPass.setVec3("light.Color", lightColor);
|
|
|
|
|
|
|
|
// Update attenuation parameters
|
|
|
|
|
|
|
|
const float constant = 1.0; // note that we don't send this to the shader, we assume it is always 1.0 (in our case)
|
|
|
|
|
|
|
|
const float linear = 0.09;
|
|
|
|
|
|
|
|
const float quadratic = 0.032;
|
|
|
|
|
|
|
|
shaderLightingPass.setFloat("light.Linear", linear);
|
|
|
|
|
|
|
|
shaderLightingPass.setFloat("light.Quadratic", quadratic);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gPosition);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gPosition);
|
|
|
|
glActiveTexture(GL_TEXTURE1);
|
|
|
|
glActiveTexture(GL_TEXTURE1);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gNormal);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gNormal);
|
|
|
|
glActiveTexture(GL_TEXTURE2);
|
|
|
|
glActiveTexture(GL_TEXTURE2);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gAlbedo);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gAlbedo);
|
|
|
|
glActiveTexture(GL_TEXTURE3); // Add extra SSAO texture to lighting pass
|
|
|
|
glActiveTexture(GL_TEXTURE3); // add extra SSAO texture to lighting pass
|
|
|
|
glBindTexture(GL_TEXTURE_2D, ssaoColorBufferBlur);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, ssaoColorBufferBlur);
|
|
|
|
// Also send light relevant uniforms
|
|
|
|
renderQuad();
|
|
|
|
glm::vec3 lightPosView = glm::vec3(camera.GetViewMatrix() * glm::vec4(lightPos, 1.0));
|
|
|
|
|
|
|
|
glUniform3fv(glGetUniformLocation(shaderLightingPass.Program, "light.Position"), 1, &lightPosView[0]);
|
|
|
|
|
|
|
|
glUniform3fv(glGetUniformLocation(shaderLightingPass.Program, "light.Color"), 1, &lightColor[0]);
|
|
|
|
|
|
|
|
// Update attenuation parameters
|
|
|
|
|
|
|
|
const GLfloat constant = 1.0; // Note that we don't send this to the shader, we assume it is always 1.0 (in our case)
|
|
|
|
|
|
|
|
const GLfloat linear = 0.09;
|
|
|
|
|
|
|
|
const GLfloat quadratic = 0.032;
|
|
|
|
|
|
|
|
glUniform1f(glGetUniformLocation(shaderLightingPass.Program, "light.Linear"), linear);
|
|
|
|
|
|
|
|
glUniform1f(glGetUniformLocation(shaderLightingPass.Program, "light.Quadratic"), quadratic);
|
|
|
|
|
|
|
|
glUniform1i(glGetUniformLocation(shaderLightingPass.Program, "draw_mode"), draw_mode);
|
|
|
|
|
|
|
|
RenderQuad();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Swap the buffers
|
|
|
|
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
glfwSwapBuffers(window);
|
|
|
|
glfwSwapBuffers(window);
|
|
|
|
|
|
|
|
glfwPollEvents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
glfwTerminate();
|
|
|
|
glfwTerminate();
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// renderCube() renders a 1x1 3D cube in NDC.
|
|
|
|
|
|
|
|
// -------------------------------------------------
|
|
|
|
|
|
|
|
unsigned int cubeVAO = 0;
|
|
|
|
|
|
|
|
unsigned int cubeVBO = 0;
|
|
|
|
|
|
|
|
void renderCube()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// initialize (if necessary)
|
|
|
|
|
|
|
|
if (cubeVAO == 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
float vertices[] = {
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
// fill buffer
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
|
|
|
|
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
// link vertex attributes
|
|
|
|
|
|
|
|
glBindVertexArray(cubeVAO);
|
|
|
|
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
|
|
|
|
|
|
|
|
glEnableVertexAttribArray(1);
|
|
|
|
|
|
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
|
|
|
|
|
|
|
|
glEnableVertexAttribArray(2);
|
|
|
|
|
|
|
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
|
|
|
|
glBindVertexArray(0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// render Cube
|
|
|
|
|
|
|
|
glBindVertexArray(cubeVAO);
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
|
|
|
|
|
|
|
glBindVertexArray(0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// RenderQuad() Renders a 1x1 quad in NDC, best used for framebuffer color targets
|
|
|
|
|
|
|
|
// and post-processing effects.
|
|
|
|
// renderQuad() renders a 1x1 XY quad in NDC
|
|
|
|
GLuint quadVAO = 0;
|
|
|
|
// -----------------------------------------
|
|
|
|
GLuint quadVBO;
|
|
|
|
unsigned int quadVAO = 0;
|
|
|
|
void RenderQuad()
|
|
|
|
unsigned int quadVBO;
|
|
|
|
|
|
|
|
void renderQuad()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (quadVAO == 0)
|
|
|
|
if (quadVAO == 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
GLfloat quadVertices[] = {
|
|
|
|
float quadVertices[] = {
|
|
|
|
// Positions // Texture Coords
|
|
|
|
// positions // texture Coords
|
|
|
|
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
|
|
|
|
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
|
|
|
|
-1.0f, -1.0f, 0.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,
|
|
|
|
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
|
|
|
|
1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
|
|
|
|
1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
// Setup plane VAO
|
|
|
|
// setup plane VAO
|
|
|
|
glGenVertexArrays(1, &quadVAO);
|
|
|
|
glGenVertexArrays(1, &quadVAO);
|
|
|
|
glGenBuffers(1, &quadVBO);
|
|
|
|
glGenBuffers(1, &quadVBO);
|
|
|
|
glBindVertexArray(quadVAO);
|
|
|
|
glBindVertexArray(quadVAO);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
|
|
|
|
glEnableVertexAttribArray(1);
|
|
|
|
glEnableVertexAttribArray(1);
|
|
|
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
|
|
|
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
glBindVertexArray(quadVAO);
|
|
|
|
glBindVertexArray(quadVAO);
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
glBindVertexArray(0);
|
|
|
|
glBindVertexArray(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// RenderCube() Renders a 1x1 3D cube in NDC.
|
|
|
|
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
|
|
|
|
GLuint cubeVAO = 0;
|
|
|
|
// ---------------------------------------------------------------------------------------------------------
|
|
|
|
GLuint cubeVBO = 0;
|
|
|
|
void processInput(GLFWwindow *window)
|
|
|
|
void RenderCube()
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Initialize (if necessary)
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
|
|
|
if (cubeVAO == 0)
|
|
|
|
glfwSetWindowShouldClose(window, true);
|
|
|
|
{
|
|
|
|
|
|
|
|
GLfloat vertices[] = {
|
|
|
|
|
|
|
|
// Back face
|
|
|
|
|
|
|
|
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, // Bottom-left
|
|
|
|
|
|
|
|
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, // top-right
|
|
|
|
|
|
|
|
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, // bottom-right
|
|
|
|
|
|
|
|
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, // top-right
|
|
|
|
|
|
|
|
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, // bottom-left
|
|
|
|
|
|
|
|
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,// top-left
|
|
|
|
|
|
|
|
// Front face
|
|
|
|
|
|
|
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom-left
|
|
|
|
|
|
|
|
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, // bottom-right
|
|
|
|
|
|
|
|
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // top-right
|
|
|
|
|
|
|
|
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // top-right
|
|
|
|
|
|
|
|
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, // top-left
|
|
|
|
|
|
|
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom-left
|
|
|
|
|
|
|
|
// Left face
|
|
|
|
|
|
|
|
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-right
|
|
|
|
|
|
|
|
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top-left
|
|
|
|
|
|
|
|
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-left
|
|
|
|
|
|
|
|
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-left
|
|
|
|
|
|
|
|
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // bottom-right
|
|
|
|
|
|
|
|
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-right
|
|
|
|
|
|
|
|
// Right face
|
|
|
|
|
|
|
|
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-left
|
|
|
|
|
|
|
|
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-right
|
|
|
|
|
|
|
|
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top-right
|
|
|
|
|
|
|
|
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-right
|
|
|
|
|
|
|
|
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-left
|
|
|
|
|
|
|
|
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // bottom-left
|
|
|
|
|
|
|
|
// Bottom face
|
|
|
|
|
|
|
|
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, // top-right
|
|
|
|
|
|
|
|
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f, // top-left
|
|
|
|
|
|
|
|
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,// bottom-left
|
|
|
|
|
|
|
|
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, // bottom-left
|
|
|
|
|
|
|
|
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, // bottom-right
|
|
|
|
|
|
|
|
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, // top-right
|
|
|
|
|
|
|
|
// Top face
|
|
|
|
|
|
|
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,// top-left
|
|
|
|
|
|
|
|
0.5f, 0.5f , 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom-right
|
|
|
|
|
|
|
|
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // top-right
|
|
|
|
|
|
|
|
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom-right
|
|
|
|
|
|
|
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,// top-left
|
|
|
|
|
|
|
|
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f // bottom-left
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
glGenVertexArrays(1, &cubeVAO);
|
|
|
|
|
|
|
|
glGenBuffers(1, &cubeVBO);
|
|
|
|
|
|
|
|
// Fill buffer
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
|
|
|
|
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
// Link vertex attributes
|
|
|
|
|
|
|
|
glBindVertexArray(cubeVAO);
|
|
|
|
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
|
|
|
|
|
|
|
|
glEnableVertexAttribArray(1);
|
|
|
|
|
|
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
|
|
|
|
|
|
|
|
glEnableVertexAttribArray(2);
|
|
|
|
|
|
|
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
|
|
|
|
glBindVertexArray(0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render Cube
|
|
|
|
|
|
|
|
glBindVertexArray(cubeVAO);
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
|
|
|
|
|
|
|
glBindVertexArray(0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool keys[1024];
|
|
|
|
float cameraSpeed = 2.5 * deltaTime;
|
|
|
|
bool keysPressed[1024];
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
|
|
|
// Moves/alters the camera positions based on user input
|
|
|
|
|
|
|
|
void Do_Movement()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// Camera controls
|
|
|
|
|
|
|
|
if (keys[GLFW_KEY_W])
|
|
|
|
|
|
|
|
camera.ProcessKeyboard(FORWARD, deltaTime);
|
|
|
|
camera.ProcessKeyboard(FORWARD, deltaTime);
|
|
|
|
if (keys[GLFW_KEY_S])
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
|
|
|
camera.ProcessKeyboard(BACKWARD, deltaTime);
|
|
|
|
camera.ProcessKeyboard(BACKWARD, deltaTime);
|
|
|
|
if (keys[GLFW_KEY_A])
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
|
|
|
camera.ProcessKeyboard(LEFT, deltaTime);
|
|
|
|
camera.ProcessKeyboard(LEFT, deltaTime);
|
|
|
|
if (keys[GLFW_KEY_D])
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
|
|
|
camera.ProcessKeyboard(RIGHT, deltaTime);
|
|
|
|
camera.ProcessKeyboard(RIGHT, deltaTime);
|
|
|
|
|
|
|
|
|
|
|
|
if (keys[GLFW_KEY_1])
|
|
|
|
|
|
|
|
draw_mode = 1;
|
|
|
|
|
|
|
|
if (keys[GLFW_KEY_2])
|
|
|
|
|
|
|
|
draw_mode = 2;
|
|
|
|
|
|
|
|
if (keys[GLFW_KEY_3])
|
|
|
|
|
|
|
|
draw_mode = 3;
|
|
|
|
|
|
|
|
if (keys[GLFW_KEY_4])
|
|
|
|
|
|
|
|
draw_mode = 4;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GLfloat lastX = 400, lastY = 300;
|
|
|
|
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
|
|
|
|
bool firstMouse = true;
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
// Is called whenever a key is pressed/released via GLFW
|
|
|
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
|
|
|
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
|
|
|
|
// make sure the viewport matches the new window dimensions; note that width and
|
|
|
|
glfwSetWindowShouldClose(window, GL_TRUE);
|
|
|
|
// height will be significantly larger than specified on retina displays.
|
|
|
|
|
|
|
|
glViewport(0, 0, width, height);
|
|
|
|
if (key >= 0 && key <= 1024)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (action == GLFW_PRESS)
|
|
|
|
|
|
|
|
keys[key] = true;
|
|
|
|
|
|
|
|
else if (action == GLFW_RELEASE)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
keys[key] = false;
|
|
|
|
|
|
|
|
keysPressed[key] = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// glfw: whenever the mouse moves, this callback is called
|
|
|
|
|
|
|
|
// -------------------------------------------------------
|
|
|
|
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
|
|
|
|
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (firstMouse)
|
|
|
|
if (firstMouse)
|
|
|
|
@@ -494,8 +470,8 @@ void mouse_callback(GLFWwindow* window, double xpos, double ypos)
|
|
|
|
firstMouse = false;
|
|
|
|
firstMouse = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GLfloat xoffset = xpos - lastX;
|
|
|
|
float xoffset = xpos - lastX;
|
|
|
|
GLfloat yoffset = lastY - ypos;
|
|
|
|
float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
|
|
|
|
|
|
|
|
|
|
|
|
lastX = xpos;
|
|
|
|
lastX = xpos;
|
|
|
|
lastY = ypos;
|
|
|
|
lastY = ypos;
|
|
|
|
@@ -503,6 +479,8 @@ void mouse_callback(GLFWwindow* window, double xpos, double ypos)
|
|
|
|
camera.ProcessMouseMovement(xoffset, yoffset);
|
|
|
|
camera.ProcessMouseMovement(xoffset, yoffset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// glfw: whenever the mouse scroll wheel scrolls, this callback is called
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
|
|
|
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
camera.ProcessMouseScroll(yoffset);
|
|
|
|
camera.ProcessMouseScroll(yoffset);
|
|
|
|
|