gpt4 book ai didi

c# - 如何防止 OpenGL 中的三角形渲染在它们前面的三角形之上

转载 作者:太空宇宙 更新时间:2023-11-03 13:41:41 24 4
gpt4 key购买 nike

我一直在使用适用于 .NET 的 OpenTK 库来处理 OpenGL,编写我自己的引擎。我放置了 3 个不同的物体,一个旋转的立方体和 2 个相邻的立方体。在我更改对象顶部四边形的颜色之前,一切似乎都运行良好。

Rendering Error

我正在渲染顶部为绿色的立方体,在左侧,背面的方 block 被渲染在前面的方 block 之上。我似乎无法找出哪里出了问题,当相机设置为从另一侧看时,它会正确渲染。

以下是类中的相关代码,省略了不相关或不相关的方法、属性和属性:

GameState.cs

class GameState : State
{
// TEMP: Test Block
SimpleBlock block;

int i = 0;
public override void Render()
{
base.Render();

// Set OpenGL Settings
GL.Viewport(0, 0, 1024, 768);
GL.Enable(EnableCap.CullFace);

// Reset the Matrices
Matrices.ClearMatrices();

// Set Camera Settings (Field of view in radians)
Matrices.ProjectionMatrix = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 2, (1024.0f / 768.0f), 1, 1000);

// Create the Camera
// this has to be in reverse
Matrix4 viewMatrix = Matrix4.CreateRotationX((float)Math.PI/8);
viewMatrix = viewMatrix.Translate(0, -2, -4);

// Multiply it with the ModelView (Which at this point is set to a value that we can just use = and it has the same result)
Matrices.ModelViewMatrix = viewMatrix;

// Render the Block
Matrices.Push();

Matrices.ModelViewMatrix = Matrices.ModelViewMatrix.Translate(2, 0, 0);
Matrices.ModelViewMatrix = Matrices.ModelViewMatrix.Translate(0.5f, 0, 0.5f);
Matrices.ModelViewMatrix = Matrices.ModelViewMatrix.Rotate(0, i / 40.0f, 0);
block.Render();

Matrices.Pop();

// Render the Block Again Twice
Matrices.Push();

Matrices.ModelViewMatrix = Matrices.ModelViewMatrix.Translate(-2, 0, 0);
Matrices.ModelViewMatrix = Matrices.ModelViewMatrix.Translate(0.5f, 0, 0.5f);
block.Render();

Matrices.ModelViewMatrix = Matrices.ModelViewMatrix.Translate(0, 0, -1);
block.Render();

Matrices.Pop();

// Increment Rotation Test Variable
i++;
}
}

SimpleBlock.cs

class SimpleBlock : IBlock
{
public void Render()
{
// Send the Shader Parameters to the GPU
Shader.Bind();
Shader.SendMatrices();

// Begin Rendering the Polys
GL.Begin(BeginMode.Triangles);

// Front Quad
Shader.SetColor(Color4.SaddleBrown);
GL.Normal3(0, 0, 1);
GLUtils.QuadVertices(
new Vector3(-0.5f, 1, 0.5f),
new Vector3(-0.5f, 0, 0.5f),
new Vector3( 0.5f, 1, 0.5f),
new Vector3( 0.5f, 0, 0.5f));

// Right Quad
GL.Normal3(1, 0, 0);
GLUtils.QuadVertices(
new Vector3(0.5f, 1, 0.5f),
new Vector3(0.5f, 0, 0.5f),
new Vector3(0.5f, 1, -0.5f),
new Vector3(0.5f, 0, -0.5f));

// Back Quad
GL.Normal3(0, 0, -1);
GLUtils.QuadVertices(
new Vector3( 0.5f, 1, -0.5f),
new Vector3( 0.5f, 0, -0.5f),
new Vector3(-0.5f, 1, -0.5f),
new Vector3(-0.5f, 0, -0.5f));

// Left Quad
GL.Normal3(-1, 0, 0);
GLUtils.QuadVertices(
new Vector3(-0.5f, 1, -0.5f),
new Vector3(-0.5f, 0, -0.5f),
new Vector3(-0.5f, 1, 0.5f),
new Vector3(-0.5f, 0, 0.5f));

// Bottom Quad
GL.Normal3(0, -1, 0);
GLUtils.QuadVertices(
new Vector3(-0.5f, 0, 0.5f),
new Vector3(-0.5f, 0, -0.5f),
new Vector3( 0.5f, 0, 0.5f),
new Vector3( 0.5f, 0, -0.5f));

// Top Quad
Shader.SetColor(Color4.Green);
GL.Normal3(0, 1, 0);
GLUtils.QuadVertices(
new Vector3(-0.5f, 1, -0.5f),
new Vector3(-0.5f, 1, 0.5f),
new Vector3(0.5f, 1, -0.5f),
new Vector3(0.5f, 1, 0.5f));

// Done!
GL.End();
}
}

BasicFragment.glfs

#version 130

// MultiColor Attribute
in vec4 multiColor;

// Output color
out vec4 gl_FragColor;

void main()
{
// Set fragment
gl_FragColor = multiColor;
}

BasicVertex.glvs

#version 130

// Transformation Matrices
uniform mat4 ProjectionMatrix;
uniform mat4 ModelViewMatrix;

// Vertex Position Attribute
in vec3 VertexPos;

// MultiColor Attributes
in vec4 MultiColor;
out vec4 multiColor;

void main()
{
// Process Colors
multiColor = MultiColor;

// Process Vertex
gl_Position = ProjectionMatrix * ModelViewMatrix * vec4(VertexPos.x, VertexPos.y, VertexPos.z, 1);
}

MainWindow.cs

// Extends OpenTK's GameWindow Class
class MainWindow : GameWindow
{
public MainWindow()
: base(1024, 768, new GraphicsMode(32, 0, 0, 4))
{
this.Title = "Trench Wars";
this.WindowBorder = WindowBorder.Fixed;
this.ClientSize = new Size(1024, 768);

// Set VSync On
this.VSync = VSyncMode.Adaptive;
}

protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);

// Clear Screen
GL.ClearColor(Color4.CornflowerBlue);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

// Do State-Specific Rendering
StateEngine.Render();

// Pull a Wicked Bluffing move in Poker
GL.Flush();

// Swap Buffers
this.SwapBuffers();
}
}

最佳答案

您似乎忘记启用深度测试。 glEnable(GL_DEPTH_TEST) 在渲染几何体之前是你的 friend (或者给定你正在使用的语言绑定(bind) GL.Enable(EnableCap.DepthTest);)。

关于c# - 如何防止 OpenGL 中的三角形渲染在它们前面的三角形之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16781873/

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