Any chance of a GLFW port?

Started by DruggedBunny, March 10, 2020, 02:04:32

Previous topic - Next topic

DruggedBunny

Hi Brucey,

Just wondering if you've looked at GLFW and how easy/difficult it would be to port?

I've been playing with this site's tutorials in Visual Studio, but would much rather be doing it in Blitz!

But I can't for the life of me figure out the basics of setting up an OpenGL window in Max with the right context version/profile, totally confused by the potential mixes of GLEW, GLAD, SDL, GLGraphics, etc, etc and I've been trawling through the mod sources for hours trying to figure it out, to no avail!

I'm basically hoping to be able to implement the window setup here using GLFW:

https://learnopengl.com/Getting-started/Hello-Window

I don't know if you're big on new library requests, but if you don't ask... !

Failing that, does anyone have a simple example that would implement a version 3.3 core profile??

Thanks for reading,
James Boyd/DruggedBunny.

Brucey

Hi James :)

You may find this interesting : https://github.com/bmx-ng/ray.mod

It is a binding for raylib, which actually uses GLFW for windowing and events, etc.

Okay, so it's not directly using it in actual BlitzMax code.. so it's a bit of a cheat. However, I guess it proves it wouldn't be a giant leap to implement it properly.

DruggedBunny

Oh, cool, thanks, Brucey!

I'll have a look tonight to see if I can see how it's been done, but if you find yourself bored one day... !

I know GLAD is in there already, so it would make NG pretty darn handy for use with LearnOpenGL.

Thanks again.

Brucey

If I was going to do it, I'd be more inclined to make it more "BlitzMaxy", so rather than have to pass a window handle into all the window functions, there'd be a Type that hides that...

Although if you really wanted to stay true to the examples, you could still work with the "raw" functions.

DruggedBunny

No complaints from me, Brucey, if you find yourself with time on your hands... ;)

I just want a nice easy way to set up windows/contexts with particular feature sets of OpenGL (eg. 3.3 in the tutorial, with Core Profile) so I can play with the actual GL code, and this was nice and easy for me to grasp in C++ with GLFW, but it's just... no fun!

Thanks for considering, appreciated!

Brucey

Would something like this work for you?

SuperStrict

Framework GLFW.GLFWWindow
Import GLFW.GLFWOpenGL

' settings
Const SCR_WIDTH:Int = 800
Const SCR_HEIGHT:Int = 600

' glfw: initialize and configure
TGLFWWindow.Hint(GLFW_CONTEXT_VERSION_MAJOR, 3)
TGLFWWindow.Hint(GLFW_CONTEXT_VERSION_MINOR, 3)
TGLFWWindow.Hint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)

' glfw window creation
Local window:TGLFWWindow = New TMyWindow.Create(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL")

If Not window Then
Throw "Failed to create GLFW window"
End If


window.MakeContextCurrent()

' glad: load all OpenGL function pointers
gladLoadGL(glfwGetProcAddress)

' render loop
While Not window.ShouldClose()

' input
ProcessInput(window)

' render
glClearColor(0.2, 0.3, 0.3, 1.0)
glClear(GL_COLOR_BUFFER_BIT)

' glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
window.SwapBuffers()
PollSystem

Wend

' process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
Function ProcessInput(window:TGLFWWindow)
If window.IsKeyDown(GLFW_KEY_ESCAPE) Then
window.SetShouldClose(True)
End If
End Function

Type TMyWindow Extends TGLFWWindow

' glfw: whenever the window size changed (by OS or user resize) this callback function executes
Method OnFramebufferSize(width:Int, height:Int) Override

' make sure the viewport matches the new window dimensions; note that width and
' height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height)
End Method

End Type


It's a bit rough around the edges at the moment - for example, I'm still toying with having the "load gl" bit done automagically somewhere...

The window handles all the callbacks, so you just override the ones you are interested in...

DruggedBunny

Yeah, that looks great to me!

It is relatively low-level stuff, especially since half the point is to get into 'raw' OpenGL, so I don't think it's too much of a hardship to have to call the one-line GLAD loader, though I get where you're coming from.

Look forward to trying it -- I'll basically be going through the tutorial (admittedly rather slowly), so should get to give it a good test anyway.

Brucey

Evening  8)

See how you get on with : https://github.com/bmx-ng/glfw.mod

There is one example - which is basically the first learnOpenGL example.
I've used the default GLAD header from which to generate the sources from. If that doesn't cover everything you need, let me know and I'll see about generating a new header with a higher OpenGL level.

It's only been tested on Win32 64-bit so far, so YMMV anywhere else.

DruggedBunny

Wow, Brucey, I'm sure I've said it a fair few times, but you are truly a god among men! I only posted thinking you might add it to a list of possibles, if I was lucky!

The sample works here (I'm on Windows 7 64-bit anyway), so I'll dig in over the next few days and see if I can implement a few more samples, and try keep them in order to add to the mod examples.

I'll buy you a few pints at the end of the month... thank you so much for this!

Brucey

Heh, glad I could be useful :)

It's all gone rather smoothly so far. No major hiccups as yet - although I should probably get around to trying it on some of the other platforms at some point.
I'll get the rest of the apis implemented and document it a bit more too.


And as it happens, we are doing a special on github sponsorship at the moment, available from only $1 a month. But no worries either way ;)

Have fun!

Derron

Quote from: Brucey on March 12, 2020, 06:21:35
And as it happens, we are doing a special on github sponsorship at the moment, available from only $1 a month. But no worries either way ;)

Dunno how long (it is for "one year") but the next few months github will double what you sponsor. So my fiver becomes a tenner.


Will try the sample on linux when back at home.


bye
Ron

Brucey

As I say, I haven't tried it on any other targets yet, and usually it requires a few tweaks to get things sorted.... don't hold your breath.

DruggedBunny

Ha, didn't even know GitHub sponsorship was a thing! I'll look into it!

Brucey

It now builds and runs on 64-bit Linux (Ubuntu).

DruggedBunny

Nice!

Just to show I'm doing something with this, I've started converting some of the GitHub samples.

Done 1.1 and 1.2, which basically puts me at your sample! However, I'm attempting to implement each from scratch myself, just referencing your sample to get the hang of the differences.

I'll do a few more tonight/tomorrow (oh, and will look at patronage) and post somewhere, then move on to the tutorial proper, which I was trying to do in C++ without reference to the 'answers' (ie. final source) so as to better understand what was going on.

It's ten to six, so I'm going back to bed...

Thanks again, Brucey.