c++ - OpenGL glGetIntegerv GL_MAX_VERTEX_ATTRIBS 分割错误

标签 c++ opengl

我正在学习 OpenGL 并遇到了这个问题:

源代码:

#include <iostream>
#include <glad/glad.h>

int nrAttributes;

int main()
{
    glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nrAttributes);

    std::cout << "max vertex: " << nrAttributes << std::endl;

    return 0;
}

我用这个命令编译我的代码:

g++ main.cpp --std=c++11 -o main.o src/glad.c -I include -lglfw -lGL -lX11 -lpthread -lXrandr -lXi -ldl

我在 linux mint 18.04 x64 上

GL 信息:glxinfo | grep OpenGL

OpenGL vendor string: X.Org
OpenGL renderer string: AMD TURKS (DRM 2.50.0 / 4.13.0-38-generic, LLVM 5.0.0)
OpenGL core profile version string: 3.3 (Core Profile) Mesa 17.2.8
OpenGL core profile shading language version string: 3.30
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile
OpenGL core profile extensions:
OpenGL version string: 3.0 Mesa 17.2.8
OpenGL shading language version string: 1.30
OpenGL context flags: (none)
OpenGL extensions:
OpenGL ES profile version string: OpenGL ES 3.0 Mesa 17.2.8
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.00
OpenGL ES profile extensions:

当我运行代码时,我得到以下输出:

Segmentation fault (core dumped)

谁能解释一下为什么会这样?

最佳答案

正如@Rabbid76 指出的那样,我需要一个有效的上下文来调用 opengl 函数。

这是工作代码,希望它能帮助其他人。

#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>

int main()
{
    glfwInit();
    GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
    glfwMakeContextCurrent(window);
    gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);

    int nrAttributes;
    glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nrAttributes);
    std::cout << "Maximum nr of vertex attributes supported: " << nrAttributes << std::endl;

    return 0;
}

关于c++ - OpenGL glGetIntegerv GL_MAX_VERTEX_ATTRIBS 分割错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49968476/

相关文章:

c++ - 使用 vector 时出现段错误

c++ - 将多态参数限制为特定子集?

opengl - 你如何在 OpenGL 中获得模型 View 和投影矩阵?

c++ - 使用 openGL 绘制 OBJ 文件

c - 如何使用 OpenGL 3.x VBO 渲染动态世界?

c++ - 使用矩阵反转顶点缠绕顺序

c++ - 设置不对称截锥体

c++ - "char *"和 "String"函数的返回类型是否相同?

c++ - 在boost::posix_time中,如何从volatile time_duration构造time_duration?

c# - EndDraw() 在 Direct2D 中占用 80% 的工作时间