Request for concise example using LWJGL in an SWT context

Started by Automaton, September 24, 2015, 20:19:46

Previous topic - Next topic

Automaton

After several days of googling around and working through many examples I've been unsatisfied with my progress at trying to implement fairly basic stuff using modern OpenGL in an SWT GLCanvas context. While this is largely due to my lack of experience with OpenGL (obviously the learning curve is fairly steep), I have had some trouble finding LWJGL + SWT examples which use modern OpenGL (and by "modern" I mean leveraging shaders, VBOs, VAOs, etc..., not necessarily the most recent versions of OpenGL). At first blush I feel like there should be almost no hurdle when migrating an example using a GLFW context to using an SWT context instead, but I keep hitting snags and this is preventing me from learning and exploring OpenGL further.

I was hoping someone in this community would be willing help me get my feet off the ground by providing a concise example which achieves the following:

  • A single SWT Composite which contains a GLCanvas for viewing a rotating cube with sides of differing colors
  • Using shaders, VBOs, VAOs, etc..
  • No deprecated OpenGL functions
  • As few supporting or convenience classes as possible; I'm basically looking for the procedure, I can abstract things as needed once I understand the process

Thanks!

Kai

With LWJGL 2.x this snippet is great to learn how LWJGL and SWT work together.

With LWJGL 3 it is just as easy: Just call `GL.createCapabilities()` on each thread which you want to render in with LWJGL after having called the SWT method `GLCanvas.setCurrent()` to really make the native SWT-managed OpenGL context current.
The GL.createCapabilities() call is just necessary for LWJGL to query function pointers of OpenGL API functions and to check the existence of certain extensions from the OpenGL context.

If you want to see "modern" OpenGL demos (though without using SWT, but with GLFW-managed windows/contexts; however the above mentioned code snippet shows how to use SWT) see the lwjgl3-demos repository.

Kai

Just for you I augmented the lwjgl3-demos repository by another demo, that uses SWT, VBOs and simple shaders.
That should give you a good start to see how LWJGL3 and SWT can play together nicely and how to use "modern" OpenGL components.

See the demo here: https://github.com/LWJGL/lwjgl3-demos/blob/master/src/org/lwjgl/demo/opengl/swt/SwtDemo.java

Cheers,
Kai

Automaton

Thanks Kai!

I got an exception on line 41:
glClearColor(0.3f, 0.5f, 0.8f, 1.0f);


The error was "There is no OpenGL context current in the current thread."

To fix this I added
GLContext.createFromCurrent();
before
GL.createCapabilities(true);
. Also, you'll notice I needed to add a boolean to the createCapabilities method. I'm guessing this is 'cause I'm on the last stable build of LWJGL and you're on a more current build, is this also why I got the exception?

Unfortunately after fixing both of those issues I just see a window with your pretty blue clear color. Thoughts?

Kai

Please use the latest nightly build. Those are as stable as the "stable" version and mostly receive recent fixes. And additionally, with the nightly versions you always stay close to the API as they will be when the final release of 3.0 happens. :)
Also please checkout the latest update to the demo. I used a debug context. Maybe that will print out some GL errors.

EDIT: Ah, that was probably a mistake in the vertex shader. Surprising how lenient and forgiving and non-standard-conformant Nvidia drivers can be...

Automaton

Yup, works great now that the shader is updated assuming it should look something like this (square spinning, as you've defined in code and shader):



Thanks Kai, this is gonna get me a long way!

Cornix

Quote from: Kai on September 24, 2015, 22:29:16
Please use the latest nightly build. Those are as stable as the "stable" version and mostly receive recent fixes. And additionally, with the nightly versions you always stay close to the API as they will be when the final release of 3.0 happens. :)
Also please checkout the latest update to the demo. I used a debug context. Maybe that will print out some GL errors.

EDIT: Ah, that was probably a mistake in the vertex shader. Surprising how lenient and forgiving and non-standard-conformant Nvidia drivers can be...
Never trust a driver.
My own drivers were very open minded when I first started writing shaders. Unfortunately the drivers of other people I shared my programs with disagreed on what was correct GLSL...

Automaton

So here's the next question: I've got JOML rocking pretty good, starting to get a hang on how to make shaders, and have gotten my rotating cube with different colored sides working, but I had to specify all the triangles manually in the correct order which is critical to achieve proper rendering. Are there any libraries out there that play well with LWJGL 3 for creating some primitive geometries (i.e. cubes, spheres, etc...)?

Kai

Quote from: Automaton on September 25, 2015, 21:26:16
Are there any libraries out there that play well with LWJGL 3 for creating some primitive geometries (i.e. cubes, spheres, etc...)?
None that I know of. And that's probably because it would raise a ton of design decisions on how to go about doing that.
Provided you want to build a sphere. Then...
- should immediate mode be used to render it (like with GLU) or should the vertices be packed in a buffer object?
- if the latter, should only vertex positions be written or also normals and texture coordinates, etc.?
- if texture coordinates, which projection should be used?
- should those vertex attribute be in an interleaved buffer or in multiple separate buffers?
- which winding order should the vertices be defined in?
- which exact topology should the sphere have? subdivided dodecahedra or longitude/latitude subdivision with two poles?

All those potential requirements make it hard to really suit a sufficiently large set of particular use cases.
But you could of course start another LWJGL companion library trying to attack this problem. :)

Automaton

Thanks for the response, Kai.

Yea I realized the problem moments after I asked the question. It would definitely be a difficult thing to do in an extensible way. If I come up with something clever I'll let you know ;p.