罗大柚OpenGL ES教程系列LessonTwo(Part 2):采用VAO绘制一个Cube
来源:互联网 发布:本子画师推荐 知乎 编辑:程序博客网 时间:2024/05/21 21:03
前面的创建过程和LessonTwo(part 1)一样, 结果也差不多,这个project和前面相比除了是使用VAO以外,还有就是启动了光照。废话不多说,直接上代码:
#import "ViewController.h"
#defineBUFFER_OFFSET(i) ((char *)NULL + (i))
//这里我们直接用系统中创建OpenGL Game程序时默认的顶点数据
GLfloat gCubeVertexData[216] =
{
// Data layout for each line below is:
// positionX, positionY, positionZ, normalX, normalY, normalZ,
//前面
0.5f, -0.5f, -0.5f, 1.0f,0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f,0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f,0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f,0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f,0.0f, 0.0f,
//后面
0.5f, 0.5f, -0.5f, 0.0f,1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f,1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f,1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f,1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f,1.0f, 0.0f,
//左面
-0.5f, 0.5f, -0.5f, -1.0f,0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, -1.0f,0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f,0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f,0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, -1.0f,0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, -1.0f,0.0f, 0.0f,
//右面
-0.5f, -0.5f, -0.5f, 0.0f,-1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f,-1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f,-1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f,-1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f,-1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f,-1.0f, 0.0f,
//下面
0.5f, 0.5f, 0.5f, 0.0f,0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f,0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f,0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f,0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f,0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f,0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f,0.0f, -1.0f,
-0.5f, -0.5f, -0.5f, 0.0f,0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 0.0f,0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 0.0f,0.0f, -1.0f,
-0.5f, -0.5f, -0.5f, 0.0f,0.0f, -1.0f,
-0.5f, 0.5f, -0.5f, 0.0f,0.0f, -1.0f
};
@interface ViewController ()
{
GLKMatrix4 _modelViewProjectionMatrix;
GLKMatrix3 _normalMatrix;
float_rotation;
GLuint _vertexArray;
GLuint _vertexBuffer;
}
@property (strong,nonatomic)EAGLContext *context;
@property (strong,nonatomic)GLKBaseEffect *effect;
- (void)setupGL;
- (void)tearDownGL;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.context = [[EAGLContextalloc]initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (!self.context) {
NSLog(@"Failed to create ES context");
}
GLKView *view = (GLKView *)self.view;
view.context = self.context;
//
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
[self setupGL];
}
- (void)setupGL
{
[EAGLContext setCurrentContext:self.context];
self.effect = [[GLKBaseEffectalloc]init];
self.effect.light0.enabled =GL_TRUE;
//散射光
self.effect.light0.diffuseColor =GLKVector4Make(1.0f, 0.4f, 0.4f, 1.0f);
//启动深度测试
glEnable(GL_DEPTH_TEST);
//设置VAO
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);
//绑定顶点buffer
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER,_vertexBuffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(gCubeVertexData),gCubeVertexData,GL_STATIC_DRAW);
//启动顶点
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3,GL_FLOAT,GL_FALSE, 24,BUFFER_OFFSET(0));
//启动顶点法线
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3,GL_FLOAT,GL_FALSE, 24,BUFFER_OFFSET(12));
//Bind back to the default state
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArrayOES(0);
}
//tear down 卸载;卸载GL
- (void)tearDownGL
{
[EAGLContext setCurrentContext:self.context];
glDeleteBuffers(1, &_vertexBuffer);
glDeleteVertexArraysOES(1, &_vertexArray);
self.effect =nil;
}
#pragma mark - GLKView and GLKViewController delegatemethods
- (void)update
{
float aspect= fabsf(self.view.bounds.size.width /self.view.bounds.size.height);
GLKMatrix4 projectionMatrix =GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f);
self.effect.transform.projectionMatrix = projectionMatrix;
GLKMatrix4 modelViewMatrix =GLKMatrix4MakeTranslation(0.0f, 0.0f, -4.5f);
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix,_rotation, 1.0f, 1.0f, 1.0f);
self.effect.transform.modelviewMatrix = modelViewMatrix;
_normalMatrix = GLKMatrix3InvertAndTranspose(GLKMatrix4GetMatrix3(modelViewMatrix),NULL);
_rotation += self.timeSinceLastUpdate * 0.5f;
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glBindVertexArrayOES(_vertexArray);
// Render the object with GLKit
[self.effectprepareToDraw];
//画立方体
glDrawArrays(GL_TRIANGLES, 0, 36);
}
- (void)dealloc
{
[self tearDownGL];
if ([EAGLContextcurrentContext] ==self.context) {
[EAGLContext setCurrentContext:nil];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
if ([selfisViewLoaded] && ([[selfview]window] ==nil)) {
self.view =nil;
[self tearDownGL];
if ([EAGLContextcurrentContext] ==self.context) {
[EAGLContext setCurrentContext:nil];
}
self.context =nil;
}
}
@end
源码下载:http://download.csdn.net/detail/luozhonglan/6987129
- 罗大柚OpenGL ES教程系列LessonTwo(Part 2):采用VAO绘制一个Cube
- 罗大柚OpenGL ES教程系列LessonTwo(Part 3):采用VAO和index,绘制一个Cube
- 罗大柚OpenGL ES教程系列LessonOne(Part 2):绘制一个五角星
- 罗大柚OpenGL ES教程系列LessonOne(Part 1): 绘制一个三角形
- 罗大柚OpenGL ES教程系列_LessonTwo(Part 4)_使用shader绘制一个立方体
- 罗大柚OpenGL ES教程系列_LessonTwo(Part 1)_使用VBO画一个立方体
- 罗大柚OpenGL ES教程系列_LessonThree(Part 2)_本地坐标系中旋转
- 罗大柚OpenGL ES教程系列_LessonThree(Part 1)_使用Touch来实现旋转
- 罗大柚OpenGL ES教程系列LessonFour(Part X):rendering multiple objects with different texture
- Android OpenGL ES 开发教程(22):绘制一个球体
- Android OpenGL ES 开发教程(22):绘制一个球体
- OpenGL ES VBO 和 VAO
- OpenGL ES学习系列教程
- iOS开发OpenGL ES教程(2)绘制矩形
- Opengl ES 学习笔记 2:绘制一个旋转的三角形
- Android OpenGL ES(十一):绘制一个20面体
- 用OpenGL ES绘制一个红色的三角形(一)
- 用OpenGL ES绘制一个红色三角形(二)
- 2n皇后问题
- STL第二章-vector的使用方法
- 我不怕千万人阻挡,只怕自己投降是谁说的
- HDU2224 TSP(货郎担)问题DP
- d3学习之(Data Visualization with d3.js Cookbook )二(第二章)
- 罗大柚OpenGL ES教程系列LessonTwo(Part 2):采用VAO绘制一个Cube
- libgdx——游戏音效
- Java枚举的七种常见用法
- Linux Documentation学习笔记 -- 写在最前面
- 01串
- 【DP入门】The Triangle
- Tez调研报告
- auto_ptr指针和explicit的使用
- Redis Redis(2.6版本以上) Hash 常用操作和方法详解