int i,j,idx, nVert;
GLfloat *vertices, *normals;
+ if (slices * stacks > 65535)
+ fgWarning("fghSphere: too many slices or stacks requested, indices will wrap");
+
/* Generate vertices and normals */
fghGenerateSphere((GLfloat)radius,slices,stacks,&vertices,&normals,&nVert);
if (useWireMode)
{
- GLuint *sliceIdx, *stackIdx;
+ GLushort *sliceIdx, *stackIdx;
/* First, generate vertex index arrays for drawing with glDrawElements
* We have a bunch of line_loops to draw for each stack, and a
* bunch for each slice.
*/
- sliceIdx = malloc(slices*(stacks+1)*sizeof(GLuint));
- stackIdx = malloc(slices*(stacks-1)*sizeof(GLuint));
+ sliceIdx = malloc(slices*(stacks+1)*sizeof(GLushort));
+ stackIdx = malloc(slices*(stacks-1)*sizeof(GLushort));
/* generate for each stack */
for (i=0,idx=0; i<slices; i++)
{
- GLuint offset = 1+i; /* start at 1 (0 is top vertex), and we advance one slice as we go along */
+ GLushort offset = 1+i; /* start at 1 (0 is top vertex), and we advance one slice as we go along */
sliceIdx[idx++] = 0; /* vertex on top */
for (j=0; j<stacks-1; j++, idx++)
{
/* generate for each stack */
for (i=0,idx=0; i<stacks-1; i++)
{
- GLuint offset = 1+i*slices; /* start at 1 (0 is top vertex), and we advance one stack down as we go along */
+ GLushort offset = 1+i*slices; /* start at 1 (0 is top vertex), and we advance one stack down as we go along */
for (j=0; j<slices; j++, idx++)
{
stackIdx[idx] = offset+j;
glNormalPointer(GL_FLOAT, 0, normals);
/*draw slices*/
for (i=0; i<slices; i++)
- glDrawElements(GL_LINE_STRIP,stacks+1,GL_UNSIGNED_INT,sliceIdx+i*(stacks+1));
+ glDrawElements(GL_LINE_STRIP,stacks+1,GL_UNSIGNED_SHORT,sliceIdx+i*(stacks+1));
/*draw stacks*/
for (i=0; i<stacks-1; i++)
- glDrawElements(GL_LINE_LOOP, slices,GL_UNSIGNED_INT,stackIdx+i*slices);
+ glDrawElements(GL_LINE_LOOP, slices,GL_UNSIGNED_SHORT,stackIdx+i*slices);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
else
{
- GLuint *topIdx, *bottomIdx, *stripIdx;
+ GLushort *topIdx, *bottomIdx, *stripIdx;
/* First, generate vertex index arrays for drawing with glDrawElements
* Top and bottom are covered with a triangle fan
* Each other stack with triangle strip. Only need to generate on
*/
/* Allocate buffers for indices, bail out if memory allocation fails */
- topIdx = malloc((slices+2)*sizeof(GLuint));
- bottomIdx = malloc((slices+2)*sizeof(GLuint));
- stripIdx = malloc((slices+1)*2*(stacks-2)*sizeof(GLuint));
+ topIdx = malloc((slices+2)*sizeof(GLushort));
+ bottomIdx = malloc((slices+2)*sizeof(GLushort));
+ stripIdx = malloc((slices+1)*2*(stacks-2)*sizeof(GLushort));
if (!(topIdx) || !(bottomIdx) || !(stripIdx))
{
free(topIdx);
fgError("Failed to allocate memory in fghGenerateSphere");
}
+ /* TODO: Can do top and bottom as Triangle strip as well
+ (just need to repeat top/btoom vertex a lot). Then we can draw
+ the whole thing with just one index array and one for-looped call
+ to glDrawElements.. That'll make it easier to reuse code with other
+ Circular objects too
+ */
topIdx[0]=0;
topIdx[1] = 1; /* repeat first slice's idx for closing off shape */
for (j=slices, idx=2; j>0; j--, idx++)
/* Strip indices are relative to first index belonging to strip, NOT relative to first vertex/normal pair in array */
for (i=0,idx=0; i<stacks-2; i++, idx+=2)
{
- GLuint offset = 1+i*slices; /* triangle_strip indices start at 1 (0 is top vertex), and we advance one stack down as we go along */
+ GLushort offset = 1+i*slices; /* triangle_strip indices start at 1 (0 is top vertex), and we advance one stack down as we go along */
for (j=0; j<slices; j++, idx+=2)
{
stripIdx[idx ] = offset+j+slices;
glVertexPointer(3, GL_FLOAT, 0, vertices);
glNormalPointer(GL_FLOAT, 0, normals);
/*draw top*/
- glDrawElements(GL_TRIANGLE_FAN,slices+2,GL_UNSIGNED_INT,topIdx);
+ glDrawElements(GL_TRIANGLE_FAN,slices+2,GL_UNSIGNED_SHORT,topIdx);
/*draw stacks*/
for (i=0; i<stacks-2; i++)
- glDrawElements(GL_TRIANGLE_STRIP,(slices+1)*2,GL_UNSIGNED_INT,stripIdx+i*(slices+1)*2);
+ glDrawElements(GL_TRIANGLE_STRIP,(slices+1)*2,GL_UNSIGNED_SHORT,stripIdx+i*(slices+1)*2);
/*draw bottom*/
- glDrawElements(GL_TRIANGLE_FAN,slices+2,GL_UNSIGNED_INT,bottomIdx);
+ glDrawElements(GL_TRIANGLE_FAN,slices+2,GL_UNSIGNED_SHORT,bottomIdx);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
/* -- INTERFACE FUNCTIONS ---------------------------------------------- */
-#ifndef EGL_VERSION_1_0
/*
* Draws a solid sphere
*/
}
+#ifndef EGL_VERSION_1_0
/*
* Draws a solid cone
*/
glEnd();
- /* Cover each stack with a quad strip, except the top stack */
-
- for( i=0; i<stacks-1; i++ )
+ /* Cover each stack with a triangle strip */
+ for( i=0; i<stacks; i++ )
{
- glBegin(GL_QUAD_STRIP);
+ glBegin(GL_TRIANGLE_STRIP);
for(j=0; j<=slices; j++)
{
glEnd();
}
- /* The top stack is covered with individual triangles */
-
- glBegin(GL_TRIANGLES);
-
- glNormal3f(cost[0]*sinn, sint[0]*sinn, cosn);
-
- for (j=0; j<slices; j++)
- {
- glVertex3f(cost[j+0]*r0, sint[j+0]*r0, z0 );
- glVertex3f(0, 0, (GLfloat)height);
- glNormal3f(cost[j+1]*sinn, sint[j+1]*sinn, cosn );
- glVertex3f(cost[j+1]*r0, sint[j+1]*r0, z0 );
- }
-
- glEnd();
-
/* Release sin and cos tables */
free(sint);
if (i==stacks)
z1 = (GLfloat)height;
- glBegin(GL_QUAD_STRIP);
+ glBegin(GL_TRIANGLE_STRIP);
for (j=0; j<=slices; j++ )
{
glNormal3f(cost[j], sint[j], 0 );