feat: split fileToShader into method

This commit is contained in:
Krzysztof Rudnicki 2022-12-11 20:17:11 +01:00
parent 2a393604c6
commit 36c0723878
3 changed files with 19 additions and 21 deletions

Binary file not shown.

View File

@ -9,40 +9,38 @@
#include <iostream> #include <iostream>
#include "shader.hpp" #include "shader.hpp"
const std::string Shader::fileToShader(const std::string shaderPath)
Shader::Shader(const std::string vertexPath, const std::string fragmentPath)
{ {
// 1. retrieve the vertex/fragment source code from filePath // 1. retrieve the vertex/fragment source code from filePath
std::string vertexCode; std::string shaderCode;
std::string fragmentCode; std::ifstream shaderFile;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
// ensure ifstream objects can throw exceptions: // ensure ifstream objects can throw exceptions:
vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit); shaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
try try
{ {
// open files // open files
vShaderFile.open(vertexPath); shaderFile.open(shaderPath);
fShaderFile.open(fragmentPath); std::stringstream shaderStream;
std::stringstream vShaderStream, fShaderStream;
// read file's buffer contents into streams // read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf(); shaderStream << shaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
// close file handlers // close file handlers
vShaderFile.close(); shaderFile.close();
fShaderFile.close();
// convert stream into string // convert stream into string
vertexCode = vShaderStream.str(); shaderCode = shaderStream.str();
fragmentCode = fShaderStream.str();
} }
catch (std::ifstream::failure& e) catch (std::ifstream::failure& e)
{ {
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl; std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl;
} }
const char* vShaderCode = vertexCode.c_str(); return shaderCode;
const char * fShaderCode = fragmentCode.c_str(); }
Shader::Shader(const std::string vertexPath, const std::string fragmentPath)
{
const std::string vshaderCodeString = this -> fileToShader(vertexPath);
const std::string fShaderCodeString = this -> fileToShader(fragmentPath);
const char* vShaderCode = vshaderCodeString.c_str();
const char * fShaderCode = fShaderCodeString.c_str();
// 2. compile shaders // 2. compile shaders
unsigned int vertex, fragment; unsigned int vertex, fragment;
// vertex shader // vertex shader

View File

@ -15,7 +15,7 @@ public:
// constructor generates the shader on the fly // constructor generates the shader on the fly
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
Shader(const std::string vertexPath, const std::string fragmentPath); Shader(const std::string vertexPath, const std::string fragmentPath);
const std::string fileToShader(const std::string shaderPath);
// activate the shader // activate the shader
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void use(); void use();