cfpperche
4/7/2015 - 2:51 PM

gistfile1.h

- (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);
        
    }
}