-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShader.cpp
executable file
·159 lines (125 loc) · 3.87 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
Copyright (C) 2006 So Yamaoka
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Modified: Rex West (2015)
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include "Shader.h"
GLhandleARB Shader::currentlyBoundShaderID = 0x0;
Shader::Shader(const char *vert, const char *frag, bool isFile)
{
if(isFile)
{
//Read in the vertex and fragment shaders
//We must delete these after we are finished compiling the shaders
char* vv = read(vert);
char* vf = read(frag);
//Setup the shader
setup(vv, vf);
//Delete the file data arrays we allocted
delete[] vv;
delete[] vf;
}
else
{
//Treat the vert and frag parameters as shader code and directly compile them
setup(vert, frag);
}
}
Shader::~Shader()
{
glDeleteObjectARB(pid);
}
void Shader::bind()
{
if(currentlyBoundShaderID != pid)
{
currentlyBoundShaderID = pid;
glUseProgramObjectARB(pid);
}
}
void Shader::unbind()
{
if(currentlyBoundShaderID != (void*)(0x0))
{
currentlyBoundShaderID = (void*)(0x0);
glUseProgramObjectARB(0);
}
}
void Shader::printLog(const char* tag)
{
char glslLog[1024];
GLsizei glslLogSize;
//Extract the error log for this shader's pid
glGetInfoLogARB(pid, 1024, &glslLogSize, glslLog);
//If the log isn't empty, print the contents
if(glslLogSize > 0)
std::cerr << tag << "(" << pid << ") - Shader error log:" << std::endl << glslLog << std::endl;
else
std::cerr << tag << "(" << pid << ") - Shaders compiled successfully!" << std::endl;
}
char* Shader::read(const char *filename)
{
char* shaderFile = 0;
//Open the file
FILE* fp = fopen(filename, "rb");
if(!fp)
{
std::cerr << "File doesn't exist [" << filename << "]" << std::endl;
std::exit(-1);
}
//Obtain the file size
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
rewind(fp);
//Alloc memory - will be deleted while setting the shader up
shaderFile = new char[size+1];
//Copy the file to the shaderFile
fread(shaderFile, sizeof(char), size, fp);
shaderFile[size]='\0'; //Eliminate the garbage at EOF
fclose(fp);
return shaderFile;
}
void Shader::setup(const char *vs, const char *fs)
{
//Create two new Shader Object IDs
GLhandleARB vid = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
GLhandleARB fid = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
//Pass the shader source code to OpenGL
glShaderSourceARB(vid, 1, &vs, 0);
glShaderSourceARB(fid, 1, &fs, 0);
//Compile the shader files
glCompileShaderARB(vid);
glCompileShaderARB(fid);
char glslLog[1024];
GLsizei glslLogSize;
//Get the error log for the Vertex shader
glGetInfoLogARB(vid, 1024, &glslLogSize, glslLog);
if (glslLogSize)
std::cerr << "Vertex program log: " << glslLog << std::endl;
//Get the error log for the Fragment shader
glGetInfoLogARB(fid, 1024, &glslLogSize, glslLog);
if (glslLogSize)
std::cerr << "Fragment program log: " << glslLog << std::endl;
//Create a new Shader Program
pid = glCreateProgramObjectARB();
//Attach the Vertex and Fragment shaders to the Shader Program
glAttachObjectARB(pid, vid);
glAttachObjectARB(pid, fid);
//Delete shader objects since they have been attached to a program
glDeleteObjectARB(vid);
glDeleteObjectARB(fid);
//Link it!
glLinkProgramARB(pid);
std::cerr<<"vid: "<<vid<<std::endl;
std::cerr<<"fid: "<<fid<<std::endl;
}