【问题标题】:How to color the face of a cube object drawn with Opentk如何为使用 Opentk 绘制的立方体对象的面着色
【发布时间】:2022-01-15 23:15:49
【问题描述】:

我正在尝试用opentk 为我在此处绘制的立方体对象的面着色。我已经注册了一种方法,可以让我听键盘按键以指定颜色为面部着色。目前,立方体对象是用白色绘制的,在黑色窗口背景下不可能知道立方体的形状。我查看了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();
    }
}

【问题讨论】:

    标签: c# opengl opentk opengl-compat face


    【解决方案1】:

    您必须在绘制四边形之前指定颜色。为 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();
    

    【讨论】:

    • 它有效,但我需要按下按钮来着色,当我点击 Y 或其他东西时,将面 1 着色为黄色
    • @KINYUATIMOTHYNJIRU 是的,我知道。我明确提到过:“如果要在按键时更改颜色,则需要更改颜色数组中的值。”。您必须在按下键时更改颜色colors[0,1] = 1.0f。诅咒colors必须是属性而不是局部变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多