簡體   English   中英

紋理映射導致紋理的純色為1像素

[英]Texture mapping results in solid color of 1 pixel from texture

事先,對於很多已發布的代碼,我們深表歉意。 我將嘗試變得盡可能簡單。

我的紋理是bmp格式的4x4紋理圖像(如果有人對此感興趣-> 4x4紋理圖像 )。 作為映射的結果,我得到了左上像素的顏色(0.0,1.0)(我檢查了一下,它始終是左上像素的顏色),而我的三角形顏色得到的是該像素的顏色,即白色。 在此處輸入圖片說明 我嘗試在GL_TEXTURE_MAG_FILTER GL_NEAREST更改為GL_LINEAR

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

我得到右下像素(1.0,0.0),這是某種綠色: 在此處輸入圖片說明 我正在使用stb_image加載紋理。 所以在我的Texture.cpp中

Texture::Texture(const std::string& fileName)
{
    int width, height, numComponents;
    unsigned char* imageData = stbi_load(fileName.c_str(), &width, &height, &numComponents, 4);
    if (imageData == NULL)
    {
        std::cerr << "Error: Texture load failed for texture: " << fileName << std::endl;
    }

    glEnable(GL_TEXTURE_2D);
    glGenTextures(1, &_texture);

    glBindTexture(GL_TEXTURE_2D, _texture);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);

    stbi_image_free(imageData);
}
Texture::~Texture(void)
{
    glDeleteTextures(1, &_texture);
}

void Texture::Bind(unsigned int unit)
{
    assert(unit >= 0 && unit <= 31);
    glActiveTexture(GL_TEXTURE0 + unit);
    glBindTexture(GL_TEXTURE_2D, _texture);
}

在我的頂點着色器中

#version 150

in vec3 position;
in vec2 texCoord;

out vec2 texCoord0;

void main(void)
{
    texCoord0 = texCoord;
    gl_Position = vec4(position, 1.0);
}

在我的片段着色器中

#version 150

in vec2 textCoord0;
uniform sampler2D texture;

void main(void)
{
    gl_FragColor = texture2D(texture, textCoord0);
}

在我的main.cpp中

#include <iostream>
#include <GL/glew.h>
#include "Display.h"
#include "Shader.h"
#include "Mesh.h"
#include "Texture.h"
int main(int argc, char** argv)
{
    Display display(800, 600, " ");
    display.Clear(0.0f, 0.15f, 0.3f, 1.0f);
    Vertex vertices[] = {
                            Vertex(glm::vec3(-1.0f, -1.0f, 0.0f), glm::vec2(0.0f, 0.0f)),
                            Vertex(glm::vec3( 0.0f,  1.0f, 0.0f), glm::vec2(0.5f, 1.0f)),
                            Vertex(glm::vec3( 1.0f, -1.0f, 0.0f), glm::vec2(1.0f, 0.0f))
    };
    Texture texture("./res/a.bmp");
    Shader shader("./res/testShader");
    Mesh mesh(vertices, sizeof(vertices) / sizeof(vertices[0]));
    while (display.IsClosed() != true)
    {
        shader.Bind();
        texture.Bind(0);
        mesh.Draw();
        display.Update();
    }
    return 0;
} 

Mesh.cpp中,我將頂點屬性(glm :: vec3 _position和glm :: vec2 _texture)拆分為2個stl向量,並使用2個緩沖區(“ 0”-用於頂點位置,“ 1”-用於紋理):

Mesh::Mesh(Vertex* vertices, unsigned int numVertices, GLenum usage)
{
    _drawCount = numVertices;

    glGenVertexArrays(1, &_vertexArrayObject);

    glBindVertexArray(_vertexArrayObject);

    std::vector<glm::vec3> positions;
    std::vector<glm::vec2> texCoords;

    positions.reserve(numVertices);
    texCoords.reserve(numVertices);

    for (unsigned int i = 0; i < numVertices; ++i)
    {
        positions.push_back(*vertices[i].Position());
        texCoords.push_back(*vertices[i].TexCoord());
    }
    glGenBuffers(NUM_BUFFERS, _vertexArrayBuffers);


    glBindBuffer(GL_ARRAY_BUFFER, _vertexArrayBuffers[POSITION_VB]);
    glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(positions[0]), positions.data(), usage);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);


    glBindBuffer(GL_ARRAY_BUFFER, _vertexArrayBuffers[TEXCOORD_VB]);
    glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(texCoords[0]), texCoords.data(), usage);

    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);


    glBindVertexArray(0);
}


Mesh::~Mesh(void)
{
    glDeleteVertexArrays(1, &_vertexArrayObject);
}

void Mesh::Draw(GLenum mode)
{
    glBindVertexArray(_vertexArrayObject);
    glDrawArrays(mode, 0, _drawCount);
    glBindVertexArray(0);
}

Shader.cpp中,我這樣綁定屬性:

glBindAttribLocation(_program, 0, "position");
glBindAttribLocation(_program, 1, "texCoord");

- - - - - - - - - - - - - - 編輯 - - - - - - - - - - - ------------將頂點着色器更改為:

#version 330 core

layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texCoord;

out vec2 TexCoord;

void main()
{
    gl_Position = vec4(position, 1.0f);
    TexCoord = texCoord;
}

和片段着色器可以:

#version 330 core
in vec2 TexCoord;

out vec4 color;

uniform sampler2D ourTexture;

void main()
{
    color = texture(ourTexture, TexCoord);
}

紋理正確映射。

但是我仍然想知道為什么我的第一個解決方案不起作用。

但是我仍然想知道為什么我的第一個解決方案不起作用。

因為你在用

#version 330 core

自版本130不推薦使用 texture2D

着色器編譯器應該對此進行警告或錯誤。 通過glGetShaderInfoLogglGetProgramInfoLog檢查着色器日志。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM