版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2018.01.06 |
前言
OpenGL ES圖形庫項目中一直也沒用過揭厚,最近也想學著使用這個圖形庫,感覺還是很有意思扶供,也就自然想著好好的總結(jié)一下筛圆,希望對大家能有所幫助。下面就開始進行實踐椿浓,寫一些小程序太援。感興趣的可以看上面幾篇文章闽晦。
1. OpenGL ES實踐(一)—— 一個簡單的小程序
功能需求
畫一個簡單的三角形。
功能實現(xiàn)
下面我們就直接看代碼提岔。
1. JJOpenglesVC.h
#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>
@interface JJOpenglesVC : GLKViewController
@end
2. JJOpenglesVC.m
#import "JJOpenglesVC.h"
@interface JJOpenglesVC()
{
GLuint vertexBufferID;
}
@property (strong, nonatomic) GLKBaseEffect *baseEffect;
@end
@implementation JJOpenglesVC
@synthesize baseEffect;
/////////////////////////////////////////////////////////////////
// This data type is used to store information for each vertex
//這個結(jié)構體用于存放頂點數(shù)據(jù)
typedef struct {
GLKVector3 positionCoords;
}SceneVertex;
/////////////////////////////////////////////////////////////////
// Define vertex data for a triangle to use in example
//定義例子中三角形需要的頂點數(shù)據(jù)
static const SceneVertex vertices[] =
{
{{-0.5f, -0.5f, 0.0}}, // lower left corner
{{ 0.5f, -0.5f, 0.0}}, // lower right corner
{{-0.5f, 0.5f, 0.0}} // upper left corner
};
#pragma mark - Override Base Function
- (void)viewDidLoad
{
// Verify the type of view created automatically by the
// Interface Builder storyboard
GLKView *view = (GLKView *)self.view;
NSAssert([view isKindOfClass:[GLKView class]],
@"View controller's view is not a GLKView");
// Create an OpenGL ES 2.0 context and provide it to the
// view
view.context = [[EAGLContext alloc]
initWithAPI:kEAGLRenderingAPIOpenGLES3];
// Make the new context current
[EAGLContext setCurrentContext:view.context];
// Create a base effect that provides standard OpenGL ES 3.0
// Shading Language programs and set constants to be used for
// all subsequent rendering
self.baseEffect = [[GLKBaseEffect alloc] init];
self.baseEffect.useConstantColor = GL_TRUE;
self.baseEffect.constantColor = GLKVector4Make(
0.7f, // Red
0.6f, // Green
0.4f, // Blue
1.0f);// Alpha
// Set the background color stored in the current context
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // background color
// Generate, bind, and initialize contents of a buffer to be
// stored in GPU memory
glGenBuffers(1, // STEP 1
&vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, // STEP 2
vertexBufferID);
glBufferData( // STEP 3
GL_ARRAY_BUFFER, // Initialize buffer contents
sizeof(vertices), // Number of bytes to copy
vertices, // Address of bytes to copy
GL_STATIC_DRAW); // Hint: cache in GPU memory
}
/////////////////////////////////////////////////////////////////
// Called when the view controller's view has been unloaded
// Perform clean-up that is possible when you know the view
// controller's view won't be asked to draw again soon.
- (void)viewDidUnload
{
[super viewDidUnload];
// Make the view's context current
GLKView *view = (GLKView *)self.view;
[EAGLContext setCurrentContext:view.context];
// Delete buffers that aren't needed when view is unloaded
if (0 != vertexBufferID)
{
glDeleteBuffers (1, // STEP 7
&vertexBufferID);
vertexBufferID = 0;
}
// Stop using the context created in -viewDidLoad
((GLKView *)self.view).context = nil;
[EAGLContext setCurrentContext:nil];
}
#pragma mark - GLKViewDelegate
/////////////////////////////////////////////////////////////////
// GLKView delegate method: Called by the view controller's view
// whenever Cocoa Touch asks the view controller's view to
// draw itself. (In this case, render into a frame buffer that
// shares memory with a Core Animation Layer)
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
[self.baseEffect prepareToDraw];
// Clear Frame Buffer (erase previous drawing)
glClear(GL_COLOR_BUFFER_BIT);
// Enable use of positions from bound vertex buffer
glEnableVertexAttribArray( // STEP 4
GLKVertexAttribPosition);
glVertexAttribPointer( // STEP 5
GLKVertexAttribPosition,
3, // three components per vertex
GL_FLOAT, // data is floating point
GL_FALSE, // no fixed point scaling
sizeof(SceneVertex), // no gaps in data
NULL); // NULL tells GPU to start at
// beginning of bound buffer
// Draw triangles using the first three vertices in the
// currently bound vertex buffer
glDrawArrays(GL_TRIANGLES, // STEP 6
0, // Start with first vertex in currently bound buffer
3); // Use three vertices from currently bound buffer
}
@end
功能效果
下面我們看一下功能效果仙蛉。
后記
未完,待續(xù)~~~