首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用opengl和SOIL2从纹理中抽取出一个黑色模型。

利用opengl和SOIL2从纹理中抽取出一个黑色模型。
EN

Stack Overflow用户
提问于 2020-07-14 15:55:57
回答 1查看 70关注 0票数 0

我试图纹理一个模型,但我得到的只是模型完全用黑色渲染。我使用SOIL2库将图像加载到内存中,下面的代码显示了纹理类中的加载函数

代码语言:javascript
复制
bool Texture::Load(std::string texturePath) 
{
    int channels = 0; 
    
    unsigned char* image = SOIL_load_image(texturePath.c_str(), &mWidth, &mHeight, &channels, SOIL_LOAD_AUTO); 
    
    if (image == nullptr) 
    {
        std::cout << "Failed to load image " << texturePath; 
        return false; 
    }

    int format = GL_RGB;
    if (channels == 4) 
    {
        format = GL_RGBA; 
    }


    glGenTextures(1, &mTextureID); 
    glBindTexture(GL_TEXTURE_2D, mTextureID);


    glGenerateMipmap(GL_TEXTURE_2D); 
    glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, format, mWidth, mHeight, 0, format, GL_UNSIGNED_BYTE, image); 

    // Free data
    SOIL_free_image_data(image);
    
    return true; 
}

当我尝试调试代码时,我发现图像指针指向一个空数组,作为下面的图像,我不知道这是否是问题所在,但我发现它很奇怪,而且我很确定该图像已经成功加载,因为mWidthmHeight有正确的值。

我的顶点着色器

代码语言:javascript
复制
#version 330 core

layout(location=0) in vec3 position ;
layout(location=1) in vec2 UVCoord ;
layout(location=2) in vec3 normal ;

uniform mat4 uWorldTransform ; 
uniform mat4 uView ; 
uniform mat4 uProjection ; 

out vec2 textCoord ; 

void main()
{
    gl_Position = uProjection * uView * uWorldTransform * vec4(position, 1.0) ; 
    textCoord = UVCoord ; 
}

和我的片段着色器

代码语言:javascript
复制
#version 330 core

in vec2 textCoord ;

out vec4 color ;

uniform sampler2D myTexture ; 

void main()
{
    color = texture(myTexture , textCoord) ; 
}

我的呈现代码

代码语言:javascript
复制
void Renderer::Draw() 
{
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST); 
    std::vector<Actor*> actors = mGame->GetActors(); 
    for (auto act : actors) 
    {
        if (act->GetDrawable()) 
        {
            glm::mat4 worldTransform = act->GetWorldTransform();
            Shader* shader = mShaders[act->GetMesh()->GetShaderName()]; 
            VertexArrayObject* vao = act->GetMesh()->GetVAO();
            Texture* text = act->GetTexture(); 

            shader->Bind();
            shader->SetMatrix4Uniform("uWorldTransform", worldTransform);
            shader->SetMatrix4Uniform("uView", mView);
            shader->SetMatrix4Uniform("uProjection", mProjection);

            if (text) 
            {
                text->Bind(); 
            }

            vao->Bind();
            glDrawElements(GL_TRIANGLES, vao->GetEBOsize(), GL_UNSIGNED_INT, nullptr);
        }
    }

    glfwSwapBuffers(mGame->GetWindow());
}

这是我在屏幕上看到的结果:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-14 16:21:43

在图像上传之后调用glGenerateMipmap() (glTexImage2D()),否则它不会做任何有用的事情,因为纹理中没有任何图像数据需要生成mipmap。

或通过将GL_TEXTURE_MIN_FILTER设置为GL_LINEAR/GL_NEAREST来禁用mip采样。

此外,要注意GL_RGB &默认的4GL_UNPACK_ALIGNMENT1是你通常想要的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62899128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档