C#模型的显示问题

时间:2018-12-25 09:19:19

标签: c# opengl graphics 3d opentk

opentk中的模型显示问题C#我不知道确切的问题出在哪里或我错了。 我真的需要这里的帮助,因为我无法确定原因或解决方案。

提示:iam编辑帖子

对象必须一个接一个,但这是结果:

[

shader.fs:

#version 330

in vec2 texCoord0;

uniform sampler2D diffuse;

out vec4 fragColor;

void main() 
{
fragColor = texture2D(diffuse, texCoord0.xy);
}

shader.vs:

#version 330

layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texCoord;

out vec2 texCoord0;

uniform mat4 MVP;

void main()
{
gl_Position = MVP * vec4(position, 1.0);
texCoord0 = texCoord;
}

这是一些代码:

    public static void InitGL() {


    GL.ClearColor(Color4.MidnightBlue);

        GL.FrontFace(FrontFaceDirection.Ccw);
        GL.CullFace(CullFaceMode.Back);
        GL.Enable(EnableCap.CullFace);

        GL.Enable(EnableCap.DepthTest);

        GL.Enable(EnableCap.FramebufferSrgb);

        GL.Enable(EnableCap.Texture2D);
    }
class Mesh {
    private int vao;

    private int vbo;
    private int tcbo;
    private int nbo;
    private int ibo;
    private int size = 0;

    private Vector3[] positions = new Vector3[0];
    private Vector2[] texCoords = new Vector2[0];
    private Vector3[] normals = new Vector3[0];
    private int[] indices = new int[0];

public Mesh() {
        vbo = GL.GenBuffer();
        tcbo = GL.GenBuffer();
        nbo = GL.GenBuffer();
        ibo = GL.GenBuffer();
       vao = GL.GenVertexArray();
 }

public void AddVertices(Vector3[] positions, Vector2[] texCoords, Vector3[] normals, int[] indices) {
        size = +indices.Length;

        Vector3[] newArray = new Vector3[this.positions.Length + positions.Length];
        Array.Copy(this.positions, newArray, 0);
        Array.Copy(positions, 0, newArray, this.positions.Length, positions.Length);
        this.positions = newArray;


        Vector2[] newArray0 = new Vector2[this.texCoords.Length + texCoords.Length];
        Array.Copy(this.texCoords, newArray0, 0);
        Array.Copy(texCoords, 0, newArray0, this.texCoords.Length, texCoords.Length);
        this.texCoords = newArray0;

        Vector3[] newArray1 = new Vector3[this.normals.Length + normals.Length];
        Array.Copy(this.normals, newArray1, 0);
        Array.Copy(normals, 0, newArray1, this.normals.Length, normals.Length);
        this.normals = newArray1;

        int[] newArray2 = new int[this.indices.Length + indices.Length];
        Array.Copy(this.indices, newArray2, 0);
        Array.Copy(indices, 0, newArray2, this.indices.Length, indices.Length);
        this.indices = newArray2;

    }

    public void Complete() {
        GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
        GL.BufferData<Vector3>(BufferTarget.ArrayBuffer, new IntPtr(positions.Length * Vector3.SizeInBytes), positions, BufferUsageHint.StaticDraw);

        GL.BindBuffer(BufferTarget.ArrayBuffer, tcbo);
        GL.BufferData<Vector2>(BufferTarget.ArrayBuffer, new IntPtr(texCoords.Length * Vector2.SizeInBytes), texCoords, BufferUsageHint.StaticDraw);

        GL.BindBuffer(BufferTarget.ArrayBuffer, nbo);
        GL.BufferData<Vector3>(BufferTarget.ArrayBuffer, new IntPtr(normals.Length * Vector3.SizeInBytes), normals, BufferUsageHint.StaticDraw);

        GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo);
        GL.BufferData(BufferTarget.ElementArrayBuffer, new IntPtr(sizeof(uint) * indices.Length), indices, BufferUsageHint.StaticDraw);

        GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);

        GL.BindVertexArray(vao);

        GL.EnableVertexAttribArray(0);
        GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
        GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0);

        GL.EnableVertexAttribArray(1);
        GL.BindBuffer(BufferTarget.ArrayBuffer, tcbo);
        GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, true, Vector2.SizeInBytes, 0);

        GL.EnableVertexAttribArray(2);
        GL.BindBuffer(BufferTarget.ArrayBuffer, nbo);
        GL.VertexAttribPointer(2, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0);

        GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo);

        GL.BindVertexArray(0);
    }

    public void Draw() {
        GL.BindVertexArray(vao);
        GL.DrawElements(PrimitiveType.Triangles, size, DrawElementsType.UnsignedInt, IntPtr.Zero);
    }
}

1 个答案:

答案 0 :(得分:0)

根据评论,似乎原始用户已解决了该问题。但是,这显然是深度测试的问题。在绘制任何内容之前,请确保清除 之前的深度缓冲区和颜色缓冲区。