Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenGL未正确加载纹理数据_C++_Opengl_Textures_Stb Image - Fatal编程技术网

C++ OpenGL未正确加载纹理数据

C++ OpenGL未正确加载纹理数据,c++,opengl,textures,stb-image,C++,Opengl,Textures,Stb Image,我一直在尝试在没有给定纹理的情况下为材质加载默认纹理。由于我使用的纹理加载器存在一些问题,我决定手动填充图像数据数组,然后将其传递给要绑定的OpenGL函数。不幸的是,这导致我的模型是黑色的,这意味着纹理没有正确加载 下面显示了两个不同的构造函数。第一个从文件加载纹理(并工作),而第二个用255字符(应该是白色)填充字符缓冲区。InitTexture()函数执行所有OpenGL函数来绑定纹理,并可以很好地用于加载的纹理,但不能用于手动缓冲区 我已经检查以确保数据是正确的,甚至将其与加载的纹理进行

我一直在尝试在没有给定纹理的情况下为材质加载默认纹理。由于我使用的纹理加载器存在一些问题,我决定手动填充图像数据数组,然后将其传递给要绑定的OpenGL函数。不幸的是,这导致我的模型是黑色的,这意味着纹理没有正确加载

下面显示了两个不同的构造函数。第一个从文件加载纹理(并工作),而第二个用255字符(应该是白色)填充字符缓冲区。InitTexture()函数执行所有OpenGL函数来绑定纹理,并可以很好地用于加载的纹理,但不能用于手动缓冲区

我已经检查以确保数据是正确的,甚至将其与加载的纹理进行比较,该纹理只是一个白色图像,并且值相同

构造函数被设计用于静态变量的初始化。不确定这是否会影响OpenGL的任何功能

// Texture loaded Constructor - Works Fine
Texture::Texture(const std::string& filename)
{
    // Load texture from file
    int width, height, numComponents;
    unsigned char* imageData = stbi_load(filename.c_str(), &width, &height, &numComponents, 4);

    if (imageData == NULL)
        std::cerr << "Error: Texture loading failed: " << filename << std::endl;

    InitTexture(imageData, width, height);

    stbi_image_free(imageData);
}

// Manual Texture Constructor - should be white but isn't
// A 4x4 image should have 16 pixels and each pixel has 4 values (RGBA) 
// meaning 64 should be the correct size for the buffer??
Texture::Texture()
{
    int height = 4;
    int width = 4;
    unsigned char* data = new unsigned char[64];

    for (int i = 0; i < 64; ++i)
        data[i] = 255;

    InitTexture(data, width, height);

    delete[] data;
}

// This is the initialization of the DefaultDiffuse texture - this is the 
// static varible in the Material class
Texture Material::defaultDiffuse = Texture();
编辑2:


在做了一些测试之后,我发现如果不是在构造函数中调用InitTexture(),而是在一个静态函数中调用它,我必须在main方法的开头调用它,出于某种原因,它可以工作。我的猜测是,在初始化静态成员变量时调用OpenGL函数时,缓冲区设置不正确。不幸的是,我对OpenGL了解不够,无法知道它的元素何时被初始化。静态变量是否有可能在OpenGL初始化之前初始化?

InitTexture(…)
您的函数吗?如果是这样,请发布其代码。。。我的意思是,问题一定在那里,对吗?我刚刚更新了OP以包含InitTexture()函数供您查看。尝试对
GL\u TEXTURE\u MIN\u FILTER
GL\u TEXTURE\u MAG\u FILTER
都使用
InitTexture(…)
您的函数是
InitTexture(…)
吗?如果是这样,请发布其代码。。。我的意思是问题一定在那里,对吗?我刚刚更新了OP,使其包含InitTexture()函数供您查看。请尝试使用
glTexParameteri
GL\u TEXTURE\u MIN\u FILTER
GL\u TEXTURE\u MAG\u FILTER
void Texture::InitTexture(unsigned char* data, const int& width, const int& height, bool mipmap)
{
    // Create handle to texture
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    // Sets texture wrap mode
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    // Handles sampling the texture
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    // Generates the texture
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
}