-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShader.cpp
114 lines (95 loc) · 3.26 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
#include "Shader.h"
#include <fstream>
#include <iostream>
#include <string.h>
#include <stdlib.h>
Shader::Shader()
{
}
Shader::Shader(std::string filepath)
{
std::cout << readText(filepath) << std::endl;
}
std::string Shader::readText(std::string filepath)
{
std::ifstream input(filepath.c_str(), std::ios::in);
std::string retval;
if(input.is_open())
{
input.seekg(0, input.end);
int length = input.tellg();
input.seekg(0,input.beg);
char *buffer = new char[length + 1];
input.read(buffer, length);
buffer[length] = '\0';
retval.insert(0, buffer); //Insert buffer string into retval string at the start position 0
input.close();
}
else
{
std::cout << "Failed to open file:" << filepath << std::endl;
}
return retval;
}
GLuint Shader::LoadShaders(std::vector<struct ShaderList> shaders)
{
GLuint program = glCreateProgram();
std::vector<GLuint> compiled_shaders;
for(std::vector<struct ShaderList>::iterator it = shaders.begin(); it != shaders.end(); it++)
{
struct ShaderList current = *it;
GLuint shaderid = glCreateShader(current.type);
std::string shaderText = readText(current.path);
GLint length = shaderText.size();
const char *blah = shaderText.c_str();
glShaderSource(shaderid, 1, &blah, 0);
glCompileShader(shaderid);
validateShader(shaderid, current.path.c_str());
glAttachShader(program, shaderid);
}
glLinkProgram(program);
validateProgram(program);
return program;
}
void Shader::validateShader(GLuint shader, const char* file)
{
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if(status == GL_TRUE)
{
std::cout << "Loaded \"" << file << "\" successfully" << std::endl;
}
else
{
GLsizei logSize;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logSize);
GLchar *buffer = new GLchar[logSize];
GLsizei length = 0;
glGetShaderInfoLog(shader, logSize, &length, buffer);
if(length > 0)
{
std::cerr << "Shader:" << shader << (file? file : "") << " compile error: " << buffer << std::endl;
}
}
}
bool Shader::validateProgram(GLuint program)
{
bool retval = true;
GLint status;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if(status == GL_TRUE)
{
retval =- true;
std::cout << "Successfully linked shaders into program" << std::endl;
}
else
{
GLsizei logLength;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
GLsizei length = 0;
GLchar *buffer = new GLchar[logLength];
glGetProgramInfoLog(program, logLength, &length, buffer);
std::cout << "Program failed to link: " << buffer << std::endl;
}
return retval;
}