Commit of work up to day 11 of learnopengl.com.
This commit is contained in:
commit
5ce5b4dda4
447 changed files with 126678 additions and 0 deletions
63
09-basic-light-materials/src/camera.cpp
Normal file
63
09-basic-light-materials/src/camera.cpp
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
#include "camera.hpp"
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
glm::mat4 Camera::lookAt() {
|
||||
return glm::lookAt(pos, pos + front, up);
|
||||
}
|
||||
|
||||
void Camera::forward() {
|
||||
pos += speed * front;
|
||||
}
|
||||
|
||||
void Camera::back() {
|
||||
pos -= speed * front;
|
||||
}
|
||||
|
||||
void Camera::left() {
|
||||
pos -= glm::normalize(glm::cross(front, up)) * speed;
|
||||
}
|
||||
|
||||
void Camera::right() {
|
||||
pos += glm::normalize(glm::cross(front, up)) * speed;
|
||||
}
|
||||
|
||||
void Camera::update(float deltaTime) {
|
||||
speed = 2.5f * deltaTime;
|
||||
}
|
||||
|
||||
void Camera::mouse_move(double xpos, double ypos) {
|
||||
if(firstMouse) {
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
firstMouse = false;
|
||||
}
|
||||
|
||||
float xoffset = xpos - lastX;
|
||||
float yoffset = lastY - ypos;
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
|
||||
const float sensitivity = 0.1f;
|
||||
xoffset *= sensitivity;
|
||||
yoffset *= sensitivity;
|
||||
|
||||
yaw += xoffset;
|
||||
pitch += yoffset;
|
||||
|
||||
if(pitch > 89.0f) pitch = 89.0f;
|
||||
if(pitch < -89.0f) pitch = -89.0f;
|
||||
|
||||
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
direction.y = sin(glm::radians(pitch));
|
||||
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
|
||||
front = glm::normalize(direction);
|
||||
}
|
||||
|
||||
void Camera::mouse_scroll(double xoffset, double yoffset) {
|
||||
fov -= (float)yoffset;
|
||||
|
||||
if(fov < 1.0f) fov = 1.0f;
|
||||
if(fov > 90.0f) fov = 90.0f;
|
||||
}
|
||||
26
09-basic-light-materials/src/camera.hpp
Normal file
26
09-basic-light-materials/src/camera.hpp
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#pragma once
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
struct Camera {
|
||||
glm::vec3 pos = glm::vec3(0.0f, 0.0f, 3.0f);
|
||||
glm::vec3 front = glm::vec3(0.0f, 0.0f, -1.0f);
|
||||
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
|
||||
glm::vec3 direction = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
|
||||
float pitch = 0.0f;
|
||||
float yaw = -90.0f;
|
||||
float speed = 0.05f;
|
||||
float lastX = 400;
|
||||
float lastY = 300;
|
||||
float fov = 45.0f;
|
||||
bool firstMouse = true;
|
||||
|
||||
glm::mat4 lookAt();
|
||||
void forward();
|
||||
void back();
|
||||
void left();
|
||||
void right();
|
||||
void update(float deltaTime);
|
||||
void mouse_move(double xpos, double ypos);
|
||||
void mouse_scroll(double xoffset, double yoffset);
|
||||
};
|
||||
46
09-basic-light-materials/src/data.hpp
Normal file
46
09-basic-light-materials/src/data.hpp
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#pragma once
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
float vertices[] = {
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
|
||||
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
|
||||
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
|
||||
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
|
||||
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f
|
||||
};
|
||||
47
09-basic-light-materials/src/dbc.cpp
Normal file
47
09-basic-light-materials/src/dbc.cpp
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#include "dbc.hpp"
|
||||
#include <iostream>
|
||||
|
||||
void dbc::log(const string &message, const std::source_location location) {
|
||||
std::cout << '[' << location.file_name() << ':'
|
||||
<< location.line() << "|"
|
||||
<< location.function_name() << "] "
|
||||
<< message << std::endl;
|
||||
}
|
||||
|
||||
void dbc::sentinel(const string &message, const std::source_location location) {
|
||||
string err = $F("[SENTINEL!] {}", message);
|
||||
dbc::log(err, location);
|
||||
throw dbc::SentinelError(err);
|
||||
}
|
||||
|
||||
void dbc::pre(const string &message, bool test, const std::source_location location) {
|
||||
if(!test) {
|
||||
string err = $F("[PRE!] {}", message);
|
||||
dbc::log(err, location);
|
||||
throw dbc::PreCondError(err);
|
||||
}
|
||||
}
|
||||
|
||||
void dbc::pre(const string &message, std::function<bool()> tester, const std::source_location location) {
|
||||
dbc::pre(message, tester(), location);
|
||||
}
|
||||
|
||||
void dbc::post(const string &message, bool test, const std::source_location location) {
|
||||
if(!test) {
|
||||
string err = $F("[POST!] {}", message);
|
||||
dbc::log(err, location);
|
||||
throw dbc::PostCondError(err);
|
||||
}
|
||||
}
|
||||
|
||||
void dbc::post(const string &message, std::function<bool()> tester, const std::source_location location) {
|
||||
dbc::post(message, tester(), location);
|
||||
}
|
||||
|
||||
void dbc::check(bool test, const string &message, const std::source_location location) {
|
||||
if(!test) {
|
||||
string err = $F("[CHECK!] {}\n", message);
|
||||
dbc::log(err, location);
|
||||
throw dbc::CheckError(err);
|
||||
}
|
||||
}
|
||||
46
09-basic-light-materials/src/dbc.hpp
Normal file
46
09-basic-light-materials/src/dbc.hpp
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <fmt/core.h>
|
||||
#include <functional>
|
||||
#include <source_location>
|
||||
|
||||
// AKA the Fuckit macro
|
||||
#define $F(FMT, ...) fmt::format(FMT, ##__VA_ARGS__)
|
||||
|
||||
namespace dbc {
|
||||
using std::string;
|
||||
|
||||
using CheckError = std::runtime_error;
|
||||
using SentinelError = std::runtime_error;
|
||||
using PreCondError = std::runtime_error;
|
||||
using PostCondError = std::runtime_error;
|
||||
|
||||
void log(const string &message,
|
||||
const std::source_location location =
|
||||
std::source_location::current());
|
||||
|
||||
[[noreturn]] void sentinel(const string &message,
|
||||
const std::source_location location =
|
||||
std::source_location::current());
|
||||
|
||||
void pre(const string &message, bool test,
|
||||
const std::source_location location =
|
||||
std::source_location::current());
|
||||
|
||||
void pre(const string &message, std::function<bool()> tester,
|
||||
const std::source_location location =
|
||||
std::source_location::current());
|
||||
|
||||
void post(const string &message, bool test,
|
||||
const std::source_location location =
|
||||
std::source_location::current());
|
||||
|
||||
void post(const string &message, std::function<bool()> tester,
|
||||
const std::source_location location =
|
||||
std::source_location::current());
|
||||
|
||||
void check(bool test, const string &message,
|
||||
const std::source_location location =
|
||||
std::source_location::current());
|
||||
}
|
||||
319
09-basic-light-materials/src/main.cpp
Normal file
319
09-basic-light-materials/src/main.cpp
Normal file
|
|
@ -0,0 +1,319 @@
|
|||
#define _USE_MATH_DEFINES
|
||||
#include <math.h>
|
||||
#include "dbc.hpp"
|
||||
#include <print>
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include "shader.hpp"
|
||||
#include <stb_image.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include "data.hpp"
|
||||
#include "physics.hpp"
|
||||
#include "model.hpp"
|
||||
#include "camera.hpp"
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
||||
void processInput(GLFWwindow *window);
|
||||
|
||||
const unsigned int SCR_WIDTH = 800;
|
||||
const unsigned int SCR_HEIGHT = 600;
|
||||
|
||||
struct Config {
|
||||
Shader shader;
|
||||
Shader light;
|
||||
Model model;
|
||||
glm::vec3 lightPos{1.2f, 1.0f, 2.0f};
|
||||
glm::vec3 boxPos{2.0f, 0.0f, 0.0f};
|
||||
unsigned int cubeVAO = 0;
|
||||
unsigned int lightCubeVAO = 0;
|
||||
unsigned int VBO = 0;
|
||||
Camera camera{};
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
|
||||
void updateDelta() {
|
||||
float currentFrame = glfwGetTime();
|
||||
deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
}
|
||||
};
|
||||
|
||||
void init_glfw() {
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
}
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow *, int width, int height) {
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void mouse_callback(GLFWwindow *window, double xpos, double ypos) {
|
||||
Camera* camera = static_cast<Camera*>(glfwGetWindowUserPointer(window));
|
||||
camera->mouse_move(xpos, ypos);
|
||||
}
|
||||
|
||||
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) {
|
||||
Camera* camera = static_cast<Camera*>(glfwGetWindowUserPointer(window));
|
||||
camera->mouse_scroll(xoffset, yoffset);
|
||||
}
|
||||
|
||||
GLFWwindow* create_window() {
|
||||
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
|
||||
dbc::check(window != NULL, "failed to open window");
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
|
||||
auto good = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
|
||||
dbc::check(good, "failed to load GLAD");
|
||||
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
|
||||
return window;
|
||||
}
|
||||
|
||||
|
||||
void processInput(GLFWwindow *window, BoxTest &box, Camera& camera) {
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
|
||||
b2Vec2 force(0.0f, 0.5f);
|
||||
for(size_t i = 0; i < BODY_COUNT; i++) {
|
||||
box.bodies[i]->ApplyForceToCenter(force, true);
|
||||
box.bodies[i]->ApplyTorque(0.1f, true);
|
||||
}
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
||||
camera.forward();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
||||
camera.back();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
|
||||
camera.left();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
|
||||
camera.right();
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int load_texture(const std::string& image_file) {
|
||||
unsigned int texture_id;
|
||||
glGenTextures(1, &texture_id);
|
||||
glBindTexture(GL_TEXTURE_2D, texture_id);
|
||||
|
||||
// set the texture wrapping parameters
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
|
||||
// set texture filtering parameters
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
int nrChannels = 0;
|
||||
stbi_set_flip_vertically_on_load(true);
|
||||
|
||||
unsigned char *data = stbi_load(image_file.c_str(), &width, &height, &nrChannels, 0);
|
||||
|
||||
dbc::check(data != nullptr, std::format("Failed to load texture: {}", image_file));
|
||||
|
||||
dbc::check(nrChannels == 3 || nrChannels == 4,
|
||||
std::format("Image {} has invalid channels {} should be 3 or 4.", image_file, nrChannels));
|
||||
|
||||
auto rgb_or_a = nrChannels == 3 ? GL_RGB : GL_RGBA;
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, rgb_or_a, width, height, 0, rgb_or_a, GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
stbi_image_free(data);
|
||||
|
||||
return texture_id;
|
||||
}
|
||||
|
||||
Config setup() {
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
Config config{
|
||||
.shader={"shaders/08-vert.glsl", "shaders/08-frag.glsl"},
|
||||
.light={"shaders/08-vert.glsl", "shaders/08-lightsource.frag.glsl"},
|
||||
.model={"assets/popcorn_model_a.glb"}
|
||||
};
|
||||
|
||||
unsigned int VBO = 0;
|
||||
unsigned int lightCubeVAO = 0;
|
||||
unsigned int cubeVAO = 0;
|
||||
|
||||
// cube vao configure, this is the light target
|
||||
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, 6 * sizeof(float), (void *)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
// light vao configure, this is the light source
|
||||
glGenVertexArrays(1, &lightCubeVAO);
|
||||
glBindVertexArray(lightCubeVAO);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
config.lightCubeVAO = lightCubeVAO;
|
||||
config.cubeVAO = cubeVAO;
|
||||
config.VBO = VBO;
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
void draw_popcorn(Config& config, BoxTest& box, size_t box_i, glm::mat4& projection, glm::mat4& view)
|
||||
{
|
||||
config.shader.use();
|
||||
config.shader.setVec3("objectColor", 1.0f, 0.5f, 0.31f);
|
||||
config.shader.setVec3("lightColor", 1.0f, 1.0f, 1.0f);
|
||||
// THIS WAS MISSING
|
||||
config.shader.setVec3("lightPos", config.lightPos);
|
||||
|
||||
config.shader.setMat4("view", view);
|
||||
config.shader.setMat4("projection", projection);
|
||||
config.shader.setVec3("viewPos", config.camera.pos);
|
||||
|
||||
float time = glfwGetTime();
|
||||
// get position from box
|
||||
b2Vec2 position = box.bodies[box_i]->GetPosition();
|
||||
float angle = box.bodies[box_i]->GetAngle();
|
||||
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, glm::vec3(position.x, position.y, 0.0f));
|
||||
|
||||
model = glm::rotate(model, angle / 5.0f, glm::vec3(1.0f, 0.0f, 0.0f));
|
||||
model = glm::rotate(model, glm::radians(time * 10.0f), glm::vec3(0.0f, 1.0f, 1.0f));
|
||||
|
||||
config.shader.setMat4("model", model);
|
||||
|
||||
config.model.Draw(config.shader);
|
||||
}
|
||||
|
||||
void draw_cube(Config& config, glm::mat4& projection, glm::mat4& view) {
|
||||
config.shader.use();
|
||||
config.shader.setVec3("objectColor", 1.0f, 0.5f, 0.31f);
|
||||
config.shader.setVec3("lightColor", 1.0f, 1.0f, 1.0f);
|
||||
// THIS WAS MISSING
|
||||
config.shader.setVec3("lightPos", config.lightPos);
|
||||
|
||||
config.shader.setMat4("view", view);
|
||||
config.shader.setMat4("projection", projection);
|
||||
config.shader.setVec3("viewPos", config.camera.pos);
|
||||
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, config.boxPos);
|
||||
config.shader.setMat4("model", model);
|
||||
glBindVertexArray(config.cubeVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
}
|
||||
|
||||
void draw_light(Config& config, glm::mat4& projection, glm::mat4& view) {
|
||||
config.light.use();
|
||||
|
||||
config.light.setVec3("lightPos", config.lightPos);
|
||||
config.light.setMat4("projection", projection);
|
||||
config.light.setMat4("view", view);
|
||||
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, config.lightPos);
|
||||
model = glm::scale(model, glm::vec3(0.2f));
|
||||
config.light.setMat4("model", model);
|
||||
|
||||
glBindVertexArray(config.lightCubeVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
}
|
||||
|
||||
|
||||
void render(GLFWwindow* window, Config& config, BoxTest& box, b2World& world) {
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
config.shader.use();
|
||||
|
||||
Box2d_step_world(world);
|
||||
|
||||
// time is used for fake 3d rotation
|
||||
float time = glfwGetTime();
|
||||
|
||||
config.camera.update(config.deltaTime);
|
||||
|
||||
glm::mat4 view = config.camera.lookAt();
|
||||
glm::mat4 projection = glm::mat4(1.0f);
|
||||
// fov, aspect, near plane, far plane
|
||||
projection = glm::perspective(glm::radians(config.camera.fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||
|
||||
// update light color
|
||||
draw_cube(config, projection, view);
|
||||
|
||||
// the 0 is for which popcorn, so later when we have N for-loop
|
||||
draw_popcorn(config, box, 0, projection, view);
|
||||
|
||||
draw_light(config, projection, view);
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
void cleanup(Config& config) {
|
||||
glDeleteVertexArrays(1, &config.lightCubeVAO);
|
||||
glDeleteBuffers(1, &config.lightCubeVAO);
|
||||
glDeleteBuffers(1, &config.VBO);
|
||||
config.shader.cleanup();
|
||||
}
|
||||
|
||||
int main() {
|
||||
init_glfw();
|
||||
|
||||
b2Vec2 gravity(0.0f, -10.0f);
|
||||
b2World world(gravity);
|
||||
BoxTest box = Box2d_setup(world);
|
||||
|
||||
auto window = create_window();
|
||||
auto config = setup();
|
||||
|
||||
glfwSetWindowUserPointer(window, &config.camera);
|
||||
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
processInput(window, box, config.camera);
|
||||
render(window, config, box, world);
|
||||
config.updateDelta();
|
||||
}
|
||||
|
||||
cleanup(config);
|
||||
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
57
09-basic-light-materials/src/mesh.cpp
Normal file
57
09-basic-light-materials/src/mesh.cpp
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
#include "mesh.hpp"
|
||||
|
||||
void Mesh::Draw(Shader &shader) {
|
||||
unsigned int diffuseNr = 1;
|
||||
unsigned int specularNr = 1;
|
||||
unsigned int normalNr = 1;
|
||||
unsigned int heightNr = 1;
|
||||
|
||||
for(unsigned int i = 0; i < textures.size(); i++) {
|
||||
glActiveTexture(GL_TEXTURE0 + i);
|
||||
std::string number;
|
||||
std::string name = textures[i].type;
|
||||
|
||||
if(name == "texture_diffuse") {
|
||||
number = std::to_string(diffuseNr++);
|
||||
} else if(name == "texture_specular") {
|
||||
number = std::to_string(specularNr++);
|
||||
} else if(name == "texture_normal") {
|
||||
number = std::to_string(normalNr++);
|
||||
} else if(name == "texture_height") {
|
||||
number = std::to_string(heightNr++);
|
||||
}
|
||||
|
||||
// TODO: Get this uniform ID once and cache it?
|
||||
glUniform1i(glGetUniformLocation(shader.ID, (name + number).c_str()), i);
|
||||
glBindTexture(GL_TEXTURE_2D, textures[i].id);
|
||||
}
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(indices.size()), GL_UNSIGNED_INT, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
}
|
||||
|
||||
|
||||
void Mesh::setupMesh() {
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
glGenBuffers(1, &EBO);
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Position));
|
||||
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));
|
||||
|
||||
glEnableVertexAttribArray(2);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
|
||||
}
|
||||
42
09-basic-light-materials/src/mesh.hpp
Normal file
42
09-basic-light-materials/src/mesh.hpp
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#pragma once
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
#include "shader.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
struct Vertex {
|
||||
glm::vec3 Position;
|
||||
glm::vec3 Normal;
|
||||
glm::vec2 TexCoords;
|
||||
};
|
||||
|
||||
struct Texture {
|
||||
unsigned int id;
|
||||
std::string type;
|
||||
std::string path;
|
||||
};
|
||||
|
||||
struct Mesh {
|
||||
std::vector<Vertex> vertices;
|
||||
std::vector<unsigned int> indices;
|
||||
std::vector<Texture> textures;
|
||||
unsigned int VAO;
|
||||
unsigned int VBO;
|
||||
unsigned int EBO;
|
||||
|
||||
Mesh(std::vector<Vertex> vertices, std::vector<unsigned int> indices, std::vector<Texture> textures) :
|
||||
vertices(vertices),
|
||||
indices(indices),
|
||||
textures(textures)
|
||||
{
|
||||
setupMesh();
|
||||
}
|
||||
|
||||
void Draw(Shader &shader);
|
||||
void setupMesh();
|
||||
};
|
||||
171
09-basic-light-materials/src/model.cpp
Normal file
171
09-basic-light-materials/src/model.cpp
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
#include "model.hpp"
|
||||
#include "dbc.hpp"
|
||||
|
||||
unsigned int TextureFromFile(const std::string& path, const std::string& directory, bool gamma)
|
||||
{
|
||||
std::string filename = path + "/" + filename;
|
||||
|
||||
unsigned int textureID;
|
||||
glGenTextures(1, &textureID);
|
||||
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
int nrComponents = 0;
|
||||
|
||||
unsigned char *data = stbi_load(filename.c_str(), &width, &height, &nrComponents, 0);
|
||||
dbc::check(data != nullptr, $F("Failed to load texture {}", filename));
|
||||
|
||||
GLenum format = GL_RGB;
|
||||
|
||||
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);
|
||||
|
||||
return textureID;
|
||||
}
|
||||
|
||||
void Model::Draw(Shader &shader) {
|
||||
for(unsigned int i = 0; i < meshes.size(); i++) {
|
||||
meshes[i].Draw(shader);
|
||||
}
|
||||
}
|
||||
|
||||
void Model::loadModel(std::string path) {
|
||||
Assimp::Importer importer;
|
||||
const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs | aiProcess_CalcTangentSpace);
|
||||
|
||||
dbc::check(scene != nullptr, "Assimp ReadFile return null");
|
||||
dbc::check(!(scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE), "Assimp says incomplete.");
|
||||
dbc::check(scene->mRootNode != nullptr, "Assimp loaded scene doesn't have a root node");
|
||||
|
||||
directory = path.substr(0, path.find_last_of('/'));
|
||||
|
||||
processNode(scene->mRootNode, scene);
|
||||
}
|
||||
|
||||
void Model::processNode(aiNode *node, const aiScene *scene) {
|
||||
for(unsigned int i = 0; i < node->mNumMeshes; i++) {
|
||||
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
|
||||
meshes.push_back(processMesh(mesh, scene));
|
||||
}
|
||||
|
||||
for(unsigned int i = 0; i < node->mNumChildren; i++) {
|
||||
processNode(node->mChildren[i], scene);
|
||||
}
|
||||
}
|
||||
|
||||
Mesh Model::processMesh(aiMesh *mesh, const aiScene *scene) {
|
||||
std::vector<Vertex> vertices;
|
||||
std::vector<unsigned int> indices;
|
||||
std::vector<Texture> textures;
|
||||
|
||||
for(unsigned int i = 0; i < mesh->mNumVertices; i++) {
|
||||
Vertex vertex;
|
||||
vertex.Position = glm::vec3(
|
||||
mesh->mVertices[i].x,
|
||||
mesh->mVertices[i].y,
|
||||
mesh->mVertices[i].z);
|
||||
|
||||
if(mesh->HasNormals()) {
|
||||
vertex.Normal = glm::vec3(
|
||||
mesh->mNormals[i].x,
|
||||
mesh->mNormals[i].y,
|
||||
mesh->mNormals[i].z);
|
||||
}
|
||||
|
||||
if(mesh->mTextureCoords[0]) {
|
||||
vertex.TexCoords = glm::vec2(
|
||||
mesh->mTextureCoords[0][i].x,
|
||||
mesh->mTextureCoords[0][i].y
|
||||
);
|
||||
} else {
|
||||
vertex.TexCoords = glm::vec2(0.0f, 0.0f);
|
||||
}
|
||||
|
||||
vertices.push_back(vertex);
|
||||
}
|
||||
|
||||
for(unsigned int i = 0; i < mesh->mNumFaces; i++) {
|
||||
aiFace face = mesh->mFaces[i];
|
||||
|
||||
for(unsigned int j = 0; j < face.mNumIndices; j++) {
|
||||
indices.push_back(face.mIndices[j]);
|
||||
}
|
||||
}
|
||||
|
||||
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
|
||||
|
||||
// we assume a convention for sampler names in the shaders. Each diffuse texture should be named
|
||||
// as 'texture_diffuseN' where N is a sequential number ranging from 1 to MAX_SAMPLER_NUMBER.
|
||||
// Same applies to other texture as the following list summarizes:
|
||||
// diffuse: texture_diffuseN
|
||||
// specular: texture_specularN
|
||||
// normal: texture_normalN
|
||||
|
||||
// 1. diffuse maps
|
||||
std::vector<Texture> diffuseMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE, "texture_diffuse");
|
||||
textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
|
||||
|
||||
// 2. specular maps
|
||||
std::vector<Texture> specularMaps = loadMaterialTextures(material, aiTextureType_SPECULAR, "texture_specular");
|
||||
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
|
||||
|
||||
// 3. normal maps
|
||||
std::vector<Texture> normalMaps = loadMaterialTextures(material, aiTextureType_HEIGHT, "texture_normal");
|
||||
textures.insert(textures.end(), normalMaps.begin(), normalMaps.end());
|
||||
|
||||
// 4. height maps
|
||||
std::vector<Texture> heightMaps = loadMaterialTextures(material, aiTextureType_AMBIENT, "texture_height");
|
||||
textures.insert(textures.end(), heightMaps.begin(), heightMaps.end());
|
||||
|
||||
return Mesh(vertices, indices, textures);
|
||||
}
|
||||
|
||||
std::vector<Texture> Model::loadMaterialTextures(aiMaterial *mat, aiTextureType type, std::string typeName)
|
||||
{
|
||||
fmt::println("TEXTURE COUNT: {}, {}, {}", (int)type, typeName, mat->GetTextureCount(type));
|
||||
|
||||
std::vector<Texture> textures;
|
||||
for(unsigned int i = 0; i < mat->GetTextureCount(type); i++) {
|
||||
aiString str;
|
||||
mat->GetTexture(type, i, &str);
|
||||
std::string tx_str{str.C_Str()};
|
||||
|
||||
bool skip = false;
|
||||
for(unsigned int j = 0; j < textures_loaded.size(); j++) {
|
||||
if(textures_loaded[j].path == tx_str) {
|
||||
textures.push_back(textures_loaded[j]);
|
||||
skip = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!skip) {
|
||||
Texture texture{
|
||||
.id = TextureFromFile(tx_str, directory),
|
||||
.type = typeName,
|
||||
.path = tx_str,
|
||||
};
|
||||
|
||||
textures.push_back(texture);
|
||||
textures_loaded.push_back(texture);
|
||||
}
|
||||
}
|
||||
|
||||
return textures;
|
||||
}
|
||||
40
09-basic-light-materials/src/model.hpp
Normal file
40
09-basic-light-materials/src/model.hpp
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#pragma once
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <stb_image.h>
|
||||
#include <assimp/Importer.hpp>
|
||||
#include <assimp/scene.h>
|
||||
#include <assimp/postprocess.h>
|
||||
|
||||
#include <mesh.hpp>
|
||||
#include <shader.hpp>
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
unsigned int TextureFromFile(const std::string& path, const std::string& directory, bool gamma = false);
|
||||
|
||||
struct Model {
|
||||
std::vector<Texture> textures_loaded;
|
||||
std::vector<Mesh> meshes;
|
||||
std::string directory;
|
||||
bool gammaCorrection;
|
||||
|
||||
Model(const char *path) {
|
||||
loadModel(path);
|
||||
}
|
||||
|
||||
void Draw(Shader &shader);
|
||||
|
||||
void loadModel(std::string path);
|
||||
void processNode(aiNode *node, const aiScene *scene);
|
||||
Mesh processMesh(aiMesh *mesh, const aiScene *scene);
|
||||
|
||||
std::vector<Texture> loadMaterialTextures(aiMaterial *mat, aiTextureType type, std::string typeName);
|
||||
};
|
||||
49
09-basic-light-materials/src/physics.cpp
Normal file
49
09-basic-light-materials/src/physics.cpp
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#include "physics.hpp"
|
||||
|
||||
struct BoxTest Box2d_setup(b2World &world) {
|
||||
BoxTest box;
|
||||
float wall_x[4] = {0.0f, 0.0f, -0.99f, 0.99f};
|
||||
float wall_y[4] = {-0.99, 0.99f, 0.0f, 0.0f};
|
||||
float bound_w[4] = {1.0f, 1.0f, 0.1f, 0.1f};
|
||||
float bound_h[4] = {0.1f, 0.1f, 1.0f, 1.0f};
|
||||
|
||||
for(size_t i = 0; i < 4; i++) {
|
||||
b2BodyDef groundBodyDef;
|
||||
groundBodyDef.position.Set(wall_x[i], wall_y[i]);
|
||||
b2Body *groundBody = world.CreateBody(&groundBodyDef);
|
||||
|
||||
b2PolygonShape groundBox;
|
||||
groundBox.SetAsBox(bound_w[i], bound_h[i]);
|
||||
groundBody->CreateFixture(&groundBox, 1.0f);
|
||||
|
||||
box.walls[i] = groundBody;
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < BODY_COUNT; i++) {
|
||||
b2BodyDef bodyDef;
|
||||
bodyDef.type = b2_dynamicBody;
|
||||
bodyDef.position.Set(0.0f, 0.5f);
|
||||
box.bodies[i] = world.CreateBody(&bodyDef);
|
||||
|
||||
b2PolygonShape dynamicBox;
|
||||
dynamicBox.SetAsBox(0.1f, 0.1f);
|
||||
b2FixtureDef fixtureDef;
|
||||
fixtureDef.shape = &dynamicBox;
|
||||
|
||||
fixtureDef.density = 1.0f;
|
||||
fixtureDef.friction = 0.3f;
|
||||
|
||||
box.bodies[i]->CreateFixture(&fixtureDef);
|
||||
}
|
||||
|
||||
return box;
|
||||
}
|
||||
|
||||
void Box2d_step_world(b2World& world) {
|
||||
float timeStep = 1.0f / 60.0f;
|
||||
int velocityIterations = 6;
|
||||
int positionIterations = 2;
|
||||
|
||||
// step the world
|
||||
world.Step(timeStep, velocityIterations, positionIterations);
|
||||
}
|
||||
13
09-basic-light-materials/src/physics.hpp
Normal file
13
09-basic-light-materials/src/physics.hpp
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#pragma once
|
||||
|
||||
#include <box2d/box2d.h>
|
||||
|
||||
#define BODY_COUNT 1
|
||||
|
||||
struct BoxTest {
|
||||
b2Body *walls[4];
|
||||
b2Body *bodies[BODY_COUNT];
|
||||
};
|
||||
|
||||
struct BoxTest Box2d_setup(b2World &world);
|
||||
void Box2d_step_world(b2World& world);
|
||||
126
09-basic-light-materials/src/shader.cpp
Normal file
126
09-basic-light-materials/src/shader.cpp
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
#include "shader.hpp"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <print>
|
||||
#include <filesystem>
|
||||
#include "dbc.hpp"
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
inline std::string read_file(const std::string& filename) {
|
||||
// load the file
|
||||
std::ifstream in_file{filename, std::ios::binary};
|
||||
|
||||
// get the size of the file
|
||||
std::stringstream in_str;
|
||||
in_str << in_file.rdbuf();
|
||||
return in_str.str();
|
||||
}
|
||||
|
||||
void check_error(const std::string& what, unsigned int thing, GLenum check_type) {
|
||||
int success = 0;
|
||||
char infoLog[512] = {0};
|
||||
|
||||
if(check_type == GL_LINK_STATUS) {
|
||||
glGetProgramiv(thing, check_type, &success);
|
||||
|
||||
if(!success) {
|
||||
glGetProgramInfoLog(thing, 512, NULL, infoLog);
|
||||
dbc::sentinel(std::format("ERROR: Program {} compile failed: {}", what, infoLog));
|
||||
}
|
||||
} else {
|
||||
glGetShaderiv(thing, check_type, &success);
|
||||
|
||||
if(!success) {
|
||||
glGetShaderInfoLog(thing, 512, NULL, infoLog);
|
||||
dbc::sentinel(std::format("ERROR: Shader {} compile failed: {}", what, infoLog));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int Shader::load_shader(const std::string& filename, GLenum shader_type) {
|
||||
dbc::check(fs::exists(filename),
|
||||
std::format("shader file {} does not exist", filename));
|
||||
|
||||
// create the shader
|
||||
std::string shader_code = read_file(filename);
|
||||
const char* shader_code_ptr = shader_code.c_str();
|
||||
|
||||
int shader_id = glCreateShader(shader_type);
|
||||
glShaderSource(shader_id, 1, &shader_code_ptr, NULL);
|
||||
glCompileShader(shader_id);
|
||||
|
||||
check_error(filename, shader_id, GL_COMPILE_STATUS);
|
||||
|
||||
// check compile error
|
||||
return shader_id;
|
||||
}
|
||||
|
||||
Shader::Shader(const char* vertexPath, const char* fragmentPath) {
|
||||
unsigned int vertex = load_shader(vertexPath, GL_VERTEX_SHADER);
|
||||
unsigned int fragment = load_shader(fragmentPath, GL_FRAGMENT_SHADER);
|
||||
|
||||
ID = glCreateProgram();
|
||||
glAttachShader(ID, vertex);
|
||||
glAttachShader(ID, fragment);
|
||||
glLinkProgram(ID);
|
||||
|
||||
check_error("link", ID, GL_LINK_STATUS);
|
||||
|
||||
glDeleteShader(vertex);
|
||||
glDeleteShader(fragment);
|
||||
}
|
||||
|
||||
void Shader::use() const {
|
||||
glUseProgram(ID);
|
||||
}
|
||||
|
||||
void Shader::cleanup() {
|
||||
glDeleteProgram(ID);
|
||||
}
|
||||
|
||||
void Shader::setBool(const std::string &name, bool value) const {
|
||||
auto uniform = glGetUniformLocation(ID, name.c_str());
|
||||
glUniform1i(uniform, (int)value);
|
||||
}
|
||||
|
||||
void Shader::setInt(const std::string &name, int value) const {
|
||||
auto uniform = glGetUniformLocation(ID, name.c_str());
|
||||
glUniform1i(uniform, value);
|
||||
}
|
||||
|
||||
void Shader::setFloat(const std::string &name, float value) const {
|
||||
auto uniform = glGetUniformLocation(ID, name.c_str());
|
||||
glUniform1f(uniform, value);
|
||||
}
|
||||
|
||||
void Shader::setVec4(const std::string &name, float v1, float v2, float v3, float v4) const {
|
||||
auto uniform = glGetUniformLocation(ID, name.c_str());
|
||||
glUniform4f(uniform, v1, v2, v3, v4);
|
||||
}
|
||||
|
||||
void Shader::setVec4(const std::string &name, const glm::vec4& value) const
|
||||
{
|
||||
auto uniform = glGetUniformLocation(ID, name.c_str());
|
||||
glUniform4fv(uniform, 1, &value[0]);
|
||||
}
|
||||
|
||||
void Shader::setVec3(const std::string &name, const glm::vec3& value) const
|
||||
{
|
||||
auto uniform = glGetUniformLocation(ID, name.c_str());
|
||||
glUniform3fv(uniform, 1, &value[0]);
|
||||
}
|
||||
|
||||
void Shader::setVec3(const std::string &name, float v1, float v2, float v3) const {
|
||||
auto uniform = glGetUniformLocation(ID, name.c_str());
|
||||
glUniform3f(uniform, v1, v2, v3);
|
||||
}
|
||||
|
||||
void Shader::setMat4(const std::string &name, const glm::mat4& mat) const {
|
||||
unsigned int loc = glGetUniformLocation(ID, name.c_str());
|
||||
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(mat));
|
||||
}
|
||||
25
09-basic-light-materials/src/shader.hpp
Normal file
25
09-basic-light-materials/src/shader.hpp
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#pragma once
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <string>
|
||||
#include <climits>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
unsigned int ID = UINT_MAX;
|
||||
|
||||
Shader(const char* vertexPath, const char* fragmentPath);
|
||||
unsigned int load_shader(const std::string& filename, GLenum shader_type);
|
||||
void use() const;
|
||||
void setBool(const std::string &name, bool value) const;
|
||||
void setInt(const std::string &name, int value) const;
|
||||
void setFloat(const std::string &name, float value) const;
|
||||
void setVec4(const std::string &name, float v1, float v2, float v3, float v4) const;
|
||||
void setVec4(const std::string &name, const glm::vec4& value) const;
|
||||
void setVec3(const std::string &name, float v1, float v2, float v3) const;
|
||||
void setVec3(const std::string &name, const glm::vec3& value) const;
|
||||
void setMat4(const std::string &name, const glm::mat4& what) const;
|
||||
void cleanup();
|
||||
};
|
||||
2
09-basic-light-materials/src/stb_image.cpp
Normal file
2
09-basic-light-materials/src/stb_image.cpp
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
7988
09-basic-light-materials/src/stb_image.h
Normal file
7988
09-basic-light-materials/src/stb_image.h
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue