luca-piccioni / OpenGL.Net

Modern OpenGL bindings for C#.
MIT License
568 stars 108 forks source link

Opengl.Net.Objects in WPF not working? #85

Closed riegaz closed 6 years ago

riegaz commented 6 years ago

How to use OpenGL.Net.Objects in a WPF application?

I get an error when calling: _Context = new GraphicsContext(e.DeviceContext, e.RenderContext); e in the WPF example comes from GlControlEventArgs instead of GLSurfaceViewEventArgs.

Calling VertexArrays.CreateSphere(1f, 5, 5); results in an exception:

System.TypeLoadException: 'Could not load type 'OpenGL.BufferTargetARB' from assembly 'OpenGL.Net, Version=0.7.1.0, Culture=neutral, PublicKeyToken=null'.'

I would like to take advantage of the sphere and font objects.

luca-piccioni commented 6 years ago

Do you use OpenGL.Net.Objects via nuget? If so, remove nuget package, and compile the OpenGL.Net.Objects from the current master.

riegaz commented 6 years ago

Thanks, that did the job!

luca-piccioni commented 6 years ago

I'd like to note that you are constructing a new GL context, while the WinForms control already creates it. You're actually sharing resources with wglShareLists with the GL control.

If you didn't intend this behavior, use the other constructor that simply wrap the GL context handle.

riegaz commented 6 years ago

I can't find the other constructor that takes the GL context only. How is it called?

luca-piccioni commented 6 years ago

Sorry, my fault for not recognizing e.RenderContext type: you're calling the right override. Cheers.

riegaz commented 6 years ago

Thanks!

riegaz commented 6 years ago

I'm running into a problem here:

protected override KeyValuePair<SceneGraphSorter, List<SceneObjectBatch>>[] Split(List<SceneObjectBatch> objects)

The object's property Program is null for me which leads to an exception while this is executed:

if (programLists.TryGetValue(objectBatch.Program, out programList))

Am I doing something wrong?

luca-piccioni commented 6 years ago

When Program is null, it means that your drawing using the fixed pipeline, which is quite odd nowadays.

Never tried using fixed pipeline, indeed I need to setup a special key value for sorting objectsnusing the fixed pipeline (good catch).

Assign a shader program to your geometry, or disable state sorting when defining the scene graph.

riegaz commented 6 years ago

I just tried to run the HelloObjects example within WPF. I just copied the code. Now I removed state sorting. Now I run into another problem.

System.MissingMethodException: 'Method not found: 'Void OpenGL.Gl.BufferStorage(OpenGL.BufferTarget, UInt32, IntPtr, OpenGL.MapBufferUsageMask)'.' I tried to get OpenGL.NET via master instead of nuget but then I get an enumeration error in opengl.

Do you have any idea?

riegaz commented 6 years ago

Enumeration error occurs when I try to run HelloObjects_net461 from the master as well.

luca-piccioni commented 6 years ago

I get a flawless executaion of HelloObjects on my machine. Can you put here some details about the exception?

I suggest to work exclusively on master,since current sources may be misaligned wrt the nuget compiled library.

riegaz commented 6 years ago

image

Do you need further information?

luca-piccioni commented 6 years ago

Stack trace?

riegaz commented 6 years ago

image

luca-piccioni commented 6 years ago

As I suspected.

The exception is user-handled, and you should continue debugging ignoring exceptions in that cases. I suggest to configure your debbugger to avoid breaks on user-handled exceptions (just to avoid annoying debugger breaks).

What's happening? Well, at initialization time, the library get all the implementation limits. So the GL driver has hard time to support all those GL limits. Most driver has at least one limit not supported, even if it should (as GL specification says).

riegaz commented 6 years ago

Thanks for your detailed explanation. One more question, it is not related at all. I'm new to opengl and calling GL.Frustum with certain values raises an exception:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

For example:

Gl.Frustum(-5,5,-5,5,1,1000);

Could you explain why in just one sentence?

luca-piccioni commented 6 years ago

Read the first paragraph of https://github.com/luca-piccioni/OpenGL.Net/wiki/GL-%5C-Common-mistakes.

This issue is very specific to this project, since it merge in a single API desktop and mobile standard.

luca-piccioni commented 6 years ago

In one sentence: wrong overload. Use the one with double-precision floating-point arguments.