-
Notifications
You must be signed in to change notification settings - Fork 0
/
glsl_utils.h
110 lines (84 loc) · 2.38 KB
/
glsl_utils.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
#ifndef _GLSDLUTIL_H
#define _GLSDLUTIL_H
#include <SDL2/SDL_opengles2.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cstdio>
void printGLVersion()
{
printf("Opengl Vender: %s\n",
(const char*) glGetString(GL_VENDOR));
printf("Opengl Renderer: %s\n",
(const char*) glGetString(GL_RENDERER));
printf("Opengl Version: %s\n",
(const char *)glGetString(GL_VERSION));
printf("GLSL Version: %s\n",
(const char *)glGetString( GL_SHADING_LANGUAGE_VERSION));
}
static GLuint loadProgram(GLuint vertex, GLuint fragment, std::vector<std::string> attributes)
{
// Create the program object
GLuint programObject = glCreateProgram ( );
if ( programObject == 0 )
return 0;
glAttachShader ( programObject, vertex);
glAttachShader ( programObject, fragment);
int i = 0;
std::for_each(std::begin(attributes), std::end(attributes),
[&](std::string attribute)
{
glBindAttribLocation(programObject, i++, attribute.c_str());
});
// Link the program
glLinkProgram ( programObject );
GLint linked;
// Check the link status
glGetProgramiv ( programObject, GL_LINK_STATUS, &linked );
if ( !linked )
{
GLint infoLen = 0;
glGetProgramiv ( programObject, GL_INFO_LOG_LENGTH, &infoLen );
if ( infoLen > 1 )
{
char* infoLog = (char *)malloc (sizeof(char) * infoLen );
glGetProgramInfoLog ( programObject, infoLen, NULL, infoLog );
std::cerr << "Error linking program:" << std::endl << infoLog ;
free ( infoLog );
}
glDeleteProgram ( programObject );
return GL_FALSE;
}
return programObject;
}
static GLuint loadShader ( GLenum type, const char *shaderSrc )
{
GLuint shader;
GLint compiled;
// Create the shader object
shader = glCreateShader ( type );
if ( shader == GL_FALSE )
return 0;
// Load the shader source
glShaderSource ( shader, 1, &shaderSrc, NULL );
// Compile the shader
glCompileShader ( shader );
// Check the compile status
glGetShaderiv ( shader, GL_COMPILE_STATUS, &compiled );
if ( !compiled )
{
GLint infoLen = 0;
glGetShaderiv ( shader, GL_INFO_LOG_LENGTH, &infoLen );
if ( infoLen > 1 )
{
char* infoLog = (char *)malloc (sizeof(char) * infoLen );
glGetShaderInfoLog ( shader, infoLen, NULL, infoLog );
std::cerr << "Error compiling shader: " << infoLog << std::endl;
free ( infoLog );
}
glDeleteShader ( shader );
return 0;
}
return shader;
}
#endif