Note that there are some explanatory texts on larger screens.

plurals
  1. POProgrammatically Generate OpenGL Vertex Data in iOS
    text
    copied!<p>Basically what I want to do is generate my vertex coordinates programmatically instead of storing it in a statically predefined array. Unfortunately I am not able to convert a very simple example to a dynamic array.</p> <p>Everything works fine if I stick to static arrays:</p> <pre><code>typedef struct { GLfloat Position[3]; GLfloat Color[4]; GLfloat TexCoord[2]; float Normal[3]; } Vertex; Vertex sphereVertices[] = { {{1, -1, 1}, {1, 0, 0, 1}, {1, 0}, {0, 0, 1}}, {{1, 1, 1}, {0, 1, 0, 1}, {1, 1}, {0, 0, 1}}, {{-1, 1, 1}, {0, 0, 1, 1}, {0, 1}, {0, 0, 1}}, {{-1, -1, 1}, {0, 0, 0, 1}, {0, 0}, {0, 0, 1}} }; GLubyte sphereIndices [] = { 0, 1, 2, 2, 3, 0 }; </code></pre> <p>...</p> <pre><code>glGenBuffers(1, &amp;sphereIndexBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sphereIndexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(sphereIndices), sphereIndices, GL_STATIC_DRAW); glEnableVertexAttribArray(GLKVertexAttribPosition); glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position)); </code></pre> <p>...</p> <pre><code>glDrawElements(GL_TRIANGLES, 6 * sizeof(GLubyte), GL_UNSIGNED_BYTE, 0); </code></pre> <p>As soon as I switch my indices to a dynamic array only the first triangle shows up.</p> <pre><code>GLubyte *sphereIndices; +(void)initialize { sphereIndices = malloc(6 * sizeof(GLubyte)); sphereIndices[0] = 0; sphereIndices[1] = 1; sphereIndices[2] = 2; sphereIndices[3] = 2; sphereIndices[4] = 3; sphereIndices[5] = 0; } </code></pre> <p>Probably this has something to do with pointers. Does anybody know what I am doing wrong? </p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload