Nove repo opengl-learning

This commit is contained in:
TheRetikGM
2021-02-12 15:43:03 +01:00
commit 63e3c0bc8b
1046 changed files with 358397 additions and 0 deletions

View 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();
};

View File

@@ -0,0 +1,17 @@
#pragma once
#define DC_RED "\x1b[31m"
#define DC_GREEN "\x1b[32m"
#define DC_YELLOW "\x1b[33m"
#define DC_CYAN "\x1b[36m"
#define DC_MAGNETA "\x1b[35m"
#define DC_DEFAULT "\x1b[0m"
#define DC_ERROR "[" DC_RED "ERROR" DC_DEFAULT "]"
#define DC_SUCCESS "[" DC_GREEN "SUCCESS" DC_DEFAULT "]"
#define DC_INFO "[" DC_CYAN "INFO" DC_DEFAULT "]"
#define DC_WARNING "[" DC_YELLOW "WARNING" DC_DEFAULT "]"
#define DC_CURSOR_MOVE(y, x) std::cout << "\033[" << (y + 1) << ";" << (x + 1) << "H"
#define DC_CURSOR_SAVE() std::cout << "\033[s"
#define DC_CURSOR_RESTORE() std::cout << "\033[u"
#define DC_CLRTOEOL() std::cout << "\033[K"
#define DC_CLRSCR() std::cout << "\033[2J"

View File

@@ -0,0 +1,29 @@
#pragma once
#include <string>
#include <glm/glm.hpp>
class Shader
{
public:
unsigned int Program;
Shader(const char* vertexShaderSourcePath, const char* fragmentShaderSourcePath, const char* geometryShaderSource = NULL, bool directSource = false);
Shader(std::string vertexShaderSourcePath, std::string fragmentShaderSourcePath, std::string geometryShaderSource = NULL, bool directSource = false);
~Shader();
void Use() const;
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 setVec2(const std::string name, float v0, float v1) const;
void setVec2(const std::string name, glm::vec2 v) const;
void setMat3(const std::string name, glm::mat3 value) const;
static std::string readFile(const char* path);
};

View File

@@ -0,0 +1,204 @@
#pragma once
#include <iostream>
#include <glad/glad.h>
#include <vector>
#include <initializer_list>
#include <unordered_map>
#include <string>
namespace glfbo
{
enum class BufferType : uint8_t
{
Texture = 0,
Renderbuffer = 1,
Cubemap = 2
};
struct vector3
{
float x, y, z;
};
enum class ImageType : size_t
{
PNG = 0,
JPG = 1
};
static const std::string ImageTypeStrings[] = { "png", "jpg" };
std::string GetScreenshotName(ImageType type);
GLint GetFramebufferBinding();
class ColorBuffer
{
public:
unsigned int ID;
const unsigned int Width, Height;
unsigned int Samples;
BufferType Type;
ColorBuffer(unsigned int width, unsigned int height,
BufferType type,
GLint internal_format,
unsigned int samples,
GLint wrap_s,
GLint wrap_t,
GLint wrap_r,
vector3 bordercolor);
ColorBuffer(unsigned int width, unsigned int height,
BufferType type = BufferType::Texture,
GLint internal_format = GL_RGB,
unsigned int samples = 0,
GLint wrap_str = GL_CLAMP_TO_EDGE,
vector3 bordercolor = vector3{1.0f, 1.0f, 1.0f});
~ColorBuffer();
GLenum BindTarget();
GLint Get_internal_format();
private:
GLint int_format;
};
class DepthBuffer
{
public:
unsigned int ID;
const unsigned int Width, Height;
unsigned int Samples;
BufferType Type;
DepthBuffer(unsigned int width, unsigned int height,
BufferType type,
GLint internal_format,
unsigned int samples,
GLint wrap_s,
GLint wrap_t,
GLint wrap_r,
vector3 bordercolor);
DepthBuffer(unsigned int width, unsigned int height,
BufferType type = BufferType::Renderbuffer,
GLint internal_format = GL_DEPTH_COMPONENT24,
unsigned int samples = 0,
GLint wrap_str = GL_CLAMP_TO_EDGE,
vector3 border_color = vector3{1.0f, 1.0f, 1.0f});
~DepthBuffer();
GLenum BindTarget();
GLint Get_internal_format();
private:
GLint int_format;
};
class StencilBuffer
{
public:
unsigned int ID;
const unsigned int Width, Height;
unsigned int Samples;
BufferType Type;
StencilBuffer(unsigned int width, unsigned int height,
BufferType type = BufferType::Renderbuffer,
unsigned int samples = 0);
~StencilBuffer();
GLenum BindTarget();
};
class DepthStencilBuffer
{
public:
unsigned int ID;
const unsigned int Width, Height;
const unsigned int Samples;
BufferType Type;
DepthStencilBuffer(unsigned int width, unsigned int height,
BufferType type,
GLint internal_format,
unsigned int samples,
GLint wrap_s,
GLint wrap_t,
GLint wrap_r,
vector3 bordercolor);
DepthStencilBuffer(unsigned int width, unsigned int height,
BufferType type = BufferType::Renderbuffer,
GLint internal_format = GL_DEPTH24_STENCIL8,
unsigned int samples = 0,
GLint wrap_str = GL_CLAMP_TO_EDGE,
vector3 bordercolor = vector3{1.0f, 1.0f, 1.0f});
~DepthStencilBuffer();
GLenum BindTarget();
GLint Get_internal_format();
private:
GLint int_format;
};
class Framebuffer
{
protected:
uint8_t jpg_export_quality = 90;
public:
unsigned int ID;
std::unordered_map<unsigned int, ColorBuffer*> ColorBufs;
DepthBuffer *DepthBuf;
StencilBuffer *StencilBuf;
DepthStencilBuffer *DepthStencilBuf;
Framebuffer(std::unordered_map<unsigned int, ColorBuffer*> cBufs, DepthBuffer* dBuf, StencilBuffer* sBuf);
Framebuffer(unsigned int attachment, ColorBuffer* cBuf, DepthBuffer* dBuf, StencilBuffer* sBuf);
Framebuffer(unsigned int attachment, ColorBuffer* cBuf, DepthStencilBuffer* dsBuf);
Framebuffer(std::unordered_map<unsigned int, ColorBuffer*> cBufs, DepthStencilBuffer* dsBuf);
Framebuffer();
~Framebuffer();
int CheckStatus();
void Set_ReadDrawBuffer(GLenum mode);
/* Specify color attachments for drawing */
void Set_DrawBuffers(std::initializer_list<unsigned int> indexes);
/* Returns color buffer ID which is binded to specified color attachment number */
unsigned int Get_ColorBuffer(unsigned int attachment);
unsigned int Get_ColorBufferWidth(unsigned int attachment);
unsigned int Get_ColorBufferHeight(unsigned int attachment);
void Set_JpgExportQuality(uint8_t quality);
uint8_t Get_JpgExportQuality();
virtual void Attach_ColorBuffer(unsigned int attachment, ColorBuffer* buffer);
virtual void Attach_DepthBuffer(DepthBuffer* buffer);
virtual void Attach_StencilBuffer(StencilBuffer* buffer);
virtual void Attach_DepthStencilBuffer(DepthStencilBuffer* buffer);
virtual std::string Export(std::string dir, ImageType type, unsigned int attachment);
};
class MSFramebuffer : public Framebuffer
{
public:
MSFramebuffer(unsigned int samples, std::unordered_map<unsigned int, ColorBuffer*> cBufs, DepthBuffer* dBuf, StencilBuffer* sBuf);
MSFramebuffer(unsigned int samples, unsigned int attachment, ColorBuffer* cBuf, DepthBuffer* dBuf, StencilBuffer* sBuf);
MSFramebuffer(unsigned int samples, unsigned int attachment, ColorBuffer* cBuf, DepthStencilBuffer* dsBuf);
MSFramebuffer(unsigned int samples, std::unordered_map<unsigned int, ColorBuffer*> cBufs, DepthStencilBuffer* dsBuf);
MSFramebuffer(unsigned int samples);
~MSFramebuffer();
/* Returns number of samples */
unsigned int Get_Samples();
/* Resolve color buffer binded to specified attachment and returns resolved color buffer ID. */
unsigned int Get_ResolvedColorBuffer(unsigned int attachment);
/* Creates intermediate Framebuffer object for specified color attachment. */
void Create_intFBO(unsigned int attachment);
/* Returns pointer to intermediate framebuffer of specified color attachment. */
Framebuffer* Get_intFBO(unsigned int attachment);
void Attach_ColorBuffer(unsigned int attachment, ColorBuffer* buffer) override;
void Attach_DepthBuffer(DepthBuffer* buffer) override;
void Attach_StencilBuffer(StencilBuffer* buffer) override;
void Attach_DepthStencilBuffer(DepthStencilBuffer* buffer) override;
std::string Export(std::string dir, ImageType type, unsigned int attachment) override;
private:
std::unordered_map<unsigned int, Framebuffer*> intFBOs;
unsigned int samples;
void handleSamples(unsigned int s);
};
}

View File

@@ -0,0 +1,53 @@
#pragma once
#include <glm/glm.hpp>
#include <string>
#include <vector>
#include "Shader.h"
namespace glml
{
static const std::string TextureTypeStrings[] = {
"texture_diffuse",
"texture_specular",
"texture_normal",
"texture_height"
};
enum class TextureType : size_t
{
diffuse = 0,
specular = 1,
normal = 2,
height = 3
};
struct Vertex {
glm::vec3 Position;
glm::vec3 Normal;
glm::vec2 TexCoord;
glm::vec3 Tangent;
glm::vec3 Bitangent;
};
struct Texture {
unsigned int id;
TextureType type;
std::string path;
};
class Mesh {
public:
unsigned int VAO;
std::vector<Vertex> vertices;
std::vector<unsigned int> indices;
std::vector<Texture> textures;
Mesh(const std::vector<Vertex>& vertices, const std::vector<unsigned int>& indices, const std::vector<Texture>& textures);
void Draw(const Shader& shader) const;
private:
unsigned int VBO, EBO;
void setupMesh();
};
};

View File

@@ -0,0 +1,48 @@
#pragma once
#include <string>
#include "Mesh.h"
#include <vector>
#include "Shader.h"
#include <assimp/scene.h>
/*
Vertex attributes are expected as follows:
0. Vertex position
1. Vertex normal
2. Vertex texture coordinate
3. Vertex tangent
4. Vertex bitangent
In the Fragment Shader there must be at least
struct Material
{
sampler2D texture_diffuse0;
}
uniform Material material;
*/
namespace glml
{
class Model {
public:
std::vector<Mesh> meshes;
std::vector<Texture> textures_loaded;
/* sRGB_diffuse: when true, save diffuse textures as sRGB textures*/
Model(const std::string path, bool flip_textures = false, bool sRGB_diffuse = false);
void Draw(const Shader& shader) const;
private:
// model data
std::string directory;
const bool sRGB_diffuse;
const bool flip_textures;
void loadModel(const std::string path);
void processNode(aiNode* node, const aiScene* scene);
Mesh processMesh(aiMesh* mesh, const aiScene* scene);
std::vector<Texture> loadMaterialTextures(aiMaterial* mat, aiTextureType type, TextureType glmlType);
};
static unsigned int load_texture(const char* path, bool flip = true, bool sRGB = true, GLint wrapS = GL_REPEAT, GLint wrapT = GL_REPEAT);
static unsigned int load_texture(std::string path, bool flip = true, bool sRGB = true, GLint wrapS = GL_REPEAT, GLint wrapT = GL_REPEAT);
}