glTexImage3D抛出异常

glTexImage3D throws exception

本文关键字:抛出异常 glTexImage3D      更新时间:2023-10-16

我正在尝试在VS13中编译此应用程序。我用这种方式连接了所有的图书馆:glew32。libReleaseWin32将glew32.dll放在与Debug相同的文件夹中有关glfw3

当我运行这段代码,它抛出一个异常在glTexImage3D;它说"访问违反执行位置0x00000000。"

    #include <GL/glew.h>
#include <GL/glfw3.h>
#include <cstdlib>
#include <iostream>
GLenum volumeTexture;
int main() {
    // Initialize GLFW
    if (!glfwInit()) {
        std::cerr << "Failed to initialize GLFW! I'm out!" << std::endl;
        exit(-1);
    }
    glewExperimental = GL_TRUE;
    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
        /* Problem: glewInit failed, something is seriously wrong. */
        fprintf(stderr, "Error: %sn", glewGetErrorString(err));
    }
    // Use red to clear the screen
    glClearColor(1, 0, 0, 1);
    glGenTextures(1, &volumeTexture);
    glBindTexture(GL_TEXTURE_3D, volumeTexture);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
    glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 256, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

我注意到,如果我添加

,错误就会解决
glTexImage3D = (PFNGLTEXIMAGE3DPROC) wglGetProcAddress("glTexImage3D");

,但后来我得到同样的异常运行glGenFramebuffers(我得到它,即使我评论glTexImage3D行)我是不是把什么东西连接错了?

你的顺序不对。

glewInit()需要一个当前的GL上下文来完成它的事情。如果没有当前上下文,它就不能查询GL的入口点,并将其所有函数指针(如glTexImage3D())设置为NULL。

glfwInit() 不创建GL上下文,也不创建当前的GL上下文。

你需要glfwCreateWindow()glfwMakeContextCurrent()