0

This problem is driving me crazy cause it seems to be right. I am working with iOS 7 and OpenGL ES 3.0.

The variables' types are:

float vertices[24];
unsigned short indices[24];
unsigned char colors[32];
unsigned int vboIds[3];

This is only executing once:

vertices[0] = -0.5f; vertices[1] = -0.5f; vertices[2] = 0.5f;
vertices[3] = 0.5f; vertices[4] = -0.5f; vertices[5] = 0.5f;
vertices[6] = 0.5f; vertices[7] = 0.5f; vertices[8] = 0.5f;
vertices[9] = -0.5f; vertices[10] = 0.5f; vertices[11] = 0.5f;
vertices[12] = -0.5f; vertices[13] = -0.5f; vertices[14] = -0.5f;
vertices[15] = 0.5f; vertices[16] = -0.5f; vertices[17] = -0.5f;
vertices[18] = 0.5f; vertices[19] = 0.5f; vertices[20] = -0.5f;
vertices[21] = -0.5f; vertices[22] = 0.5f; vertices[23] = -0.5f;

indices[0] = 0; indices[1] = 1; // front
indices[2] = 1; indices[3] = 2;
indices[4] = 2; indices[5] = 3;
indices[6] = 3; indices[7] = 0;

indices[8] = 4; indices[9] = 5; // back
indices[10] = 5; indices[11] = 6;
indices[12] = 6; indices[13] = 7;
indices[14] = 7; indices[15] = 4;

indices[16] = 0; indices[17] = 4; // sides
indices[18] = 1; indices[19] = 5;
indices[20] = 2; indices[21] = 6;
indices[22] = 3; indices[23] = 7;

for (size_t i = 0;i<8;++i) {
    colors[i*4+0] = (unsigned char)((vertices[i*3+0]+0.5)*255);
    colors[i*4+1] = (unsigned char)((vertices[i*3+1]+0.5)*255);
    colors[i*4+2] = (unsigned char)((vertices[i*3+2]+0.5)*255);
    colors[i*4+3] = 255;
}

GL(glGenBuffers(3, vboIds));

GL(glBindBuffer(GL_ARRAY_BUFFER, vboIds[0]));
GL(glBufferData(GL_ARRAY_BUFFER, sizeof(float)*24, vertices, GL_STATIC_DRAW));

GL(glBindBuffer(GL_ARRAY_BUFFER, vboIds[1]));
GL(glBufferData(GL_ARRAY_BUFFER, sizeof(char)*32, colors, GL_STATIC_DRAW));

GL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIds[2]));
GL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(short)*24, indices, GL_STATIC_DRAW));

And this is always executing when rendering:

GL(glBindBuffer(GL_ARRAY_BUFFER, vboIds[0]));
GL(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float)*3, 0));
GL(glEnableVertexAttribArray(0));

GL(glBindBuffer(GL_ARRAY_BUFFER, vboIds[1]));
GL(glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(char)*4, 0));
GL(glEnableVertexAttribArray(1));

GL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIds[2]));

GL(glDrawElements(GL_LINES, 24, GL_UNSIGNED_SHORT, 0));

GL(glDisableVertexAttribArray(0));
GL(glDisableVertexAttribArray(1));

GL(glBindBuffer(GL_ARRAY_BUFFER, 0));
GL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));

The glDrawElements(...) is throwing an EXC_BAD_ACCESS (code=1, address=0x0) and I do not know why.

Alex Moreno
  • 729
  • 3
  • 8
  • 14
  • Since I couldn't see anything wrong, I tried the code. I was too lazy to create an iOS project, but I dropped it into a GLUT app on my Mac. It doesn't crash, and it rendered a square, which I expected because I didn't apply any transformations. So either there's something different on iOS, or there's something going on in code we are not seeing here. One recommendation: I would use the GL defined types for vertex data, like GLushort, GLuchar, GLfloat, etc. There's no guarantee that for example GLushort and unsigned short are the same type on every platform. I think they are on iOS. – Reto Koradi Apr 22 '14 at 07:23
  • I have just tried to change the data typer to OpenGL's and it is throwing the same error. – Alex Moreno Apr 22 '14 at 09:19

1 Answers1

1

I solved it by placing the:

GL(glGenBuffers(3, vboIds));

GL(glBindBuffer(GL_ARRAY_BUFFER, vboIds[0]));
GL(glBufferData(GL_ARRAY_BUFFER, sizeof(float)*24, vertices, GL_STATIC_DRAW));

GL(glBindBuffer(GL_ARRAY_BUFFER, vboIds[1]));
GL(glBufferData(GL_ARRAY_BUFFER, sizeof(char)*32, colors, GL_STATIC_DRAW));

GL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIds[2]));
GL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(short)*24, indices, GL_STATIC_DRAW));

inside a InitGL method instead of into the constructor.

Alex Moreno
  • 729
  • 3
  • 8
  • 14
  • If you had the code in the constructor, your object was probably instantiated before the context was created and current. When I write C++ classes that encapsulate GL objects, I always use an init type method to create the GL objects. If that is done in the constructor, it's much too easy to accidentally create the object before the context is ready. – Reto Koradi Apr 22 '14 at 15:20