Skip to content

Instantly share code, notes, and snippets.

@davidbitton
Created July 20, 2011 04:12
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidbitton/1094320 to your computer and use it in GitHub Desktop.
Save davidbitton/1094320 to your computer and use it in GitHub Desktop.
sphere vertex and index generation
- (void)setupSphere {
std::vector<vertexStruct> verticesVector;
std::vector<int> indicesVector;
double latitudeBands = 30;
double longitudeBands = 30;
double radius = 2;
for (double latNumber = 0; latNumber <= latitudeBands; latNumber++) {
double theta = latNumber * M_PI / latitudeBands;
double sinTheta = sin(theta);
double cosTheta = cos(theta);
for (double longNumber = 0; longNumber <= longitudeBands; longNumber++) {
double phi = longNumber * 2 * M_PI / longitudeBands;
double sinPhi = sin(phi);
double cosPhi = cos(phi);
vertexStruct vs;
vs.Normal[0] = cosPhi * sinTheta; // x
vs.Normal[1] = cosTheta; // y
vs.Normal[2] = sinPhi * sinTheta; // z
vs.Texcoord[0] = 1 - (longNumber / longitudeBands); // u
vs.Texcoord[1] = 1 - (latNumber / latitudeBands); // v
vs.Vertex[0] = radius * vs.Normal[0];
vs.Vertex[1] = radius * vs.Normal[1];
vs.Vertex[2] = radius * vs.Normal[2];
verticesVector.push_back(vs);
}
for (int latNumber = 0; latNumber < latitudeBands; latNumber++) {
for (int longNumber; longNumber < longitudeBands; longNumber++) {
int first = (latNumber * (longitudeBands + 1)) + longNumber;
int second = first + longitudeBands + 1;
indicesVector.push_back(first);
indicesVector.push_back(second);
indicesVector.push_back(first + 1);
indicesVector.push_back(second);
indicesVector.push_back(second + 1);
indicesVector.push_back(first + 1);
}
}
size_t verticesCount = verticesVector.size();
vertexStruct vertices[verticesCount];
size_t indicesCount = indicesVector.size();
int indices[indicesCount];
copy(verticesVector.begin(), verticesVector.end(), vertices);
copy(indicesVector.begin(), indicesVector.end(), indices);
// compass
glGenVertexArraysOES(1, &sphereVertexArrayObject);
glBindVertexArrayOES(sphereVertexArrayObject);
glGenBuffers(2, &sphereVertexBuffers[0]);
glBindBuffer(GL_ARRAY_BUFFER, sphereVertexBuffers[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(sphereHandles.textureCoordAttribute, 2, GL_FLOAT, GL_FALSE, sizeof(vertexStruct), (GLvoid*)offsetof(vertexStruct, Texcoord));
glEnableVertexAttribArray(sphereHandles.textureCoordAttribute);
glVertexAttribPointer(sphereHandles.vertexNormalAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(vertexStruct), (GLvoid*)offsetof(vertexStruct, Normal));
glEnableVertexAttribArray(sphereHandles.vertexNormalAttribute);
glVertexAttribPointer(sphereHandles.vertexPositionAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(vertexStruct), (GLvoid*)offsetof(vertexStruct, Vertex));
glEnableVertexAttribArray(sphereHandles.vertexPositionAttribute);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sphereVertexBuffers[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}
}
@deep4U
Copy link

deep4U commented Jul 18, 2016

hey ,
It seems I Need your help
I am trying to render video on sphere for which I am able to bring video as texture into opengles code.
Now as I am able to play video on square I am changing my vertex and texturecoordinate buffer for sphere using little modifications to your function.
I am using array so I calculated what shall be size of array and than everything is fine
I get the sphere but problem is with texture coordinates ...
am I doing something wrong ?
please have a look in the function

as below
void setupSphere()
{
int texind=0,vertind=0,indicesind=0;

        //vector<_vertexStruct> verticesVector;
        //vector<int> indicesVector;

double Normal1,Normal2,Normal3;
double latitudeBands = 10;
double longitudeBands = 10;
double radius = 1;
mVertices = new float[((int)latitudeBands+1)((int)longitudeBands+1)(3)]; // NOPMD
mTexture = new float[((int)latitudeBands+1)((int)longitudeBands+1)(2)]; // NOPMD
mIndexes = new short[(int)(6_latitudeBands_longitudeBands*(latitudeBands+1))];
for (double latNumber = 0; latNumber <= latitudeBands; latNumber++) {
double theta = latNumber * Math.PI / latitudeBands;
double sinTheta = sin(theta);
double cosTheta = cos(theta);

            for (double longNumber = 0; longNumber <= longitudeBands; longNumber++) {
                double phi = longNumber * 2 * Math.PI / longitudeBands;
                double sinPhi = sin(phi);
                double cosPhi = cos(phi);

          //      vertexStruct vs;
                Normal1 = cosPhi * sinTheta;   // x
                Normal2 = cosTheta;            // y
                Normal3 = sinPhi * sinTheta;   // z
                mTexture[texind++] =(float)( 1 - (longNumber / longitudeBands)); // u
                mTexture[texind++] = (float)(1 - (latNumber / latitudeBands));   // v
                mVertices[vertind++] =(float)( radius * Normal1);
                mVertices[vertind++] =(float)( radius * Normal2);
                mVertices[vertind++] =(float)( radius * Normal3);

                //verticesVector.push_back(vs);
            }

            for (int latNumber1 = 0; latNumber1 < latitudeBands; latNumber1++) {
                for (int longNumber=0; longNumber < longitudeBands; longNumber++) {
                    int first = (latNumber1 * ((int)longitudeBands + 1)) + longNumber;
                    int second = first + (int)longitudeBands + 1;

                    //indicesVector.push_back(first);
                    mIndexes[indicesind++]=(short)first;
                    //indicesVector.push_back(second);
                    mIndexes[indicesind++]=(short)second;
                    //indicesVector.push_back(first + 1);
                    mIndexes[indicesind++]=(short)(first+1);

                    //indicesVector.push_back(second);
                    mIndexes[indicesind++]=(short)second;
                    //indicesVector.push_back(second + 1);
                    mIndexes[indicesind++]=(short)(second+1);
                    //indicesVector.push_back(first + 1);
                    mIndexes[indicesind++]=(short)(first+1);

                }
            }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment