-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShader.cpp
More file actions
68 lines (57 loc) · 1.58 KB
/
Copy pathShader.cpp
File metadata and controls
68 lines (57 loc) · 1.58 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
#include "Shader.hpp"
#include <sstream>
#include <iostream>
#include <streambuf>
#include <fstream>
#include <cstdio>
Shader::Shader()
:id(-1)
{}
Shader::Shader(std::string fpath, GLenum type){
loadFromFile(fpath, type);
}
Shader::~Shader(){
glDeleteShader(id);
}
void Shader::loadFromFile(std::string fpath, GLenum type){
std::ifstream file(fpath);
if(!file){
printf("[error]: couldn't open %s", fpath.c_str());
}
shaderCode = std::string((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
compile(type);
}
void Shader::loadFromString(std::string str, GLenum type){
shaderCode = str;
compile(type);
}
bool Shader::compile(GLenum type){
shaderType = type;
id = glCreateShader(type);
const GLchar* tmpCode = &shaderCode[0];
glShaderSource(id, 1, &tmpCode, NULL);
glCompileShader(id);
// Check for compile time errors
GLint success;
GLchar infoLog[512];
glGetShaderiv(id, GL_COMPILE_STATUS, &success);
if (!success){
glGetShaderInfoLog(id, 512, NULL, infoLog);
printf("ERROR::SHADER::COMPILATION_FAILED: %s, (%s)",
infoLog, getShaderName().c_str());
}
return success;
}
GLuint Shader::getId(){
return id;
}
std::string Shader::getShaderName(){
if(shaderType == GL_VERTEX_SHADER)return "VERTEX_SHADER";
if(shaderType == GL_FRAGMENT_SHADER)return "FRAGMENT_SHADER";
//opengl ES 3.2 and higher
#ifdef GEOMETRY_SHADER_ENABLED
if(shaderType == GL_GEOMETRY_SHADER) return "GEOMETRY_SHADER";
#endif
return "[error] UNKNOWN_SHADER [error]";
}