所谓顶点缓冲就是直接将顶点数据存储在gpu的一段缓冲区,不需要从cpu拷贝到gpu。提高了程序的运行效率。

  操作步骤

  1.创建顶点缓冲对象

  

GLuint vertexBufferID;

  2.分配空间

  

 glGenBuffers(,  &vertexBufferID);

  3.绑定当前顶点缓冲对象

  

   glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);

  4.初始化缓冲区数据

  

   glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,  GL_STATIC_DRAW); 

  5.启用顶点属性数组

  

glEnableVertexAttribArray(GLKVertexAttribPosition);

  6.使用顶点数据进行渲染

   glVertexAttribPointer(
GLKVertexAttribPosition,
,
GL_FLOAT,
GL_FALSE,
sizeof(SceneVertex),
NULL);

  7.绘制

  

   glDrawArrays(GL_TRIANGLES, ,); 

下面赋全部代码

@interface OpenGLESViewController : GLKViewController
{
GLuint vertexBufferID;
} @property (strong, nonatomic) GLKBaseEffect *baseEffect; @end
#import "OpenGLESViewController.h"

@implementation OpenGLESViewController

@synthesize baseEffect;

/////////////////////////////////////////////////////////////////
// This data type is used to store information for each vertex
typedef struct {
GLKVector3 positionCoords;
}
SceneVertex; // Define vertex data for a triangle to use in example
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
}; /////////////////////////////////////////////////////////////////
// Called when the view controller's view is loaded
// Perform initialization before the view is asked to draw
- (void)viewDidLoad
{
[super 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:kEAGLRenderingAPIOpenGLES2]; // Make the new context current
[EAGLContext setCurrentContext:view.context]; // Create a base effect that provides standard OpenGL ES 2.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(
1.0f, // Red
1.0f, // Green
1.0f, // Blue
1.0f);// Alpha // Set the background color stored in the current context
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // background color // Generate, bind, and initialize contents of a buffer to be
// stored in GPU memory
glGenBuffers(, // 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
} /////////////////////////////////////////////////////////////////
// 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,
, // 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
, // Start with first vertex in currently bound buffer
); // Use three vertices from currently bound buffer
} /////////////////////////////////////////////////////////////////
// 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 ( != vertexBufferID)
{
glDeleteBuffers (, // STEP 7
&vertexBufferID);
vertexBufferID = ;
} // Stop using the context created in -viewDidLoad
((GLKView *)self.view).context = nil;
[EAGLContext setCurrentContext:nil];
} @end

最新文章

  1. C语言中函数的传入值与传出值
  2. Linux搭建apache +PHP(CentOS系统)
  3. photoshop工具使用的简单介绍
  4. ceph--磁盘和rbd、rados性能测试工具和方法
  5. C# 四舍五入
  6. 公交wifi运营平台分析
  7. linux下杀死进程(kill)的N种方法 【转】
  8. iOSUI基础——懒加载
  9. 分分钟带你玩转 Web Services【2】CXF
  10. Chrome浏览器扩展开发系列之九:Chrome浏览器的chrome.alarms.* API
  11. 软件测试自动化的最新趋势对开源测试管理软件ITEST的启示
  12. JAVA实训第二次作业
  13. vsCode关闭代码检查工具
  14. angular 表单元素的验证清除问题
  15. 关于 php 和 python 的浮点计算 0.1+0.2
  16. Springbatch Miscellanea Notes
  17. P2858 [USACO06FEB]奶牛零食Treats for the Cows
  18. android 代码实现back键功能
  19. Codeforces 603E Pastoral Oddities
  20. django参考博客学习

热门文章

  1. 一共81个,开源大数据处理工具汇总(下),包括日志收集系统/集群管理/RPC等
  2. win7设置电脑锁屏时间
  3. MongoDB:Map-Reduce
  4. 真爱如血第七季/全集True Blood迅雷下载
  5. 获取客户端网卡MAC地址和IP地址实现JS代码
  6. .Net Standard简介
  7. [转]php-mobile-detect
  8. uva 331 Mapping the Swaps 求交换排序的map 纯DFS
  9. 第十章 dubbo线程模型
  10. Go语言之进阶篇获取文件属性