gpt4 book ai didi

c++ - 去除opengl中2个四边形相交处的白线

转载 作者:太空狗 更新时间:2023-10-29 20:05:38 27 4
gpt4 key购买 nike

我正在构建一个用户可以从内向外看的盒子,天空盒或类似的东西。我这样做是通过取 6 个四边形并用它们创建一个立方体,然后将理论相机放在立方体内部并用图像对立方体进行纹理处理。用户可以使用鼠标环顾四周,立方体会旋转,看起来就像是在移动他们的头。我遇到了在两侧的交叉点出现白线的问题,我尝试拉入四边形,同时保持实际四边形的大小相同。我已经确定清晰的颜色是黑色,但我仍然看到白线。

这是我设置的属性:

glClearColor(0.0, 0.0, 0.0, 0.0); 
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT);

glClearDepth(1);
//S is the side length of the cube
float s = 5;
glShadeModel(GL_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glHint(GL_POLYGON_SMOOTH, GL_NICEST);

glEnable(GL_POLYGON_SMOOTH);

glDisable(GL_COLOR_MATERIAL);
glDisable(GL_LIGHTING);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

这是创建立方体的代码:

float s = 5; //S is the side length of each quad
glNewList(m_cube, GL_COMPILE);
{
glPushMatrix();
{
glEnable(GL_TEXTURE_2D);

glPushMatrix();
{

glBegin(GL_QUADS);
{

//front
{
glTexCoord2f(0.5f, (1.0f / 3.0f));
// glColor3f(1, 0, 0);
glVertex3f(((s / 2.0f)), ((s / 2.0f)),
-((s / 2.0f))); //1

glTexCoord2f(0.25f, (1.0f / 3.0f));
// glColor3f(1, 1, 0);
glVertex3f(-((s / 2.0f)), ((s / 2.0f)),
-((s / 2.0f))); //2
glTexCoord2f(0.25f, (2.0f / 3.0f));
// glColor3f(1, 0, 1);
glVertex3f(-((s / 2.0f)), -((s / 2.0f)),
-((s / 2.0f))); //3
glTexCoord2f(0.5f, (2.0f / 3.0f));
// glColor3f(1, 1, 1);
glVertex3f(((s / 2.0f)), -((s / 2.0f)),
-((s / 2.0f))); //4
}

//left
{
glTexCoord2f(0.25f, (1.0f / 3.0f));
// glColor3f(1, 0, 0);
glVertex3f(-((s / 2.0f)), ((s / 2.0f)),
-((s / 2.0f))); //2
glTexCoord2f(0.0f, (1.0f / 3.0f));
// glColor3f(1, 1, 0);
glVertex3f(-((s / 2.0f)), ((s / 2.0f)),
((s / 2.0f))); //5
glTexCoord2f(0.0f, (2.0f / 3.0f));
// glColor3f(1, 0, 1);
glVertex3f(-((s / 2.0f)), -((s / 2.0f)),
((s / 2.0f))); //6
glTexCoord2f(0.25f, (2.0f / 3.0f));
// glColor3f(1, 1, 1);
glVertex3f(-((s / 2.0f)), -((s / 2.0f)),
-((s / 2.0f))); //3
}

//right
{
glTexCoord2f(0.75f, (1.0f / 3.0f));
// glColor3f(1, 0, 0);
glVertex3f(((s / 2.0f)), ((s / 2.0f)),
((s / 2.0f))); //7
glTexCoord2f(0.5f, (1.0f / 3.0f));
// glColor3f(1, 1, 0);
glVertex3f(((s / 2.0f)), ((s / 2.0f)),
-((s / 2.0f))); //1
glTexCoord2f(0.5f, (2.0f / 3.0f));
// glColor3f(1, 0, 1);
glVertex3f(((s / 2.0f)), -((s / 2.0f)),
-((s / 2.0f))); //4
glTexCoord2f(0.75f, (2.0f / 3.0f));
// glColor3f(1, 1, 1);
glVertex3f(((s / 2.0f)), -((s / 2.0f)),
((s / 2.0f))); //8
}

//back
{
glTexCoord2f(1.0f, (1.0f / 3.0f));
// glColor3f(1, 0, 0);
glVertex3f(-((s / 2.0f)), ((s / 2.0f)),
((s / 2.0f))); //5
glTexCoord2f(0.75f, (1.0f / 3.0f));
// glColor3f(1, 1, 0);
glVertex3f(((s / 2.0f)), ((s / 2.0f)),
((s / 2.0f))); //7
glTexCoord2f(0.75f, (2.0f / 3.0f));
// glColor3f(1, 0, 1);
glVertex3f(((s / 2.0f)), -((s / 2.0f)),
((s / 2.0f))); //8
glTexCoord2f(1.0f, (2.0f / 3.0f));
// glColor3f(1, 1, 1);
glVertex3f(-((s / 2.0f)), -((s / 2.0f)),
((s / 2.0f))); //6
}

//top
{
glTexCoord2f(.5f, 1.0f);
// glColor3f(1, 0, 0);
glVertex3f(((s / 2.0f)), -((s / 2.0f)),
((s / 2.0f))); //7
glTexCoord2f(0.25f, 1.0f);
// glColor3f(1, 1, 0);
glVertex3f(-((s / 2.0f)), -((s / 2.0f)),
((s / 2.0f))); //5
glTexCoord2f(0.25f, (2.0f / 3.0f));
// glColor3f(1, 0, 1);
glVertex3f(-((s / 2.0f)), -((s / 2.0f)),
-((s / 2.0f))); //2
glTexCoord2f(.5f, (2.0f / 3.0f));
// glColor3f(1, 1, 1);
glVertex3f(((s / 2.0f)), -((s / 2.0f)),
-((s / 2.0f))); //1
}

//bottom
{
glTexCoord2f(.5f, (1.0f / 3.0f));
// glColor3f(1, 0, 0);
glVertex3f(((s / 2.0f)), ((s / 2.0f)),
-((s / 2.0f))); //4
glTexCoord2f(0.25f, (1.0f / 3.0f));
// glColor3f(1, 1, 0);
glVertex3f(-((s / 2.0f)), ((s / 2.0f)),
-((s / 2.0f))); //3
glTexCoord2f(0.25f, 0.0f);
// glColor3f(1, 0, 1);
glVertex3f(-((s / 2.0f)), ((s / 2.0f)),
((s / 2.0f))); //6
glTexCoord2f(.5f, 0.0f);
// glColor3f(1, 1, 1);
glVertex3f(((s / 2.0f)), ((s / 2.0f)),
((s / 2.0f))); //8
}

}
glEnd();

}
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
glPopMatrix();
}
glEndList();

这是设置相机的代码:

float near_ = 1f;
float far_ = 10.0f;

float halfHeight = near_ * (Radian(m_fov)) / 2.0f;
float halfWidth = (static_cast<float>((m_width)) / static_cast<float>((m_height))) * halfHeight;
glViewport(0, 0, m_width, m_height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glFrustum(-halfWidth, halfWidth, -halfHeight, halfHeight, near_, far_);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

这是呈现到屏幕的代码:

void VideoStreamer::render()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

float pitch = clamp(Degree(m_pitch), Degree(-(80.f)), Degree(80.f));
float yaw = wrap(m_yaw, Degree(0), Degree(360));
float roll = wrap(m_roll, Degree(0), Degree(360));
glPushMatrix();
{
glLineWidth(5);
glPushMatrix();
{
if (1)
{
glRotatef(pitch, 1.0, 0.0, 0.0);
glRotatef(yaw, 0.0, 1.0, 0.0);
glRotatef(roll, 0.0, 0.0, 1.0);
}

glCallList((m_cube));
}
glPopMatrix();
}
glPopMatrix();
SDL_GL_SwapBuffers();
}//end render

这是正在发生的事情的屏幕截图,注意蓝色和黑色四边形之间的白线:
enter image description here

这是我正在使用的纹理,分辨率为 600x450,但线条出现在多种分辨率下:

enter image description here

我的问题是:如何去掉那条白线?

编辑:白线只出现在顶部和底部与左右相交的地方

编辑:更新代码以反射(reflect)建议

最佳答案

您所看到的只是纹理中确实有白色。因此,面部边界上的碎片最终会从白色部分获取(如果您在纹理上启用线性过滤器类型,您会更清楚地看到这一点)。

你可以做些什么来修复它:

  1. 在展开的立方体纹理的边界处复制预期的颜色(在蓝色左侧添加红色,在右侧添加黑色,在红色和黑色顶部添加蓝色,等等底部...)
  2. 或者更好的是,将您的代码切换为使用立方体贴图而不是二维贴图。它们在立方体表面提供适当的过滤

关于c++ - 去除opengl中2个四边形相交处的白线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12373605/

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