gpt4 book ai didi

c++ - OpenGL 纹理白线间隙

转载 作者:太空宇宙 更新时间:2023-11-04 14:10:27 26 4
gpt4 key购买 nike

作为一个业余项目,我已经有一段时间了,现在我一直在尝试创建一些体素地形。然而,令我沮丧的是,纹理似乎无法正常工作。我正在使用 GL_REPEAT 和 GL_NEAREST 映射纹理。纹理位于 sprite 表中,大小为 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

最佳答案

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

我注意到您正在打开各向异性过滤。即使没有 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。它之所以有效,是因为您的行数和列数相同。

最后你像这样计算 UVs:

t1.x/TILE_TEXTURE_SIZE + u

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

关于c++ - OpenGL 纹理白线间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14715467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com