C# i';我试图在opentk中实现索引缓冲区对象,但不';I don’我不知道在画东西时如何实际使用它

C# i';我试图在opentk中实现索引缓冲区对象,但不';I don’我不知道在画东西时如何实际使用它,c#,indexing,glsl,opentk,C#,Indexing,Glsl,Opentk,我试图使用索引缓冲区,但不知道如何使用 我真的被困在一堵墙里,不知道我应该怎么做。 没有一个教程真正展示了与我的代码类似的东西,这就是我遇到问题的主要原因。我可能需要重写立方体顶点数组生成器。(另一方面,也许我不应该把opengl硬塞进一些现成的代码中)现在我只启用了顶面,以查看发生了什么 /// <summary> /// Draws the specified Cube Onsccreen. /// </summary> public void Draw() {

我试图使用索引缓冲区,但不知道如何使用

我真的被困在一堵墙里,不知道我应该怎么做。 没有一个教程真正展示了与我的代码类似的东西,这就是我遇到问题的主要原因。我可能需要重写立方体顶点数组生成器。(另一方面,也许我不应该把opengl硬塞进一些现成的代码中)现在我只启用了顶面,以查看发生了什么

/// <summary>
/// Draws the specified Cube Onsccreen.
/// </summary>
public void Draw()
{
    GL.UseProgram(VSID);
    Vector3 ObjectPosition = new Vector3((new Vector(1, 1) * (Position - Offset)))
    {
        Z = Layer * 0.1f
    };
    Matrix4 Scale = Matrix4.CreateScale(Width + highlight, Height + highlight, Height + highlight);
    Matrix4 Translation = Matrix4.CreateTranslation(ObjectPosition);
            
    Matrix4 ViewPoint = Matrix4.CreateOrthographic(Game.window.Width , Game.window.Height, -1, 1000);
    ViewPoint = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI-Zoom,Game.window.Width/Game.window.Height,0.0001f,1000);
    Matrix4 Rotation = Matrix4.CreateRotationZ((float)Angle);
    Matrix4 CameraRotX = Matrix4.CreateRotationX((float)Math.PI / 180 * 40f);
    Matrix4 CameraRotZ = Matrix4.CreateRotationZ(Game.CameraAngle);

    Matrix4 Combined = Scale * Rotation  * Translation * ViewPoint * CameraRotZ * CameraRotX;

    GL.ProgramUniformMatrix4(VSID, GL.GetUniformLocation(VSID, "QuadMatrix"), false, ref Combined);
    GL.ProgramUniform4(VSID, GL.GetUniformLocation(VSID, "ColorIn"), Color);
    GL.ProgramUniform1(VSID, GL.GetUniformLocation(VSID, "SS"), 0);

    GL.Enable(EnableCap.Texture2D);
    GL.Enable(EnableCap.IndexArray);

    GL.ActiveTexture(TextureUnit.Texture0);
    GL.BindTexture(TextureTarget.Texture2D, Texture);
    GL.BindVertexArray(QID);
    GL.DrawArrays(PrimitiveType.Triangles, 0, indices.Length);
}

public static void CreateVisuals()
{
    int VS = Shaders.Load("Shaders.vs", ShaderType.VertexShader);
    int FS = Shaders.Load("Shaders.fs", ShaderType.FragmentShader);

    VSID = Visuals.Create(VS, FS);
}

public static void CreateCube()
{
    QID = GL.GenVertexArray();
    GL.BindVertexArray(QID);

    int VID =GL.GenBuffer();
    float[] Verticles = 
    {
        -0.5f,  0.5f,  0.5f,
         0.5f,  0.5f,  0.5f,
        -0.5f, -0.5f,  0.5f,
         0.5f, -0.5f,  0.5f,
        -0.5f,  0.5f, -0.5f,
         0.5f,  0.5f, -0.5f,
        -0.5f, -0.5f, -0.5f,
         0.5f, -0.5f, -0.5f
    };

    GL.BindBuffer(BufferTarget.ArrayBuffer, VID);
    GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * Verticles.Length, Verticles,BufferUsageHint.StaticDraw);
    GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0);
    GL.EnableVertexAttribArray(0);
            
    int UVID = GL.GenBuffer();
    float[] UVs =
    {
        0, 1,
        1, 1,
        0, 0,
        0, 0,
        1, 1,
        1, 0
    };

    GL.BindBuffer(BufferTarget.ArrayBuffer, UVID);
    GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * UVs.Length, UVs, BufferUsageHint.StaticDraw);
    GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 0, 0);
    GL.EnableVertexAttribArray(1);

    indices = new uint[]
    {
        0, 1, 2,
        2, 1, 3 /*,
        0, 1, 5,
        0, 1, 4,
        1, 3, 5,
        3, 5, 7,
        2, 3, 7,
        2, 6, 7,
        0, 2, 6,
        0, 4, 6,
        4, 5, 6,
        5, 6, 7 //*/
    };
    IBO = GL.GenBuffer();

    GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBO);
    GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(sizeof(uint) * indices.Length), indices, BufferUsageHint.StaticDraw);
    GL.VertexAttribPointer(IBO,1,VertexAttribPointerType.UnsignedInt,false,0,0);
    GL.EnableVertexAttribArray(2);
}
//
///在屏幕上绘制指定的多维数据集。
/// 
公众抽签()
{
总账使用程序(VSID);
Vector3 ObjectPosition=新矢量3((新矢量(1,1)*(位置-偏移)))
{
Z=层*0.1f
};
Matrix4 Scale=Matrix4.CreateScale(宽度+高光,高度+高光,高度+高光);
Matrix4 Translation=Matrix4.CreateTranslation(ObjectPosition);
Matrix4视点=Matrix4.CreateOrthographic(Game.window.Width,Game.window.Height,-1,1000);
视点=Matrix4.CreatePerspectiveFieldOfView((float)Math.PI Zoom,Game.window.Width/Game.window.Height,0.0001f,1000);
Matrix4旋转=Matrix4.CreateRotationZ((浮动)角度);
Matrix4 CameraRotX=Matrix4.CreateRotationX((float)Math.PI/180*40f);
Matrix4 CameraRotZ=Matrix4.CreateRotationZ(Game.CameraAngle);
Matrix4组合=比例*旋转*平移*视点*摄影机镜头*摄影机镜头X;
GL.ProgramUniformMatrix4(VSID,GL.GetUniformLocation(VSID,“四矩阵”),false,参考组合);
GL.ProgramUniform4(VSID,GL.GetUniformLocation(VSID,“ColorIn”),颜色);
GL.ProgramUniform1(VSID,GL.GetUniformLocation(VSID,“SS”),0;
总账启用(EnableCap.Texture2D);
总账启用(启用CAP.IndexArray);
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D,纹理);
德国劳埃德船级社(QID);
GL.DrawArray(基本类型.三角形,0,索引.长度);
}
公共静态void CreateVisuals()
{
int VS=Shaders.Load(“Shaders.VS”,ShaderType.VertexShader);
int FS=Shaders.Load(“Shaders.FS”,ShaderType.FragmentShader);
VSID=Visuals.Create(VS,FS);
}
公共静态void CreateCube()
{
QID=GL.GenVertexArray();
德国劳埃德船级社(QID);
int VID=GL.GenBuffer();
浮动[]垂直线=
{
-0.5f,0.5f,0.5f,
0.5f,0.5f,0.5f,
-0.5f,-0.5f,0.5f,
0.5f,-0.5f,0.5f,
-0.5f,0.5f,-0.5f,
0.5f,0.5f,-0.5f,
-0.5f,-0.5f,-0.5f,
0.5f,-0.5f,-0.5f
};
GL.BindBuffer(BufferTarget.ArrayBuffer,VID);
GL.BufferData(BufferTarget.ArrayBuffer,sizeof(float)*Verticles.Length,Verticles,BufferUsageHint.StaticDraw);
GL.VertexAttributePointer(0,3,VertexAttributePointerType.Float,false,0,0);
GL.EnableVertexAttributeArray(0);
int UVID=GL.GenBuffer();
浮动[]UV=
{
0, 1,
1, 1,
0, 0,
0, 0,
1, 1,
1, 0
};
GL.BindBuffer(BufferTarget.ArrayBuffer,UVID);
GL.BufferData(BufferTarget.ArrayBuffer,sizeof(float)*UV.Length,UV,BufferUsageHint.StaticDraw);
GL.VertexAttributePointer(1,2,VertexAttributePointerType.Float,false,0,0);
GL.EnableVertexAttributeArray(1);
索引=新的uint[]
{
0, 1, 2,
2, 1, 3 /*,
0, 1, 5,
0, 1, 4,
1, 3, 5,
3, 5, 7,
2, 3, 7,
2, 6, 7,
0, 2, 6,
0, 4, 6,
4, 5, 6,
5, 6, 7 //*/
};
IBO=GL.GenBuffer();
GL.BindBuffer(BufferTarget.ElementArrayBuffer,IBO);
GL.BufferData(BufferTarget.ElementArrayBuffer,(IntPtr)(sizeof(uint)*index.Length)、index、BufferUsageHint.StaticDraw);
GL.VertexAttributePointer(IBO,1,VertexAttributePointerType.UnsignedInt,false,0,0);
GL.EnableVertexAttributeArray(2);
}
这两个三角形没有绘制简单的四边形,而是以错误的顺序绘制。它实际上只是使用空间中的坐标,而不是这些坐标的索引 目前看起来是这样的:

名称(值)在的状态向量中声明。
要“使用”它,您必须将顶点数组对象binbd并调用,而不是:


必须“复制”顶点位置。看见顶点坐标及其属性(如纹理坐标)形成一个元组。可以将3D顶点坐标和2D纹理坐标想象为单个5D坐标

对于立方体的两条边,至少必须使用单独的顶点和属性。这意味着您至少需要8+2*2=12组不同的顶点属性

xyzuv
0  :    -0.5  0.5  0.5    0  0
1  :     0.5  0.5  0.5    1  0
2  :    -0.5 -0.5  0.5    0  1
3  :     0.5 -0.5  0.5    1  1
4  :    -0.5 -0.5 -0.5    0  0
5  :     0.5 -0.5 -0.5    1  0
6  :    -0.5  0.5 -0.5    0  1 
7  :     0.5  0.5 -0.5    1  1
8  :    -0.5  0.5  0.5    1  1
9  :    -0.5  0.5 -0.5    1  0
10 :     0.5  0.5  0.5    0  1
11 :     0.5  0.5 -0.5    0  0
请注意,索引为0、2、4和6的顶点属性集具有相同的u坐标0。索引为1、3、5和7的顶点属性集具有相同的u坐标1。
如果要将纹理包裹到四边形,则必须改变u和v坐标。因此,必须添加分离的顶点属性集8、9、10和11

使用此顶点坐标和纹理坐标设置,可以定义立方体的12个三角形面:

0-2-12-3-1
8 - 9 -  2    9 -  4 -  2
2 - 4 -  3    4 -  5 -  3
3 - 5 - 10    5 - 11 - 10
4 - 6 -  5    6 -  7 -  5
6 - 0 -  7    0 -  1 -  7

所以我的问题是,我从二维到三维,从简单的四边形到索引缓冲对象,当你从你使用的顶点绘制直线时

GL.DrawArrays()

但是当你使用ind的时候
GL.BindVertexArray(QID);
GL.DrawElements(PrimitiveType.Triangles, 0, DrawElementsType.UnsignedInt, NULL);