Merge ShaderProgram and Shader into a single Shader class

master
rubenwardy 2019-08-08 13:26:42 +01:00
parent 6388cb4018
commit 2439edb8fd
8 changed files with 42 additions and 84 deletions

View File

@ -36,8 +36,7 @@ set(SOURCE_LIST
src/main.cpp
src/engine/Window.cpp
src/engine/Shader.cpp
src/engine/ShaderProgram.cpp
src/engine/ShaderProgram.hpp
src/engine/Shader.hpp
src/graph/Node.cpp
src/graph/Component.cpp
src/game/MeshDrawComponent.cpp

View File

@ -1,9 +1,15 @@
#include <fstream>
#include <assert.h>
#include <iostream>
#include <fstream>
#include "Shader.hpp"
#include "gl.hpp"
Shader::Shader(ShaderType type, const char *source) {
bool loadShader(int &handle, int type, const std::string &path) {
std::ifstream t(path);
std::string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
const char *source = str.c_str();
handle = glCreateShader(type);
glShaderSource(handle, 1, &source, NULL);
glCompileShader(handle);
@ -14,18 +20,33 @@ Shader::Shader(ShaderType type, const char *source) {
if (!success) {
glGetShaderInfoLog(handle, 512, NULL, infoLog);
std::cout << "Failed to compile shader:\n" << infoLog << "\nSource:\n" << source << std::endl;
return false;
}
return true;
}
Shader::Shader(const std::string &vpath, const std::string &fpath) {
int vert, frag;
loadShader(vert, GL_VERTEX_SHADER, vpath);
loadShader(frag, GL_FRAGMENT_SHADER, fpath);
handle = glCreateProgram();
glAttachShader(handle, vert);
glAttachShader(handle, frag);
glLinkProgram(handle);
int success;
glGetProgramiv(handle, GL_LINK_STATUS, &success);
if (!success) {
char infoLog[512];
glGetProgramInfoLog(handle, 512, NULL, infoLog);
std::cout << "Failed to compile shader program:\n" << infoLog << std::endl;
}
}
Shader Shader::Load(ShaderType type, const std::string &path) {
std::ifstream t(path);
std::string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
return Shader(type, str.c_str());
void Shader::use() const {
glUseProgram(handle);
}
Shader::~Shader() {
glDeleteShader(handle);
}

View File

@ -1,23 +1,9 @@
#pragma once
#include <string>
enum ShaderType {
FRAGMENT = 0x8B30,
VERTEX = 0x8B31
};
class Shader {
unsigned int handle;
public:
Shader(ShaderType type, const char *source);
Shader(const Shader &other) = delete;
~Shader();
static Shader Load(ShaderType type, const std::string &path);
operator unsigned int() const {
return handle;
}
Shader(const std::string &vpath, const std::string &fpath);
void use() const;
};

View File

@ -1,30 +0,0 @@
#include <assert.h>
#include <iostream>
#include "ShaderProgram.hpp"
#include "gl.hpp"
ShaderProgram::ShaderProgram() {
handle = glCreateProgram();
}
void ShaderProgram::attach(const Shader &shader) {
assert(!locked);
glAttachShader(handle, shader);
}
void ShaderProgram::link() {
glLinkProgram(handle);
locked = true;
int success;
glGetProgramiv(handle, GL_LINK_STATUS, &success);
if (!success) {
char infoLog[512];
glGetProgramInfoLog(handle, 512, NULL, infoLog);
std::cout << "Failed to compile shader program:\n" << infoLog << std::endl;
}
}
void ShaderProgram::use() const {
glUseProgram(handle);
}

View File

@ -1,14 +0,0 @@
#pragma once
#include "Shader.hpp"
class ShaderProgram {
unsigned int handle;
bool locked = false;
public:
ShaderProgram();
void attach(const Shader &shader);
void link();
void use() const;
};

View File

@ -6,7 +6,7 @@
const std::string MeshDrawComponent::className = "MeshDrawComponent";
MeshDrawComponent::MeshDrawComponent(Node::Ptr node, const ShaderProgram &shader):
MeshDrawComponent::MeshDrawComponent(Node::Ptr node, const Shader &shader):
Component(std::move(node)), shader(shader) {
glGenBuffers(1, &vbo);
glGenBuffers(1, &ebo);

View File

@ -1,14 +1,14 @@
#pragma once
#include "../graph/Component.hpp"
#include "../engine/ShaderProgram.hpp"
#include "../engine/Shader.hpp"
class MeshDrawComponent : public Component {
unsigned int vbo, ebo, vao, texture;
const ShaderProgram &shader;
const Shader &shader;
public:
MeshDrawComponent(Node::Ptr node, const ShaderProgram &shader);
MeshDrawComponent(Node::Ptr node, const Shader &shader);
void update(float delta) override;
void draw() const;

View File

@ -1,7 +1,7 @@
#include <iostream>
#include "engine/Window.hpp"
#include "engine/Shader.hpp"
#include "engine/ShaderProgram.hpp"
#include "engine/Shader.hpp"
#include "graph/Node.hpp"
#include "game/MeshDrawComponent.hpp"
#include "tests/tests.hpp"
@ -14,11 +14,7 @@ int main() {
}
Window window;
ShaderProgram shaderProgram;
shaderProgram.attach(Shader::Load(ShaderType::VERTEX, "assets/shaders/base.vert.glsl"));
shaderProgram.attach(Shader::Load(ShaderType::FRAGMENT, "assets/shaders/base.frag.glsl"));
shaderProgram.link();
Shader shaderProgram("assets/shaders/base.vert.glsl", "assets/shaders/base.frag.glsl");
auto root = std::make_shared<Node>();
{