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

View File

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