2

I have read some tutorials to write the following code. The only difference is the original tutorials where using SDL instead of GLEW.

I do not understand what is wrong in this code. It compiles but i do not see the triangle. (the tutorial were not using shaders too)

#include <iostream>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>

GLFWwindow* window;

int main(int argc, const char * argv[])
{
    if (!glfwInit())
    {
        return -1;
    }
    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

    window = glfwCreateWindow(640, 480, "Test", NULL, NULL);
    if (window==NULL)
    {
        return -1;
    }
    glfwMakeContextCurrent(window);

    glewExperimental = true;
    if (glewInit() != GLEW_OK)
    {
        return -1;
    }

    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
    glClearColor(0.0f, 1.0f, 1.0f, 1.0f);

    do
    {
        glfwPollEvents();

        float vertices[] = {-0.5, -0.5,   0.0, 0.5,   0.5, -0.5};
        glClear(GL_COLOR_BUFFER_BIT);
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices);
        glEnableVertexAttribArray(0);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glDisableVertexAttribArray(0);

        glfwSwapBuffers(window);
    }
    while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && glfwWindowShouldClose(window) == 0 );

    glfwTerminate();

    return 0;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
Bob5421
  • 7,757
  • 14
  • 81
  • 175
  • see [complete GL+GLSL+VAO/VBO C++ example](https://stackoverflow.com/a/31913542/2521214) for simple shader example ... – Spektre Oct 02 '17 at 06:42

1 Answers1

8

If you're using the fixed-function pipeline, you cannot use generic vertex attributes like glVertexAttribPointer.

NVIDIA's implementation, however, illegally aliases between generic attributes and non-generic ones. This is probably why the initial writer of the tutorial got away with it on their machine.

If you want to write this in a cross-platform way, you have to use glVertexPointer and glEnableClientState:

glVertexPointer(2, GL_FLOAT, 0, vertices);
glEnableClientState(GL_VERTEX_ARRAY);
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Thanks. Is there in OpenGL some "pre-builted" shaders ? – Bob5421 Oct 01 '17 at 18:55
  • @Bob5421: No. That's what the fixed-function pipeline is. Why are you so interested in *not* using shaders? – Nicol Bolas Oct 01 '17 at 19:20
  • Because i want to write a very basic program first. And i want to do modern opengl too – Bob5421 Oct 01 '17 at 19:32
  • 7
    @Bob5421: "Modern OpenGL" means using shaders. That's part of what makes it "modern". Similarly, "modern OpenGL" means using buffer objects to store your vertex data. And other things too. So you can either have "a very basic program" or "modern opengl"; you cannot have both. Though personally, I don't see anything particularly non-"basic" about shaders or buffer objects. – Nicol Bolas Oct 01 '17 at 19:59