Ooops
October 30, 2020, 10:20:29 PM

Author Topic: Any chance of a GLFW port?  (Read 6729 times)

Offline DruggedBunny

  • Jr. Member
  • **
  • Posts: 60
Re: Any chance of a GLFW port?
« Reply #30 on: March 19, 2020, 01:20:58 AM »
Cool... all working for me here! Thank you.

Offline DruggedBunny

  • Jr. Member
  • **
  • Posts: 60
Re: Any chance of a GLFW port?
« Reply #31 on: March 20, 2020, 12:49:46 AM »
Last one before the GLFW samples get into separation of shader files, reworking everything into classes, etc.

We finally get the interpolated RGB triangle!

3.2 Shaders Interpolation

Code: [Select]

' https://learnopengl.com/

SuperStrict

Framework GLFW.GLFWWindow

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

Import BRL.StandardIO

Local app_name:String = "Shaders Interpolation"

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:String

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

Local vertexShader:Int = glCreateShader (GL_VERTEX_SHADER)

glShaderSource (vertexShader, 1, vertexShaderSource)
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 fragmentShaderSource:String

fragmentShaderSource :+ "#version 330 core~n"
fragmentShaderSource :+ "out vec4 FragColor;~n"
fragmentShaderSource :+ "in vec3 ourColor;~n"
fragmentShaderSource :+ "void main()~n"
fragmentShaderSource :+ "{~n"
fragmentShaderSource :+ "   FragColor = vec4(ourColor, 1.0f);~n"
fragmentShaderSource :+ "}~n"

Local fragmentShader:Int = glCreateShader (GL_FRAGMENT_SHADER)

glShaderSource (fragmentShader, 1, fragmentShaderSource)
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, 1.0, 0.0, 0.0, ..
-0.5, -0.5, 0.0, 0.0, 1.0, 0.0, ..
0.0, 0.5, 0.0, 0.0, 0.0, 1.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, 6 * Sizeof (0:Float), 0:Byte Ptr)
glEnableVertexAttribArray (0)

Local attribute_offset:Int = 3 * Sizeof (0:Float)

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

glUseProgram (shaderProgram)

While Not window.ShouldClose ()

ProcessInput (window)

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

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

window.SwapBuffers ()
PollSystem ()

Wend

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

End


Offline medi71

  • Jr. Member
  • **
  • Posts: 28
Re: Any chance of a GLFW port?
« Reply #32 on: April 17, 2020, 01:46:21 AM »
I like this glfw.mod. I have some codes in glfw3 which I like to convert to Blitzmax. I downloaded glfw.mod, but it seems bmk makemods -a glfw.mod is not the way. Can someone help me how to use glfw.mod?

Offline DruggedBunny

  • Jr. Member
  • **
  • Posts: 60
Re: Any chance of a GLFW port?
« Reply #33 on: April 18, 2020, 04:46:53 PM »
Are you using bmx-ng? If you place the mod folder at BlitzMaxNG\mod\glfw.mod it should just build by itself when you run one of the examples.

Note that you'll need the latest bmx-ng, from blitzmax.org if you don't have it.

Offline medi71

  • Jr. Member
  • **
  • Posts: 28
Re: Any chance of a GLFW port?
« Reply #34 on: April 18, 2020, 06:10:17 PM »
I use NG because of its clear editor. It worked, thanks.

Offline Brucey

  • Full Member
  • ***
  • Posts: 136
Re: Any chance of a GLFW port?
« Reply #35 on: April 20, 2020, 03:50:47 PM »
I like this glfw.mod. I have some codes in glfw3 which I like to convert to Blitzmax. I downloaded glfw.mod, but it seems bmk makemods -a glfw.mod is not the way. Can someone help me how to use glfw.mod?

FYI, the correct command would be
Code: [Select]
bmk makemods -a glfwWithout the ".mod" part.

Offline medi71

  • Jr. Member
  • **
  • Posts: 28
Re: Any chance of a GLFW port?
« Reply #36 on: April 20, 2020, 04:35:41 PM »
The way DruggedBunny said, worked for me. Also, yes, .mod shouldn't be there. I could manually install on an old laptop the way you said, the old way. All good. 
I may have a question about reading shader source file. I am working on the 2.1.hello_triangle. I am trying to make it work with two external shader files. So far, reading seems good, but I get:" Shader program linking failed: "  without any explanation, I mean nothing in infolog. If I get nowhere, I will paste my code tonight. Thanks.

Offline medi71

  • Jr. Member
  • **
  • Posts: 28
Re: Any chance of a GLFW port?
« Reply #37 on: April 20, 2020, 08:06:32 PM »
This works for reading the shader file in the example:
Code: [Select]
Function readShaderFile:String(file:String)
Local shaderSource:String
Local lineFromFile:String
Local in:TStream = OpenStream(file)
While Not Eof(in)
lineFromFile = ReadLine(in)
shaderSource :+ lineFromFile + "~n"
Wend
CloseStream(in)
Return shaderSource
End Function

Offline medi71

  • Jr. Member
  • **
  • Posts: 28
Re: Any chance of a GLFW port?
« Reply #38 on: April 21, 2020, 10:07:39 PM »
Very nice to see a shader class in glfw mod folder. I haven't use it, good to have it. The Camera class helps me to see how BRL.Matrix is being used. About the Mesh class, is it to be used for a model loader?
I like this work, if the glfw mod project needs donation to be alive, I can begin with little, later more.
I think BlitzMax need a morale boost because of its previous disappointments. Good working modules and examples help a lot. It is sad to see it fade away and things like Python becomes international!!

Offline DruggedBunny

  • Jr. Member
  • **
  • Posts: 60
Re: Any chance of a GLFW port?
« Reply #39 on: April 22, 2020, 03:21:06 PM »
Hi all, wondering if a grown-up could have a look at this! I've been stuck for about a week, totally confused as to why I need vastly different settings to the tutorials I've been reading in order to render my cube!

I've tried the original C code and that works fine with the original settings, but in my version I have to use different camera position and far values.

No doubt I'm doing something stupid, but I want to try and get this working correctly before going any further...

This will show nothing at first -- enable the HACK const to use the 'working' settings.

Main difference is I'm not using a texture, but vertex colours instead... but I think I have my strides and offsets correct!

I've attached the runnable code, with copy below, if anyone's feeling helpful!  :D

shaders/fragment.glsl:
Code: [Select]
#version 330 core

out vec4 FragColor;
in vec3 ourColor;

void main()
{
   FragColor = vec4(ourColor, 1.0f);
}

shaders/vertex.glsl:
Code: [Select]

#version 330 core

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;

uniform mat4 projection;
uniform mat4 camera;

out vec3 ourColor;

void main()
{
gl_Position = projection * camera * vec4(aPos, 1.0);
ourColor = aColor;
}


Main code:

Code: [Select]

SuperStrict

Framework GLFW.GLFWWindow

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

Import BRL.StandardIO
Import BRL.LinkedList

Import BRL.Matrix
Import BRL.Quaternion
Import BRL.Vector

' CAMERA STUFF

Local view_matrix:SMat4F
Local projection_matrix:SMat4F

' -----------------------------------------------------------------------------
Const HACK:Int = False ' H A C K    M E !
' -----------------------------------------------------------------------------

Local near:Float
Local far:Float
Local fov:Float
Local position:SVec3F
Local tri_scale:Float

If Not HACK ' Settings used in tutorials
near = 0.1
far = 10.0
fov = Radians (50.0)
position = New SVec3F (3.0, 3.0, 3.0)
tri_scale = 1.0
Else ' Settings that render something!
near = 0.1
far = 250.0
fov = Radians (50.0)
position = New SVec3F (3.5, 3.5, 3.5)
tri_scale = 0.5
EndIf

' https://learnopengl.com/Getting-started/Transformations
' https://www.tomdalling.com/blog/modern-opengl/03-matrices-depth-buffering-animation/
' https://github.com/tomdalling/opengl-series/blob/master/source/03_matrices/source/main.cpp

Local TMP_BasicCube:Float [] = [..
..
..
.. ' x y z
-tri_scale, tri_scale, tri_scale, 1.0, 0.0, 0.0, .. ' Near - Red
tri_scale, -tri_scale, tri_scale, 1.0, 0.0, 0.0, ..
-tri_scale, -tri_scale, tri_scale, 1.0, 0.0, 0.0, ..

-tri_scale, tri_scale, tri_scale, 0.5, 0.0, 0.0, ..
tri_scale, tri_scale, tri_scale, 0.5, 0.0, 0.0, ..
tri_scale, -tri_scale, tri_scale, 0.5, 0.0, 0.0, ..

tri_scale, tri_scale, -tri_scale, 0.0, 1.0, 0.0, .. ' Far - Green
-tri_scale, -tri_scale, -tri_scale, 0.0, 1.0, 0.0, ..
tri_scale, -tri_scale, -tri_scale, 0.0, 1.0, 0.0, ..

tri_scale, tri_scale, -tri_scale, 0.0, 0.5, 0.0, ..
-tri_scale, tri_scale, -tri_scale, 0.0, 0.5, 0.0, ..
-tri_scale, -tri_scale, -tri_scale, 0.0, 0.5, 0.0, ..

-tri_scale, tri_scale, -tri_scale, 0.0, 0.0, 1.0, .. ' Left - Blue
-tri_scale, -tri_scale, tri_scale, 0.0, 0.0, 1.0, ..
-tri_scale, -tri_scale, -tri_scale, 0.0, 0.0, 1.0, ..
'
-tri_scale, tri_scale, -tri_scale, 0.0, 0.0, 0.5, ..
-tri_scale, tri_scale, tri_scale, 0.0, 0.0, 0.5, ..
-tri_scale, -tri_scale, tri_scale, 0.0, 0.0, 0.5, ..

tri_scale, tri_scale, tri_scale, 1.0, 1.0, 0.0, .. ' Right - Yellow
tri_scale, -tri_scale, -tri_scale, 1.0, 1.0, 0.0, ..
tri_scale, -tri_scale, tri_scale, 1.0, 1.0, 0.0, ..
'
tri_scale, tri_scale, tri_scale, 0.5, 0.5, 0.0, ..
tri_scale, tri_scale, -tri_scale, 0.5, 0.5, 0.0, ..
tri_scale, -tri_scale, -tri_scale, 0.5, 0.5, 0.0, ..

-tri_scale, tri_scale, -tri_scale, 0.0, 1.0, 1.0, .. ' Top - Cyan
tri_scale, tri_scale, tri_scale, 0.0, 1.0, 1.0, ..
-tri_scale, tri_scale, tri_scale, 0.0, 1.0, 1.0, ..

-tri_scale, tri_scale, -tri_scale, 0.0, 0.5, 0.5, ..
tri_scale, tri_scale, -tri_scale, 0.0, 0.5, 0.5, ..
tri_scale, tri_scale, tri_scale, 0.0, 0.5, 0.5, ..

-tri_scale, -tri_scale, tri_scale, 1.0, 0.0, 1.0, .. ' Bottom - Magenta
tri_scale, -tri_scale, -tri_scale, 1.0, 0.0, 1.0, ..
-tri_scale, -tri_scale, -tri_scale, 1.0, 0.0, 1.0, ..

-tri_scale, -tri_scale, tri_scale, 0.5, 0.0, 0.5, ..
tri_scale, -tri_scale, tri_scale, 0.5, 0.0, 0.5, ..
tri_scale, -tri_scale, -tri_scale, 0.5, 0.0, 0.5]

' Demo window control...

Global AppWindow:TGLFWWindow ' Application window

Global _RetroGraphicsWidth:Int = 800 ' Display width
Global _RetroGraphicsHeight:Int = 600 ' Display height

'Retro3D (UInt (_RetroGraphicsWidth), UInt (_RetroGraphicsHeight))

'TGLFWWindow.Hint (GLFW_SAMPLES, msaa_level)
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 monitor:TGLFWMonitor = Null

'If full_screen Then monitor = TGLFWMonitor.GetPrimaryMonitor ()

AppWindow = New RetroGameWindow.Create (_RetroGraphicsWidth, _RetroGraphicsHeight, "Demo", monitor)

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

AppWindow.MakeContextCurrent ()

gladLoadGL (glfwGetProcAddress)

' DEPTH TESTING

glEnable (GL_DEPTH_TEST)
glDepthFunc (GL_LESS)

AppWindow.GetFrameBufferSize (_RetroGraphicsWidth, _RetroGraphicsHeight)

' SHADER PROGRAM

Local program:Int

' MESH

Local vertex_data:Float [] ' Mesh vertices (3D points)
Local vao:UInt, vbo:UInt ' VAO and VBO
Local triangle_count:Int ' Pre-calc number of triangles
Local vertex_count:Int ' Pre-calc number of vertices

' ASSIGN CUBE DATA TO MESH

vertex_data = TMP_BasicCube

triangle_count = (vertex_data.length / ATTRIBUTES_PER_VERTEX) / FLOATS_PER_ATTRIBUTE / VERTICES_PER_TRIANGLE
vertex_count = triangle_count * VERTICES_PER_TRIANGLE

Const FLOATS_PER_ATTRIBUTE:Int = 3 ' R, G, B and X, Y, Z each contain 3 floats... for now.
Const ATTRIBUTES_PER_VERTEX:Int = 2 ' Vertex data contains both RGB and XYZ attributes
Const VERTICES_PER_TRIANGLE:Int = 3 ' Just to avoid Magic Number in code!

Global RetroFloatSize:Int = Sizeof (0:Float) ' Avoiding reallocating a 0 for obtaining pointer each time!

Global VA_Offset:Int = FLOATS_PER_ATTRIBUTE * RetroFloatSize
Global VA_Stride:Int = VA_Offset * ATTRIBUTES_PER_VERTEX

' LOAD SHADERS

Local vertex_source_file:String = "vertex.glsl"
Local fragment_source_file:String = "fragment.glsl"

If Not FileType (vertex_source_file) Then vertex_source_file = "shaders/" + vertex_source_file
If Not FileType (fragment_source_file) Then fragment_source_file = "shaders/" + fragment_source_file

Local vertex_source:String = LoadString (vertex_source_file)
Local fragment_source:String = LoadString (fragment_source_file)

' ---------------------------------------------------------------------
' Create vertex shader object...
' ---------------------------------------------------------------------

Local vertex_shader:Int = glCreateShader (GL_VERTEX_SHADER)

' ---------------------------------------------------------------------
' Build and check vertex shader...
' ---------------------------------------------------------------------

glShaderSource (vertex_shader, 1, vertex_source)
glCompileShader (vertex_shader)

Local success:Int

glGetShaderiv (vertex_shader, GL_COMPILE_STATUS, Varptr success)

If Not success
'ShaderCompilationError (fragment_shader, fragment_source, vertex_source_file)
Print "Failed to compile shader"
End
EndIf

' ---------------------------------------------------------------------
' Create vertex shader object...
' ---------------------------------------------------------------------

Local fragment_shader:Int = glCreateShader (GL_FRAGMENT_SHADER)

' ---------------------------------------------------------------------
' Build and check fragment shader...
' ---------------------------------------------------------------------

glShaderSource (fragment_shader, 1, fragment_source)
glCompileShader (fragment_shader)

glGetShaderiv (fragment_shader, GL_COMPILE_STATUS, Varptr success)

If Not success
'ShaderCompilationError (fragment_shader, fragment_source, vertex_source_file)
Print "Failed to compile shader"
End
EndIf

' ---------------------------------------------------------------------
' Create shader program and attach compiled shader objects...
' ---------------------------------------------------------------------

program:Int = glCreateProgram ()

glAttachShader (program, vertex_shader)
glAttachShader (program, fragment_shader)

' ---------------------------------------------------------------------
' Link and check shader program...
' ---------------------------------------------------------------------

glLinkProgram (program)

glGetProgramiv (program, GL_LINK_STATUS, Varptr success)

If Not success
Print ""
Print "Shader program linking failed: " + glGetProgramInfoLog (program)
End
EndIf

' ---------------------------------------------------------------------
' Detach and delete shaders as no longer needed, now part of program...
' ---------------------------------------------------------------------

glDetachShader (program, vertex_shader)
glDetachShader (program, fragment_shader)

glDeleteShader (vertex_shader)
glDeleteShader (fragment_shader)

' ---------------------------------------------------------------------
' Needs to be active before uniforms can be used!
' ---------------------------------------------------------------------

glUseProgram (program)

' ---------------------------------------------------------------------
' VAO: Container for raw data...
' ---------------------------------------------------------------------

' Create and bind as current VAO...

glGenVertexArrays (1, Varptr vao) ' NB. 1 is NUMBER of buffers, not an ID!
glBindVertexArray (vao)

' ---------------------------------------------------------------------
' VBO: Raw data...
' ---------------------------------------------------------------------

' Create and bind as current VBO for currently-bound VAO...

glGenBuffers (1, Varptr vbo) ' NB. 1 is NUMBER of buffers, not an ID!
glBindBuffer (GL_ARRAY_BUFFER, vbo)

' Set buffer data for currently-bound VBO...

glBufferData (GL_ARRAY_BUFFER, SizeOf (vertex_data), vertex_data, GL_STATIC_DRAW)

glEnableVertexAttribArray (0)
glEnableVertexAttribArray (1)

' ---------------------------------------------------------------------
' Vertex attribute pointers operate on the currently-bound VAO...
' ---------------------------------------------------------------------

' Vertex attribute pointers below refer to those declared in vertex.glsl:

' layout (location = 0) in vec3 aPos;
' layout (location = 1) in vec3 aColor;

' Vertices data format description (aPos)...

glVertexAttribPointer (0, FLOATS_PER_ATTRIBUTE, GL_FLOAT, GL_FALSE, VA_Stride, Null)

' Colour data format description (aColor)...

glVertexAttribPointer (1, FLOATS_PER_ATTRIBUTE, GL_FLOAT, GL_FALSE, VA_Stride, Byte Ptr (VA_Offset))

glBindBuffer (GL_ARRAY_BUFFER, 0)
glBindVertexArray (0)

glDisableVertexAttribArray (1)
glDisableVertexAttribArray (0)

' WINDING (clockwise) AND CULLING

glFrontFace (GL_CW)
glEnable (GL_CULL_FACE)

' CAMERA

view_matrix = SMat4F.LookAt (position, New SVec3f (0.0, 0.0, 0.0), New SVec3f (0.0, 1.0, 0.0))
projection_matrix = SMat4F.Perspective (fov, _RetroGraphicsWidth, _RetroGraphicsHeight, near, far)

Local uni_cam:Int = glGetUniformLocation (program, "camera")
glUniformMatrix4fv (uni_cam, 1, False, Varptr view_matrix.a)

Local uni_prj:Int = glGetUniformLocation (program, "projection")
glUniformMatrix4fv (uni_prj, 1, False, Varptr projection_matrix.a)

' SET CLS COLOUR

glClearColor (0.1, 0.2, 0.75, 1.0)

' MAIN LOOP

While Not AppWindow.GetKey (GLFW_KEY_ESCAPE)

' CLEAR SCREEN/DEPTH BUFFER

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

glUseProgram (program)

' Using vertex attributes 0 and 1 from vertex.glsl...

glEnableVertexAttribArray (0)
glEnableVertexAttribArray (1)

glBindVertexArray (vao)

' Draw using currently-bound VAO...

glDrawArrays (GL_TRIANGLES, 0, vertex_count)

glBindVertexArray (0)

' Done using vertex attributes 0 and 1...

glDisableVertexAttribArray (1)
glDisableVertexAttribArray (0)

AppWindow.SwapBuffers ()

PollSystem ()

Wend

AppWindow.SetShouldClose (True)

glDeleteBuffers (1, Varptr vbo)
glDeleteVertexArrays (1, Varptr vao)

End

' WINDOW

Function Retro3D:TGLFWWindow (width:UInt, height:UInt, full_screen:Int = False, msaa_level:Int = 0)

TGLFWWindow.Hint (GLFW_SAMPLES, msaa_level)
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 monitor:TGLFWMonitor = Null

If full_screen Then monitor = TGLFWMonitor.GetPrimaryMonitor ()

AppWindow = New RetroGameWindow.Create (width, height, "Demo", monitor)

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

AppWindow.MakeContextCurrent ()

gladLoadGL (glfwGetProcAddress)

' DEPTH TESTING

glEnable (GL_DEPTH_TEST)
glDepthFunc (GL_LESS)

AppWindow.GetFrameBufferSize (_RetroGraphicsWidth, _RetroGraphicsHeight)

Return AppWindow

EndFunction

Type RetroGameWindow Extends TGLFWWindow

Method OnFrameBufferSize (width:Int, height:Int)
glViewport (0, 0, width, height)
AppWindow.GetFrameBufferSize (_RetroGraphicsWidth, _RetroGraphicsHeight)
EndMethod

EndType

Const RETRO_RAD_DIVIDER:Float = 180.0 / Pi
Const RETRO_DEG_DIVIDER:Float = Pi / 180.0

Function Degrees:Float (radians:Float)
Return radians * RETRO_RAD_DIVIDER
EndFunction

Function Radians:Float (degrees:Float)
Return degrees * RETRO_DEG_DIVIDER
EndFunction

Offline medi71

  • Jr. Member
  • **
  • Posts: 28
Re: Any chance of a GLFW port?
« Reply #40 on: April 22, 2020, 04:48:07 PM »
Are you working on one of the examples from learnopengl.com? Which one?

Offline DruggedBunny

  • Jr. Member
  • **
  • Posts: 60
Re: Any chance of a GLFW port?
« Reply #41 on: April 22, 2020, 06:00:26 PM »
I thought I'd grasped enough to try something for myself, so it's a mix of the tutorials noted in my source comments, both of which use similar fov/projection settings and vertices:

https://learnopengl.com/Getting-started/Transformations [Scroll halfway down to "In Practice".]

https://www.tomdalling.com/blog/modern-opengl/03-matrices-depth-buffering-animation/ [See the related code below.]
https://github.com/tomdalling/opengl-series/blob/master/source/03_matrices/source/main.cpp

Offline medi71

  • Jr. Member
  • **
  • Posts: 28
Re: Any chance of a GLFW port?
« Reply #42 on: April 22, 2020, 06:17:24 PM »
I just saw the links in your codes too, sorry for asking again.

glfw mod is new to me, and I am not expert in OpenGL. I am learning just like you. So, your problems help me too. 

About fov degree to radian conversion, it seems to me that BlitzMax's matrix uses degree, because I see it uses Tan, which in BlitzMax, requires degree. I tried degree with your code, it didn't help. I am struggling with my own example too, I will work on yours after a while, if I see something, I will let you know. But, don't count on it.

Code: [Select]
Function Perspective:SMat4F(fov:Float, w:Float, h:Float, n:Float, f:Float)
Local ft:Float = 1.0 / Tan(fov * 0.5)
Local nf:Float = 1.0 / (n - f)
Return New SMat4F(ft, 0, 0, 0, ..
0, ft * w / h, 0, 0, ..
0, 0, (f + n) * nf, -1, ..
0, 0, (2.0 * f * n) * nf, 0)
End Function

Offline DruggedBunny

  • Jr. Member
  • **
  • Posts: 60
Re: Any chance of a GLFW port?
« Reply #43 on: April 23, 2020, 12:50:06 AM »
Interesting, I wondered if there was something possibly wrong in the module itself, but of course can't really tell. I did try degrees, though not with the insight you had! Just in case 'our' version used degrees instead of radians.

Thanks for having a look, though, really appreciate it.

I think I'll have to convert the C source to really tell, though there's more to it than there appears at first, so was hoping to avoid that!

Offline medi71

  • Jr. Member
  • **
  • Posts: 28
Re: Any chance of a GLFW port?
« Reply #44 on: April 23, 2020, 01:07:10 AM »
Take a look at my code too, it doesn't show the cube, just like yours. The C++ version works. This is the first example of this book:
https://www.amazon.com/COMPUTER-GRAPHICS-PROGRAMMING-OPENGL-C-ebook/dp/B07MHD8TKB/ref=sr_1_4?dchild=1&keywords=opengl+programming+c%2B%2B&qid=1587599826&sr=8-4

I think there is something wrong with sending the uniform matrices to shader program. Either our way is bad, or something wrong with the module.

Code: [Select]
SuperStrict

Framework GLFW.GLFWWindow

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

Import BRL.StandardIO

Import BRL.Matrix

'------------------------- gl & window setup --------------
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
TGLFWWindow.Hint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)
?

Local  width:Int = 800, height:Int = 600
Local app_name:String = "Medi Cube"

Local window:TGLFWWindow = New TGameWindow.Create (width, height, app_name)

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

window.MakeContextCurrent ()

gladLoadGL (glfwGetProcAddress)

'-----------------------------------------------------
Local numVAOs:Int=1
Local numVBOs:Int=2

Local cameraX:Float= 0, cameraY:Float=0, cameraZ:Float=8
Local cubeLocX:Float = 0.0, cubeLocY:Float = 0.0, cubeLocZ:Float = 0.0
Local renderProgram:Int
Local vbo:UInt[numVBOs]
Local vao:UInt[numVAOs]

Local mvLoc:UInt, projLoc:UInt

Local aspect:Float
Local pMat:SMat4F, vMat:SMat4F, mMat:SMat4F, mvMat:SMat4F

'--------------------------- setup vertecies  -------------------
renderProgram = CreateShaderProgram("vshader.glsl", "fshader.glsl")
Local vertexPositions:Float[] = [..
-1.0,  1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, ..
1.0, -1.0, -1.0, 1.0,  1.0, -1.0, -1.0,  1.0, -1.0, ..
1.0, -1.0, -1.0, 1.0, -1.0,  1.0, 1.0,  1.0, -1.0, ..
1.0, -1.0,  1.0, 1.0,  1.0,  1.0, 1.0,  1.0, -1.0, ..
1.0, -1.0,  1.0, -1.0, -1.0,  1.0, 1.0,  1.0,  1.0, ..
-1.0, -1.0,  1.0, -1.0,  1.0,  1.0, 1.0,  1.0,  1.0, ..
-1.0, -1.0,  1.0, -1.0, -1.0, -1.0, -1.0,  1.0,  1.0, ..
-1.0, -1.0, -1.0, -1.0,  1.0, -1.0, -1.0,  1.0,  1.0, ..
-1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  1.0, -1.0, -1.0, ..
1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0,  1.0, ..
-1.0,  1.0, -1.0, 1.0,  1.0, -1.0, 1.0,  1.0,  1.0, ..
1.0,  1.0,  1.0, -1.0,  1.0,  1.0, -1.0,  1.0, -1.0]

glGenVertexArrays (1, Varptr vao)
glBindVertexArray (vao[0])
glGenBuffers (numVBOs, Varptr vbo)

glBindBuffer (GL_ARRAY_BUFFER, vbo[0])
glBufferData (GL_ARRAY_BUFFER, SizeOf (vertexPositions ), vertexPositions, GL_STATIC_DRAW)


' -------------- ------------- main loop -----------------------------------
While Not window.ShouldClose ()
ProcessInput (window)
glClearColor (0.2, 0.3, 0.3, 1.0)
glClear (GL_COLOR_BUFFER_BIT)

glUseProgram (renderProgram)

mvLoc = glGetUniformLocation(renderProgram, "mv_matrix")
projLoc = glGetUniformLocation(renderProgram, "proj_matrix")

pMat = SMat4F.Perspective(60, width , height, 0.1, 1000.0)

vMat = SMat4F.Translation(New SVec3F(-cameraX, -cameraY, -cameraZ))
mMat = SMat4F.Translation(New SVec3F(cubeLocX, cubeLocY, cubeLocZ))
mvMat = vMat * mMat; 'model-view

glUniformMatrix4fv(mvLoc, 1, GL_FALSE,  Varptr mvMat.a)
glUniformMatrix4fv(projLoc, 1, GL_FALSE, Varptr pMat.a)

glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, False, 0, 0);
glEnableVertexAttribArray(0);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

glDrawArrays(GL_TRIANGLES, 0, 36);

window.SwapBuffers ()
PollSystem ()
Wend

glDeleteVertexArrays (1, Varptr vao)
glDeleteBuffers (1, Varptr vbo)

End

'------------------------- end --------------------------------

'------------ function read shader file  ----------------------
Function readShaderFile:String(file:String)
Local shaderSource:String
Local lineFromFile:String
Local in:TStream = OpenStream(file)
While Not Eof(in)
lineFromFile = ReadLine(in)
shaderSource :+ lineFromFile + "~n"
Wend
CloseStream(in)
Return shaderSource
End Function
'------------- function create shader program ------------------
Function CreateShaderProgram:Int(vsFile:String, frFile:String)

' read and compile vertex shader
Local infoLog:String
Local success:Int
Local vertexShaderSource:String
vertexShaderSource = readShaderFile(vsFile)
Local vertexShader:Int = glCreateShader (GL_VERTEX_SHADER)
glShaderSource (vertexShader, 1, vertexShaderSource)
glCompileShader (vertexShader)
glGetShaderiv (vertexShader, GL_COMPILE_STATUS, Varptr success)
If Not success
infoLog = glGetShaderInfoLog (vertexShader )
Print "Vertex shader compilation failed: " + String.FromCString (infoLog)
EndIf

' read and compile fragment shader
Local fragmentShaderSource:String
fragmentShaderSource = readShaderFile("fshader.glsl")
Local fragmentShader:Int = glCreateShader (GL_FRAGMENT_SHADER)
glShaderSource (fragmentShader, 1, fragmentShaderSource)
glCompileShader (fragmentShader)
glGetShaderiv (fragmentShader, GL_COMPILE_STATUS, Varptr success)
If Not success
infoLog = glGetShaderInfoLog (fragmentShader)
Print "Fragment shader compilation failed: " + String.FromCString (infoLog)
EndIf

' link, attach and check
Local shaderProgram:Int = glCreateProgram ()
glAttachShader (shaderProgram, vertexShader)
glAttachShader (shaderProgram, fragmentShader)
glLinkProgram (shaderProgram)
glGetProgramiv (shaderProgram, GL_LINK_STATUS, Varptr success)
If Not success
infoLog = glGetProgramInfoLog (shaderProgram)
Print "Shader program linking failed: " + String.FromCString (infoLog)
EndIf

' have the program, don't need anymore
glDeleteShader (vertexShader)
glDeleteShader (fragmentShader)

Return shaderProgram
End Function


vertex shader:
Code: [Select]

#version 330

layout (location=0) in vec3 position;

uniform mat4 mv_matrix;
uniform mat4 proj_matrix;

void main(void)
{
gl_Position = proj_matrix * mv_matrix * vec4(position,1.0);
}

fragment shader:
Code: [Select]
#version 330

out vec4 color;

void main(void)
{ color = vec4(1.0, 0.0, 1.0, 1.0);
}

Simple stuff, but doesn't work!  :(
Here I say it doesn't but it does, read later posts.

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal