博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OpenGL ES 3.0之顶点缓冲
阅读量:7110 次
发布时间:2019-06-28

本文共 4745 字,大约阅读时间需要 15 分钟。

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

  操作步骤

  1.创建顶点缓冲对象

  

GLuint vertexBufferID;

  2.分配空间

  

glGenBuffers(1,  &vertexBufferID);

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

  

glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);

  4.初始化缓冲区数据

  

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

  5.启用顶点属性数组

  

glEnableVertexAttribArray(GLKVertexAttribPosition);

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

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

  7.绘制

  

glDrawArrays(GL_TRIANGLES, 0,3);

下面赋全部代码

@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 vertextypedef struct {   GLKVector3  positionCoords;}SceneVertex;// Define vertex data for a triangle to use in examplestatic 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(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}/////// 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}/////// 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];}@end

转载地址:http://ddmhl.baihongyu.com/

你可能感兴趣的文章
libevent(一)定时器Demo
查看>>
(4)Django框架学习-Model篇
查看>>
Python 中文编码
查看>>
BigDecimal舍入模式使用及建议
查看>>
Tornado初探
查看>>
HDU5690: All X(大数取模,同余)
查看>>
HDU2544:最短路(最短路)
查看>>
js ==与===区别(两个等号与三个等号)
查看>>
小程序:最难点For的wx:key
查看>>
struts2 标签的使用之一 s:if
查看>>
Light Switching(SPOJ LITE)—— 线段树成段更新异或值
查看>>
2015 Multi-University Training Contest 2 1002 Buildings
查看>>
滑动平均线的notebook画法
查看>>
Spring_IOC
查看>>
CodeForces - 633 H Fibonacci-ish II(莫队+线段树)
查看>>
1012. 数字分类 (20)
查看>>
个人最终总结
查看>>
Array inversion case
查看>>
JavaScript数组
查看>>
Juqery让世界更美好--超级简单实用的(上、下)自动翻的最佳效果,有图为证!...
查看>>