Cube的底部面没有渲染 – 面剔除OpenGL

huangapple go评论81阅读模式
英文:

Bottom Face of Cube not rendered - Face Cullling OpenGL

问题

我用SFML替代GLFW在OpenGL C++中绘制了一个立方体。立方体在不进行面剔除时呈现正常,但在进行面剔除后,立方体的底部面不可见。

我希望摄像机朝向立方体的面能够呈现,但即使摄像机朝向它们,底部面也不被呈现。以下是我的立方体顶点数据和纹理数据:

GLfloat vertices[] = {
    // 前面  // 纹理坐标
    -1.0f, -1.0f,  1.0f,  0.0f, 1.0f,
    1.0f, -1.0f,  1.0f,  1.0f, 1.0f,
    1.0f,  1.0f,  1.0f,  1.0f, 0.0f,
    -1.0f,  1.0f,  1.0f,  0.0f, 0.0f,
    // 其他面的顶点数据...
};

我的立方体索引数据:

GLuint indices[] = {
    // 前面
    0, 1, 2,
    2, 3, 0,
    // 其他面的索引数据...
};

我的面剔除设置:

// 面剔除
glEnable(GL_CULL_FACE);
// 剔除设置
glCullFace(GL_FRONT);
glFrontFace(GL_CW);

从你提供的信息和图片来看,面剔除可能导致底部面不可见。你可以尝试将 glCullFace(GL_FRONT); 改为 glCullFace(GL_BACK);,这样应该会呈现底部面。希望这有所帮助!

英文:

I am drawing a cube in OpenGL C++ with SFML in place of GLFW. The Cube is being rendered correctly without face culling but after face culling the bottom face of the cube is not visible.

I wanted the camera - facing sides of the cube to be rendered but the bottom side even when the camera is facing them they are not being rendered. My cube vertex data with texture data :

GLfloat vertices[] = {
		// Front face  //Texture Coordinates
		-1.0f, -1.0f,  1.0f,  0.0f, 1.0f,
		1.0f, -1.0f,  1.0f,  1.0f, 1.0f,
		1.0f,  1.0f,  1.0f,  1.0f, 0.0f,
		-1.0f,  1.0f,  1.0f,  0.0f, 0.0f,
		// Back face
		-1.0f, -1.0f, -1.0f,  1.0f, 1.0f,
		-1.0f,  1.0f, -1.0f,  1.0f, 0.0f,
		1.0f,  1.0f, -1.0f,  0.0f, 0.0f,
		1.0f, -1.0f, -1.0f,  0.0f, 1.0f,
		// Left face
		-1.0f, -1.0f, -1.0f,  0.0f, 0.0f,
		-1.0f, -1.0f,  1.0f,  1.0f, 0.0f,
		-1.0f,  1.0f,  1.0f,  1.0f, 1.0f,
		-1.0f,  1.0f, -1.0f,  0.0f, 1.0f,
		// Right face
		1.0f, -1.0f, -1.0f,  1.0f, 0.0f,
		1.0f,  1.0f, -1.0f,  1.0f, 1.0f,
		1.0f,  1.0f,  1.0f,  0.0f, 1.0f,
		1.0f, -1.0f,  1.0f,  0.0f, 0.0f,
		// Top face
		-1.0f,  1.0f, -1.0f,  0.0f, 1.0f,
		-1.0f,  1.0f,  1.0f,  1.0f, 1.0f,
		1.0f,  1.0f,  1.0f,  1.0f, 0.0f,
		1.0f,  1.0f, -1.0f,  0.0f, 0.0f,
		// Bottom face
		-1.0f, -1.0f, -1.0f, 1.0f, 0.0f,
		-1.0f, -1.0f,  1.0f, 0.0f, 0.0f,
		1.0f, -1.0f,  1.0f, 0.0f, 1.0f,
		1.0f, -1.0f, -1.0f, 1.0f, 1.0f
};

My cube indice data :

GLuint indices[] = {
		// Front face
		0, 1, 2,
		2, 3, 0,
		// Back face
		4, 5, 6,
		6, 7, 4,
		// Left face
		8, 9, 10,
		10, 11, 8,
		// Right face
		12, 13, 14,
		14, 15, 12,
		// Top face
		16, 17, 18,
		18, 19, 16,
		// Bottom face
		20, 21, 22,
		22, 23, 20
};

My Face culling settings

//Face culling
glEnable(GL_CULL_FACE);
//Cull settings
glCullFace(GL_FRONT);
glFrontFace(GL_CW);

IMAGES :
Cube的底部面没有渲染 – 面剔除OpenGL
Cube的底部面没有渲染 – 面剔除OpenGL

答案1

得分: 1

以下是翻译好的部分:

"Works as expected." -> "正常工作。"

"Top and bottom sides of your box have vertices defined in CCW order." -> "你的盒子的顶部和底部边缘以逆时针顺序定义了顶点。"

"If vertices defined in CW order are taken to be front-facing when you see at the top side of the box from above you are looking at the back face whose rendering is enabled that is why you see box's top side." -> "如果以顺时针顺序定义的顶点被认为是正面,当你从上面看到盒子的顶面时,你实际上是在看到启用了渲染的背面,这就是为什么你看到了盒子的顶面。"

"When you look at the bottom side of box below the box you cannot see that side because it is treated as front face - rendering of which is disabled." -> "当你从盒子下面看底部时,你无法看到那一面,因为它被视为正面,其渲染已禁用。"

"Just change the order of indices for box's bottom side to be CW order:" -> "只需更改盒子底部的索引顺序为顺时针顺序:"

"// Bottom face
21, 20, 23,
21, 23, 22" -> "// 底部面
21, 20, 23,
21, 23, 22"

英文:

Works as expected.

Top and bottom sides of your box have vertices defined in CCW order.

If vertices defined in CW order are taken to be front-facing when you see at the top side of the box from above you are looking at the back face whose rendering is enabled that is why you see box's top side.

When you look at the bottom side of box below the box you cannot see that side because it is treated as front face - rendering of which is disabled.

Just change the order of indices for box's bottom side to be CW order:

    // Bottom face
    21, 20, 23,
    21, 23, 22

huangapple
  • 本文由 发表于 2023年4月13日 18:36:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定