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
    Senior Member Regular Contributor Agent D's Avatar
    Join Date
    Sep 2011
    Location
    Innsbruck, Austria
    Posts
    281
    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
    Member Newbie
    Join Date
    Sep 2013
    Posts
    31
    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).

Similar Threads

  1. not drawing the triangle
    By WhiteSword in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 06-17-2017, 02:42 AM
  2. Drawing Triangle, help please!
    By walshy1112 in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 12-25-2015, 07:37 PM
  3. Help with drawing a triangle in 3.2 GLSL 1.4
    By jmfairlie in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 04-22-2010, 04:47 PM
  4. drawing 3d triangle mesh
    By badass in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 04-20-2010, 09:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean