Repository navigation
Expand file tree
/
Copy pathShader.cpp
More file actions
executable file
·127 lines (101 loc) · 3.32 KB
/
Copy pathShader.cpp
File metadata and controls
executable file
·127 lines (101 loc) · 3.32 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
123
124
125
126
127
#include "Shader.h"
namespace Engine
{
Shader::Shader()
:
progID( 0 )
{}
Shader::~Shader()
{
glDeleteProgram( this->progID );
}
void Shader::load( const std::string& vertexPath, const std::string& fragmentPath )
{
GLuint vertID = glCreateShader( GL_VERTEX_SHADER );
GLuint fragID = glCreateShader( GL_FRAGMENT_SHADER );
std::string vertexSource;
std::ifstream vertexStream( vertexPath.c_str() );
if( vertexStream.is_open() )
{
std::stringstream buff;
buff << vertexStream.rdbuf();
vertexSource = buff.str();
}
else
{
fprintf( stderr, "Could not find vertex shader: %s\n", vertexPath.c_str() );
return;
}
std::string fragSource;
std::ifstream fragmentStream( fragmentPath.c_str() );
if( fragmentStream.is_open() )
{
std::stringstream buff;
buff << fragmentStream.rdbuf();
fragSource = buff.str();
}
else
{
fprintf( stderr, "Could not find fragment shader: %s\n", fragmentPath.c_str() );
return;
}
int infoLogLength;
GLint compiled = GL_FALSE;
fprintf( stdout, "Compiling vertex shader..." );
// Vertex Shader
const char* vertexPointer = vertexSource.c_str();
glShaderSource( vertID, 1, &vertexPointer, NULL );
glCompileShader( vertID );
glGetShaderiv( vertID, GL_COMPILE_STATUS, &compiled );
if (!compiled)
{
glGetShaderiv( vertID, GL_INFO_LOG_LENGTH, &infoLogLength );
std::string vertShaderLog( infoLogLength, ' ' );
glGetShaderInfoLog( vertID, infoLogLength, &infoLogLength, &vertShaderLog[0] );
fprintf( stderr, "\n\n%s\n", &vertShaderLog[0] );
}
else
fprintf( stdout, " success!\n" );
fprintf( stdout, "Compiling fragment shader... " );
// Fragment Shader
const char* fragPointer = fragSource.c_str();
glShaderSource( fragID, 1, &fragPointer, NULL );
glCompileShader( fragID );
glGetShaderiv( fragID, GL_COMPILE_STATUS, &compiled );
if( !compiled )
{
glGetShaderiv( fragID, GL_INFO_LOG_LENGTH, &infoLogLength );
std::string fragShaderLog( infoLogLength, ' ' );
glGetShaderInfoLog( fragID, infoLogLength, NULL, &fragShaderLog[0] );
fprintf( stderr, "\n\n%s\n", &fragShaderLog[0] );
}
else
fprintf(stdout, " success!\n" );
fprintf( stdout, "\nLinking program... " );
GLuint tmpProgID = glCreateProgram();
glAttachShader( tmpProgID, vertID );
glAttachShader( tmpProgID, fragID );
glLinkProgram( tmpProgID );
glGetProgramiv( tmpProgID, GL_INFO_LOG_LENGTH, &infoLogLength );
if( !compiled )
{
glGetProgramiv( tmpProgID, GL_LINK_STATUS, &compiled );
std::string progLog( infoLogLength, ' ' );
glGetProgramInfoLog( tmpProgID, infoLogLength, &infoLogLength, &progLog[ 0 ] );
fprintf( stdout, "\n\n%s\n", &progLog[ 0 ] );
}
else
fprintf( stderr, " success!\n" );
glDeleteShader( vertID );
glDeleteShader( fragID );
this->progID = tmpProgID;
}
void Shader::bind() const
{
glUseProgram( this->progID );
}
GLuint Shader::getID() const
{
return this->progID;
}
}