-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShader.cpp
More file actions
78 lines (65 loc) · 1.57 KB
/
Copy pathShader.cpp
File metadata and controls
78 lines (65 loc) · 1.57 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
69
70
71
72
73
74
75
76
77
78
#include "Shader.h"
#include <fstream>
Shader::Shader()
{
shaderId = 0;
compiled = false;
}
void Shader::initFromSource(const ShaderType type, const std::string &source)
{
const char *sourcePtr = source.c_str();
GLint status;
char buffer[512];
switch (type)
{
case VERTEX_SHADER:
shaderId = glCreateShader(GL_VERTEX_SHADER);
break;
case FRAGMENT_SHADER:
shaderId = glCreateShader(GL_FRAGMENT_SHADER);
break;
}
if (shaderId == 0)
return;
glShaderSource(shaderId, 1, &sourcePtr, NULL);
glCompileShader(shaderId);
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &status);
compiled = (status == GL_TRUE);
glGetShaderInfoLog(shaderId, 512, NULL, buffer);
errorLog.assign(buffer);
}
bool Shader::initFromFile(const ShaderType type, const std::string &filename)
{
std::string shaderSource;
if (!loadShaderSource(filename, shaderSource))
return false;
initFromSource(type, shaderSource);
return true;
}
void Shader::free()
{
glDeleteShader(shaderId);
shaderId = 0;
compiled = false;
}
GLuint Shader::getId() const
{
return shaderId;
}
bool Shader::isCompiled() const
{
return compiled;
}
const std::string &Shader::log() const
{
return errorLog;
}
bool Shader::loadShaderSource(const std::string &filename, std::string &shaderSource)
{
std::ifstream fin;
fin.open(filename.c_str());
if (!fin.is_open())
return false;
shaderSource.assign(std::istreambuf_iterator<char>(fin), std::istreambuf_iterator<char>());
return true;
}