PDA

View Full Version : vertex buffer objects for OpenGl 1.5



primo
03-12-2019, 10:56
Hi Petr
i want to implement this VBO code in
https://en.wikipedia.org/wiki/Vertex_buffer_object#In_C,_using_OpenGL_2.1
for opengl 1.5 to 2.1
i have added #Include "%app_includepath%\thinBasic_GLEXT.inc"
but i get errors about the function glGenBuffers:

A general declared function or sub has no process address. See DECLARE SET ADDRESS help for more info.
i have searched the file "thinBasic_GLEXT.inc" and the glGenBuffers is declared like:
DECLARE SUB glGenBuffers ( BYVAL Par1 AS GLsizei, BYREF Par2 AS GLuint)
so what is the mystery?
i have searched for glGenBuffers inside opengl32.dll which is located my windows xp32 system folder and can't find the glGenBuffers inside the DLL, the only place i find it is inside my nvidea card driver software. i also find it in mesa opengl32.dll https://fdossena.com/?p=mesa/index.frag in which its opengl32.dll have big size > 16MB and seems to contains all extensions.
best regards

Uses "tbgl", "MATH"

#Def %GL_GLEXT_PROTOTYPES
#Include "%app_includepath%\thinbasic_gl.inc"
#Include "%app_includepath%\thinbasic_glu.inc"
#Include "%app_includepath%\thinbasic_WGL.inc"
#Include "%app_includepath%\thinBasic_GLEXT.inc"

Dim HglGenBuffers As DWord = TBGL_GetProcAddress("glGenBuffers")
Declare Set Address glGenBuffers, HglGenBuffers

Dim hwnd As DWord

hwnd = TBGL_CreateWindowEx("glDrawArrays example - esc to exit", 600, 600, 32, 0)
TBGL_ShowWindow
TBGL_ResetKeyState()

'try float data[] = {0.0, 1.0, 0.0, -1.0, -1.0, 0.0, 1.0, -1.0, 0.0}
Global data(9) As Single
Array Assign data = 1.0, 0.0, 1.0, 0.0, 0.0, -1.0, -1.0, 0.0, 1.0

'Create a New VBO And use the variable id To store the VBO id
Global triangleVBO As DWord
glGenBuffers(1, VarPtr(triangleVBO)) ' //Make the New VBO active
glBindBuffer(%GL_ARRAY_BUFFER, triangleVBO)
'Upload vertex data To the video device
glBufferData(%GL_ARRAY_BUFFER, SizeOf(data), data, %GL_STATIC_DRAW)
'Make the New VBO active. Repeat here incase changed since initialisation
glBindBuffer(%GL_ARRAY_BUFFER, triangleVBO)

While TBGL_IsWindow(hwnd)
TBGL_ClearFrame

display
TBGL_DrawFrame
If TBGL_GetWindowKeyState( hwnd, %VK_ESCAPE) Then Exit While
Wend

TBGL_DestroyWindow

'=================================================================================

'--------------------------------------------------------
'--------------------------------------------------------
Sub display()

glMatrixMode(%GL_PROJECTION)
glLoadIdentity()
gluPerspective(60.0, 800/600, 1.0, 100.0)
glMatrixMode(%GL_MODELVIEW)
'glTranslatef(0, 0, -40)
glShadeModel(%GL_SMOOTH)
glEnable(%GL_DEPTH_TEST)
gluLookAt( 0, 1, 1,
0, 1, 0,
0, 1, 0 )
glclear(%gl_color_buffer_bit)

TBGL_Rotate GetTickCount/30,0,1,0

'Initialise VBO - do only once, at start of program
'Create a variable To hold the VBO identifier GLuint triangleVBO;
'Vertices of a triangle (counter-clockwise winding)

'Draw Triangle from VBO - Do Each time Window, view point Or data changes
'Establish its 3 coordinates per vertex With zero stride In This Array; necessary here
glVertexPointer(3, %GL_FLOAT, 0, NULL)
'Establish Array Contains vertices (Not normals, colours, texture coords etc)
glEnableClientState(%GL_VERTEX_ARRAY)
'Actually Draw the triangle, giving the Number of vertices provided
glDrawArrays(%GL_TRIANGLES, 1, 9)
'Force display To be drawn Now
'glFlush;

glClear(%GL_COLOR_BUFFER_BIT Or %GL_DEPTH_BUFFER_BIT)
glClearColor(1, 1, 1, 1)

glDisableClientState(%GL_VERTEX_ARRAY)
'**************************************************


End Sub



Edit: i have added :

Dim HglGenBuffers As DWord = TBGL_GetProcAddress("glGenBuffers")
Declare Set Address glGenBuffers, HglGenBuffers
but still can't access glGenBuffers

Edit2:
my Last try, but still can't access the glGenBuffers
my card support VBO as i can run the VBO examples in other languages.

Uses "tbgl"

#Def %GL_GLEXT_PROTOTYPES
#Include "%app_includepath%\thinbasic_gl.inc"
#Include "%app_includepath%\thinbasic_glu.inc"
#Include "%app_includepath%\thinBasic_GLEXT.inc"
#Include "%app_includepath%\thinbasic_WGL.inc"

Declare Set Address glGenBuffers, wglGetProcAddress("glGenBuffers")
Declare Set Address glBindBuffer, wglGetProcAddress("glBindBuffer")
Declare Set Address glBufferData, wglGetProcAddress("glBufferData")

Dim hwnd As DWord

hwnd = TBGL_CreateWindowEx("glDrawArrays example - esc to exit", 600, 600, 32, 0)
TBGL_ShowWindow
TBGL_ResetKeyState()

'try float data[] = {0.0, 1.0, 0.0, -1.0, -1.0, 0.0, 1.0, -1.0, 0.0}
Global data(9) As Single
Array Assign data = 1.0, 0.0, 1.0, 0.0, 0.0, -1.0, -1.0, 0.0, 1.0

'Create a New VBO And use the variable id To store the VBO id
Dim triangleVBO As DWord
glGenBuffers(1, triangleVBO) ' //Make the New VBO active
glBindBuffer(%GL_ARRAY_BUFFER, triangleVBO)
'Upload vertex data To the video device
glBufferData(%GL_ARRAY_BUFFER, SizeOf(data), data, %GL_STATIC_DRAW)
'Make the New VBO active. Repeat here incase changed since initialisation
glBindBuffer(%GL_ARRAY_BUFFER, triangleVBO)

While TBGL_IsWindow(hwnd)
TBGL_ClearFrame

display
TBGL_DrawFrame
If TBGL_GetWindowKeyState( hwnd, %VK_ESCAPE) Then Exit While
Wend

TBGL_DestroyWindow

'=================================================================================

Sub display()

glMatrixMode(%GL_PROJECTION)
glLoadIdentity()
gluPerspective(60.0, 800/600, 1.0, 100.0)
glMatrixMode(%GL_MODELVIEW)
'glTranslatef(0, 0, -40)
glShadeModel(%GL_SMOOTH)
glEnable(%GL_DEPTH_TEST)
gluLookAt( 0, 1, 1,
0, 1, 0,
0, 1, 0 )
glclear(%gl_color_buffer_bit)

TBGL_Rotate GetTickCount/30,0,1,0

'Initialise VBO - do only once, at start of program
'Create a variable To hold the VBO identifier GLuint triangleVBO;
'Vertices of a triangle (counter-clockwise winding)

'Draw Triangle from VBO - Do Each time Window, view point Or data changes
'Establish its 3 coordinates per vertex With zero stride In This Array; necessary here
glVertexPointer(3, %GL_FLOAT, 0, NULL)
'Establish Array Contains vertices (Not normals, colours, texture coords etc)
glEnableClientState(%GL_VERTEX_ARRAY)
'Actually Draw the triangle, giving the Number of vertices provided
glDrawArrays(%GL_TRIANGLES, 1, 9)
'Force display To be drawn Now
'glFlush;

glClear(%GL_COLOR_BUFFER_BIT Or %GL_DEPTH_BUFFER_BIT)
glClearColor(1, 1, 1, 1)

glDisableClientState(%GL_VERTEX_ARRAY)
'**************************************************

End Sub

Petr Schreiber
03-12-2019, 22:54
Hi Primo,

thanks for reaching me. I think you must perform the Declare Set Address once you have active OpenGL context, that is, after TBGL_CreateWindowEx.

Then, I think you need to pass pointer to glBufferData, let's try something like:


glBufferData(%GL_ARRAY_BUFFER, SizeOf(data), varptr(data(1)), %GL_STATIC_DRAW)


I think that should boost you in the right direction :)


Petr

primo
04-12-2019, 11:25
You are so nice Petr to provide a solution, Thank you.
yes it works now with the suggestions. following is the new code
but there is the problem of the triangle colors, it seems the color is sticky and can't be changed . as an example in line 34:
data(1).red = 1: data(1).green = 0: data(1).blue=0
if i changed red to 0 and green to 1 i can't get it green and the red stay on. also i can't display the color blue vividly like red or green

i have noticed that:
glVertexPointer(3, %GL_FLOAT, 0, NULL)
glColorPointer(3, %GL_FLOAT, 0, NULL)

it refer to Null, but i can understand this as preventing the user from accessing the GPU memory directly, and that the moving the data to the GPU is done by glBufferData which we provide it with the address of the data. this is my own imagination and can be wrong

Uses "tbgl"

#Def %GL_GLEXT_PROTOTYPES
#Include "%app_includepath%\thinbasic_gl.inc"
#Include "%app_includepath%\thinbasic_glu.inc"
#Include "%app_includepath%\thinBasic_GLEXT.inc"
#Include "%app_includepath%\thinbasic_WGL.inc"

Type Point3D
x As Single
y As Single
z As Single
red As Single
green As Single
blue As Single
End Type


Dim hwnd As DWord

hwnd = TBGL_CreateWindowEx("VBO example - esc to exit", 600, 600, 32, 0)
TBGL_ShowWindow
Declare Set Address glGenBuffers, wglGetProcAddress("glGenBuffers")
Declare Set Address glBindBuffer, wglGetProcAddress("glBindBuffer")
Declare Set Address glBufferData, wglGetProcAddress("glBufferData")

TBGL_ResetKeyState()

Global data(3) As Point3D

data(1).x = 0: data(1).y = 1: data(1).z = 0
data(2).x = -1: data(2).y = -1: data(2).z = 0
data(3).x = 1: data(3).y = -1: data(3).z = 0
data(1).red = 1: data(1).green = 0: data(1).blue=0
data(2).red = 0: data(2).green = 1: data(2).blue=0
data(3).red = 0: data(3).green = 0: data(3).blue=1

'Create a New VBO And use the variable id To store the VBO id
Global triangleVBO, triangleColor As DWord

glGenBuffers(1, triangleVBO) ' //Make the New VBO active
glBindBuffer(%GL_ARRAY_BUFFER, triangleVBO)
glBufferData(%GL_ARRAY_BUFFER, SizeOf(data), VarPtr(data(1).x), %GL_STATIC_DRAW)
glBindBuffer(%GL_ARRAY_BUFFER, triangleVBO)

glGenBuffers( 1, triangleColor )
glBindBuffer(%GL_ARRAY_BUFFER, triangleColor)
glBufferData(%GL_ARRAY_BUFFER, SizeOf(data),VarPtr(data(1).red),%GL_STATIC_DRAW)

While TBGL_IsWindow(hwnd)
TBGL_ClearFrame

display
TBGL_DrawFrame
If TBGL_GetWindowKeyState( hwnd, %VK_ESCAPE) Then Exit While
Wend

TBGL_DestroyWindow

'=================================================================================

Sub display()

glEnable(%GL_DEPTH_TEST)
glDisable(%GL_CULL_FACE)
TBGL_Camera( 0, 0, 4, 0, 0, 0 )

glClear(%GL_COLOR_BUFFER_BIT Or %GL_DEPTH_BUFFER_BIT)
glClearColor(1, 1, 1, 1)

TBGL_Rotate GetTickCount/30,0,1,0

'Establish Array Contains vertices (Not normals, colours, texture coords etc)
glEnableClientState(%GL_VERTEX_ARRAY)
glEnableClientState(%GL_COLOR_ARRAY)
'Establish its 3 coordinates per vertex With zero stride In This Array; necessary here
glVertexPointer(3, %GL_FLOAT, 0, NULL)
glColorPointer(3, %GL_FLOAT, 0, NULL)
'Actually Draw the triangle, giving the Number of vertices provided
glDrawArrays(%GL_TRIANGLES, 1, 3)

glDisableClientState(%GL_COLOR_ARRAY)
glDisableClientState(%GL_VERTEX_ARRAY)
'**************************************************

End Sub

DirectuX
04-12-2019, 17:00
but there is the problem of the triangle colors, it seems the color is sticky and can't be changed . as an example in line 34:
data(1).red = 1: data(1).green = 0: data(1).blue=0
if i changed red to 0 and green to 1 i can't get it green and the red stay on. also i can't display the color blue vividly like red or green


Hi Primo,

Unless I'm mistaken, changing vertex coordinates does not work either. It looks, from my point of view, that you have to specify how the vertex is stored (vertex format) in the array ( according to the Type Point3D you defined )
maybe this (https://www.khronos.org/opengl/wiki/VBO#Vertex_Buffer_Object)can help.

primo
04-12-2019, 20:51
Thanks DirectuX , you are right changing vertex positions not possible, i haven't noticed this. will look more about the subject. this makes the subject more complex.

DirectuX
04-12-2019, 23:15
Hi Primo,
According to this documentation (https://www.khronos.org/registry/OpenGL-Refpages/es2.0/xhtml/), you may want to write it like this :


Uses "tbgl"

#Def %GL_GLEXT_PROTOTYPES
#Include "%app_includepath%\thinbasic_gl.inc"
#Include "%app_includepath%\thinbasic_glu.inc"
#Include "%app_includepath%\thinBasic_GLEXT.inc"
#Include "%app_includepath%\thinbasic_WGL.inc"

Type Point3D

red As Single ' Color first see line #93
green As Single
blue As Single
x As Single
y As Single
z As Single

End Type


Dim hwnd As DWord

hwnd = TBGL_CreateWindowEx("VBO example - esc to exit", 600, 600, 32, 0)
TBGL_ShowWindow
Declare Set Address glGenBuffers, wglGetProcAddress("glGenBuffers")
Declare Set Address glBindBuffer, wglGetProcAddress("glBindBuffer")
Declare Set Address glBufferData, wglGetProcAddress("glBufferData")

TBGL_ResetKeyState()

Global data(3) As Point3D

data(1).x = -1
data(1).y = 0
data(1).z = 0

data(2).x = 1
data(2).y = 0
data(2).z = 0

data(3).x = 0
data(3).y = 1
data(3).z = 0

data(1).red = 1
data(1).green = 0
data(1).blue = 0

data(2).red = 0
data(2).green = 1
data(2).blue = 0

data(3).red = 0
data(3).green = 0
data(3).blue = 1

'Create a New VBO And use the variable id To store the VBO id
Global triangleVBO(1) As DWord ' Doc says an array

glGenBuffers(1, triangleVBO) ' //Make the New VBO active
'Once is sufficient
glBindBuffer(%GL_ARRAY_BUFFER, triangleVBO(1))
glBufferData(%GL_ARRAY_BUFFER, SizeOf(data), VarPtr(data(1).red), %GL_STATIC_DRAW)

While TBGL_IsWindow(hwnd)
TBGL_ClearFrame

display
TBGL_DrawFrame
If TBGL_GetWindowKeyState( hwnd, %VK_ESCAPE) Then Exit While
Wend

TBGL_DestroyWindow

'=================================================================================

Sub display()

glEnable(%GL_DEPTH_TEST)
glDisable(%GL_CULL_FACE)
TBGL_Camera( 0, 0, 4, 0, 0, 0 )

glClear(%GL_COLOR_BUFFER_BIT Or %GL_DEPTH_BUFFER_BIT)
glClearColor(1, 1, 1, 1)

TBGL_Rotate GetTickCount/30,0,1,0

'Establish Array Contains vertices (Not normals, colours, texture coords etc)

glEnableClientState(%GL_VERTEX_ARRAY)
glEnableClientState(%GL_COLOR_ARRAY)

glInterleavedArrays(%GL_C3F_V3F, 0, null) ' using TYPE Point3D , you interleave your data

'Actually Draw the triangle, giving the Number of vertices provided
glDrawArrays(%GL_TRIANGLES, 0, 3) 'notice, this is not TB array : it starts at 0


glDisableClientState(%GL_VERTEX_ARRAY)
glDisableClientState(%GL_COLOR_ARRAY)
'**************************************************

End Sub

primo
05-12-2019, 07:06
Thank you very much DirectuX, your solution have kept the project small and simple.
it seems "glInterleavedArrays" is very nice function and liberating us from the trouble of mixed arrays. you have used also one buffer and glInterleavedArrays does the complex job for servicing the glDrawArrays function.
there is a big archive in https://github.com/gamedev-net/nehe-opengl
and i find lesson 45 is about VBO, there is an example using the old delphi, will install it to see the behavior of the first code
Thank You again

primo
19-01-2020, 08:50
albeit DirectuX approach https://www.thinbasic.com/community/showthread.php?12992-vertex-buffer-objects-for-OpenGl-1-5&p=95195&viewfull=1#post95195
using glInterleavedArrays with glDrawArrays is nice and short, i still thinking why my example does not work, the answer is we should use inside the main loop:

glEnableClientState(%GL_VERTEX_ARRAY)
glEnableClientState(%GL_COLOR_ARRAY)

glBindBuffer(%GL_ARRAY_BUFFER, triangleVBO)
glVertexPointer(3, %GL_FLOAT, SizeOf(Point3D), 0)

glBindBuffer(%GL_ARRAY_BUFFER, triangleColor)
glColorPointer(3, %GL_FLOAT, SizeOf(Point3D), 0)

glDrawArrays(%GL_TRIANGLES, 0, 3)

glDisableClientState(%GL_COLOR_ARRAY)
glDisableClientState(%GL_VERTEX_ARRAY)

now we can change dimensions and color any time and the color is okay,

Uses "tbgl"

#Def %GL_GLEXT_PROTOTYPES
#Include "%app_includepath%\thinbasic_gl.inc"
#Include "%app_includepath%\thinbasic_glu.inc"
#Include "%app_includepath%\thinBasic_GLEXT.inc"
#Include "%app_includepath%\thinbasic_WGL.inc"

Type Point3D
x As Single
y As Single
z As Single
red As Single
green As Single
blue As Single
End Type

Dim hwnd As DWord

hwnd = TBGL_CreateWindowEx("glDrawArrays example - esc to exit", 600, 600, 32, 0)
TBGL_ShowWindow
TBGL_ResetKeyState()

Declare Set Address glGenBuffers, wglGetProcAddress("glGenBuffers")
Declare Set Address glBindBuffer, wglGetProcAddress("glBindBuffer")
Declare Set Address glBufferData, wglGetProcAddress("glBufferData")

Global data(3) As Point3D

data(1).x = 0: data(1).y = 1: data(1).z = 0
data(2).x = -1: data(2).y = -1: data(2).z = 0
data(3).x = 1: data(3).y = -1: data(3).z = 0

data(1).red = 1: data(1).green = 0: data(1).blue=0
data(2).red = 0: data(2).green = 1: data(2).blue=0
data(3).red = 0: data(3).green = 0: data(3).blue=1

'msgbox 0, str$(SizeOf(Point3D))
'Create a New VBO And use the variable id To store the VBO id
Dim triangleVBO As DWord
Dim triangleColor As DWord

glGenBuffers(1, triangleVBO) ' //Make the New VBO active
glBindBuffer(%GL_ARRAY_BUFFER, triangleVBO)
'Upload vertex data To the video device
glBufferData(%GL_ARRAY_BUFFER, (SizeOf(Point3D)*3), VarPtr(data(1).x), %GL_STATIC_DRAW)

glGenBuffers(2, triangleColor)
glBindBuffer(%GL_ARRAY_BUFFER, triangleColor)
glBufferData(%GL_ARRAY_BUFFER, (SizeOf(Point3D)*3), VarPtr(data(1).red), %GL_STATIC_DRAW)

'msgbox 0, str$(SizeOf(data) )
While TBGL_IsWindow(hwnd)
TBGL_ClearFrame

display
TBGL_DrawFrame
If TBGL_GetWindowKeyState( hwnd, %VK_ESCAPE) Then Exit While
Wend

TBGL_DestroyWindow

'=================================================================================

Sub display()
glEnable(%GL_DEPTH_TEST)
glDisable(%GL_CULL_FACE)
TBGL_Camera( 0, 0, 4, 0, 0, 0 )

glClear(%GL_COLOR_BUFFER_BIT Or %GL_DEPTH_BUFFER_BIT)
glClearColor(0.5, 0.5, 0.5, 0.5)
'TBGL_Rotate GetTickCount/30,0,1,0
'Draw Triangle from VBO - Do Each time Window, view point Or data changes
glEnableClientState(%GL_VERTEX_ARRAY)
glEnableClientState(%GL_COLOR_ARRAY)

glBindBuffer(%GL_ARRAY_BUFFER, triangleVBO)
glVertexPointer(3, %GL_FLOAT, SizeOf(Point3D), 0)

glBindBuffer(%GL_ARRAY_BUFFER, triangleColor)
glColorPointer(3, %GL_FLOAT, SizeOf(Point3D), 0)
'Actually Draw the triangle, giving the Number of vertices provided
glDrawArrays(%GL_TRIANGLES, 0, 3)
'Force display To be drawn Now
'glFlush

glDisableClientState(%GL_COLOR_ARRAY)
glDisableClientState(%GL_VERTEX_ARRAY)
'**************************************************

End Sub

Petr Schreiber
19-01-2020, 10:24
Hi Primo, DirectuX,

if you want, I could add this interleaved option to GBuffers, what do you think?


Petr

DirectuX
19-01-2020, 14:09
Hi Primo, DirectuX,

if you want, I could add this interleaved option to GBuffers, what do you think?


Petr

Primo, it's up to you, as I would prefer clearing tbgl issues (https://github.com/ThinBASIC/thinBasic_TBGL/issues).

primo
19-01-2020, 14:42
Hi Petr, DirectuX
i remember Petr said that GBuffer is created using glDrawArrays, but it is not known that its coupled with glVertexPointer/glColorPointer/... without VBO context, or created with VBO Context and buffered to GPU with glBufferData, something like that. it is said glInterleavedArrays can speed the execution.
but there is a problem :
the arrays presented to Gbuffer is either VertexArray() [, ColorArray(), NormalArray(), TexCoordArray() ])
but i can't use the data type like this

Type Point3D
x As Single
y As Single
z As Single
r As Byte
g As Byte
b As Byte
End Type

i am restricted to something like :
Type PointPosition
x As Single
y As Single
z As Single
End Type
Type PointColor
r As Byte
g As Byte
b As Byte
End Type

this works while the later does not work

Uses "TBGL"
Uses "math"

Type PointPosition
x As Single
y As Single
z As Single
End Type
Type PointColor
r As Byte
g As Byte
b As Byte
End Type

Function TBMain()

Local hWnd As DWord
Local FrameRate As Double

' -- Create and show window
hWnd = TBGL_CreateWindowEx("Plotting 3D Data in an array using GBuffers - press ESC to quit", 600, 600, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow

TBGL_BackColor 255,255,255
TBGL_SetDrawDistance 250

' -- Create 3D points buffer
'Dim gbPoints As DWord = TBGL_GBufferCreate(%TBGL_POINTS, %TBGL_3D)
Dim gbPoints As DWord = TBGL_GBufferCreate(%TBGL_TRIANGLES, %TBGL_3D)

Global Nb As DWord = 3

' -- Define data for it
Global VertexA(Nb) As PointPosition 'TBGL_TVECTOR3F
Global ColorA(Nb) As PointColor 'TBGL_TRGB

VertexA(1).x = 0: VertexA(1).y = 1: VertexA(1).z = 0
VertexA(2).x = -1: VertexA(2).y = -1: VertexA(2).z = 0
VertexA(3).x = 1: VertexA(3).y = -1: VertexA(3).z = 0

ColorA(1).r = 255: ColorA(1).g = 0: ColorA(1).b=0
ColorA(2).r = 0: ColorA(2).g = 255: ColorA(2).b=0
ColorA(3).r = 0: ColorA(3).g = 0: ColorA(3).b=255

' -- Create buffer dynamically linked to the arrays above
TBGL_GBufferDefineFromArray(gbPoints, %TBGL_DYNAMIC, CountOf(VertexA), VertexA(1), ColorA(1))

' -- Resets status of all keys
TBGL_ResetKeyState()
'TBGL_PointSize 2

' -- Main loop
While TBGL_IsWindow(hWnd)
'init
FrameRate = TBGL_GetFrameRate

TBGL_ClearFrame
TBGL_Camera(0, 0, 5, 0, 0, 0)

' -- Turn triangle
TBGL_Rotate GetTickCount/50, 0, 1, 0
'TBGL_Scale 0.6, 0.6, 0.6

' -- Render it
TBGL_GBufferRender(gbPoints)

TBGL_DrawFrame

' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While

Wend
' -- Destroying the buffer is not necessary,
' -- the garbage collector will take care of it

' -- Destroy window
TBGL_DestroyWindow
End Function




this does not work:

Uses "TBGL"
Uses "math"

Type Point3D
x As Single
y As Single
z As Single
r As Byte
g As Byte
b As Byte
End Type

Function TBMain()

Local hWnd As DWord
Local FrameRate As Double

' -- Create and show window
hWnd = TBGL_CreateWindowEx("Plotting 3D Data in an array using GBuffers - press ESC to quit", 600, 600, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow

TBGL_BackColor 255,255,255
TBGL_SetDrawDistance 250

' -- Create 3D points buffer
'Dim gbPoints As DWord = TBGL_GBufferCreate(%TBGL_POINTS, %TBGL_3D)
Dim gbPoints As DWord = TBGL_GBufferCreate(%TBGL_TRIANGLES, %TBGL_3D)

Global Nb As DWord = 3

' -- Define data for it
Global data(Nb) As Point3D 'TBGL_TVECTOR3F
'Global ColorA(Nb) As PointColor 'TBGL_TRGB

data(1).x = 0: data(1).y = 1: data(1).z = 0
data(2).x = -1: data(2).y = -1: data(2).z = 0
data(3).x = 1: data(3).y = -1: data(3).z = 0

data(1).r = 255: data(1).g = 0: data(1).b=0
data(2).r = 0: data(2).g = 255: data(2).b=0
data(3).r = 0: data(3).g = 0: data(3).b=255
'MsgBox(0, Str$(CountOf(data)))
' -- Create buffer dynamically linked to the arrays above
TBGL_GBufferDefineFromArray(gbPoints, %TBGL_DYNAMIC, CountOf(data), data(1).x, data(1).r)

' -- Resets status of all keys
TBGL_ResetKeyState()
'TBGL_PointSize 2

' -- Main loop
While TBGL_IsWindow(hWnd)
'init
FrameRate = TBGL_GetFrameRate

TBGL_ClearFrame
TBGL_Camera(0, 0, 5, 0, 0, 0)

' -- Turn triangle
TBGL_Rotate GetTickCount/50, 0, 1, 0
'TBGL_Scale 0.6, 0.6, 0.6

' -- Render it
TBGL_GBufferRender(gbPoints)

TBGL_DrawFrame

' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While

Wend
' -- Destroying the buffer is not necessary,
' -- the garbage collector will take care of it

' -- Destroy window
TBGL_DestroyWindow
End Function




also i suggest to let the color type Single , since it is hard coded as byte.

Petr Schreiber
19-01-2020, 21:21
Hi Primo,

GBuffers are "abstraction for geometry definition and rendering". The fact they use Vertex Arrays internally now is an implementation detail which might or might not change anytime on the future.

With that I want to say - let's not restrict our vision with what we have. It can be expanded, improved, reworked.

Of course it does not work now, because the documentation indicates this is not one of the valid uses - now.

I could keep the current TBGL_GBufferDefineFromArray, and add different TBGL_GBufferDefine* function to work with interleaved format, for example. Just thinking out loud.

Just let me know if you see any benefit in this.

DirectuX - I think it is important to collect inspiration from users continuously, not just once all issues are resolved. I see no harm with adding them to the issue list as they come.


Petr

Petr Schreiber
19-01-2020, 21:26
Primo,

could you please explain more what do you mean by "also i suggest to let the color type Single , since it is hard coded as byte."?

Do you mean you would like to specify color channels in range 0.0 - 1.0 as a 32bit floating point?


Thank you,
Petr

primo
19-01-2020, 22:10
Hi Petr, yes TBGL_GBufferDefine will be appropriate
regarding the single versus Byte,
this works:

Type PointColor
r As Byte
g As Byte
b As Byte
End Type
while this:

Type PointColor
r As Single
g As Single
b As Single
End Type
does not work

so it is hard coded.

Do you mean you would like to specify color channels in range 0.0 - 1.0 as a 32bit floating point?
Yes, the reason is what if we need more than the eye can see, also with single we have more freedom .
i remember the perlin noise example posted 2 years ago using byte for color channels in a gbuffer example gives different results than when we use single for color in an opengl example

DirectuX
19-01-2020, 22:29
Hi Primo,

GBuffers are "abstraction for geometry definition and rendering". The fact they use Vertex Arrays internally now is an implementation detail which might or might not change anytime on the future.

With that I want to say - let's not restrict our vision with what we have. It can be expanded, improved, reworked.

Of course it does not work now, because the documentation indicates this is not one of the valid uses - now.

I could keep the current TBGL_GBufferDefineFromArray, and add different TBGL_GBufferDefine* function to work with interleaved format, for example. Just thinking out loud.

Just let me know if you see any benefit in this.

DirectuX - I think it is important to collect inspiration from users continuously, not just once all issues are resolved. I see no harm with adding them to the issue list as they come.


Petr


indeed Petr, I've just entierly left the choice to Primo, it is his code example after all :)

Petr Schreiber
23-01-2020, 21:01
Primo,

well, it is documented behaviour. If you want, I can add TBGL_tRGB_f32 and TBGL_tRGBA_f32 for 32bit floating point colors. Deal? :)


Petr

primo
24-01-2020, 10:01
Thanks Petr,

TBGL_tRGB_f32 and TBGL_tRGBA_f32 for 32bit floating point colors.
this is a nice suggestion. Deal.

Petr Schreiber
24-01-2020, 20:23
Hi primo,

I added the support for single color, you can grab new TBGL here:
https://github.com/ThinBASIC/thinBasic_TBGL/releases/tag/1.11.2.3

Will be present in next release :)

The interleaved arrays need deeper thought, so I will switch now to another issue we discussed with DirectuX and then will get back to interleaved.


Petr