2
\$\begingroup\$

I have some old code that i imported into an OpenTK C# application running on NET 5.0. The rendering uses old fashioned immediate mode and it works on my current computer but nothing renders in the new application. The old application was NET framework 4.7.2 and an older version of OpenTK. When creating the window i request the same OpenGL version as the older application uses.

Did OpenTK remove this functionality in the newer version?

This is code i used to test this:

using OpenTK.Graphics.OpenGL;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
using System;
using OpenTK.Mathematics;

namespace tktest2
{
    public class Window : GameWindow
    {
        private float ClipNear = 0.2f;
        private float ClipFar = 10000.0f;
        public Matrix4 ProjectionMatrix;
        public Matrix4 ViewMatrix;

        public Window(GameWindowSettings gameWindowSettings, NativeWindowSettings nativeWindowSettings)
            : base(gameWindowSettings, nativeWindowSettings)
        {
        }

        protected override void OnLoad()
        {
            base.OnLoad();

            Console.WriteLine(GL.GetString(StringName.Vendor));
            Console.WriteLine(GL.GetString(StringName.Renderer));
            Console.WriteLine(GL.GetString(StringName.Version));
            Console.WriteLine(GL.GetString(StringName.ShadingLanguageVersion));

            GL.Enable(EnableCap.DepthTest);
            GL.Disable(EnableCap.Texture2D);
            GL.Disable(EnableCap.CullFace);
            GL.Disable(EnableCap.Blend);

            this.VSync = VSyncMode.On;
        }

        protected override void OnUnload()
        {
            base.OnUnload();
        }

        protected override void OnResize(ResizeEventArgs e)
        {
            base.OnResize(e);

            ProjectionMatrix = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, (float)Size.X / (float)Size.Y, ClipNear, ClipFar);
            GL.Viewport(0, 0, Size.X, Size.Y);
        }

        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            ViewMatrix = Matrix4.LookAt(new Vector3(0, -5, 0), new Vector3(0, 0, 0), Vector3.UnitZ);
            GL.Color4(1.0f, 1.0f, 1.0f, 1.0f);
            GL.Normal3(Vector3.UnitZ);

            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref ProjectionMatrix);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref ViewMatrix);

            GL.PointSize(10);
            GL.Begin(BeginMode.Points);
            for (int x = 0; x < 10; x++)
            {
                GL.Color3(1.0f, 1.0f, x / 10.0f);
                GL.Vertex3(x / 10.0f, 0.0f, 0.0f);
            }
            GL.End();

            SwapBuffers();
        }
    }
}

\$\endgroup\$
0

1 Answer 1

4
\$\begingroup\$

OpenTK is discouraging the use of immediate mode. But it is still supported. To use it, you need to set NativeWindowSettings.Profile to ContextProfile.Compatability. That is, you want an entry point that looks something like this:

using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;

namespace tktest2
{
    internal static class Program
    {
        static void Main()
        {
            var gameSettings = GameWindowSettings.Default;
            var nativeSettings = NativeWindowSettings.Default;
            nativeSettings.Profile = ContextProfile.Compatability;
            using (var window = new Window(gameSettings, nativeSettings))
            {
                window.Run();
            }
        }
    }
}

I have tested this on Windows 10, .NET 6, with the OpenTK Nuget version 4.6.7, on Visual Studio 2022. This is the output I got with your code:

OpenTK Window, with black background. An horizontal bar is visible on the center, slightly to the right. The bar has a gradient from yellow on the left, to white on the right.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for your answer. Adding that flag does not work for me unfortunately, i see the same in the version and renderer strings. I created a net framework version with the same code and it works there, with a lower openTK version as the newer ones are for net core. I decided to rewrite my code so it's not that relevant anymore but it's still a bit puzzling to me.. \$\endgroup\$
    – Rick P
    Jan 12, 2022 at 10:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .