+
+
+
+/* Version for OpenGL (ES) >= 2.0 */
+static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLfloat *textcs, GLsizei numVertices,
+ GLushort *vertIdxs, GLsizei numParts, GLsizei numVertIdxsPerPart,
+ GLint attribute_v_coord, GLint attribute_v_normal, GLint attribute_v_texture)
+{
+ GLuint vbo_coords = 0, vbo_normals = 0, vbo_textcs = 0, ibo_elements = 0;
+ GLsizei numVertIdxs = numParts * numVertIdxsPerPart;
+ int i;
+
+ if (numVertices > 0 && attribute_v_coord != -1) {
+ fghGenBuffers(1, &vbo_coords);
+ fghBindBuffer(FGH_ARRAY_BUFFER, vbo_coords);
+ fghBufferData(FGH_ARRAY_BUFFER, numVertices * 3 * sizeof(vertices[0]),
+ vertices, FGH_STATIC_DRAW);
+ fghBindBuffer(FGH_ARRAY_BUFFER, 0);
+ }
+
+ if (numVertices > 0 && attribute_v_normal != -1) {
+ fghGenBuffers(1, &vbo_normals);
+ fghBindBuffer(FGH_ARRAY_BUFFER, vbo_normals);
+ fghBufferData(FGH_ARRAY_BUFFER, numVertices * 3 * sizeof(normals[0]),
+ normals, FGH_STATIC_DRAW);
+ fghBindBuffer(FGH_ARRAY_BUFFER, 0);
+ }
+
+ if (numVertices > 0 && attribute_v_texture != -1 && textcs) {
+ fghGenBuffers(1, &vbo_textcs);
+ fghBindBuffer(FGH_ARRAY_BUFFER, vbo_textcs);
+ fghBufferData(FGH_ARRAY_BUFFER, numVertices * 2 * sizeof(textcs[0]),
+ textcs, FGH_STATIC_DRAW);
+ fghBindBuffer(FGH_ARRAY_BUFFER, 0);
+ }
+
+ if (vertIdxs != NULL) {
+ fghGenBuffers(1, &ibo_elements);
+ fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, ibo_elements);
+ fghBufferData(FGH_ELEMENT_ARRAY_BUFFER, numVertIdxs * sizeof(vertIdxs[0]),
+ vertIdxs, FGH_STATIC_DRAW);
+ fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, 0);
+ }
+
+ if (vbo_coords) {
+ fghEnableVertexAttribArray(attribute_v_coord);
+ fghBindBuffer(FGH_ARRAY_BUFFER, vbo_coords);
+ fghVertexAttribPointer(
+ attribute_v_coord, /* attribute */
+ 3, /* number of elements per vertex, here (x,y,z) */
+ GL_FLOAT, /* the type of each element */
+ GL_FALSE, /* take our values as-is */
+ 0, /* no extra data between each position */
+ 0 /* offset of first element */
+ );
+ fghBindBuffer(FGH_ARRAY_BUFFER, 0);
+ };
+
+ if (vbo_normals) {
+ fghEnableVertexAttribArray(attribute_v_normal);
+ fghBindBuffer(FGH_ARRAY_BUFFER, vbo_normals);
+ fghVertexAttribPointer(
+ attribute_v_normal, /* attribute */
+ 3, /* number of elements per vertex, here (x,y,z) */
+ GL_FLOAT, /* the type of each element */
+ GL_FALSE, /* take our values as-is */
+ 0, /* no extra data between each position */
+ 0 /* offset of first element */
+ );
+ fghBindBuffer(FGH_ARRAY_BUFFER, 0);
+ };
+
+ if (vbo_textcs) {
+ fghEnableVertexAttribArray(attribute_v_texture);
+ fghBindBuffer(FGH_ARRAY_BUFFER, vbo_textcs);
+ fghVertexAttribPointer(
+ attribute_v_texture,/* attribute */
+ 2, /* number of elements per vertex, here (s,t) */
+ GL_FLOAT, /* the type of each element */
+ GL_FALSE, /* take our values as-is */
+ 0, /* no extra data between each position */
+ 0 /* offset of first element */
+ );
+ fghBindBuffer(FGH_ARRAY_BUFFER, 0);
+ };
+
+ if (vertIdxs == NULL) {
+ glDrawArrays(GL_TRIANGLES, 0, numVertices);
+ } else {
+ fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, ibo_elements);
+ if (numParts>1) {
+ for (i=0; i<numParts; i++) {
+ glDrawElements(GL_TRIANGLE_STRIP, numVertIdxsPerPart, GL_UNSIGNED_SHORT, (GLvoid*)(sizeof(vertIdxs[0])*i*numVertIdxsPerPart));
+ }
+ } else {
+ glDrawElements(GL_TRIANGLES, numVertIdxsPerPart, GL_UNSIGNED_SHORT, 0);
+ }
+ /* Clean existing bindings before clean-up */
+ /* Android showed instability otherwise */
+ fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, 0);
+ }
+
+ if (vbo_coords != 0)
+ fghDisableVertexAttribArray(attribute_v_coord);
+ if (vbo_normals != 0)
+ fghDisableVertexAttribArray(attribute_v_normal);
+ if (vbo_textcs != 0)
+ fghDisableVertexAttribArray(attribute_v_texture);
+
+ if (vbo_coords != 0)
+ fghDeleteBuffers(1, &vbo_coords);
+ if (vbo_normals != 0)
+ fghDeleteBuffers(1, &vbo_normals);
+ if (vbo_textcs != 0)
+ fghDeleteBuffers(1, &vbo_textcs);
+ if (ibo_elements != 0)
+ fghDeleteBuffers(1, &ibo_elements);
+}
+
+
+
+/**
+ * Generate vertex indices for visualizing the normals.
+ * vertices are written into verticesForNormalVisualization.
+ * This must be freed by caller, we do the free at the
+ * end of fghDrawNormalVisualization11/fghDrawNormalVisualization20
+ */
+static GLfloat *verticesForNormalVisualization;
+static GLsizei numNormalVertices = 0;
+static void fghGenerateNormalVisualization(GLfloat *vertices, GLfloat *normals, GLsizei numVertices)
+{
+ int i,j;
+ numNormalVertices = numVertices * 2;
+ verticesForNormalVisualization = malloc(numNormalVertices*3 * sizeof(GLfloat));
+
+ for (i=0,j=0; i<numNormalVertices*3/2; i+=3, j+=6)
+ {
+ verticesForNormalVisualization[j+0] = vertices[i+0];
+ verticesForNormalVisualization[j+1] = vertices[i+1];
+ verticesForNormalVisualization[j+2] = vertices[i+2];
+ verticesForNormalVisualization[j+3] = vertices[i+0] + normals[i+0]/4.f;
+ verticesForNormalVisualization[j+4] = vertices[i+1] + normals[i+1]/4.f;
+ verticesForNormalVisualization[j+5] = vertices[i+2] + normals[i+2]/4.f;
+ }
+}
+
+/* Version for OpenGL (ES) 1.1 */
+static void fghDrawNormalVisualization11()
+{
+ GLfloat currentColor[4];
+ /* Setup draw color: (1,1,1)-shape's color */
+ glGetFloatv(GL_CURRENT_COLOR,currentColor);
+ glColor4f(1-currentColor[0],1-currentColor[1],1-currentColor[2],currentColor[3]);
+
+ glEnableClientState(GL_VERTEX_ARRAY);
+
+ glVertexPointer(3, GL_FLOAT, 0, verticesForNormalVisualization);
+ glDrawArrays(GL_LINES, 0, numNormalVertices);
+
+ glDisableClientState(GL_VERTEX_ARRAY);
+
+ /* Done, free memory, reset color */
+ free(verticesForNormalVisualization);
+ glColor4f(currentColor[0],currentColor[1],currentColor[2],currentColor[3]);
+}
+
+/* Version for OpenGL (ES) >= 2.0 */
+static void fghDrawNormalVisualization20(GLint attribute_v_coord)
+{
+ GLuint vbo_coords = 0;
+
+ if (attribute_v_coord != -1) {
+ fghGenBuffers(1, &vbo_coords);
+ fghBindBuffer(FGH_ARRAY_BUFFER, vbo_coords);
+ fghBufferData(FGH_ARRAY_BUFFER, numNormalVertices * 3 * sizeof(verticesForNormalVisualization[0]),
+ verticesForNormalVisualization, FGH_STATIC_DRAW);
+ }
+
+
+ if (vbo_coords) {
+ fghEnableVertexAttribArray(attribute_v_coord);
+ fghBindBuffer(FGH_ARRAY_BUFFER, vbo_coords);
+ fghVertexAttribPointer(
+ attribute_v_coord, /* attribute */
+ 3, /* number of elements per vertex, here (x,y,z) */
+ GL_FLOAT, /* the type of each element */
+ GL_FALSE, /* take our values as-is */
+ 0, /* no extra data between each position */
+ 0 /* offset of first element */
+ );
+ fghBindBuffer(FGH_ARRAY_BUFFER, 0);
+ }
+
+ glDrawArrays(GL_LINES, 0, numNormalVertices);
+
+ if (vbo_coords != 0)
+ fghDisableVertexAttribArray(attribute_v_coord);
+
+ if (vbo_coords != 0)
+ fghDeleteBuffers(1, &vbo_coords);
+
+ /* Done, free memory */
+ free(verticesForNormalVisualization);
+}
+
+/**
+ * Generate all combinations of vertices and normals needed to draw object.
+ * Optional shape decomposition to triangles:
+ * We'll use glDrawElements to draw all shapes that are not naturally
+ * composed of triangles, so generate an index vector here, using the
+ * below sampling scheme.