Проблема в отображении моделей c # opentk - PullRequest
0 голосов
/ 25 декабря 2018

Проблема с отображением моделей в opentk c # Я не знаю, где именно проблема или где я не прав.Мне действительно нужна была помощь, потому что я не мог определить причину или решение для этого.

Подсказка: я редактирую сообщение

Объекты должны быть один за другим, но это результат:

[

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 голосов
/ 21 января 2019

Судя по комментариям, похоже, что оригинальный пользователь решил проблему.Тем не менее, это явно проблема с тестом глубины.Убедитесь, что вы очищаете буфер глубины, а также буфер цвета до рисования чего-либо.

...