-
Notifications
You must be signed in to change notification settings - Fork 1
/
Shader.h
76 lines (70 loc) · 2.01 KB
/
Shader.h
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
#pragma once
#include "4dm.h"
namespace fdm
{
class Shader
{
public:
enum shaderType : int
{
VERTEX_SHADER = GL_VERTEX_SHADER,
GEOMETRY_SHADER = GL_GEOMETRY_SHADER,
FRAGMENT_SHADER = GL_FRAGMENT_SHADER
};
unsigned int ID;
// thank you mashed potatoes for this
bool load(const stl::string& vertexPath, const stl::string& geometryPath, const stl::string& fragmentPath)
{
if (ID != 0)
{
glDeleteProgram(ID);
}
unsigned int vertex = compileShader(vertexPath, VERTEX_SHADER);
unsigned int geometry = compileShader(geometryPath, GEOMETRY_SHADER);
unsigned int fragment = compileShader(fragmentPath, FRAGMENT_SHADER);
if (vertex != 0 && geometry != 0 && fragment != 0)
{
// shader Program
ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, geometry);
glAttachShader(ID, fragment);
glLinkProgram(ID);
// print linking errors if any
int success;
char infoLog[512];
glGetProgramiv(ID, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(ID, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
}
// delete the shaders as they're linked into our program now and no longer necessary
glDeleteShader(vertex);
glDeleteShader(geometry);
glDeleteShader(fragment);
return ID != 0;
}
bool load(const stl::string& vertexPath, const stl::string& fragmentPath)
{
return reinterpret_cast<bool(__thiscall*)(Shader*, const stl::string & vertexPath, const stl::string & fragmentPath)>(
getFuncAddr((int)Func::Shader::load)
)(this, vertexPath, fragmentPath);
}
unsigned int compileShader(const stl::string& path, shaderType type) const
{
return reinterpret_cast<unsigned int(__thiscall*)(const Shader*, const stl::string & path, shaderType fragmentPath)>(
getFuncAddr((int)Func::Shader::compileShader)
)(this, path, type);
}
unsigned int id() const
{
return ID;
}
void use() const
{
glUseProgram(ID);
}
};
}