Created
September 25, 2010 18:49
-
-
Save DylanLukes/597157 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
// | |
// CNWorldView+Drawing.m | |
// Concorde | |
// | |
// Created by Dylan Lukes on 9/22/10. | |
// Copyright 2010 Dylan Lukes. All rights reserved. | |
// | |
#import <OpenGL/gl.h> | |
#import <OpenGL/glu.h> | |
#import "CNWorldView+Drawing.h" | |
#import "geometry.h" | |
#import "error.h" | |
#import "texture.h" | |
#import "shader.h" | |
static const GLfloat vbdata[] = { | |
-1.0f, -1.0f, | |
1.0f, -1.0f, | |
-1.0f, 1.0f, | |
1.0f, 1.0f | |
}; | |
static const GLushort ebdata[] = {0, 1, 2, 3}; | |
@implementation CNWorldView (Drawing) | |
- (GLvoid)setupOpenGL{ | |
[[self openGLContext] makeCurrentContext]; | |
/* Create buffers */ { | |
glGenBuffers(2, buffers); | |
// Vertex buffer | |
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]); | |
eglGetError(); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(vbdata), vbdata, GL_STATIC_DRAW); | |
eglGetError(); | |
// Element buffer | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]); | |
eglGetError(); | |
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(ebdata), ebdata, GL_STATIC_DRAW); | |
eglGetError(); | |
} | |
/* Load textures */ { | |
glGenTextures(2, textures); | |
eglGetError(); | |
GLsizei width, height; | |
GLvoid* pixels; | |
// Texture #1 | |
pixels = load_image(@"hello1.png", &width, &height); | |
glBindTexture(GL_TEXTURE_2D, textures[0]); | |
eglGetError(); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
eglGetError(); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
eglGetError(); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
eglGetError(); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
eglGetError(); | |
glTexImage2D(GL_TEXTURE_2D, 0, | |
GL_RGBA8, | |
width, height, 0, | |
GL_RGBA, GL_UNSIGNED_BYTE, | |
pixels); | |
free(pixels); | |
eglGetError(); | |
// Texture #2 | |
pixels = load_image(@"hello2.png", &width, &height); | |
glBindTexture(GL_TEXTURE_2D, textures[1]); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
eglGetError(); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
eglGetError(); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
eglGetError(); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
eglGetError(); | |
glTexImage2D(GL_TEXTURE_2D, 0, | |
GL_RGBA8, | |
width, height, 0, | |
GL_RGBA, GL_UNSIGNED_BYTE, | |
pixels); | |
eglGetError(); | |
free(pixels); | |
//eglGetError(); | |
} | |
/* Load shaders */ { | |
programs[0] = glCreateProgram(); | |
eglGetError(); | |
shaders[0] = glCreateShader(GL_VERTEX_SHADER); | |
eglGetError(); | |
shaders[1] = glCreateShader(GL_FRAGMENT_SHADER); | |
eglGetError(); | |
const GLchar* vsh; GLint vlen; | |
const GLchar* fsh; GLint flen; | |
load_shader(@"hello-gl.vert", &vsh, &vlen); | |
load_shader(@"hello-gl.vert", &fsh, &flen); | |
eglGetError(); | |
// Vertex | |
glShaderSource(shaders[0], 1, &vsh, &vlen); | |
eglGetError(); | |
glCompileShader(shaders[0]); | |
eglGetError(); | |
glAttachShader(programs[0], shaders[0]); | |
eglGetError(); | |
// Fragment | |
glShaderSource(shaders[1], 1, &fsh, &flen); | |
eglGetError(); | |
glCompileShader(shaders[1]); | |
eglGetError(); | |
glAttachShader(programs[0], shaders[1]); | |
eglGetError(); | |
glLinkProgram(programs[0]); | |
eglGetError(); | |
glUseProgram(programs[0]); | |
eglGetError(); | |
} | |
} | |
- (CVReturn)drawWithGL{ | |
[[self openGLContext] makeCurrentContext]; | |
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); | |
eglGetError(); | |
glClear(GL_COLOR_BUFFER_BIT); | |
eglGetError(); | |
glUseProgram(programs[0]); | |
eglGetError(); | |
glUniform1f(glGetUniformLocation(programs[0], "fade_factor"), fade_factor); | |
eglGetError(); | |
glActiveTexture(GL_TEXTURE0); | |
eglGetError(); | |
glBindTexture(GL_TEXTURE_2D, textures[0]); | |
eglGetError(); | |
glUniform1i(glGetUniformLocation(programs[0], "textures[0]"), 0); | |
eglGetError(); | |
glActiveTexture(GL_TEXTURE1); | |
eglGetError(); | |
glBindTexture(GL_TEXTURE_2D, textures[1]); | |
eglGetError(); | |
glUniform1i(glGetUniformLocation(programs[0], "textures[1]"), 1); | |
eglGetError(); | |
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]); | |
eglGetError(); | |
glVertexAttribPointer(glGetAttribLocation(programs[0], "position"), | |
2, | |
GL_FLOAT, | |
GL_FALSE, | |
sizeof(GLfloat)*2, | |
(void*)0 | |
); | |
eglGetError(); | |
glEnableVertexAttribArray(glGetAttribLocation(programs[0], "position")); | |
eglGetError(); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]); | |
eglGetError(); | |
glDrawElements(GL_TRIANGLE_STRIP, | |
4, | |
GL_UNSIGNED_SHORT, | |
(void*)0 | |
); | |
eglGetError(); | |
glDisableVertexAttribArray(glGetAttribLocation(programs[0], "position")); | |
eglGetError(); | |
glSwapAPPLE(); | |
eglGetError(); | |
return kCVReturnSuccess; | |
} | |
- (GLvoid)cleanupOpenGL{ | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment