user8221752
user8221752

Reputation:

Why core profile is not working?

This code works fine with compatibility profile context version 4.3 but when I switch to core, nothing is drawn, even though all of the GL functions I've used are included in the core profile. Here's the main part of code

    glClearColor(0.f, 0.f, 0.f, 0.f);
    glClearDepth(-1.d);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_GREATER);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glFrontFace(GL_CCW);
    vs= glCreateShader(GL_VERTEX_SHADER);
    fs= glCreateShader(GL_FRAGMENT_SHADER);
    program= glCreateProgram();

    const char* vertex_shader_text=
        "#version 430 core\n"
        "in vec2 vPos;"
        "in vec3 vCol;"
        "out vec3 colour;"
        "void main() {"
        "colour = vCol;"
        "gl_Position = vec4(vPos, 1.0, 1.0);"
        "}";
    const char* fragment_shader_text=
        "#version 430 core\n"
        "in vec3 colour;"
        "out vec4 frag_colour;"
        "out vec4 frag_colour2;"
        "void main() {"
        "frag_colour = vec4(1.0, 1.0, 1.0, 0.0);"
        "frag_colour2 = vec4(colour, 0.0);"
        "}";
    glShaderSource(vs, 1, &vertex_shader_text, NULL);
    glShaderSource(fs, 1, &fragment_shader_text, NULL);
    glCompileShader(vs);
    glCompileShader(fs);
    glAttachShader(program, vs);
    glAttachShader(program, fs);

    glBindAttribLocation(program,0,"vPos");
    glBindAttribLocation(program,1,"vCol");
    glBindFragDataLocation(program,0,"frag_colour2");

    glLinkProgram(program);
    glUseProgram(program);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    float vertices[] =
    {
        -1.f, 1.f, 1.f, 0.f, 0.f,
        3.f-2.f*(float)sqrt(3.d), -1.f, 0.f, 1.f, 0.f,
        1.f, 2.f*(float)sqrt(3.d)-3.f, 0.f, 0.f, 1.f
    };
    GLuint vertex_buffer = 0;
    glGenBuffers(1, &vertex_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*) 0);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*) (sizeof(float) * 2));

A:
    if (GetMessage(&msg, NULL, 0, 0) )
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    else return TRUE;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDrawArrays(GL_TRIANGLES,0,3);
    SwapBuffers(ghDC2);
    goto A;

Upvotes: 0

Views: 178

Answers (1)

genpfault
genpfault

Reputation: 52084

Make sure you create & bind a vertex array object (VAO) before setting up your vertex attributes:

A Vertex Array Object (VAO) is an OpenGL Object that stores all of the state needed to supply vertex data (with one minor exception noted below). It stores the format of the vertex data as well as the Buffer Objects (see below) providing the vertex data arrays. Note that VAOs do not copy, freeze or store the contents of the referenced buffers - if you change any of the data in the buffers referenced by an existing VAO, those changes will be seen by users of the VAO.

VAOs are required in Core contexts but optional in Compatibility.

Upvotes: 1

Related Questions