OpenGL纹理白线间隙

时间:2013-02-05 19:35:53

标签: c++ opengl textures terrain voxel

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

enter image description here

以下是处理它的代码:

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。这也没有帮助。我对如何处理这个问题感到很茫然。有任何想法吗?这是纹理: enter image description here

2 个答案:

答案 0 :(得分: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。它只能起作用,因为你有相同数量的行和列。

最后你计算这样的紫外线:

t1.x/TILE_TEXTURE_SIZE + u

除以图块大小是错误的。它应该是* TILE_SIZE / TEXTURE_SIZE。幸运的是,这恰好相同(256/16 = 16)。如果你有不同大小的纹理,它将无法正常工作。

答案 1 :(得分:0)

您的基本问题是您将多个图像平铺到一个大纹理中。这将导致任何类型的纹理过滤问题,如mipmapping和各向异性过滤。您不想禁用纹理过滤。相反,您应该将每个图像逻辑地视为不同的图像。一种简单的方法是使每个图块成为单独的纹理。这有点慢,所以为了获得更好的性能,你应该使用Array Texture