With the release of the GFX engine for .NET 8, setting up a new project is also much easier. With the exception of the native OpenAL libraries, all referenced content is provided as Nuget references. There are also the starter templates which can be downloaded from the website.
You can also implement your own scenes, game elements and much more through interfaces so that you can create the enviroment your game needs. This short article will show you how to create your first project with GFX.
First, you should download a project template and add it to your Visual Studio templates. This already contains the required Nuget packages as references and has the advantage that you can use it for all your future projects.
After you have downloaded a template and added it to visual Studio, you can start setting up your project. In both templates you will find a starter asset and an example of how the engine can be used.
The first step you should take is to create the project for the first time. For this you can simply use the build command, Ctrl + B or however you like to create your projects. This will ensure that the linked packages are installed.
Then, if you want to use 3D sound, you should install OpenAL or put the native files in your build folder to the exe of your game. Installing is easier for you, but you should deliver the native libarys later together with your game. You can download OpenAL here: OpenAL: Cross Platform 3D Audio
Your project is now set up so that you can start creating your game. In the following code you will find the code of the game class from the 3D template so that you can see what the workflow might look like.
Example Code
C#
usingLibGFX.Core.GameElements;usingLibGFX.Core;usingLibGFX.Graphics.Lights;usingLibGFX.Graphics.Materials;usingLibGFX.Graphics;usingLibGFX.Pyhsics;usingLibGFX;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingOpenTK.Mathematics;usingAssimp;usingSystem.Diagnostics;usingLibGFX.Graphics.Enviroment;usingLibGFX.Graphics.Primitives;namespaceGFX3DGame{publicclassGame {privateWindow_window;privateGLRenderer_renderer;privatePerspectiveCamera_camera;privateScene3D_scene;privatePhysicsHandler3D_physicsHandler;publicGame() { // Initialize game components here }publicvoidInitialize() { // Create an new game windowvarviewport=newViewport(800, 600); _window = GFX.Instance.CreateWindow("GFX", viewport, OpenTK.Windowing.Common.WindowState.Normal); // Create an OpenGL rendererw _renderer =newGLRenderer(); _renderer.Init(_window); _renderer.UseVsync(true); // Create the camera for the rendering _camera =newPerspectiveCamera(newVector3(0.0f, 5f, -7.0f), newVector3(viewport.Width, viewport.Height, 0.0f)); _camera.SetAsCurrent(); // Create the scene with layers _scene =newScene3D("BASE_LAYER", "AI_LAYER", "PLAYER_LAYER", "ITEM_LAYER"); _scene.Enviroment =newProceduralSky(); _scene.SetDirectionalLight(newDirectionalLight(newVector3(-0.2f, 1.0f, -0.3f), newVector4(0.025f, 0.025f, 0.025f, 1.0f), 1.5f)); _scene.AddPointLight(newPointLight3D(newVector3(2f, 0f, 0f), newVector4(0.8f, 0.0f, 0.0f, 1.0f), 4f, 30f)); _scene.AddPointLight(newPointLight3D(newVector3(-2f, 0f, 0f), newVector4(0.0f, 0.8f, 0.0f, 1.0f), 4f, 30f)); _scene.AddPointLight(newPointLight3D(newVector3(0f, 2f, 0f), newVector4(0.0f, 0.0f, 0.8f, 1.0f), 4f, 30f)); // Load the assetsvarmodel= GFX.Instance.AssetManager.Load<Model>("D:/3D Modele/GFX/Sponza/scene.gltf"); model.Transform.Position =newVector3(0.0f, 0.0f, 0.0f); model.Transform.Rotate(0.0f, -90f, 0.0f); model.Transform.Scale =newVector3(1.5f, 1.5f, 1.5f); model.Shader = _renderer.GetShaderProgram("MeshShader"); model.AddBehavior(newFlyCam()); _scene.AddGameElement("BASE_LAYER", model); _camera.LookAt(model.Transform.Position); // Create an 2D physics handler with zero gravity _physicsHandler =newPhysicsHandler3D(Vector3.Zero); _scene.PhysicsHandler = _physicsHandler; // Initialize game objects and resources here GFX.Instance.AssetManager.ForeachAsset<IMaterial>(material=> { material.Init(_renderer); }); // Initialize the scene _scene.Init(_window.GetViewport(), _renderer); }publicvoidRun() { // Main game loopwhile (!_window.RequestClose()) {varviewport= _window.GetViewport(); _window.ProcessEvents();Update();Render(viewport); }Dispose(); }publicvoidStop() { _window.Close(); }privatevoidUpdate() { // Update game logic here _scene.UpdatePhysics(); _scene.Update(); }privatevoidRender(Viewportviewport) { _camera.Transform.Scale =newVector3(viewport.Width, viewport.Height, 0.0f); // Clear the screen _renderer.MakeCurrent(); _renderer.ClearColor(0.0f, 0.0f, 0.0f, 1.0f); _renderer.Clear(RenderFlags.ClearFlags.Color | RenderFlags.ClearFlags.Depth); // Render game graphics here _scene.Render(viewport, _renderer, _camera); // Swap buffers _renderer.Flush(); _renderer.SwapBuffers(); // Check for errors Debug.WriteLine($"Render Error {_renderer.GetError()}"); }privatevoidDispose() { _scene.DisposeScene(_renderer); _renderer.Dispose(); } }}