setup complete - visible character

This commit is contained in:
2021-07-07 23:20:04 +02:00
parent 9014681460
commit e4c6a45427
53 changed files with 180 additions and 422 deletions

View File

@@ -0,0 +1,41 @@
#pragma once
#include <map>
#include <string>
#include <glad/glad.h>
#include <stb_image.h>
#include "texture.h"
#include "shader.h"
// A static singleton ResourceManager class that hosts several
// functions to load Textures and Shaders. Each loaded Texture
// and/or shader is also stored for future reference by string
// handles. All functions and resources are static and no
// public constructor is defined.
class ResourceManager
{
public:
// Resource storage
static std::map<std::string, Shader> Shaders;
static std::map<std::string, Texture> Textures;
// Loads (and generates) a shader program from file loading vertex, fragment (and geometry) shader's source code. If gShaderFile is not nullptr, it also loads a geometry shader
static Shader LoadShader(const GLchar* vShaderFile, const GLchar* fShaderFile, const GLchar* gShaderFile, std::string name);
// Retrieves a stored sader
static Shader GetShader(std::string name);
// Loads (and generates) a Texture from file
static Texture LoadTexture(const GLchar* file, std::string name);
// Retrieves a stored Texture
static Texture GetTexture(std::string name);
// Properly de-allocates all loaded resources
static void Clear();
private:
// Private constructor, that is we do not want any actual resource manager objects. Its members and functions should be publicly available (static).
ResourceManager() { }
// Loads and generates a shader from file
static Shader loadShaderFromFile(const GLchar* vShaderFile, const GLchar* fShaderFile, const GLchar* gShaderFile = nullptr);
// Loads a single Texture from file
static Texture loadTextureFromFile(const GLchar* file);
};

41
include/logic/shader.h Normal file
View File

@@ -0,0 +1,41 @@
#pragma once
#include <glad/glad.h>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// General purpsoe shader object. Compiles from file, generates
// compile/link-time error messages and hosts several utility
// functions for easy management.
class Shader
{
public:
// State
GLuint ID;
// Constructor
Shader() { }
// Sets the current shader as active
Shader& Use();
// Compiles the shader from given source code
void Compile(const GLchar* vertexSource, const GLchar* fragmentSource, const GLchar* geometrySource = nullptr); // Note: geometry source code is optional
// Utility functions
void SetFloat(const GLchar* name, GLfloat value, GLboolean useShader = false);
void SetInteger(const GLchar* name, GLint value, GLboolean useShader = false);
void SetVector2f(const GLchar* name, GLfloat x, GLfloat y, GLboolean useShader = false);
void SetVector2f(const GLchar* name, const glm::vec2& value, GLboolean useShader = false);
void SetVector3f(const GLchar* name, GLfloat x, GLfloat y, GLfloat z, GLboolean useShader = false);
void SetVector3f(const GLchar* name, const glm::vec3& value, GLboolean useShader = false);
void SetVector4f(const GLchar* name, GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLboolean useShader = false);
void SetVector4f(const GLchar* name, const glm::vec4& value, GLboolean useShader = false);
void SetMatrix4(const GLchar* name, const glm::mat4& matrix, GLboolean useShader = false);
private:
// Checks if compilation or linking failed and if so, print the error logs
void checkCompileErrors(GLuint object, std::string type);
};

View File

@@ -0,0 +1,25 @@
#pragma once
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "texture.h"
#include "shader.h"
class SpriteRenderer
{
public:
// Constructor (inits shaders/shapes)
SpriteRenderer(Shader& shader);
// Destructor
~SpriteRenderer();
// Renders a defined quad textured with given sprite
void DrawSprite(Texture& texture, glm::vec2 position, glm::vec4 UV, glm::vec2 size = glm::vec2(10, 10), GLfloat rotate = 0.0f);
private:
// Render state
Shader shader;
GLuint quadVAO, VBO, EBO;
// Initializes and configures the quad's buffer and vertex attributes
void initRenderData();
};

27
include/logic/texture.h Normal file
View File

@@ -0,0 +1,27 @@
#pragma once
// Texture2D is able to store and configure a texture in OpenGL.
// It also hosts utility functions for easy management.
#include <glad/glad.h>
class Texture
{
public:
// Holds the ID of the texture object, used for all texture operations to reference to this particlar texture
GLuint ID;
// Texture image dimensions
GLuint Width, Height; // Width and height of loaded image in pixels
// Texture Format
GLuint Internal_Format; // Format of texture object
GLuint Image_Format; // Format of loaded image
// Texture configuration
GLuint Wrap_S; // Wrapping mode on S axis
GLuint Wrap_T; // Wrapping mode on T axis
GLuint Filter_Min; // Filtering mode if texture pixels < screen pixels
GLuint Filter_Mag; // Filtering mode if texture pixels > screen pixels
// Constructor (sets default texture modes)
Texture();
// Generates texture from image data
void generate(GLuint width, GLuint height, unsigned char* data);
// Binds the texture as the current active GL_TEXTURE_2D texture object
void bind() const;
};