mirror of
https://github.com/JoeyDeVries/LearnOpenGL.git
synced 2026-01-07 01:03:22 +08:00
Code re-work: parallax mapping.
This commit is contained in:
@@ -139,7 +139,8 @@ set(5.advanced_lighting
|
|||||||
3.2.2.point_shadows_soft
|
3.2.2.point_shadows_soft
|
||||||
4.normal_mapping
|
4.normal_mapping
|
||||||
5.1.parallax_mapping
|
5.1.parallax_mapping
|
||||||
5.2.parallax_occlusion_mapping
|
5.2.steep_parallax_mapping
|
||||||
|
5.3.parallax_occlusion_mapping
|
||||||
6.hdr
|
6.hdr
|
||||||
7.bloom
|
7.bloom
|
||||||
8.1.deferred_shading
|
8.1.deferred_shading
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ int main()
|
|||||||
shader.setMat4("view", view);
|
shader.setMat4("view", view);
|
||||||
// render normal-mapped quad
|
// render normal-mapped quad
|
||||||
glm::mat4 model;
|
glm::mat4 model;
|
||||||
model = glm::rotate(model, glm::radians((float)glfwGetTime() * -10.0f), glm::normalize(glm::vec3(1.0, 0.0, 1.0))); // Rotates the quad to show normal mapping works in all directions
|
model = glm::rotate(model, glm::radians((float)glfwGetTime() * -10.0f), glm::normalize(glm::vec3(1.0, 0.0, 1.0))); // rotate the quad to show normal mapping from multiple directions
|
||||||
shader.setMat4("model", model);
|
shader.setMat4("model", model);
|
||||||
shader.setVec3("viewPos", camera.Position);
|
shader.setVec3("viewPos", camera.Position);
|
||||||
shader.setVec3("lightPos", lightPos);
|
shader.setVec3("lightPos", lightPos);
|
||||||
|
|||||||
@@ -13,84 +13,41 @@ uniform sampler2D diffuseMap;
|
|||||||
uniform sampler2D normalMap;
|
uniform sampler2D normalMap;
|
||||||
uniform sampler2D depthMap;
|
uniform sampler2D depthMap;
|
||||||
|
|
||||||
uniform bool parallax;
|
uniform float heightScale;
|
||||||
uniform float height_scale;
|
|
||||||
|
|
||||||
vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir)
|
vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir)
|
||||||
{
|
{
|
||||||
// float height = texture(depthMap, texCoords).r;
|
float height = texture(depthMap, texCoords).r;
|
||||||
// return texCoords - viewDir.xy * (height * height_scale);
|
return texCoords - viewDir.xy * (height * heightScale);
|
||||||
|
|
||||||
// number of depth layers
|
|
||||||
const float minLayers = 8;
|
|
||||||
const float maxLayers = 32;
|
|
||||||
float numLayers = mix(maxLayers, minLayers, abs(dot(vec3(0.0, 0.0, 1.0), viewDir)));
|
|
||||||
// calculate the size of each layer
|
|
||||||
float layerDepth = 1.0 / numLayers;
|
|
||||||
// depth of current layer
|
|
||||||
float currentLayerDepth = 0.0;
|
|
||||||
// the amount to shift the texture coordinates per layer (from vector P)
|
|
||||||
vec2 P = viewDir.xy / viewDir.z * height_scale;
|
|
||||||
vec2 deltaTexCoords = P / numLayers;
|
|
||||||
|
|
||||||
// get initial values
|
|
||||||
vec2 currentTexCoords = texCoords;
|
|
||||||
float currentDepthMapValue = texture(depthMap, currentTexCoords).r;
|
|
||||||
|
|
||||||
while(currentLayerDepth < currentDepthMapValue)
|
|
||||||
{
|
|
||||||
// shift texture coordinates along direction of P
|
|
||||||
currentTexCoords -= deltaTexCoords;
|
|
||||||
// get depthmap value at current texture coordinates
|
|
||||||
currentDepthMapValue = texture(depthMap, currentTexCoords).r;
|
|
||||||
// get depth of next layer
|
|
||||||
currentLayerDepth += layerDepth;
|
|
||||||
}
|
|
||||||
|
|
||||||
// -- parallax occlusion mapping interpolation from here on
|
|
||||||
// get texture coordinates before collision (reverse operations)
|
|
||||||
vec2 prevTexCoords = currentTexCoords + deltaTexCoords;
|
|
||||||
|
|
||||||
// get depth after and before collision for linear interpolation
|
|
||||||
float afterDepth = currentDepthMapValue - currentLayerDepth;
|
|
||||||
float beforeDepth = texture(depthMap, prevTexCoords).r - currentLayerDepth + layerDepth;
|
|
||||||
|
|
||||||
// interpolation of texture coordinates
|
|
||||||
float weight = afterDepth / (afterDepth - beforeDepth);
|
|
||||||
vec2 finalTexCoords = prevTexCoords * weight + currentTexCoords * (1.0 - weight);
|
|
||||||
|
|
||||||
return finalTexCoords;
|
|
||||||
// return currentTexCoords;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
// Offset texture coordinates with Parallax Mapping
|
// offset texture coordinates with Parallax Mapping
|
||||||
vec3 viewDir = normalize(fs_in.TangentViewPos - fs_in.TangentFragPos);
|
vec3 viewDir = normalize(fs_in.TangentViewPos - fs_in.TangentFragPos);
|
||||||
vec2 texCoords = fs_in.TexCoords;
|
vec2 texCoords = fs_in.TexCoords;
|
||||||
if(parallax)
|
|
||||||
texCoords = ParallaxMapping(fs_in.TexCoords, viewDir);
|
|
||||||
|
|
||||||
|
texCoords = ParallaxMapping(fs_in.TexCoords, viewDir);
|
||||||
if(texCoords.x > 1.0 || texCoords.y > 1.0 || texCoords.x < 0.0 || texCoords.y < 0.0)
|
if(texCoords.x > 1.0 || texCoords.y > 1.0 || texCoords.x < 0.0 || texCoords.y < 0.0)
|
||||||
discard;
|
discard;
|
||||||
|
|
||||||
// Obtain normal from normal map
|
// obtain normal from normal map
|
||||||
vec3 normal = texture(normalMap, texCoords).rgb;
|
vec3 normal = texture(normalMap, texCoords).rgb;
|
||||||
normal = normalize(normal * 2.0 - 1.0);
|
normal = normalize(normal * 2.0 - 1.0);
|
||||||
|
|
||||||
// Get diffuse color
|
// get diffuse color
|
||||||
vec3 color = texture(diffuseMap, texCoords).rgb;
|
vec3 color = texture(diffuseMap, texCoords).rgb;
|
||||||
// Ambient
|
// ambient
|
||||||
vec3 ambient = 0.1 * color;
|
vec3 ambient = 0.1 * color;
|
||||||
// Diffuse
|
// diffuse
|
||||||
vec3 lightDir = normalize(fs_in.TangentLightPos - fs_in.TangentFragPos);
|
vec3 lightDir = normalize(fs_in.TangentLightPos - fs_in.TangentFragPos);
|
||||||
float diff = max(dot(lightDir, normal), 0.0);
|
float diff = max(dot(lightDir, normal), 0.0);
|
||||||
vec3 diffuse = diff * color;
|
vec3 diffuse = diff * color;
|
||||||
// Specular
|
// specular
|
||||||
vec3 reflectDir = reflect(-lightDir, normal);
|
vec3 reflectDir = reflect(-lightDir, normal);
|
||||||
vec3 halfwayDir = normalize(lightDir + viewDir);
|
vec3 halfwayDir = normalize(lightDir + viewDir);
|
||||||
float spec = pow(max(dot(normal, halfwayDir), 0.0), 32.0);
|
float spec = pow(max(dot(normal, halfwayDir), 0.0), 32.0);
|
||||||
|
|
||||||
vec3 specular = vec3(0.2) * spec;
|
vec3 specular = vec3(0.2) * spec;
|
||||||
FragColor = vec4(ambient + diffuse + specular, 1.0f);
|
FragColor = vec4(ambient + diffuse + specular, 1.0);
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
layout (location = 0) in vec3 position;
|
layout (location = 0) in vec3 aPos;
|
||||||
layout (location = 1) in vec3 normal;
|
layout (location = 1) in vec3 aNormal;
|
||||||
layout (location = 2) in vec2 texCoords;
|
layout (location = 2) in vec2 aTexCoords;
|
||||||
layout (location = 3) in vec3 tangent;
|
layout (location = 3) in vec3 aTangent;
|
||||||
layout (location = 4) in vec3 bitangent;
|
layout (location = 4) in vec3 aBitangent;
|
||||||
|
|
||||||
out VS_OUT {
|
out VS_OUT {
|
||||||
vec3 FragPos;
|
vec3 FragPos;
|
||||||
@@ -22,17 +22,17 @@ uniform vec3 viewPos;
|
|||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
gl_Position = projection * view * model * vec4(position, 1.0f);
|
vs_out.FragPos = vec3(model * vec4(aPos, 1.0));
|
||||||
vs_out.FragPos = vec3(model * vec4(position, 1.0));
|
vs_out.TexCoords = aTexCoords;
|
||||||
vs_out.TexCoords = texCoords;
|
|
||||||
|
|
||||||
|
vec3 T = normalize(mat3(model) * aTangent);
|
||||||
vec3 T = normalize(mat3(model) * tangent);
|
vec3 B = normalize(mat3(model) * aBitangent);
|
||||||
vec3 B = normalize(mat3(model) * bitangent);
|
vec3 N = normalize(mat3(model) * aNormal);
|
||||||
vec3 N = normalize(mat3(model) * normal);
|
|
||||||
mat3 TBN = transpose(mat3(T, B, N));
|
mat3 TBN = transpose(mat3(T, B, N));
|
||||||
|
|
||||||
vs_out.TangentLightPos = TBN * lightPos;
|
vs_out.TangentLightPos = TBN * lightPos;
|
||||||
vs_out.TangentViewPos = TBN * viewPos;
|
vs_out.TangentViewPos = TBN * viewPos;
|
||||||
vs_out.TangentFragPos = TBN * vs_out.FragPos;
|
vs_out.TangentFragPos = TBN * vs_out.FragPos;
|
||||||
|
|
||||||
|
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||||
}
|
}
|
||||||
@@ -1,175 +1,185 @@
|
|||||||
// Std. Includes
|
#include <glad/glad.h>
|
||||||
#include <string>
|
|
||||||
|
|
||||||
// GLEW
|
|
||||||
#define GLEW_STATIC
|
|
||||||
#include <GL/glew.h>
|
|
||||||
|
|
||||||
// GLFW
|
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <stb_image.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 <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 = 800, SCR_HEIGHT = 600;
|
|
||||||
|
|
||||||
// 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);
|
||||||
GLuint loadTexture(GLchar const * path);
|
void processInput(GLFWwindow *window);
|
||||||
void RenderQuad();
|
unsigned int loadTexture(const char *path);
|
||||||
|
void renderQuad();
|
||||||
|
|
||||||
// Camera
|
// settings
|
||||||
|
const unsigned int SCR_WIDTH = 1280;
|
||||||
|
const unsigned int SCR_HEIGHT = 720;
|
||||||
|
float heightScale = 0.1;
|
||||||
|
|
||||||
|
// camera
|
||||||
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
|
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
|
||||||
|
float lastX = (float)SCR_WIDTH / 2.0;
|
||||||
|
float lastY = (float)SCR_HEIGHT / 2.0;
|
||||||
|
bool firstMouse = true;
|
||||||
|
|
||||||
GLfloat deltaTime = 0.0f;
|
// timing
|
||||||
GLfloat lastFrame = 0.0f;
|
float deltaTime = 0.0f;
|
||||||
|
float lastFrame = 0.0f;
|
||||||
|
|
||||||
GLboolean parallax_mapping = true;
|
|
||||||
GLfloat height_scale = 0.1;
|
|
||||||
|
|
||||||
// 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))
|
||||||
|
{
|
||||||
|
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 shader("parallax_mapping.vs", "parallax_mapping.frag");
|
// -------------------------
|
||||||
|
Shader shader("5.1.parallax_mapping.vs", "5.1.parallax_mapping.fs");
|
||||||
|
|
||||||
// Load textures
|
// load textures
|
||||||
GLuint diffuseMap = loadTexture(FileSystem::getPath("resources/textures/bricks2.jpg").c_str());
|
// -------------
|
||||||
GLuint normalMap = loadTexture(FileSystem::getPath("resources/textures/bricks2_normal.jpg").c_str());
|
unsigned int diffuseMap = loadTexture(FileSystem::getPath("resources/textures/bricks2.jpg").c_str());
|
||||||
GLuint heightMap = loadTexture(FileSystem::getPath("resources/textures/bricks2_disp.jpg").c_str());
|
unsigned int normalMap = loadTexture(FileSystem::getPath("resources/textures/bricks2_normal.jpg").c_str());
|
||||||
//GLuint diffuseMap = loadTexture(FileSystem::getPath("resources/textures/wood.png").c_str();
|
unsigned int heightMap = loadTexture(FileSystem::getPath("resources/textures/bricks2_disp.jpg").c_str());
|
||||||
//GLuint normalMap = loadTexture(FileSystem::getPath("resources/textures/toy_box_normal.png").c_str());
|
/* unsigned int diffuseMap = loadTexture(FileSystem::getPath("resources/textures/toy_box_diffuse.png").c_str());
|
||||||
//GLuint heightMap = loadTexture(FileSystem::getPath("resources/textures/toy_box_disp.png").c_str());
|
unsigned int normalMap = loadTexture(FileSystem::getPath("resources/textures/toy_box_normal.png").c_str());
|
||||||
|
unsigned int heightMap = loadTexture(FileSystem::getPath("resources/textures/toy_box_disp.png").c_str());*/
|
||||||
|
|
||||||
// Set texture units
|
// shader configuration
|
||||||
shader.Use();
|
// --------------------
|
||||||
glUniform1i(glGetUniformLocation(shader.Program, "diffuseMap"), 0);
|
shader.use();
|
||||||
glUniform1i(glGetUniformLocation(shader.Program, "normalMap"), 1);
|
shader.setInt("diffuseMap", 0);
|
||||||
glUniform1i(glGetUniformLocation(shader.Program, "depthMap"), 2);
|
shader.setInt("normalMap", 1);
|
||||||
|
shader.setInt("depthMap", 2);
|
||||||
|
|
||||||
// Light position
|
// lighting info
|
||||||
|
// -------------
|
||||||
glm::vec3 lightPos(0.5f, 1.0f, 0.3f);
|
glm::vec3 lightPos(0.5f, 1.0f, 0.3f);
|
||||||
|
|
||||||
// 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);
|
||||||
|
|
||||||
// Clear the colorbuffer
|
// render
|
||||||
|
// ------
|
||||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
// Configure view/projection matrices
|
// configure view/projection matrices
|
||||||
shader.Use();
|
glm::mat4 projection = glm::perspective(camera.Zoom, (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||||
glm::mat4 view = camera.GetViewMatrix();
|
glm::mat4 view = camera.GetViewMatrix();
|
||||||
glm::mat4 projection = glm::perspective(camera.Zoom, (GLfloat)SCR_WIDTH / (GLfloat)SCR_HEIGHT, 0.1f, 100.0f);
|
shader.use();
|
||||||
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "view"), 1, GL_FALSE, glm::value_ptr(view));
|
shader.setMat4("projection", projection);
|
||||||
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
|
shader.setMat4("view", view);
|
||||||
// Render normal-mapped quad
|
// render parallax-mapped quad
|
||||||
glm::mat4 model;
|
glm::mat4 model;
|
||||||
//model = glm::rotate(model, (GLfloat)glfwGetTime() * -10, glm::normalize(glm::vec3(1.0, 0.0, 1.0))); // Rotates the quad to show parallax mapping works in all directions
|
model = glm::rotate(model, glm::radians((float)glfwGetTime() * -10.0f), glm::normalize(glm::vec3(1.0, 0.0, 1.0))); // rotate the quad to show parallax mapping from multiple directions
|
||||||
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
|
shader.setMat4("model", model);
|
||||||
glUniform3fv(glGetUniformLocation(shader.Program, "lightPos"), 1, &lightPos[0]);
|
shader.setVec3("viewPos", camera.Position);
|
||||||
glUniform3fv(glGetUniformLocation(shader.Program, "viewPos"), 1, &camera.Position[0]);
|
shader.setVec3("lightPos", lightPos);
|
||||||
glUniform1f(glGetUniformLocation(shader.Program, "height_scale"), height_scale);
|
shader.setFloat("heightScale", heightScale); // adjust with Q and E keys
|
||||||
glUniform1i(glGetUniformLocation(shader.Program, "parallax"), parallax_mapping);
|
std::cout << heightScale << std::endl;
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glBindTexture(GL_TEXTURE_2D, diffuseMap);
|
glBindTexture(GL_TEXTURE_2D, diffuseMap);
|
||||||
glActiveTexture(GL_TEXTURE1);
|
glActiveTexture(GL_TEXTURE1);
|
||||||
glBindTexture(GL_TEXTURE_2D, normalMap);
|
glBindTexture(GL_TEXTURE_2D, normalMap);
|
||||||
glActiveTexture(GL_TEXTURE2);
|
glActiveTexture(GL_TEXTURE2);
|
||||||
glBindTexture(GL_TEXTURE_2D, heightMap);
|
glBindTexture(GL_TEXTURE_2D, heightMap);
|
||||||
RenderQuad();
|
renderQuad();
|
||||||
|
|
||||||
// render light source (simply renders a smaller plane at the light's position for debugging/visualization)
|
// render light source (simply re-renders a smaller plane at the light's position for debugging/visualization)
|
||||||
model = glm::mat4();
|
model = glm::mat4();
|
||||||
model = glm::translate(model, lightPos);
|
model = glm::translate(model, lightPos);
|
||||||
model = glm::scale(model, glm::vec3(0.1f));
|
model = glm::scale(model, glm::vec3(0.1f));
|
||||||
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
|
shader.setMat4("model", model);
|
||||||
//RenderQuad();
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// RenderQuad() Renders a 1x1 quad in NDC
|
// renderQuad() renders a 1x1 quad in NDC
|
||||||
GLuint quadVAO = 0;
|
unsigned int quadVAO = 0;
|
||||||
GLuint quadVBO;
|
unsigned int quadVBO;
|
||||||
void RenderQuad()
|
void renderQuad()
|
||||||
{
|
{
|
||||||
if (quadVAO == 0)
|
if (quadVAO == 0)
|
||||||
{
|
{
|
||||||
// positions
|
// positions
|
||||||
glm::vec3 pos1(-1.0, 1.0, 0.0);
|
glm::vec3 pos1(-1.0f, 1.0f, 0.0f);
|
||||||
glm::vec3 pos2(-1.0, -1.0, 0.0);
|
glm::vec3 pos2(-1.0f, -1.0f, 0.0f);
|
||||||
glm::vec3 pos3(1.0, -1.0, 0.0);
|
glm::vec3 pos3( 1.0f, -1.0f, 0.0f);
|
||||||
glm::vec3 pos4(1.0, 1.0, 0.0);
|
glm::vec3 pos4( 1.0f, 1.0f, 0.0f);
|
||||||
// texture coordinates
|
// texture coordinates
|
||||||
glm::vec2 uv1(0.0, 1.0);
|
glm::vec2 uv1(0.0f, 1.0f);
|
||||||
glm::vec2 uv2(0.0, 0.0);
|
glm::vec2 uv2(0.0f, 0.0f);
|
||||||
glm::vec2 uv3(1.0, 0.0);
|
glm::vec2 uv3(1.0f, 0.0f);
|
||||||
glm::vec2 uv4(1.0, 1.0);
|
glm::vec2 uv4(1.0f, 1.0f);
|
||||||
// normal vector
|
// normal vector
|
||||||
glm::vec3 nm(0.0, 0.0, 1.0);
|
glm::vec3 nm(0.0f, 0.0f, 1.0f);
|
||||||
|
|
||||||
// calculate tangent/bitangent vectors of both triangles
|
// calculate tangent/bitangent vectors of both triangles
|
||||||
glm::vec3 tangent1, bitangent1;
|
glm::vec3 tangent1, bitangent1;
|
||||||
glm::vec3 tangent2, bitangent2;
|
glm::vec3 tangent2, bitangent2;
|
||||||
// - triangle 1
|
// triangle 1
|
||||||
|
// ----------
|
||||||
glm::vec3 edge1 = pos2 - pos1;
|
glm::vec3 edge1 = pos2 - pos1;
|
||||||
glm::vec3 edge2 = pos3 - pos1;
|
glm::vec3 edge2 = pos3 - pos1;
|
||||||
glm::vec2 deltaUV1 = uv2 - uv1;
|
glm::vec2 deltaUV1 = uv2 - uv1;
|
||||||
@@ -187,7 +197,8 @@ void RenderQuad()
|
|||||||
bitangent1.z = f * (-deltaUV2.x * edge1.z + deltaUV1.x * edge2.z);
|
bitangent1.z = f * (-deltaUV2.x * edge1.z + deltaUV1.x * edge2.z);
|
||||||
bitangent1 = glm::normalize(bitangent1);
|
bitangent1 = glm::normalize(bitangent1);
|
||||||
|
|
||||||
// - triangle 2
|
// triangle 2
|
||||||
|
// ----------
|
||||||
edge1 = pos3 - pos1;
|
edge1 = pos3 - pos1;
|
||||||
edge2 = pos4 - pos1;
|
edge2 = pos4 - pos1;
|
||||||
deltaUV1 = uv3 - uv1;
|
deltaUV1 = uv3 - uv1;
|
||||||
@@ -207,8 +218,8 @@ void RenderQuad()
|
|||||||
bitangent2 = glm::normalize(bitangent2);
|
bitangent2 = glm::normalize(bitangent2);
|
||||||
|
|
||||||
|
|
||||||
GLfloat quadVertices[] = {
|
float quadVertices[] = {
|
||||||
// Positions // normal // TexCoords // Tangent // Bitangent
|
// positions // normal // texcoords // tangent // bitangent
|
||||||
pos1.x, pos1.y, pos1.z, nm.x, nm.y, nm.z, uv1.x, uv1.y, tangent1.x, tangent1.y, tangent1.z, bitangent1.x, bitangent1.y, bitangent1.z,
|
pos1.x, pos1.y, pos1.z, nm.x, nm.y, nm.z, uv1.x, uv1.y, tangent1.x, tangent1.y, tangent1.z, bitangent1.x, bitangent1.y, bitangent1.z,
|
||||||
pos2.x, pos2.y, pos2.z, nm.x, nm.y, nm.z, uv2.x, uv2.y, tangent1.x, tangent1.y, tangent1.z, bitangent1.x, bitangent1.y, bitangent1.z,
|
pos2.x, pos2.y, pos2.z, nm.x, nm.y, nm.z, uv2.x, uv2.y, tangent1.x, tangent1.y, tangent1.z, bitangent1.x, bitangent1.y, bitangent1.z,
|
||||||
pos3.x, pos3.y, pos3.z, nm.x, nm.y, nm.z, uv3.x, uv3.y, tangent1.x, tangent1.y, tangent1.z, bitangent1.x, bitangent1.y, bitangent1.z,
|
pos3.x, pos3.y, pos3.z, nm.x, nm.y, nm.z, uv3.x, uv3.y, tangent1.x, tangent1.y, tangent1.z, bitangent1.x, bitangent1.y, bitangent1.z,
|
||||||
@@ -217,105 +228,73 @@ void RenderQuad()
|
|||||||
pos3.x, pos3.y, pos3.z, nm.x, nm.y, nm.z, uv3.x, uv3.y, tangent2.x, tangent2.y, tangent2.z, bitangent2.x, bitangent2.y, bitangent2.z,
|
pos3.x, pos3.y, pos3.z, nm.x, nm.y, nm.z, uv3.x, uv3.y, tangent2.x, tangent2.y, tangent2.z, bitangent2.x, bitangent2.y, bitangent2.z,
|
||||||
pos4.x, pos4.y, pos4.z, nm.x, nm.y, nm.z, uv4.x, uv4.y, tangent2.x, tangent2.y, tangent2.z, bitangent2.x, bitangent2.y, bitangent2.z
|
pos4.x, pos4.y, pos4.z, nm.x, nm.y, nm.z, uv4.x, uv4.y, tangent2.x, tangent2.y, tangent2.z, bitangent2.x, bitangent2.y, bitangent2.z
|
||||||
};
|
};
|
||||||
// Setup plane VAO
|
// configure 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, 14 * sizeof(GLfloat), (GLvoid*)0);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)0);
|
||||||
glEnableVertexAttribArray(1);
|
glEnableVertexAttribArray(1);
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||||
glEnableVertexAttribArray(2);
|
glEnableVertexAttribArray(2);
|
||||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 14 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(6 * sizeof(float)));
|
||||||
glEnableVertexAttribArray(3);
|
glEnableVertexAttribArray(3);
|
||||||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(GLfloat), (GLvoid*)(8 * sizeof(GLfloat)));
|
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(8 * sizeof(float)));
|
||||||
glEnableVertexAttribArray(4);
|
glEnableVertexAttribArray(4);
|
||||||
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(GLfloat), (GLvoid*)(11 * sizeof(GLfloat)));
|
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(11 * sizeof(float)));
|
||||||
}
|
}
|
||||||
glBindVertexArray(quadVAO);
|
glBindVertexArray(quadVAO);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function loads a texture from file. Note: texture loading functions like these are usually
|
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
|
||||||
// managed by a 'Resource Manager' that manages all resources (like textures, models, audio).
|
// ---------------------------------------------------------------------------------------------------------
|
||||||
// For learning purposes we'll just define it as a utility function.
|
void processInput(GLFWwindow *window)
|
||||||
GLuint loadTexture(GLchar const * path)
|
|
||||||
{
|
{
|
||||||
//Generate texture ID and load texture data
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||||
GLuint textureID;
|
glfwSetWindowShouldClose(window, true);
|
||||||
glGenTextures(1, &textureID);
|
|
||||||
int width, height;
|
|
||||||
unsigned char* image = SOIL_load_image(path, &width, &height, 0, SOIL_LOAD_RGB);
|
|
||||||
// Assign texture to ID
|
|
||||||
glBindTexture(GL_TEXTURE_2D, textureID);
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
|
|
||||||
glGenerateMipmap(GL_TEXTURE_2D);
|
|
||||||
|
|
||||||
// Parameters
|
float cameraSpeed = 2.5 * deltaTime;
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
|
||||||
SOIL_free_image_data(image);
|
|
||||||
return textureID;
|
|
||||||
}
|
|
||||||
|
|
||||||
#pragma region "User input"
|
|
||||||
|
|
||||||
bool keys[1024];
|
|
||||||
bool keysPressed[1024];
|
|
||||||
// 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);
|
||||||
|
|
||||||
// Change parallax height scale
|
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
|
||||||
if (keys[GLFW_KEY_Q])
|
|
||||||
height_scale -= 0.05 * deltaTime;
|
|
||||||
else if (keys[GLFW_KEY_E])
|
|
||||||
height_scale += 0.05 * deltaTime;
|
|
||||||
|
|
||||||
// Enable/disable parallax mapping
|
|
||||||
if (keys[GLFW_KEY_SPACE] && !keysPressed[GLFW_KEY_SPACE])
|
|
||||||
{
|
|
||||||
parallax_mapping = !parallax_mapping;
|
|
||||||
keysPressed[GLFW_KEY_SPACE] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Is called whenever a key is pressed/released via GLFW
|
|
||||||
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
|
|
||||||
{
|
|
||||||
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
|
|
||||||
glfwSetWindowShouldClose(window, GL_TRUE);
|
|
||||||
|
|
||||||
if (key >= 0 && key <= 1024)
|
|
||||||
{
|
{
|
||||||
if (action == GLFW_PRESS)
|
if (heightScale > 0.0f)
|
||||||
keys[key] = true;
|
heightScale -= 0.0005f;
|
||||||
else if (action == GLFW_RELEASE)
|
else
|
||||||
{
|
heightScale = 0.0f;
|
||||||
keys[key] = false;
|
}
|
||||||
keysPressed[key] = false;
|
else if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
|
||||||
}
|
{
|
||||||
|
if (heightScale < 1.0f)
|
||||||
|
heightScale += 0.0005f;
|
||||||
|
else
|
||||||
|
heightScale = 1.0f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GLfloat lastX = 400, lastY = 300;
|
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
|
||||||
bool firstMouse = true;
|
// ---------------------------------------------------------------------------------------------
|
||||||
// Moves/alters the camera positions based on user input
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
||||||
|
{
|
||||||
|
// make sure the viewport matches the new window dimensions; note that width and
|
||||||
|
// height will be significantly larger than specified on retina displays.
|
||||||
|
glViewport(0, 0, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 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)
|
||||||
@@ -325,8 +304,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;
|
||||||
@@ -334,9 +313,48 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma endregion
|
// utility function for loading a 2D texture from file
|
||||||
|
// ---------------------------------------------------
|
||||||
|
unsigned int loadTexture(char const * path)
|
||||||
|
{
|
||||||
|
unsigned int textureID;
|
||||||
|
glGenTextures(1, &textureID);
|
||||||
|
|
||||||
|
int width, height, nrComponents;
|
||||||
|
unsigned char *data = stbi_load(path, &width, &height, &nrComponents, 0);
|
||||||
|
if (data)
|
||||||
|
{
|
||||||
|
GLenum format;
|
||||||
|
if (nrComponents == 1)
|
||||||
|
format = GL_RED;
|
||||||
|
else if (nrComponents == 3)
|
||||||
|
format = GL_RGB;
|
||||||
|
else if (nrComponents == 4)
|
||||||
|
format = GL_RGBA;
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
|
||||||
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
|
|
||||||
|
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_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
|
||||||
|
stbi_image_free(data);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "Texture failed to load at path: " << path << std::endl;
|
||||||
|
stbi_image_free(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return textureID;
|
||||||
|
}
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
#version 330 core
|
|
||||||
layout (location = 0) in vec3 position;
|
|
||||||
layout (location = 1) in vec3 normal;
|
|
||||||
layout (location = 2) in vec2 texCoords;
|
|
||||||
layout (location = 3) in vec3 tangent;
|
|
||||||
layout (location = 4) in vec3 bitangent;
|
|
||||||
|
|
||||||
out VS_OUT {
|
|
||||||
vec3 FragPos;
|
|
||||||
vec2 TexCoords;
|
|
||||||
vec3 TangentLightPos;
|
|
||||||
vec3 TangentViewPos;
|
|
||||||
vec3 TangentFragPos;
|
|
||||||
} vs_out;
|
|
||||||
|
|
||||||
uniform mat4 projection;
|
|
||||||
uniform mat4 view;
|
|
||||||
uniform mat4 model;
|
|
||||||
|
|
||||||
uniform vec3 lightPos;
|
|
||||||
uniform vec3 viewPos;
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
gl_Position = projection * view * model * vec4(position, 1.0f);
|
|
||||||
vs_out.FragPos = vec3(model * vec4(position, 1.0));
|
|
||||||
vs_out.TexCoords = texCoords;
|
|
||||||
|
|
||||||
|
|
||||||
vec3 T = normalize(mat3(model) * tangent);
|
|
||||||
vec3 B = normalize(mat3(model) * bitangent);
|
|
||||||
vec3 N = normalize(mat3(model) * normal);
|
|
||||||
mat3 TBN = transpose(mat3(T, B, N));
|
|
||||||
|
|
||||||
vs_out.TangentLightPos = TBN * lightPos;
|
|
||||||
vs_out.TangentViewPos = TBN * viewPos;
|
|
||||||
vs_out.TangentFragPos = TBN * vs_out.FragPos;
|
|
||||||
}
|
|
||||||
@@ -1,342 +0,0 @@
|
|||||||
// Std. Includes
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
// GLEW
|
|
||||||
#define GLEW_STATIC
|
|
||||||
#include <GL/glew.h>
|
|
||||||
|
|
||||||
// GLFW
|
|
||||||
#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/gtc/matrix_transform.hpp>
|
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
|
||||||
|
|
||||||
// Other Libs
|
|
||||||
#include <SOIL.h>
|
|
||||||
#include <learnopengl/filesystem.h>
|
|
||||||
|
|
||||||
// Properties
|
|
||||||
const GLuint SCR_WIDTH = 800, SCR_HEIGHT = 600;
|
|
||||||
|
|
||||||
// Function prototypes
|
|
||||||
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 Do_Movement();
|
|
||||||
GLuint loadTexture(GLchar const * path);
|
|
||||||
void RenderQuad();
|
|
||||||
|
|
||||||
// Camera
|
|
||||||
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
|
|
||||||
|
|
||||||
GLfloat deltaTime = 0.0f;
|
|
||||||
GLfloat lastFrame = 0.0f;
|
|
||||||
|
|
||||||
GLboolean parallax_mapping = true;
|
|
||||||
GLfloat height_scale = 0.1;
|
|
||||||
|
|
||||||
// The MAIN function, from here we start our application and run our Game loop
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
// Init GLFW
|
|
||||||
glfwInit();
|
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
||||||
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
|
|
||||||
|
|
||||||
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", nullptr, nullptr); // Windowed
|
|
||||||
glfwMakeContextCurrent(window);
|
|
||||||
|
|
||||||
// Set the required callback functions
|
|
||||||
glfwSetKeyCallback(window, key_callback);
|
|
||||||
glfwSetCursorPosCallback(window, mouse_callback);
|
|
||||||
glfwSetScrollCallback(window, scroll_callback);
|
|
||||||
|
|
||||||
// Options
|
|
||||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
|
||||||
|
|
||||||
// Initialize GLEW to setup the OpenGL Function pointers
|
|
||||||
glewExperimental = GL_TRUE;
|
|
||||||
glewInit();
|
|
||||||
|
|
||||||
// Define the viewport dimensions
|
|
||||||
glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
|
|
||||||
|
|
||||||
// Setup some OpenGL options
|
|
||||||
glEnable(GL_DEPTH_TEST);
|
|
||||||
|
|
||||||
// Setup and compile our shaders
|
|
||||||
Shader shader("parallax_mapping.vs", "parallax_mapping.frag");
|
|
||||||
|
|
||||||
// Load textures
|
|
||||||
GLuint diffuseMap = loadTexture(FileSystem::getPath("resources/textures/bricks2.jpg").c_str());
|
|
||||||
GLuint normalMap = loadTexture(FileSystem::getPath("resources/textures/bricks2_normal.jpg").c_str());
|
|
||||||
GLuint heightMap = loadTexture(FileSystem::getPath("resources/textures/bricks2_disp.jpg").c_str());
|
|
||||||
//GLuint diffuseMap = loadTexture(FileSystem::getPath("resources/textures/wood.png").c_str();
|
|
||||||
//GLuint normalMap = loadTexture(FileSystem::getPath("resources/textures/toy_box_normal.png").c_str());
|
|
||||||
//GLuint heightMap = loadTexture(FileSystem::getPath("resources/textures/toy_box_disp.png").c_str());
|
|
||||||
|
|
||||||
// Set texture units
|
|
||||||
shader.Use();
|
|
||||||
glUniform1i(glGetUniformLocation(shader.Program, "diffuseMap"), 0);
|
|
||||||
glUniform1i(glGetUniformLocation(shader.Program, "normalMap"), 1);
|
|
||||||
glUniform1i(glGetUniformLocation(shader.Program, "depthMap"), 2);
|
|
||||||
|
|
||||||
// Light position
|
|
||||||
glm::vec3 lightPos(0.5f, 1.0f, 0.3f);
|
|
||||||
|
|
||||||
// Game loop
|
|
||||||
while (!glfwWindowShouldClose(window))
|
|
||||||
{
|
|
||||||
// Set frame time
|
|
||||||
GLfloat currentFrame = glfwGetTime();
|
|
||||||
deltaTime = currentFrame - lastFrame;
|
|
||||||
lastFrame = currentFrame;
|
|
||||||
|
|
||||||
// Check and call events
|
|
||||||
glfwPollEvents();
|
|
||||||
Do_Movement();
|
|
||||||
|
|
||||||
// Clear the colorbuffer
|
|
||||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
||||||
|
|
||||||
// Configure view/projection matrices
|
|
||||||
shader.Use();
|
|
||||||
glm::mat4 view = camera.GetViewMatrix();
|
|
||||||
glm::mat4 projection = glm::perspective(camera.Zoom, (GLfloat)SCR_WIDTH / (GLfloat)SCR_HEIGHT, 0.1f, 100.0f);
|
|
||||||
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "view"), 1, GL_FALSE, glm::value_ptr(view));
|
|
||||||
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
|
|
||||||
// Render normal-mapped quad
|
|
||||||
glm::mat4 model;
|
|
||||||
//model = glm::rotate(model, (GLfloat)glfwGetTime() * -10, glm::normalize(glm::vec3(1.0, 0.0, 1.0))); // Rotates the quad to show parallax mapping works in all directions
|
|
||||||
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
|
|
||||||
glUniform3fv(glGetUniformLocation(shader.Program, "lightPos"), 1, &lightPos[0]);
|
|
||||||
glUniform3fv(glGetUniformLocation(shader.Program, "viewPos"), 1, &camera.Position[0]);
|
|
||||||
glUniform1f(glGetUniformLocation(shader.Program, "height_scale"), height_scale);
|
|
||||||
glUniform1i(glGetUniformLocation(shader.Program, "parallax"), parallax_mapping);
|
|
||||||
glActiveTexture(GL_TEXTURE0);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, diffuseMap);
|
|
||||||
glActiveTexture(GL_TEXTURE1);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, normalMap);
|
|
||||||
glActiveTexture(GL_TEXTURE2);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, heightMap);
|
|
||||||
RenderQuad();
|
|
||||||
|
|
||||||
// render light source (simply renders a smaller plane at the light's position for debugging/visualization)
|
|
||||||
model = glm::mat4();
|
|
||||||
model = glm::translate(model, lightPos);
|
|
||||||
model = glm::scale(model, glm::vec3(0.1f));
|
|
||||||
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
|
|
||||||
//RenderQuad();
|
|
||||||
|
|
||||||
// Swap the buffers
|
|
||||||
glfwSwapBuffers(window);
|
|
||||||
}
|
|
||||||
|
|
||||||
glfwTerminate();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// RenderQuad() Renders a 1x1 quad in NDC
|
|
||||||
GLuint quadVAO = 0;
|
|
||||||
GLuint quadVBO;
|
|
||||||
void RenderQuad()
|
|
||||||
{
|
|
||||||
if (quadVAO == 0)
|
|
||||||
{
|
|
||||||
// positions
|
|
||||||
glm::vec3 pos1(-1.0, 1.0, 0.0);
|
|
||||||
glm::vec3 pos2(-1.0, -1.0, 0.0);
|
|
||||||
glm::vec3 pos3(1.0, -1.0, 0.0);
|
|
||||||
glm::vec3 pos4(1.0, 1.0, 0.0);
|
|
||||||
// texture coordinates
|
|
||||||
glm::vec2 uv1(0.0, 1.0);
|
|
||||||
glm::vec2 uv2(0.0, 0.0);
|
|
||||||
glm::vec2 uv3(1.0, 0.0);
|
|
||||||
glm::vec2 uv4(1.0, 1.0);
|
|
||||||
// normal vector
|
|
||||||
glm::vec3 nm(0.0, 0.0, 1.0);
|
|
||||||
|
|
||||||
// calculate tangent/bitangent vectors of both triangles
|
|
||||||
glm::vec3 tangent1, bitangent1;
|
|
||||||
glm::vec3 tangent2, bitangent2;
|
|
||||||
// - triangle 1
|
|
||||||
glm::vec3 edge1 = pos2 - pos1;
|
|
||||||
glm::vec3 edge2 = pos3 - pos1;
|
|
||||||
glm::vec2 deltaUV1 = uv2 - uv1;
|
|
||||||
glm::vec2 deltaUV2 = uv3 - uv1;
|
|
||||||
|
|
||||||
GLfloat f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);
|
|
||||||
|
|
||||||
tangent1.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x);
|
|
||||||
tangent1.y = f * (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y);
|
|
||||||
tangent1.z = f * (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z);
|
|
||||||
tangent1 = glm::normalize(tangent1);
|
|
||||||
|
|
||||||
bitangent1.x = f * (-deltaUV2.x * edge1.x + deltaUV1.x * edge2.x);
|
|
||||||
bitangent1.y = f * (-deltaUV2.x * edge1.y + deltaUV1.x * edge2.y);
|
|
||||||
bitangent1.z = f * (-deltaUV2.x * edge1.z + deltaUV1.x * edge2.z);
|
|
||||||
bitangent1 = glm::normalize(bitangent1);
|
|
||||||
|
|
||||||
// - triangle 2
|
|
||||||
edge1 = pos3 - pos1;
|
|
||||||
edge2 = pos4 - pos1;
|
|
||||||
deltaUV1 = uv3 - uv1;
|
|
||||||
deltaUV2 = uv4 - uv1;
|
|
||||||
|
|
||||||
f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);
|
|
||||||
|
|
||||||
tangent2.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x);
|
|
||||||
tangent2.y = f * (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y);
|
|
||||||
tangent2.z = f * (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z);
|
|
||||||
tangent2 = glm::normalize(tangent2);
|
|
||||||
|
|
||||||
|
|
||||||
bitangent2.x = f * (-deltaUV2.x * edge1.x + deltaUV1.x * edge2.x);
|
|
||||||
bitangent2.y = f * (-deltaUV2.x * edge1.y + deltaUV1.x * edge2.y);
|
|
||||||
bitangent2.z = f * (-deltaUV2.x * edge1.z + deltaUV1.x * edge2.z);
|
|
||||||
bitangent2 = glm::normalize(bitangent2);
|
|
||||||
|
|
||||||
|
|
||||||
GLfloat quadVertices[] = {
|
|
||||||
// Positions // normal // TexCoords // Tangent // Bitangent
|
|
||||||
pos1.x, pos1.y, pos1.z, nm.x, nm.y, nm.z, uv1.x, uv1.y, tangent1.x, tangent1.y, tangent1.z, bitangent1.x, bitangent1.y, bitangent1.z,
|
|
||||||
pos2.x, pos2.y, pos2.z, nm.x, nm.y, nm.z, uv2.x, uv2.y, tangent1.x, tangent1.y, tangent1.z, bitangent1.x, bitangent1.y, bitangent1.z,
|
|
||||||
pos3.x, pos3.y, pos3.z, nm.x, nm.y, nm.z, uv3.x, uv3.y, tangent1.x, tangent1.y, tangent1.z, bitangent1.x, bitangent1.y, bitangent1.z,
|
|
||||||
|
|
||||||
pos1.x, pos1.y, pos1.z, nm.x, nm.y, nm.z, uv1.x, uv1.y, tangent2.x, tangent2.y, tangent2.z, bitangent2.x, bitangent2.y, bitangent2.z,
|
|
||||||
pos3.x, pos3.y, pos3.z, nm.x, nm.y, nm.z, uv3.x, uv3.y, tangent2.x, tangent2.y, tangent2.z, bitangent2.x, bitangent2.y, bitangent2.z,
|
|
||||||
pos4.x, pos4.y, pos4.z, nm.x, nm.y, nm.z, uv4.x, uv4.y, tangent2.x, tangent2.y, tangent2.z, bitangent2.x, bitangent2.y, bitangent2.z
|
|
||||||
};
|
|
||||||
// Setup 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);
|
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(GLfloat), (GLvoid*)0);
|
|
||||||
glEnableVertexAttribArray(1);
|
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
|
|
||||||
glEnableVertexAttribArray(2);
|
|
||||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 14 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
|
|
||||||
glEnableVertexAttribArray(3);
|
|
||||||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(GLfloat), (GLvoid*)(8 * sizeof(GLfloat)));
|
|
||||||
glEnableVertexAttribArray(4);
|
|
||||||
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(GLfloat), (GLvoid*)(11 * sizeof(GLfloat)));
|
|
||||||
}
|
|
||||||
glBindVertexArray(quadVAO);
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
||||||
glBindVertexArray(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// This function loads a texture from file. Note: texture loading functions like these are usually
|
|
||||||
// managed by a 'Resource Manager' that manages all resources (like textures, models, audio).
|
|
||||||
// For learning purposes we'll just define it as a utility function.
|
|
||||||
GLuint loadTexture(GLchar const * path)
|
|
||||||
{
|
|
||||||
//Generate texture ID and load texture data
|
|
||||||
GLuint textureID;
|
|
||||||
glGenTextures(1, &textureID);
|
|
||||||
int width, height;
|
|
||||||
unsigned char* image = SOIL_load_image(path, &width, &height, 0, SOIL_LOAD_RGB);
|
|
||||||
// Assign texture to ID
|
|
||||||
glBindTexture(GL_TEXTURE_2D, textureID);
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
|
|
||||||
glGenerateMipmap(GL_TEXTURE_2D);
|
|
||||||
|
|
||||||
// Parameters
|
|
||||||
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_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
|
||||||
SOIL_free_image_data(image);
|
|
||||||
return textureID;
|
|
||||||
}
|
|
||||||
|
|
||||||
#pragma region "User input"
|
|
||||||
|
|
||||||
bool keys[1024];
|
|
||||||
bool keysPressed[1024];
|
|
||||||
// Moves/alters the camera positions based on user input
|
|
||||||
void Do_Movement()
|
|
||||||
{
|
|
||||||
// Camera controls
|
|
||||||
if (keys[GLFW_KEY_W])
|
|
||||||
camera.ProcessKeyboard(FORWARD, deltaTime);
|
|
||||||
if (keys[GLFW_KEY_S])
|
|
||||||
camera.ProcessKeyboard(BACKWARD, deltaTime);
|
|
||||||
if (keys[GLFW_KEY_A])
|
|
||||||
camera.ProcessKeyboard(LEFT, deltaTime);
|
|
||||||
if (keys[GLFW_KEY_D])
|
|
||||||
camera.ProcessKeyboard(RIGHT, deltaTime);
|
|
||||||
|
|
||||||
// Change parallax height scale
|
|
||||||
if (keys[GLFW_KEY_Q])
|
|
||||||
height_scale -= 0.05 * deltaTime;
|
|
||||||
else if (keys[GLFW_KEY_E])
|
|
||||||
height_scale += 0.05 * deltaTime;
|
|
||||||
|
|
||||||
// Enable/disable parallax mapping
|
|
||||||
if (keys[GLFW_KEY_SPACE] && !keysPressed[GLFW_KEY_SPACE])
|
|
||||||
{
|
|
||||||
parallax_mapping = !parallax_mapping;
|
|
||||||
keysPressed[GLFW_KEY_SPACE] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Is called whenever a key is pressed/released via GLFW
|
|
||||||
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
|
|
||||||
{
|
|
||||||
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
|
|
||||||
glfwSetWindowShouldClose(window, GL_TRUE);
|
|
||||||
|
|
||||||
if (key >= 0 && key <= 1024)
|
|
||||||
{
|
|
||||||
if (action == GLFW_PRESS)
|
|
||||||
keys[key] = true;
|
|
||||||
else if (action == GLFW_RELEASE)
|
|
||||||
{
|
|
||||||
keys[key] = false;
|
|
||||||
keysPressed[key] = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
GLfloat lastX = 400, lastY = 300;
|
|
||||||
bool firstMouse = true;
|
|
||||||
// Moves/alters the camera positions based on user input
|
|
||||||
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
|
|
||||||
{
|
|
||||||
if (firstMouse)
|
|
||||||
{
|
|
||||||
lastX = xpos;
|
|
||||||
lastY = ypos;
|
|
||||||
firstMouse = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
GLfloat xoffset = xpos - lastX;
|
|
||||||
GLfloat yoffset = lastY - ypos;
|
|
||||||
|
|
||||||
lastX = xpos;
|
|
||||||
lastY = ypos;
|
|
||||||
|
|
||||||
camera.ProcessMouseMovement(xoffset, yoffset);
|
|
||||||
}
|
|
||||||
|
|
||||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
|
||||||
{
|
|
||||||
camera.ProcessMouseScroll(yoffset);
|
|
||||||
}
|
|
||||||
|
|
||||||
#pragma endregion
|
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
in VS_OUT {
|
||||||
|
vec3 FragPos;
|
||||||
|
vec2 TexCoords;
|
||||||
|
vec3 TangentLightPos;
|
||||||
|
vec3 TangentViewPos;
|
||||||
|
vec3 TangentFragPos;
|
||||||
|
} fs_in;
|
||||||
|
|
||||||
|
uniform sampler2D diffuseMap;
|
||||||
|
uniform sampler2D normalMap;
|
||||||
|
uniform sampler2D depthMap;
|
||||||
|
|
||||||
|
uniform float heightScale;
|
||||||
|
|
||||||
|
vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir)
|
||||||
|
{
|
||||||
|
// number of depth layers
|
||||||
|
const float minLayers = 8;
|
||||||
|
const float maxLayers = 32;
|
||||||
|
float numLayers = mix(maxLayers, minLayers, abs(dot(vec3(0.0, 0.0, 1.0), viewDir)));
|
||||||
|
// calculate the size of each layer
|
||||||
|
float layerDepth = 1.0 / numLayers;
|
||||||
|
// depth of current layer
|
||||||
|
float currentLayerDepth = 0.0;
|
||||||
|
// the amount to shift the texture coordinates per layer (from vector P)
|
||||||
|
vec2 P = viewDir.xy / viewDir.z * heightScale;
|
||||||
|
vec2 deltaTexCoords = P / numLayers;
|
||||||
|
|
||||||
|
// get initial values
|
||||||
|
vec2 currentTexCoords = texCoords;
|
||||||
|
float currentDepthMapValue = texture(depthMap, currentTexCoords).r;
|
||||||
|
|
||||||
|
while(currentLayerDepth < currentDepthMapValue)
|
||||||
|
{
|
||||||
|
// shift texture coordinates along direction of P
|
||||||
|
currentTexCoords -= deltaTexCoords;
|
||||||
|
// get depthmap value at current texture coordinates
|
||||||
|
currentDepthMapValue = texture(depthMap, currentTexCoords).r;
|
||||||
|
// get depth of next layer
|
||||||
|
currentLayerDepth += layerDepth;
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentTexCoords;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
// offset texture coordinates with Parallax Mapping
|
||||||
|
vec3 viewDir = normalize(fs_in.TangentViewPos - fs_in.TangentFragPos);
|
||||||
|
vec2 texCoords = fs_in.TexCoords;
|
||||||
|
|
||||||
|
texCoords = ParallaxMapping(fs_in.TexCoords, viewDir);
|
||||||
|
if(texCoords.x > 1.0 || texCoords.y > 1.0 || texCoords.x < 0.0 || texCoords.y < 0.0)
|
||||||
|
discard;
|
||||||
|
|
||||||
|
// obtain normal from normal map
|
||||||
|
vec3 normal = texture(normalMap, texCoords).rgb;
|
||||||
|
normal = normalize(normal * 2.0 - 1.0);
|
||||||
|
|
||||||
|
// get diffuse color
|
||||||
|
vec3 color = texture(diffuseMap, texCoords).rgb;
|
||||||
|
// ambient
|
||||||
|
vec3 ambient = 0.1 * color;
|
||||||
|
// diffuse
|
||||||
|
vec3 lightDir = normalize(fs_in.TangentLightPos - fs_in.TangentFragPos);
|
||||||
|
float diff = max(dot(lightDir, normal), 0.0);
|
||||||
|
vec3 diffuse = diff * color;
|
||||||
|
// specular
|
||||||
|
vec3 reflectDir = reflect(-lightDir, normal);
|
||||||
|
vec3 halfwayDir = normalize(lightDir + viewDir);
|
||||||
|
float spec = pow(max(dot(normal, halfwayDir), 0.0), 32.0);
|
||||||
|
|
||||||
|
vec3 specular = vec3(0.2) * spec;
|
||||||
|
FragColor = vec4(ambient + diffuse + specular, 1.0);
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec3 aNormal;
|
||||||
|
layout (location = 2) in vec2 aTexCoords;
|
||||||
|
layout (location = 3) in vec3 aTangent;
|
||||||
|
layout (location = 4) in vec3 aBitangent;
|
||||||
|
|
||||||
|
out VS_OUT {
|
||||||
|
vec3 FragPos;
|
||||||
|
vec2 TexCoords;
|
||||||
|
vec3 TangentLightPos;
|
||||||
|
vec3 TangentViewPos;
|
||||||
|
vec3 TangentFragPos;
|
||||||
|
} vs_out;
|
||||||
|
|
||||||
|
uniform mat4 projection;
|
||||||
|
uniform mat4 view;
|
||||||
|
uniform mat4 model;
|
||||||
|
|
||||||
|
uniform vec3 lightPos;
|
||||||
|
uniform vec3 viewPos;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vs_out.FragPos = vec3(model * vec4(aPos, 1.0));
|
||||||
|
vs_out.TexCoords = aTexCoords;
|
||||||
|
|
||||||
|
vec3 T = normalize(mat3(model) * aTangent);
|
||||||
|
vec3 B = normalize(mat3(model) * aBitangent);
|
||||||
|
vec3 N = normalize(mat3(model) * aNormal);
|
||||||
|
mat3 TBN = transpose(mat3(T, B, N));
|
||||||
|
|
||||||
|
vs_out.TangentLightPos = TBN * lightPos;
|
||||||
|
vs_out.TangentViewPos = TBN * viewPos;
|
||||||
|
vs_out.TangentFragPos = TBN * vs_out.FragPos;
|
||||||
|
|
||||||
|
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||||
|
}
|
||||||
@@ -0,0 +1,360 @@
|
|||||||
|
#include <glad/glad.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <stb_image.h>
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
|
||||||
|
#include <learnopengl/filesystem.h>
|
||||||
|
#include <learnopengl/shader.h>
|
||||||
|
#include <learnopengl/camera.h>
|
||||||
|
#include <learnopengl/model.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void framebuffer_size_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 processInput(GLFWwindow *window);
|
||||||
|
unsigned int loadTexture(const char *path);
|
||||||
|
void renderQuad();
|
||||||
|
|
||||||
|
// settings
|
||||||
|
const unsigned int SCR_WIDTH = 1280;
|
||||||
|
const unsigned int SCR_HEIGHT = 720;
|
||||||
|
float heightScale = 0.1;
|
||||||
|
|
||||||
|
// camera
|
||||||
|
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
|
||||||
|
float lastX = (float)SCR_WIDTH / 2.0;
|
||||||
|
float lastY = (float)SCR_HEIGHT / 2.0;
|
||||||
|
bool firstMouse = true;
|
||||||
|
|
||||||
|
// timing
|
||||||
|
float deltaTime = 0.0f;
|
||||||
|
float lastFrame = 0.0f;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// glfw: initialize and configure
|
||||||
|
// ------------------------------
|
||||||
|
glfwInit();
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
|
||||||
|
// glfw window creation
|
||||||
|
// --------------------
|
||||||
|
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
|
||||||
|
glfwMakeContextCurrent(window);
|
||||||
|
if (window == NULL)
|
||||||
|
{
|
||||||
|
std::cout << "Failed to create GLFW window" << std::endl;
|
||||||
|
glfwTerminate();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||||
|
glfwSetCursorPosCallback(window, mouse_callback);
|
||||||
|
glfwSetScrollCallback(window, scroll_callback);
|
||||||
|
|
||||||
|
// tell GLFW to capture our mouse
|
||||||
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||||
|
|
||||||
|
// glad: load all OpenGL function pointers
|
||||||
|
// ---------------------------------------
|
||||||
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||||
|
{
|
||||||
|
std::cout << "Failed to initialize GLAD" << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// configure global opengl state
|
||||||
|
// -----------------------------
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
|
||||||
|
// build and compile shaders
|
||||||
|
// -------------------------
|
||||||
|
Shader shader("5.2.parallax_mapping.vs", "5.2.parallax_mapping.fs");
|
||||||
|
|
||||||
|
// load textures
|
||||||
|
// -------------
|
||||||
|
unsigned int diffuseMap = loadTexture(FileSystem::getPath("resources/textures/bricks2.jpg").c_str());
|
||||||
|
unsigned int normalMap = loadTexture(FileSystem::getPath("resources/textures/bricks2_normal.jpg").c_str());
|
||||||
|
unsigned int heightMap = loadTexture(FileSystem::getPath("resources/textures/bricks2_disp.jpg").c_str());
|
||||||
|
/* unsigned int diffuseMap = loadTexture(FileSystem::getPath("resources/textures/toy_box_diffuse.png").c_str());
|
||||||
|
unsigned int normalMap = loadTexture(FileSystem::getPath("resources/textures/toy_box_normal.png").c_str());
|
||||||
|
unsigned int heightMap = loadTexture(FileSystem::getPath("resources/textures/toy_box_disp.png").c_str());*/
|
||||||
|
|
||||||
|
// shader configuration
|
||||||
|
// --------------------
|
||||||
|
shader.use();
|
||||||
|
shader.setInt("diffuseMap", 0);
|
||||||
|
shader.setInt("normalMap", 1);
|
||||||
|
shader.setInt("depthMap", 2);
|
||||||
|
|
||||||
|
// lighting info
|
||||||
|
// -------------
|
||||||
|
glm::vec3 lightPos(0.5f, 1.0f, 0.3f);
|
||||||
|
|
||||||
|
// render loop
|
||||||
|
// -----------
|
||||||
|
while (!glfwWindowShouldClose(window))
|
||||||
|
{
|
||||||
|
// per-frame time logic
|
||||||
|
// --------------------
|
||||||
|
float currentFrame = glfwGetTime();
|
||||||
|
deltaTime = currentFrame - lastFrame;
|
||||||
|
lastFrame = currentFrame;
|
||||||
|
|
||||||
|
// input
|
||||||
|
// -----
|
||||||
|
processInput(window);
|
||||||
|
|
||||||
|
// render
|
||||||
|
// ------
|
||||||
|
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
// configure view/projection matrices
|
||||||
|
glm::mat4 projection = glm::perspective(camera.Zoom, (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||||
|
glm::mat4 view = camera.GetViewMatrix();
|
||||||
|
shader.use();
|
||||||
|
shader.setMat4("projection", projection);
|
||||||
|
shader.setMat4("view", view);
|
||||||
|
// render parallax-mapped quad
|
||||||
|
glm::mat4 model;
|
||||||
|
model = glm::rotate(model, glm::radians((float)glfwGetTime() * -10.0f), glm::normalize(glm::vec3(1.0, 0.0, 1.0))); // rotate the quad to show parallax mapping from multiple directions
|
||||||
|
shader.setMat4("model", model);
|
||||||
|
shader.setVec3("viewPos", camera.Position);
|
||||||
|
shader.setVec3("lightPos", lightPos);
|
||||||
|
shader.setFloat("heightScale", heightScale); // adjust with Q and E keys
|
||||||
|
std::cout << heightScale << std::endl;
|
||||||
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, diffuseMap);
|
||||||
|
glActiveTexture(GL_TEXTURE1);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, normalMap);
|
||||||
|
glActiveTexture(GL_TEXTURE2);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, heightMap);
|
||||||
|
renderQuad();
|
||||||
|
|
||||||
|
// render light source (simply re-renders a smaller plane at the light's position for debugging/visualization)
|
||||||
|
model = glm::mat4();
|
||||||
|
model = glm::translate(model, lightPos);
|
||||||
|
model = glm::scale(model, glm::vec3(0.1f));
|
||||||
|
shader.setMat4("model", model);
|
||||||
|
renderQuad();
|
||||||
|
|
||||||
|
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
|
||||||
|
// -------------------------------------------------------------------------------
|
||||||
|
glfwSwapBuffers(window);
|
||||||
|
glfwPollEvents();
|
||||||
|
}
|
||||||
|
|
||||||
|
glfwTerminate();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// renderQuad() renders a 1x1 quad in NDC
|
||||||
|
unsigned int quadVAO = 0;
|
||||||
|
unsigned int quadVBO;
|
||||||
|
void renderQuad()
|
||||||
|
{
|
||||||
|
if (quadVAO == 0)
|
||||||
|
{
|
||||||
|
// positions
|
||||||
|
glm::vec3 pos1(-1.0f, 1.0f, 0.0f);
|
||||||
|
glm::vec3 pos2(-1.0f, -1.0f, 0.0f);
|
||||||
|
glm::vec3 pos3( 1.0f, -1.0f, 0.0f);
|
||||||
|
glm::vec3 pos4( 1.0f, 1.0f, 0.0f);
|
||||||
|
// texture coordinates
|
||||||
|
glm::vec2 uv1(0.0f, 1.0f);
|
||||||
|
glm::vec2 uv2(0.0f, 0.0f);
|
||||||
|
glm::vec2 uv3(1.0f, 0.0f);
|
||||||
|
glm::vec2 uv4(1.0f, 1.0f);
|
||||||
|
// normal vector
|
||||||
|
glm::vec3 nm(0.0f, 0.0f, 1.0f);
|
||||||
|
|
||||||
|
// calculate tangent/bitangent vectors of both triangles
|
||||||
|
glm::vec3 tangent1, bitangent1;
|
||||||
|
glm::vec3 tangent2, bitangent2;
|
||||||
|
// triangle 1
|
||||||
|
// ----------
|
||||||
|
glm::vec3 edge1 = pos2 - pos1;
|
||||||
|
glm::vec3 edge2 = pos3 - pos1;
|
||||||
|
glm::vec2 deltaUV1 = uv2 - uv1;
|
||||||
|
glm::vec2 deltaUV2 = uv3 - uv1;
|
||||||
|
|
||||||
|
GLfloat f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);
|
||||||
|
|
||||||
|
tangent1.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x);
|
||||||
|
tangent1.y = f * (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y);
|
||||||
|
tangent1.z = f * (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z);
|
||||||
|
tangent1 = glm::normalize(tangent1);
|
||||||
|
|
||||||
|
bitangent1.x = f * (-deltaUV2.x * edge1.x + deltaUV1.x * edge2.x);
|
||||||
|
bitangent1.y = f * (-deltaUV2.x * edge1.y + deltaUV1.x * edge2.y);
|
||||||
|
bitangent1.z = f * (-deltaUV2.x * edge1.z + deltaUV1.x * edge2.z);
|
||||||
|
bitangent1 = glm::normalize(bitangent1);
|
||||||
|
|
||||||
|
// triangle 2
|
||||||
|
// ----------
|
||||||
|
edge1 = pos3 - pos1;
|
||||||
|
edge2 = pos4 - pos1;
|
||||||
|
deltaUV1 = uv3 - uv1;
|
||||||
|
deltaUV2 = uv4 - uv1;
|
||||||
|
|
||||||
|
f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);
|
||||||
|
|
||||||
|
tangent2.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x);
|
||||||
|
tangent2.y = f * (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y);
|
||||||
|
tangent2.z = f * (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z);
|
||||||
|
tangent2 = glm::normalize(tangent2);
|
||||||
|
|
||||||
|
|
||||||
|
bitangent2.x = f * (-deltaUV2.x * edge1.x + deltaUV1.x * edge2.x);
|
||||||
|
bitangent2.y = f * (-deltaUV2.x * edge1.y + deltaUV1.x * edge2.y);
|
||||||
|
bitangent2.z = f * (-deltaUV2.x * edge1.z + deltaUV1.x * edge2.z);
|
||||||
|
bitangent2 = glm::normalize(bitangent2);
|
||||||
|
|
||||||
|
|
||||||
|
float quadVertices[] = {
|
||||||
|
// positions // normal // texcoords // tangent // bitangent
|
||||||
|
pos1.x, pos1.y, pos1.z, nm.x, nm.y, nm.z, uv1.x, uv1.y, tangent1.x, tangent1.y, tangent1.z, bitangent1.x, bitangent1.y, bitangent1.z,
|
||||||
|
pos2.x, pos2.y, pos2.z, nm.x, nm.y, nm.z, uv2.x, uv2.y, tangent1.x, tangent1.y, tangent1.z, bitangent1.x, bitangent1.y, bitangent1.z,
|
||||||
|
pos3.x, pos3.y, pos3.z, nm.x, nm.y, nm.z, uv3.x, uv3.y, tangent1.x, tangent1.y, tangent1.z, bitangent1.x, bitangent1.y, bitangent1.z,
|
||||||
|
|
||||||
|
pos1.x, pos1.y, pos1.z, nm.x, nm.y, nm.z, uv1.x, uv1.y, tangent2.x, tangent2.y, tangent2.z, bitangent2.x, bitangent2.y, bitangent2.z,
|
||||||
|
pos3.x, pos3.y, pos3.z, nm.x, nm.y, nm.z, uv3.x, uv3.y, tangent2.x, tangent2.y, tangent2.z, bitangent2.x, bitangent2.y, bitangent2.z,
|
||||||
|
pos4.x, pos4.y, pos4.z, nm.x, nm.y, nm.z, uv4.x, uv4.y, tangent2.x, tangent2.y, tangent2.z, bitangent2.x, bitangent2.y, bitangent2.z
|
||||||
|
};
|
||||||
|
// 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);
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)0);
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||||
|
glEnableVertexAttribArray(2);
|
||||||
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(6 * sizeof(float)));
|
||||||
|
glEnableVertexAttribArray(3);
|
||||||
|
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(8 * sizeof(float)));
|
||||||
|
glEnableVertexAttribArray(4);
|
||||||
|
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(11 * sizeof(float)));
|
||||||
|
}
|
||||||
|
glBindVertexArray(quadVAO);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
|
glBindVertexArray(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
|
||||||
|
// ---------------------------------------------------------------------------------------------------------
|
||||||
|
void processInput(GLFWwindow *window)
|
||||||
|
{
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||||
|
glfwSetWindowShouldClose(window, true);
|
||||||
|
|
||||||
|
float cameraSpeed = 2.5 * deltaTime;
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||||
|
camera.ProcessKeyboard(FORWARD, deltaTime);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||||
|
camera.ProcessKeyboard(BACKWARD, deltaTime);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||||
|
camera.ProcessKeyboard(LEFT, deltaTime);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||||
|
camera.ProcessKeyboard(RIGHT, deltaTime);
|
||||||
|
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
if (heightScale > 0.0f)
|
||||||
|
heightScale -= 0.0005f;
|
||||||
|
else
|
||||||
|
heightScale = 0.0f;
|
||||||
|
}
|
||||||
|
else if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
if (heightScale < 1.0f)
|
||||||
|
heightScale += 0.0005f;
|
||||||
|
else
|
||||||
|
heightScale = 1.0f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
|
||||||
|
// ---------------------------------------------------------------------------------------------
|
||||||
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
||||||
|
{
|
||||||
|
// make sure the viewport matches the new window dimensions; note that width and
|
||||||
|
// height will be significantly larger than specified on retina displays.
|
||||||
|
glViewport(0, 0, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// glfw: whenever the mouse moves, this callback is called
|
||||||
|
// -------------------------------------------------------
|
||||||
|
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
|
||||||
|
{
|
||||||
|
if (firstMouse)
|
||||||
|
{
|
||||||
|
lastX = xpos;
|
||||||
|
lastY = ypos;
|
||||||
|
firstMouse = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
float xoffset = xpos - lastX;
|
||||||
|
float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
|
||||||
|
|
||||||
|
lastX = xpos;
|
||||||
|
lastY = ypos;
|
||||||
|
|
||||||
|
camera.ProcessMouseMovement(xoffset, yoffset);
|
||||||
|
}
|
||||||
|
|
||||||
|
// glfw: whenever the mouse scroll wheel scrolls, this callback is called
|
||||||
|
// ----------------------------------------------------------------------
|
||||||
|
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
||||||
|
{
|
||||||
|
camera.ProcessMouseScroll(yoffset);
|
||||||
|
}
|
||||||
|
|
||||||
|
// utility function for loading a 2D texture from file
|
||||||
|
// ---------------------------------------------------
|
||||||
|
unsigned int loadTexture(char const * path)
|
||||||
|
{
|
||||||
|
unsigned int textureID;
|
||||||
|
glGenTextures(1, &textureID);
|
||||||
|
|
||||||
|
int width, height, nrComponents;
|
||||||
|
unsigned char *data = stbi_load(path, &width, &height, &nrComponents, 0);
|
||||||
|
if (data)
|
||||||
|
{
|
||||||
|
GLenum format;
|
||||||
|
if (nrComponents == 1)
|
||||||
|
format = GL_RED;
|
||||||
|
else if (nrComponents == 3)
|
||||||
|
format = GL_RGB;
|
||||||
|
else if (nrComponents == 4)
|
||||||
|
format = GL_RGBA;
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
|
||||||
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
|
|
||||||
|
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_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
|
||||||
|
stbi_image_free(data);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "Texture failed to load at path: " << path << std::endl;
|
||||||
|
stbi_image_free(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return textureID;
|
||||||
|
}
|
||||||
@@ -13,14 +13,10 @@ uniform sampler2D diffuseMap;
|
|||||||
uniform sampler2D normalMap;
|
uniform sampler2D normalMap;
|
||||||
uniform sampler2D depthMap;
|
uniform sampler2D depthMap;
|
||||||
|
|
||||||
uniform bool parallax;
|
uniform float heightScale;
|
||||||
uniform float height_scale;
|
|
||||||
|
|
||||||
vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir)
|
vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir)
|
||||||
{
|
{
|
||||||
// float height = texture(depthMap, texCoords).r;
|
|
||||||
// return texCoords - viewDir.xy * (height * height_scale);
|
|
||||||
|
|
||||||
// number of depth layers
|
// number of depth layers
|
||||||
const float minLayers = 8;
|
const float minLayers = 8;
|
||||||
const float maxLayers = 32;
|
const float maxLayers = 32;
|
||||||
@@ -30,7 +26,7 @@ vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir)
|
|||||||
// depth of current layer
|
// depth of current layer
|
||||||
float currentLayerDepth = 0.0;
|
float currentLayerDepth = 0.0;
|
||||||
// the amount to shift the texture coordinates per layer (from vector P)
|
// the amount to shift the texture coordinates per layer (from vector P)
|
||||||
vec2 P = viewDir.xy / viewDir.z * height_scale;
|
vec2 P = viewDir.xy / viewDir.z * heightScale;
|
||||||
vec2 deltaTexCoords = P / numLayers;
|
vec2 deltaTexCoords = P / numLayers;
|
||||||
|
|
||||||
// get initial values
|
// get initial values
|
||||||
@@ -47,7 +43,6 @@ vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir)
|
|||||||
currentLayerDepth += layerDepth;
|
currentLayerDepth += layerDepth;
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- parallax occlusion mapping interpolation from here on
|
|
||||||
// get texture coordinates before collision (reverse operations)
|
// get texture coordinates before collision (reverse operations)
|
||||||
vec2 prevTexCoords = currentTexCoords + deltaTexCoords;
|
vec2 prevTexCoords = currentTexCoords + deltaTexCoords;
|
||||||
|
|
||||||
@@ -60,37 +55,35 @@ vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir)
|
|||||||
vec2 finalTexCoords = prevTexCoords * weight + currentTexCoords * (1.0 - weight);
|
vec2 finalTexCoords = prevTexCoords * weight + currentTexCoords * (1.0 - weight);
|
||||||
|
|
||||||
return finalTexCoords;
|
return finalTexCoords;
|
||||||
// return currentTexCoords;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
// Offset texture coordinates with Parallax Mapping
|
// offset texture coordinates with Parallax Mapping
|
||||||
vec3 viewDir = normalize(fs_in.TangentViewPos - fs_in.TangentFragPos);
|
vec3 viewDir = normalize(fs_in.TangentViewPos - fs_in.TangentFragPos);
|
||||||
vec2 texCoords = fs_in.TexCoords;
|
vec2 texCoords = fs_in.TexCoords;
|
||||||
if(parallax)
|
|
||||||
texCoords = ParallaxMapping(fs_in.TexCoords, viewDir);
|
|
||||||
|
|
||||||
|
texCoords = ParallaxMapping(fs_in.TexCoords, viewDir);
|
||||||
if(texCoords.x > 1.0 || texCoords.y > 1.0 || texCoords.x < 0.0 || texCoords.y < 0.0)
|
if(texCoords.x > 1.0 || texCoords.y > 1.0 || texCoords.x < 0.0 || texCoords.y < 0.0)
|
||||||
discard;
|
discard;
|
||||||
|
|
||||||
// Obtain normal from normal map
|
// obtain normal from normal map
|
||||||
vec3 normal = texture(normalMap, texCoords).rgb;
|
vec3 normal = texture(normalMap, texCoords).rgb;
|
||||||
normal = normalize(normal * 2.0 - 1.0);
|
normal = normalize(normal * 2.0 - 1.0);
|
||||||
|
|
||||||
// Get diffuse color
|
// get diffuse color
|
||||||
vec3 color = texture(diffuseMap, texCoords).rgb;
|
vec3 color = texture(diffuseMap, texCoords).rgb;
|
||||||
// Ambient
|
// ambient
|
||||||
vec3 ambient = 0.1 * color;
|
vec3 ambient = 0.1 * color;
|
||||||
// Diffuse
|
// diffuse
|
||||||
vec3 lightDir = normalize(fs_in.TangentLightPos - fs_in.TangentFragPos);
|
vec3 lightDir = normalize(fs_in.TangentLightPos - fs_in.TangentFragPos);
|
||||||
float diff = max(dot(lightDir, normal), 0.0);
|
float diff = max(dot(lightDir, normal), 0.0);
|
||||||
vec3 diffuse = diff * color;
|
vec3 diffuse = diff * color;
|
||||||
// Specular
|
// specular
|
||||||
vec3 reflectDir = reflect(-lightDir, normal);
|
vec3 reflectDir = reflect(-lightDir, normal);
|
||||||
vec3 halfwayDir = normalize(lightDir + viewDir);
|
vec3 halfwayDir = normalize(lightDir + viewDir);
|
||||||
float spec = pow(max(dot(normal, halfwayDir), 0.0), 32.0);
|
float spec = pow(max(dot(normal, halfwayDir), 0.0), 32.0);
|
||||||
|
|
||||||
vec3 specular = vec3(0.2) * spec;
|
vec3 specular = vec3(0.2) * spec;
|
||||||
FragColor = vec4(ambient + diffuse + specular, 1.0f);
|
FragColor = vec4(ambient + diffuse + specular, 1.0);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec3 aNormal;
|
||||||
|
layout (location = 2) in vec2 aTexCoords;
|
||||||
|
layout (location = 3) in vec3 aTangent;
|
||||||
|
layout (location = 4) in vec3 aBitangent;
|
||||||
|
|
||||||
|
out VS_OUT {
|
||||||
|
vec3 FragPos;
|
||||||
|
vec2 TexCoords;
|
||||||
|
vec3 TangentLightPos;
|
||||||
|
vec3 TangentViewPos;
|
||||||
|
vec3 TangentFragPos;
|
||||||
|
} vs_out;
|
||||||
|
|
||||||
|
uniform mat4 projection;
|
||||||
|
uniform mat4 view;
|
||||||
|
uniform mat4 model;
|
||||||
|
|
||||||
|
uniform vec3 lightPos;
|
||||||
|
uniform vec3 viewPos;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vs_out.FragPos = vec3(model * vec4(aPos, 1.0));
|
||||||
|
vs_out.TexCoords = aTexCoords;
|
||||||
|
|
||||||
|
vec3 T = normalize(mat3(model) * aTangent);
|
||||||
|
vec3 B = normalize(mat3(model) * aBitangent);
|
||||||
|
vec3 N = normalize(mat3(model) * aNormal);
|
||||||
|
mat3 TBN = transpose(mat3(T, B, N));
|
||||||
|
|
||||||
|
vs_out.TangentLightPos = TBN * lightPos;
|
||||||
|
vs_out.TangentViewPos = TBN * viewPos;
|
||||||
|
vs_out.TangentFragPos = TBN * vs_out.FragPos;
|
||||||
|
|
||||||
|
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||||
|
}
|
||||||
@@ -0,0 +1,360 @@
|
|||||||
|
#include <glad/glad.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <stb_image.h>
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
|
||||||
|
#include <learnopengl/filesystem.h>
|
||||||
|
#include <learnopengl/shader.h>
|
||||||
|
#include <learnopengl/camera.h>
|
||||||
|
#include <learnopengl/model.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void framebuffer_size_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 processInput(GLFWwindow *window);
|
||||||
|
unsigned int loadTexture(const char *path);
|
||||||
|
void renderQuad();
|
||||||
|
|
||||||
|
// settings
|
||||||
|
const unsigned int SCR_WIDTH = 1280;
|
||||||
|
const unsigned int SCR_HEIGHT = 720;
|
||||||
|
float heightScale = 0.1;
|
||||||
|
|
||||||
|
// camera
|
||||||
|
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
|
||||||
|
float lastX = (float)SCR_WIDTH / 2.0;
|
||||||
|
float lastY = (float)SCR_HEIGHT / 2.0;
|
||||||
|
bool firstMouse = true;
|
||||||
|
|
||||||
|
// timing
|
||||||
|
float deltaTime = 0.0f;
|
||||||
|
float lastFrame = 0.0f;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// glfw: initialize and configure
|
||||||
|
// ------------------------------
|
||||||
|
glfwInit();
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
|
||||||
|
// glfw window creation
|
||||||
|
// --------------------
|
||||||
|
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
|
||||||
|
glfwMakeContextCurrent(window);
|
||||||
|
if (window == NULL)
|
||||||
|
{
|
||||||
|
std::cout << "Failed to create GLFW window" << std::endl;
|
||||||
|
glfwTerminate();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||||
|
glfwSetCursorPosCallback(window, mouse_callback);
|
||||||
|
glfwSetScrollCallback(window, scroll_callback);
|
||||||
|
|
||||||
|
// tell GLFW to capture our mouse
|
||||||
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||||
|
|
||||||
|
// glad: load all OpenGL function pointers
|
||||||
|
// ---------------------------------------
|
||||||
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||||
|
{
|
||||||
|
std::cout << "Failed to initialize GLAD" << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// configure global opengl state
|
||||||
|
// -----------------------------
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
|
||||||
|
// build and compile shaders
|
||||||
|
// -------------------------
|
||||||
|
Shader shader("5.3.parallax_mapping.vs", "5.3.parallax_mapping.fs");
|
||||||
|
|
||||||
|
// load textures
|
||||||
|
// -------------
|
||||||
|
unsigned int diffuseMap = loadTexture(FileSystem::getPath("resources/textures/bricks2.jpg").c_str());
|
||||||
|
unsigned int normalMap = loadTexture(FileSystem::getPath("resources/textures/bricks2_normal.jpg").c_str());
|
||||||
|
unsigned int heightMap = loadTexture(FileSystem::getPath("resources/textures/bricks2_disp.jpg").c_str());
|
||||||
|
/*unsigned int diffuseMap = loadTexture(FileSystem::getPath("resources/textures/toy_box_diffuse.png").c_str());
|
||||||
|
unsigned int normalMap = loadTexture(FileSystem::getPath("resources/textures/toy_box_normal.png").c_str());
|
||||||
|
unsigned int heightMap = loadTexture(FileSystem::getPath("resources/textures/toy_box_disp.png").c_str());*/
|
||||||
|
|
||||||
|
// shader configuration
|
||||||
|
// --------------------
|
||||||
|
shader.use();
|
||||||
|
shader.setInt("diffuseMap", 0);
|
||||||
|
shader.setInt("normalMap", 1);
|
||||||
|
shader.setInt("depthMap", 2);
|
||||||
|
|
||||||
|
// lighting info
|
||||||
|
// -------------
|
||||||
|
glm::vec3 lightPos(0.5f, 1.0f, 0.3f);
|
||||||
|
|
||||||
|
// render loop
|
||||||
|
// -----------
|
||||||
|
while (!glfwWindowShouldClose(window))
|
||||||
|
{
|
||||||
|
// per-frame time logic
|
||||||
|
// --------------------
|
||||||
|
float currentFrame = glfwGetTime();
|
||||||
|
deltaTime = currentFrame - lastFrame;
|
||||||
|
lastFrame = currentFrame;
|
||||||
|
|
||||||
|
// input
|
||||||
|
// -----
|
||||||
|
processInput(window);
|
||||||
|
|
||||||
|
// render
|
||||||
|
// ------
|
||||||
|
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
// configure view/projection matrices
|
||||||
|
glm::mat4 projection = glm::perspective(camera.Zoom, (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||||
|
glm::mat4 view = camera.GetViewMatrix();
|
||||||
|
shader.use();
|
||||||
|
shader.setMat4("projection", projection);
|
||||||
|
shader.setMat4("view", view);
|
||||||
|
// render parallax-mapped quad
|
||||||
|
glm::mat4 model;
|
||||||
|
model = glm::rotate(model, glm::radians((float)glfwGetTime() * -10.0f), glm::normalize(glm::vec3(1.0, 0.0, 1.0))); // rotate the quad to show parallax mapping from multiple directions
|
||||||
|
shader.setMat4("model", model);
|
||||||
|
shader.setVec3("viewPos", camera.Position);
|
||||||
|
shader.setVec3("lightPos", lightPos);
|
||||||
|
shader.setFloat("heightScale", heightScale); // adjust with Q and E keys
|
||||||
|
std::cout << heightScale << std::endl;
|
||||||
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, diffuseMap);
|
||||||
|
glActiveTexture(GL_TEXTURE1);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, normalMap);
|
||||||
|
glActiveTexture(GL_TEXTURE2);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, heightMap);
|
||||||
|
renderQuad();
|
||||||
|
|
||||||
|
// render light source (simply re-renders a smaller plane at the light's position for debugging/visualization)
|
||||||
|
model = glm::mat4();
|
||||||
|
model = glm::translate(model, lightPos);
|
||||||
|
model = glm::scale(model, glm::vec3(0.1f));
|
||||||
|
shader.setMat4("model", model);
|
||||||
|
renderQuad();
|
||||||
|
|
||||||
|
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
|
||||||
|
// -------------------------------------------------------------------------------
|
||||||
|
glfwSwapBuffers(window);
|
||||||
|
glfwPollEvents();
|
||||||
|
}
|
||||||
|
|
||||||
|
glfwTerminate();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// renderQuad() renders a 1x1 quad in NDC
|
||||||
|
unsigned int quadVAO = 0;
|
||||||
|
unsigned int quadVBO;
|
||||||
|
void renderQuad()
|
||||||
|
{
|
||||||
|
if (quadVAO == 0)
|
||||||
|
{
|
||||||
|
// positions
|
||||||
|
glm::vec3 pos1(-1.0f, 1.0f, 0.0f);
|
||||||
|
glm::vec3 pos2(-1.0f, -1.0f, 0.0f);
|
||||||
|
glm::vec3 pos3( 1.0f, -1.0f, 0.0f);
|
||||||
|
glm::vec3 pos4( 1.0f, 1.0f, 0.0f);
|
||||||
|
// texture coordinates
|
||||||
|
glm::vec2 uv1(0.0f, 1.0f);
|
||||||
|
glm::vec2 uv2(0.0f, 0.0f);
|
||||||
|
glm::vec2 uv3(1.0f, 0.0f);
|
||||||
|
glm::vec2 uv4(1.0f, 1.0f);
|
||||||
|
// normal vector
|
||||||
|
glm::vec3 nm(0.0f, 0.0f, 1.0f);
|
||||||
|
|
||||||
|
// calculate tangent/bitangent vectors of both triangles
|
||||||
|
glm::vec3 tangent1, bitangent1;
|
||||||
|
glm::vec3 tangent2, bitangent2;
|
||||||
|
// triangle 1
|
||||||
|
// ----------
|
||||||
|
glm::vec3 edge1 = pos2 - pos1;
|
||||||
|
glm::vec3 edge2 = pos3 - pos1;
|
||||||
|
glm::vec2 deltaUV1 = uv2 - uv1;
|
||||||
|
glm::vec2 deltaUV2 = uv3 - uv1;
|
||||||
|
|
||||||
|
GLfloat f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);
|
||||||
|
|
||||||
|
tangent1.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x);
|
||||||
|
tangent1.y = f * (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y);
|
||||||
|
tangent1.z = f * (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z);
|
||||||
|
tangent1 = glm::normalize(tangent1);
|
||||||
|
|
||||||
|
bitangent1.x = f * (-deltaUV2.x * edge1.x + deltaUV1.x * edge2.x);
|
||||||
|
bitangent1.y = f * (-deltaUV2.x * edge1.y + deltaUV1.x * edge2.y);
|
||||||
|
bitangent1.z = f * (-deltaUV2.x * edge1.z + deltaUV1.x * edge2.z);
|
||||||
|
bitangent1 = glm::normalize(bitangent1);
|
||||||
|
|
||||||
|
// triangle 2
|
||||||
|
// ----------
|
||||||
|
edge1 = pos3 - pos1;
|
||||||
|
edge2 = pos4 - pos1;
|
||||||
|
deltaUV1 = uv3 - uv1;
|
||||||
|
deltaUV2 = uv4 - uv1;
|
||||||
|
|
||||||
|
f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);
|
||||||
|
|
||||||
|
tangent2.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x);
|
||||||
|
tangent2.y = f * (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y);
|
||||||
|
tangent2.z = f * (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z);
|
||||||
|
tangent2 = glm::normalize(tangent2);
|
||||||
|
|
||||||
|
|
||||||
|
bitangent2.x = f * (-deltaUV2.x * edge1.x + deltaUV1.x * edge2.x);
|
||||||
|
bitangent2.y = f * (-deltaUV2.x * edge1.y + deltaUV1.x * edge2.y);
|
||||||
|
bitangent2.z = f * (-deltaUV2.x * edge1.z + deltaUV1.x * edge2.z);
|
||||||
|
bitangent2 = glm::normalize(bitangent2);
|
||||||
|
|
||||||
|
|
||||||
|
float quadVertices[] = {
|
||||||
|
// positions // normal // texcoords // tangent // bitangent
|
||||||
|
pos1.x, pos1.y, pos1.z, nm.x, nm.y, nm.z, uv1.x, uv1.y, tangent1.x, tangent1.y, tangent1.z, bitangent1.x, bitangent1.y, bitangent1.z,
|
||||||
|
pos2.x, pos2.y, pos2.z, nm.x, nm.y, nm.z, uv2.x, uv2.y, tangent1.x, tangent1.y, tangent1.z, bitangent1.x, bitangent1.y, bitangent1.z,
|
||||||
|
pos3.x, pos3.y, pos3.z, nm.x, nm.y, nm.z, uv3.x, uv3.y, tangent1.x, tangent1.y, tangent1.z, bitangent1.x, bitangent1.y, bitangent1.z,
|
||||||
|
|
||||||
|
pos1.x, pos1.y, pos1.z, nm.x, nm.y, nm.z, uv1.x, uv1.y, tangent2.x, tangent2.y, tangent2.z, bitangent2.x, bitangent2.y, bitangent2.z,
|
||||||
|
pos3.x, pos3.y, pos3.z, nm.x, nm.y, nm.z, uv3.x, uv3.y, tangent2.x, tangent2.y, tangent2.z, bitangent2.x, bitangent2.y, bitangent2.z,
|
||||||
|
pos4.x, pos4.y, pos4.z, nm.x, nm.y, nm.z, uv4.x, uv4.y, tangent2.x, tangent2.y, tangent2.z, bitangent2.x, bitangent2.y, bitangent2.z
|
||||||
|
};
|
||||||
|
// 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);
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)0);
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||||
|
glEnableVertexAttribArray(2);
|
||||||
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(6 * sizeof(float)));
|
||||||
|
glEnableVertexAttribArray(3);
|
||||||
|
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(8 * sizeof(float)));
|
||||||
|
glEnableVertexAttribArray(4);
|
||||||
|
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), (void*)(11 * sizeof(float)));
|
||||||
|
}
|
||||||
|
glBindVertexArray(quadVAO);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
|
glBindVertexArray(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
|
||||||
|
// ---------------------------------------------------------------------------------------------------------
|
||||||
|
void processInput(GLFWwindow *window)
|
||||||
|
{
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||||
|
glfwSetWindowShouldClose(window, true);
|
||||||
|
|
||||||
|
float cameraSpeed = 2.5 * deltaTime;
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||||
|
camera.ProcessKeyboard(FORWARD, deltaTime);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||||
|
camera.ProcessKeyboard(BACKWARD, deltaTime);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||||
|
camera.ProcessKeyboard(LEFT, deltaTime);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||||
|
camera.ProcessKeyboard(RIGHT, deltaTime);
|
||||||
|
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
if (heightScale > 0.0f)
|
||||||
|
heightScale -= 0.0005f;
|
||||||
|
else
|
||||||
|
heightScale = 0.0f;
|
||||||
|
}
|
||||||
|
else if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
if (heightScale < 1.0f)
|
||||||
|
heightScale += 0.0005f;
|
||||||
|
else
|
||||||
|
heightScale = 1.0f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
|
||||||
|
// ---------------------------------------------------------------------------------------------
|
||||||
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
||||||
|
{
|
||||||
|
// make sure the viewport matches the new window dimensions; note that width and
|
||||||
|
// height will be significantly larger than specified on retina displays.
|
||||||
|
glViewport(0, 0, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// glfw: whenever the mouse moves, this callback is called
|
||||||
|
// -------------------------------------------------------
|
||||||
|
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
|
||||||
|
{
|
||||||
|
if (firstMouse)
|
||||||
|
{
|
||||||
|
lastX = xpos;
|
||||||
|
lastY = ypos;
|
||||||
|
firstMouse = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
float xoffset = xpos - lastX;
|
||||||
|
float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
|
||||||
|
|
||||||
|
lastX = xpos;
|
||||||
|
lastY = ypos;
|
||||||
|
|
||||||
|
camera.ProcessMouseMovement(xoffset, yoffset);
|
||||||
|
}
|
||||||
|
|
||||||
|
// glfw: whenever the mouse scroll wheel scrolls, this callback is called
|
||||||
|
// ----------------------------------------------------------------------
|
||||||
|
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
||||||
|
{
|
||||||
|
camera.ProcessMouseScroll(yoffset);
|
||||||
|
}
|
||||||
|
|
||||||
|
// utility function for loading a 2D texture from file
|
||||||
|
// ---------------------------------------------------
|
||||||
|
unsigned int loadTexture(char const * path)
|
||||||
|
{
|
||||||
|
unsigned int textureID;
|
||||||
|
glGenTextures(1, &textureID);
|
||||||
|
|
||||||
|
int width, height, nrComponents;
|
||||||
|
unsigned char *data = stbi_load(path, &width, &height, &nrComponents, 0);
|
||||||
|
if (data)
|
||||||
|
{
|
||||||
|
GLenum format;
|
||||||
|
if (nrComponents == 1)
|
||||||
|
format = GL_RED;
|
||||||
|
else if (nrComponents == 3)
|
||||||
|
format = GL_RGB;
|
||||||
|
else if (nrComponents == 4)
|
||||||
|
format = GL_RGBA;
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
|
||||||
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
|
|
||||||
|
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_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
|
||||||
|
stbi_image_free(data);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "Texture failed to load at path: " << path << std::endl;
|
||||||
|
stbi_image_free(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return textureID;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user