I am trying to color the faces of a cube object I have drawn here with opentk. I have registered a method that lets me listen on keyboard key presses to color a face with a specified color. Currently the cube object is drawn with a white color that it is impossible to know the shape of the cube against a black window background. I have checked the official documentation of opengl and they suggest that in order to color a face of a cube, you need to invoke the function gl.color3(float red, float green, float blue) on the instance. The code that to draw the object is here, is it possible to color the face of the object when a keyboard key is pressed. Here is my code
//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();
}
}