-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshaders.cpp
152 lines (139 loc) · 3.63 KB
/
shaders.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
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <string>
#include <GL/glew.h>
#include <GL/glut.h>
using namespace std ;
// This is a basic program to initiate a shader
// The textFileRead function reads in a filename into a string
// programerrors and shadererrors output compilation errors
// initshaders initiates a vertex or fragment shader
// initprogram initiates a program with vertex and fragment shaders
string textFileRead (const char * filename) {
string str, ret = "" ;
ifstream in ;
in.open(filename) ;
if (in.is_open()) {
getline (in, str) ;
while (in) {
ret += str + "\n" ;
getline (in, str) ;
}
// cout << "Shader below\n" << ret << "\n" ;
return ret ;
}
else {
cerr << "Unable to Open File " << filename << "\n" ;
throw 2 ;
}
}
/*
bool parseLine(string line) {
string cmd;
if (line.empty())
return true;
stringstream ss(stringstream::in | stringstream::out) ;
ss.str(line);
ss >> cmd;
if (cmd[0] == '#') { //access strings as arrays
return true;
} else if { //stuff goes here for cases?
}
if (ss.fail())
return false;
return true;
}
void parseScene(string filename) {
char line[1024] ; //Temporary storage
ifstream inFile(filename.c_str(), ifstream::in); //Open as stream
if (!inFile) {
std::cout << "Could not open given file " << filename;
exit(1);
}
while (inFile.good()) {
inFile.getline(line, 1023); //read line into temporary storage
if (!parseLine(string(line))) {
exit(1);
}
}
inFile.close();
}
*/
/*
string fileparser (const char * filename) {
string str, ret = "" ;
ifstream in ;
in.open(filename) ;
if (in.is_open()) {
getline (in, str) ;
while (in) {
if ((str.find_first_not_of(" \t\r\n") != string::npos) && (str[0] != '#')) {
stringstream s(str) ;
s >> cmd ;
}
ret += str + "\n" ;
getline (in, str) ;
}
// cout << "Shader below\n" << ret << "\n" ;
return ret ;
}
else {
cerr << "Unable to Open File " << filename << "\n" ;
throw 2 ;
}
}
*/
void programerrors (const GLint program) {
GLint length ;
GLchar * log ;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length) ;
log = new GLchar[length+1] ;
glGetProgramInfoLog(program, length, &length, log) ;
cout << "Compile Error, Log Below\n" << log << "\n" ;
delete [] log ;
}
void shadererrors (const GLint shader) {
GLint length ;
GLchar * log ;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length) ;
log = new GLchar[length+1] ;
glGetShaderInfoLog(shader, length, &length, log) ;
cout << "Compile Error, Log Below\n" << log << "\n" ;
delete [] log ;
}
GLuint initshaders (GLenum type, const char *filename)
{
// Using GLSL shaders, OpenGL book, page 679
GLuint shader = glCreateShader(type) ;
GLint compiled ;
string str = textFileRead (filename) ;
GLchar * cstr = new GLchar[str.size()+1] ;
const GLchar * cstr2 = cstr ; // Weirdness to get a const char
strcpy(cstr,str.c_str()) ;
glShaderSource (shader, 1, &cstr2, NULL) ;
glCompileShader (shader) ;
glGetShaderiv (shader, GL_COMPILE_STATUS, &compiled) ;
if (!compiled) {
shadererrors (shader) ;
throw 3 ;
}
return shader ;
}
GLuint initprogram (GLuint vertexshader, GLuint fragmentshader)
{
GLuint program = glCreateProgram() ;
GLint linked ;
glAttachShader(program, vertexshader) ;
glAttachShader(program, fragmentshader) ;
glLinkProgram(program) ;
glGetProgramiv(program, GL_LINK_STATUS, &linked) ;
if (linked) glUseProgram(program) ;
else {
programerrors(program) ;
throw 4 ;
}
return program ;
}