无法使用 GLEW 在 openGL 中绘制任何内容 (mac,c++)

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

我正在尝试使用 This tutorial 学习 C++ 中的 OpenGL 编程。我使用的是 macbook pro,运行ning OSX-Yosemite 版本 10.10.3。我的 openGL 版本是 3.3.

我正在使用库 GLFW 和 GLEW。

我正在使用以下命令从我的终端编译(和链接)我的代码:g++ triangleTest.cpp -o triangleTest -lglfw3 -lglew -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo(我不是在 Xcode 中编写)。

这个程序的目标是在黑色背景上做一个白色三角形,代码如下:

// 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();    
}

代码已编译 运行(来自终端),黑色 window 没有三角形,但显示正确的名称和大小。 window 完美运行,可以像预期的那样四处移动和关闭,但未绘制三角形。

查看教程,您似乎缺少 VAO。具体来说, 您应该在初始化 GLEW 后添加这些行。

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

然后,在你调用 VertexAttribPointer 时,你的属性应该是 0,而不是 1。所以调用应该是这样的

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
);

这使得它匹配属性的索引,因为索引 1 没有属性

忘掉那个网站,非常丑陋和黑暗。 Reddit 上有一个人为初学者编制了一小部分非常有用的教程。只要去那里,选择一个,我向你保证,每天 8 小时,大约一周后,你将拥有一个旋转的立方体,带有纹理,也许还有一些光! =) 所以试试吧: List of cool Modern Opengl tutorials this tutorials 在 Mac 上工作。