Nove repo opengl-learning
This commit is contained in:
33
lessons/1. Getting started/1. Creating a window/Makefile
Normal file
33
lessons/1. Getting started/1. Creating a window/Makefile
Normal file
@@ -0,0 +1,33 @@
|
||||
NAME := main
|
||||
CXX=g++
|
||||
DEBUG :=
|
||||
CXX_FLAGS := $(DEBUG) -std=c++14
|
||||
LIBS := -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -pthread -lXi -ldl -lm
|
||||
INCLUDE := ../../../include/
|
||||
BIN := bin
|
||||
SRC := src
|
||||
INPUT = src/main.cpp ../../../glad.c
|
||||
|
||||
all: $(BIN)/$(NAME)
|
||||
|
||||
run:
|
||||
./$(BIN)/$(NAME)
|
||||
|
||||
$(BIN)/$(NAME): $(SRC)/main.o $(SRC)/glad.o
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) $^ -o $@ $(LIBS)
|
||||
|
||||
.cpp.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
.c.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/glad.o: ../../../glad.c
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
# dependencies
|
||||
$(SRC)/main.o: $(SRC)/main.cpp
|
||||
|
||||
clean:
|
||||
rm -rf $(SRC)/*.o
|
||||
touch $(SRC)/*
|
||||
@@ -0,0 +1,8 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
int main(int *argc, char **argv)
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
||||
33
lessons/1. Getting started/2. Hello window/Makefile
Normal file
33
lessons/1. Getting started/2. Hello window/Makefile
Normal file
@@ -0,0 +1,33 @@
|
||||
NAME := main
|
||||
CXX=g++
|
||||
DEBUG :=
|
||||
CXX_FLAGS := $(DEBUG) -std=c++14
|
||||
LIBS := -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -pthread -lXi -ldl -lm
|
||||
INCLUDE := ../../../include/
|
||||
BIN := bin
|
||||
SRC := src
|
||||
INPUT = src/main.cpp ../../../glad.c
|
||||
|
||||
all: $(BIN)/$(NAME)
|
||||
|
||||
run:
|
||||
./$(BIN)/$(NAME)
|
||||
|
||||
$(BIN)/$(NAME): $(SRC)/main.o $(SRC)/glad.o
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) $^ -o $@ $(LIBS)
|
||||
|
||||
.cpp.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
.c.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/glad.o: ../../../glad.c
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
# dependencies
|
||||
$(SRC)/main.o: $(SRC)/main.cpp
|
||||
|
||||
clean:
|
||||
rm -rf $(SRC)/*.o
|
||||
touch $(SRC)/*
|
||||
75
lessons/1. Getting started/2. Hello window/src/main.cpp
Normal file
75
lessons/1. Getting started/2. Hello window/src/main.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Global Constants
|
||||
const int WINDOW_WIDTH = 800;
|
||||
const int WINDOW_HEIGHT = 600;
|
||||
// Globals
|
||||
GLFWwindow* window;
|
||||
|
||||
uint8_t init(void);
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height);
|
||||
void freeAll(void);
|
||||
void proccessInput(GLFWwindow* window);
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (init() != 0)
|
||||
return -1;
|
||||
|
||||
// MAIN loop
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
// input ...
|
||||
proccessInput(window);
|
||||
|
||||
// rendering commands here ...
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
// check and calls events and swap the buffers
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
freeAll();
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t init(void) {
|
||||
// init of glfw window
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Hello Window!", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
cerr << "Failed to create glfw window" << endl;
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
// init of GLAD
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
cerr << "Failed to initialize GLAD" << endl;
|
||||
}
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffersize_callback);
|
||||
return 0;
|
||||
}
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
void freeAll(void)
|
||||
{
|
||||
glfwTerminate();
|
||||
}
|
||||
void proccessInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
}
|
||||
33
lessons/1. Getting started/3. Hello Triangle/Makefile
Normal file
33
lessons/1. Getting started/3. Hello Triangle/Makefile
Normal file
@@ -0,0 +1,33 @@
|
||||
NAME := main
|
||||
CXX=g++
|
||||
DEBUG :=
|
||||
CXX_FLAGS := $(DEBUG) -std=c++14
|
||||
LIBS := -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -pthread -lXi -ldl -lm
|
||||
INCLUDE := ../../../include/
|
||||
BIN := bin
|
||||
SRC := src
|
||||
INPUT = src/main.cpp ../../../glad.c
|
||||
|
||||
all: $(BIN)/$(NAME)
|
||||
|
||||
run:
|
||||
./$(BIN)/$(NAME)
|
||||
|
||||
$(BIN)/$(NAME): $(SRC)/main.o $(SRC)/glad.o
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) $^ -o $@ $(LIBS)
|
||||
|
||||
.cpp.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
.c.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/glad.o: ../../../glad.c
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
# dependencies
|
||||
$(SRC)/main.o: $(SRC)/main.cpp
|
||||
|
||||
clean:
|
||||
rm -rf $(SRC)/*.o
|
||||
touch $(SRC)/*
|
||||
17
lessons/1. Getting started/3. Hello Triangle/meson.build
Normal file
17
lessons/1. Getting started/3. Hello Triangle/meson.build
Normal file
@@ -0,0 +1,17 @@
|
||||
project('3. Hello Triangle', ['cpp', 'c'], version : '1.0-0')
|
||||
|
||||
libsDir = [meson.current_source_dir() + '/../../../lib']
|
||||
|
||||
cxx = meson.get_compiler('cpp')
|
||||
libs = [cxx.find_library('glfw3', dirs : libsDir, static : true),
|
||||
cxx.find_library('dl'),
|
||||
cxx.find_library('pthread')]
|
||||
|
||||
depend = [dependency('X11')]
|
||||
sourceFiles = ['src/main.cpp',
|
||||
'../../../glad.c']
|
||||
|
||||
include = include_directories('../../../include')
|
||||
|
||||
main = executable('main', sourceFiles, include_directories : include, dependencies : [depend, libs])
|
||||
test('HelloTriangle test', main)
|
||||
168
lessons/1. Getting started/3. Hello Triangle/src/main.cpp
Normal file
168
lessons/1. Getting started/3. Hello Triangle/src/main.cpp
Normal file
@@ -0,0 +1,168 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
typedef unsigned int uint;
|
||||
|
||||
// fuj! pouze pro tento program
|
||||
const char* vertexShaderSource =
|
||||
"#version 330 core\n"
|
||||
"layout (location = 0) in vec3 aPos;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_Position = vec4(aPos.xyz, 1.0);\n"
|
||||
"}\0";
|
||||
const char* fragmentShaderSource =
|
||||
"#version 330 core\n"
|
||||
"out vec4 FragColor;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" FragColor = vec4(1.0, 0.5, 0.2, 1.0f);"
|
||||
"}\0";
|
||||
|
||||
// Global Constants
|
||||
const int WINDOW_WIDTH = 800;
|
||||
const int WINDOW_HEIGHT = 600;
|
||||
// Global Variables
|
||||
GLFWwindow* window;
|
||||
|
||||
uint8_t init(void);
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height);
|
||||
void proccessInput(GLFWwindow* window);
|
||||
|
||||
int main(int* argc, char** argv)
|
||||
{
|
||||
if (init() != 0)
|
||||
return -1;
|
||||
|
||||
uint vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
|
||||
glCompileShader(vertexShader);
|
||||
|
||||
int success;
|
||||
char infoLog[512];
|
||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
||||
cerr << "[ERROR] Vertex shader compilation failed!\n" << infoLog << endl;
|
||||
}
|
||||
|
||||
uint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
|
||||
glCompileShader(fragmentShader);
|
||||
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
||||
cerr << "[ERROR] Fragment shader compilation failed!\n" << infoLog << endl;
|
||||
}
|
||||
|
||||
uint shaderProgram = glCreateProgram();
|
||||
glAttachShader(shaderProgram, vertexShader);
|
||||
glAttachShader(shaderProgram, fragmentShader);
|
||||
glLinkProgram(shaderProgram);
|
||||
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
|
||||
cerr << "[ERROR] Program linking failed\n" << infoLog << endl;
|
||||
}
|
||||
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
|
||||
|
||||
float vertices[] = {
|
||||
0.5f, 0.5f, 0.0f, // top right
|
||||
0.5f, -0.5f, 0.0f, // bottom right
|
||||
-0.5f, -0.5f, 0.0f, // bottom left
|
||||
-0.5f, 0.5f, 0.0f // top left
|
||||
};
|
||||
uint indices[] = {
|
||||
0, 1, 3, // first triangle
|
||||
1, 2, 3 // second triangle
|
||||
};
|
||||
|
||||
uint VAO; // Vertex array buffer
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
uint VBO; // Vertex Buffer Object
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // copy data to Graphics card array buffer (buffer for vertices)
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (const void *)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
uint EBO; // Element Buffer Object
|
||||
glGenBuffers(1, &EBO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
/* ------------------ MAIN loop ------------------ */
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
// input ...
|
||||
proccessInput(window);
|
||||
|
||||
// rendering commands here ...
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glUseProgram(shaderProgram);
|
||||
glBindVertexArray(VAO);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
|
||||
// check and calls events and swap the buffers
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glDeleteBuffers(1, &EBO);
|
||||
glDeleteProgram(shaderProgram);
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t init(void) {
|
||||
// init of glfw window
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Hello Window!", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
cerr << "Failed to create glfw window" << endl;
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
// init of GLAD
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
cerr << "Failed to initialize GLAD" << endl;
|
||||
}
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffersize_callback);
|
||||
return 0;
|
||||
}
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
void proccessInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
if (glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
}
|
||||
34
lessons/1. Getting started/4. Shaders/Makefile
Normal file
34
lessons/1. Getting started/4. Shaders/Makefile
Normal file
@@ -0,0 +1,34 @@
|
||||
NAME := main
|
||||
CXX=g++
|
||||
DEBUG :=
|
||||
CXX_FLAGS := $(DEBUG) -std=c++14
|
||||
LIBS := -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -pthread -lXi -ldl -lm
|
||||
INCLUDE := ../../../include/
|
||||
BIN := bin
|
||||
SRC := src
|
||||
INPUT = src/main.cpp ../../../glad.c
|
||||
|
||||
all: $(BIN)/$(NAME)
|
||||
|
||||
run:
|
||||
cd $(BIN) && ./$(NAME)
|
||||
|
||||
$(BIN)/$(NAME): $(SRC)/main.o $(SRC)/glad.o $(SRC)/Shader.o
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) $^ -o $@ $(LIBS)
|
||||
|
||||
.cpp.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
.c.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/glad.o: ../../../glad.c
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
# dependencies
|
||||
$(SRC)/main.o: $(SRC)/main.cpp
|
||||
$(SRC)/Shader.o: $(SRC)/Shader.cpp
|
||||
|
||||
clean:
|
||||
rm -rf $(SRC)/*.o
|
||||
touch $(SRC)/*
|
||||
87
lessons/1. Getting started/4. Shaders/src/Shader.cpp
Normal file
87
lessons/1. Getting started/4. Shaders/src/Shader.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
#include "Shader.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <glad/glad.h>
|
||||
|
||||
Shader::Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath)
|
||||
{
|
||||
std::string v = readFile(vertexShaderSourcePath);
|
||||
std::string f = readFile(fragmentShaderSourcePath);
|
||||
const char *vertexSource = v.c_str();
|
||||
const char *fragmentSource = f.c_str();
|
||||
|
||||
int success;
|
||||
char infoLog[512];
|
||||
|
||||
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertexShader, 1, &vertexSource, NULL);
|
||||
glCompileShader(vertexShader);
|
||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Vertex shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
|
||||
glCompileShader(fragmentShader);
|
||||
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Fragment shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
Program = glCreateProgram();
|
||||
glAttachShader(Program, vertexShader);
|
||||
glAttachShader(Program, fragmentShader);
|
||||
glLinkProgram(Program);
|
||||
glGetProgramiv(Program, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(Program, 512, NULL, infoLog);
|
||||
std::cerr << "[ERROR] Program linking failed\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
}
|
||||
Shader::Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath)
|
||||
{
|
||||
Shader(vertexShaderSourcePath.c_str(), fragmentShaderSourcePath.c_str());
|
||||
}
|
||||
|
||||
std::string Shader::readFile(const char* path)
|
||||
{
|
||||
std::string out;
|
||||
try
|
||||
{
|
||||
std::ifstream ifs(path); // input file stream
|
||||
std::stringstream buffer;
|
||||
buffer << ifs.rdbuf();
|
||||
out = buffer.str();
|
||||
}
|
||||
catch (std::ifstream::failure e)
|
||||
{
|
||||
std::cerr << "[ERROR] Could not read/open file '" << path << "'\n";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
void Shader::Use()
|
||||
{
|
||||
glUseProgram(Program);
|
||||
}
|
||||
void Shader::setBool(const std::string name, bool value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value ? 1 : 0);
|
||||
}
|
||||
void Shader::setFloat(const std::string name, float value) const
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setInt(const std::string name, int value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
20
lessons/1. Getting started/4. Shaders/src/Shader.h
Normal file
20
lessons/1. Getting started/4. Shaders/src/Shader.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
unsigned int Program;
|
||||
|
||||
Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath);
|
||||
Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath);
|
||||
|
||||
void Use();
|
||||
|
||||
void setBool(const std::string name, bool value) const;
|
||||
void setFloat(const std::string name, float value) const;
|
||||
void setInt(const std::string name, int value) const;
|
||||
private:
|
||||
std::string readFile(const char* path);
|
||||
};
|
||||
|
||||
107
lessons/1. Getting started/4. Shaders/src/main.cpp
Normal file
107
lessons/1. Getting started/4. Shaders/src/main.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include "Shader.h"
|
||||
|
||||
using namespace std;
|
||||
typedef unsigned int uint;
|
||||
|
||||
// Global Constants
|
||||
const int WINDOW_WIDTH = 800;
|
||||
const int WINDOW_HEIGHT = 600;
|
||||
// Global Variables
|
||||
GLFWwindow* window;
|
||||
|
||||
uint8_t init(void);
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height);
|
||||
void proccessInput(GLFWwindow* window);
|
||||
|
||||
int main(int* argc, char** argv)
|
||||
{
|
||||
if (init() != 0)
|
||||
return -1;
|
||||
|
||||
Shader myShader("./../src/shaders/vertexShader.vert", "./../src/shaders/fragmentShader.frag");
|
||||
|
||||
float vertices[] = {
|
||||
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
|
||||
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f
|
||||
};
|
||||
|
||||
uint VAO; // Vertex array buffer
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
uint VBO; // Vertex Buffer Object
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // copy data to Graphics card array buffer (buffer for vertices)
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (const void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (const void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
/* ------------------ MAIN loop ------------------ */
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
// input ...
|
||||
proccessInput(window);
|
||||
|
||||
// rendering commands here ...
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
myShader.Use();
|
||||
glBindVertexArray(VAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
|
||||
// check and calls events and swap the buffers
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t init(void) {
|
||||
// init of glfw window
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Hello Window!", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
cerr << "Failed to create glfw window" << endl;
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
// init of GLAD
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
cerr << "Failed to initialize GLAD" << endl;
|
||||
}
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffersize_callback);
|
||||
return 0;
|
||||
}
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
void proccessInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
if (glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 FragColor;
|
||||
in vec4 outColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = outColor;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aColor;
|
||||
|
||||
out vec4 outColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(aPos.xyz, 1.0);
|
||||
outColor = vec4(aColor, 1.0f);
|
||||
}
|
||||
37
lessons/1. Getting started/5. Textures/Makefile
Normal file
37
lessons/1. Getting started/5. Textures/Makefile
Normal file
@@ -0,0 +1,37 @@
|
||||
NAME := main
|
||||
CXX=g++
|
||||
DEBUG :=
|
||||
CXX_FLAGS := $(DEBUG) -std=c++14
|
||||
LIBS := -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -pthread -lXi -ldl -lm
|
||||
INCLUDE := ../../../include/
|
||||
BIN := bin
|
||||
SRC := src
|
||||
INPUT = src/main.cpp ../../../glad.c
|
||||
|
||||
all: $(BIN)/$(NAME)
|
||||
|
||||
run:
|
||||
cd $(BIN) && ./$(NAME)
|
||||
|
||||
$(BIN)/$(NAME): $(SRC)/main.o $(SRC)/glad.o $(SRC)/Shader.o $(SRC)/stb_image.o
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) $^ -o $@ $(LIBS)
|
||||
|
||||
.cpp.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
.c.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/glad.o: ../../../glad.c
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/stb_image.o: ../../../stb_image.cpp
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
# dependencies
|
||||
$(SRC)/main.o: $(SRC)/main.cpp
|
||||
$(SRC)/Shader.o: $(SRC)/Shader.cpp
|
||||
|
||||
clean:
|
||||
rm -rf $(SRC)/*.o
|
||||
touch $(SRC)/*
|
||||
87
lessons/1. Getting started/5. Textures/src/Shader.cpp
Normal file
87
lessons/1. Getting started/5. Textures/src/Shader.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
#include "Shader.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <glad/glad.h>
|
||||
|
||||
Shader::Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath)
|
||||
{
|
||||
std::string v = readFile(vertexShaderSourcePath);
|
||||
std::string f = readFile(fragmentShaderSourcePath);
|
||||
const char *vertexSource = v.c_str();
|
||||
const char *fragmentSource = f.c_str();
|
||||
|
||||
int success;
|
||||
char infoLog[512];
|
||||
|
||||
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertexShader, 1, &vertexSource, NULL);
|
||||
glCompileShader(vertexShader);
|
||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Vertex shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
|
||||
glCompileShader(fragmentShader);
|
||||
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Fragment shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
Program = glCreateProgram();
|
||||
glAttachShader(Program, vertexShader);
|
||||
glAttachShader(Program, fragmentShader);
|
||||
glLinkProgram(Program);
|
||||
glGetProgramiv(Program, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(Program, 512, NULL, infoLog);
|
||||
std::cerr << "[ERROR] Program linking failed\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
}
|
||||
Shader::Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath)
|
||||
{
|
||||
Shader(vertexShaderSourcePath.c_str(), fragmentShaderSourcePath.c_str());
|
||||
}
|
||||
|
||||
std::string Shader::readFile(const char* path)
|
||||
{
|
||||
std::string out;
|
||||
try
|
||||
{
|
||||
std::ifstream ifs(path); // input file stream
|
||||
std::stringstream buffer;
|
||||
buffer << ifs.rdbuf();
|
||||
out = buffer.str();
|
||||
}
|
||||
catch (std::ifstream::failure e)
|
||||
{
|
||||
std::cerr << "[ERROR] Could not read/open file '" << path << "'\n";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
void Shader::Use()
|
||||
{
|
||||
glUseProgram(Program);
|
||||
}
|
||||
void Shader::setBool(const std::string name, bool value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value ? 1 : 0);
|
||||
}
|
||||
void Shader::setFloat(const std::string name, float value) const
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setInt(const std::string name, int value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
20
lessons/1. Getting started/5. Textures/src/Shader.h
Normal file
20
lessons/1. Getting started/5. Textures/src/Shader.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
unsigned int Program;
|
||||
|
||||
Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath);
|
||||
Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath);
|
||||
|
||||
void Use();
|
||||
|
||||
void setBool(const std::string name, bool value) const;
|
||||
void setFloat(const std::string name, float value) const;
|
||||
void setInt(const std::string name, int value) const;
|
||||
private:
|
||||
std::string readFile(const char* path);
|
||||
};
|
||||
|
||||
184
lessons/1. Getting started/5. Textures/src/main.cpp
Normal file
184
lessons/1. Getting started/5. Textures/src/main.cpp
Normal file
@@ -0,0 +1,184 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <stb_image.h>
|
||||
#include "Shader.h"
|
||||
|
||||
#define TEXTURES_DIR "../../../../textures/"
|
||||
|
||||
using namespace std;
|
||||
typedef unsigned int uint;
|
||||
|
||||
// Global Constants
|
||||
const int WINDOW_WIDTH = 800;
|
||||
const int WINDOW_HEIGHT = 600;
|
||||
// Global Variables
|
||||
GLFWwindow* window;
|
||||
float ratio = 0.2;
|
||||
|
||||
uint8_t init(void);
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height);
|
||||
void proccessInput(GLFWwindow* window);
|
||||
unsigned int load_texture(const char* path, bool flip = true);
|
||||
|
||||
int main(int* argc, char** argv)
|
||||
{
|
||||
if (init() != 0)
|
||||
return -1;
|
||||
|
||||
Shader myShader("../src/shaders/vertexShader.vert", "../src/shaders/fragmentShader.frag");
|
||||
|
||||
float vertices[] = {
|
||||
// positions // colors // texture coords
|
||||
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
|
||||
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
|
||||
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
|
||||
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
|
||||
};
|
||||
unsigned int indices[] = {
|
||||
0, 1, 3,
|
||||
1, 2, 3
|
||||
};
|
||||
|
||||
uint VAO; // Vertex array buffer
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
uint VBO; // Vertex Buffer Object
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // copy data to Graphics card array buffer (buffer for vertices)
|
||||
|
||||
uint EBO;
|
||||
glGenBuffers(1, &EBO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)(6 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
uint texture1 = load_texture(TEXTURES_DIR "container.jpg", false);
|
||||
uint texture2 = load_texture(TEXTURES_DIR "awesomeface.png");
|
||||
|
||||
myShader.Use();
|
||||
myShader.setInt("texture1", 0); // set Texture units IDs
|
||||
myShader.setInt("texture2", 1);
|
||||
|
||||
/* ------------------ MAIN loop ------------------ */
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
// input ...
|
||||
proccessInput(window);
|
||||
|
||||
// rendering commands here ...
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
myShader.Use();
|
||||
|
||||
myShader.setFloat("ratio", ratio);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, texture1);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, texture2);
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
|
||||
// check and calls events and swap the buffers
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t init(void) {
|
||||
// init of glfw window
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Hello Window!", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
cerr << "Failed to create glfw window" << endl;
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
// init of GLAD
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
cerr << "Failed to initialize GLAD" << endl;
|
||||
}
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffersize_callback);
|
||||
return 0;
|
||||
}
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
void proccessInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
if (glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
|
||||
if (ratio < 1.0)
|
||||
ratio += 0.001f;
|
||||
}
|
||||
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
|
||||
if (ratio > 0.0)
|
||||
ratio -= 0.001f;
|
||||
}
|
||||
|
||||
}
|
||||
unsigned int load_texture(const char* path, bool flip)
|
||||
{
|
||||
unsigned int texture = 0;
|
||||
int width, height, nrChannels, format;
|
||||
stbi_set_flip_vertically_on_load(flip);
|
||||
unsigned char* data = stbi_load(path, &width, &height, &nrChannels, 0); // load image to the array of bites (char = 1 byte)
|
||||
|
||||
switch (nrChannels)
|
||||
{
|
||||
case 4: format = GL_RGBA; break;
|
||||
case 3: format = GL_RGB; break;
|
||||
case 2: format = GL_RED; break;
|
||||
default: format = GL_RGB; break;
|
||||
}
|
||||
|
||||
if (data) // data != NULL
|
||||
{
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
|
||||
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);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, data); // generates texture
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
else {
|
||||
cerr << "Could not load texture '" << path << "'\n";
|
||||
}
|
||||
stbi_image_free(data);
|
||||
|
||||
return texture;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec4 outColor;
|
||||
in vec2 TexCoord;
|
||||
|
||||
uniform sampler2D texture1;
|
||||
uniform sampler2D texture2;
|
||||
uniform float ratio;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), ratio);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aColor;
|
||||
layout (location = 2) in vec2 aTexCoord;
|
||||
|
||||
out vec4 outColor;
|
||||
out vec2 TexCoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(aPos.xyz, 1.0);
|
||||
outColor = vec4(aColor, 1.0f);
|
||||
TexCoord = aTexCoord;
|
||||
}
|
||||
37
lessons/1. Getting started/6. Transformations/Makefile
Normal file
37
lessons/1. Getting started/6. Transformations/Makefile
Normal file
@@ -0,0 +1,37 @@
|
||||
NAME := main
|
||||
CXX=g++
|
||||
DEBUG :=
|
||||
CXX_FLAGS := $(DEBUG) -std=c++14
|
||||
LIBS := -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -pthread -lXi -ldl -lm
|
||||
INCLUDE := ../../../include/
|
||||
BIN := bin
|
||||
SRC := src
|
||||
INPUT = src/main.cpp ../../../glad.c
|
||||
|
||||
all: $(BIN)/$(NAME)
|
||||
|
||||
run:
|
||||
cd $(BIN) && ./$(NAME)
|
||||
|
||||
$(BIN)/$(NAME): $(SRC)/main.o $(SRC)/glad.o $(SRC)/Shader.o $(SRC)/stb_image.o
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) $^ -o $@ $(LIBS)
|
||||
|
||||
.cpp.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
.c.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/glad.o: ../../../glad.c
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/stb_image.o: ../../../stb_image.cpp
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
# dependencies
|
||||
$(SRC)/main.o: $(SRC)/main.cpp
|
||||
$(SRC)/Shader.o: $(SRC)/Shader.cpp
|
||||
|
||||
clean:
|
||||
rm -rf $(SRC)/*.o
|
||||
touch $(SRC)/*
|
||||
92
lessons/1. Getting started/6. Transformations/src/Shader.cpp
Normal file
92
lessons/1. Getting started/6. Transformations/src/Shader.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
#include "Shader.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <glad/glad.h>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
Shader::Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath)
|
||||
{
|
||||
std::string v = readFile(vertexShaderSourcePath);
|
||||
std::string f = readFile(fragmentShaderSourcePath);
|
||||
const char *vertexSource = v.c_str();
|
||||
const char *fragmentSource = f.c_str();
|
||||
|
||||
int success;
|
||||
char infoLog[512];
|
||||
|
||||
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertexShader, 1, &vertexSource, NULL);
|
||||
glCompileShader(vertexShader);
|
||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Vertex shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
|
||||
glCompileShader(fragmentShader);
|
||||
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Fragment shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
Program = glCreateProgram();
|
||||
glAttachShader(Program, vertexShader);
|
||||
glAttachShader(Program, fragmentShader);
|
||||
glLinkProgram(Program);
|
||||
glGetProgramiv(Program, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(Program, 512, NULL, infoLog);
|
||||
std::cerr << "[ERROR] Program linking failed\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
}
|
||||
Shader::Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath)
|
||||
{
|
||||
Shader(vertexShaderSourcePath.c_str(), fragmentShaderSourcePath.c_str());
|
||||
}
|
||||
|
||||
std::string Shader::readFile(const char* path)
|
||||
{
|
||||
std::string out;
|
||||
try
|
||||
{
|
||||
std::ifstream ifs(path); // input file stream
|
||||
std::stringstream buffer;
|
||||
buffer << ifs.rdbuf();
|
||||
out = buffer.str();
|
||||
}
|
||||
catch (std::ifstream::failure e)
|
||||
{
|
||||
std::cerr << "[ERROR] Could not read/open file '" << path << "'\n";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
void Shader::Use()
|
||||
{
|
||||
glUseProgram(Program);
|
||||
}
|
||||
void Shader::setBool(const std::string name, bool value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value ? 1 : 0);
|
||||
}
|
||||
void Shader::setFloat(const std::string name, float value) const
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setInt(const std::string name, int value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setMat4(const std::string name, glm::mat4 value) const
|
||||
{
|
||||
glUniformMatrix4fv(glGetUniformLocation(Program, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
|
||||
}
|
||||
22
lessons/1. Getting started/6. Transformations/src/Shader.h
Normal file
22
lessons/1. Getting started/6. Transformations/src/Shader.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
unsigned int Program;
|
||||
|
||||
Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath);
|
||||
Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath);
|
||||
|
||||
void Use();
|
||||
|
||||
void setBool(const std::string name, bool value) const;
|
||||
void setFloat(const std::string name, float value) const;
|
||||
void setInt(const std::string name, int value) const;
|
||||
void setMat4(const std::string name, glm::mat4 value) const;
|
||||
private:
|
||||
std::string readFile(const char* path);
|
||||
};
|
||||
|
||||
189
lessons/1. Getting started/6. Transformations/src/main.cpp
Normal file
189
lessons/1. Getting started/6. Transformations/src/main.cpp
Normal file
@@ -0,0 +1,189 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <stb_image.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include "Shader.h"
|
||||
|
||||
#define TEXTURES_DIR "../../../../textures/"
|
||||
|
||||
using namespace std;
|
||||
typedef unsigned int uint;
|
||||
|
||||
// Global Constants
|
||||
const int WINDOW_WIDTH = 800;
|
||||
const int WINDOW_HEIGHT = 600;
|
||||
// Global Variables
|
||||
GLFWwindow* window;
|
||||
|
||||
uint8_t init(void);
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height);
|
||||
void proccessInput(GLFWwindow* window);
|
||||
unsigned int load_texture(const char* path, bool flip = true);
|
||||
|
||||
int main(int* argc, char** argv)
|
||||
{
|
||||
if (init() != 0)
|
||||
return -1;
|
||||
|
||||
Shader myShader("../src/shaders/vertexShader.vert", "../src/shaders/fragmentShader.frag");
|
||||
|
||||
float vertices[] = {
|
||||
// positions // colors // texture coords
|
||||
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
|
||||
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
|
||||
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
|
||||
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
|
||||
};
|
||||
unsigned int indices[] = {
|
||||
0, 1, 3,
|
||||
1, 2, 3
|
||||
};
|
||||
|
||||
uint VAO; // Vertex array buffer
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
uint VBO; // Vertex Buffer Object
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // copy data to Graphics card array buffer (buffer for vertices)
|
||||
|
||||
uint EBO;
|
||||
glGenBuffers(1, &EBO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)(6 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
uint texture1 = load_texture(TEXTURES_DIR "container.jpg", false);
|
||||
uint texture2 = load_texture(TEXTURES_DIR "awesomeface.png");
|
||||
|
||||
myShader.Use();
|
||||
myShader.setInt("texture1", 0); // set Texture units IDs
|
||||
myShader.setInt("texture2", 1);
|
||||
|
||||
/* ------------------ MAIN loop ------------------ */
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
// input ...
|
||||
proccessInput(window);
|
||||
|
||||
// rendering commands here ...
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
myShader.Use();
|
||||
|
||||
glm::mat4 trans(1.0f); // transformation matrix
|
||||
// FIRST translate THEN rotate THEN scale !!
|
||||
trans = glm::translate(trans, glm::vec3(0.5f, -0.5f, 0.0f)) * glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f)) * glm::scale(trans, glm::vec3(0.5f, 0.5f, 0.5f));
|
||||
//trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
|
||||
//trans = glm::scale(trans, glm::vec3(0.5f, 0.5f, 0.5f));
|
||||
myShader.setMat4("transform", trans);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, texture1);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, texture2);
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
|
||||
trans = glm::mat4(1.0f);
|
||||
trans = glm::translate(trans, glm::vec3(-0.5f, 0.5f, 0.0f));
|
||||
trans = glm::scale(trans, glm::vec3(sin(glfwGetTime())));
|
||||
myShader.setMat4("transform", trans);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
|
||||
// check and calls events and swap the buffers
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t init(void) {
|
||||
// init of glfw window
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Hello Window!", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
cerr << "Failed to create glfw window" << endl;
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
// init of GLAD
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
cerr << "Failed to initialize GLAD" << endl;
|
||||
}
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffersize_callback);
|
||||
return 0;
|
||||
}
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
void proccessInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
if (glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
|
||||
}
|
||||
unsigned int load_texture(const char* path, bool flip)
|
||||
{
|
||||
unsigned int texture = 0;
|
||||
int width, height, nrChannels, format;
|
||||
stbi_set_flip_vertically_on_load(flip);
|
||||
unsigned char* data = stbi_load(path, &width, &height, &nrChannels, 0); // load image to the array of bytes (char = 1 byte)
|
||||
|
||||
switch (nrChannels)
|
||||
{
|
||||
case 4: format = GL_RGBA; break;
|
||||
case 3: format = GL_RGB; break;
|
||||
case 2: format = GL_RED; break;
|
||||
default: format = GL_RGB; break;
|
||||
}
|
||||
|
||||
if (data) // data != NULL
|
||||
{
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
|
||||
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);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, data); // generates texture
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
else {
|
||||
cerr << "Could not load texture '" << path << "'\n";
|
||||
}
|
||||
stbi_image_free(data);
|
||||
|
||||
return texture;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec4 outColor;
|
||||
in vec2 TexCoord;
|
||||
|
||||
uniform sampler2D texture1;
|
||||
uniform sampler2D texture2;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aColor;
|
||||
layout (location = 2) in vec2 aTexCoord;
|
||||
|
||||
out vec4 outColor;
|
||||
out vec2 TexCoord;
|
||||
|
||||
uniform mat4 transform;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = transform * vec4(aPos.xyz, 1.0);
|
||||
outColor = vec4(aColor, 1.0f);
|
||||
TexCoord = aTexCoord;
|
||||
}
|
||||
37
lessons/1. Getting started/7. Coordinate Systems/Makefile
Normal file
37
lessons/1. Getting started/7. Coordinate Systems/Makefile
Normal file
@@ -0,0 +1,37 @@
|
||||
NAME := main
|
||||
CXX=g++
|
||||
DEBUG :=
|
||||
CXX_FLAGS := $(DEBUG) -std=c++14
|
||||
LIBS := -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -pthread -lXi -ldl -lm
|
||||
INCLUDE := ../../../include/
|
||||
BIN := bin
|
||||
SRC := src
|
||||
INPUT = src/main.cpp ../../../glad.c
|
||||
|
||||
all: $(BIN)/$(NAME)
|
||||
|
||||
run:
|
||||
cd $(BIN) && ./$(NAME)
|
||||
|
||||
$(BIN)/$(NAME): $(SRC)/main.o $(SRC)/glad.o $(SRC)/Shader.o $(SRC)/stb_image.o
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) $^ -o $@ $(LIBS)
|
||||
|
||||
.cpp.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
.c.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/glad.o: ../../../glad.c
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/stb_image.o: ../../../stb_image.cpp
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
# dependencies
|
||||
$(SRC)/main.o: $(SRC)/main.cpp
|
||||
$(SRC)/Shader.o: $(SRC)/Shader.cpp
|
||||
|
||||
clean:
|
||||
rm -rf $(SRC)/*.o
|
||||
touch $(SRC)/*
|
||||
@@ -0,0 +1,92 @@
|
||||
#include "Shader.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <glad/glad.h>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
Shader::Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath)
|
||||
{
|
||||
std::string v = readFile(vertexShaderSourcePath);
|
||||
std::string f = readFile(fragmentShaderSourcePath);
|
||||
const char *vertexSource = v.c_str();
|
||||
const char *fragmentSource = f.c_str();
|
||||
|
||||
int success;
|
||||
char infoLog[512];
|
||||
|
||||
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertexShader, 1, &vertexSource, NULL);
|
||||
glCompileShader(vertexShader);
|
||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Vertex shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
|
||||
glCompileShader(fragmentShader);
|
||||
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Fragment shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
Program = glCreateProgram();
|
||||
glAttachShader(Program, vertexShader);
|
||||
glAttachShader(Program, fragmentShader);
|
||||
glLinkProgram(Program);
|
||||
glGetProgramiv(Program, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(Program, 512, NULL, infoLog);
|
||||
std::cerr << "[ERROR] Program linking failed\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
}
|
||||
Shader::Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath)
|
||||
{
|
||||
Shader(vertexShaderSourcePath.c_str(), fragmentShaderSourcePath.c_str());
|
||||
}
|
||||
|
||||
std::string Shader::readFile(const char* path)
|
||||
{
|
||||
std::string out;
|
||||
try
|
||||
{
|
||||
std::ifstream ifs(path); // input file stream
|
||||
std::stringstream buffer;
|
||||
buffer << ifs.rdbuf();
|
||||
out = buffer.str();
|
||||
}
|
||||
catch (std::ifstream::failure e)
|
||||
{
|
||||
std::cerr << "[ERROR] Could not read/open file '" << path << "'\n";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
void Shader::Use()
|
||||
{
|
||||
glUseProgram(Program);
|
||||
}
|
||||
void Shader::setBool(const std::string name, bool value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value ? 1 : 0);
|
||||
}
|
||||
void Shader::setFloat(const std::string name, float value) const
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setInt(const std::string name, int value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setMat4(const std::string name, glm::mat4 value) const
|
||||
{
|
||||
glUniformMatrix4fv(glGetUniformLocation(Program, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
unsigned int Program;
|
||||
|
||||
Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath);
|
||||
Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath);
|
||||
|
||||
void Use();
|
||||
|
||||
void setBool(const std::string name, bool value) const;
|
||||
void setFloat(const std::string name, float value) const;
|
||||
void setInt(const std::string name, int value) const;
|
||||
void setMat4(const std::string name, glm::mat4 value) const;
|
||||
private:
|
||||
std::string readFile(const char* path);
|
||||
};
|
||||
|
||||
237
lessons/1. Getting started/7. Coordinate Systems/src/main.cpp
Normal file
237
lessons/1. Getting started/7. Coordinate Systems/src/main.cpp
Normal file
@@ -0,0 +1,237 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <stb_image.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include "Shader.h"
|
||||
|
||||
#define TEXTURES_DIR "../../../../textures/"
|
||||
|
||||
using namespace std;
|
||||
typedef unsigned int uint;
|
||||
|
||||
// Global Constants
|
||||
// ...
|
||||
// Global Variables
|
||||
GLFWwindow* window;
|
||||
int WINDOW_WIDTH = 800;
|
||||
int WINDOW_HEIGHT = 600;
|
||||
|
||||
uint8_t init(void);
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height);
|
||||
void proccessInput(GLFWwindow* window);
|
||||
unsigned int load_texture(const char* path, bool flip = true);
|
||||
|
||||
int main(int* argc, char** argv)
|
||||
{
|
||||
if (init() != 0)
|
||||
return -1;
|
||||
|
||||
Shader myShader("../src/shaders/vertexShader.vert", "../src/shaders/fragmentShader.frag");
|
||||
|
||||
float vertices[] = {
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||
|
||||
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
|
||||
};
|
||||
|
||||
glm::vec3 cubePositions[] = {
|
||||
glm::vec3(0.0f, 0.0f, 0.0f),
|
||||
glm::vec3(2.0f, 5.0f, -15.0f),
|
||||
glm::vec3(-1.5f, -2.2f, -2.5f),
|
||||
glm::vec3(-3.8f, -2.0f, -12.3f),
|
||||
glm::vec3(2.4f, -0.4f, -3.5f),
|
||||
glm::vec3(-1.7f, 3.0f, -7.5f),
|
||||
glm::vec3(1.3f, -2.0f, -2.5f),
|
||||
glm::vec3(1.5f, 2.0f, -2.5f),
|
||||
glm::vec3(1.5f, 0.2f, -1.5f),
|
||||
glm::vec3(-1.3f, 1.0f, -1.5f)
|
||||
};
|
||||
|
||||
uint VAO; // Vertex array buffer
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
uint VBO; // Vertex Buffer Object
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // copy data to Graphics card array buffer (buffer for vertices)
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (const void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (const void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
uint texture1 = load_texture(TEXTURES_DIR "container.jpg", false);
|
||||
uint texture2 = load_texture(TEXTURES_DIR "awesomeface.png");
|
||||
|
||||
myShader.Use();
|
||||
myShader.setInt("texture1", 0); // set Texture units IDs
|
||||
myShader.setInt("texture2", 1);
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
/* ------------------ MAIN loop ------------------ */
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
// input ...
|
||||
proccessInput(window);
|
||||
|
||||
// rendering commands here ...
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, texture1);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, texture2);
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
myShader.Use();
|
||||
|
||||
glm::mat4 view(1.0f);
|
||||
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
|
||||
|
||||
glm::mat4 projection(1.0f);
|
||||
projection = glm::perspective(glm::radians(45.0f), (float)WINDOW_WIDTH / WINDOW_HEIGHT, 0.1f, 100.0f);
|
||||
|
||||
myShader.setMat4("view", view);
|
||||
myShader.setMat4("projection", projection);
|
||||
|
||||
for (int i = 0; i < sizeof(cubePositions) / sizeof(glm::vec3); i++)
|
||||
{
|
||||
glm::mat4 model(1.0f);
|
||||
model = glm::translate(model, cubePositions[i]);
|
||||
float angle = i * 20.0f;
|
||||
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
|
||||
myShader.setMat4("model", model);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
}
|
||||
|
||||
// check and calls events and swap the buffers
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t init(void) {
|
||||
// init of glfw window
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "7. Coordinate Systems", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
cerr << "Failed to create glfw window" << endl;
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
// init of GLAD
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
cerr << "Failed to initialize GLAD" << endl;
|
||||
}
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffersize_callback);
|
||||
return 0;
|
||||
}
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
WINDOW_HEIGHT = height;
|
||||
WINDOW_WIDTH = width;
|
||||
}
|
||||
void proccessInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
if (glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
|
||||
}
|
||||
unsigned int load_texture(const char* path, bool flip)
|
||||
{
|
||||
unsigned int texture = 0;
|
||||
int width, height, nrChannels, format;
|
||||
stbi_set_flip_vertically_on_load(flip);
|
||||
unsigned char* data = stbi_load(path, &width, &height, &nrChannels, 0); // load image to the array of bytes (char = 1 byte)
|
||||
|
||||
switch (nrChannels)
|
||||
{
|
||||
case 4: format = GL_RGBA; break;
|
||||
case 3: format = GL_RGB; break;
|
||||
case 2: format = GL_RED; break;
|
||||
default: format = GL_RGB; break;
|
||||
}
|
||||
|
||||
if (data) // data != NULL
|
||||
{
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
|
||||
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);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, data); // generates texture
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
else {
|
||||
cerr << "Could not load texture '" << path << "'\n";
|
||||
}
|
||||
stbi_image_free(data);
|
||||
|
||||
return texture;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec4 outColor;
|
||||
in vec2 TexCoord;
|
||||
|
||||
uniform sampler2D texture1;
|
||||
uniform sampler2D texture2;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec2 aTexCoord;
|
||||
|
||||
out vec2 TexCoord;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(aPos.xyz, 1.0);
|
||||
TexCoord = aTexCoord;
|
||||
}
|
||||
35
lessons/1. Getting started/8. Camera/CMakeLists.txt
Normal file
35
lessons/1. Getting started/8. Camera/CMakeLists.txt
Normal file
@@ -0,0 +1,35 @@
|
||||
cmake_minimum_required(VERSION 3.13.4)
|
||||
|
||||
project("main")
|
||||
|
||||
add_subdirectory("src")
|
||||
|
||||
target_include_directories("${PROJECT_NAME}"
|
||||
PUBLIC "${CMAKE_SOURCE_DIR}/../../../include"
|
||||
)
|
||||
target_link_directories("${PROJECT_NAME}"
|
||||
PRIVATE "${CMAKE_SOURCE_DIR}/../../../lib"
|
||||
)
|
||||
target_link_libraries("${PROJECT_NAME}"
|
||||
camera
|
||||
shader
|
||||
)
|
||||
if (MSVC)
|
||||
set (glfw3Lib debug Debug/glfw3d optimized Release/glfw3)
|
||||
target_link_libraries("${PROJECT_NAME}"
|
||||
opengl32
|
||||
${glfw3Lib}
|
||||
)
|
||||
elseif (UNIX)
|
||||
target_link_libraries("${PROJECT_NAME}"
|
||||
glfw3
|
||||
dl
|
||||
m
|
||||
Xi
|
||||
pthread
|
||||
Xrandr
|
||||
X11
|
||||
GL
|
||||
GLU
|
||||
)
|
||||
endif()
|
||||
37
lessons/1. Getting started/8. Camera/Makefile
Normal file
37
lessons/1. Getting started/8. Camera/Makefile
Normal file
@@ -0,0 +1,37 @@
|
||||
NAME := main
|
||||
CXX=g++
|
||||
DEBUG :=
|
||||
CXX_FLAGS := $(DEBUG) -std=c++14
|
||||
LIBS := -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -pthread -lXi -ldl -lm
|
||||
INCLUDE := ../../../include/
|
||||
BIN := bin
|
||||
SRC := src
|
||||
INPUT = src/main.cpp ../../../glad.c
|
||||
|
||||
all: $(BIN)/$(NAME)
|
||||
|
||||
run:
|
||||
cd $(BIN) && ./$(NAME)
|
||||
|
||||
$(BIN)/$(NAME): $(SRC)/main.o $(SRC)/glad.o $(SRC)/Shader.o $(SRC)/stb_image.o $(SRC)/Camera.o
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) $^ -o $@ $(LIBS)
|
||||
|
||||
.cpp.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
.c.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/glad.o: ../../../glad.c
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/stb_image.o: ../../../stb_image.cpp
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
# dependencies
|
||||
$(SRC)/main.o: $(SRC)/main.cpp
|
||||
$(SRC)/Shader.o: $(SRC)/Shader.cpp
|
||||
|
||||
clean:
|
||||
rm -rf $(SRC)/*.o
|
||||
touch $(SRC)/*
|
||||
3
lessons/1. Getting started/8. Camera/build.sh
Normal file
3
lessons/1. Getting started/8. Camera/build.sh
Normal file
@@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
|
||||
cmake --build build
|
||||
3
lessons/1. Getting started/8. Camera/configure.sh
Normal file
3
lessons/1. Getting started/8. Camera/configure.sh
Normal file
@@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
|
||||
cmake -S . -B build
|
||||
8
lessons/1. Getting started/8. Camera/recompilerun.sh
Normal file
8
lessons/1. Getting started/8. Camera/recompilerun.sh
Normal file
@@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
|
||||
if [ ! -d "build" ]; then meson build; fi
|
||||
|
||||
cd build
|
||||
meson compile
|
||||
meson test
|
||||
cd ..
|
||||
5
lessons/1. Getting started/8. Camera/run.sh
Normal file
5
lessons/1. Getting started/8. Camera/run.sh
Normal file
@@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
cd build/src/
|
||||
exec ./main
|
||||
#cd ../../
|
||||
19
lessons/1. Getting started/8. Camera/src/CMakeLists.txt
Normal file
19
lessons/1. Getting started/8. Camera/src/CMakeLists.txt
Normal file
@@ -0,0 +1,19 @@
|
||||
add_library(camera Camera.cpp Camera.h)
|
||||
add_library(shader Shader.cpp Shader.h)
|
||||
|
||||
set(REPO_ROOT "${CMAKE_SOURCE_DIR}/../../..")
|
||||
set(PROJECT_DIR "${CMAKE_SOURCE_DIR}")
|
||||
set(SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
|
||||
|
||||
include_directories(
|
||||
"${CMAKE_CURRENT_BINARY_DIR}"
|
||||
"${REPO_ROOT}/include"
|
||||
)
|
||||
|
||||
configure_file(config.h.in config.h @ONLY)
|
||||
|
||||
add_executable(${PROJECT_NAME}
|
||||
main.cpp
|
||||
"${REPO_ROOT}/glad.c"
|
||||
"${REPO_ROOT}/stb_image.cpp"
|
||||
)
|
||||
83
lessons/1. Getting started/8. Camera/src/Camera.cpp
Normal file
83
lessons/1. Getting started/8. Camera/src/Camera.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include "Camera.h"
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
Camera::Camera(glm::vec3 position, glm::vec3 worldUp, glm::vec3 worldFront, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(2.0f), MouseSensitivity(0.001f), FOV(45.0f), Mode(CAM_FLOAT)
|
||||
{
|
||||
this->Position = position;
|
||||
this->WorldUp = worldUp;
|
||||
this->Yaw = yaw;
|
||||
this->Pitch = pitch;
|
||||
this->WorldFront = worldFront;
|
||||
this->WorldRight = glm::normalize(glm::cross(this->WorldFront, this->WorldUp));
|
||||
|
||||
updateCameraVectors();
|
||||
}
|
||||
void Camera::updateCameraVectors()
|
||||
{
|
||||
glm::vec3 front;
|
||||
front.x = cos(Yaw) * cos(Pitch);
|
||||
front.y = sin(Pitch);
|
||||
front.z = sin(Yaw) * cos(Pitch);
|
||||
Front = glm::normalize(front);
|
||||
|
||||
Right = glm::normalize(glm::cross(Front, WorldUp));
|
||||
Up = glm::normalize(glm::cross(Right, Front));
|
||||
}
|
||||
glm::mat4 Camera::getViewMatrix()
|
||||
{
|
||||
return glm::lookAt(Position, Position + Front, WorldUp);
|
||||
}
|
||||
void Camera::ProccessKeyboard(camera_movement direction, float deltaTime)
|
||||
{
|
||||
switch (direction)
|
||||
{
|
||||
case CAM_FORWARD:
|
||||
Position += deltaTime * Front;
|
||||
break;
|
||||
case CAM_BACKWARD:
|
||||
Position -= deltaTime * Front;
|
||||
break;
|
||||
case CAM_LEFT:
|
||||
Position -= deltaTime * Right;
|
||||
break;
|
||||
case CAM_RIGHT:
|
||||
Position += deltaTime * Right;
|
||||
break;
|
||||
case CAM_UP:
|
||||
Position += deltaTime * WorldUp;
|
||||
break;
|
||||
case CAM_DOWN:
|
||||
Position -= deltaTime * WorldUp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
void Camera::ProccessMouse(float xoffset, float yoffset, bool constrainPitch)
|
||||
{
|
||||
xoffset *= MouseSensitivity;
|
||||
yoffset *= MouseSensitivity;
|
||||
|
||||
Yaw = fmodf(Yaw + xoffset, 360.0f);
|
||||
Pitch += yoffset;
|
||||
|
||||
if (constrainPitch)
|
||||
{
|
||||
if (Pitch > 89.0f)
|
||||
Pitch = 89.0f;
|
||||
else if (Pitch < -89.0f)
|
||||
Pitch = -89.0f;
|
||||
}
|
||||
|
||||
updateCameraVectors();
|
||||
}
|
||||
void Camera::ProccessScroll(float yoffset)
|
||||
{
|
||||
FOV -= yoffset;
|
||||
if (FOV < 1.0f)
|
||||
FOV = 1.0f;
|
||||
else if (FOV > 45.0f)
|
||||
FOV = 45.0f;
|
||||
}
|
||||
void Camera::SetCameraMode(camera_mode mode)
|
||||
{
|
||||
Mode = mode;
|
||||
}
|
||||
53
lessons/1. Getting started/8. Camera/src/Camera.h
Normal file
53
lessons/1. Getting started/8. Camera/src/Camera.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
enum camera_movement {
|
||||
CAM_FORWARD,
|
||||
CAM_BACKWARD,
|
||||
CAM_LEFT,
|
||||
CAM_RIGHT,
|
||||
CAM_UP,
|
||||
CAM_DOWN
|
||||
};
|
||||
|
||||
enum camera_mode {
|
||||
CAM_FLOAT,
|
||||
CAM_MINECRAFT
|
||||
};
|
||||
|
||||
class Camera
|
||||
{
|
||||
public:
|
||||
float FOV;
|
||||
float MovementSpeed;
|
||||
float MouseSensitivity;
|
||||
float Yaw; // vlevo vpravo
|
||||
float Pitch; // nahoru dolu
|
||||
|
||||
camera_mode Mode;
|
||||
|
||||
glm::vec3 Position;
|
||||
glm::vec3 Front;
|
||||
glm::vec3 Right;
|
||||
glm::vec3 Up;
|
||||
glm::vec3 WorldUp;
|
||||
glm::vec3 WorldRight;
|
||||
glm::vec3 WorldFront;
|
||||
|
||||
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f),
|
||||
glm::vec3 worldUp = glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3 worldFront = glm::vec3(0.0f, 0.0f, -1.0),
|
||||
float yaw = -90.0f, float pitch = 0);
|
||||
|
||||
glm::mat4 getViewMatrix();
|
||||
void ProccessKeyboard(camera_movement direction, float deltaTime);
|
||||
void ProccessMouse(float xoffset, float yoffset, bool constrainPitch = true);
|
||||
void ProccessScroll(float yoffset);
|
||||
void SetCameraMode(camera_mode mode);
|
||||
|
||||
private:
|
||||
float lastY;
|
||||
float lastX;
|
||||
|
||||
void updateCameraVectors();
|
||||
};
|
||||
|
||||
92
lessons/1. Getting started/8. Camera/src/Shader.cpp
Normal file
92
lessons/1. Getting started/8. Camera/src/Shader.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
#include "Shader.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <glad/glad.h>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
Shader::Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath)
|
||||
{
|
||||
std::string v = readFile(vertexShaderSourcePath);
|
||||
std::string f = readFile(fragmentShaderSourcePath);
|
||||
const char *vertexSource = v.c_str();
|
||||
const char *fragmentSource = f.c_str();
|
||||
|
||||
int success;
|
||||
char infoLog[512];
|
||||
|
||||
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertexShader, 1, &vertexSource, NULL);
|
||||
glCompileShader(vertexShader);
|
||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Vertex shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
|
||||
glCompileShader(fragmentShader);
|
||||
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Fragment shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
Program = glCreateProgram();
|
||||
glAttachShader(Program, vertexShader);
|
||||
glAttachShader(Program, fragmentShader);
|
||||
glLinkProgram(Program);
|
||||
glGetProgramiv(Program, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(Program, 512, NULL, infoLog);
|
||||
std::cerr << "[ERROR] Program linking failed\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
}
|
||||
Shader::Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath)
|
||||
{
|
||||
Shader(vertexShaderSourcePath.c_str(), fragmentShaderSourcePath.c_str());
|
||||
}
|
||||
|
||||
std::string Shader::readFile(const char* path)
|
||||
{
|
||||
std::string out;
|
||||
try
|
||||
{
|
||||
std::ifstream ifs(path); // input file stream
|
||||
std::stringstream buffer;
|
||||
buffer << ifs.rdbuf();
|
||||
out = buffer.str();
|
||||
}
|
||||
catch (std::ifstream::failure& e)
|
||||
{
|
||||
std::cerr << "[ERROR] Could not read/open file '" << path << "'\n";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
void Shader::Use()
|
||||
{
|
||||
glUseProgram(Program);
|
||||
}
|
||||
void Shader::setBool(const std::string name, bool value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value ? 1 : 0);
|
||||
}
|
||||
void Shader::setFloat(const std::string name, float value) const
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setInt(const std::string name, int value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setMat4(const std::string name, glm::mat4 value) const
|
||||
{
|
||||
glUniformMatrix4fv(glGetUniformLocation(Program, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
|
||||
}
|
||||
22
lessons/1. Getting started/8. Camera/src/Shader.h
Normal file
22
lessons/1. Getting started/8. Camera/src/Shader.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
unsigned int Program;
|
||||
|
||||
Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath);
|
||||
Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath);
|
||||
|
||||
void Use();
|
||||
|
||||
void setBool(const std::string name, bool value) const;
|
||||
void setFloat(const std::string name, float value) const;
|
||||
void setInt(const std::string name, int value) const;
|
||||
void setMat4(const std::string name, glm::mat4 value) const;
|
||||
private:
|
||||
std::string readFile(const char* path);
|
||||
};
|
||||
|
||||
3
lessons/1. Getting started/8. Camera/src/config.h.in
Normal file
3
lessons/1. Getting started/8. Camera/src/config.h.in
Normal file
@@ -0,0 +1,3 @@
|
||||
#cmakedefine REPO_ROOT "@REPO_ROOT@"
|
||||
#cmakedefine PROJECT_DIR "@PROJECT_DIR@"
|
||||
#cmakedefine SOURCE_DIR "@SOURCE_DIR@"
|
||||
292
lessons/1. Getting started/8. Camera/src/main.cpp
Normal file
292
lessons/1. Getting started/8. Camera/src/main.cpp
Normal file
@@ -0,0 +1,292 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <stb_image.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include "Shader.h"
|
||||
#include "Camera.h"
|
||||
#include "config.h"
|
||||
|
||||
#define TEXTURES_DIR REPO_ROOT "/textures"
|
||||
#define SHADERS_DIR SOURCE_DIR "/shaders"
|
||||
|
||||
using namespace std;
|
||||
typedef unsigned int uint;
|
||||
|
||||
// Global Constants
|
||||
// ...
|
||||
// Global Variables
|
||||
GLFWwindow* window;
|
||||
int WINDOW_WIDTH = 800;
|
||||
int WINDOW_HEIGHT = 600;
|
||||
|
||||
Camera myCamera(glm::vec3(0.0f, 0.0f, 3.0f));
|
||||
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
|
||||
float lastX = 400;
|
||||
float lastY = 300;
|
||||
bool firstmouse = true;
|
||||
|
||||
uint8_t init(void);
|
||||
void framebuffersize_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 proccessInput(GLFWwindow* window);
|
||||
unsigned int load_texture(const char* path, bool flip = true);
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (init() != 0)
|
||||
return -1;
|
||||
|
||||
Shader myShader(SHADERS_DIR "/vertexShader.vert", SHADERS_DIR "/fragmentShader.frag");
|
||||
float vertices[] = {
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||
|
||||
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
|
||||
};
|
||||
|
||||
glm::vec3 cubePositions[] = {
|
||||
glm::vec3(0.0f, 0.0f, 0.0f),
|
||||
glm::vec3(2.0f, 5.0f, -15.0f),
|
||||
glm::vec3(-1.5f, -2.2f, -2.5f),
|
||||
glm::vec3(-3.8f, -2.0f, -12.3f),
|
||||
glm::vec3(2.4f, -0.4f, -3.5f),
|
||||
glm::vec3(-1.7f, 3.0f, -7.5f),
|
||||
glm::vec3(1.3f, -2.0f, -2.5f),
|
||||
glm::vec3(1.5f, 2.0f, -2.5f),
|
||||
glm::vec3(1.5f, 0.2f, -1.5f),
|
||||
glm::vec3(-1.3f, 1.0f, -1.5f)
|
||||
};
|
||||
|
||||
uint VAO; // Vertex array buffer
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
uint VBO; // Vertex Buffer Object
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // copy data to Graphics card array buffer (buffer for vertices)
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (const void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (const void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
uint texture1 = load_texture(TEXTURES_DIR "/container.jpg", false);
|
||||
uint texture2 = load_texture(TEXTURES_DIR "/awesomeface.png");
|
||||
|
||||
myShader.Use();
|
||||
myShader.setInt("texture1", 0); // set Texture units IDs
|
||||
myShader.setInt("texture2", 1);
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
/* ------------------ MAIN loop ------------------ */
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
float currentFrame = glfwGetTime();
|
||||
deltaTime = currentFrame - lastFrame; // cas jak dlouho trval posledni frame
|
||||
lastFrame = currentFrame; // cas kdy zacal tento frame
|
||||
// input ...
|
||||
proccessInput(window);
|
||||
|
||||
// rendering commands here ...
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, texture1);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, texture2);
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
myShader.Use();
|
||||
|
||||
glm::mat4 view = myCamera.getViewMatrix();
|
||||
|
||||
glm::mat4 projection(1.0f);
|
||||
projection = glm::perspective(glm::radians(myCamera.FOV), (float)WINDOW_WIDTH / WINDOW_HEIGHT, 0.1f, 100.0f);
|
||||
|
||||
myShader.setMat4("view", view);
|
||||
myShader.setMat4("projection", projection);
|
||||
|
||||
for (int i = 0; (long unsigned int)i < sizeof(cubePositions) / sizeof(glm::vec3); i++)
|
||||
{
|
||||
glm::mat4 model(1.0f);
|
||||
model = glm::translate(model, cubePositions[i]);
|
||||
float angle = i * 20.0f;
|
||||
|
||||
if (i % 3 == 0)
|
||||
model = glm::rotate(model, (float)glfwGetTime() * 0.2f, glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
else
|
||||
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
|
||||
|
||||
myShader.setMat4("model", model);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
}
|
||||
|
||||
// check and calls events and swap the buffers
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t init(void) {
|
||||
// init of glfw window
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "8. Camera", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
cerr << "Failed to create glfw window" << endl;
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
|
||||
// init of GLAD
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
cerr << "Failed to initialize GLAD" << endl;
|
||||
}
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffersize_callback);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
return 0;
|
||||
}
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
WINDOW_HEIGHT = height;
|
||||
WINDOW_WIDTH = width;
|
||||
}
|
||||
void proccessInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
if (glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
/* -------------------- Movement -------------------- */
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_FORWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_BACKWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_RIGHT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_LEFT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_DOWN, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_UP, deltaTime);
|
||||
|
||||
}
|
||||
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;
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
|
||||
myCamera.ProccessMouse(xoffset, yoffset);
|
||||
}
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
||||
{
|
||||
myCamera.ProccessScroll(yoffset);
|
||||
}
|
||||
unsigned int load_texture(const char* path, bool flip)
|
||||
{
|
||||
unsigned int texture = 0;
|
||||
int width, height, nrChannels, format;
|
||||
stbi_set_flip_vertically_on_load(flip);
|
||||
unsigned char* data = stbi_load(path, &width, &height, &nrChannels, 0); // load image to the array of bytes (char = 1 byte)
|
||||
|
||||
switch (nrChannels)
|
||||
{
|
||||
case 4: format = GL_RGBA; break;
|
||||
case 3: format = GL_RGB; break;
|
||||
case 2: format = GL_RED; break;
|
||||
default: format = GL_RGB; break;
|
||||
}
|
||||
|
||||
if (data) // data != NULL
|
||||
{
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
|
||||
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);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, data); // generates texture
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
else {
|
||||
cerr << "Could not load texture '" << path << "'\n";
|
||||
}
|
||||
stbi_image_free(data);
|
||||
|
||||
return texture;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec4 outColor;
|
||||
in vec2 TexCoord;
|
||||
|
||||
uniform sampler2D texture1;
|
||||
uniform sampler2D texture2;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec2 aTexCoord;
|
||||
|
||||
out vec2 TexCoord;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(aPos.xyz, 1.0);
|
||||
TexCoord = aTexCoord;
|
||||
}
|
||||
37
lessons/2. Lighting/1. Colors/Makefile
Normal file
37
lessons/2. Lighting/1. Colors/Makefile
Normal file
@@ -0,0 +1,37 @@
|
||||
NAME := main
|
||||
CXX=g++
|
||||
DEBUG :=
|
||||
CXX_FLAGS := $(DEBUG) -std=c++14
|
||||
LIBS := -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -pthread -lXi -ldl -lm
|
||||
INCLUDE := ../../../include/
|
||||
BIN := bin
|
||||
SRC := src
|
||||
INPUT = src/main.cpp ../../../glad.c
|
||||
|
||||
all: $(BIN)/$(NAME)
|
||||
|
||||
run:
|
||||
cd $(BIN) && ./$(NAME)
|
||||
|
||||
$(BIN)/$(NAME): $(SRC)/main.o $(SRC)/glad.o $(SRC)/Shader.o $(SRC)/stb_image.o $(SRC)/Camera.o
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) $^ -o $@ $(LIBS)
|
||||
|
||||
.cpp.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
.c.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/glad.o: ../../../glad.c
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/stb_image.o: ../../../stb_image.cpp
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
# dependencies
|
||||
$(SRC)/main.o: $(SRC)/main.cpp
|
||||
$(SRC)/Shader.o: $(SRC)/Shader.cpp
|
||||
|
||||
clean:
|
||||
rm -rf $(SRC)/*.o
|
||||
touch $(SRC)/*
|
||||
83
lessons/2. Lighting/1. Colors/src/Camera.cpp
Normal file
83
lessons/2. Lighting/1. Colors/src/Camera.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include "Camera.h"
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
Camera::Camera(glm::vec3 position, glm::vec3 worldUp, glm::vec3 worldFront, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(2.5f), MouseSensitivity(0.001f), FOV(45.0f), Mode(CAM_FLOAT)
|
||||
{
|
||||
this->Position = position;
|
||||
this->WorldUp = worldUp;
|
||||
this->Yaw = yaw;
|
||||
this->Pitch = pitch;
|
||||
this->WorldFront = worldFront;
|
||||
this->WorldRight = glm::normalize(glm::cross(this->WorldFront, this->WorldUp));
|
||||
|
||||
updateCameraVectors();
|
||||
}
|
||||
void Camera::updateCameraVectors()
|
||||
{
|
||||
glm::vec3 front;
|
||||
front.x = cos(Yaw) * cos(Pitch);
|
||||
front.y = sin(Pitch);
|
||||
front.z = sin(Yaw) * cos(Pitch);
|
||||
Front = glm::normalize(front);
|
||||
|
||||
Right = glm::normalize(glm::cross(Front, WorldUp));
|
||||
Up = glm::normalize(glm::cross(Right, Front));
|
||||
}
|
||||
glm::mat4 Camera::getViewMatrix()
|
||||
{
|
||||
return glm::lookAt(Position, Position + Front, WorldUp);
|
||||
}
|
||||
void Camera::ProccessKeyboard(camera_movement direction, float deltaTime)
|
||||
{
|
||||
switch (direction)
|
||||
{
|
||||
case CAM_FORWARD:
|
||||
Position += deltaTime * Front;
|
||||
break;
|
||||
case CAM_BACKWARD:
|
||||
Position -= deltaTime * Front;
|
||||
break;
|
||||
case CAM_LEFT:
|
||||
Position -= deltaTime * Right;
|
||||
break;
|
||||
case CAM_RIGHT:
|
||||
Position += deltaTime * Right;
|
||||
break;
|
||||
case CAM_UP:
|
||||
Position += deltaTime * WorldUp;
|
||||
break;
|
||||
case CAM_DOWN:
|
||||
Position -= deltaTime * WorldUp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
void Camera::ProccessMouse(float xoffset, float yoffset, bool constrainPitch)
|
||||
{
|
||||
xoffset *= MouseSensitivity;
|
||||
yoffset *= MouseSensitivity;
|
||||
|
||||
Yaw = fmodf(Yaw + xoffset, 360.0f);
|
||||
Pitch += yoffset;
|
||||
|
||||
if (constrainPitch)
|
||||
{
|
||||
if (Pitch > 89.0f)
|
||||
Pitch = 89.0f;
|
||||
else if (Pitch < -89.0f)
|
||||
Pitch = -89.0f;
|
||||
}
|
||||
|
||||
updateCameraVectors();
|
||||
}
|
||||
void Camera::ProccessScroll(float yoffset)
|
||||
{
|
||||
FOV -= yoffset;
|
||||
if (FOV < 1.0f)
|
||||
FOV = 1.0f;
|
||||
else if (FOV > 45.0f)
|
||||
FOV = 45.0f;
|
||||
}
|
||||
void Camera::SetCameraMode(camera_mode mode)
|
||||
{
|
||||
Mode = mode;
|
||||
}
|
||||
54
lessons/2. Lighting/1. Colors/src/Camera.h
Normal file
54
lessons/2. Lighting/1. Colors/src/Camera.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
enum camera_movement {
|
||||
CAM_FORWARD,
|
||||
CAM_BACKWARD,
|
||||
CAM_LEFT,
|
||||
CAM_RIGHT,
|
||||
CAM_UP,
|
||||
CAM_DOWN
|
||||
};
|
||||
|
||||
enum camera_mode {
|
||||
CAM_FLOAT,
|
||||
CAM_MINECRAFT
|
||||
};
|
||||
|
||||
class Camera
|
||||
{
|
||||
public:
|
||||
float FOV;
|
||||
float MovementSpeed;
|
||||
float MouseSensitivity;
|
||||
float Yaw; // vlevo vpravo
|
||||
float Pitch; // nahoru dolu
|
||||
|
||||
camera_mode Mode;
|
||||
|
||||
glm::vec3 Position;
|
||||
glm::vec3 Front;
|
||||
glm::vec3 Right;
|
||||
glm::vec3 Up;
|
||||
glm::vec3 WorldUp;
|
||||
glm::vec3 WorldRight;
|
||||
glm::vec3 WorldFront;
|
||||
|
||||
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f),
|
||||
glm::vec3 worldUp = glm::vec3(0.0f, 1.0f, 0.0f),
|
||||
glm::vec3 worldFront = glm::vec3(0.0f, 0.0f, -1.0),
|
||||
float yaw = -90.0f, float pitch = 0);
|
||||
|
||||
glm::mat4 getViewMatrix();
|
||||
void ProccessKeyboard(camera_movement direction, float deltaTime);
|
||||
void ProccessMouse(float xoffset, float yoffset, bool constrainPitch = true);
|
||||
void ProccessScroll(float yoffset);
|
||||
void SetCameraMode(camera_mode mode);
|
||||
|
||||
private:
|
||||
float lastY;
|
||||
float lastX;
|
||||
|
||||
void updateCameraVectors();
|
||||
};
|
||||
|
||||
100
lessons/2. Lighting/1. Colors/src/Shader.cpp
Normal file
100
lessons/2. Lighting/1. Colors/src/Shader.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#include "Shader.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <glad/glad.h>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
Shader::Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath)
|
||||
{
|
||||
std::string v = readFile(vertexShaderSourcePath);
|
||||
std::string f = readFile(fragmentShaderSourcePath);
|
||||
const char *vertexSource = v.c_str();
|
||||
const char *fragmentSource = f.c_str();
|
||||
|
||||
int success;
|
||||
char infoLog[512];
|
||||
|
||||
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertexShader, 1, &vertexSource, NULL);
|
||||
glCompileShader(vertexShader);
|
||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Vertex shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
|
||||
glCompileShader(fragmentShader);
|
||||
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Fragment shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
Program = glCreateProgram();
|
||||
glAttachShader(Program, vertexShader);
|
||||
glAttachShader(Program, fragmentShader);
|
||||
glLinkProgram(Program);
|
||||
glGetProgramiv(Program, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(Program, 512, NULL, infoLog);
|
||||
std::cerr << "[ERROR] Program linking failed\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
}
|
||||
Shader::Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath)
|
||||
{
|
||||
Shader(vertexShaderSourcePath.c_str(), fragmentShaderSourcePath.c_str());
|
||||
}
|
||||
|
||||
std::string Shader::readFile(const char* path)
|
||||
{
|
||||
std::string out;
|
||||
try
|
||||
{
|
||||
std::ifstream ifs(path); // input file stream
|
||||
std::stringstream buffer;
|
||||
buffer << ifs.rdbuf();
|
||||
out = buffer.str();
|
||||
}
|
||||
catch (std::ifstream::failure e)
|
||||
{
|
||||
std::cerr << "[ERROR] Could not read/open file '" << path << "'\n";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
void Shader::Use()
|
||||
{
|
||||
glUseProgram(Program);
|
||||
}
|
||||
void Shader::setBool(const std::string name, bool value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value ? 1 : 0);
|
||||
}
|
||||
void Shader::setFloat(const std::string name, float value) const
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setInt(const std::string name, int value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setMat4(const std::string name, glm::mat4 value) const
|
||||
{
|
||||
glUniformMatrix4fv(glGetUniformLocation(Program, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
|
||||
}
|
||||
void Shader::setVec3(const std::string name, glm::vec3 value) const
|
||||
{
|
||||
setVec3(name, value.x, value.y, value.z);
|
||||
}
|
||||
void Shader::setVec3(const std::string name, float v0, float v1, float v2) const
|
||||
{
|
||||
glUniform3f(glGetUniformLocation(Program, name.c_str()), v0, v1, v2);
|
||||
}
|
||||
24
lessons/2. Lighting/1. Colors/src/Shader.h
Normal file
24
lessons/2. Lighting/1. Colors/src/Shader.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
unsigned int Program;
|
||||
|
||||
Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath);
|
||||
Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath);
|
||||
|
||||
void Use();
|
||||
|
||||
void setBool(const std::string name, bool value) const;
|
||||
void setFloat(const std::string name, float value) const;
|
||||
void setInt(const std::string name, int value) const;
|
||||
void setMat4(const std::string name, glm::mat4 value) const;
|
||||
void setVec3(const std::string name, glm::vec3 value) const;
|
||||
void setVec3(const std::string name, float v0, float v1, float v2) const;
|
||||
private:
|
||||
std::string readFile(const char* path);
|
||||
};
|
||||
|
||||
278
lessons/2. Lighting/1. Colors/src/main.cpp
Normal file
278
lessons/2. Lighting/1. Colors/src/main.cpp
Normal file
@@ -0,0 +1,278 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <stb_image.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include "Shader.h"
|
||||
#include "Camera.h"
|
||||
|
||||
#define TEXTURES_DIR "../../../../textures/"
|
||||
#define SHADERS_DIR "../src/shaders/"
|
||||
|
||||
using namespace std;
|
||||
typedef unsigned int uint;
|
||||
|
||||
// Global Constants
|
||||
// ...
|
||||
// Global Variables
|
||||
GLFWwindow* window;
|
||||
int WINDOW_WIDTH = 800;
|
||||
int WINDOW_HEIGHT = 600;
|
||||
Camera myCamera(glm::vec3(0.0f, 0.0f, 3.0f));
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
float lastX = 400;
|
||||
float lastY = 300;
|
||||
bool firstmouse = true;
|
||||
glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
|
||||
glm::vec3 lightColor(1.0f, 1.0f, 1.0f);
|
||||
|
||||
uint8_t init(void);
|
||||
void framebuffersize_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 proccessInput(GLFWwindow* window);
|
||||
unsigned int load_texture(const char* path, bool flip = true);
|
||||
|
||||
int main(int* argc, char** argv)
|
||||
{
|
||||
if (init() != 0)
|
||||
return -1;
|
||||
|
||||
Shader lightingShader(SHADERS_DIR "vertexShader.vert", SHADERS_DIR "fragmentShader.frag");
|
||||
Shader lightSourceShader(SHADERS_DIR "lightSource.vert", SHADERS_DIR "lightSource.frag");
|
||||
|
||||
float vertices[] = {
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||
|
||||
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
|
||||
};
|
||||
|
||||
uint cubeVAO; // Vertex array buffer
|
||||
glGenVertexArrays(1, &cubeVAO);
|
||||
glBindVertexArray(cubeVAO);
|
||||
|
||||
uint VBO; // Vertex Buffer Object
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // copy data to Graphics card array buffer (buffer for vertices)
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (const void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (const void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
uint lightSourceVAO;
|
||||
glGenVertexArrays(1, &lightSourceVAO);
|
||||
glBindVertexArray(lightSourceVAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (const void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
/* ------------------ MAIN loop ------------------ */
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
float currentFrame = glfwGetTime();
|
||||
deltaTime = currentFrame - lastFrame; // cas jak dlouho trval posledni frame
|
||||
lastFrame = currentFrame; // cas kdy zacal tento frame
|
||||
// input ...
|
||||
proccessInput(window);
|
||||
|
||||
// rendering commands here ...
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glm::mat4 model(1.0f);
|
||||
model = glm::translate(model, glm::vec3(0.0f));
|
||||
glm::mat4 view = myCamera.getViewMatrix();
|
||||
glm::mat4 projection(1.0f);
|
||||
projection = glm::perspective(glm::radians(myCamera.FOV), (float)WINDOW_WIDTH / WINDOW_HEIGHT, 0.1f, 100.0f);
|
||||
|
||||
lightingShader.Use();
|
||||
lightingShader.setMat4("model", model);
|
||||
lightingShader.setMat4("view", view);
|
||||
lightingShader.setMat4("projection", projection);
|
||||
lightingShader.setVec3("objectColor", 1.0f, 0.5f, 0.31f); // color the object reflects
|
||||
lightingShader.setVec3("lightColor", lightColor); // color of the light source
|
||||
|
||||
glBindVertexArray(cubeVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
|
||||
|
||||
/* ----------- Light Source ----------- */
|
||||
model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, lightPos);
|
||||
model = glm::scale(model, glm::vec3(0.2f));
|
||||
|
||||
lightSourceShader.Use();
|
||||
glm::mat4 PVM = projection * view * model;
|
||||
lightSourceShader.setMat4("PVM", PVM);
|
||||
lightSourceShader.setVec3("lightColor", lightColor);
|
||||
|
||||
glBindVertexArray(lightSourceVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
|
||||
// check and calls events and swap the buffers
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glDeleteVertexArrays(1, &lightSourceVAO);
|
||||
glDeleteVertexArrays(1, &cubeVAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t init(void) {
|
||||
// init of glfw window
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "1. Colors", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
cerr << "Failed to create glfw window" << endl;
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
|
||||
// init of GLAD
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
cerr << "Failed to initialize GLAD" << endl;
|
||||
}
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffersize_callback);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
return 0;
|
||||
}
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
WINDOW_HEIGHT = height;
|
||||
WINDOW_WIDTH = width;
|
||||
}
|
||||
void proccessInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
if (glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
/* -------------------- Movement -------------------- */
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_FORWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_BACKWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_RIGHT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_LEFT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_DOWN, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_UP, deltaTime);
|
||||
|
||||
}
|
||||
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;
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
|
||||
myCamera.ProccessMouse(xoffset, yoffset);
|
||||
}
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
||||
{
|
||||
myCamera.ProccessScroll(yoffset);
|
||||
}
|
||||
unsigned int load_texture(const char* path, bool flip)
|
||||
{
|
||||
unsigned int texture = 0;
|
||||
int width, height, nrChannels, format;
|
||||
stbi_set_flip_vertically_on_load(flip);
|
||||
unsigned char* data = stbi_load(path, &width, &height, &nrChannels, 0); // load image to the array of bytes (char = 1 byte)
|
||||
|
||||
switch (nrChannels)
|
||||
{
|
||||
case 4: format = GL_RGBA; break;
|
||||
case 3: format = GL_RGB; break;
|
||||
case 2: format = GL_RED; break;
|
||||
default: format = GL_RGB; break;
|
||||
}
|
||||
|
||||
if (data) // data != NULL
|
||||
{
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
|
||||
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);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, data); // generates texture
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
else {
|
||||
cerr << "Could not load texture '" << path << "'\n";
|
||||
}
|
||||
stbi_image_free(data);
|
||||
|
||||
return texture;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec3 objectColor;
|
||||
uniform vec3 lightColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(lightColor * objectColor, 1.0f);
|
||||
}
|
||||
10
lessons/2. Lighting/1. Colors/src/shaders/lightSource.frag
Normal file
10
lessons/2. Lighting/1. Colors/src/shaders/lightSource.frag
Normal file
@@ -0,0 +1,10 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec3 lightColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(lightColor, 1.0f);
|
||||
}
|
||||
10
lessons/2. Lighting/1. Colors/src/shaders/lightSource.vert
Normal file
10
lessons/2. Lighting/1. Colors/src/shaders/lightSource.vert
Normal file
@@ -0,0 +1,10 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 aPos;
|
||||
|
||||
uniform mat4 PVM;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = PVM * vec4(aPos.xyz, 1.0);
|
||||
}
|
||||
12
lessons/2. Lighting/1. Colors/src/shaders/vertexShader.vert
Normal file
12
lessons/2. Lighting/1. Colors/src/shaders/vertexShader.vert
Normal file
@@ -0,0 +1,12 @@
|
||||
#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.xyz, 1.0);
|
||||
}
|
||||
37
lessons/2. Lighting/2. Basic Lighting/Makefile
Normal file
37
lessons/2. Lighting/2. Basic Lighting/Makefile
Normal file
@@ -0,0 +1,37 @@
|
||||
NAME := main
|
||||
CXX=g++
|
||||
DEBUG :=
|
||||
CXX_FLAGS := $(DEBUG) -std=c++14
|
||||
LIBS := -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -pthread -lXi -ldl -lm
|
||||
INCLUDE := ../../../include/
|
||||
BIN := bin
|
||||
SRC := src
|
||||
INPUT = src/main.cpp ../../../glad.c
|
||||
|
||||
all: $(BIN)/$(NAME)
|
||||
|
||||
run:
|
||||
cd $(BIN) && ./$(NAME)
|
||||
|
||||
$(BIN)/$(NAME): $(SRC)/main.o $(SRC)/glad.o $(SRC)/Shader.o $(SRC)/stb_image.o $(SRC)/Camera.o
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) $^ -o $@ $(LIBS)
|
||||
|
||||
.cpp.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
.c.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/glad.o: ../../../glad.c
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/stb_image.o: ../../../stb_image.cpp
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
# dependencies
|
||||
$(SRC)/main.o: $(SRC)/main.cpp
|
||||
$(SRC)/Shader.o: $(SRC)/Shader.cpp
|
||||
|
||||
clean:
|
||||
rm -rf $(SRC)/*.o
|
||||
touch $(SRC)/*
|
||||
83
lessons/2. Lighting/2. Basic Lighting/src/Camera.cpp
Normal file
83
lessons/2. Lighting/2. Basic Lighting/src/Camera.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include "Camera.h"
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
Camera::Camera(glm::vec3 position, glm::vec3 worldUp, glm::vec3 worldFront, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(2.5f), MouseSensitivity(0.001f), FOV(45.0f), Mode(CAM_FLOAT)
|
||||
{
|
||||
this->Position = position;
|
||||
this->WorldUp = worldUp;
|
||||
this->Yaw = yaw;
|
||||
this->Pitch = pitch;
|
||||
this->WorldFront = worldFront;
|
||||
this->WorldRight = glm::normalize(glm::cross(this->WorldFront, this->WorldUp));
|
||||
|
||||
updateCameraVectors();
|
||||
}
|
||||
void Camera::updateCameraVectors()
|
||||
{
|
||||
glm::vec3 front;
|
||||
front.x = cos(Yaw) * cos(Pitch);
|
||||
front.y = sin(Pitch);
|
||||
front.z = sin(Yaw) * cos(Pitch);
|
||||
Front = glm::normalize(front);
|
||||
|
||||
Right = glm::normalize(glm::cross(Front, WorldUp));
|
||||
Up = glm::normalize(glm::cross(Right, Front));
|
||||
}
|
||||
glm::mat4 Camera::getViewMatrix()
|
||||
{
|
||||
return glm::lookAt(Position, Position + Front, WorldUp);
|
||||
}
|
||||
void Camera::ProccessKeyboard(camera_movement direction, float deltaTime)
|
||||
{
|
||||
switch (direction)
|
||||
{
|
||||
case CAM_FORWARD:
|
||||
Position += deltaTime * Front;
|
||||
break;
|
||||
case CAM_BACKWARD:
|
||||
Position -= deltaTime * Front;
|
||||
break;
|
||||
case CAM_LEFT:
|
||||
Position -= deltaTime * Right;
|
||||
break;
|
||||
case CAM_RIGHT:
|
||||
Position += deltaTime * Right;
|
||||
break;
|
||||
case CAM_UP:
|
||||
Position += deltaTime * WorldUp;
|
||||
break;
|
||||
case CAM_DOWN:
|
||||
Position -= deltaTime * WorldUp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
void Camera::ProccessMouse(float xoffset, float yoffset, bool constrainPitch)
|
||||
{
|
||||
xoffset *= MouseSensitivity;
|
||||
yoffset *= MouseSensitivity;
|
||||
|
||||
Yaw = fmodf(Yaw + xoffset, 360.0f);
|
||||
Pitch += yoffset;
|
||||
|
||||
if (constrainPitch)
|
||||
{
|
||||
if (Pitch > 89.0f)
|
||||
Pitch = 89.0f;
|
||||
else if (Pitch < -89.0f)
|
||||
Pitch = -89.0f;
|
||||
}
|
||||
|
||||
updateCameraVectors();
|
||||
}
|
||||
void Camera::ProccessScroll(float yoffset)
|
||||
{
|
||||
FOV -= yoffset;
|
||||
if (FOV < 1.0f)
|
||||
FOV = 1.0f;
|
||||
else if (FOV > 45.0f)
|
||||
FOV = 45.0f;
|
||||
}
|
||||
void Camera::SetCameraMode(camera_mode mode)
|
||||
{
|
||||
Mode = mode;
|
||||
}
|
||||
54
lessons/2. Lighting/2. Basic Lighting/src/Camera.h
Normal file
54
lessons/2. Lighting/2. Basic Lighting/src/Camera.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
enum camera_movement {
|
||||
CAM_FORWARD,
|
||||
CAM_BACKWARD,
|
||||
CAM_LEFT,
|
||||
CAM_RIGHT,
|
||||
CAM_UP,
|
||||
CAM_DOWN
|
||||
};
|
||||
|
||||
enum camera_mode {
|
||||
CAM_FLOAT,
|
||||
CAM_MINECRAFT
|
||||
};
|
||||
|
||||
class Camera
|
||||
{
|
||||
public:
|
||||
float FOV;
|
||||
float MovementSpeed;
|
||||
float MouseSensitivity;
|
||||
float Yaw; // vlevo vpravo
|
||||
float Pitch; // nahoru dolu
|
||||
|
||||
camera_mode Mode;
|
||||
|
||||
glm::vec3 Position;
|
||||
glm::vec3 Front;
|
||||
glm::vec3 Right;
|
||||
glm::vec3 Up;
|
||||
glm::vec3 WorldUp;
|
||||
glm::vec3 WorldRight;
|
||||
glm::vec3 WorldFront;
|
||||
|
||||
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f),
|
||||
glm::vec3 worldUp = glm::vec3(0.0f, 1.0f, 0.0f),
|
||||
glm::vec3 worldFront = glm::vec3(0.0f, 0.0f, -1.0),
|
||||
float yaw = -90.0f, float pitch = 0);
|
||||
|
||||
glm::mat4 getViewMatrix();
|
||||
void ProccessKeyboard(camera_movement direction, float deltaTime);
|
||||
void ProccessMouse(float xoffset, float yoffset, bool constrainPitch = true);
|
||||
void ProccessScroll(float yoffset);
|
||||
void SetCameraMode(camera_mode mode);
|
||||
|
||||
private:
|
||||
float lastY;
|
||||
float lastX;
|
||||
|
||||
void updateCameraVectors();
|
||||
};
|
||||
|
||||
119
lessons/2. Lighting/2. Basic Lighting/src/Shader.cpp
Normal file
119
lessons/2. Lighting/2. Basic Lighting/src/Shader.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
#include "Shader.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <glad/glad.h>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
Shader::Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath, bool directSource)
|
||||
{
|
||||
const char* vertexSource;
|
||||
const char* fragmentSource;
|
||||
std::string v, f;
|
||||
if (!directSource)
|
||||
{
|
||||
v = readFile(vertexShaderSourcePath);
|
||||
f = readFile(fragmentShaderSourcePath);
|
||||
vertexSource = v.c_str();
|
||||
fragmentSource = f.c_str();
|
||||
}
|
||||
else
|
||||
{
|
||||
vertexSource = vertexShaderSourcePath;
|
||||
fragmentSource = fragmentShaderSourcePath;
|
||||
}
|
||||
|
||||
int success;
|
||||
char infoLog[512];
|
||||
|
||||
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertexShader, 1, &vertexSource, NULL);
|
||||
glCompileShader(vertexShader);
|
||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Vertex shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
|
||||
glCompileShader(fragmentShader);
|
||||
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Fragment shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
Program = glCreateProgram();
|
||||
glAttachShader(Program, vertexShader);
|
||||
glAttachShader(Program, fragmentShader);
|
||||
glLinkProgram(Program);
|
||||
glGetProgramiv(Program, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(Program, 512, NULL, infoLog);
|
||||
std::cerr << "[ERROR] Program linking failed\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
}
|
||||
Shader::Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath, bool directSource)
|
||||
{
|
||||
Shader(vertexShaderSourcePath.c_str(), fragmentShaderSourcePath.c_str(), directSource);
|
||||
}
|
||||
Shader::~Shader()
|
||||
{
|
||||
glDeleteProgram(Program);
|
||||
}
|
||||
|
||||
std::string Shader::readFile(const char* path)
|
||||
{
|
||||
std::string out;
|
||||
try
|
||||
{
|
||||
std::ifstream ifs(path); // input file stream
|
||||
std::stringstream buffer;
|
||||
buffer << ifs.rdbuf();
|
||||
out = buffer.str();
|
||||
}
|
||||
catch (std::ifstream::failure e)
|
||||
{
|
||||
std::cerr << "[ERROR] Could not read/open file '" << path << "'\n";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
void Shader::Use()
|
||||
{
|
||||
glUseProgram(Program);
|
||||
}
|
||||
void Shader::setBool(const std::string name, bool value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value ? 1 : 0);
|
||||
}
|
||||
void Shader::setFloat(const std::string name, float value) const
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setInt(const std::string name, int value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setMat4(const std::string name, glm::mat4 value) const
|
||||
{
|
||||
glUniformMatrix4fv(glGetUniformLocation(Program, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
|
||||
}
|
||||
void Shader::setVec3(const std::string name, glm::vec3 value) const
|
||||
{
|
||||
setVec3(name, value.x, value.y, value.z);
|
||||
}
|
||||
void Shader::setVec3(const std::string name, float v0, float v1, float v2) const
|
||||
{
|
||||
glUniform3f(glGetUniformLocation(Program, name.c_str()), v0, v1, v2);
|
||||
}
|
||||
void Shader::setMat3(const std::string name, glm::mat3 value) const
|
||||
{
|
||||
glUniformMatrix3fv(glGetUniformLocation(Program, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
|
||||
}
|
||||
27
lessons/2. Lighting/2. Basic Lighting/src/Shader.h
Normal file
27
lessons/2. Lighting/2. Basic Lighting/src/Shader.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
unsigned int Program;
|
||||
|
||||
Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath, bool directSource = false);
|
||||
Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath, bool directSource = false);
|
||||
~Shader();
|
||||
|
||||
|
||||
void Use();
|
||||
|
||||
void setBool(const std::string name, bool value) const;
|
||||
void setFloat(const std::string name, float value) const;
|
||||
void setInt(const std::string name, int value) const;
|
||||
void setMat4(const std::string name, glm::mat4 value) const;
|
||||
void setVec3(const std::string name, glm::vec3 value) const;
|
||||
void setVec3(const std::string name, float v0, float v1, float v2) const;
|
||||
void setMat3(const std::string name, glm::mat3 value) const;
|
||||
|
||||
static std::string readFile(const char* path);
|
||||
};
|
||||
|
||||
288
lessons/2. Lighting/2. Basic Lighting/src/main.cpp
Normal file
288
lessons/2. Lighting/2. Basic Lighting/src/main.cpp
Normal file
@@ -0,0 +1,288 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <stb_image.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include "Shader.h"
|
||||
#include "Camera.h"
|
||||
//#include <DebugDrawer.h>
|
||||
|
||||
#define TEXTURES_DIR "../../../../textures/"
|
||||
#define SHADERS_DIR "../src/shaders/"
|
||||
|
||||
using namespace std;
|
||||
typedef unsigned int uint;
|
||||
|
||||
// Global Constants
|
||||
// ...
|
||||
// Global Variables
|
||||
GLFWwindow* window;
|
||||
int WINDOW_WIDTH = 800;
|
||||
int WINDOW_HEIGHT = 600;
|
||||
Camera myCamera(glm::vec3(0.0f, 0.0f, 3.0f));
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
float lastX = 400;
|
||||
float lastY = 300;
|
||||
bool firstmouse = true;
|
||||
glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
|
||||
glm::vec3 lightColor(1.0f, 1.0f, 1.0f);
|
||||
|
||||
uint8_t init(void);
|
||||
void framebuffersize_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 proccessInput(GLFWwindow* window);
|
||||
unsigned int load_texture(const char* path, bool flip = true);
|
||||
|
||||
int main(int* argc, char** argv)
|
||||
{
|
||||
if (init() != 0)
|
||||
return -1;
|
||||
|
||||
// DebugDrawer debugDrawer;
|
||||
|
||||
Shader lightingShader(SHADERS_DIR "vertexShader.vert", SHADERS_DIR "fragmentShader.frag");
|
||||
Shader lightSourceShader(SHADERS_DIR "lightSource.vert", SHADERS_DIR "lightSource.frag");
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
uint cubeVAO; // Vertex array buffer
|
||||
glGenVertexArrays(1, &cubeVAO);
|
||||
glBindVertexArray(cubeVAO);
|
||||
|
||||
uint VBO; // Vertex Buffer Object
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // copy data to Graphics card array buffer (buffer for vertices)
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (const void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (const void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
uint lightSourceVAO;
|
||||
glGenVertexArrays(1, &lightSourceVAO);
|
||||
glBindVertexArray(lightSourceVAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (const void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
/* ------------------ MAIN loop ------------------ */
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
float currentFrame = glfwGetTime();
|
||||
deltaTime = currentFrame - lastFrame; // cas jak dlouho trval posledni frame
|
||||
lastFrame = currentFrame; // cas kdy zacal tento frame
|
||||
// input ...
|
||||
proccessInput(window);
|
||||
|
||||
// rendering commands here ...
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glm::mat4 model(1.0f);
|
||||
model = glm::translate(model, glm::vec3(0.0f));
|
||||
|
||||
glm::mat4 view = myCamera.getViewMatrix();
|
||||
glm::mat4 projection(1.0f);
|
||||
projection = glm::perspective(glm::radians(myCamera.FOV), (float)WINDOW_WIDTH / WINDOW_HEIGHT, 0.1f, 100.0f);
|
||||
glm::mat3 normalMatrix = glm::transpose(glm::inverse(model));
|
||||
|
||||
lightingShader.Use();
|
||||
lightingShader.setMat4("model", model);
|
||||
lightingShader.setMat4("view", view);
|
||||
lightingShader.setMat4("projection", projection);
|
||||
lightingShader.setMat3("normalMatrix", normalMatrix);
|
||||
lightPos.x = cos(glfwGetTime() * 0.5) * 3.0f;
|
||||
lightPos.z = sin(glfwGetTime() * 0.5) * 3.0f;
|
||||
lightingShader.setVec3("objectColor", 1.0f, 0.5f, 0.31f); // color the object reflects
|
||||
lightingShader.setVec3("lightColor", lightColor); // color of the light source
|
||||
lightingShader.setVec3("lightPos", lightPos);
|
||||
lightingShader.setVec3("viewPos", myCamera.Position);
|
||||
|
||||
glBindVertexArray(cubeVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
|
||||
/* ----------- Light Source ----------- */
|
||||
model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, lightPos);
|
||||
model = glm::scale(model, glm::vec3(0.2f));
|
||||
|
||||
lightSourceShader.Use();
|
||||
glm::mat4 PVM = projection * view * model;
|
||||
lightSourceShader.setMat4("PVM", PVM);
|
||||
lightSourceShader.setVec3("lightColor", lightColor);
|
||||
|
||||
glBindVertexArray(lightSourceVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
|
||||
// debugDrawer.DrawCoordLines(projection * view);
|
||||
// check and calls events and swap the buffers
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glDeleteVertexArrays(1, &lightSourceVAO);
|
||||
glDeleteVertexArrays(1, &cubeVAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t init(void) {
|
||||
// init of glfw window
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "1. Colors", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
cerr << "Failed to create glfw window" << endl;
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
|
||||
// init of GLAD
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
cerr << "Failed to initialize GLAD" << endl;
|
||||
}
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffersize_callback);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
return 0;
|
||||
}
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
WINDOW_HEIGHT = height;
|
||||
WINDOW_WIDTH = width;
|
||||
}
|
||||
void proccessInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
if (glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
/* -------------------- Movement -------------------- */
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_FORWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_BACKWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_RIGHT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_LEFT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_DOWN, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_UP, deltaTime);
|
||||
|
||||
}
|
||||
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;
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
|
||||
myCamera.ProccessMouse(xoffset, yoffset);
|
||||
}
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
||||
{
|
||||
myCamera.ProccessScroll(yoffset);
|
||||
}
|
||||
unsigned int load_texture(const char* path, bool flip)
|
||||
{
|
||||
unsigned int texture = 0;
|
||||
int width, height, nrChannels, format;
|
||||
stbi_set_flip_vertically_on_load(flip);
|
||||
unsigned char* data = stbi_load(path, &width, &height, &nrChannels, 0); // load image to the array of bytes (char = 1 byte)
|
||||
|
||||
switch (nrChannels)
|
||||
{
|
||||
case 4: format = GL_RGBA; break;
|
||||
case 3: format = GL_RGB; break;
|
||||
case 2: format = GL_RED; break;
|
||||
default: format = GL_RGB; break;
|
||||
}
|
||||
|
||||
if (data) // data != NULL
|
||||
{
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
|
||||
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);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, data); // generates texture
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
else {
|
||||
cerr << "Could not load texture '" << path << "'\n";
|
||||
}
|
||||
stbi_image_free(data);
|
||||
|
||||
return texture;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec3 objectColor;
|
||||
uniform vec3 lightColor;
|
||||
|
||||
uniform vec3 lightPos;
|
||||
uniform vec3 viewPos;
|
||||
|
||||
in vec3 Normal;
|
||||
in vec3 FragPos;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 lightDir = normalize(lightPos - FragPos); // direction towards light source
|
||||
vec3 normal = normalize(Normal);
|
||||
|
||||
float ambientStrength = 0.1;
|
||||
vec3 ambient = ambientStrength * lightColor;
|
||||
|
||||
float diff = max(dot(lightDir, normal), 0.0f);
|
||||
vec3 diffuse = diff * objectColor;
|
||||
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
float spec = pow(max(dot(normalize(viewPos - FragPos), reflectDir), 0.0), 32.0);
|
||||
float specularStrength = 0.5;
|
||||
vec3 specular = specularStrength * spec * objectColor;
|
||||
|
||||
vec3 result = (ambient + diffuse + specular) * objectColor;
|
||||
FragColor = vec4(result, 1.0f);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec3 lightColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(lightColor, 1.0f);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 aPos;
|
||||
|
||||
uniform mat4 PVM;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = PVM * vec4(aPos.xyz, 1.0);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aNormal;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
uniform mat3 normalMatrix;
|
||||
|
||||
out vec3 Normal;
|
||||
out vec3 FragPos;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(aPos.xyz, 1.0);
|
||||
Normal = normalMatrix * aNormal;
|
||||
FragPos = vec3(model * vec4(aPos, 1.0));
|
||||
}
|
||||
37
lessons/2. Lighting/3. Materials/Makefile
Normal file
37
lessons/2. Lighting/3. Materials/Makefile
Normal file
@@ -0,0 +1,37 @@
|
||||
NAME := main
|
||||
CXX=g++
|
||||
DEBUG :=
|
||||
CXX_FLAGS := $(DEBUG) -std=c++14
|
||||
LIBS := -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -pthread -lXi -ldl -lm
|
||||
INCLUDE := ../../../include/
|
||||
BIN := bin
|
||||
SRC := src
|
||||
INPUT = src/main.cpp ../../../glad.c
|
||||
|
||||
all: $(BIN)/$(NAME)
|
||||
|
||||
run:
|
||||
cd $(BIN) && ./$(NAME)
|
||||
|
||||
$(BIN)/$(NAME): $(SRC)/main.o $(SRC)/glad.o $(SRC)/Shader.o $(SRC)/stb_image.o $(SRC)/Camera.o
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) $^ -o $@ $(LIBS)
|
||||
|
||||
.cpp.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
.c.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/glad.o: ../../../glad.c
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/stb_image.o: ../../../stb_image.cpp
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
# dependencies
|
||||
$(SRC)/main.o: $(SRC)/main.cpp
|
||||
$(SRC)/Shader.o: $(SRC)/Shader.cpp
|
||||
|
||||
clean:
|
||||
rm -rf $(SRC)/*.o
|
||||
touch $(SRC)/*
|
||||
83
lessons/2. Lighting/3. Materials/src/Camera.cpp
Normal file
83
lessons/2. Lighting/3. Materials/src/Camera.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include "Camera.h"
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
Camera::Camera(glm::vec3 position, glm::vec3 worldUp, glm::vec3 worldFront, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(2.5f), MouseSensitivity(0.001f), FOV(45.0f), Mode(CAM_FLOAT)
|
||||
{
|
||||
this->Position = position;
|
||||
this->WorldUp = worldUp;
|
||||
this->Yaw = yaw;
|
||||
this->Pitch = pitch;
|
||||
this->WorldFront = worldFront;
|
||||
this->WorldRight = glm::normalize(glm::cross(this->WorldFront, this->WorldUp));
|
||||
|
||||
updateCameraVectors();
|
||||
}
|
||||
void Camera::updateCameraVectors()
|
||||
{
|
||||
glm::vec3 front;
|
||||
front.x = cos(Yaw) * cos(Pitch);
|
||||
front.y = sin(Pitch);
|
||||
front.z = sin(Yaw) * cos(Pitch);
|
||||
Front = glm::normalize(front);
|
||||
|
||||
Right = glm::normalize(glm::cross(Front, WorldUp));
|
||||
Up = glm::normalize(glm::cross(Right, Front));
|
||||
}
|
||||
glm::mat4 Camera::getViewMatrix()
|
||||
{
|
||||
return glm::lookAt(Position, Position + Front, WorldUp);
|
||||
}
|
||||
void Camera::ProccessKeyboard(camera_movement direction, float deltaTime)
|
||||
{
|
||||
switch (direction)
|
||||
{
|
||||
case CAM_FORWARD:
|
||||
Position += deltaTime * Front;
|
||||
break;
|
||||
case CAM_BACKWARD:
|
||||
Position -= deltaTime * Front;
|
||||
break;
|
||||
case CAM_LEFT:
|
||||
Position -= deltaTime * Right;
|
||||
break;
|
||||
case CAM_RIGHT:
|
||||
Position += deltaTime * Right;
|
||||
break;
|
||||
case CAM_UP:
|
||||
Position += deltaTime * WorldUp;
|
||||
break;
|
||||
case CAM_DOWN:
|
||||
Position -= deltaTime * WorldUp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
void Camera::ProccessMouse(float xoffset, float yoffset, bool constrainPitch)
|
||||
{
|
||||
xoffset *= MouseSensitivity;
|
||||
yoffset *= MouseSensitivity;
|
||||
|
||||
Yaw = fmodf(Yaw + xoffset, 360.0f);
|
||||
Pitch += yoffset;
|
||||
|
||||
if (constrainPitch)
|
||||
{
|
||||
if (Pitch > 89.0f)
|
||||
Pitch = 89.0f;
|
||||
else if (Pitch < -89.0f)
|
||||
Pitch = -89.0f;
|
||||
}
|
||||
|
||||
updateCameraVectors();
|
||||
}
|
||||
void Camera::ProccessScroll(float yoffset)
|
||||
{
|
||||
FOV -= yoffset;
|
||||
if (FOV < 1.0f)
|
||||
FOV = 1.0f;
|
||||
else if (FOV > 45.0f)
|
||||
FOV = 45.0f;
|
||||
}
|
||||
void Camera::SetCameraMode(camera_mode mode)
|
||||
{
|
||||
Mode = mode;
|
||||
}
|
||||
54
lessons/2. Lighting/3. Materials/src/Camera.h
Normal file
54
lessons/2. Lighting/3. Materials/src/Camera.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
enum camera_movement {
|
||||
CAM_FORWARD,
|
||||
CAM_BACKWARD,
|
||||
CAM_LEFT,
|
||||
CAM_RIGHT,
|
||||
CAM_UP,
|
||||
CAM_DOWN
|
||||
};
|
||||
|
||||
enum camera_mode {
|
||||
CAM_FLOAT,
|
||||
CAM_MINECRAFT
|
||||
};
|
||||
|
||||
class Camera
|
||||
{
|
||||
public:
|
||||
float FOV;
|
||||
float MovementSpeed;
|
||||
float MouseSensitivity;
|
||||
float Yaw; // vlevo vpravo
|
||||
float Pitch; // nahoru dolu
|
||||
|
||||
camera_mode Mode;
|
||||
|
||||
glm::vec3 Position;
|
||||
glm::vec3 Front;
|
||||
glm::vec3 Right;
|
||||
glm::vec3 Up;
|
||||
glm::vec3 WorldUp;
|
||||
glm::vec3 WorldRight;
|
||||
glm::vec3 WorldFront;
|
||||
|
||||
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f),
|
||||
glm::vec3 worldUp = glm::vec3(0.0f, 1.0f, 0.0f),
|
||||
glm::vec3 worldFront = glm::vec3(0.0f, 0.0f, -1.0),
|
||||
float yaw = -90.0f, float pitch = 0);
|
||||
|
||||
glm::mat4 getViewMatrix();
|
||||
void ProccessKeyboard(camera_movement direction, float deltaTime);
|
||||
void ProccessMouse(float xoffset, float yoffset, bool constrainPitch = true);
|
||||
void ProccessScroll(float yoffset);
|
||||
void SetCameraMode(camera_mode mode);
|
||||
|
||||
private:
|
||||
float lastY;
|
||||
float lastX;
|
||||
|
||||
void updateCameraVectors();
|
||||
};
|
||||
|
||||
119
lessons/2. Lighting/3. Materials/src/Shader.cpp
Normal file
119
lessons/2. Lighting/3. Materials/src/Shader.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
#include "Shader.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <glad/glad.h>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
Shader::Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath, bool directSource)
|
||||
{
|
||||
const char* vertexSource;
|
||||
const char* fragmentSource;
|
||||
std::string v, f;
|
||||
if (!directSource)
|
||||
{
|
||||
v = readFile(vertexShaderSourcePath);
|
||||
f = readFile(fragmentShaderSourcePath);
|
||||
vertexSource = v.c_str();
|
||||
fragmentSource = f.c_str();
|
||||
}
|
||||
else
|
||||
{
|
||||
vertexSource = vertexShaderSourcePath;
|
||||
fragmentSource = fragmentShaderSourcePath;
|
||||
}
|
||||
|
||||
int success;
|
||||
char infoLog[512];
|
||||
|
||||
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertexShader, 1, &vertexSource, NULL);
|
||||
glCompileShader(vertexShader);
|
||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Vertex shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
|
||||
glCompileShader(fragmentShader);
|
||||
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Fragment shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
Program = glCreateProgram();
|
||||
glAttachShader(Program, vertexShader);
|
||||
glAttachShader(Program, fragmentShader);
|
||||
glLinkProgram(Program);
|
||||
glGetProgramiv(Program, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(Program, 512, NULL, infoLog);
|
||||
std::cerr << "[ERROR] Program linking failed\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
}
|
||||
Shader::Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath, bool directSource)
|
||||
{
|
||||
Shader(vertexShaderSourcePath.c_str(), fragmentShaderSourcePath.c_str(), directSource);
|
||||
}
|
||||
Shader::~Shader()
|
||||
{
|
||||
glDeleteProgram(Program);
|
||||
}
|
||||
|
||||
std::string Shader::readFile(const char* path)
|
||||
{
|
||||
std::string out;
|
||||
try
|
||||
{
|
||||
std::ifstream ifs(path); // input file stream
|
||||
std::stringstream buffer;
|
||||
buffer << ifs.rdbuf();
|
||||
out = buffer.str();
|
||||
}
|
||||
catch (std::ifstream::failure e)
|
||||
{
|
||||
std::cerr << "[ERROR] Could not read/open file '" << path << "'\n";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
void Shader::Use()
|
||||
{
|
||||
glUseProgram(Program);
|
||||
}
|
||||
void Shader::setBool(const std::string name, bool value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value ? 1 : 0);
|
||||
}
|
||||
void Shader::setFloat(const std::string name, float value) const
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setInt(const std::string name, int value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setMat4(const std::string name, glm::mat4 value) const
|
||||
{
|
||||
glUniformMatrix4fv(glGetUniformLocation(Program, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
|
||||
}
|
||||
void Shader::setVec3(const std::string name, glm::vec3 value) const
|
||||
{
|
||||
setVec3(name, value.x, value.y, value.z);
|
||||
}
|
||||
void Shader::setVec3(const std::string name, float v0, float v1, float v2) const
|
||||
{
|
||||
glUniform3f(glGetUniformLocation(Program, name.c_str()), v0, v1, v2);
|
||||
}
|
||||
void Shader::setMat3(const std::string name, glm::mat3 value) const
|
||||
{
|
||||
glUniformMatrix3fv(glGetUniformLocation(Program, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
|
||||
}
|
||||
27
lessons/2. Lighting/3. Materials/src/Shader.h
Normal file
27
lessons/2. Lighting/3. Materials/src/Shader.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
unsigned int Program;
|
||||
|
||||
Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath, bool directSource = false);
|
||||
Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath, bool directSource = false);
|
||||
~Shader();
|
||||
|
||||
|
||||
void Use();
|
||||
|
||||
void setBool(const std::string name, bool value) const;
|
||||
void setFloat(const std::string name, float value) const;
|
||||
void setInt(const std::string name, int value) const;
|
||||
void setMat4(const std::string name, glm::mat4 value) const;
|
||||
void setVec3(const std::string name, glm::vec3 value) const;
|
||||
void setVec3(const std::string name, float v0, float v1, float v2) const;
|
||||
void setMat3(const std::string name, glm::mat3 value) const;
|
||||
|
||||
static std::string readFile(const char* path);
|
||||
};
|
||||
|
||||
292
lessons/2. Lighting/3. Materials/src/main.cpp
Normal file
292
lessons/2. Lighting/3. Materials/src/main.cpp
Normal file
@@ -0,0 +1,292 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <stb_image.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include "Shader.h"
|
||||
#include "Camera.h"
|
||||
|
||||
#define TEXTURES_DIR "../../../../textures/"
|
||||
#define SHADERS_DIR "../src/shaders/"
|
||||
|
||||
using namespace std;
|
||||
typedef unsigned int uint;
|
||||
|
||||
// Global Constants
|
||||
// ...
|
||||
// Global Variables
|
||||
GLFWwindow* window;
|
||||
int WINDOW_WIDTH = 800;
|
||||
int WINDOW_HEIGHT = 600;
|
||||
Camera myCamera(glm::vec3(0.0f, 0.0f, 3.0f));
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
float lastX = 400;
|
||||
float lastY = 300;
|
||||
bool firstmouse = true;
|
||||
glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
|
||||
glm::vec3 lightColor(1.0f, 1.0f, 1.0f);
|
||||
|
||||
uint8_t init(void);
|
||||
void framebuffersize_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 proccessInput(GLFWwindow* window);
|
||||
unsigned int load_texture(const char* path, bool flip = true);
|
||||
|
||||
int main(int* argc, char** argv)
|
||||
{
|
||||
if (init() != 0)
|
||||
return -1;
|
||||
|
||||
Shader lightingShader(SHADERS_DIR "vertexShader.vert", SHADERS_DIR "fragmentShader.frag");
|
||||
Shader lightSourceShader(SHADERS_DIR "lightSource.vert", SHADERS_DIR "lightSource.frag");
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
uint cubeVAO; // Vertex array buffer
|
||||
glGenVertexArrays(1, &cubeVAO);
|
||||
glBindVertexArray(cubeVAO);
|
||||
|
||||
uint VBO; // Vertex Buffer Object
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // copy data to Graphics card array buffer (buffer for vertices)
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (const void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (const void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
uint lightSourceVAO;
|
||||
glGenVertexArrays(1, &lightSourceVAO);
|
||||
glBindVertexArray(lightSourceVAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (const void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
/* ------------------ MAIN loop ------------------ */
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
float currentFrame = glfwGetTime();
|
||||
deltaTime = currentFrame - lastFrame; // cas jak dlouho trval posledni frame
|
||||
lastFrame = currentFrame; // cas kdy zacal tento frame
|
||||
// input ...
|
||||
proccessInput(window);
|
||||
|
||||
// rendering commands here ...
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glm::mat4 model(1.0f);
|
||||
model = glm::translate(model, glm::vec3(0.0f));
|
||||
|
||||
glm::mat4 view = myCamera.getViewMatrix();
|
||||
glm::mat4 projection(1.0f);
|
||||
projection = glm::perspective(glm::radians(myCamera.FOV), (float)WINDOW_WIDTH / WINDOW_HEIGHT, 0.1f, 100.0f);
|
||||
glm::mat3 normalMatrix = glm::transpose(glm::inverse(model));
|
||||
|
||||
lightingShader.Use();
|
||||
lightingShader.setMat4("model", model);
|
||||
lightingShader.setMat4("view", view);
|
||||
lightingShader.setMat4("projection", projection);
|
||||
lightingShader.setMat3("normalMatrix", normalMatrix);
|
||||
//lightPos.x = cos(glfwGetTime() * 0.5) * 3.0f;
|
||||
//lightPos.z = sin(glfwGetTime() * 0.5) * 3.0f;
|
||||
lightingShader.setVec3("material.ambient_color", glm::vec3(1.0f, 0.5f, 0.31f));
|
||||
lightingShader.setVec3("material.diffuse_color", glm::vec3(1.0f, 0.5f, 0.31f));
|
||||
lightingShader.setVec3("material.specular_color", glm::vec3(0.5f, 0.5f, 0.5f));
|
||||
lightingShader.setFloat("material.shininess", 32.0f);
|
||||
|
||||
lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
|
||||
glm::vec3 light_diffuse_color = lightColor * glm::vec3(0.5f);
|
||||
lightingShader.setVec3("light.position", lightPos);
|
||||
lightingShader.setVec3("light.ambient_intensity", light_diffuse_color * glm::vec3(0.2f));
|
||||
lightingShader.setVec3("light.diffuse_intensity", light_diffuse_color);
|
||||
lightingShader.setVec3("light.specular_intensity", glm::vec3(1.0f));
|
||||
lightingShader.setVec3("viewPos", myCamera.Position);
|
||||
|
||||
glBindVertexArray(cubeVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
|
||||
/* ----------- Light Source ----------- */
|
||||
model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, lightPos);
|
||||
model = glm::scale(model, glm::vec3(0.2f));
|
||||
|
||||
lightSourceShader.Use();
|
||||
glm::mat4 PVM = projection * view * model;
|
||||
lightSourceShader.setMat4("PVM", PVM);
|
||||
lightSourceShader.setVec3("lightColor", lightColor);
|
||||
|
||||
glBindVertexArray(lightSourceVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
|
||||
// check and calls events and swap the buffers
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glDeleteVertexArrays(1, &lightSourceVAO);
|
||||
glDeleteVertexArrays(1, &cubeVAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t init(void) {
|
||||
// init of glfw window
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "1. Colors", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
cerr << "Failed to create glfw window" << endl;
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
|
||||
// init of GLAD
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
cerr << "Failed to initialize GLAD" << endl;
|
||||
}
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffersize_callback);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
return 0;
|
||||
}
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
WINDOW_HEIGHT = height;
|
||||
WINDOW_WIDTH = width;
|
||||
}
|
||||
void proccessInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
if (glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
/* -------------------- Movement -------------------- */
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_FORWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_BACKWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_RIGHT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_LEFT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_DOWN, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_UP, deltaTime);
|
||||
|
||||
}
|
||||
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;
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
|
||||
myCamera.ProccessMouse(xoffset, yoffset);
|
||||
}
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
||||
{
|
||||
myCamera.ProccessScroll(yoffset);
|
||||
}
|
||||
unsigned int load_texture(const char* path, bool flip)
|
||||
{
|
||||
unsigned int texture = 0;
|
||||
int width, height, nrChannels, format;
|
||||
stbi_set_flip_vertically_on_load(flip);
|
||||
unsigned char* data = stbi_load(path, &width, &height, &nrChannels, 0); // load image to the array of bytes (char = 1 byte)
|
||||
|
||||
switch (nrChannels)
|
||||
{
|
||||
case 4: format = GL_RGBA; break;
|
||||
case 3: format = GL_RGB; break;
|
||||
case 2: format = GL_RED; break;
|
||||
default: format = GL_RGB; break;
|
||||
}
|
||||
|
||||
if (data) // data != NULL
|
||||
{
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
|
||||
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);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, data); // generates texture
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
else {
|
||||
cerr << "Could not load texture '" << path << "'\n";
|
||||
}
|
||||
stbi_image_free(data);
|
||||
|
||||
return texture;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#version 330 core
|
||||
|
||||
struct Material {
|
||||
vec3 ambient_color;
|
||||
vec3 diffuse_color;
|
||||
vec3 specular_color;
|
||||
|
||||
float shininess;
|
||||
};
|
||||
struct Light {
|
||||
vec3 position;
|
||||
|
||||
vec3 ambient_intensity;
|
||||
vec3 diffuse_intensity;
|
||||
vec3 specular_intensity;
|
||||
};
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec3 viewPos;
|
||||
uniform Material material;
|
||||
uniform Light light;
|
||||
|
||||
in vec3 Normal;
|
||||
in vec3 FragPos;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 lightDir = normalize(light.position - FragPos); // direction towards light source
|
||||
vec3 normal = normalize(Normal);
|
||||
|
||||
float ambientStrength = 0.1;
|
||||
vec3 ambient = light.ambient_intensity * material.ambient_color;
|
||||
|
||||
float diff = max(dot(lightDir, normal), 0.0f);
|
||||
vec3 diffuse = light.diffuse_intensity * material.diffuse_color * diff;
|
||||
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
float spec = pow(max(dot(normalize(viewPos - FragPos), reflectDir), 0.0), material.shininess);
|
||||
float specularStrength = 0.5;
|
||||
vec3 specular = light.specular_intensity * material.specular_color * spec;
|
||||
|
||||
vec3 result = ambient + diffuse + specular;
|
||||
FragColor = vec4(result, 1.0f);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec3 lightColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(lightColor, 1.0f);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 aPos;
|
||||
|
||||
uniform mat4 PVM;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = PVM * vec4(aPos.xyz, 1.0);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aNormal;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
uniform mat3 normalMatrix;
|
||||
|
||||
out vec3 Normal;
|
||||
out vec3 FragPos;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(aPos.xyz, 1.0);
|
||||
Normal = normalMatrix * aNormal;
|
||||
FragPos = vec3(model * vec4(aPos, 1.0));
|
||||
}
|
||||
37
lessons/2. Lighting/4. Lighting Maps/Makefile
Normal file
37
lessons/2. Lighting/4. Lighting Maps/Makefile
Normal file
@@ -0,0 +1,37 @@
|
||||
NAME := main
|
||||
CXX=g++
|
||||
DEBUG :=
|
||||
CXX_FLAGS := $(DEBUG) -std=c++14
|
||||
LIBS := -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -pthread -lXi -ldl -lm
|
||||
INCLUDE := ../../../include/
|
||||
BIN := bin
|
||||
SRC := src
|
||||
INPUT = src/main.cpp ../../../glad.c
|
||||
|
||||
all: $(BIN)/$(NAME)
|
||||
|
||||
run:
|
||||
cd $(BIN) && ./$(NAME)
|
||||
|
||||
$(BIN)/$(NAME): $(SRC)/main.o $(SRC)/glad.o $(SRC)/Shader.o $(SRC)/stb_image.o $(SRC)/Camera.o
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) $^ -o $@ $(LIBS)
|
||||
|
||||
.cpp.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
.c.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/glad.o: ../../../glad.c
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/stb_image.o: ../../../stb_image.cpp
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
# dependencies
|
||||
$(SRC)/main.o: $(SRC)/main.cpp
|
||||
$(SRC)/Shader.o: $(SRC)/Shader.cpp
|
||||
|
||||
clean:
|
||||
rm -rf $(SRC)/*.o
|
||||
touch $(SRC)/*
|
||||
83
lessons/2. Lighting/4. Lighting Maps/src/Camera.cpp
Normal file
83
lessons/2. Lighting/4. Lighting Maps/src/Camera.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include "Camera.h"
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
Camera::Camera(glm::vec3 position, glm::vec3 worldUp, glm::vec3 worldFront, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(2.5f), MouseSensitivity(0.001f), FOV(45.0f), Mode(CAM_FLOAT)
|
||||
{
|
||||
this->Position = position;
|
||||
this->WorldUp = worldUp;
|
||||
this->Yaw = yaw;
|
||||
this->Pitch = pitch;
|
||||
this->WorldFront = worldFront;
|
||||
this->WorldRight = glm::normalize(glm::cross(this->WorldFront, this->WorldUp));
|
||||
|
||||
updateCameraVectors();
|
||||
}
|
||||
void Camera::updateCameraVectors()
|
||||
{
|
||||
glm::vec3 front;
|
||||
front.x = cos(Yaw) * cos(Pitch);
|
||||
front.y = sin(Pitch);
|
||||
front.z = sin(Yaw) * cos(Pitch);
|
||||
Front = glm::normalize(front);
|
||||
|
||||
Right = glm::normalize(glm::cross(Front, WorldUp));
|
||||
Up = glm::normalize(glm::cross(Right, Front));
|
||||
}
|
||||
glm::mat4 Camera::getViewMatrix()
|
||||
{
|
||||
return glm::lookAt(Position, Position + Front, WorldUp);
|
||||
}
|
||||
void Camera::ProccessKeyboard(camera_movement direction, float deltaTime)
|
||||
{
|
||||
switch (direction)
|
||||
{
|
||||
case CAM_FORWARD:
|
||||
Position += deltaTime * Front;
|
||||
break;
|
||||
case CAM_BACKWARD:
|
||||
Position -= deltaTime * Front;
|
||||
break;
|
||||
case CAM_LEFT:
|
||||
Position -= deltaTime * Right;
|
||||
break;
|
||||
case CAM_RIGHT:
|
||||
Position += deltaTime * Right;
|
||||
break;
|
||||
case CAM_UP:
|
||||
Position += deltaTime * WorldUp;
|
||||
break;
|
||||
case CAM_DOWN:
|
||||
Position -= deltaTime * WorldUp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
void Camera::ProccessMouse(float xoffset, float yoffset, bool constrainPitch)
|
||||
{
|
||||
xoffset *= MouseSensitivity;
|
||||
yoffset *= MouseSensitivity;
|
||||
|
||||
Yaw = fmodf(Yaw + xoffset, 360.0f);
|
||||
Pitch += yoffset;
|
||||
|
||||
if (constrainPitch)
|
||||
{
|
||||
if (Pitch > 89.0f)
|
||||
Pitch = 89.0f;
|
||||
else if (Pitch < -89.0f)
|
||||
Pitch = -89.0f;
|
||||
}
|
||||
|
||||
updateCameraVectors();
|
||||
}
|
||||
void Camera::ProccessScroll(float yoffset)
|
||||
{
|
||||
FOV -= yoffset;
|
||||
if (FOV < 1.0f)
|
||||
FOV = 1.0f;
|
||||
else if (FOV > 45.0f)
|
||||
FOV = 45.0f;
|
||||
}
|
||||
void Camera::SetCameraMode(camera_mode mode)
|
||||
{
|
||||
Mode = mode;
|
||||
}
|
||||
54
lessons/2. Lighting/4. Lighting Maps/src/Camera.h
Normal file
54
lessons/2. Lighting/4. Lighting Maps/src/Camera.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
enum camera_movement {
|
||||
CAM_FORWARD,
|
||||
CAM_BACKWARD,
|
||||
CAM_LEFT,
|
||||
CAM_RIGHT,
|
||||
CAM_UP,
|
||||
CAM_DOWN
|
||||
};
|
||||
|
||||
enum camera_mode {
|
||||
CAM_FLOAT,
|
||||
CAM_MINECRAFT
|
||||
};
|
||||
|
||||
class Camera
|
||||
{
|
||||
public:
|
||||
float FOV;
|
||||
float MovementSpeed;
|
||||
float MouseSensitivity;
|
||||
float Yaw; // vlevo vpravo
|
||||
float Pitch; // nahoru dolu
|
||||
|
||||
camera_mode Mode;
|
||||
|
||||
glm::vec3 Position;
|
||||
glm::vec3 Front;
|
||||
glm::vec3 Right;
|
||||
glm::vec3 Up;
|
||||
glm::vec3 WorldUp;
|
||||
glm::vec3 WorldRight;
|
||||
glm::vec3 WorldFront;
|
||||
|
||||
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f),
|
||||
glm::vec3 worldUp = glm::vec3(0.0f, 1.0f, 0.0f),
|
||||
glm::vec3 worldFront = glm::vec3(0.0f, 0.0f, -1.0),
|
||||
float yaw = -90.0f, float pitch = 0);
|
||||
|
||||
glm::mat4 getViewMatrix();
|
||||
void ProccessKeyboard(camera_movement direction, float deltaTime);
|
||||
void ProccessMouse(float xoffset, float yoffset, bool constrainPitch = true);
|
||||
void ProccessScroll(float yoffset);
|
||||
void SetCameraMode(camera_mode mode);
|
||||
|
||||
private:
|
||||
float lastY;
|
||||
float lastX;
|
||||
|
||||
void updateCameraVectors();
|
||||
};
|
||||
|
||||
119
lessons/2. Lighting/4. Lighting Maps/src/Shader.cpp
Normal file
119
lessons/2. Lighting/4. Lighting Maps/src/Shader.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
#include "Shader.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <glad/glad.h>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
Shader::Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath, bool directSource)
|
||||
{
|
||||
const char* vertexSource;
|
||||
const char* fragmentSource;
|
||||
std::string v, f;
|
||||
if (!directSource)
|
||||
{
|
||||
v = readFile(vertexShaderSourcePath);
|
||||
f = readFile(fragmentShaderSourcePath);
|
||||
vertexSource = v.c_str();
|
||||
fragmentSource = f.c_str();
|
||||
}
|
||||
else
|
||||
{
|
||||
vertexSource = vertexShaderSourcePath;
|
||||
fragmentSource = fragmentShaderSourcePath;
|
||||
}
|
||||
|
||||
int success;
|
||||
char infoLog[512];
|
||||
|
||||
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertexShader, 1, &vertexSource, NULL);
|
||||
glCompileShader(vertexShader);
|
||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Vertex shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
|
||||
glCompileShader(fragmentShader);
|
||||
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Fragment shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
Program = glCreateProgram();
|
||||
glAttachShader(Program, vertexShader);
|
||||
glAttachShader(Program, fragmentShader);
|
||||
glLinkProgram(Program);
|
||||
glGetProgramiv(Program, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(Program, 512, NULL, infoLog);
|
||||
std::cerr << "[ERROR] Program linking failed\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
}
|
||||
Shader::Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath, bool directSource)
|
||||
{
|
||||
Shader(vertexShaderSourcePath.c_str(), fragmentShaderSourcePath.c_str(), directSource);
|
||||
}
|
||||
Shader::~Shader()
|
||||
{
|
||||
glDeleteProgram(Program);
|
||||
}
|
||||
|
||||
std::string Shader::readFile(const char* path)
|
||||
{
|
||||
std::string out;
|
||||
try
|
||||
{
|
||||
std::ifstream ifs(path); // input file stream
|
||||
std::stringstream buffer;
|
||||
buffer << ifs.rdbuf();
|
||||
out = buffer.str();
|
||||
}
|
||||
catch (std::ifstream::failure e)
|
||||
{
|
||||
std::cerr << "[ERROR] Could not read/open file '" << path << "'\n";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
void Shader::Use()
|
||||
{
|
||||
glUseProgram(Program);
|
||||
}
|
||||
void Shader::setBool(const std::string name, bool value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value ? 1 : 0);
|
||||
}
|
||||
void Shader::setFloat(const std::string name, float value) const
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setInt(const std::string name, int value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setMat4(const std::string name, glm::mat4 value) const
|
||||
{
|
||||
glUniformMatrix4fv(glGetUniformLocation(Program, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
|
||||
}
|
||||
void Shader::setVec3(const std::string name, glm::vec3 value) const
|
||||
{
|
||||
setVec3(name, value.x, value.y, value.z);
|
||||
}
|
||||
void Shader::setVec3(const std::string name, float v0, float v1, float v2) const
|
||||
{
|
||||
glUniform3f(glGetUniformLocation(Program, name.c_str()), v0, v1, v2);
|
||||
}
|
||||
void Shader::setMat3(const std::string name, glm::mat3 value) const
|
||||
{
|
||||
glUniformMatrix3fv(glGetUniformLocation(Program, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
|
||||
}
|
||||
27
lessons/2. Lighting/4. Lighting Maps/src/Shader.h
Normal file
27
lessons/2. Lighting/4. Lighting Maps/src/Shader.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
unsigned int Program;
|
||||
|
||||
Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath, bool directSource = false);
|
||||
Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath, bool directSource = false);
|
||||
~Shader();
|
||||
|
||||
|
||||
void Use();
|
||||
|
||||
void setBool(const std::string name, bool value) const;
|
||||
void setFloat(const std::string name, float value) const;
|
||||
void setInt(const std::string name, int value) const;
|
||||
void setMat4(const std::string name, glm::mat4 value) const;
|
||||
void setVec3(const std::string name, glm::vec3 value) const;
|
||||
void setVec3(const std::string name, float v0, float v1, float v2) const;
|
||||
void setMat3(const std::string name, glm::mat3 value) const;
|
||||
|
||||
static std::string readFile(const char* path);
|
||||
};
|
||||
|
||||
309
lessons/2. Lighting/4. Lighting Maps/src/main.cpp
Normal file
309
lessons/2. Lighting/4. Lighting Maps/src/main.cpp
Normal file
@@ -0,0 +1,309 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <stb_image.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include "Shader.h"
|
||||
#include "Camera.h"
|
||||
|
||||
#define TEXTURES_DIR "../../../../textures/"
|
||||
#define SHADERS_DIR "../src/shaders/"
|
||||
|
||||
using namespace std;
|
||||
typedef unsigned int uint;
|
||||
|
||||
// Global Constants
|
||||
// ...
|
||||
// Global Variables
|
||||
GLFWwindow* window;
|
||||
int WINDOW_WIDTH = 800;
|
||||
int WINDOW_HEIGHT = 600;
|
||||
Camera myCamera(glm::vec3(0.0f, 0.0f, 3.0f));
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
float lastX = 400;
|
||||
float lastY = 300;
|
||||
bool firstmouse = true;
|
||||
glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
|
||||
glm::vec3 lightColor(1.0f, 1.0f, 1.0f);
|
||||
|
||||
uint8_t init(void);
|
||||
void framebuffersize_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 proccessInput(GLFWwindow* window);
|
||||
unsigned int load_texture(const char* path, bool flip = true);
|
||||
|
||||
int main(int* argc, char** argv)
|
||||
{
|
||||
if (init() != 0)
|
||||
return -1;
|
||||
|
||||
Shader lightingShader(SHADERS_DIR "vertexShader.vert", SHADERS_DIR "fragmentShader.frag");
|
||||
Shader lightSourceShader(SHADERS_DIR "lightSource.vert", SHADERS_DIR "lightSource.frag");
|
||||
|
||||
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
uint cubeVAO; // Vertex array buffer
|
||||
glGenVertexArrays(1, &cubeVAO);
|
||||
glBindVertexArray(cubeVAO);
|
||||
|
||||
uint VBO; // Vertex Buffer Object
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // copy data to Graphics card array buffer (buffer for vertices)
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)(6 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
uint lightSourceVAO;
|
||||
glGenVertexArrays(1, &lightSourceVAO);
|
||||
glBindVertexArray(lightSourceVAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
uint container_texture = load_texture(TEXTURES_DIR "container2.png");
|
||||
uint container2_specular = load_texture(TEXTURES_DIR "container2_specular.png");
|
||||
uint matrix_texture = load_texture(TEXTURES_DIR "matrix.jpg");
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
/* ------------------ MAIN loop ------------------ */
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
float currentFrame = glfwGetTime();
|
||||
deltaTime = currentFrame - lastFrame; // cas jak dlouho trval posledni frame
|
||||
lastFrame = currentFrame; // cas kdy zacal tento frame
|
||||
// input ...
|
||||
proccessInput(window);
|
||||
|
||||
// rendering commands here ...
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glm::mat4 model(1.0f);
|
||||
model = glm::translate(model, glm::vec3(0.0f));
|
||||
|
||||
glm::mat4 view = myCamera.getViewMatrix();
|
||||
glm::mat4 projection(1.0f);
|
||||
projection = glm::perspective(glm::radians(myCamera.FOV), (float)WINDOW_WIDTH / WINDOW_HEIGHT, 0.1f, 100.0f);
|
||||
glm::mat3 normalMatrix = glm::transpose(glm::inverse(model));
|
||||
|
||||
lightingShader.Use();
|
||||
lightingShader.setMat4("model", model);
|
||||
lightingShader.setMat4("view", view);
|
||||
lightingShader.setMat4("projection", projection);
|
||||
lightingShader.setMat3("normalMatrix", normalMatrix);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, container_texture);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, container2_specular);
|
||||
glActiveTexture(GL_TEXTURE2);
|
||||
glBindTexture(GL_TEXTURE_2D, matrix_texture);
|
||||
|
||||
lightingShader.setFloat("time", (float)glfwGetTime() * 2.0f);
|
||||
|
||||
lightingShader.setInt("emission_map", 2);
|
||||
lightingShader.setInt("material.diffuse_map", 0);
|
||||
lightingShader.setInt("material.specular_map", 1);
|
||||
lightingShader.setFloat("material.shininess", 64.0f);
|
||||
|
||||
lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
|
||||
glm::vec3 light_diffuse_color = lightColor * glm::vec3(0.6f);
|
||||
lightingShader.setVec3("light.position", lightPos);
|
||||
lightingShader.setVec3("light.ambient_intensity", light_diffuse_color * glm::vec3(0.1f));
|
||||
lightingShader.setVec3("light.diffuse_intensity", light_diffuse_color);
|
||||
lightingShader.setVec3("light.specular_intensity", lightColor * glm::vec3(1.0f));
|
||||
lightingShader.setVec3("viewPos", myCamera.Position);
|
||||
|
||||
glBindVertexArray(cubeVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
|
||||
/* ----------- Light Source ----------- */
|
||||
model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, lightPos);
|
||||
model = glm::scale(model, glm::vec3(0.2f));
|
||||
|
||||
lightSourceShader.Use();
|
||||
glm::mat4 PVM = projection * view * model;
|
||||
lightSourceShader.setMat4("PVM", PVM);
|
||||
lightSourceShader.setVec3("lightColor", lightColor);
|
||||
|
||||
glBindVertexArray(lightSourceVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
|
||||
// check and calls events and swap the buffers
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glDeleteVertexArrays(1, &lightSourceVAO);
|
||||
glDeleteVertexArrays(1, &cubeVAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t init(void) {
|
||||
// init of glfw window
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "4. Lighting Maps", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
cerr << "Failed to create glfw window" << endl;
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
|
||||
// init of GLAD
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
cerr << "Failed to initialize GLAD" << endl;
|
||||
}
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffersize_callback);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
return 0;
|
||||
}
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
WINDOW_HEIGHT = height;
|
||||
WINDOW_WIDTH = width;
|
||||
}
|
||||
void proccessInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
if (glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
/* -------------------- Movement -------------------- */
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_FORWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_BACKWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_RIGHT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_LEFT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_DOWN, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_UP, deltaTime);
|
||||
|
||||
}
|
||||
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;
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
|
||||
myCamera.ProccessMouse(xoffset, yoffset);
|
||||
}
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
||||
{
|
||||
myCamera.ProccessScroll(yoffset);
|
||||
}
|
||||
unsigned int load_texture(const char* path, bool flip)
|
||||
{
|
||||
unsigned int texture = 0;
|
||||
int width, height, nrChannels, format;
|
||||
stbi_set_flip_vertically_on_load(flip);
|
||||
unsigned char* data = stbi_load(path, &width, &height, &nrChannels, 0); // load image to the array of bytes (char = 1 byte)
|
||||
|
||||
switch (nrChannels)
|
||||
{
|
||||
case 4: format = GL_RGBA; break;
|
||||
case 3: format = GL_RGB; break;
|
||||
case 2: format = GL_RED; break;
|
||||
default: format = GL_RGB; break;
|
||||
}
|
||||
|
||||
if (data) // data != NULL
|
||||
{
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
|
||||
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);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, data); // generates texture
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
else {
|
||||
cerr << "Could not load texture '" << path << "'\n";
|
||||
}
|
||||
stbi_image_free(data);
|
||||
|
||||
return texture;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
#version 330 core
|
||||
|
||||
struct Material {
|
||||
sampler2D diffuse_map;
|
||||
sampler2D specular_map;
|
||||
|
||||
float shininess;
|
||||
};
|
||||
struct Light {
|
||||
vec3 position;
|
||||
|
||||
vec3 ambient_intensity;
|
||||
vec3 diffuse_intensity;
|
||||
vec3 specular_intensity;
|
||||
};
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec3 viewPos;
|
||||
uniform Material material;
|
||||
uniform Light light;
|
||||
uniform sampler2D emission_map;
|
||||
|
||||
in vec3 Normal;
|
||||
in vec3 FragPos;
|
||||
in vec2 TexCoord;
|
||||
|
||||
uniform float time;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 lightDir = normalize(light.position - FragPos); // direction towards light source
|
||||
vec3 normal = normalize(Normal);
|
||||
|
||||
vec3 ambient = light.ambient_intensity * vec3(texture(material.diffuse_map, TexCoord));
|
||||
|
||||
float diff = max(dot(lightDir, normal), 0.0f);
|
||||
vec3 diffuse = light.diffuse_intensity * diff * vec3(texture(material.diffuse_map, TexCoord));
|
||||
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
float spec = pow(max(dot(normalize(viewPos - FragPos), reflectDir), 0.0), material.shininess);
|
||||
vec3 specular = light.specular_intensity * spec * vec3(texture(material.specular_map, TexCoord));
|
||||
|
||||
vec3 emission = vec3(0.0);
|
||||
if (vec3(texture(material.specular_map, TexCoord)) == vec3(0.0))
|
||||
emission = vec3(texture(emission_map, TexCoord + vec2(0.0, time)));
|
||||
|
||||
vec3 result = ambient + diffuse + specular + emission;
|
||||
FragColor = vec4(result, 1.0f);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec3 lightColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(lightColor, 1.0f);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 aPos;
|
||||
|
||||
uniform mat4 PVM;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = PVM * vec4(aPos.xyz, 1.0);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aNormal;
|
||||
layout (location = 2) in vec2 aTexCoord;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
uniform mat3 normalMatrix;
|
||||
|
||||
out vec3 Normal;
|
||||
out vec3 FragPos;
|
||||
out vec2 TexCoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(aPos.xyz, 1.0);
|
||||
Normal = normalMatrix * aNormal;
|
||||
FragPos = vec3(model * vec4(aPos, 1.0));
|
||||
TexCoord = aTexCoord;
|
||||
}
|
||||
37
lessons/2. Lighting/5. Light casters/Makefile
Normal file
37
lessons/2. Lighting/5. Light casters/Makefile
Normal file
@@ -0,0 +1,37 @@
|
||||
NAME := main
|
||||
CXX=g++
|
||||
DEBUG :=
|
||||
CXX_FLAGS := $(DEBUG) -std=c++14
|
||||
LIBS := -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -pthread -lXi -ldl -lm
|
||||
INCLUDE := ../../../include/
|
||||
BIN := bin
|
||||
SRC := src
|
||||
INPUT = src/main.cpp ../../../glad.c
|
||||
|
||||
all: $(BIN)/$(NAME)
|
||||
|
||||
run:
|
||||
cd $(BIN) && ./$(NAME)
|
||||
|
||||
$(BIN)/$(NAME): $(SRC)/main.o $(SRC)/glad.o $(SRC)/Shader.o $(SRC)/stb_image.o $(SRC)/Camera.o
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) $^ -o $@ $(LIBS)
|
||||
|
||||
.cpp.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
.c.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/glad.o: ../../../glad.c
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/stb_image.o: ../../../stb_image.cpp
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
# dependencies
|
||||
$(SRC)/main.o: $(SRC)/main.cpp
|
||||
$(SRC)/Shader.o: $(SRC)/Shader.cpp
|
||||
|
||||
clean:
|
||||
rm -rf $(SRC)/*.o
|
||||
touch $(SRC)/*
|
||||
83
lessons/2. Lighting/5. Light casters/src/Camera.cpp
Normal file
83
lessons/2. Lighting/5. Light casters/src/Camera.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include "Camera.h"
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
Camera::Camera(glm::vec3 position, glm::vec3 worldUp, glm::vec3 worldFront, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(2.5f), MouseSensitivity(0.001f), FOV(45.0f), Mode(CAM_FLOAT)
|
||||
{
|
||||
this->Position = position;
|
||||
this->WorldUp = worldUp;
|
||||
this->Yaw = yaw;
|
||||
this->Pitch = pitch;
|
||||
this->WorldFront = worldFront;
|
||||
this->WorldRight = glm::normalize(glm::cross(this->WorldFront, this->WorldUp));
|
||||
|
||||
updateCameraVectors();
|
||||
}
|
||||
void Camera::updateCameraVectors()
|
||||
{
|
||||
glm::vec3 front;
|
||||
front.x = cos(Yaw) * cos(Pitch);
|
||||
front.y = sin(Pitch);
|
||||
front.z = sin(Yaw) * cos(Pitch);
|
||||
Front = glm::normalize(front);
|
||||
|
||||
Right = glm::normalize(glm::cross(Front, WorldUp));
|
||||
Up = glm::normalize(glm::cross(Right, Front));
|
||||
}
|
||||
glm::mat4 Camera::getViewMatrix()
|
||||
{
|
||||
return glm::lookAt(Position, Position + Front, WorldUp);
|
||||
}
|
||||
void Camera::ProccessKeyboard(camera_movement direction, float deltaTime)
|
||||
{
|
||||
switch (direction)
|
||||
{
|
||||
case CAM_FORWARD:
|
||||
Position += deltaTime * MovementSpeed * Front;
|
||||
break;
|
||||
case CAM_BACKWARD:
|
||||
Position -= deltaTime * MovementSpeed * Front;
|
||||
break;
|
||||
case CAM_LEFT:
|
||||
Position -= deltaTime * MovementSpeed * Right;
|
||||
break;
|
||||
case CAM_RIGHT:
|
||||
Position += deltaTime * MovementSpeed * Right;
|
||||
break;
|
||||
case CAM_UP:
|
||||
Position += deltaTime * MovementSpeed * WorldUp;
|
||||
break;
|
||||
case CAM_DOWN:
|
||||
Position -= deltaTime * MovementSpeed * WorldUp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
void Camera::ProccessMouse(float xoffset, float yoffset, bool constrainPitch)
|
||||
{
|
||||
xoffset *= MouseSensitivity;
|
||||
yoffset *= MouseSensitivity;
|
||||
|
||||
Yaw = fmodf(Yaw + xoffset, 360.0f);
|
||||
Pitch += yoffset;
|
||||
|
||||
if (constrainPitch)
|
||||
{
|
||||
if (Pitch > 89.0f)
|
||||
Pitch = 89.0f;
|
||||
else if (Pitch < -89.0f)
|
||||
Pitch = -89.0f;
|
||||
}
|
||||
|
||||
updateCameraVectors();
|
||||
}
|
||||
void Camera::ProccessScroll(float yoffset)
|
||||
{
|
||||
FOV -= yoffset;
|
||||
if (FOV < 1.0f)
|
||||
FOV = 1.0f;
|
||||
else if (FOV > 45.0f)
|
||||
FOV = 45.0f;
|
||||
}
|
||||
void Camera::SetCameraMode(camera_mode mode)
|
||||
{
|
||||
Mode = mode;
|
||||
}
|
||||
54
lessons/2. Lighting/5. Light casters/src/Camera.h
Normal file
54
lessons/2. Lighting/5. Light casters/src/Camera.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
enum camera_movement {
|
||||
CAM_FORWARD,
|
||||
CAM_BACKWARD,
|
||||
CAM_LEFT,
|
||||
CAM_RIGHT,
|
||||
CAM_UP,
|
||||
CAM_DOWN
|
||||
};
|
||||
|
||||
enum camera_mode {
|
||||
CAM_FLOAT,
|
||||
CAM_MINECRAFT
|
||||
};
|
||||
|
||||
class Camera
|
||||
{
|
||||
public:
|
||||
float FOV;
|
||||
float MovementSpeed;
|
||||
float MouseSensitivity;
|
||||
float Yaw; // vlevo vpravo
|
||||
float Pitch; // nahoru dolu
|
||||
|
||||
camera_mode Mode;
|
||||
|
||||
glm::vec3 Position;
|
||||
glm::vec3 Front;
|
||||
glm::vec3 Right;
|
||||
glm::vec3 Up;
|
||||
glm::vec3 WorldUp;
|
||||
glm::vec3 WorldRight;
|
||||
glm::vec3 WorldFront;
|
||||
|
||||
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f),
|
||||
glm::vec3 worldUp = glm::vec3(0.0f, 1.0f, 0.0f),
|
||||
glm::vec3 worldFront = glm::vec3(0.0f, 0.0f, -1.0),
|
||||
float yaw = -90.0f, float pitch = 0);
|
||||
|
||||
glm::mat4 getViewMatrix();
|
||||
void ProccessKeyboard(camera_movement direction, float deltaTime);
|
||||
void ProccessMouse(float xoffset, float yoffset, bool constrainPitch = true);
|
||||
void ProccessScroll(float yoffset);
|
||||
void SetCameraMode(camera_mode mode);
|
||||
|
||||
private:
|
||||
float lastY;
|
||||
float lastX;
|
||||
|
||||
void updateCameraVectors();
|
||||
};
|
||||
|
||||
119
lessons/2. Lighting/5. Light casters/src/Shader.cpp
Normal file
119
lessons/2. Lighting/5. Light casters/src/Shader.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
#include "Shader.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <glad/glad.h>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
Shader::Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath, bool directSource)
|
||||
{
|
||||
const char* vertexSource;
|
||||
const char* fragmentSource;
|
||||
std::string v, f;
|
||||
if (!directSource)
|
||||
{
|
||||
v = readFile(vertexShaderSourcePath);
|
||||
f = readFile(fragmentShaderSourcePath);
|
||||
vertexSource = v.c_str();
|
||||
fragmentSource = f.c_str();
|
||||
}
|
||||
else
|
||||
{
|
||||
vertexSource = vertexShaderSourcePath;
|
||||
fragmentSource = fragmentShaderSourcePath;
|
||||
}
|
||||
|
||||
int success;
|
||||
char infoLog[512];
|
||||
|
||||
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertexShader, 1, &vertexSource, NULL);
|
||||
glCompileShader(vertexShader);
|
||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Vertex shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
|
||||
glCompileShader(fragmentShader);
|
||||
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
||||
std::cerr << "Shader.h: [ERROR] Fragment shader compilation failed!\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
Program = glCreateProgram();
|
||||
glAttachShader(Program, vertexShader);
|
||||
glAttachShader(Program, fragmentShader);
|
||||
glLinkProgram(Program);
|
||||
glGetProgramiv(Program, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(Program, 512, NULL, infoLog);
|
||||
std::cerr << "[ERROR] Program linking failed\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
}
|
||||
Shader::Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath, bool directSource)
|
||||
{
|
||||
Shader(vertexShaderSourcePath.c_str(), fragmentShaderSourcePath.c_str(), directSource);
|
||||
}
|
||||
Shader::~Shader()
|
||||
{
|
||||
glDeleteProgram(Program);
|
||||
}
|
||||
|
||||
std::string Shader::readFile(const char* path)
|
||||
{
|
||||
std::string out;
|
||||
try
|
||||
{
|
||||
std::ifstream ifs(path); // input file stream
|
||||
std::stringstream buffer;
|
||||
buffer << ifs.rdbuf();
|
||||
out = buffer.str();
|
||||
}
|
||||
catch (std::ifstream::failure e)
|
||||
{
|
||||
std::cerr << "[ERROR] Could not read/open file '" << path << "'\n";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
void Shader::Use()
|
||||
{
|
||||
glUseProgram(Program);
|
||||
}
|
||||
void Shader::setBool(const std::string name, bool value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value ? 1 : 0);
|
||||
}
|
||||
void Shader::setFloat(const std::string name, float value) const
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setInt(const std::string name, int value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(Program, name.c_str()), value);
|
||||
}
|
||||
void Shader::setMat4(const std::string name, glm::mat4 value) const
|
||||
{
|
||||
glUniformMatrix4fv(glGetUniformLocation(Program, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
|
||||
}
|
||||
void Shader::setVec3(const std::string name, glm::vec3 value) const
|
||||
{
|
||||
setVec3(name, value.x, value.y, value.z);
|
||||
}
|
||||
void Shader::setVec3(const std::string name, float v0, float v1, float v2) const
|
||||
{
|
||||
glUniform3f(glGetUniformLocation(Program, name.c_str()), v0, v1, v2);
|
||||
}
|
||||
void Shader::setMat3(const std::string name, glm::mat3 value) const
|
||||
{
|
||||
glUniformMatrix3fv(glGetUniformLocation(Program, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
|
||||
}
|
||||
27
lessons/2. Lighting/5. Light casters/src/Shader.h
Normal file
27
lessons/2. Lighting/5. Light casters/src/Shader.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
unsigned int Program;
|
||||
|
||||
Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath, bool directSource = false);
|
||||
Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath, bool directSource = false);
|
||||
~Shader();
|
||||
|
||||
|
||||
void Use();
|
||||
|
||||
void setBool(const std::string name, bool value) const;
|
||||
void setFloat(const std::string name, float value) const;
|
||||
void setInt(const std::string name, int value) const;
|
||||
void setMat4(const std::string name, glm::mat4 value) const;
|
||||
void setVec3(const std::string name, glm::vec3 value) const;
|
||||
void setVec3(const std::string name, float v0, float v1, float v2) const;
|
||||
void setMat3(const std::string name, glm::mat3 value) const;
|
||||
|
||||
static std::string readFile(const char* path);
|
||||
};
|
||||
|
||||
328
lessons/2. Lighting/5. Light casters/src/main.cpp
Normal file
328
lessons/2. Lighting/5. Light casters/src/main.cpp
Normal file
@@ -0,0 +1,328 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <stb_image.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include "Shader.h"
|
||||
#include "Camera.h"
|
||||
|
||||
#define TEXTURES_DIR "../../../../textures/"
|
||||
#define SHADERS_DIR "../src/shaders/"
|
||||
|
||||
using namespace std;
|
||||
typedef unsigned int uint;
|
||||
|
||||
// Global Constants
|
||||
// ...
|
||||
// Global Variables
|
||||
GLFWwindow* window;
|
||||
int WINDOW_WIDTH = 800;
|
||||
int WINDOW_HEIGHT = 600;
|
||||
Camera myCamera(glm::vec3(0.0f, 0.0f, 3.0f));
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
float lastX = 400;
|
||||
float lastY = 300;
|
||||
bool firstmouse = true;
|
||||
glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
|
||||
glm::vec3 lightColor(1.0f, 1.0f, 1.0f);
|
||||
|
||||
uint8_t init(void);
|
||||
void framebuffersize_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 proccessInput(GLFWwindow* window);
|
||||
unsigned int load_texture(const char* path, bool flip = true);
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (init() != 0)
|
||||
return -1;
|
||||
|
||||
Shader lightingShader(SHADERS_DIR "vertexShader.vert", SHADERS_DIR "fragmentShader.frag");
|
||||
Shader lightSourceShader(SHADERS_DIR "lightSource.vert", SHADERS_DIR "lightSource.frag");
|
||||
|
||||
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
|
||||
};
|
||||
glm::vec3 cubePositions[] = {
|
||||
glm::vec3(0.0f, 0.0f, 0.0f),
|
||||
glm::vec3(2.0f, 5.0f, -15.0f),
|
||||
glm::vec3(-1.5f, -2.2f, -2.5f),
|
||||
glm::vec3(-3.8f, -2.0f, -12.3f),
|
||||
glm::vec3(2.4f, -0.4f, -3.5f),
|
||||
glm::vec3(-1.7f, 3.0f, -7.5f),
|
||||
glm::vec3(1.3f, -2.0f, -2.5f),
|
||||
glm::vec3(1.5f, 2.0f, -2.5f),
|
||||
glm::vec3(1.5f, 0.2f, -1.5f),
|
||||
glm::vec3(-1.3f, 1.0f, -1.5f)
|
||||
};
|
||||
|
||||
uint cubeVAO; // Vertex array buffer
|
||||
glGenVertexArrays(1, &cubeVAO);
|
||||
glBindVertexArray(cubeVAO);
|
||||
|
||||
uint VBO; // Vertex Buffer Object
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // copy data to Graphics card array buffer (buffer for vertices)
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)(6 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
uint lightSourceVAO;
|
||||
glGenVertexArrays(1, &lightSourceVAO);
|
||||
glBindVertexArray(lightSourceVAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
uint container_texture = load_texture(TEXTURES_DIR "container2.png");
|
||||
uint container2_specular = load_texture(TEXTURES_DIR "container2_specular.png");
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
/* ------------------ MAIN loop ------------------ */
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
float currentFrame = glfwGetTime();
|
||||
deltaTime = currentFrame - lastFrame; // cas jak dlouho trval posledni frame
|
||||
lastFrame = currentFrame; // cas kdy zacal tento frame
|
||||
// input ...
|
||||
proccessInput(window);
|
||||
|
||||
// rendering commands here ...
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, container_texture);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, container2_specular);
|
||||
|
||||
lightingShader.Use();
|
||||
lightingShader.setInt("material.diffuse_map", 0);
|
||||
lightingShader.setInt("material.specular_map", 1);
|
||||
lightingShader.setFloat("material.shininess", 64.0f);
|
||||
|
||||
lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
|
||||
glm::vec3 light_diffuse_color = lightColor * glm::vec3(0.6f);
|
||||
// lightingShader.setVec3("light.position", lightPos);
|
||||
// lightingShader.setVec3("light.direction", -0.2f, -1.0f, -0.3f);
|
||||
lightingShader.setVec3("light.position", myCamera.Position);
|
||||
lightingShader.setVec3("light.direction", myCamera.Front);
|
||||
lightingShader.setFloat("light.cutOff", glm::cos(glm::radians(12.5f)));
|
||||
lightingShader.setFloat("light.outerCutOff", glm::cos(glm::radians(17.5f)));
|
||||
lightingShader.setVec3("light.ambient_intensity", light_diffuse_color * glm::vec3(0.2f));
|
||||
lightingShader.setVec3("light.diffuse_intensity", light_diffuse_color);
|
||||
lightingShader.setVec3("light.specular_intensity", lightColor * glm::vec3(1.0f));
|
||||
lightingShader.setFloat("light.constant", 1.0f);
|
||||
lightingShader.setFloat("light.linear", 0.07f);
|
||||
lightingShader.setFloat("light.quadratic", 0.032f);
|
||||
lightingShader.setVec3("viewPos", myCamera.Position);
|
||||
|
||||
glm::mat4 view = myCamera.getViewMatrix();
|
||||
glm::mat4 projection(1.0f);
|
||||
projection = glm::perspective(glm::radians(myCamera.FOV), (float)WINDOW_WIDTH / WINDOW_HEIGHT, 0.1f, 100.0f);
|
||||
|
||||
lightingShader.setMat4("view", view);
|
||||
lightingShader.setMat4("projection", projection);
|
||||
|
||||
for (int i = 0; i < sizeof(cubePositions) / sizeof(glm::vec3); i++)
|
||||
{
|
||||
float angle = i * 20.0f;
|
||||
glm::mat4 model(1.0f);
|
||||
model = glm::translate(model, cubePositions[i]);
|
||||
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
|
||||
|
||||
glm::mat3 normalMatrix = glm::transpose(glm::inverse(model));
|
||||
|
||||
lightingShader.setMat4("model", model);
|
||||
lightingShader.setMat3("normalMatrix", normalMatrix);
|
||||
|
||||
glBindVertexArray(cubeVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
}
|
||||
|
||||
/* ----------- Light Source ----------- */
|
||||
glm::mat4 model(1.0f);
|
||||
model = glm::translate(model, lightPos);
|
||||
model = glm::scale(model, glm::vec3(0.2f));
|
||||
|
||||
lightSourceShader.Use();
|
||||
glm::mat4 PVM = projection * view * model;
|
||||
lightSourceShader.setMat4("PVM", PVM);
|
||||
lightSourceShader.setVec3("lightColor", lightColor);
|
||||
|
||||
glBindVertexArray(lightSourceVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
|
||||
// check and calls events and swap the buffers
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glDeleteVertexArrays(1, &lightSourceVAO);
|
||||
glDeleteVertexArrays(1, &cubeVAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t init(void) {
|
||||
// init of glfw window
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "5. Light casters", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
cerr << "Failed to create glfw window" << endl;
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
|
||||
// init of GLAD
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
cerr << "Failed to initialize GLAD" << endl;
|
||||
}
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffersize_callback);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
return 0;
|
||||
}
|
||||
void framebuffersize_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
WINDOW_HEIGHT = height;
|
||||
WINDOW_WIDTH = width;
|
||||
}
|
||||
void proccessInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
if (glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
/* -------------------- Movement -------------------- */
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_FORWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_BACKWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_RIGHT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_LEFT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_DOWN, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
||||
myCamera.ProccessKeyboard(CAM_UP, deltaTime);
|
||||
|
||||
}
|
||||
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;
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
|
||||
myCamera.ProccessMouse(xoffset, yoffset);
|
||||
}
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
||||
{
|
||||
myCamera.ProccessScroll(yoffset);
|
||||
}
|
||||
unsigned int load_texture(const char* path, bool flip)
|
||||
{
|
||||
unsigned int texture = 0;
|
||||
int width, height, nrChannels, format;
|
||||
stbi_set_flip_vertically_on_load(flip);
|
||||
unsigned char* data = stbi_load(path, &width, &height, &nrChannels, 0); // load image to the array of bytes (char = 1 byte)
|
||||
|
||||
switch (nrChannels)
|
||||
{
|
||||
case 4: format = GL_RGBA; break;
|
||||
case 3: format = GL_RGB; break;
|
||||
case 2: format = GL_RED; break;
|
||||
default: format = GL_RGB; break;
|
||||
}
|
||||
|
||||
if (data) // data != NULL
|
||||
{
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
|
||||
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);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, data); // generates texture
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
else {
|
||||
cerr << "Could not load texture '" << path << "'\n";
|
||||
}
|
||||
stbi_image_free(data);
|
||||
|
||||
return texture;
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
#version 330 core
|
||||
|
||||
struct Material {
|
||||
sampler2D diffuse_map;
|
||||
sampler2D specular_map;
|
||||
|
||||
float shininess;
|
||||
};
|
||||
struct Light {
|
||||
vec3 position;
|
||||
vec3 direction;
|
||||
|
||||
vec3 ambient_intensity;
|
||||
vec3 diffuse_intensity;
|
||||
vec3 specular_intensity;
|
||||
|
||||
float constant;
|
||||
float linear;
|
||||
float quadratic;
|
||||
|
||||
float cutOff;
|
||||
float outerCutOff;
|
||||
};
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec3 viewPos;
|
||||
uniform Material material;
|
||||
uniform Light light;
|
||||
|
||||
in vec3 Normal;
|
||||
in vec3 FragPos;
|
||||
in vec2 TexCoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
|
||||
// vec3 lightDir = normalize(light.position - FragPos); // direction towards light source
|
||||
//// vec3 lightDir = normalize(-light.direction);
|
||||
// float frag_dist = length(light.position - FragPos); // fragment distance from light source position
|
||||
// float attenuation = 1.0 / (light.constant + light.linear * frag_dist + light.quadratic * frag_dist * frag_dist);
|
||||
// vec3 normal = normalize(Normal);
|
||||
//
|
||||
// vec3 ambient = light.ambient_intensity * vec3(texture(material.diffuse_map, TexCoord));
|
||||
//
|
||||
// float diff = max(dot(lightDir, normal), 0.0f);
|
||||
// vec3 diffuse = light.diffuse_intensity * diff * vec3(texture(material.diffuse_map, TexCoord));
|
||||
//
|
||||
// vec3 reflectDir = reflect(-lightDir, normal);
|
||||
// float spec = pow(max(dot(normalize(viewPos - FragPos), reflectDir), 0.0), material.shininess);
|
||||
// vec3 specular = light.specular_intensity * spec * vec3(texture(material.specular_map, TexCoord));
|
||||
//
|
||||
// vec3 result = (ambient + diffuse + specular) * attenuation;
|
||||
// FragColor = vec4(result, 1.0f);
|
||||
|
||||
|
||||
vec3 lightDir = normalize(light.position - FragPos); // direction towards light source
|
||||
// vec3 lightDir = normalize(-light.direction);
|
||||
|
||||
vec3 normal = normalize(Normal);
|
||||
|
||||
vec3 ambient = light.ambient_intensity * vec3(texture(material.diffuse_map, TexCoord));
|
||||
|
||||
float diff = max(dot(lightDir, normal), 0.0f);
|
||||
vec3 diffuse = light.diffuse_intensity * diff * vec3(texture(material.diffuse_map, TexCoord));
|
||||
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
float spec = pow(max(dot(normalize(viewPos - FragPos), reflectDir), 0.0), material.shininess);
|
||||
vec3 specular = light.specular_intensity * spec * vec3(texture(material.specular_map, TexCoord));
|
||||
|
||||
float dist = length(light.position - FragPos);
|
||||
float attenuation = 1.0 / (light.constant + light.linear * dist + light.quadratic * (dist * dist));
|
||||
|
||||
float theta = dot(lightDir, normalize(-light.direction));
|
||||
float epsilon = light.cutOff - light.outerCutOff;
|
||||
float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
|
||||
//float intensity = smoothstep(0.0, 1.0, (theta - light.outerCutOff) / epsilon);
|
||||
|
||||
diffuse *= attenuation * intensity;
|
||||
specular *= attenuation * intensity;
|
||||
|
||||
vec3 result = ambient + diffuse + specular;
|
||||
FragColor = vec4(result, 1.0f);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec3 lightColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(lightColor, 1.0f);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 aPos;
|
||||
|
||||
uniform mat4 PVM;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = PVM * vec4(aPos.xyz, 1.0);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aNormal;
|
||||
layout (location = 2) in vec2 aTexCoord;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
uniform mat3 normalMatrix;
|
||||
|
||||
out vec3 Normal;
|
||||
out vec3 FragPos;
|
||||
out vec2 TexCoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(aPos.xyz, 1.0);
|
||||
Normal = normalMatrix * aNormal;
|
||||
FragPos = vec3(model * vec4(aPos, 1.0));
|
||||
TexCoord = aTexCoord;
|
||||
}
|
||||
37
lessons/2. Lighting/6. Multiple Lights/Makefile
Normal file
37
lessons/2. Lighting/6. Multiple Lights/Makefile
Normal file
@@ -0,0 +1,37 @@
|
||||
NAME := main
|
||||
CXX=g++
|
||||
DEBUG :=
|
||||
CXX_FLAGS := $(DEBUG) -std=c++14
|
||||
LIBS := -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -pthread -lXi -ldl -lm
|
||||
INCLUDE := ../../../include/
|
||||
BIN := bin
|
||||
SRC := src
|
||||
INPUT = src/main.cpp ../../../glad.c
|
||||
|
||||
all: $(BIN)/$(NAME)
|
||||
|
||||
run:
|
||||
cd $(BIN) && ./$(NAME)
|
||||
|
||||
$(BIN)/$(NAME): $(SRC)/main.o $(SRC)/glad.o $(SRC)/Shader.o $(SRC)/stb_image.o $(SRC)/Camera.o
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) $^ -o $@ $(LIBS)
|
||||
|
||||
.cpp.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
.c.o:
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/glad.o: ../../../glad.c
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
$(SRC)/stb_image.o: ../../../stb_image.cpp
|
||||
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -c $< -o $@
|
||||
|
||||
# dependencies
|
||||
$(SRC)/main.o: $(SRC)/main.cpp
|
||||
$(SRC)/Shader.o: $(SRC)/Shader.cpp
|
||||
|
||||
clean:
|
||||
rm -rf $(SRC)/*.o
|
||||
touch $(SRC)/*
|
||||
83
lessons/2. Lighting/6. Multiple Lights/src/Camera.cpp
Normal file
83
lessons/2. Lighting/6. Multiple Lights/src/Camera.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include "Camera.h"
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
Camera::Camera(glm::vec3 position, glm::vec3 worldUp, glm::vec3 worldFront, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(2.5f), MouseSensitivity(0.1f), FOV(45.0f), Mode(CAM_FLOAT)
|
||||
{
|
||||
this->Position = position;
|
||||
this->WorldUp = worldUp;
|
||||
this->Yaw = yaw;
|
||||
this->Pitch = pitch;
|
||||
this->WorldFront = worldFront;
|
||||
this->WorldRight = glm::normalize(glm::cross(this->WorldFront, this->WorldUp));
|
||||
|
||||
updateCameraVectors();
|
||||
}
|
||||
void Camera::updateCameraVectors()
|
||||
{
|
||||
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);
|
||||
|
||||
Right = glm::normalize(glm::cross(Front, WorldUp));
|
||||
Up = glm::normalize(glm::cross(Right, Front));
|
||||
}
|
||||
glm::mat4 Camera::getViewMatrix()
|
||||
{
|
||||
return glm::lookAt(Position, Position + Front, WorldUp);
|
||||
}
|
||||
void Camera::ProccessKeyboard(camera_movement direction, float deltaTime)
|
||||
{
|
||||
switch (direction)
|
||||
{
|
||||
case CAM_FORWARD:
|
||||
Position += deltaTime * MovementSpeed * Front;
|
||||
break;
|
||||
case CAM_BACKWARD:
|
||||
Position -= deltaTime * MovementSpeed * Front;
|
||||
break;
|
||||
case CAM_LEFT:
|
||||
Position -= deltaTime * MovementSpeed * Right;
|
||||
break;
|
||||
case CAM_RIGHT:
|
||||
Position += deltaTime * MovementSpeed * Right;
|
||||
break;
|
||||
case CAM_UP:
|
||||
Position += deltaTime * MovementSpeed * WorldUp;
|
||||
break;
|
||||
case CAM_DOWN:
|
||||
Position -= deltaTime * MovementSpeed * WorldUp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
void Camera::ProccessMouse(float xoffset, float yoffset, bool constrainPitch)
|
||||
{
|
||||
xoffset *= MouseSensitivity;
|
||||
yoffset *= MouseSensitivity;
|
||||
|
||||
Yaw = fmodf(Yaw + xoffset, 360.0f);
|
||||
Pitch += yoffset;
|
||||
|
||||
if (constrainPitch)
|
||||
{
|
||||
if (Pitch > 89.0f)
|
||||
Pitch = 89.0f;
|
||||
else if (Pitch < -89.0f)
|
||||
Pitch = -89.0f;
|
||||
}
|
||||
|
||||
updateCameraVectors();
|
||||
}
|
||||
void Camera::ProccessScroll(float yoffset)
|
||||
{
|
||||
FOV -= yoffset;
|
||||
if (FOV < 1.0f)
|
||||
FOV = 1.0f;
|
||||
else if (FOV > 45.0f)
|
||||
FOV = 45.0f;
|
||||
}
|
||||
void Camera::SetCameraMode(camera_mode mode)
|
||||
{
|
||||
Mode = mode;
|
||||
}
|
||||
54
lessons/2. Lighting/6. Multiple Lights/src/Camera.h
Normal file
54
lessons/2. Lighting/6. Multiple Lights/src/Camera.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
enum camera_movement {
|
||||
CAM_FORWARD,
|
||||
CAM_BACKWARD,
|
||||
CAM_LEFT,
|
||||
CAM_RIGHT,
|
||||
CAM_UP,
|
||||
CAM_DOWN
|
||||
};
|
||||
|
||||
enum camera_mode {
|
||||
CAM_FLOAT,
|
||||
CAM_MINECRAFT
|
||||
};
|
||||
|
||||
class Camera
|
||||
{
|
||||
public:
|
||||
float FOV;
|
||||
float MovementSpeed;
|
||||
float MouseSensitivity;
|
||||
float Yaw; // vlevo vpravo
|
||||
float Pitch; // nahoru dolu
|
||||
|
||||
camera_mode Mode;
|
||||
|
||||
glm::vec3 Position;
|
||||
glm::vec3 Front;
|
||||
glm::vec3 Right;
|
||||
glm::vec3 Up;
|
||||
glm::vec3 WorldUp;
|
||||
glm::vec3 WorldRight;
|
||||
glm::vec3 WorldFront;
|
||||
|
||||
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f),
|
||||
glm::vec3 worldUp = glm::vec3(0.0f, 1.0f, 0.0f),
|
||||
glm::vec3 worldFront = glm::vec3(0.0f, 0.0f, -1.0),
|
||||
float yaw = -90.0f, float pitch = 0);
|
||||
|
||||
glm::mat4 getViewMatrix();
|
||||
void ProccessKeyboard(camera_movement direction, float deltaTime);
|
||||
void ProccessMouse(float xoffset, float yoffset, bool constrainPitch = true);
|
||||
void ProccessScroll(float yoffset);
|
||||
void SetCameraMode(camera_mode mode);
|
||||
|
||||
private:
|
||||
float lastY;
|
||||
float lastX;
|
||||
|
||||
void updateCameraVectors();
|
||||
};
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user