Nove repo opengl-learning
This commit is contained in:
37
lessons/4. Advanced OpenGL/2. Stencil testing/Makefile
Normal file
37
lessons/4. Advanced OpenGL/2. Stencil testing/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 -lassimp
|
||||
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/4. Advanced OpenGL/2. Stencil testing/src/Camera.cpp
Normal file
83
lessons/4. Advanced OpenGL/2. Stencil testing/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/4. Advanced OpenGL/2. Stencil testing/src/Camera.h
Normal file
54
lessons/4. Advanced OpenGL/2. Stencil testing/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/4. Advanced OpenGL/2. Stencil testing/src/Shader.cpp
Normal file
119
lessons/4. Advanced OpenGL/2. Stencil testing/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/4. Advanced OpenGL/2. Stencil testing/src/Shader.h
Normal file
27
lessons/4. Advanced OpenGL/2. Stencil testing/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);
|
||||
};
|
||||
|
||||
323
lessons/4. Advanced OpenGL/2. Stencil testing/src/main.cpp
Normal file
323
lessons/4. Advanced OpenGL/2. Stencil testing/src/main.cpp
Normal file
@@ -0,0 +1,323 @@
|
||||
#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 MODELS_DIR "../../../../models/"
|
||||
#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 shader(SHADERS_DIR "depthTesting.vert", SHADERS_DIR "depthTesting.frag");
|
||||
Shader outlineShader(SHADERS_DIR "depthTesting.vert", SHADERS_DIR "outlineShader.frag");
|
||||
|
||||
float cubeVertices[] = {
|
||||
// positions // texture Coords
|
||||
-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
|
||||
};
|
||||
float planeVertices[] = {
|
||||
// positions // texture Coords (note we set these higher than 1 (together with GL_REPEAT as texture wrapping mode). this will cause the floor texture to repeat)
|
||||
5.0f, -0.5f, 5.0f, 2.0f, 0.0f,
|
||||
-5.0f, -0.5f, 5.0f, 0.0f, 0.0f,
|
||||
-5.0f, -0.5f, -5.0f, 0.0f, 2.0f,
|
||||
|
||||
5.0f, -0.5f, 5.0f, 2.0f, 0.0f,
|
||||
-5.0f, -0.5f, -5.0f, 0.0f, 2.0f,
|
||||
5.0f, -0.5f, -5.0f, 2.0f, 2.0f
|
||||
};
|
||||
|
||||
unsigned int cubeVAO, cubeVBO;
|
||||
glGenVertexArrays(1, &cubeVAO);
|
||||
glGenBuffers(1, &cubeVBO);
|
||||
glBindVertexArray(cubeVAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW);
|
||||
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);
|
||||
|
||||
unsigned int planeVAO, planeVBO;
|
||||
glGenVertexArrays(1, &planeVAO);
|
||||
glGenBuffers(1, &planeVBO);
|
||||
glBindVertexArray(planeVAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, planeVBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(planeVertices), planeVertices, GL_STATIC_DRAW);
|
||||
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);
|
||||
|
||||
unsigned int cubeTexture = load_texture(TEXTURES_DIR "marble.jpg");
|
||||
unsigned int planeTexture = load_texture(TEXTURES_DIR "metal.png");
|
||||
|
||||
shader.Use();
|
||||
shader.setInt("texture1", 0);
|
||||
|
||||
glEnable(GL_DEPTH_TEST); // zapne depth test
|
||||
glDepthFunc(GL_LESS); // nastav funkci na blizsi fragmenty
|
||||
glEnable(GL_STENCIL_TEST); // zapne stencil test
|
||||
glStencilFunc(GL_NOTEQUAL, 1, 0xFF); // stencil test zobrazi fragment, kdyz hodnota v buffernu neni rovna 1
|
||||
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); // pokud stencil nebo depth test selze, tak zanech hodnotu v bufferu, jestli neselze, tak ji nahrad hodnotou 1 (specifikovao v glStencilFunc)
|
||||
/* ------------------ 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 | GL_STENCIL_BUFFER_BIT);
|
||||
|
||||
shader.Use();
|
||||
|
||||
glm::mat4 model(1.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);
|
||||
|
||||
shader.setMat4("view", view);
|
||||
shader.setMat4("projection", projection);
|
||||
|
||||
// plane
|
||||
glStencilMask(0x00); // vypne zapisovani do stencil bufferu
|
||||
glBindVertexArray(planeVAO);
|
||||
glBindTexture(GL_TEXTURE_2D, planeTexture);
|
||||
shader.setMat4("model", glm::mat4(1.0f));
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
glBindVertexArray(0);
|
||||
|
||||
// cubes
|
||||
glBindVertexArray(cubeVAO);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, cubeTexture);
|
||||
|
||||
glStencilFunc(GL_ALWAYS, 1, 0xff); // stencil test je pokazde uspesny
|
||||
glStencilMask(0xff); // zapne zapisovani do bufferu (maska je ANDnuta k hodnote, ktera by byla pridana do bufferu)
|
||||
|
||||
// first container
|
||||
model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));
|
||||
shader.setMat4("model", model);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
// second container
|
||||
model = glm::translate(glm::mat4(1.0f), glm::vec3(2.0f, 0.0f, 0.0f));
|
||||
shader.setMat4("model", model);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
|
||||
glStencilMask(0x00); // vypne zapis do bufferu ( kazda hodnota, ktera by byla pridana do bufferu skonci jako nula --> x AND 0x00 = 0)
|
||||
glStencilFunc(GL_NOTEQUAL, 1, 0xff); // pokud se hodnota v bufferu nerovna 1, tak test uspeje --> fragment se vykresli
|
||||
glDisable(GL_DEPTH_TEST); // vypne depth test; nechceme aby podlaha prekryvala outline objektu
|
||||
|
||||
outlineShader.Use();
|
||||
outlineShader.setMat4("view", view);
|
||||
outlineShader.setMat4("projection", projection);
|
||||
model = glm::translate(glm::mat4(1.0f), glm::vec3(-1.0f, 0.0f, -1.0f));
|
||||
model = glm::scale(model, glm::vec3(1.025f));
|
||||
shader.setMat4("model", model);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
model = glm::translate(glm::mat4(1.0f), glm::vec3(2.0f, 0.0f, 0.0f));
|
||||
model = glm::scale(model, glm::vec3(1.025f));
|
||||
shader.setMat4("model", model);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
|
||||
/* nastavy stencil testing na defaultni hodnoty */
|
||||
glStencilMask(0xff); // zapne zapis
|
||||
glStencilFunc(GL_ALWAYS, 1, 0xff); // test vydy projde --> vsechny fragmenty se vykresli
|
||||
glEnable(GL_DEPTH_TEST); // zpatky zapne depth test
|
||||
|
||||
// check and calls events and swap the buffers
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
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, "2. Stencil testing", 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,12 @@
|
||||
#version 330
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec2 TexCoord;
|
||||
|
||||
uniform sampler2D texture1;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = texture(texture1, TexCoord);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#version 330
|
||||
|
||||
layout (location = 0) in vec3 aPosition;
|
||||
layout (location = 1) in vec2 aTexCoord;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
out vec2 TexCoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(aPosition, 1.0);
|
||||
TexCoord = aTexCoord;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#version 330
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
// FragColor = vec4(0.04, 0.28, 0.26, 1.0);
|
||||
FragColor = vec4(1.0, 0.0, 0.0, 1.0);
|
||||
}
|
||||
Reference in New Issue
Block a user