-
-
Save PoisonousJohn/4328217 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
varying lowp vec4 DestinationColor; // 1 | |
void main(void) { // 2 | |
gl_FragColor = DestinationColor; // 3 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by JohnPoison <[email protected]> on 10/24/12. | |
#import <QuartzCore/QuartzCore.h> | |
#import "OpenGLView.h" | |
#import "CC3GLMatrix.h" | |
@implementation OpenGLView { | |
} | |
//typedef struct { | |
// float Position[3]; | |
// float Color[4]; | |
//} Vertex; | |
//const Vertex Vertices[] = { | |
// {{1, -1, -7}, {1, 0, 0, 1}}, | |
// {{1, 1, -7}, {0, 1, 0, 1}}, | |
// {{-1, 1, -7}, {0, 0, 1, 1}}, | |
// {{-1, -1, -7}, {0, 0, 0, 1}} | |
//}; | |
const GLubyte Indices[] = { | |
0, 1, 2, | |
2, 3, 0 | |
}; | |
+ (Class)layerClass { | |
return [CAEAGLLayer class]; | |
} | |
- (void)setupLayer { | |
_eaglLayer = (CAEAGLLayer*) self.layer; | |
_eaglLayer.opaque = YES; | |
} | |
- (void)setupContext { | |
EAGLRenderingAPI api = kEAGLRenderingAPIOpenGLES2; | |
_context = [[EAGLContext alloc] initWithAPI:api]; | |
if (!_context) { | |
NSLog(@"Failed to initialize OpenGLES 2.0 context"); | |
exit(1); | |
} | |
if (![EAGLContext setCurrentContext:_context]) { | |
NSLog(@"Failed to set current OpenGL context"); | |
exit(1); | |
} | |
} | |
- (void)setupRenderBuffer { | |
glGenRenderbuffers(1, &_colorRenderBuffer); | |
glBindRenderbuffer(GL_RENDERBUFFER, _colorRenderBuffer); | |
[_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:_eaglLayer]; | |
} | |
- (void)setupFrameBuffer { | |
GLuint framebuffer; | |
glGenFramebuffers(1, &framebuffer); | |
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); | |
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | |
GL_RENDERBUFFER, _colorRenderBuffer); | |
} | |
- (void) render { | |
glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0); | |
glClear(GL_COLOR_BUFFER_BIT); | |
CC3GLMatrix *projection = [CC3GLMatrix matrix]; | |
float h = 4.0f * self.frame.size.height / self.frame.size.width; | |
[projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:10]; | |
glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix); | |
// 1 | |
glViewport(0, 0, self.frame.size.width, self.frame.size.height); | |
// 2 | |
// glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, | |
// sizeof(Vertex), 0); | |
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, | |
sizeof(float), 0); | |
// glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, | |
// sizeof(Vertex), (GLvoid*) (sizeof(float) * 3)); | |
float color[4] = {1.0,1.0,1.0, 1.0}; | |
// float color[4] = {0,0,0,1}; | |
glUniform4fv(_colorSlot, 1, color); | |
// glUniform4f(_colorSlot, 1, 1, 1, 1); | |
// 3 | |
// glDrawElements(GL_LINE_STRIP, sizeof(Indices)/sizeof(Indices[0]),GL_UNSIGNED_BYTE, 0); | |
glDrawArrays(GL_LINE_STRIP, 0, sizeof(verticesCount)); | |
[_context presentRenderbuffer:GL_RENDERBUFFER]; | |
} | |
- (GLuint)compileShader:(NSString*)shaderName withType:(GLenum)shaderType { | |
NSLog(@"Compiling shader %@", shaderName); | |
// 1 | |
NSString* shaderPath = [[NSBundle mainBundle] pathForResource:shaderName | |
ofType:@"glsl"]; | |
NSError* error; | |
NSString* shaderString = [NSString stringWithContentsOfFile:shaderPath | |
encoding:NSUTF8StringEncoding error:&error]; | |
if (!shaderString) { | |
NSLog(@"Error loading shader: %@", error.localizedDescription); | |
exit(1); | |
} | |
// 2 | |
GLuint shaderHandle = glCreateShader(shaderType); | |
// 3 | |
const char * shaderStringUTF8 = [shaderString UTF8String]; | |
int shaderStringLength = [shaderString length]; | |
glShaderSource(shaderHandle, 1, &shaderStringUTF8, &shaderStringLength); | |
// 4 | |
glCompileShader(shaderHandle); | |
// 5 | |
GLint compileSuccess; | |
glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &compileSuccess); | |
if (compileSuccess == GL_FALSE) { | |
GLchar messages[256]; | |
glGetShaderInfoLog(shaderHandle, sizeof(messages), 0, &messages[0]); | |
NSString *messageString = [NSString stringWithUTF8String:messages]; | |
NSLog(@"%@", messageString); | |
exit(1); | |
} | |
return shaderHandle; | |
} | |
- (void)dealloc { | |
free (cylinderVertices); | |
free (drawIndexes); | |
} | |
- (void)compileShaders { | |
NSLog(@"Compiling shaders"); | |
// 1 | |
GLuint vertexShader = [self compileShader:@"SimpleVertex" | |
withType:GL_VERTEX_SHADER]; | |
GLuint fragmentShader = [self compileShader:@"SimpleFragment" | |
withType:GL_FRAGMENT_SHADER]; | |
// 2 | |
GLuint programHandle = glCreateProgram(); | |
glAttachShader(programHandle, vertexShader); | |
glAttachShader(programHandle, fragmentShader); | |
glLinkProgram(programHandle); | |
// 3 | |
GLint linkSuccess; | |
glGetProgramiv(programHandle, GL_LINK_STATUS, &linkSuccess); | |
if (linkSuccess == GL_FALSE) { | |
GLchar messages[256]; | |
glGetProgramInfoLog(programHandle, sizeof(messages), 0, &messages[0]); | |
NSString *messageString = [NSString stringWithUTF8String:messages]; | |
NSLog(@"shaders compile error: %@", messageString); | |
exit(1); | |
} | |
// 4 | |
glUseProgram(programHandle); | |
// 5 | |
_positionSlot = glGetAttribLocation(programHandle, "Position"); | |
_colorSlot = glGetUniformLocation(programHandle, "SourceColor"); | |
glEnableVertexAttribArray(_positionSlot); | |
// glEnableVertexAttribArray(_colorSlot); | |
_projectionUniform = glGetUniformLocation(programHandle, "Projection"); | |
} | |
- (id)initWithFrame:(CGRect)frame { | |
self = [super initWithFrame:frame]; | |
if (self) { | |
[self setupLayer]; | |
[self setupContext]; | |
[self setupRenderBuffer]; | |
[self setupFrameBuffer]; | |
[self compileShaders]; | |
[self setupVBOs]; | |
[self render]; | |
} | |
return self; | |
} | |
- (void)setupVBOs { | |
float radius = 1; | |
float segments = 32; | |
float height = 10; | |
verticesCount = (int)segments*2*3; | |
// double Vertices[segments*2*3] = {}; | |
cylinderVertices = malloc(sizeof(float) * verticesCount); | |
for (int i = 0; i < segments ; i++) { | |
float alpha = i / segments; | |
// x | |
cylinderVertices[3*i] = radius * cosf(alpha * 2.0 * M_PI); | |
// y | |
cylinderVertices[3*i+1] = radius * sinf(alpha * 2.0 * M_PI); | |
// z | |
cylinderVertices[3*i+2] = i % 2 ? height : - height; | |
} | |
GLuint vertexBuffer; | |
glGenBuffers(1, &vertexBuffer); | |
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(cylinderVertices), cylinderVertices, GL_STATIC_DRAW); | |
// GLuint indexBuffer; | |
// glGenBuffers(1, &indexBuffer); | |
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); | |
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW); | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
attribute vec4 Position; // 1 | |
attribute vec4 SourceColor; // 2 | |
varying vec4 DestinationColor; // 3 | |
uniform mat4 Projection; | |
void main(void) { // 4 | |
DestinationColor = SourceColor; // 5 | |
gl_Position = Position; // 6 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment