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

标签 c++ macos opengl glfw glew

我正在尝试学习 C++ 中的 OpenGL 编程,使用 This tutorial .我正在使用 macbook pro,运行 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();    
}

代码已编译并运行(从终端),出现一个没有三角形的黑色窗口,但显示正确的名称和大小。该窗口运行良好,可以像预期的那样四处移动和关闭,但未绘制三角形。

最佳答案

查看教程,您似乎缺少 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 处没有属性

关于c++ - 无法使用 GLEW 在 openGL 中绘制任何内容(mac、c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31101604/

相关文章:

c++ - 如何将我的程序指向我输入到列表中的值? C++

c++ - 我能否确定从文件中读取的 32 字节二进制数据等于 256 位?

macos - 使用 echo 修改 Hosts 文件

macos - 如何在文件 > 新建/Cmd+N 上显示应用程序模式 "New Document"窗口?

visual-studio - 如何在 OpenGL 中渲染阿拉伯字符?

c++ - 如何在模板函数中使用 typedef?

c++ - 如何将 std::map 与 boost::phoenix 一起使用?

c++ - CMake 测试在 MacOS 上找不到库

c++ - 为什么我必须除以 Z?

c++ - 被渲染的 OpenGL 顶点被剪裁