1

作为一个我已经有一段时间的辅助项目,我一直在尝试创建一些体素地形。然而,令我沮丧的是,纹理似乎无法正常工作。我正在使用 GL_REPEAT 和 GL_NEAREST 映射纹理。纹理位于精灵表中,大小为 16x16。我目前正在使用 minecraft 纹理进行调试。我尝试通过移动纹理 1 texel 来修复它,但这也不起作用。

在此处输入图像描述

这是处理它的代码:

void Chunk::CreateCube(int x, int y, int z, bool activeStates[], int ID)
{

        double TEXTURE_SIZE = 256;
        glm::vec3 p1(x-BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE); //left bottom  front
        glm::vec2 t1(0.5/TEXTURE_SIZE, 0.5/TEXTURE_SIZE);
        glm::vec3 p2(x+BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE); //right bottom front
        glm::vec2 t2(1-(0.5/TEXTURE_SIZE), 0.5/TEXTURE_SIZE);
        glm::vec3 p3(x+BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE); // right top front
        glm::vec2 t3(1-(0.5/TEXTURE_SIZE), 1-(0.5/TEXTURE_SIZE));
        glm::vec3 p4(x-BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z+BLOCK_RENDER_SIZE); // left top front
        glm::vec2 t4(0.5/TEXTURE_SIZE, 1-(0.5/TEXTURE_SIZE));
        glm::vec3 p5(x+BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE); // right bottom back
        glm::vec2 t5(0.5/TEXTURE_SIZE, 0.5/TEXTURE_SIZE);
        glm::vec3 p6(x-BLOCK_RENDER_SIZE, y-BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE); // left bottom back
        glm::vec2 t6(1-(0.5/TEXTURE_SIZE), 0.5/TEXTURE_SIZE);
        glm::vec3 p7(x-BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE); // left top back
        glm::vec2 t7(1-(0.5/TEXTURE_SIZE), 1-(0.5/TEXTURE_SIZE));
        glm::vec3 p8(x+BLOCK_RENDER_SIZE, y+BLOCK_RENDER_SIZE, z-BLOCK_RENDER_SIZE); // right top back
        glm::vec2 t8(0.5/TEXTURE_SIZE, 1-(0.5/TEXTURE_SIZE));

        int numCols = 16;
        int numRows = 16;

        double u = ((double)(ID % numCols) / (double)numCols);
        double v = ((double)(ID / numRows) / (double)numRows);
        double TILE_TEXTURE_SIZE = 16;

        glm::vec3 n1;


        if(activeStates[5] == false)
        {
                // Front Face Normal
                n1 = glm::vec3(0.0f, 0.0f, 1.0f);
                //Triangle 1
                vertexData.push_back(p1);
                uvData.push_back(glm::vec2(t1.x/TILE_TEXTURE_SIZE + u, t1.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p2);
                uvData.push_back(glm::vec2(t2.x/TILE_TEXTURE_SIZE + u, t2.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p3);
                uvData.push_back(glm::vec2(t3.x/TILE_TEXTURE_SIZE + u, t3.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);
                //Triangle 2
                vertexData.push_back(p1);
                uvData.push_back(glm::vec2(t1.x/TILE_TEXTURE_SIZE + u, t1.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p3);
                uvData.push_back(glm::vec2(t3.x/TILE_TEXTURE_SIZE + u, t3.y/TILE_TEXTURE_SIZE + v ));
                normalData.push_back(n1);

                vertexData.push_back(p4);
                uvData.push_back(glm::vec2(t4.x/TILE_TEXTURE_SIZE + u, t4.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);
        }

        if(activeStates[4] == false)
        {
                // Back
                n1 = glm::vec3(0.0f, 0.0f, -1.0f);
                //Triangle 1
                vertexData.push_back(p5);
                uvData.push_back(glm::vec2(t5.x/TILE_TEXTURE_SIZE + u, t5.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p6);
                uvData.push_back(glm::vec2(t6.x/TILE_TEXTURE_SIZE + u, t6.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p7);
                uvData.push_back(glm::vec2(t7.x/TILE_TEXTURE_SIZE + u, t7.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);
                //Triangle 2
                vertexData.push_back(p5);
                uvData.push_back(glm::vec2(t5.x/TILE_TEXTURE_SIZE + u, t5.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p7);
                uvData.push_back(glm::vec2(t7.x/TILE_TEXTURE_SIZE + u, t7.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p8);
                uvData.push_back(glm::vec2(t8.x/TILE_TEXTURE_SIZE + u, t8.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);
        }
        if(activeStates[1] == false)
        {
                // Right
                n1 = glm::vec3(1.0f, 0.0f, 0.0f);
                //Triangle 1
                vertexData.push_back(p2);
                uvData.push_back(glm::vec2(t2.x/TILE_TEXTURE_SIZE + u, t2.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p5);
                uvData.push_back(glm::vec2(t5.x/TILE_TEXTURE_SIZE + u, t5.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p8);
                uvData.push_back(glm::vec2(t8.x/TILE_TEXTURE_SIZE + u, t8.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);
                //Triangle 2
                vertexData.push_back(p2);
                uvData.push_back(glm::vec2(t2.x/TILE_TEXTURE_SIZE + u, t2.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p8);
                uvData.push_back(glm::vec2(t8.x/TILE_TEXTURE_SIZE + u, t8.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p3);
                uvData.push_back(glm::vec2(t3.x/TILE_TEXTURE_SIZE + u, t3.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);
        }
        if(activeStates[0] == false)
        {
                // left
                n1 = glm::vec3(-1.0f, 0.0f, 0.0f);
                //Triangle 1
                vertexData.push_back(p6);
                uvData.push_back(glm::vec2(t6.x/TILE_TEXTURE_SIZE + u, t6.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p1);
                uvData.push_back(glm::vec2(t1.x/TILE_TEXTURE_SIZE + u, t1.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p4);
                uvData.push_back(glm::vec2(t4.x/TILE_TEXTURE_SIZE + u, t4.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);
                //Triangle 2
                vertexData.push_back(p6);
                uvData.push_back(glm::vec2(t6.x/TILE_TEXTURE_SIZE + u, t6.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p4);
                uvData.push_back(glm::vec2(t4.x/TILE_TEXTURE_SIZE + u, t4.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p7);
                uvData.push_back(glm::vec2(t7.x/TILE_TEXTURE_SIZE + u, t7.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);
        }
        if(activeStates[3] == false)
        {
                // Top
                n1 = glm::vec3(0.0f, 1.0f, 0.0f);
                //Triangle 1
                vertexData.push_back(p4);
                uvData.push_back(glm::vec2(t6.x/TILE_TEXTURE_SIZE + u, t6.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p3);
                uvData.push_back(glm::vec2(t1.x/TILE_TEXTURE_SIZE + u, t1.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p8);
                uvData.push_back(glm::vec2(t4.x/TILE_TEXTURE_SIZE + u, t4.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);
                //Triangle 2
                vertexData.push_back(p4);
                uvData.push_back(glm::vec2(t6.x/TILE_TEXTURE_SIZE + u, t6.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p8);
                uvData.push_back(glm::vec2(t4.x/TILE_TEXTURE_SIZE + u, t4.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p7);
                uvData.push_back(glm::vec2(t7.x/TILE_TEXTURE_SIZE + u, t7.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);
        }

        if(activeStates[2] == false)
        {
                // Bottom
                n1 = glm::vec3(0.0f, -1.0f, 0.0f);

                //Triangle 1
                vertexData.push_back(p6);
                uvData.push_back(glm::vec2(t6.x/TILE_TEXTURE_SIZE + u, t6.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p5);
                uvData.push_back(glm::vec2(t1.x/TILE_TEXTURE_SIZE + u, t1.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p2);
                uvData.push_back(glm::vec2(t4.x/TILE_TEXTURE_SIZE + u, t4.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);
                //Triangle 2
                vertexData.push_back(p6);
                uvData.push_back(glm::vec2(t6.x/TILE_TEXTURE_SIZE + u, t6.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p2);
                uvData.push_back(glm::vec2(t4.x/TILE_TEXTURE_SIZE + u, t4.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);

                vertexData.push_back(p1);
                uvData.push_back(glm::vec2(t7.x/TILE_TEXTURE_SIZE + u, t7.y/TILE_TEXTURE_SIZE + v));
                normalData.push_back(n1);
        }

        /*glm::vec2 t1(0.5/256, 0.5/256);

        glm::vec2 t2(1-(0.5/256), 0.5/256);

        glm::vec2 t3(1-(0.5/256), 1-(0.5/256));

        glm::vec2 t4(0.5/256, 1-(0.5/256));

        glm::vec2 t5(0.5/256, 0.5/256);

        glm::vec2 t6(1-(0.5/256), 0.5/256);

        glm::vec2 t7(1-(0.5/256), 1-(0.5/256));

        glm::vec2 t8(0.5/256, 1-(0.5/256));
        */

        /*
        for(int i = 0; i < vertexData.size(); i+=3)
        {

        // get the three vertices that make the faces
        glm::vec3 p1 = vertexData[i+0];
        glm::vec3 p2 = vertexData[i+1];
        glm::vec3 p3 = vertexData[i+2];

        glm::vec3 v1 = p2 - p1;
        glm::vec3 v2 = p3 - p1;
        glm::vec3 normal = glm::cross( v1,v2 );

        normal = glm::normalize(normal);

        normalData[i+0] = normal;
        normalData[i+1] = normal;
        normalData[i+2] = normal;

        }
        */
}



GLuint Graphics3D::loadTexture(const char* theFileName)
{
        ILuint imageID;                

        GLuint textureID;              

        ILboolean success;

        ILenum error;                          

        ilGenImages(1, &imageID);      

        ilBindImage(imageID);          

        success = ilLoadImage(theFileName);

        if (success)
        {

                ILinfo ImageInfo;
                iluGetImageInfo(&ImageInfo);
                if (ImageInfo.Origin == IL_ORIGIN_UPPER_LEFT)
                {
        //              iluFlipImage();
                }

                success = ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);


                if (!success)
                {
                        error = ilGetError();
                        std::cout << "Image conversion failed - IL reports error: " << error << " - " << iluErrorString(error) << std::endl;
                }


                glGenTextures(1, &textureID);

        glBindTexture(GL_TEXTURE_2D, textureID);


                glTexImage2D(GL_TEXTURE_2D,                             // Type of texture
                        0,                              // Pyramid level (for mip-mapping) - 0 is the top level
                        ilGetInteger(IL_IMAGE_BPP),     // Image colour depth
                        ilGetInteger(IL_IMAGE_WIDTH),   // Image width
                        ilGetInteger(IL_IMAGE_HEIGHT),  // Image height
                        0,                              // Border width in pixels (can either be 1 or 0)
                        ilGetInteger(IL_IMAGE_FORMAT),  // Image format (i.e. RGB, RGBA, BGR etc.)
                        GL_UNSIGNED_BYTE,               // Image data type
                        ilGetData());                   // The actual image data itself
                /*
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
                */

        //      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        //      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

                if(GLEW_EXT_texture_filter_anisotropic)
                {

                        GLfloat maximumAnisotropy;
                        //get the value
                        glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maximumAnisotropy);
                        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maximumAnisotropy);
                }

                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
        //      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        //      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
                glGenerateMipmap(GL_TEXTURE_2D);


        }
        else
        {
                error = ilGetError();
                std::cout << "Image load failed - IL reports error: " << error << " - " << iluErrorString(error) << std::endl;

        }

        ilDeleteImages(1, &imageID);

        std::cout << "Texture creation successful." << std::endl;

        return textureID;

}

似乎离我越远,线条就越明显。所以我尝试删除 mipmap。这也没有帮助。我不知道如何处理这个问题。有任何想法吗?这是纹理: 在此处输入图像描述

4

2 回答 2

0

您的基本问题是您将多个图像平铺成一个大纹理。这将导致任何类型的纹理过滤(例如 mipmapping 和各向异性过滤)出现问题。您不想禁用纹理过滤。相反,您应该将每个图像在逻辑上视为不同的图像。一个简单的方法是让每个图块成为一个单独的纹理。这有点慢,因此为了获得更好的性能,您应该改用Array Texture

于 2013-02-07T19:26:23.923 回答
0

这几乎可以肯定是启用了某种形式的过滤。

我注意到您正在打开各向异性过滤。即使没有 mip-mapping,这也会导致获取子纹理之外的像素,从而导致您在此处看到的伪影。

你真的需要关闭它,以及摆脱 mip-mapping。

另外,我认为您的 UV 生成已关闭,并且包含一些错误。这些可能不是这里的问题,但我建议修复它们。

例如,在这里添加半个纹素:

glm::vec2 t1(0.5/TEXTURE_SIZE, 0.5/TEXTURE_SIZE);

然而,这后来被缩小了,所以实际上你只是抵消了一小部分。无论如何这可能是不必要的(你为什么认为你需要这样做?),但几乎可以肯定它没有做你认为它在做的事情。一个非常小的偏移量可能是一个好主意,以防止任何舍入错误将获取的纹素滑入下一个子纹理,但只有很小的量。

然后你这样做:

double u = ((double)(ID % numCols) / (double)numCols);
double v = ((double)(ID / numRows) / (double)numRows);

您应该对除法和模数使用相同的值ID。它之所以有效,是因为您具有相同数量的行和列。

最后,您可以像这样计算 UV:

t1.x/TILE_TEXTURE_SIZE + u

除以瓷砖大小是错误的。应该是* TILE_SIZE / TEXTURE_SIZE。幸运的是,这恰好是相同的 (256 / 16 = 16)。如果你有不同大小的纹理,它就行不通。

于 2013-02-05T21:42:23.310 回答