-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshaderprogram.cpp
61 lines (50 loc) · 1.41 KB
/
shaderprogram.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
#include "shaderprogram.h"
#include <GL/glew.h>
#include <iostream>
using namespace std;
ShaderProgram::ShaderProgram(): programObject(0)
{
}
ShaderProgram::~ShaderProgram()
{
programObject=0;
}
void ShaderProgram::init(const char* vName, const char* fName)
{
GLint success; //local variable to check status
//load and compile vertex shader
success = vertex.readAndCompile(vName,GL_VERTEX_SHADER);
if (!success)
{
throw "Vertex Compilation Error";
}
//load and compile fragment shader
success = fragment.readAndCompile(fName,GL_FRAGMENT_SHADER);
if (!success)
{
throw "Fragment Compilation Error";
}
//create programObject
programObject = glCreateProgram();
//attach shaders
glAttachShader(programObject, vertex.shaderObject);
glAttachShader(programObject, fragment.shaderObject);
glBindFragDataLocation(programObject, GL_NONE,"fragColor");
//link shaders in program
glLinkProgram(programObject);
glGetProgramiv (programObject, GL_LINK_STATUS, &success);
if (!success)
{
GLint maxLength = 0;
glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &maxLength);
char* errorLog = new char [maxLength];
glGetProgramInfoLog(programObject, maxLength, &maxLength, &errorLog[0]);
cout << errorLog << endl;
delete[] errorLog;
glDetachShader(programObject,vertex.shaderObject);
glDetachShader(programObject,fragment.shaderObject);
vertex.Release();
fragment.Release();
throw "Link Error";
}
}