Wiki

Clone wiki

FPGL / FPM (Full Pipe Model) Binary Format Specification Proposal

FPM (Full Pipe Model) Binary Format Specification Proposal

Introduction

The proposed FPM binary format specification is based the following assumptions:

  1. the vertex data must be organized to be ready for OpenGL ES specification, with less possible intermediate conversions;
  2. ideally, the vertex data should be loaded as a single buffer and in a single operation.

To understand OpenGL ES vertex buffers we can refer to Apple documentation Best Practices for Working with Vertex Data.

The main requirements of OpenGL ES specification are:

  1. to use interleaved vertex data;
  2. to use aligned vertex data;
  3. to use vertex buffer objects to copy vertex data.

Interleaved vertex data

Currently the FPGLMesh class has separate buffers for vertex data: one for vertices positions, one for texture UVs, one for vertices normals and one for vertices colors.

To create a single buffer of interleaved data we should use a structure like FPVertex that is declared in "FPVertex.h".

#!objective-c

struct FPVertex_S {
    FPVec3f position;
    FPVec3f normal;
    FPVec2f uv;
    FPGLColor color;
};
typedef struct FPVertex_S FPVertex;
typedef FPVertex * FPVertexPtr;

Accordingly to Apple documentation we can do something like the following to create a vertex buffer and draw it.

#!objective-c

FPVertex * vertices;
GLushort * indices;

GLuint vertexBuffer;
GLuint indexBuffer;

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

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

Then we can draw the mesh using something like this.

#!objective-c

void draw() {
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glVertexAttribPointer(ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(FPVertex), (void*)offsetof(FPVertex, position));
    glEnableVertexAttribArray(ATTRIB_POSITION);
    glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(FPVertex), (void*)offsetof(FPVertex, color);
    glEnableVertexAttribArray(ATTRIB_COLOR);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
    glDrawElements(GL_TRIANGLE_STRIP, sizeof(indices)/sizeof(GLubyte), GL_UNSIGNED_BYTE, (void*)0);
}

To create more readable code we can create global variables to hold the value returned by functions like sizeof() and offsetof(). For example sizeof(FPVertex) can be precalculated when the engine initialize.

As a reminder I publish here a copy of the glVertexAttribPointer() function documentation.

#!

C SPECIFICATION
       void glVertexAttribPointer(GLuint index, GLint size, GLenum type,
                                  GLboolean normalized, GLsizei stride,
                                  const GLvoid * pointer);

       void glVertexAttribIPointer(GLuint index, GLint size, GLenum type,
                                   GLsizei stride, const GLvoid * pointer);

PARAMETERS
       index
           Specifies the index of the generic vertex attribute to be modified.

       size
           Specifies the number of components per generic vertex attribute.
           Must be 1, 2, 3, 4. Additionally, the symbolic constant GL_BGRA is
           accepted by glVertexAttribPointer. The initial value is 4.

       type
           Specifies the data type of each component in the array. The
           symbolic constants GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT,
           GL_UNSIGNED_SHORT, GL_INT, and GL_UNSIGNED_INT are accepted by both
           functions. Additionally GL_HALF_FLOAT, GL_FLOAT, GL_DOUBLE,
           GL_INT_2_10_10_10_REV, and GL_UNSIGNED_INT_2_10_10_10_REV are
           accepted by glVertexAttribPointer. The initial value is GL_FLOAT.

       normalized
           For glVertexAttribPointer, specifies whether fixed-point data
           values should be normalized (GL_TRUE) or converted directly as
           fixed-point values (GL_FALSE) when they are accessed.

       stride
           Specifies the byte offset between consecutive generic vertex
           attributes. If stride is 0, the generic vertex attributes are
           understood to be tightly packed in the array. The initial value is
           0.

       pointer
           Specifies a offset of the first component of the first generic
           vertex attribute in the array in the data store of the buffer
           currently bound to the GL_ARRAY_BUFFER target. The initial value is
           0.

Updated