P.Carlino
P.Carlino

Reputation: 681

OpenGl cube isn't showed

hi guys i'm new of opengl's world so it is like a week that i have tried to understand how does opengl work. So i put down my code using different examples and i compiled it without problems. But the cube doesn't appear and i don't know why. Can anyone explain to me my mistakes? Here there is the code:

#define GL_GLEXT_PROTOTYPES

#include <GL/glew.h>
#include <iostream>
#include <GL/gl.h>
#include <GL/freeglut.h>



using namespace std;

void display(void);

int main(int argc, char ** argv)
{

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowSize(512, 512);

    glutCreateWindow(argv[0]);


    glutInitContextVersion(4, 5);
    glutInitContextProfile(GLUT_CORE_PROFILE);
    cout<< glGetString(GL_VERSION)<< endl;
    if (glewInit()) {
        cerr << "Unable to initialize GLEW ... exiting" << endl;
        exit(EXIT_FAILURE);
    }   


    GLuint i;
    GLuint vertex[1];

    glGenVertexArrays(3*12,vertex);
    glBindVertexArray(vertex[0]);
    static const GLfloat fl[] = {
    0.583f,  0.771f,  0.014f,
    0.609f,  0.115f,  0.436f,
    0.327f,  0.483f,  0.844f,
    0.822f,  0.569f,  0.201f,
    0.435f,  0.602f,  0.223f,
    0.310f,  0.747f,  0.185f,
    0.597f,  0.770f,  0.761f,
    0.559f,  0.436f,  0.730f,
    0.359f,  0.583f,  0.152f,
    0.483f,  0.596f,  0.789f,
    0.559f,  0.861f,  0.639f,
    0.195f,  0.548f,  0.859f,
    0.014f,  0.184f,  0.576f,
    0.771f,  0.328f,  0.970f,
    0.406f,  0.615f,  0.116f,
    0.676f,  0.977f,  0.133f,
    0.971f,  0.572f,  0.833f,
    0.140f,  0.616f,  0.489f,
    0.997f,  0.513f,  0.064f,
    0.945f,  0.719f,  0.592f,
    0.543f,  0.021f,  0.978f,
    0.279f,  0.317f,  0.505f,
    0.167f,  0.620f,  0.077f,
    0.347f,  0.857f,  0.137f,
    0.055f,  0.953f,  0.042f,
    0.714f,  0.505f,  0.345f,
    0.783f,  0.290f,  0.734f,
    0.722f,  0.645f,  0.174f,
    0.302f,  0.455f,  0.848f,
    0.225f,  0.587f,  0.040f,
    0.517f,  0.713f,  0.338f,
    0.053f,  0.959f,  0.120f,
    0.393f,  0.621f,  0.362f,
    0.673f,  0.211f,  0.457f,
    0.820f,  0.883f,  0.371f,
    0.982f,  0.099f,  0.879f
};

    glGenBuffers(1,&i);
    glBindBuffer(GL_ARRAY_BUFFER,i);
    glBufferData(GL_ARRAY_BUFFER,sizeof(fl),fl,GL_STATIC_DRAW);
    glEnableVertexAttribArray(1);

    const GLchar* vvs="#version 450\n"
    "in vec3 vp;\n"
    "void main() {\n"
    "  gl_Position = vec4(vp, 1.0);\n"
    "}\n";
    const GLchar* gg=
    "#version 450 core"
    "out vec4 fColor;"
    "void"
    "main()"
    "{"
    "fColor = vec4(0.0, 0.0, 1.0, 1.0);"
    "}";

    GLint sh= glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(sh,1,&vvs,NULL);
    glShaderSource(sh,1,&gg,NULL);
    glCompileShader(sh);

    GLint program=glCreateProgram();
    glAttachShader(program,sh);
    glLinkProgram(program);

        glUseProgram(program);
glVertexAttribPointer(
    1,                                // attribute. No particular reason for 1, but must match the layout in the shader.
    3,                                // size
    GL_FLOAT,                         // type
    GL_FALSE,                         // normalized?
    0,                                // stride
    (void*)0                          // array buffer offset
);

    glDisableVertexAttribArray(1);
    glutDisplayFunc(display);
    glutMainLoop();
}


void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);



    glDrawArrays(GL_TRIANGLES,0,12*3);
    glutSwapBuffers();
    glFlush();
}

Upvotes: 0

Views: 83

Answers (3)

Zebrafish
Zebrafish

Reputation: 14356

After Glew init add this code:

GLuint VBO;
GLuint VAO;

glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);

static const GLfloat fl[] = {
    0.583f,  0.771f,  0.014f,
    0.609f,  0.115f,  0.436f,
    0.327f,  0.483f,  0.844f,
    0.822f,  0.569f,  0.201f,
    0.435f,  0.602f,  0.223f,
    0.310f,  0.747f,  0.185f,
    0.597f,  0.770f,  0.761f,
    0.559f,  0.436f,  0.730f,
    0.359f,  0.583f,  0.152f,
    0.483f,  0.596f,  0.789f,
    0.559f,  0.861f,  0.639f,
    0.195f,  0.548f,  0.859f,
    0.014f,  0.184f,  0.576f,
    0.771f,  0.328f,  0.970f,
    0.406f,  0.615f,  0.116f,
    0.676f,  0.977f,  0.133f,
    0.971f,  0.572f,  0.833f,
    0.140f,  0.616f,  0.489f,
    0.997f,  0.513f,  0.064f,
    0.945f,  0.719f,  0.592f,
    0.543f,  0.021f,  0.978f,
    0.279f,  0.317f,  0.505f,
    0.167f,  0.620f,  0.077f,
    0.347f,  0.857f,  0.137f,
    0.055f,  0.953f,  0.042f,
    0.714f,  0.505f,  0.345f,
    0.783f,  0.290f,  0.734f,
    0.722f,  0.645f,  0.174f,
    0.302f,  0.455f,  0.848f,
    0.225f,  0.587f,  0.040f,
    0.517f,  0.713f,  0.338f,
    0.053f,  0.959f,  0.120f,
    0.393f,  0.621f,  0.362f,
    0.673f,  0.211f,  0.457f,
    0.820f,  0.883f,  0.371f,
    0.982f,  0.099f,  0.879f
};

glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(fl), fl, GL_STATIC_DRAW);

const GLchar* vertShaderSource = "#version 450 core\n"
    "in vec3 vp;\n"
    "void main() {\n"
    "  gl_Position = vec4(vp, 1.0);\n"
    "}\n";

const GLchar* fragShaderSource =
    "#version 450 core\n"
    "out vec4 fColor;\n"
    "void main()"
    "{"
    "fColor = vec4(0.0, 0.0, 1.0, 1.0);"
    "}";

GLint vertShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertShader, 1, &vertShaderSource, NULL);
glCompileShader(vertShader);

GLint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragShader, 1, &fragShaderSource, NULL);
glCompileShader(fragShader);

GLint program = glCreateProgram();
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
glLinkProgram(program);

glUseProgram(program);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);

Then draw.

The code you had had problems everywhere, you just copied and pasted it, didn't you? Here's what I get before and after adding negative to some x values. I don't think it's a cube, I think it's random points.

enter image description here

Upvotes: 1

BDL
BDL

Reputation: 22174

In addition to the other two answers:

  • You'll have to add linebreaks to the fragment shader code. And you should check whether they compiled and linked successfully.
  • You assume that the vp attribute gets location 1 assigned, but thats not guaranteed. Either you add a layout qualifier to the shader or you query the attribute location.

Upvotes: 1

david
david

Reputation: 292

From the looks of it, you're overwriting the vertex shader code with the fragment string. You need two glCreateShader calls with GL_VERTEX_SHADER and GL_FRAGMENT_SHADER respectively, along with another glAttachShader (to the same program).

Upvotes: 1

Related Questions