-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshader.cpp
More file actions
executable file
·122 lines (95 loc) · 3.26 KB
/
shader.cpp
File metadata and controls
executable file
·122 lines (95 loc) · 3.26 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "shader.h"
#include <stdio.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
using namespace std;
Shader::Shader() :
_programId(0) {
}
Shader::~Shader() {
if(glIsProgram(_programId)) {
glDeleteProgram(_programId);
}
}
void Shader::load(const char *vertex_file_path,
const char *fragment_file_path) {
// create and compile vertex shader object
std::string vertexCode = getCode(vertex_file_path);
const char * vertexCodeC = vertexCode.c_str();
GLuint vertexId = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexId,1,&(vertexCodeC),NULL);
glCompileShader(vertexId);
cout << vertex_file_path << " :" << endl;
checkCompilation(vertexId);
// create and compile fragment shader object
std::string fragmentCode = getCode(fragment_file_path);
const char * fragmentCodeC = fragmentCode.c_str();
GLuint fragmentId = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentId,1,&(fragmentCodeC),NULL);
glCompileShader(fragmentId);
cout << fragment_file_path << " :" << endl;
checkCompilation(fragmentId);
// create, attach and link program object
_programId = glCreateProgram();
glAttachShader(_programId,vertexId);
glAttachShader(_programId,fragmentId);
glLinkProgram(_programId);
checkLinks(_programId);
// delete vertex and fragment ids
glDeleteShader(vertexId);
glDeleteShader(fragmentId);
}
void Shader::reload(const char *vertex_file_path,
const char *fragment_file_path) {
// check if the program already contains a shader
if(glIsProgram(_programId)) {
// delete it...
glDeleteProgram(_programId);
}
// ... and reload it
load(vertex_file_path,fragment_file_path);
}
void Shader::checkCompilation(GLuint shaderId) {
// check if the compilation was successfull (and display syntax errors)
// call it after each shader compilation
GLint result = GL_FALSE;
int infoLogLength;
glGetShaderiv(shaderId,GL_COMPILE_STATUS,&result);
glGetShaderiv(shaderId,GL_INFO_LOG_LENGTH,&infoLogLength);
if(infoLogLength>0) {
std::vector<char> message(infoLogLength+1);
glGetShaderInfoLog(shaderId,infoLogLength,NULL,&message[0]);
printf("%s\n", &message[0]);
}
}
void Shader::checkLinks(GLuint programId) {
// check if links were successfull (and display errors)
// call it after linking the program
GLint result = GL_FALSE;
int infoLogLength;
glGetProgramiv(programId,GL_LINK_STATUS,&result);
glGetProgramiv(programId,GL_INFO_LOG_LENGTH,&infoLogLength);
if(infoLogLength>0) {
std::vector<char> message(infoLogLength+1);
glGetProgramInfoLog(programId,infoLogLength,NULL,&message[0]);
printf("%s\n", &message[0]);
}
}
std::string Shader::getCode(const char *file_path) {
// return a string containing the source code of the input file
std::string shaderCode;
std::ifstream shaderStream(file_path,std::ios::in);
if(!shaderStream.is_open()) {
cout << "Unable to open " << file_path << endl;
return "";
}
std::string line = "";
while(getline(shaderStream,line))
shaderCode += "\n" + line;
shaderStream.close();
return shaderCode;
}