简体   繁体   中英

Can't draw anything in openGL using GLEW (mac,c++)

I am trying to learn OpenGL programming in c++, using This tutorial . i am using a macbook pro, running OSX-Yosemite version 10.10.3. My openGL version is 3.3.

I am using the libraries GLFW, and GLEW.

I am compiling (and linking) my code from my terminal using this command: g++ triangleTest.cpp -o triangleTest -lglfw3 -lglew -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo (i am not writing in Xcode).

The goal of this program is to make a white triangle on a black background, the code looks like this:

// Include standard headers
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
#include <GL/glew.h>
#include<GLFW/glfw3.h>
GLFWwindow* window;

// An array of 3 vectors which represents 3 vertices
static const GLfloat g_vertex_buffer_data[] = {
   -1.0f, -1.0f, 0.0f,
   1.0f, -1.0f, 0.0f,
   0.0f,  1.0f, 1.0f,
};

int main(){

    // Initialise GLFW
    if( !glfwInit() )
    {
        fprintf( stderr, "Failed to initialize GLFW\n" );
        return -1;
    }

    glfwWindowHint(GLFW_SAMPLES, 4); 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);


    std::cout<<"GLFW3 initialized\n";


    window = glfwCreateWindow(640, 480, "I am a triangle", NULL, NULL);

     if (!window)
     {
         glfwTerminate();
         std::cout<<"Window initialization failed\n";
        return 1;   
     }

    std::cout<<"Window initialized\n";

    glfwMakeContextCurrent(window);

    if (glewInit() != GLEW_OK) {
        std::cout<<"Failed to initialize GLEW try again\n";
        return 1;
    }


    // This will identify our vertex buffer
    GLuint vertexbuffer;
    // Generate 1 buffer, put the resulting identifier in vertexbuffer
    glGenBuffers(1, &vertexbuffer); 
    // The following commands will talk about our 'vertexbuffer' buffer
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    // Give our vertices to OpenGL.
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);






    glfwSwapInterval(1);

     while (!glfwWindowShouldClose(window)){

        // Clear the screen
        glClear( GL_COLOR_BUFFER_BIT );

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

        // Draw the triangle !
        glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle

        glDisableVertexAttribArray(0);

        glfwSwapBuffers(window);
        glfwPollEvents();
     }


    glfwTerminate();    
}

The code is compiled and run (from the terminal), and a black window with no triangle, but the correct name and size apears. The window works perfectly, and can be moved around and closed like it is suposed to, but the triangle is not drawn.

Looking at the tutorial, it seems that you're missing the VAO. Specifically, you should add these lines after you initialize GLEW.

GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);

Then, in your call to VertexAttribPointer, your attribute should be 0, not 1. So the call shoud look like this

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

That makes it match the index of he attribute, since there is no attribute at index 1

Forget that site, very ugly and dark. There are one guy from Reddit who compiled a small list of VERY useful tutorials for beginners. Just go there, choose one and I promise you that for 8 hours per day, in about a week you will have a cube rotating with texture and maybe some light! =) So, give it a try: List of cool Modern Opengl tutorials And this tutorials work on Mac.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM