123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- #include "rendering/Shader.h"
- #include <stdio.h>
- #include "utils/Logger.h"
- Shader::Shader() : shaders(0), program(0) {
- }
- Shader::~Shader() {
- for(GL::Shader shader : shaders) {
- GL::deleteShader(shader);
- }
- GL::deleteProgram(program);
- }
- static bool endsWith(const char* path, int length, const char* ending) {
- int endingLength = static_cast<int>(strlen(ending));
- if(length < endingLength) {
- return false;
- }
- return strcmp(path + (length - endingLength), ending) == 0;
- }
- GL::ShaderType Shader::getShaderType(const char* path) const {
- int length = static_cast<int>(strlen(path));
- if(endsWith(path, length, ".vs")) {
- return GL::ShaderType::VERTEX;
- } else if(endsWith(path, length, ".fs")) {
- return GL::ShaderType::FRAGMENT;
- } else if(endsWith(path, length, ".gs")) {
- return GL::ShaderType::GEOMETRY;
- } else if(endsWith(path, length, ".tcs")) {
- return GL::ShaderType::TESSELATION_CONTROL;
- } else if(endsWith(path, length, ".tes")) {
- return GL::ShaderType::TESSELATION_EVALUATION;
- }
- return GL::ShaderType::INVALID;
- }
- Error Shader::readAndCompile(const char* path, GL::Shader& s,
- GL::ShaderType st) {
- List<char> code;
- Error error = readFile(code, path);
- if(error.has()) {
- return error;
- }
- return compileType(s, code, st);
- }
- Error Shader::readFile(List<char>& code, const char* path) const {
- FILE* in = fopen(path, "r");
- if(in == nullptr) {
- Error e = {"cannot read file: "};
- e.message.append(path);
- return e;
- }
- while(true) {
- int c = fgetc(in);
- if(c == EOF) {
- break;
- }
- code.add(static_cast<char>(c));
- }
- code.add('\0');
- if(fclose(in) != 0) {
- Error e = {"cannot close file: "};
- e.message.append(path);
- return e;
- }
- return {};
- }
- Error Shader::compileType(GL::Shader& s, const List<char>& code,
- GL::ShaderType st) {
- s = GL::createShader(st);
- GL::compileShader(s, code.begin());
- Error error = GL::getError("compile error");
- if(error.has()) {
- return error;
- }
- return GL::getCompileError(s);
- }
- void Shader::use() const {
- GL::useProgram(program);
- }
- void Shader::setMatrix(const char* name, const float* data) {
- GL::setMatrix(program, name, data);
- }
- void Shader::setMatrix(const char* name, const Matrix& m) {
- setMatrix(name, m.getValues());
- }
- void Shader::setInt(const char* name, int data) {
- GL::setInt(program, name, data);
- }
- void Shader::setFloat(const char* name, float data) {
- GL::setFloat(program, name, data);
- }
- void Shader::setVector(const char* name, const Vector2& v) {
- GL::set2Float(program, name, &(v[0]));
- }
- void Shader::setVector(const char* name, const Vector3& v) {
- GL::set3Float(program, name, &(v[0]));
- }
- void Shader::setVector(const char* name, const Vector4& v) {
- GL::set4Float(program, name, &(v[0]));
- }
|