-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaderProgram.cpp
More file actions
68 lines (60 loc) · 2.4 KB
/
Copy pathShaderProgram.cpp
File metadata and controls
68 lines (60 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "ShaderProgram.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <exception>
#include <ios>
// Create a GPU program i.e., a graphics pipeline
ShaderProgram::ShaderProgram() : _id(glCreateProgram()) {}
ShaderProgram::~ShaderProgram()
{
glDeleteProgram(_id);
}
std::string ShaderProgram::file2String(const std::string &filename)
{
std::ifstream input(filename.c_str());
if(!input)
throw std::ios_base::failure("[Shader Program][file2String] Error: cannot open " + filename);
std::stringstream buffer;
buffer << input.rdbuf();
return buffer.str();
}
void ShaderProgram::loadShader(GLenum type, const std::string &shaderFilename)
{
// Loads and compile a shader, before attaching it to a program
GLuint shader = glCreateShader(type); // Create the shader, e.g., a vertex shader to be applied to every single vertex of a mesh
std::string shaderSourceString = file2String(shaderFilename); // Loads the shader source from a file to a C++ string
if(shaderSourceString.empty()) {
std::cerr << "No content in shader " << shaderFilename << std::endl;
glDeleteShader(shader);
return;
}
const GLchar *shaderSource = (const GLchar *) shaderSourceString.c_str(); // Interface the C++ string through a C pointer
glShaderSource(shader, 1, &shaderSource, NULL); // Load the vertex shader source code
glCompileShader(shader); // THe GPU driver compile the shader
GLint compiled;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if(!compiled) {
GLsizei len;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
GLchar *log = new GLchar[len+1];
glGetShaderInfoLog(shader, len, &len, log);
std::cerr << "Compilation error in shader " << shaderFilename << " : " << std::endl << log << std::endl;
delete [] log;
glDeleteShader(shader);
return;
}
glAttachShader(_id, shader); // Set the vertex shader as the one ot be used with the program/pipeline
glDeleteShader(shader);
}
std::shared_ptr<ShaderProgram> ShaderProgram::genBasicShaderProgram(
const std::string &vertexShaderFilename,
const std::string &fragmentShaderFilename)
{
std::shared_ptr<ShaderProgram> shaderProgramPtr = std::make_shared<ShaderProgram>();
shaderProgramPtr->loadShader(GL_VERTEX_SHADER, vertexShaderFilename);
shaderProgramPtr->loadShader(GL_FRAGMENT_SHADER, fragmentShaderFilename);
shaderProgramPtr->link();
shaderProgramPtr->use();
return shaderProgramPtr;
}