2011-10-10 64 views
4

我正在使用Android中的OpenGL ES 2.0,并通过the docs for GLES20查看我遇到了以下方法:GLES20.glVertexAttribPointer/glDrawElements中的“offset”参数是什么,ptr/indices来自哪里?

public static void glDrawElements(
    int mode, int count, int type, Buffer indices) 
public static void glDrawElements(
    int mode, int count, int type, int offset) 

public static void glVertexAttribPointer(
    int indx, int size, int type, boolean normalized, int stride, Buffer ptr) 
public static void glVertexAttribPointer(
    int indx, int size, int type, boolean normalized, int stride, int offset) 

采用Buffer对象的两种方法对我有意义,但另外两种方法不合适吨。他们在哪里获得index/attibute-values(分别),offset是什么? (我假设这两个问题有相同的答案。)

+0

这两个怪异的方法都被标记为“9级”,(目前我编码为8级),所以这更多的是满足我的好奇心比什么都重要...... –

回答

6

原型中的偏移表示您在此调用之前提交了INDEX数组。如果使用VBO(顶点缓冲区对象),则应该使用该指针。 glBindBuffer绑定索引缓冲区并在下一次调用时指定偏移量(如果需要)。

首先,你需要绑定缓存(索引缓冲这里),您可以从您的内容与“偏移”开始指定。

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_resources.element_buffer); 
glDrawElements(
    GL_TRIANGLE_STRIP, /* mode */ 
    4,     /* count */ 
    GL_UNSIGNED_SHORT, /* type */ 
    (void*)0   /* element array buffer offset */ 
); 

public static void glVertexAttribPointer(
int indx, int size, int type, boolean normalized, int stride, int offset) 

这意味着,你的这一号召提交顶点缓冲区之前,你可以指定它应该采取与在缓冲区中的偏移量。 请查看以下链接获取更多帮助。 http://duriansoftware.com/joe/An-intro-to-modern-OpenGL.-Chapter-2.3:-Rendering.html

正如您所料,两者都有相同的原因:)。希望这有助于!

更新:你需要创建一个缓冲区绑定it.This可以通过以下步骤来完成。

 glGenBuffers(); // create a buffer object 
    glBindBuffer(); // use the buffer 
    glBufferData(); // allocate memory in the buffer 

检查此链接以创建VBO。 http://www.opengl.org/wiki/Vertex_Buffer_Object

关于偏移类型:偏移量作为指针传递,但该参数用于其整数值,因此我们将整型转换为void *。

+0

谢谢,但会是什么非零“偏移”变成,当你上面似乎使用NULL?另外,GLES.glBindBuffer只需要几个整数。 n个索引/属性值通过了哪里?我对VBOs一无所知......我想我还有一些阅读要做。 –

+0

@LaurenceGonsalves:为你添加了一个更新的问题。让我知道如果有任何怀疑与此有关。 – Ayyappa

+0

谢谢。这清除了一切。我的一部分困惑似乎也来自于我正在研究glDrawElements的OpenGL(not-ES)文档,并没有看到任何提及的指针的奇怪的双重用法。 (另外:Ick!他们为什么这样做?OpenGL通常不会为添加不同类型参数的额外函数而感到害羞。) –

0

方法

public static void glDrawElements(
    int mode, int count, int type, int offset) 

用于与VBO指数呈现。

偏移 - 是一个以字节为单位偏移indexVBO ;

使用它之前,您应该上传指数来VBO对象:

import static android.opengl.GLES20.*; 

private static final int SIZEOF_SHORT = 2; 

void fillIndices() { 
    int indexAmount = ...; 
    int sizeBytes = indexAmount * SIZEOF_SHORT; 
    ShortBuffer indicesS = ByteBuffer.allocateDirect(sizeBytes).order(ByteOrder.nativeOrder()).asShortBuffer(); 
    indicesS.put(getShortIndices(indexAmount)); 

    indicesS.position(0); 

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBO); 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeBytes, indicesS, GL_STATIC_DRAW); 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 
} 

short[] getShortIndices(int indexAmount) { 
    short[] indices = new short[indexAmount]; 
    // fill indices here 
    return indices; 
} 

然后你就可以将其绑定与glDrawElements()

public void draw(){ 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBO); 
    glDrawElements(GL_TRIANGLE_STRIP, indexCount, GL_UNSIGNED_SHORT, firstIndex * SIZEOF_SHORT);} 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 
} 

您可以使用GL_UNSIGNED_BYTE,GL_UNSIGNED_SHORT和GL_UNSIGNED_INT为使用它指数。

相关问题