Draw sphere OpenGL C++

  2dsphere, 3d, c++, opengl

I am trying to draw a Sphere using OpenGL and C++, but I can’t use the glut Sphere function. I am passing the coordinates (x, y, z), the color white (1.0f, 1.0f, 1.0f) and saving sequentially in the vertex array (vertices).

/** Vertex shader. */
const char *vertex_code = "n"
"#version 330 coren"
"layout (location = 0) in vec3 position;n"
"layout (location = 1) in vec3 color;n"
"n"
"out vec3 vColor;n"
"n"
"uniform mat4 model;n"
"uniform mat4 view;n"
"uniform mat4 projection;n"
"n"
"void main()n"
"{n"
"    gl_Position = projection * view * model * vec4(position, 1.0);n"
"    vColor = color;n"
"}{$content}";

/** Fragment shader. */
const char *fragment_code = "n"
"#version 330 coren"
"n"
"in vec3 vColor;n"
"out vec4 FragColor;n"
"n"
"void main()n"
"{n"
"    FragColor = vec4(vColor, 1.0f);n"
"}{$content}";
void display(){

    glClearColor(0.2, 0.3, 0.3, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glUseProgram(program);
    
    // Define view matrix.
    glm::mat4 view = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -3.0f));

    // Retrieve location of tranform variable in shader.
    unsigned int loc = glGetUniformLocation(program, "view");
    // Send matrix to shader.
    glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(view));

    // Define projection matrix.
    glm::mat4 projection = glm::perspective(glm::radians(60.0f), (win_width/(float)win_height), 0.1f, 100.0f);
    
    // Retrieve location of tranform variable in shader.    
    loc = glGetUniformLocation(program, "projection");
    
    // Send matrix to shader.
    glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(projection));
    
    // Pyramid
    glBindVertexArray(VAO2);

    // Define model matrix.
    glm::mat4 S  = glm::scale(glm::mat4(1.0f), glm::vec3(1.0f, 1.0f, 1.0f));
    glm::mat4 Rx = glm::rotate(glm::mat4(1.0f), glm::radians(px_angle), glm::vec3(1.0f,0.0f,0.0f));
    glm::mat4 Ry = glm::rotate(glm::mat4(1.0f), glm::radians(py_angle), glm::vec3(0.0f,1.0f,0.0f));
    glm::mat4 T  = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -1.0f));
    glm::mat4 model = T*Ry*Rx*S;

    // Retrieve location of tranform variable in shader.
    loc = glGetUniformLocation(program, "model");
    
    // Send matrix to shader.
    glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(model));

    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glDrawArrays(GL_POINTS, 0, 10 * 20 * 20);
        
    glutSwapBuffers();
}
void initData(float radius, float stacks, float sectors){

float *vertices;
    int quantidade = 0;

    vertices = (float *) malloc(radius * stacks * sectors * 6 * sizeof(float));

    if(!vertices){
        printf("Erro ao alocar memória!n");
        exit(-1);
    }

    const float pi = 3.14159265359;
    const float deltaPhi = pi/stacks;
    const float deltaTheta = (2 * pi)/sectors;

    for(int i=0; i<=stacks; i++){

        float phi = (-pi / 2.0) + (i * deltaPhi);
        float temp = radius * cos(phi);
        float z = radius * sin(phi);

        for(int j=0; j<=sectors; j++){

            float theta = j * deltaTheta;
            float y = temp * sin(theta);
            float x = temp * cos(theta);

            // normalize
            x = x / radius;
            y = y / radius;
            z = z / radius;

            vertices[quantidade++] = x;
            vertices[quantidade++] = y;
            vertices[quantidade++] = z;
            vertices[quantidade++] = 1.0f;
            vertices[quantidade++] = 1.0f;
            vertices[quantidade++] = 1.0f;
        }
    }

    // Vertex array.
    glGenVertexArrays(1, &VAO2);
    glBindVertexArray(VAO2);

    // Vertex buffer
    glGenBuffers(1, &VBO2);
    glBindBuffer(GL_ARRAY_BUFFER, VBO2);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * radius * stacks * sectors * 6, vertices, GL_STATIC_DRAW);
    
    // Set attributes.
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*)(3*sizeof(float)));
    glEnableVertexAttribArray(1);

    // Unbind Vertex Array Object.
    glBindVertexArray(0);
    
    glEnable(GL_DEPTH_TEST);
int main(int argc, char** argv){

    glutInit(&argc, argv);
    glutInitContextVersion(3, 3);
    glutInitContextProfile(GLUT_CORE_PROFILE);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowSize(win_width,win_height);
    glutCreateWindow(argv[0]);
    glewExperimental = GL_TRUE;
    glewInit();

    // Init vertex data for the sphere
    initData(10.0, 20.0, 20.0);

    // Create shaders.
    initShaders();
    
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    //glutIdleFunc(idle);

    glutMainLoop();
}

And the final result is this:

https://i.imgur.com/b7EwhVG.png

https://i.imgur.com/Piopsgw.png

What am I doing wrong? I am using this link (http://www.songho.ca/opengl/gl_sphere.html) as a reference.

Source: Windows Questions C++

LEAVE A COMMENT