-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shader.cpp
executable file
·119 lines (92 loc) · 2.95 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
#include "Shader.h"
Shader::Shader()
{}
Shader::Shader( const char* vertPath, const char* fragPath )
{
loadAndCompile( vertPath, fragPath );
}
Shader::~Shader()
{
glDeleteProgram( this->programID );
}
void Shader::loadAndCompile( const char* vertPath, const char* fragPath )
{
std::string vsCode = "";
std::ifstream vsStream( vertPath, std::ios::in );
fprintf( stdout, "loading vertex shader %s\n", vertPath );
if( vsStream.is_open() )
{
std::string line;
while( std::getline( vsStream, line ) )
vsCode += line + '\n';
vsStream.close();
}
else
{
fprintf( stderr, "Error: could not find vertex shader %s\n", vertPath );
return;
}
std::string fsCode = "";
std::ifstream fsStream( fragPath, std::ios::in );
fprintf( stdout, "loading fragment shader %s\n", fragPath );
if( fsStream.is_open() )
{
std::string line;
while( std::getline( fsStream, line ) )
fsCode += line + '\n';
fsStream.close();
}
else
{
fprintf( stderr, "Error: could not find fragment shader %s\n", fragPath );
return;
}
fprintf( stdout, "Compiling vertexShader %s\n", vertPath );
const char* vertCode = vsCode.c_str();
GLuint vertShader = glCreateShader( GL_VERTEX_SHADER );
glShaderSource( vertShader, 1, &vertCode, NULL );
glCompileShader( vertShader );
GLint result = GL_FALSE;
int infoLogLength;
glGetShaderiv( vertShader, GL_COMPILE_STATUS, &result );
glGetShaderiv( vertShader, GL_INFO_LOG_LENGTH, &infoLogLength );
if( infoLogLength > 0 )
{
std::vector< char > errorMsg( infoLogLength );
glGetShaderInfoLog( vertShader, infoLogLength, NULL, &errorMsg[ 0 ] );
fprintf( stderr, "%s\n", &errorMsg[ 0 ] );
}
fprintf( stdout, "Compiling fragmentShader %s\n", fragPath );
const char* fragCode = fsCode.c_str();
GLuint fragShader = glCreateShader( GL_FRAGMENT_SHADER );
glShaderSource( fragShader, 1, &fragCode, NULL );
glCompileShader( fragShader );
glGetShaderiv( fragShader, GL_COMPILE_STATUS, &result );
glGetShaderiv( fragShader, GL_INFO_LOG_LENGTH, &infoLogLength );
if( infoLogLength > 0 )
{
std::vector< char > errorMsg( infoLogLength );
glGetShaderInfoLog( fragShader, infoLogLength, NULL, &errorMsg[ 0 ] );
fprintf( stderr, "%s\n", &errorMsg[ 0 ] );
}
fprintf( stdout, "Linking program...\n" );
GLuint tmpProgID = glCreateProgram();
glAttachShader( tmpProgID, vertShader );
glAttachShader( tmpProgID, fragShader );
glLinkProgram( tmpProgID );
glGetProgramiv( tmpProgID, GL_LINK_STATUS, &result );
glGetProgramiv( tmpProgID, GL_INFO_LOG_LENGTH, &infoLogLength );
if( infoLogLength > 0 )
{
std::vector< char > errorMsg( infoLogLength );
glGetProgramInfoLog( tmpProgID, infoLogLength, NULL, &errorMsg[ 0 ] );
fprintf( stderr, "%s\n", &errorMsg[ 0 ] );
}
glDeleteShader( vertShader );
glDeleteShader( fragShader );
this->programID = tmpProgID;
}
GLuint Shader::getProgram()
{
return this->programID;
}