-
Notifications
You must be signed in to change notification settings - Fork 6
/
GLESProgram.m
executable file
·174 lines (131 loc) · 4.68 KB
/
GLESProgram.m
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// GLESProgram.m
// AprilTag
//
// Created by Edwin Olson on 10/18/13.
// Copyright (c) 2013 Edwin Olson. All rights reserved.
//
#import "GLESProgram.h"
@implementation GLESProgram
- (const GLchar **)readFile:(NSString *)name
{
NSString *path;
const GLchar **source = calloc(2, sizeof(GLchar*));
path = [[NSBundle mainBundle] pathForResource:name ofType: nil];
source[0] = (GLchar *)[[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil] UTF8String];
if (source[0] == nil)
printf("unable to load %s\n", [name UTF8String]);
return source;
}
- (GLchar**) copyArray:(const GLchar**)array
{
return NULL;
}
- (id) initWithVertexShaderPath:(NSString*)vertPath
andFragmentShaderPath:(NSString*)fragPath
andAttributeNames:(NSArray*)_attributeNames
andUniformNames:(NSArray*)_uniformNames
{
char log[1024];
int logLength;
self = [super init];
attributeNames = _attributeNames;
uniformNames = _uniformNames;
const GLchar** vertSrc = [self readFile:vertPath];
const GLchar** fragSrc = [self readFile:fragPath];
GLint status = 1;
_program = glCreateProgram();
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertShader, 1, vertSrc, NULL);
glCompileShader(vertShader);
glGetShaderiv(vertShader, GL_COMPILE_STATUS, &status);
if (!status) {
printf("Failed to compile vertex shader %s\n", [vertPath UTF8String]);
glGetShaderInfoLog(vertShader, sizeof(log), &logLength, log);
printf("Log: %s\n", log);
exit(-1);
}
free(vertSrc);
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragShader, 1, fragSrc, NULL);
glCompileShader(fragShader);
glGetShaderiv(fragShader, GL_COMPILE_STATUS, &status);
if (!status) {
printf("Failed to compile fragment shader %s\n", [fragPath UTF8String]);
glGetShaderInfoLog(fragShader, sizeof(log), &logLength, log);
printf("Log: %s\n", log);
exit(-1);
}
free(fragSrc);
glAttachShader(_program, vertShader);
glAttachShader(_program, fragShader);
glLinkProgram(_program);
glGetProgramiv(_program, GL_LINK_STATUS, &status);
if (status == 0) {
printf("Failed to link program %d\n", _program);
glGetProgramInfoLog(_program, sizeof(log), &logLength, log);
printf("Log: %s\n", log);
exit(-1);
}
NSUInteger nattribs = [attributeNames count];
attributeLocations = calloc(nattribs, sizeof(GLuint));
for (int attribidx = 0; attribidx < nattribs; attribidx++)
attributeLocations[attribidx] = glGetAttribLocation(_program, [[attributeNames objectAtIndex:attribidx] UTF8String]);
NSUInteger nuniforms = [uniformNames count];
uniformLocations = calloc(nuniforms, sizeof(GLuint));
for (int uniformidx = 0; uniformidx < nuniforms; uniformidx++)
uniformLocations[uniformidx] = glGetUniformLocation(_program, [[uniformNames objectAtIndex:uniformidx] UTF8String]);
glDeleteShader(vertShader);
glDeleteShader(fragShader);
return self;
}
- (GLuint) getAttributeIndex:(NSString*)name
{
NSUInteger nattribs = [attributeNames count];
for (int idx = 0; idx < nattribs; idx++)
if ([name isEqualToString:[attributeNames objectAtIndex:idx]])
return idx;
assert(0);
return -1;
}
- (GLuint) getUniformIndex:(NSString*)name
{
NSUInteger nuniforms = [uniformNames count];
for (int idx = 0; idx < nuniforms; idx++)
if ([name isEqualToString:[uniformNames objectAtIndex:idx]])
return idx;
assert(0);
return -1;
}
- (void) enableVertexAttribute:(NSString*)name withFloats:(float*)v withNumComponents:(int)ncomponents
{
int idx = [self getAttributeIndex:name];
glVertexAttribPointer(attributeLocations[idx], ncomponents, GL_FLOAT, 0, 0, v);
glEnableVertexAttribArray(idx);
}
- (void) enableVertexAttribute:(NSString*)name withInt32s:(uint32_t*)v
{
assert(0);
}
- (void) uniformMatrix4f:(NSString*)name withFloats:(float*)v
{
int idx = [self getUniformIndex:name];
glUniformMatrix4fv(uniformLocations[idx], 1, 0, v);
}
- (void) uniform3f:(NSString*)name withFloats:(float*)v
{
int idx = [self getUniformIndex:name];
glUniform3fv(uniformLocations[idx], 1, v);
}
- (void) uniform4f:(NSString*)name withFloats:(float*)v
{
int idx = [self getUniformIndex:name];
glUniform4fv(uniformLocations[idx], 1, v);
}
- (void) disableVertexAttributes
{
NSUInteger nattribs = [attributeNames count];
for (int i = 0; i < nattribs; i++)
glDisableVertexAttribArray(attributeLocations[i]);
}
@end