Transform shader sources to header files

This commit is contained in:
Dane Johnson
2021-01-17 18:03:09 -06:00
parent 5e8d693758
commit 0f3dbdff64
8 changed files with 59 additions and 43 deletions

View File

@@ -1,42 +1,12 @@
#include "Shader.h"
Shader::Shader(const char* vertexPath, const char* fragmentPath) {
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
vShaderFile.close();
fShaderFile.close();
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
} catch (std::ifstream::failure e) {
std::cerr << "Error reading shader file." << std::endl;
}
const char * vShaderCode = vertexCode.c_str();
const char * fShaderCode = fragmentCode.c_str();
Shader::Shader(const char* vertexCode, const char* fragmentCode) {
Id vertex, fragment;
int success;
char infoLog[512];
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glShaderSource(vertex, 1, &vertexCode, NULL);
glCompileShader(vertex);
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
if (!success) {
@@ -45,7 +15,7 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath) {
}
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glShaderSource(fragment, 1, &fragmentCode, NULL);
glCompileShader(fragment);
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if (!success) {

View File

@@ -1,11 +1,6 @@
#ifndef SHADER_H
#define SHADER_H
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <glm/gtc/type_ptr.hpp>
#include "types.h"
@@ -15,7 +10,7 @@ class Shader {
public:
Id id;
Shader(const char *vertexPath, const char *fragmentPath);
Shader(const char *vertexCode, const char *fragmentCode);
void Use();
void UpdateView(Matrix view);
void UpdateModel(Matrix model);

View File

@@ -1,6 +1,8 @@
#include "FlatShader.h"
#include "flat.vert.h"
#include "flat.frag.h"
FlatShader::FlatShader() : Shader("shaders/flat.vert", "shaders/flat.frag") {}
FlatShader::FlatShader() : Shader(flat_vert, flat_frag) {}
void FlatShader::UpdateColor(Vector3 color) {
glUniform3f(glGetUniformLocation(id, "color"), color.r, color.g, color.b);