如何给用 Opentk 绘制的立方体对象的表面着色

How to color the face of a cube object drawn with Opentk

我正在尝试为我在此处绘制的立方体对象的表面着色 opentk。我已经注册了一种方法,可以让我监听键盘按键以使用指定颜色为面部着色。目前立方体对象是用白色绘制的,因此不可能知道立方体在黑色 window 背景下的形状。我查看了 opengl 的官方文档,他们建议为了给立方体的一个面着色,您需要在 instance 上调用函数 gl.color3(float red, float green, float blue)。绘制对象的代码在这里,是否可以在按下键盘键时为对象的表面着色。这是我的代码

//extend the GameWindow class
class MyWindow : GameWindow
{
   public MyWindow(GameWindowSettings gameWindowSettings, NativeWindowSettings nativeWindowSettings) : base(gameWindowSettings, nativeWindowSettings)
    {
        WindowState = OpenTK.Windowing.Common.WindowState.Normal;
      //register a listener for keyboard events
        KeyDown += MyWindow_KeyDown;
    }

    private void MyWindow_KeyDown(KeyboardKeyEventArgs obj)
    {
        switch (obj.Key)
        {
            case Keys.W:
                //rotate cube upwards
                Console.WriteLine("Rotating object upward");
                //opengl function to rotate the cube downwards
                GL.Rotate(15, OpenTK.Mathematics.Vector3d.UnitZ);
                break;
                case Keys.S:
                Console.WriteLine("Rotating object downward");
                break;
            case Keys.A:
                Console.WriteLine("Rotating object to the left");
                break;
            case Keys.D:
                
                GL.Rotate(15, OpenTK.Mathematics.Vector3d.UnitY);
                GL.Color3(0,0,0);
                break;
             case Keys.P:
                //color a face on keypress with a different color
                 
                break;
        }
    }
    //method that draws the cube inside the GameWindow object
      protected override void OnUpdateFrame(FrameEventArgs args)
    {


        

        Context.SwapBuffers();

        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

        this.DrawBox(0.5f);
        base.OnUpdateFrame(args);

    }
      //the cube drawing logic method
     private void DrawBox(float size)
    {
        float[,] n = new float[,]{
            {-1.0f, 0.0f, 0.0f},
            {0.0f, 1.0f, 0.0f},
            {1.0f, 0.0f, 0.0f},
            {0.0f, -1.0f, 0.0f},
            {0.0f, 0.0f, 1.0f},
            {0.0f, 0.0f, -1.0f}
        };
        int[,] faces = new int[,]{
            {0, 1, 2, 3},
            {3, 2, 6, 7},
            {7, 6, 5, 4},
            {4, 5, 1, 0},
            {5, 6, 2, 1},
            {7, 4, 0, 3}
        };
        float[,] v = new float[8, 3];
        int i;

        v[0, 0] = v[1, 0] = v[2, 0] = v[3, 0] = -size / 2;
        v[4, 0] = v[5, 0] = v[6, 0] = v[7, 0] = size / 2;
        v[0, 1] = v[1, 1] = v[4, 1] = v[5, 1] = -size / 2;
        v[2, 1] = v[3, 1] = v[6, 1] = v[7, 1] = size / 2;
        v[0, 2] = v[3, 2] = v[4, 2] = v[7, 2] = -size / 2;
        v[1, 2] = v[2, 2] = v[5, 2] = v[6, 2] = size / 2;

        GL.Begin(BeginMode.Quads);
        for (i = 5; i >= 0; i--)
        {
      // s
            GL.Normal3(ref n[i, 0]);
            //GL.Color3(1, 1, 1);
            GL.Vertex3(ref v[faces[i, 0], 0]);
            GL.Vertex3(ref v[faces[i, 1], 0]);
            GL.Vertex3(ref v[faces[i, 2], 0]);
            GL.Vertex3(ref v[faces[i, 3], 0]);
        }
        GL.End();
    }
}

您必须在绘制四边形之前指定颜色。为 6 条边分别定义一种颜色。 (此颜色只是示例):

float[,] colors = new float[,]{
    {1.0f, 0.0f, 0.0f},
    {0.0f, 1.0f, 0.0f},
    {0.0f, 0.0f, 1.0f},
    {1.0f, 1.0f, 0.0f},
    {0.0f, 1.0f, 1.0f},
    {1.0f, 0.0f, 1.0f}
};

如果你想在按键时改变颜色,你需要改变colors数组中的值。

在绘制四边形之前使用 GL.Color3 设置颜色:

GL.Begin(BeginMode.Quads);
for (i = 5; i >= 0; i--)
{
    GL.Color3(ref colors[i, 0]);
    GL.Vertex3(ref v[faces[i, 0], 0]);
    GL.Vertex3(ref v[faces[i, 1], 0]);
    GL.Vertex3(ref v[faces[i, 2], 0]);
    GL.Vertex3(ref v[faces[i, 3], 0]);
}
GL.End();