-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shader.cpp
executable file
·130 lines (97 loc) · 2.82 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
// Shader.cpp
// SDLOpenglTexture
//
// Created by Rafael Nogueira on 10/07/2018.
// Copyright © 2018 Rafael Nogueira. All rights reserved.
//
#include "Shader.hpp"
Shader::Shader(void)
{
}
Shader::Shader(const char* vertexPath, const char * fragmentPath)
{
std::string vertexFileData;
std::string fragmentFileData;
std::ifstream vertexFile;
std::ifstream fragmentFile;
vertexFile.open(vertexPath);
fragmentFile.open(fragmentPath);
if(vertexFile.is_open())
{
std::stringstream vertexStream;
vertexStream << vertexFile.rdbuf();
vertexFileData = vertexStream.str();
vertexFile.close();
}
if(fragmentFile.is_open())
{
std::stringstream fragmentStream;
fragmentStream << fragmentFile.rdbuf();
fragmentFileData = fragmentStream.str();
fragmentFile.close();
}
const char* vertexSource = vertexFileData.c_str();
const char* fragmentSource = fragmentFileData.c_str();
GLuint vertexShader, fragmentShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, nullptr);
glCompileShader(vertexShader);
shaderCompileLog(vertexShader);
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1 , &fragmentSource, nullptr);
glCompileShader(fragmentShader);
shaderCompileLog(fragmentShader);
IDProgram = glCreateProgram();
glAttachShader(IDProgram, vertexShader);
glAttachShader(IDProgram, fragmentShader);
glLinkProgram(IDProgram);
programLog(IDProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
Shader::~Shader(void)
{
}
void Shader::Use(void)
{
glUseProgram(IDProgram);
}
void Shader::setBool(const std::string &name, bool value)
{
glUniform1i(glGetUniformLocation(IDProgram, name.c_str()), (int)value);
}
void Shader::setInt(const std::string &name, int value)
{
glUniform1i(glGetUniformLocation(IDProgram, name.c_str()), value);
}
void Shader::setFloat(const std::string &name, float value)
{
glUniform1f(glGetUniformLocation(IDProgram, name.c_str()), value);
}
GLuint Shader::getProgram(void)
{
return this->IDProgram;
}
void Shader::shaderCompileLog(GLuint &shaderPtr)
{
int success;
char info[512];
glGetShaderiv(shaderPtr, GL_COMPILE_STATUS, &success);
if(!success)
{
glGetShaderInfoLog(shaderPtr , 512, nullptr, info);
std::cout<<info << std::endl;
}
}
void Shader::programLog(GLuint &ID)
{
char info[512];
int success;
glGetProgramiv(ID , GL_LINK_STATUS, &success);
if(!success)
{
glGetProgramInfoLog(ID, 512, NULL, info);
std::cout << "SHADER PROGRAM LINKING_FAIL: " << info << std::endl;
}
}