OpenGL发送顶点指针或生成缓冲区 [英] OpenGL sending vertex pointers or generating buffers

查看:145
本文介绍了OpenGL发送顶点指针或生成缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚使用GLKit启动了iPhone的OpenGL。我的编程背景几乎只是Java和目标c,十年前几乎没有使用C,C ++的经验。剩下的一切只是我如何处理指针的远程记忆-我认为我失败了。

I just started OpenGL for iPhone, using GLKit. My programming background is almost just java and objective c with little,little experiences with C, C++ over ten years ago. All what remained is a remote memory of how I struggled with pointers - and I think I failed.

现在看来一切都回到了我的脑海...

Now it seems as if it all comes back to me ...

我去过非常棒的Ian Terrel教程系列,对我很有帮助(谢谢!!!)。

I went to some iterations of the great, great tutorial series of Ian Terrel, which really helped me (Thanks!!!).

此问题与代码的以下部分有关(大部分直接从教程中获取):

This question is about the following parts of the code (which is mostly directly taken from the tutorials):

@interface AAAShape : NSObject
{
  NSMutableData *vertexData;
  // ...
}
@property(readonly) int vertexCount;
@property(readonly) GLKVector2 *vertices; 
//...    
@end

@implementation AAAShape
//...


-(GLKVector2 *)vertices
{
  if(!vertexData)
  {
    vertexData = [NSMutableData dataWithLength:sizeof(GLKVector2)*self.vertexCount];
  }
  return [vertexData mutableBytes];
}

-(void)renderInScene:(AAAScene *)scene 
{
  //... Effect Stuff
  //...
  glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, self.vertices);
  //...
}
//...
@end

//Using the Shape: 
//(AATriangle is of AAAShape and implements the vertextCount with "return 3;")    
AAATriangle *triangle = [[AAATriangle alloc]init];

triangle.vertices[0] = GLKVector2Make(2., .0);
triangle.vertices[1] = GLKVector2Make(-2., .0);
triangle.vertices[2] = GLKVector2Make(.0, -3.);
//...

所有这些都很好,但是后来我偶然发现Apple的 OpenGl指南中的以下内容:

All of this works really fine, but then I stumbled upon the following in Apple's OpenGl Guide:


[...],但效率不高。每次调用DrawModel时,索引
和顶点数据都会复制到OpenGL ES,并传输到
图形硬件。[...]会影响性能。您的应用程序
应该将其顶点数据存储在顶点缓冲对象(VBO)中。 [...]

[...], but is inefficient. Each time DrawModel is called, the index and vertex data are copied to OpenGL ES, and transferred to the graphics hardware.[...] can impact performance. [...]your application should store its vertex data in a vertex buffer object (VBO). [...]

此处建议的代码示例(这样做的其他来源几乎相同)如下:

The suggested code sample there, for doing that (other sources showing nearly the same) looks like this:

GLuint    vertexBuffer;
void CreateVertexBuffers()
{

    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

} 

,然后绘制:

void DrawModelUsingVertexBuffers()
{
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glVertexAttribPointer(ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(vertexStruct), (void*)offsetof(vertexStruct,position));
    glEnableVertexAttribArray(ATTRIB_POSITION);
   //...
 }

我对以下几个问题


  • 上述性能影响有多重要?

  • 上面的第一个代码示例(伊恩/我的代码)中真正发生了什么?
    >

    • 如果顶点是只读的,为什么要设置顶点[i],以及在何处以及如何为顶点分配内存?

    • How significant are the mentioned performance impacts? Is it necessary to change the code?
    • What the ... is really going on in the first code samples above (Ian's/my code)?
      • Why is it possible to set vertices[i] if vertices is readonly and where and how memory is allocated for vertices?

      推荐答案



      • 上述性能影响有多重要?有必要更改代码吗?

      如果数据在不断变化,则收效甚微从VBO。但是,如果几何是静态的,则会产生巨大的差异,因为它节省了CPU和GPU之间的宝贵带宽,而不必一直复制它。

      If the data is constantly changing, there is little gained from a VBO. If however the geometry is static it makes a huge difference, as it saves precious bandwidth between CPU and GPU not to copy it all the time.



      • 上面的第一个代码示例(Ian的/我的代码)中...的实际含义是什么?

      一个数组充满了顶点数据。然后,将OpenGL中的指针设置为该数据。调用glDraw…时,指针将被取消引用,并且数据将从进程内存中获取到呈现侧,并在绘制操作中在那里进行处理。

      An array is filled with the vertex data. Then a pointer in OpenGL is set to that data. When glDraw… is called, the pointers are dereferenced, and the data fetch from process memory to the render side, processed there in drawing operations.



      • 如果顶点是只读的,为什么要设置顶点[i],以及在何处以及如何为顶点分配内存?

      因为只有指针是只读的。它指向的变量是读/写

      Because only the pointer is readonly. The variable it points to is read/write



      • 我在哪里可以将代码放在上面(创建缓冲区和-binding的东西)在Ian的方法中

      glGenBuffers和glBufferData转到顶点数据创建。 glVertexPointer保持原样。

      glGenBuffers and glBufferData go to the vertex data creation. glVertexPointer stays where it is. and glBindBuffer is used at both sites.


      以及为什么绑定之间没有连接(在变量中是方法调用之类的东西)和图纸?

      and why is there no connection (in means of variables are method calls or something) between the binding and the drawing?

      我注意到您确定这个问题是什么意思。

      I'm note sure what you mean by that question.

      这篇关于OpenGL发送顶点指针或生成缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆