Created
July 20, 2011 04:12
-
-
Save davidbitton/1094320 to your computer and use it in GitHub Desktop.
sphere vertex and index generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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;
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);