daily update
This commit is contained in:
16
lighting-maps/CMakeLists.txt
Executable file
16
lighting-maps/CMakeLists.txt
Executable file
@@ -0,0 +1,16 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
project(lighting-color)
|
||||
|
||||
add_executable(
|
||||
${PROJECT_NAME}
|
||||
main.cc
|
||||
glad.c
|
||||
stb_image_wrap.cc
|
||||
)
|
||||
|
||||
target_link_libraries(
|
||||
${PROJECT_NAME}
|
||||
PRIVATE
|
||||
libglfw3.a
|
||||
)
|
||||
131
lighting-maps/camera.hh
Executable file
131
lighting-maps/camera.hh
Executable file
@@ -0,0 +1,131 @@
|
||||
|
||||
#ifndef CAMERA_H
|
||||
#define CAMERA_H
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
// Defines several possible options for camera movement. Used as abstraction to
|
||||
// stay away from window-system specific input methods
|
||||
enum Camera_Movement { FORWARD, BACKWARD, LEFT, RIGHT };
|
||||
|
||||
// Default camera values
|
||||
const float YAW = -90.0f;
|
||||
const float PITCH = 0.0f;
|
||||
const float SPEED = 2.5f;
|
||||
const float SENSITIVITY = 0.1f;
|
||||
const float ZOOM = 45.0f;
|
||||
|
||||
// An abstract camera class that processes input and calculates the
|
||||
// corresponding Euler Angles, Vectors and Matrices for use in OpenGL
|
||||
class Camera {
|
||||
public:
|
||||
// camera Attributes
|
||||
glm::vec3 Position;
|
||||
glm::vec3 Front;
|
||||
glm::vec3 Up;
|
||||
glm::vec3 Right;
|
||||
glm::vec3 WorldUp;
|
||||
// euler Angles
|
||||
float Yaw;
|
||||
float Pitch;
|
||||
// camera options
|
||||
float MovementSpeed;
|
||||
float MouseSensitivity;
|
||||
float Zoom;
|
||||
|
||||
// constructor with vectors
|
||||
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f),
|
||||
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), float yaw = YAW,
|
||||
float pitch = PITCH)
|
||||
: Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED),
|
||||
MouseSensitivity(SENSITIVITY), Zoom(ZOOM) {
|
||||
Position = position;
|
||||
WorldUp = up;
|
||||
Yaw = yaw;
|
||||
Pitch = pitch;
|
||||
updateCameraVectors();
|
||||
}
|
||||
// constructor with scalar values
|
||||
Camera(float posX, float posY, float posZ, float upX, float upY, float upZ,
|
||||
float yaw, float pitch)
|
||||
: Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED),
|
||||
MouseSensitivity(SENSITIVITY), Zoom(ZOOM) {
|
||||
Position = glm::vec3(posX, posY, posZ);
|
||||
WorldUp = glm::vec3(upX, upY, upZ);
|
||||
Yaw = yaw;
|
||||
Pitch = pitch;
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
// returns the view matrix calculated using Euler Angles and the LookAt Matrix
|
||||
glm::mat4 GetViewMatrix() {
|
||||
return glm::lookAt(Position, Position + Front, Up);
|
||||
}
|
||||
|
||||
// processes input received from any keyboard-like input system. Accepts input
|
||||
// parameter in the form of camera defined ENUM (to abstract it from windowing
|
||||
// systems)
|
||||
void ProcessKeyboard(Camera_Movement direction, float deltaTime) {
|
||||
float velocity = MovementSpeed * deltaTime;
|
||||
if (direction == FORWARD)
|
||||
Position += Front * velocity;
|
||||
if (direction == BACKWARD)
|
||||
Position -= Front * velocity;
|
||||
if (direction == LEFT)
|
||||
Position -= Right * velocity;
|
||||
if (direction == RIGHT)
|
||||
Position += Right * velocity;
|
||||
}
|
||||
|
||||
// processes input received from a mouse input system. Expects the offset
|
||||
// value in both the x and y direction.
|
||||
void ProcessMouseMovement(float xoffset, float yoffset,
|
||||
GLboolean constrainPitch = true) {
|
||||
xoffset *= MouseSensitivity;
|
||||
yoffset *= MouseSensitivity;
|
||||
|
||||
Yaw += xoffset;
|
||||
Pitch += yoffset;
|
||||
|
||||
// make sure that when pitch is out of bounds, screen doesn't get flipped
|
||||
if (constrainPitch) {
|
||||
if (Pitch > 89.0f)
|
||||
Pitch = 89.0f;
|
||||
if (Pitch < -89.0f)
|
||||
Pitch = -89.0f;
|
||||
}
|
||||
|
||||
// update Front, Right and Up Vectors using the updated Euler angles
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
// processes input received from a mouse scroll-wheel event. Only requires
|
||||
// input on the vertical wheel-axis
|
||||
void ProcessMouseScroll(float yoffset) {
|
||||
Zoom -= (float)yoffset;
|
||||
if (Zoom < 1.0f)
|
||||
Zoom = 1.0f;
|
||||
if (Zoom > 45.0f)
|
||||
Zoom = 45.0f;
|
||||
}
|
||||
|
||||
private:
|
||||
// calculates the front vector from the Camera's (updated) Euler Angles
|
||||
void updateCameraVectors() {
|
||||
// calculate the new Front vector
|
||||
glm::vec3 front;
|
||||
front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
|
||||
front.y = sin(glm::radians(Pitch));
|
||||
front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
|
||||
Front = glm::normalize(front);
|
||||
// also re-calculate the Right and Up vector
|
||||
Right = glm::normalize(glm::cross(
|
||||
Front, WorldUp)); // normalize the vectors, because their length gets
|
||||
// closer to 0 the more you look up or down which
|
||||
// results in slower movement.
|
||||
Up = glm::normalize(glm::cross(Right, Front));
|
||||
}
|
||||
};
|
||||
#endif
|
||||
1140
lighting-maps/glad.c
Executable file
1140
lighting-maps/glad.c
Executable file
File diff suppressed because it is too large
Load Diff
BIN
lighting-maps/images/container2.png
Normal file
BIN
lighting-maps/images/container2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 435 KiB |
BIN
lighting-maps/images/container2_specular.png
Normal file
BIN
lighting-maps/images/container2_specular.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 141 KiB |
BIN
lighting-maps/images/matrix.jpg
Normal file
BIN
lighting-maps/images/matrix.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 291 KiB |
345
lighting-maps/main.cc
Executable file
345
lighting-maps/main.cc
Executable file
@@ -0,0 +1,345 @@
|
||||
#include <cmath>
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <glm/ext/vector_float3.hpp>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <iostream>
|
||||
#include "stb_image_wrap.h"
|
||||
#include "camera.hh"
|
||||
#include "myshader.hh"
|
||||
|
||||
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(char const* path);
|
||||
|
||||
// settings
|
||||
const unsigned int SCR_WIDTH = 800;
|
||||
const unsigned int SCR_HEIGHT = 600;
|
||||
|
||||
// camera
|
||||
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
|
||||
float lastX = SCR_WIDTH / 2.0f;
|
||||
float lastY = SCR_HEIGHT / 2.0f;
|
||||
bool firstMouse = true;
|
||||
|
||||
// timing
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
|
||||
// lighting
|
||||
glm::vec3 lightPos(1.2f, 1.0f, 2.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);
|
||||
|
||||
#ifdef __APPLE__
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
#endif
|
||||
|
||||
// glfw window creation
|
||||
// --------------------
|
||||
GLFWwindow* window =
|
||||
glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
std::cout << "Failed to create GLFW window" << std::endl;
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
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 our shader zprogram
|
||||
// ------------------------------------
|
||||
Shader lightingShader("../shaders/vscolors.glsl",
|
||||
"../shaders/fscolors.glsl");
|
||||
Shader lightCubeShader("../shaders/vslightCube.glsl",
|
||||
"../shaders/fslightCube.glsl");
|
||||
|
||||
// set up vertex data (and buffer(s)) and configure vertex attributes
|
||||
// ------------------------------------------------------------------
|
||||
float vertices[] = {
|
||||
// positions // normals // texture coords
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.5f, -0.5f,
|
||||
-0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, 0.5f, 0.5f, -0.5f, 0.0f,
|
||||
0.0f, -1.0f, 1.0f, 1.0f, 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
1.0f, 1.0f, -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
|
||||
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f, -0.5f,
|
||||
0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.0f,
|
||||
0.0f, 1.0f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
1.0f, 1.0f, -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
|
||||
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, -0.5f, 0.5f,
|
||||
-0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, -0.5f, -0.5f, -0.5f, -1.0f,
|
||||
0.0f, 0.0f, 0.0f, 1.0f, -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
|
||||
0.0f, 1.0f, -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
|
||||
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.5f, 0.5f,
|
||||
-0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.5f, -0.5f, -0.5f, 1.0f,
|
||||
0.0f, 0.0f, 0.0f, 1.0f, 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 1.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
|
||||
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.5f, -0.5f,
|
||||
-0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.5f, -0.5f, 0.5f, 0.0f,
|
||||
-1.0f, 0.0f, 1.0f, 0.0f, 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||
1.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
|
||||
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.5f, 0.5f,
|
||||
-0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 0.0f,
|
||||
1.0f, 0.0f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||
1.0f, 0.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f};
|
||||
unsigned int VBO, cubeVAO;
|
||||
glGenVertexArrays(1, &cubeVAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
glBindVertexArray(cubeVAO);
|
||||
|
||||
// position attribute
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
|
||||
(void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
|
||||
(void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
|
||||
(void*)(6 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
// second, configure the light's VAO (VBO stays the same; the vertices
|
||||
// are the same for the light object which is also a 3D cube)
|
||||
unsigned int lightCubeVAO;
|
||||
glGenVertexArrays(1, &lightCubeVAO);
|
||||
glBindVertexArray(lightCubeVAO);
|
||||
|
||||
// we only need to bind to the VBO (to link it with glVertexAttribPointer),
|
||||
// no need to fill it; the VBO's data already contains all we need (it's
|
||||
// already bound, but we do it again for educational purposes)
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
|
||||
(void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
unsigned int diffuseMap = loadTexture("../images/container2.png");
|
||||
unsigned int specularMap = loadTexture("../images/container2_specular.png");
|
||||
unsigned int creativesam = loadTexture("../images/matrix.jpg");
|
||||
|
||||
// render loop
|
||||
// -----------
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
// per-frame time logic
|
||||
// --------------------
|
||||
float currentFrame = static_cast<float>(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);
|
||||
|
||||
// glm::vec3 lightColor;
|
||||
// lightColor.x = sin(glfwGetTime() * 2.0f);
|
||||
// lightColor.y = sin(glfwGetTime() * 0.7f);
|
||||
// lightColor.z = sin(glfwGetTime() * 1.3f);
|
||||
glm::vec3 lightColor(1.0f);
|
||||
glm::vec3 diffuseColor = lightColor * glm::vec3(0.5f);
|
||||
glm::vec3 ambientColor = diffuseColor * glm::vec3(0.2f);
|
||||
// be sure to activate shader when setting uniforms/drawing objects
|
||||
lightingShader.use();
|
||||
float t = static_cast<float>(glfwGetTime());
|
||||
glm::vec3 curLightPos = glm::vec3(cos(t), lightPos.y, sin(t));
|
||||
lightingShader.setVec3("light.position", curLightPos);
|
||||
lightingShader.setVec3("light.ambient", ambientColor);
|
||||
lightingShader.setVec3("light.diffuse", diffuseColor);
|
||||
lightingShader.setVec3("light.specular", glm::vec3(0.1f));
|
||||
lightingShader.setVec3("viewPos", camera.Position);
|
||||
lightingShader.setInt("material.diffuse", 0);
|
||||
lightingShader.setInt("material.specular", 1);
|
||||
lightingShader.setInt("material.creativesam", 2);
|
||||
lightingShader.setFloat("material.shininess", 32.0f);
|
||||
lightingShader.setFloat("light.matrix", abs(sin(glfwGetTime())));
|
||||
lightingShader.setFloat("matrixOffset", glfwGetTime());
|
||||
// view/projection transformations
|
||||
glm::mat4 projection = glm::perspective(
|
||||
glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT,
|
||||
0.1f, 100.0f);
|
||||
glm::mat4 view = camera.GetViewMatrix();
|
||||
lightingShader.setMat4("projection", projection);
|
||||
lightingShader.setMat4("view", view);
|
||||
|
||||
// world transformation
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
lightingShader.setMat4("model", model);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, diffuseMap);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, specularMap);
|
||||
glActiveTexture(GL_TEXTURE2);
|
||||
glBindTexture(GL_TEXTURE_2D, creativesam);
|
||||
|
||||
// render the cube
|
||||
glBindVertexArray(cubeVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
|
||||
// also draw the lamp object
|
||||
lightCubeShader.use();
|
||||
lightCubeShader.setMat4("projection", projection);
|
||||
lightCubeShader.setMat4("view", view);
|
||||
lightCubeShader.setVec3("aFragColor", lightColor);
|
||||
model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, curLightPos);
|
||||
model = glm::scale(model, glm::vec3(0.2f)); // a smaller cube
|
||||
lightCubeShader.setMat4("model", model);
|
||||
|
||||
glBindVertexArray(lightCubeVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
|
||||
// glfw: swap buffers and poll IO events (keys pressed/released, mouse
|
||||
// moved etc.)
|
||||
// -------------------------------------------------------------------------------
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
// optional: de-allocate all resources once they've outlived their purpose:
|
||||
// ------------------------------------------------------------------------
|
||||
glDeleteVertexArrays(1, &cubeVAO);
|
||||
glDeleteVertexArrays(1, &lightCubeVAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
|
||||
// glfw: terminate, clearing all previously allocated GLFW resources.
|
||||
// ------------------------------------------------------------------
|
||||
glfwTerminate();
|
||||
return 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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// 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 xposIn, double yposIn) {
|
||||
float xpos = static_cast<float>(xposIn);
|
||||
float ypos = static_cast<float>(yposIn);
|
||||
|
||||
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(static_cast<float>(yoffset));
|
||||
}
|
||||
|
||||
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) {
|
||||
std::cout << "Texture successed to load at path: " << path << std::endl;
|
||||
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;
|
||||
}
|
||||
109
lighting-maps/myshader.hh
Executable file
109
lighting-maps/myshader.hh
Executable file
@@ -0,0 +1,109 @@
|
||||
#pragma once
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <glm/glm.hpp>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <sstream>
|
||||
class Shader {
|
||||
public:
|
||||
// 程序ID
|
||||
unsigned int ID;
|
||||
// 构造器读取并构建着色器
|
||||
Shader(const char* vertexPath, const char* fragmentPath) {
|
||||
std::string vertexCode;
|
||||
std::string fragmentCode;
|
||||
std::ifstream vShaderFile;
|
||||
std::ifstream fShaderFile;
|
||||
|
||||
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
|
||||
try {
|
||||
vShaderFile.open(vertexPath);
|
||||
fShaderFile.open(fragmentPath);
|
||||
std::stringstream vShaderStream, fShaderStream;
|
||||
vShaderStream << vShaderFile.rdbuf();
|
||||
fShaderStream << fShaderFile.rdbuf();
|
||||
vShaderFile.close();
|
||||
fShaderFile.close();
|
||||
vertexCode = vShaderStream.str();
|
||||
fragmentCode = fShaderStream.str();
|
||||
} catch (std::ifstream::failure e) {
|
||||
std::cout << "Error::SHADER::FILE_NOT_SUCCESFULLY_READ"
|
||||
<< std::endl;
|
||||
std::cout << "=====Error Path=====" << std::endl
|
||||
<< vertexPath << std::endl
|
||||
<< fragmentPath << std::endl;
|
||||
}
|
||||
|
||||
const char* vShaderCode = vertexCode.c_str();
|
||||
const char* fShaderCode = fragmentCode.c_str();
|
||||
|
||||
// 编译着色器
|
||||
unsigned int vertex, fragment;
|
||||
int success;
|
||||
char infoLog[512];
|
||||
|
||||
// 顶点着色器
|
||||
vertex = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertex, 1, &vShaderCode, NULL);
|
||||
glCompileShader(vertex);
|
||||
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
|
||||
if (!success) {
|
||||
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n"
|
||||
<< infoLog << std::endl;
|
||||
}
|
||||
|
||||
// 片段着色器
|
||||
fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragment, 1, &fShaderCode, NULL);
|
||||
glCompileShader(fragment);
|
||||
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
|
||||
if (!success) {
|
||||
glGetShaderInfoLog(fragment, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n"
|
||||
<< infoLog << std::endl;
|
||||
}
|
||||
|
||||
ID = glCreateProgram();
|
||||
glAttachShader(ID, vertex);
|
||||
glAttachShader(ID, fragment);
|
||||
glLinkProgram(ID);
|
||||
glGetProgramiv(ID, GL_LINK_STATUS, &success);
|
||||
if (!success) {
|
||||
glGetShaderInfoLog(ID, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n"
|
||||
<< infoLog << std::endl;
|
||||
}
|
||||
|
||||
glDeleteShader(vertex);
|
||||
glDeleteShader(fragment);
|
||||
}
|
||||
// 使用/激活程序
|
||||
void use() { glUseProgram(ID); }
|
||||
// uniform工具函数
|
||||
void setBool(const std::string& name, bool value) const {
|
||||
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
|
||||
}
|
||||
void setInt(const std::string& name, int value) const {
|
||||
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
|
||||
}
|
||||
void setFloat(const std::string& name, float value) const {
|
||||
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
||||
}
|
||||
void setMat4(const std::string& name, glm::mat4 m) {
|
||||
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE,
|
||||
&m[0][0]);
|
||||
}
|
||||
void setVec3(const std::string& name, float x, float y, float z) {
|
||||
glUniform3f(glGetUniformLocation(ID, name.c_str()), x, y, z);
|
||||
}
|
||||
void setVec3(const std::string& name, const glm::vec3 v) {
|
||||
glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1, &v[0]);
|
||||
}
|
||||
};
|
||||
51
lighting-maps/shaders/fscolors.glsl
Normal file
51
lighting-maps/shaders/fscolors.glsl
Normal file
@@ -0,0 +1,51 @@
|
||||
#version 330 core
|
||||
|
||||
struct Material {
|
||||
sampler2D diffuse;
|
||||
sampler2D specular;
|
||||
sampler2D creativesam;
|
||||
float shininess;
|
||||
};
|
||||
|
||||
// 光强控制
|
||||
struct Light {
|
||||
vec3 position;
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
float matrix;
|
||||
};
|
||||
uniform Light light;
|
||||
uniform Material material;
|
||||
out vec4 FragColor;
|
||||
in vec3 FragPos;
|
||||
in vec3 Normal;
|
||||
in vec2 TexCoords;
|
||||
|
||||
uniform float matrixOffset;
|
||||
uniform vec3 viewPos;
|
||||
|
||||
void main() {
|
||||
// ambient
|
||||
vec3 ambient = light.ambient * texture(material.diffuse, TexCoords).rgb;
|
||||
|
||||
// diffuse
|
||||
vec3 norm = normalize(Normal);
|
||||
vec3 lightDir = normalize(light.position - FragPos);
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = light.diffuse * diff * texture(material.diffuse, TexCoords).rgb;
|
||||
|
||||
// specular
|
||||
vec3 viewDir = normalize(viewPos - FragPos);
|
||||
vec3 reflectDir = reflect(-lightDir, norm);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
||||
vec3 specular = light.specular * spec * texture(material.specular, TexCoords).rgb;
|
||||
|
||||
float matrixClamp = clamp(light.matrix, 0.5, 1.0);
|
||||
vec3 creativesam = vec3(matrixClamp) * texture(material.creativesam, vec2(TexCoords.x, TexCoords.y + matrixOffset)).rgb;
|
||||
|
||||
vec3 result = ambient + diffuse + specular + creativesam;
|
||||
// vec3 result = ambient + diffuse + specular;
|
||||
|
||||
FragColor = vec4(result, 1.0);
|
||||
}
|
||||
8
lighting-maps/shaders/fslightCube.glsl
Normal file
8
lighting-maps/shaders/fslightCube.glsl
Normal file
@@ -0,0 +1,8 @@
|
||||
#version 330 core
|
||||
|
||||
uniform vec3 aFragColor;
|
||||
out vec4 FragColor;
|
||||
void main() {
|
||||
FragColor = vec4(aFragColor, 1.0);
|
||||
}
|
||||
|
||||
19
lighting-maps/shaders/vscolors.glsl
Normal file
19
lighting-maps/shaders/vscolors.glsl
Normal file
@@ -0,0 +1,19 @@
|
||||
#version 330 core
|
||||
layout(location = 0) in vec3 aPos;
|
||||
layout(location = 1) in vec3 aNormal;
|
||||
layout(location = 2) in vec2 aTexCoords;
|
||||
|
||||
out vec2 TexCoords;
|
||||
out vec3 FragPos;
|
||||
out vec3 Normal;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main() {
|
||||
TexCoords = aTexCoords;
|
||||
FragPos = vec3(model * vec4(aPos, 1.0));
|
||||
Normal = mat3(transpose(inverse(model))) * aNormal;
|
||||
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||
}
|
||||
10
lighting-maps/shaders/vslightCube.glsl
Normal file
10
lighting-maps/shaders/vslightCube.glsl
Normal file
@@ -0,0 +1,10 @@
|
||||
#version 330 core
|
||||
layout(location = 0) in vec3 aPos;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main() {
|
||||
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||
}
|
||||
7988
lighting-maps/stb_image.h
Executable file
7988
lighting-maps/stb_image.h
Executable file
File diff suppressed because it is too large
Load Diff
3
lighting-maps/stb_image_wrap.cc
Executable file
3
lighting-maps/stb_image_wrap.cc
Executable file
@@ -0,0 +1,3 @@
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
|
||||
6
lighting-maps/stb_image_wrap.h
Executable file
6
lighting-maps/stb_image_wrap.h
Executable file
@@ -0,0 +1,6 @@
|
||||
#ifndef STB_IMAGE_WRAP_H
|
||||
#define STB_IMAGE_WRAP_H
|
||||
|
||||
#include "stb_image.h"
|
||||
|
||||
#endif // STB_IMAGE_WRAP_H
|
||||
Reference in New Issue
Block a user