first commit
This commit is contained in:
129
shaders/Shader.cpp
Normal file
129
shaders/Shader.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
#include "shader.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
Shader& Shader::Use()
|
||||
{
|
||||
glUseProgram(this->ID);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Shader::Compile(const GLchar* vertexSource, const GLchar* fragmentSource, const GLchar* geometrySource)
|
||||
{
|
||||
GLuint sVertex, sFragment, gShader;
|
||||
// Vertex Shader
|
||||
sVertex = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(sVertex, 1, &vertexSource, NULL);
|
||||
glCompileShader(sVertex);
|
||||
checkCompileErrors(sVertex, "VERTEX");
|
||||
// Fragment Shader
|
||||
sFragment = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(sFragment, 1, &fragmentSource, NULL);
|
||||
glCompileShader(sFragment);
|
||||
checkCompileErrors(sFragment, "FRAGMENT");
|
||||
// If geometry shader source code is given, also compile geometry shader
|
||||
if (geometrySource != nullptr)
|
||||
{
|
||||
gShader = glCreateShader(GL_GEOMETRY_SHADER);
|
||||
glShaderSource(gShader, 1, &geometrySource, NULL);
|
||||
glCompileShader(gShader);
|
||||
checkCompileErrors(gShader, "GEOMETRY");
|
||||
}
|
||||
// Shader Program
|
||||
this->ID = glCreateProgram();
|
||||
glAttachShader(this->ID, sVertex);
|
||||
glAttachShader(this->ID, sFragment);
|
||||
if (geometrySource != nullptr)
|
||||
glAttachShader(this->ID, gShader);
|
||||
glLinkProgram(this->ID);
|
||||
checkCompileErrors(this->ID, "PROGRAM");
|
||||
// Delete the shaders as they're linked into our program now and no longer necessery
|
||||
glDeleteShader(sVertex);
|
||||
glDeleteShader(sFragment);
|
||||
if (geometrySource != nullptr)
|
||||
glDeleteShader(gShader);
|
||||
}
|
||||
|
||||
void Shader::SetFloat(const GLchar* name, GLfloat value, GLboolean useShader)
|
||||
{
|
||||
if (useShader)
|
||||
this->Use();
|
||||
glUniform1f(glGetUniformLocation(this->ID, name), value);
|
||||
}
|
||||
void Shader::SetInteger(const GLchar* name, GLint value, GLboolean useShader)
|
||||
{
|
||||
if (useShader)
|
||||
this->Use();
|
||||
glUniform1i(glGetUniformLocation(this->ID, name), value);
|
||||
}
|
||||
void Shader::SetVector2f(const GLchar* name, GLfloat x, GLfloat y, GLboolean useShader)
|
||||
{
|
||||
if (useShader)
|
||||
this->Use();
|
||||
glUniform2f(glGetUniformLocation(this->ID, name), x, y);
|
||||
}
|
||||
void Shader::SetVector2f(const GLchar* name, const glm::vec2& value, GLboolean useShader)
|
||||
{
|
||||
if (useShader)
|
||||
this->Use();
|
||||
glUniform2f(glGetUniformLocation(this->ID, name), value.x, value.y);
|
||||
}
|
||||
void Shader::SetVector3f(const GLchar* name, GLfloat x, GLfloat y, GLfloat z, GLboolean useShader)
|
||||
{
|
||||
if (useShader)
|
||||
this->Use();
|
||||
glUniform3f(glGetUniformLocation(this->ID, name), x, y, z);
|
||||
}
|
||||
void Shader::SetVector3f(const GLchar* name, const glm::vec3& value, GLboolean useShader)
|
||||
{
|
||||
if (useShader)
|
||||
this->Use();
|
||||
glUniform3f(glGetUniformLocation(this->ID, name), value.x, value.y, value.z);
|
||||
}
|
||||
void Shader::SetVector4f(const GLchar* name, GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLboolean useShader)
|
||||
{
|
||||
if (useShader)
|
||||
this->Use();
|
||||
glUniform4f(glGetUniformLocation(this->ID, name), x, y, z, w);
|
||||
}
|
||||
void Shader::SetVector4f(const GLchar* name, const glm::vec4& value, GLboolean useShader)
|
||||
{
|
||||
if (useShader)
|
||||
this->Use();
|
||||
glUniform4f(glGetUniformLocation(this->ID, name), value.x, value.y, value.z, value.w);
|
||||
}
|
||||
void Shader::SetMatrix4(const GLchar* name, const glm::mat4& matrix, GLboolean useShader)
|
||||
{
|
||||
if (useShader)
|
||||
this->Use();
|
||||
glUniformMatrix4fv(glGetUniformLocation(this->ID, name), 1, GL_FALSE, glm::value_ptr(matrix));
|
||||
}
|
||||
|
||||
|
||||
void Shader::checkCompileErrors(GLuint object, std::string type)
|
||||
{
|
||||
GLint success;
|
||||
GLchar infoLog[1024];
|
||||
if (type != "PROGRAM")
|
||||
{
|
||||
glGetShaderiv(object, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(object, 1024, NULL, infoLog);
|
||||
std::cout << "| ERROR::SHADER: Compile-time error: Type: " << type << "\n"
|
||||
<< infoLog << "\n -- --------------------------------------------------- -- "
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
glGetProgramiv(object, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(object, 1024, NULL, infoLog);
|
||||
std::cout << "| ERROR::Shader: Link-time error: Type: " << type << "\n"
|
||||
<< infoLog << "\n -- --------------------------------------------------- -- "
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
41
shaders/Shader.h
Normal file
41
shaders/Shader.h
Normal 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);
|
||||
};
|
||||
14
shaders/shader.frag
Normal file
14
shaders/shader.frag
Normal file
@@ -0,0 +1,14 @@
|
||||
#version 330 core
|
||||
in vec2 texCoord;
|
||||
|
||||
out vec4 fragColor;
|
||||
|
||||
uniform sampler2D image;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 texColor = texture(image, texCoord);
|
||||
if(texColor.a < 0.1)
|
||||
discard;
|
||||
fragColor = texColor;
|
||||
}
|
||||
15
shaders/shader.vert
Normal file
15
shaders/shader.vert
Normal file
@@ -0,0 +1,15 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec4 aPos;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
out vec2 texCoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * vec4(aPos.xy,0.0,1.0);
|
||||
texCoord = aPos.zw;
|
||||
}
|
||||
14
shaders/shaderTilemap.frag
Normal file
14
shaders/shaderTilemap.frag
Normal file
@@ -0,0 +1,14 @@
|
||||
#version 330 core
|
||||
in vec2 texCoord;
|
||||
|
||||
out vec4 fragColor;
|
||||
|
||||
uniform sampler2D image;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 texColor = texture(image, texCoord);
|
||||
if(texColor.a < 0.1)
|
||||
discard;
|
||||
fragColor = texColor;
|
||||
}
|
||||
15
shaders/shaderTilemap.vert
Normal file
15
shaders/shaderTilemap.vert
Normal file
@@ -0,0 +1,15 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec4 aPos;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
uniform int offset[];
|
||||
out vec2 texCoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(aPos.xy,0.0,1.0);
|
||||
texCoord = aPos.zw;
|
||||
}
|
||||
Reference in New Issue
Block a user