国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

OpenGLES3.0之頂點緩沖

2019-11-14 18:00:26
字體:
來源:轉載
供稿:網友

  所謂頂點緩沖就是直接將頂點數據存儲在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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇雄县| 桂平市| 开封市| 凤凰县| 桑日县| 万州区| 抚州市| 黔西| 平顶山市| 汕头市| 石林| 高陵县| 东宁县| 平邑县| 克什克腾旗| 黄冈市| 连平县| 剑阁县| 丰都县| 石狮市| 古交市| 黔南| 黄梅县| 罗平县| 全州县| 腾冲县| 时尚| 连云港市| 碌曲县| 绍兴市| 临武县| 青海省| 柞水县| 宁陕县| 科技| 咸丰县| 仁寿县| 康乐县| 额济纳旗| 筠连县| 宝清县|