Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to draw a simple 3D object on a window using C# and Open TK. I have defined the GameWindowSettings and the NativeWindowSettings and passed them to the constructor of the GameWindow constructor. I have attached event handlers for the OnLoad and OnRenderFrame events of the game window and pasted code inside both of them that should draw a simple 3D cube on the window but its not working, can anyone help me make my code draw the desired object in the game window.

What I have tried:

C#
namespace MyVideoGame
{
	internal static class Program
	{
		[STAThread]
		static void Main(string[] args) {
			//create a new window game settings
			var windowsettigs = new GameWindowSettings
			{
				//set the size of the game screen
				RenderFrequency = 60,
				UpdateFrequency = 60
			};
			//define the native window settings
			var nativeSettings = new NativeWindowSettings
			{
				Size = new Vector2i(900, 600),
				Title = "Rotating Bridge",

			};
			//create a new game object
			var mygame = new GameWindow(windowsettigs, nativeSettings);
			//attach code to execute when the game is loaded
			mygame.Load += Mygame_Load;
			mygame.RenderFrame += Mygame_RenderFrame;
			//run the game
			mygame.Run();

		}

		private static void Mygame_RenderFrame(OpenTK.Windowing.Common.FrameEventArgs obj)
		{

			GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); // Clear the color and depth buffers

			Matrix4 modelview = Matrix4.LookAt(new Vector3(0, 0, 3), Vector3.Zero, Vector3.UnitY); // Create a modelview matrix
			GL.MatrixMode(MatrixMode.Modelview); // Set the matrix mode to modelview
			GL.LoadMatrix(ref modelview); // Load the modelview matrix

			GL.Color3(Color.Green); // Set the color to green
			GL.Begin(PrimitiveType.Quads); // Begin drawing a cube
			GL.Vertex3(-1.0f, -1.0f, 1.0f); // Front bottom left
			GL.Vertex3(1.0f, -1.0f, 1.0f); // Front bottom right
			GL.Vertex3(1.0f, 1.0f, 1.0f); // Front top right
			GL.Vertex3(-1.0f, 1.0f, 1.0f); // Front top left

			GL.Vertex3(-1.0f, -1.0f, -1.0f); // Back bottom left
			GL.Vertex3(1.0f, -1.0f, -1.0f); // Back bottom right
			GL.Vertex3(1.0f, 1.0f, -1.0f); // Back top right
			GL.Vertex3(-1.0f, 1.0f, -1.0f); // Back top left

			GL.Vertex3(-1.0f, 1.0f, 1.0f); // Left top front
			GL.Vertex3(-1.0f, 1.0f, -1.0f); // Left top back
			GL.Vertex3(-1.0f, -1.0f, -1.0f); // Left bottom back
			GL.Vertex3(-1.0f, -1.0f, 1.0f); // Left bottom front

		}

		private static void Mygame_Load()
		{
			//use open gl to draw a cube

			//set the color of the window to white
			GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);


		}
	}
}
Posted
Updated 1-Apr-23 1:21am

1 solution

 
Share this answer
 
Comments
Tim the Gamer 1-Apr-23 7:31am    
Looks helpful, thanks for the link.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900