Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Problems drawing a triangle

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    6

    Problems drawing a triangle

    Hi. I'm trying to draw a triangle on the screen but the following code is not working.
    Code :
    #include <glew.h>
    #include <glfw3.h>
    #include <stdexcept>
     
    int main(int argc, char** argv)
    {
        if(!glfwInit())
        {
            throw std::runtime_error("GLFW could not initialize.");
        }
     
        GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGL / GLFW", NULL, NULL);
     
        if(!window)
        {
            throw std::runtime_error("GLFWwindow could not be created.");
        }
     
        glewInit();
     
        glfwMakeContextCurrent(window);
     
        const float vertices[] =
        {
            0.0f, 0.5f, 0.0f,
            0.5f, -0.5f, 0.0f,
            -0.5f, -0.5f, 0.0f
        };
     
        GLuint vbo;
        glGenBuffers(1, &vbo);
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
     
        GLuint vao;
        glGenVertexArrays(1, &vao);
        glBindVertexArray(vao);
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
     
        while(!glfwWindowShouldClose(window))
        {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
            glDrawArrays(GL_TRIANGLES, 0, 3);
     
            glfwPollEvents();
            glfwSwapBuffers(window);
        }
     
        glfwTerminate();
    }

  2. #2
    Junior Member Regular Contributor Agent D's Avatar
    Join Date
    Sep 2011
    Location
    Innsbruck, Austria
    Posts
    121
    What OpenGL(R) version and profile are you using? I don't see any shader loading and binding in your code at all.

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    6
    It's been just a couple of days since I've started to learn OpenGL so I'm not using yet shaders.
    I'm using OpenGL 4.3.

  4. #4
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    6
    I finally made it. I put
    Code :
    glewExperimental = GL_TRUE
    glewInit()

    before the GL functions.

  5. #5
    Junior Member Newbie
    Join Date
    Sep 2012
    Posts
    28
    Hmm... How did it work if you didn't use shaders with OpenGL 4.3? glEnableVertexAttribArray(0) without shaders won't do anything (in both, core and compatibility profile).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •