luca-piccioni / OpenGL.Net

Modern OpenGL bindings for C#.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issues after switching to current NuGet package

ericlass opened this issue · comments

Hi,
I've been using OpenGL.Net for a while now, without any issues. I'm creating the window and the context manually and all worked fine until today, I removed the pre-compiled OpenGL.Net assembly (compiled August 2015) and instead installed the NuGet package (version 0.4.0). Window and context creation still work fine, but Gl.GetString(StringName.Version) now returns NULL. Gl.GetError() says InvalidOperation, but already does so before calling GetString(), right after making the context the current. Here's my code:

// Find matching pixel format
PixelFormatDescriptor pfd = PixelFormatDescriptor.Build();
pfd.nVersion = 1;
pfd.dwFlags = WinApi.PFD_DOUBLEBUFFER | WinApi.PFD_SUPPORT_OPENGL | WinApi.PFD_DRAW_TO_WINDOW;
pfd.iPixelType = WinApi.PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 32;
pfd.cStencilBits = 8;
pfd.iLayerType = WinApi.PFD_MAIN_PLANE;

IntPtr dc = WinApi.GetDC(_hwnd);
int pixelFormat = WinApi.ChoosePixelFormat(dc, ref pfd);

if (pixelFormat == 0)
  throw new Exception("ChoosePixelFormat failed: " + Marshal.GetLastWin32Error());

//Set the new pixel format
if (!WinApi.SetPixelFormat(dc, pixelFormat, ref pfd))
  throw new Exception("SetPixelFormat failed: " + Marshal.GetLastWin32Error());

//Create temporary GL context
IntPtr tempRc = Wgl.CreateContext(dc);
if (tempRc == IntPtr.Zero)
  throw new Exception("CreateContext failed: " + Marshal.GetLastWin32Error());

if (!Wgl.MakeCurrent(dc, tempRc))
  throw new Exception("MakeCurrent failed: " + Marshal.GetLastWin32Error());

//Get Gl version the old way first
string versionStr = Gl.GetString(StringName.Version);

As written before, all checks pass successful, but Gl.GetString() still returns NULL. Do you have any ideas what could cause this? Using the pre-compiled assembly, the above code works perfectly fine. The issue is only with the NuGet package.

Thanks and regards,
Eric

From version 0.4 the OpenGL procedure loading has changed to have more clean DeviceContext implementation.

To solve your issue, just call Gl.BindAPI() after you Wgl.MakeCurrent call. I'm pretty sure that it will solve the problem. Alternatively, you can use DeviceContext abstraction.

The reason behind it is that OpenGL method pointers are loaded by calling Gl.BindAPI(); in previous versions this method was executed automatically by Wgl.MakeCurrent, while now you have to do it manually. Sorry, I promised to document the BindAPI methods, but I didn't it yet.


Additionally, be aware of Gl.Initialize(); if you construct the OpenGL context yourself, I suggest to call this method before any of your code.

Works perfectly when calling both Initialize() and BindAPI(). Thanks for quick help. And also for the library. ;)