forked from Milez213/CityTrial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLSL_helper.h
143 lines (107 loc) · 3.81 KB
/
GLSL_helper.h
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
/*
* GLSL_helper.h
* CSC473
*
* Many useful helper functions for GLSL shaders - gleaned from various sources including orange book
* Created by zwood on 2/21/10.
*
*/
#ifndef GLSL_HELPER_H
#define GLSL_HELPER_H
#include "include_gl.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <stdexcept>
#include "glm/glm.hpp"
bool installShader(const GLchar *vShaderStr, const GLchar *fShaderStr,
GLuint &ShadeProg);
int printOglError (const char *file, int line);
/* Print out the information log for a program object */
void printProgramInfoLog (GLuint program);
void printShaderInfoLog (GLuint shader);
#define printOpenGLError() printOglError(__FILE__, __LINE__)
//A helper routine to make it easier to set uniform variables in the shader
GLint getUniLoc(GLuint program, const GLchar *name);
void getGLversion();
int textFileWrite(char *fn, char *s);
char *textFileRead(char *fn);
inline GLint safe_glGetAttribLocation(const GLint program, const char varname[]) {
GLint r = glGetAttribLocation(program, varname);
if (r < 0)
std::cerr << "WARN: "<< varname << " cannot be bound (it either doesn't exist or has been optimized away). safe_glAttrib calls will silently ignore it.\n" << std::endl;
return r;
}
inline GLint safe_glGetUniformLocation(const GLint program, const char varname[]) {
GLint r = glGetUniformLocation(program, varname);
if (r < 0)
std::cerr << "WARN: "<< varname << " cannot be bound (it either doesn't exist or has been optimized away). safe_glUniform calls will silently ignore it.\n" << std::endl;
return r;
}
inline void safe_glEnableVertexAttribArray(const GLint handle) {
if (handle >= 0)
glEnableVertexAttribArray(handle);
}
inline void safe_glDisableVertexAttribArray(const GLint handle) {
if (handle >= 0)
glDisableVertexAttribArray(handle);
}
inline void safe_glVertexAttribPointer(const GLint handle, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer) {
if (handle >= 0)
glVertexAttribPointer(handle, size, type, normalized, stride, pointer);
}
inline void safe_glUniformMatrix4fv(const GLint handle, const GLfloat data[]) {
if (handle >= 0)
glUniformMatrix4fv(handle, 1, GL_FALSE, data);
}
inline void safe_glUniform1i(const GLint handle, const GLint a) {
if (handle >= 0)
glUniform1i(handle, a);
}
inline void safe_glUniform2i(const GLint handle, const GLint a, const GLint b) {
if (handle >= 0)
glUniform2i(handle, a, b);
}
inline void safe_glUniform3i(const GLint handle, const GLint a, const GLint b, const GLint c) {
if (handle >= 0)
glUniform3i(handle, a, b, c);
}
inline void safe_glUniform4i(const GLint handle, const GLint a, const GLint b, const GLint c, const GLint d) {
if (handle >= 0)
glUniform4i(handle, a, b, c, d);
}
inline void safe_glUniform1f(const GLint handle, const GLfloat a) {
if (handle >= 0)
glUniform1f(handle, a);
}
inline void safe_glUniform3f(const GLint handle, const GLfloat a, const GLfloat b, const GLfloat c) {
if (handle >= 0)
glUniform3f(handle, a, b, c);
}
inline void safe_glUniform3f(const GLint handle, glm::vec3 v) {
if (handle >= 0)
safe_glUniform3f(handle, v.x, v.y, v.z);
}
// Check if there has been an error inside OpenGL and if yes, print the error and
// through a runtime_error exception.
void checkGlErrors();
// Light wrapper around a GL buffer object handle that automatically allocates
// and deallocates. Can be casted to a GLuint.
class GlBufferObject{
GLuint handle;
public:
GlBufferObject() {
glGenBuffers(1, &handle);
checkGlErrors();
}
~GlBufferObject() {
glDeleteBuffers(1, &handle);
}
// Casts to GLuint so can be used directly glBindBuffer and so on
operator GLuint() const {
return handle;
}
};
void usePointSprites();
#endif /* GLSL_HELPER_H */