如何防止OpenGL中的三角形在自身前面的三角形上呈现

时间:2013-05-27 23:39:27

标签: c# .net opengl shader

我一直在使用OpenGL的OpenGL库来编写我自己的引擎。我放置了3个不同的物体,一个旋转立方体和两个相邻的立方体在我改变对象顶部四边形的颜色之前,一切似乎都能正常工作。

Rendering Error

我正在使用绿色顶部渲染立方体,在左侧,背面的块正在正面的块上渲染。我似乎无法找到我出错的地方,当相机设置为从另一侧看时它会正确呈现。

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

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();
    }
}

1 个答案:

答案 0 :(得分:1)

好像你忘了启用深度测试。渲染几何体之前的glEnable(GL_DEPTH_TEST)是您的朋友(或者您使用的语言绑定是GL.Enable(EnableCap.DepthTest);)。