-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGLProgram.m
96 lines (76 loc) · 2.81 KB
/
GLProgram.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
// Created by fernlightning on 27/11/2010.
#include </Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/OpenGL.framework/Headers/gl3.h>
#include </Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/OpenGL.framework/Headers/glu.h>
#import "GLProgram.h"
@implementation GLProgram
-(GLuint)loadShader:(GLenum)type code:(const char *)code {
NSString *desc = [NSString stringWithFormat:@"%@ shader %@", ((type == GL_VERTEX_SHADER)?@"Vertex":@"Fragment"), _name];
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, (const GLchar **)&code, NULL);
glCompileShader(shader);
GLint logLength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
if(logLength > 0) {
GLchar *log = malloc(logLength);
glGetShaderInfoLog(shader, logLength, &logLength, log);
NSLog(@"%@ compile log:\n%s", desc, log);
free(log);
}
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if(status == 0)
NSLog(@"Failed to compile desc: %@ : %s", desc, code);
return shader;
}
- (id)initWithName:(NSString*)name VS:(const char*)vs FS:(const char*)fs {
if((self = [super init])) {
_name = [name retain];
NSString *desc = [NSString stringWithFormat:@"Program %@", _name];
GLuint cvs = [self loadShader:GL_VERTEX_SHADER code:vs];
GLuint cfs = [self loadShader:GL_FRAGMENT_SHADER code:fs];
_id = glCreateProgram();
glAttachShader(_id, cvs);
glAttachShader(_id, cfs);
glLinkProgram(_id);
GLint logLength;
glGetProgramiv(_id, GL_INFO_LOG_LENGTH, &logLength);
if(logLength > 0) {
GLchar *log = malloc(logLength);
glGetProgramInfoLog(_id, logLength, &logLength, log);
NSLog(@"%@ link log:\n%s", desc, log);
free(log);
}
GLint status;
glGetProgramiv(_id, GL_LINK_STATUS, &status);
if(status == 0) {
NSLog(@"Failed to link %@", desc);
}
glDeleteShader(cvs);
glDeleteShader(cfs);
}
return self;
}
- (void)dealloc {
[_name release];
glDeleteProgram(_id);
[super dealloc];
}
- (NSString*)name { return _name; }
- (void)bind {
glUseProgram(_id);
}
- (void)unbind {
glUseProgram(0);
}
- (GLint)uniformLocation:(NSString*)name {
GLint location = glGetUniformLocation(_id, [name UTF8String]);
if(location < 0) NSLog(@"No such uniform named %@ in %@\n", name, _name);
return location;
}
- (void)setUniformInt:(int)i forName:(NSString*)name {
glUniform1i([self uniformLocation:name], i);
}
- (void)setUniformFloat:(float)f forName:(NSString*)name {
glUniform1f([self uniformLocation:name], f);
}
@end