在OpenGL ES 1.1和ES 2.0中使用顶点缓冲区对象进行绘图 [英] Drawing using Vertex Buffer Objects in OpenGL ES 1.1 vs ES 2.0

查看:106
本文介绍了在OpenGL ES 1.1和ES 2.0中使用顶点缓冲区对象进行绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是openGL的新手. Iam使用Apple文档作为我的主要参考文献 http://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/TechniquesforWorkingwithVertexData/TechniquesforWorkingwithVertexData.html#//apple_ref/doc/uid/TP40008793-CH107-SW6

i am new to openGL. Iam using apple documentation as my major referens http://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/TechniquesforWorkingwithVertexData/TechniquesforWorkingwithVertexData.html#//apple_ref/doc/uid/TP40008793-CH107-SW6

我的问题是我使用的是openGL ES 1.1 ,而不是 2 ,因此清单9-3中使用的函数,例如 glVertexAttribPointer ,无法识别 glEnableVertexAttribArray ...:)

My problem is that i am using openGL ES 1.1 and not 2 thus functions which are used in Listing 9-3 such as glVertexAttribPointer, glEnableVertexAttribArray are not recognized ... :)

我试图进行本文档中描述的优化: 将索引和顶点作为结构包含其所有数据:位置,颜色(清单9-1)

I trying to make the optimizations which are described in this documentation: to hold indices and vertex as a struct with all of its data: position, color (Listing 9-1)

typedef struct _vertexStruct
{
  GLfloat position[3];
  GLubyte color[4];
} VertexStruct;

const VertexStruct vertices[] = {...};
const GLushort indices[] = {...};

并使用清单9-2、9-3中的VBO

and to use VBOs such as in Listing 9-2, 9-3

正如我提到的,在那里使用的某些功能在openGL ES 1.1中不存在.我想知道在ES 1.1中是否可以通过其他代码来做到这一点?

As i mentioned, some of the functions that are used there don't exists in openGL ES 1.1. I am wondering if there is a way to do the same in ES 1.1 maybe with some other code ?

谢谢, 亚历克斯

根据基督徒的回答进行编辑,尝试使用glVertexPointer,glColorPointer. 这是代码,它打印多维数据集,但不显示颜色... :(.任何人,都可以使用 这样使用ES 1.1的VBO

Edit according to Christians answer, tried to use glVertexPointer, glColorPointer. Here is the code, it prints the cube but no colors ... :(. Anyone, is it possible to use VBOs in such menner using ES 1.1

typedef struct {
    GLubyte red;
    GLubyte green;
    GLubyte blue;
    GLubyte alpha;
} Color3D;

typedef struct {
    GLfloat x;
    GLfloat y;
    GLfloat z;
} Vertex3D;

typedef struct{
   Vector3D position;
   Color3D color;
} MeshVertex;

多维数据集数据:

static const MeshVertex meshVertices [] =
{

    { { 0.0, 1.0, 0.0 } , { 1.0, 0.0, 0.0 ,1.0 } },
    { { 0.0, 1.0, 1.0 } , { 0.0, 1.0, 0.0 ,1.0 } },
    { { 0.0, 0.0, 0.0 } , { 0.0, 0.0, 1.0 ,1.0 } },
    { { 0.0, 0.0, 1.0 } , { 1.0, 0.0, 0.0, 1.0 } },
    { { 1.0, 0.0, 0.0 } , { 0.0, 1.0, 0.0, 1.0 } },
    { { 1.0, 0.0, 1.0 } , { 0.0, 0.0, 1.0, 1.0 } },
    { { 1.0, 1.0, 0.0 } , { 1.0, 0.0, 0.0, 1.0 } },
    { { 1.0, 1.0, 1.0 } , { 0.0, 1.0, 0.0, 1.0 } }

};

static const GLushort meshIndices [] =
{   0, 1, 2 , 
    2, 1, 3 , 
    2, 3, 4 ,
    3, 5, 4 ,
    0, 2, 6 ,
    6, 2, 4 ,
    1, 7, 3 ,
    7, 5, 3 ,
    0, 6, 1 ,
    1, 6, 7 , 
    6, 4, 7 , 
    4, 5, 7 
};

功能

GLuint vertexBuffer;
GLuint indexBuffer;

- (void) CreateVertexBuffers 
{ 
    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(meshVertices), meshVertices, GL_STATIC_DRAW);

    glGenBuffers(1, &indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(meshIndices), meshIndices, GL_STATIC_DRAW);

}

- (void) DrawModelUsingVertexBuffers
{
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glVertexPointer(3, GL_FLOAT, sizeof(MeshVertex), (void*)offsetof(MeshVertex,position));
    glEnableClientState(GL_VERTEX_ARRAY);


    glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(MeshVertex), (void*)offsetof(MeshVertex,color));
    glEnableClientState(GL_COLOR_ARRAY);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);  
    glDrawElements(GL_TRIANGLE_STRIP, sizeof(meshIndices)/sizeof(GLushort), GL_UNSIGNED_SHORT,    (void*)0);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);
}

推荐答案

诸如glVertexAttribPointerglEnableVertexAttribArray之类的功能用于通用自定义顶点属性(这是OpenGL ES 2.0中唯一受支持的提交顶点数据的方法).

Functions like glVertexAttribPointer and glEnableVertexAttribArray are used for generic custom vertex attributes (which are the only supported method for submitting vertex data in OpenGL ES 2.0).

在使用固定功能管线时(如在OpenGL ES 1.1中所必须的那样),您仅使用内置属性(考虑到glVertexglColor调用,您可能在切换到顶点数组之前就已经使用过).每个属性都有一些函数,它们的调用方式类似于其即时模式对应项,例如glVertexPointerglColorPointer(而不是glVertexAttribPointer).通过使用GL_VERTEX_ARRAYGL_COLOR_ARRAY(而不是gl(En/Dis)ableVertexAttribArray)之类的常量调用gl(En/Dis)ableClientState来启用/禁用这些数组.

When using the fixed-function pipeline (as you have to in OpenGL ES 1.1) you just use the builtin attributes (think of the glVertex and glColor calls, you might have used before switching to vertex arrays). There are functions for each attribute which are called similar to their immediate mode counterparts, like glVertexPointer or glColorPointer (instead of glVertexAttribPointer). These arrays are enabled/disabled by calling gl(En/Dis)ableClientState with constants like GL_VERTEX_ARRAY or GL_COLOR_ARRAY (instead of gl(En/Dis)ableVertexAttribArray).

但是作为一般规则,您不应该学习使用2.0资源的OpenGL ES 1.1编程,因为许多信息对您来说不会有用(至少如果您是OpenGL的新手).例如,您的链接站点上描述的某些方法可能在1.1中不受支持,例如VBO甚至VAO.但是我也必须承认,我完全没有ES经验,所以对此不太确定.

But as a general rule you should not learn OpenGL ES 1.1 programming with 2.0 resources, as much of the information won't be of use to you (at least if you are new to OpenGL). For example some methods described on your linked site may not be supported in 1.1, like VBOs or even VAOs. But I also have to admit, that I have completely no ES experience, so am not perfectly sure about that.

编辑:关于更新后的代码:我认为没有颜色意味着立方体是单一颜色,可能是白色.在您的第一个代码示例中,您使用了GLubyte color[4],现在使用的是Color3D类型,也许这不适合glColorPointer(4, GL_UNSIGNED_BYTE, ...)调用(其中第一个参数是组件的数量,第二个参数是类型)?

Regarding your updated code: I assume no colors means the cube is of a single color, probably white. In your first code example you used GLubyte color[4], and now its some Color3D type, maybe this doesn't fit to the glColorPointer(4, GL_UNSIGNED_BYTE, ...) call (where the first argument is the number of components and the second one the type)?

如果您的Color3D类型仅包含3种颜色或浮点颜色,无论如何我还是建议您使用4字节的颜色,因为与位置的3个浮点数一起,您应该获得一个完美的16字节对齐的顶点,这也是他们在您提供的链接中建议的优化方案.

If your Color3D type only contains 3 colors or floating point colors, I would anyway suggest you to use 4-ubyte colors, because together with your 3 floats for the position you should get a perfectly 16-byte aligned vertex, which is also and optimization they suggest in your provided link.

顺便说一句,在CreateVertexBuffers函数中重复创建索引缓冲区是一种错字,不是吗?

And by the way, the repetition of the index buffer creation in your CreateVertexBuffers function is rather a typo, isn't it?

编辑:您的颜色包含ubytes(范围从0(黑色)到255(全色)),并使用浮点数对其进行初始化.因此,将您的浮点值1.0(这肯定意味着全彩)转换为ubyte,您得到1,与整个[0,255]范围相比,它仍然很小,因此所有内容都是黑色的.使用ubyte时,还应该使用ubyte初始化它们,因此只需在颜色数据中将每个0.0替换为0,将每个1.0替换为255.

Your colors contain ubytes (which range from 0 (black) to 255 (full color)) and you initialize them with floats. So your float value 1.0 (which should surely mean full color) is converted to ubyte and you get 1, which compared to the whole [0,255] range is still very small, so everything is black. When you use ubytes, then you should also initialize them with ubytes, so just replace every 0.0 with 0 and every 1.0 with 255 in the color data.

顺便说一句,由于您在ES 1.1中使用VBO,并且至少已绘制了一些内容,因此ES 1.1似乎支持VBO.我不知道但是我不确定它是否也支持VAO.

And by the way, since you're using VBOs in ES 1.1 and at least something is drawn, then ES 1.1 seems to support VBOs. I didn't know that. But I'm not sure if it also supports VAOs.

顺便说一句,在这两个函数的末尾使用完它们之后,应该调用glBindBuffer(GL_ARRAY_BUFFER, 0),同样调用元素数组缓冲区.否则,您可能会在其他函数中遇到问题,这些函数假定没有缓冲区,但缓冲区仍然绑定.永远记住,OpenGL是一个状态机,您设置的每个状态都会保留下来,直到再次更改它或上下文被破坏为止.

And by the way, you should call glBindBuffer(GL_ARRAY_BUFFER, 0) and likewise for the element array buffer after you're finished with using them at the end of these two functions. Othwerwise you may get problems in other functions which assume no buffers but the buffers are still bound. Always remember that OpenGL is a state machine and every state you set stays until it's changed again or the context is destroyed.

这篇关于在OpenGL ES 1.1和ES 2.0中使用顶点缓冲区对象进行绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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