-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShaderProgram.hpp
More file actions
106 lines (93 loc) · 2.69 KB
/
ShaderProgram.hpp
File metadata and controls
106 lines (93 loc) · 2.69 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once
#include <type_traits>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "GLAD/glad.h"
#include "Shader.hpp"
struct ShaderProgram
{
GLuint Index = 0;
ShaderProgram()
{
Index = glCreateProgram();
}
void Attach(Shader shader)
{
glAttachShader(Index, shader.Index);
}
void Link()
{
glLinkProgram(Index);
}
template <typename T, auto F, auto S>
void Location(const GLchar* name)
{
const GLint IdxLocation = glGetAttribLocation(Index, name);
glEnableVertexAttribArray(IdxLocation);
glVertexAttribPointer(
IdxLocation,
S,
GL_FLOAT,
GL_FALSE,
sizeof(T),
(void*)F);
}
template <typename... Args>
void Uniform(const GLchar* name, Args... args)
{
using FirstType = std::tuple_element_t<0, std::tuple<Args...>>;
int UniformLocation = glGetUniformLocation(Index, name);
const std::size_t SizeOfArgs = sizeof...(Args);
if constexpr (std::is_same_v<FirstType, glm::mat4>)
{
return glUniformMatrix4fv(UniformLocation, 1, GL_FALSE, glm::value_ptr(args...));
}
else if constexpr (std::is_same_v<FirstType, glm::vec3>)
{
return glUniform3fv(UniformLocation, 1, glm::value_ptr(args...));
}
else if constexpr (std::is_same_v<FirstType, int>)
{
if constexpr (SizeOfArgs == 1)
{
return glUniform1i(UniformLocation, args...);
}
else if constexpr (SizeOfArgs == 2)
{
return glUniform2i(UniformLocation, args...);
}
else if constexpr (SizeOfArgs == 3)
{
return glUniform3i(UniformLocation, args...);
}
else if constexpr (SizeOfArgs == 4)
{
return glUniform4i(UniformLocation, args...);
}
}
else if constexpr (std::is_same_v<FirstType, float>)
{
if constexpr (SizeOfArgs == 1)
{
return glUniform1f(UniformLocation, args...);
}
else if constexpr (SizeOfArgs == 2)
{
return glUniform2f(UniformLocation, args...);
}
else if constexpr (SizeOfArgs == 3)
{
return glUniform3f(UniformLocation, args...);
}
else if constexpr (SizeOfArgs == 4)
{
return glUniform4f(UniformLocation, args...);
}
}
}
void Use()
{
glUseProgram(Index);
}
};