-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader.cpp
55 lines (44 loc) · 1.16 KB
/
shader.cpp
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <GL/glew.h>
#include "shader.hpp"
using std::cout;
using std::cerr;
const std::string readFile(const std::string &filename);
Shader::Shader() : shader(0)
{
}
Shader::~Shader()
{
if (shader != 0 ) {
glDeleteShader(shader);
}
}
bool Shader::compile(const std::string& filename, ShaderType shaderType)
{
cout << "loading shader " << filename << ", type: " << static_cast<int>(shaderType) << '\n';
const std::string contents = readFile(filename);
const char * cStr = contents.c_str();
shader = glCreateShader(shaderType==ShaderType::VERTEX?GL_VERTEX_SHADER:GL_FRAGMENT_SHADER);
glShaderSource(shader, 1, &cStr, NULL);
glCompileShader(shader);
int success;
char infoLog[512];
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(shader, 512, NULL, infoLog);
cerr << "Error: shader compilation failed: " << infoLog << std::endl;
return false;
}
return true;
}
const std::string readFile(const std::string &filename)
{
std::ifstream f("resources/" + filename);
std::stringstream strStream;
strStream << f.rdbuf();
return strStream.str();
}