Any chance of a GLFW port?

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

Previous topic - Next topic

Brucey

Cool.
If you want, we can end up having a "learnopengl" sub folder/s in examples with all of them there for future learners to reference :)
(assuming you make it that far! - as there are a few)

DruggedBunny

Yeah, I'll submit whatever I manage to get through anyway!

DruggedBunny

Hi Brucey, I get an error with the latest release when building the example, or my code (deleted .bmx folder), on Windows 7 64-bit:

[  9%] Processing:source.bmx
[ 11%] Processing:common.bmx
[ 13%] Processing:glad00.bmx
[ 14%] Processing:opengl.bmx
[ 16%] Processing:common.bmx
[ 26%] Processing:common.bmx
[ 27%] Processing:glfwmonitor.bmx
[ 29%] Processing:glfwopengl.bmx
Compile Error: Operator [] with 'Int' index is not defined for type 'SGLFWvidmode'
[d:/DevTools/Blitz/BlitzMaxNG/mod/glfw.mod/glfwmonitor.mod/glfwmonitor.bmx;126;0]
Build Error: failed to compile (-1) d:/DevTools/Blitz/BlitzMaxNG/mod/glfw.mod/glfwmonitor.mod/glfwmonitor.bmxThe terminal process terminated with exit code: 4294967295


Going back to previous version still builds properly.

therevills

Whats the advantage of using GLFW over the normal Blitz/NG drivers of OpenGL/DX?

DruggedBunny

My main reason: I've no idea how to use them!

That tutorial site uses GLFW so it's easy to get along with it.

And it's a popular system, so good to have.

Brucey

Oh, sorry James. You'll need the latest bcc... Assuming you are fairly up-to-date, that should be all you need. (otherwise, after updating bcc, you may also need to update brl...)
(I've been making enhancements/fixes to the Struct stuff, which lets you do much more with them than previously)


Brucey

@therevills

For now, GLFW is not going to be much used to you, unless you are happy to start doing some low-level coding - I've only implemented GLFW, and nothing much to integrate it with max2d etc.

But for what James wanted - to use the learnopengl stuff - it's an almost complete implementation. I've just got some joystick/gamepad stuff to finish, but everything else is done, I think.

At some point, I may consider using it as a "backend" for the standard BRL stuff - much in the way SDL works now.

DruggedBunny

That would be really cool, but I'll let you take a breather for now!  :D

I'll update and report back, cheers.

DruggedBunny

Hmm, I've spent about 2 hours trying to get latest bcc/brl/pub to build, but all to no avail! (Got bcc going, but a simple Print "Hello" just ends with (3)/BlitzMax/mod/brl.mod/max2d.mod/.bmx/max2d.bmx.release.win32.x64.c:915:12: error: incompatible types when assigning to type 'struct brl_color_SColor8' from type.)

I'll stick with the previous version for the moment, should do me fine for now!

DruggedBunny

#24
Here are my straight conversions of the official samples so far:

1.1 Hello Window



' https://learnopengl.com/

SuperStrict

Framework GLFW.GLFWWindow

Import GLFW.GLFW
Import GLFW.GLFWOpenGL
Import GLFW.GLFWSystem

Import BRL.StandardIO

Local app_name:String = "Hello Window"

Const SCR_WIDTH:UInt = 800
Const SCR_HEIGHT:UInt = 600

Type TGameWindow Extends TGLFWWindow

Method OnFrameBufferSize (width:Int, height:Int)
glViewport (0, 0, width, height)
EndMethod

EndType

Function ProcessInput (window:TGLFWWindow)

If window.IsKeyDown (GLFW_KEY_ESCAPE)
window.SetShouldClose (True)
EndIf

EndFunction

TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MAJOR, 3)
TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MINOR, 3)
TGLFWWindow.Hint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)

?MacOS ' Ewww...
TGLFWWindow.Hint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)
?

Local window:TGLFWWindow = New TGameWindow.Create (SCR_WIDTH, SCR_HEIGHT, app_name)

If Not window
Print "Failed to create GLFW window!"
End
EndIf

window.MakeContextCurrent ()

gladLoadGL (glfwGetProcAddress)

While Not window.ShouldClose ()
ProcessInput (window)
window.SwapBuffers ()
PollSystem ()
Wend

End



1.2 Hello Window Clear



' https://learnopengl.com/

SuperStrict

Framework GLFW.GLFWWindow

Import GLFW.GLFW
Import GLFW.GLFWOpenGL
Import GLFW.GLFWSystem

Import BRL.StandardIO

Local app_name:String = "Hello Window Clear"

Const SCR_WIDTH:UInt = 800
Const SCR_HEIGHT:UInt = 600

Type TGameWindow Extends TGLFWWindow

Method OnFrameBufferSize (width:Int, height:Int)
glViewport (0, 0, width, height)
EndMethod

EndType

Function ProcessInput (window:TGLFWWindow)

If window.IsKeyDown (GLFW_KEY_ESCAPE)
window.SetShouldClose (True)
EndIf

EndFunction

TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MAJOR, 3)
TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MINOR, 3)
TGLFWWindow.Hint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)

?MacOS ' Ewww...
TGLFWWindow.Hint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)
?

Local window:TGLFWWindow = New TGameWindow.Create (SCR_WIDTH, SCR_HEIGHT, app_name)

If Not window
Print "Failed to create GLFW window!"
End
EndIf

window.MakeContextCurrent ()

gladLoadGL (glfwGetProcAddress)

While Not window.ShouldClose ()

ProcessInput (window)

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

window.SwapBuffers ()
PollSystem ()

Wend

End


2.1 Hello Triangle:



' https://learnopengl.com/

SuperStrict

Framework GLFW.GLFWWindow

Import GLFW.GLFW
Import GLFW.GLFWOpenGL
Import GLFW.GLFWSystem

Import BRL.StandardIO

Local app_name:String = "Hello Triangle"

Const SCR_WIDTH:UInt = 800
Const SCR_HEIGHT:UInt = 600

Type TGameWindow Extends TGLFWWindow

Method OnFrameBufferSize (width:Int, height:Int)
glViewport (0, 0, width, height)
EndMethod

EndType

Function ProcessInput (window:TGLFWWindow)

If window.IsKeyDown (GLFW_KEY_ESCAPE)
window.SetShouldClose (True)
EndIf

EndFunction

TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MAJOR, 3)
TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MINOR, 3)
TGLFWWindow.Hint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)

?MacOS ' Ewww...
TGLFWWindow.Hint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)
?

Local window:TGLFWWindow = New TGameWindow.Create (SCR_WIDTH, SCR_HEIGHT, app_name)

If Not window
Print "Failed to create GLFW window!"
End
EndIf

window.MakeContextCurrent ()

gladLoadGL (glfwGetProcAddress)

Local vertexShaderSource:Byte Ptr
Local vertexShaderString:String

vertexShaderString :+ "#version 330 core~n"
vertexShaderString :+ "layout (location = 0) in vec3 aPos;~n"
vertexShaderString :+ "void main()~n"
vertexShaderString :+ "{~n"
vertexShaderString :+ "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);~n"
vertexShaderString :+ "}~n"

vertexShaderSource = vertexShaderString.ToCString ()

Local fragmentShaderSource:Byte Ptr
Local fragmentShaderString:String

fragmentShaderString :+ "#version 330 core~n"
fragmentShaderString :+ "out vec4 FragColor;~n"
fragmentShaderString :+ "void main()~n"
fragmentShaderString :+ "{~n"
fragmentShaderString :+ "   FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);~n"
fragmentShaderString :+ "}~n"

fragmentShaderSource = fragmentShaderString.ToCString ()

Local vertexShader:Int = glCreateShader (GL_VERTEX_SHADER)

glShaderSource (vertexShader, 1, Varptr vertexShaderSource, Null)
glCompileShader (vertexShader)

Local success:Int
Local infoLog:Byte [512]

glGetShaderiv (vertexShader, GL_COMPILE_STATUS, Varptr success)

If Not success
glGetShaderInfoLog (vertexShader, 512, Null, infoLog)
Print "Vertex shader compilation failed: " + String.FromCString (infoLog)
EndIf

Local fragmentShader:Int = glCreateShader (GL_FRAGMENT_SHADER)

glShaderSource (fragmentShader, 1, Varptr fragmentShaderSource, Null)
glCompileShader (fragmentShader)

glGetShaderiv (fragmentShader, GL_COMPILE_STATUS, Varptr success)

If Not success
glGetShaderInfoLog (fragmentShader, 512, Null, infoLog)
Print "Fragment shader compilation failed: " + String.FromCString (infoLog)
EndIf

Local shaderProgram:Int = glCreateProgram ()

glAttachShader (shaderProgram, vertexShader)
glAttachShader (shaderProgram, fragmentShader)
glLinkProgram (shaderProgram)

glGetProgramiv (shaderProgram, GL_LINK_STATUS, Varptr success)

If Not success
glGetProgramInfoLog (shaderProgram, 512, Null, infoLog)
Print "Shader program linking failed: " + String.FromCString (infoLog)
EndIf

glDeleteShader (vertexShader)
glDeleteShader (fragmentShader)

Local vertices:Float [] = [..
-0.5, -0.5, 0.0, ..
0.5, -0.5, 0.0, ..
0.0, 0.5, 0.0]

Local VBO:UInt
Local VAO:UInt

glGenVertexArrays (1, Varptr VAO)
glGenBuffers (1, Varptr VBO)
glBindVertexArray (VAO)

glBindBuffer (GL_ARRAY_BUFFER, VBO)
glBufferData (GL_ARRAY_BUFFER, vertices.length * SizeOf (0:Float), vertices, GL_STATIC_DRAW)

glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 3 * Sizeof (0:Float), 0:Byte Ptr)
glEnableVertexAttribArray (0)

glBindBuffer (GL_ARRAY_BUFFER, 0)

glBindVertexArray (0)

While Not window.ShouldClose ()

ProcessInput (window)

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

glUseProgram (shaderProgram)
glBindVertexArray (VAO)
glDrawArrays (GL_TRIANGLES, 0, 3)

window.SwapBuffers ()
PollSystem ()

Wend

glDeleteVertexArrays (1, Varptr VAO)
glDeleteBuffers (1, Varptr VBO)

End

DruggedBunny

#25
Fourth sample:

2.2 Hello Triangle Indexed



' https://learnopengl.com/

SuperStrict

Framework GLFW.GLFWWindow

Import GLFW.GLFW
Import GLFW.GLFWOpenGL
Import GLFW.GLFWSystem

Import BRL.StandardIO

Local app_name:String = "Hello Triangle Indexed"

Const SCR_WIDTH:UInt = 800
Const SCR_HEIGHT:UInt = 600

Type TGameWindow Extends TGLFWWindow

Method OnFrameBufferSize (width:Int, height:Int)
glViewport (0, 0, width, height)
EndMethod

EndType

Function ProcessInput (window:TGLFWWindow)

If window.IsKeyDown (GLFW_KEY_ESCAPE)
window.SetShouldClose (True)
EndIf

EndFunction

TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MAJOR, 3)
TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MINOR, 3)
TGLFWWindow.Hint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)

?MacOS ' Ewww...
TGLFWWindow.Hint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)
?

Local window:TGLFWWindow = New TGameWindow.Create (SCR_WIDTH, SCR_HEIGHT, app_name)

If Not window
Print "Failed to create GLFW window!"
End
EndIf

window.MakeContextCurrent ()

gladLoadGL (glfwGetProcAddress)

Local vertexShaderSource:Byte Ptr
Local vertexShaderString:String

vertexShaderString :+ "#version 330 core~n"
vertexShaderString :+ "layout (location = 0) in vec3 aPos;~n"
vertexShaderString :+ "void main()~n"
vertexShaderString :+ "{~n"
vertexShaderString :+ "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);~n"
vertexShaderString :+ "}~n"

vertexShaderSource = vertexShaderString.ToCString ()

Local fragmentShaderSource:Byte Ptr
Local fragmentShaderString:String

fragmentShaderString :+ "#version 330 core~n"
fragmentShaderString :+ "out vec4 FragColor;~n"
fragmentShaderString :+ "void main()~n"
fragmentShaderString :+ "{~n"
fragmentShaderString :+ "   FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);~n"
fragmentShaderString :+ "}~n"

fragmentShaderSource = fragmentShaderString.ToCString ()

Local vertexShader:Int = glCreateShader (GL_VERTEX_SHADER)

glShaderSource (vertexShader, 1, Varptr vertexShaderSource, Null)
glCompileShader (vertexShader)

Local success:Int
Local infoLog:Byte [512]

glGetShaderiv (vertexShader, GL_COMPILE_STATUS, Varptr success)

If Not success
glGetShaderInfoLog (vertexShader, 512, Null, infoLog)
Print "Vertex shader compilation failed: " + String.FromCString (infoLog)
EndIf

Local fragmentShader:Int = glCreateShader (GL_FRAGMENT_SHADER)

glShaderSource (fragmentShader, 1, Varptr fragmentShaderSource, Null)
glCompileShader (fragmentShader)

glGetShaderiv (fragmentShader, GL_COMPILE_STATUS, Varptr success)

If Not success
glGetShaderInfoLog (fragmentShader, 512, Null, infoLog)
Print "Fragment shader compilation failed: " + String.FromCString (infoLog)
EndIf

Local shaderProgram:Int = glCreateProgram ()

glAttachShader (shaderProgram, vertexShader)
glAttachShader (shaderProgram, fragmentShader)
glLinkProgram (shaderProgram)

glGetProgramiv (shaderProgram, GL_LINK_STATUS, Varptr success)

If Not success
glGetProgramInfoLog (shaderProgram, 512, Null, infoLog)
Print "Shader program linking failed: " + String.FromCString (infoLog)
EndIf

glDeleteShader (vertexShader)
glDeleteShader (fragmentShader)

Local vertices:Float [] = [..
0.5, 0.5, 0.0, ..
0.5, -0.5, 0.0, ..
-0.5, -0.5, 0.0, ..
-0.5, 0.5, 0.0]

Local indices:UInt [] = [..
0, 1, 3, ..
1, 2, 3]

Local VBO:UInt
Local VAO:UInt
Local EBO:UInt

glGenVertexArrays (1, Varptr VAO)
glGenBuffers (1, Varptr VBO)
glGenBuffers (1, Varptr EBO)

glBindVertexArray (VAO)

glBindBuffer (GL_ARRAY_BUFFER, VBO)
glBufferData (GL_ARRAY_BUFFER, vertices.length * SizeOf (0:Float), vertices, GL_STATIC_DRAW)

glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, EBO)
glBufferData (GL_ELEMENT_ARRAY_BUFFER, indices.length * SizeOf (0:Float), indices, GL_STATIC_DRAW)

glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 3 * Sizeof (0:Float), 0:Byte Ptr)
glEnableVertexAttribArray (0)

glBindBuffer (GL_ARRAY_BUFFER, 0)

glBindVertexArray (0)

While Not window.ShouldClose ()

ProcessInput (window)

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

glUseProgram (shaderProgram)
glBindVertexArray (VAO)
glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)

window.SwapBuffers ()
PollSystem ()

Wend

glDeleteVertexArrays (1, Varptr VAO)
glDeleteBuffers (1, Varptr VBO)
glDeleteBuffers (1, Varptr EBO)

End



DruggedBunny

#26
One more for tonight!

Note: Uses MilliSecs in place of glfwGetTime as that's not in yet. Subtle differences possible against other code as MilliSecs is based off computer start time, while glfwGetTime is based off GLFW's own initialisation.

I'll probably do the remaining 3.x samples and then call it quits, so I can get on and try to 'LearnOpenGL'!

Feel free to add these, Brucey, and thanks again! Awesome!  :D

3.1 Shaders Uniform



' https://learnopengl.com/

SuperStrict

Framework GLFW.GLFWWindow

Import GLFW.GLFW
Import GLFW.GLFWOpenGL
Import GLFW.GLFWSystem

Import BRL.StandardIO

Local app_name:String = "Shaders Uniform"

Const SCR_WIDTH:UInt = 800
Const SCR_HEIGHT:UInt = 600

Type TGameWindow Extends TGLFWWindow

Method OnFrameBufferSize (width:Int, height:Int)
glViewport (0, 0, width, height)
EndMethod

EndType

Function ProcessInput (window:TGLFWWindow)

If window.IsKeyDown (GLFW_KEY_ESCAPE)
window.SetShouldClose (True)
EndIf

EndFunction

TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MAJOR, 3)
TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MINOR, 3)
TGLFWWindow.Hint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)

?MacOS ' Ewww...
TGLFWWindow.Hint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)
?

Local window:TGLFWWindow = New TGameWindow.Create (SCR_WIDTH, SCR_HEIGHT, app_name)

If Not window
Print "Failed to create GLFW window!"
End
EndIf

window.MakeContextCurrent ()

gladLoadGL (glfwGetProcAddress)

Local vertexShaderSource:Byte Ptr
Local vertexShaderString:String

vertexShaderString :+ "#version 330 core~n"
vertexShaderString :+ "layout (location = 0) in vec3 aPos;~n"
vertexShaderString :+ "void main()~n"
vertexShaderString :+ "{~n"
vertexShaderString :+ "   gl_Position = vec4(aPos, 1.0);~n"
vertexShaderString :+ "}~n"

vertexShaderSource = vertexShaderString.ToCString ()

Local fragmentShaderSource:Byte Ptr
Local fragmentShaderString:String

fragmentShaderString :+ "#version 330 core~n"
fragmentShaderString :+ "out vec4 FragColor;~n"
fragmentShaderString :+ "uniform vec4 ourColor;~n"
fragmentShaderString :+ "void main()~n"
fragmentShaderString :+ "{~n"
fragmentShaderString :+ "   FragColor = ourColor;~n"
fragmentShaderString :+ "}~n"

fragmentShaderSource = fragmentShaderString.ToCString ()

Local vertexShader:Int = glCreateShader (GL_VERTEX_SHADER)

glShaderSource (vertexShader, 1, Varptr vertexShaderSource, Null)
glCompileShader (vertexShader)

Local success:Int
Local infoLog:Byte [512]

glGetShaderiv (vertexShader, GL_COMPILE_STATUS, Varptr success)

If Not success
glGetShaderInfoLog (vertexShader, 512, Null, infoLog)
Print "Vertex shader compilation failed: " + String.FromCString (infoLog)
EndIf

Local fragmentShader:Int = glCreateShader (GL_FRAGMENT_SHADER)

glShaderSource (fragmentShader, 1, Varptr fragmentShaderSource, Null)
glCompileShader (fragmentShader)

glGetShaderiv (fragmentShader, GL_COMPILE_STATUS, Varptr success)

If Not success
glGetShaderInfoLog (fragmentShader, 512, Null, infoLog)
Print "Fragment shader compilation failed: " + String.FromCString (infoLog)
EndIf

Local shaderProgram:Int = glCreateProgram ()

glAttachShader (shaderProgram, vertexShader)
glAttachShader (shaderProgram, fragmentShader)
glLinkProgram (shaderProgram)

glGetProgramiv (shaderProgram, GL_LINK_STATUS, Varptr success)

If Not success
glGetProgramInfoLog (shaderProgram, 512, Null, infoLog)
Print "Shader program linking failed: " + String.FromCString (infoLog)
EndIf

glDeleteShader (vertexShader)
glDeleteShader (fragmentShader)

Local vertices:Float [] = [..
0.5, -0.5, 0.0, ..
-0.5, -0.5, 0.0, ..
0.0, 0.5, 0.0]

Local VBO:UInt
Local VAO:UInt

glGenVertexArrays (1, Varptr VAO)
glGenBuffers (1, Varptr VBO)

glBindVertexArray (VAO)

glBindBuffer (GL_ARRAY_BUFFER, VBO)
glBufferData (GL_ARRAY_BUFFER, vertices.length * SizeOf (0:Float), vertices, GL_STATIC_DRAW)

glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 3 * Sizeof (0:Float), 0:Byte Ptr)
glEnableVertexAttribArray (0)

glBindVertexArray (VAO)

While Not window.ShouldClose ()

ProcessInput (window)

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

glUseProgram (shaderProgram)

Local timeValue:Float = MilliSecs ()
Local greenValue:Float = Sin (timeValue) / 2.0 + 0.5

Local vertexColorLocation:Int = glGetUniformLocation (shaderProgram, "ourColor")
glUniform4f (vertexColorLocation, 0.0, greenValue, 0.0, 1.0)

glDrawArrays (GL_TRIANGLES, 0, 3)

window.SwapBuffers ()
PollSystem ()

Wend

glDeleteVertexArrays (1, Varptr VAO)
glDeleteBuffers (1, Varptr VBO)

End



Derron

You know you could always have a

Global MillisecsOnStart:Int = Millisecs() 'or even just before your loop

and
Function MillisecsSinceStart:Int()
  Return Millisecs() - MillisecsOnStart
End Function



bye
Ron

DruggedBunny

Good call! I was only pointing it out, though, on the off-chance someone got different results with 3rd-party code -- MilliSecs will do me fine!

Brucey

I've added GetTime and SetTime functions.

I've changed glShaderSource() to take a BlitzMax String (and dropped the length arg), rather than have the user need to remember to free memory etc. It just makes it easier to use in the BlitzMax environment.

I've added your examples. Thanks! :o)