S h a d e r s - can sleep now
This commit is contained in:
12
resources/shaders/simple.frag
Normal file
12
resources/shaders/simple.frag
Normal file
@@ -0,0 +1,12 @@
|
||||
#version 330
|
||||
|
||||
in vec2 TexCoord;
|
||||
out vec4 outColour;
|
||||
|
||||
uniform vec3 triangleColour;
|
||||
|
||||
uniform sampler2D tex;
|
||||
|
||||
void main() {
|
||||
outColour = texture(tex, TexCoord);
|
||||
}
|
||||
15
resources/shaders/simple.vert
Normal file
15
resources/shaders/simple.vert
Normal file
@@ -0,0 +1,15 @@
|
||||
#version 330
|
||||
|
||||
in vec3 position;
|
||||
in vec2 texcoord;
|
||||
|
||||
out vec2 TexCoord;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 proj;
|
||||
|
||||
void main() {
|
||||
TexCoord = texcoord;
|
||||
gl_Position = proj * view * model * vec4(position, 1.0);
|
||||
}
|
||||
BIN
resources/textures/dirt.png
Normal file
BIN
resources/textures/dirt.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 266 B |
BIN
resources/textures/grass_side.png
Normal file
BIN
resources/textures/grass_side.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 660 B |
BIN
resources/textures/grass_top.png
Normal file
BIN
resources/textures/grass_top.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 766 B |
@@ -3,6 +3,7 @@
|
||||
#define LOGGER_DEFINITION
|
||||
#include <logger.h>
|
||||
|
||||
#include "renderer/shader.hpp"
|
||||
#include "renderer/camera.hpp"
|
||||
|
||||
#include "common.hpp"
|
||||
@@ -119,6 +120,10 @@ void Game::Run() {
|
||||
const float clear[] = { 0.1f, 0.45f, 0.9f, 1.0f };
|
||||
glClearBufferfv(GL_COLOR, 0, clear);
|
||||
|
||||
Shader shader;
|
||||
shader.Load("E:/Games/minecraft/resources/shaders/simple");
|
||||
shader.Use();
|
||||
|
||||
while (IsDisplayOpen) {
|
||||
|
||||
Input(&e);
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
#include "shader.hpp"
|
||||
|
||||
Shader::Shader()
|
||||
: m_fileReader() {
|
||||
|
||||
m_program = 0;
|
||||
m_frag = 0;
|
||||
m_vert = 0;
|
||||
|
||||
m_logger = std::make_shared<Logger>();
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Shader::Load(std::string path) {
|
||||
|
||||
std::string vertexLocation = path + ".vert";
|
||||
Load(vertexLocation, GL_VERTEX_SHADER);
|
||||
*m_logger << LOGGER_INFO << "Vertex shader at '" << vertexLocation << "' loaded..." << LOGGER_ENDL;
|
||||
|
||||
|
||||
std::string fragmentLocation = path + ".frag";
|
||||
Load(fragmentLocation, GL_FRAGMENT_SHADER);
|
||||
*m_logger << LOGGER_INFO << "Fragment shader at '" << fragmentLocation << "' loaded..." << LOGGER_ENDL;
|
||||
|
||||
Link();
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Shader::Load(std::string path, GLenum type) {
|
||||
|
||||
GLuint activeShader = 0;
|
||||
|
||||
if (type == GL_VERTEX_SHADER)
|
||||
m_vert = activeShader = glCreateShader(type);
|
||||
|
||||
if (type == GL_FRAGMENT_SHADER)
|
||||
m_frag = activeShader = glCreateShader(type);
|
||||
|
||||
std::string loadedShaderSource = m_fileReader.LoadTextFromFile(path);
|
||||
const char* shaderSource = loadedShaderSource.c_str();
|
||||
int shaderSourceLength = loadedShaderSource.length();
|
||||
|
||||
glShaderSource(activeShader, 1, &shaderSource, &shaderSourceLength);
|
||||
|
||||
}
|
||||
|
||||
void Shader::Link() {
|
||||
|
||||
if (m_vert == 0 || m_frag == 0) {
|
||||
*m_logger << LOGGER_ERROR << "Failed to link programs: Both programs not present" << LOGGER_ENDL;
|
||||
return;
|
||||
}
|
||||
|
||||
glCompileShader(m_vert);
|
||||
if (m_CheckShader(m_vert)) {
|
||||
*m_logger << LOGGER_INFO << "Vertex shader '" << m_vert << "' compiled..." << LOGGER_ENDL;
|
||||
}
|
||||
|
||||
glCompileShader(m_frag);
|
||||
if (m_CheckShader(m_frag)) {
|
||||
*m_logger << LOGGER_INFO << "Fragment shader '" << m_frag << "' compiled..." << LOGGER_ENDL;
|
||||
}
|
||||
|
||||
m_program = glCreateProgram();
|
||||
|
||||
glAttachShader(m_program, m_vert);
|
||||
glAttachShader(m_program, m_frag);
|
||||
|
||||
glLinkProgram(m_program);
|
||||
|
||||
glDeleteShader(m_vert);
|
||||
glDeleteShader(m_frag);
|
||||
|
||||
*m_logger << LOGGER_INFO << "Program '" << m_program << "' loaded..." << LOGGER_ENDL;
|
||||
|
||||
}
|
||||
|
||||
void Shader::Use() {
|
||||
|
||||
glUseProgram(m_program);
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool Shader::m_CheckShader(GLuint uid) {
|
||||
|
||||
GLint status = GL_TRUE;
|
||||
|
||||
glGetShaderiv(uid, GL_COMPILE_STATUS, &status);
|
||||
|
||||
if (status == GL_FALSE) {
|
||||
char buf[512];
|
||||
glGetShaderInfoLog(uid, 512, NULL, buf);
|
||||
*m_logger << LOGGER_ERROR << buf << LOGGER_ENDL;
|
||||
delete buf;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Shader::~Shader() {
|
||||
|
||||
glDeleteProgram(m_program);
|
||||
glDeleteShader(m_vert);
|
||||
glDeleteShader(m_frag);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,33 @@
|
||||
#ifndef MINECRAFT_RENDERER_SHADER_H_
|
||||
#define MINECRAFT_RENDERER_SHADER_H_
|
||||
|
||||
#include <logger.h>
|
||||
|
||||
#include "../util/filereader.hpp"
|
||||
#include "../common.hpp"
|
||||
|
||||
class Shader {
|
||||
public:
|
||||
Shader();
|
||||
|
||||
void Load(std::string path);
|
||||
void Load(std::string path, GLenum type);
|
||||
|
||||
void Link();
|
||||
|
||||
void Use();
|
||||
|
||||
~Shader();
|
||||
private:
|
||||
std::shared_ptr<Logger> m_logger;
|
||||
|
||||
bool m_CheckShader(GLuint uid);
|
||||
|
||||
FileReader m_fileReader;
|
||||
|
||||
GLuint m_program;
|
||||
GLuint m_vert;
|
||||
GLuint m_frag;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
15
src/util/filereader.cpp
Normal file
15
src/util/filereader.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "filereader.hpp"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
FileReader::FileReader() {
|
||||
|
||||
}
|
||||
|
||||
std::string FileReader::LoadTextFromFile(std::string path) {
|
||||
std::ifstream t(path);
|
||||
std::string text((std::istreambuf_iterator<char>(t)),
|
||||
std::istreambuf_iterator<char>());
|
||||
return text;
|
||||
}
|
||||
|
||||
14
src/util/filereader.hpp
Normal file
14
src/util/filereader.hpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef MINECRAFT_UTIL_FILEREADER_H_
|
||||
#define MINECRAFT_UTIL_FILEREADER_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
class FileReader {
|
||||
public:
|
||||
FileReader();
|
||||
|
||||
std::string LoadTextFromFile(std::string path);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user