Are VBOs and Instancing not available in freebasic's OpenGL?

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
Post Reply
Boromir
Posts: 463
Joined: Apr 30, 2015 19:28
Location: Oklahoma,U.S., Earth,Solar System
Contact:

Are VBOs and Instancing not available in freebasic's OpenGL?

Post by Boromir »

Are VBOs and Instancing not available in freebasic's OpenGL?

Why do none of these work in my freebasic OpenGL programs.

Code: Select all

glDrawArraysInstanced

glGenBuffers
glBindBuffer
paul doe
Moderator
Posts: 1732
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Are VBOs and Instancing not available in freebasic's OpenGL?

Post by paul doe »

Boromir wrote:Are VBOs and Instancing not available in freebasic's OpenGL?

Why do none of these work in my freebasic OpenGL programs.

Code: Select all

glDrawArraysInstanced

glGenBuffers
glBindBuffer
Check out this demo: viewtopic.php?f=15&t=26105. There I use VBOs and VAOs. If you can't setup the VAOs correctly, glDrawArraysInstanced won't work right.
Boromir
Posts: 463
Joined: Apr 30, 2015 19:28
Location: Oklahoma,U.S., Earth,Solar System
Contact:

Re: Are VBOs and Instancing not available in freebasic's OpenGL?

Post by Boromir »

Thanks! I added all the bindproc stuff in and now I can use those functions.
I'm having some trouble using a VBO though. This is my setup code.

Code: Select all

glGenBuffers(1, @vboId)
glBindBuffer(GL_ARRAY_BUFFER, vboId)
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), verticess, GL_STATIC_DRAW)
glVertexPointer(3, GL_FLOAT, 0, 0)
glColorPointer(  3,GL_FLOAT,0,0)
glNormalPointer( GL_FLOAT,0,0)
Also how can I make the instanced drawing mode not stack them all in the same spot

Code: Select all

'opengl test program
dim as integer nc=370 'number of rows and columns of quad faces
dim as boolean vbomode=false 'try to use vbo

#include once "GL/gl.bi"
#include once "GL/glu.bi"
#include once "GL/glext.bi"
#include once "fbgfx.bi"
screenres 640,480,32,,2
glViewport 0, 0, 640, 480
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective 45.0, 640.0/480.0, 0.1, 9000.0
glMatrixMode GL_MODELVIEW
glLoadIdentity
'glEnable GL_TEXTURE_2D
glShadeModel GL_FLAT
glClearColor 0.0, 0.0, 0.0, 0.5
glClearDepth 1.0
glEnable GL_DEPTH_TEST
glDepthFunc GL_LEQUAL
glEnable GL_LIGHT0
glEnable GL_LIGHTING
glEnable GL_COLOR_MATERIAL
glCullFace(GL_BACK)
glEnable(GL_CULL_FACE)
glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST

glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_COLOR_ARRAY)
glEnableClientState(GL_NORMAL_ARRAY)
' Some useful helper functions
' This sub prints messages to the console for debugging
sub cPrint( byref text as string = "" )
	dim as integer fn = freeFile()
	
	open cons for output as fn
		? #fn, text
	close( fn )
end sub

' This sub exits the program with an error message
sub errorExit( byref msg as const string )
  if( screenPtr() <> 0 ) then
  	screen( 0 )
  end if
  
  dim as integer w, h
  screenInfo( w, h )
  
  w *= 0.75
  h *= 0.75
  
  screenres( w, h )
  width w / 8, h / 16
  
  ? "GL Error: ";
  ? msg

  sleep()
  
  end( 1 )
end sub


#macro glBindProc( n )
	dim shared as PFN##n##PROC n
	
	n = screenGLProc( #n )
	
	if( n = 0 ) then
		errorExit( "glBindProc(): failed to bind " & #n )
	end if
#endmacro

glBindProc( glGenVertexArrays )
glBindProc( glBindVertexArray )
glBindProc( glGenBuffers )
glBindProc( glBindBuffer )
glBindProc( glBufferData )
glBindProc( glEnableVertexAttribArray )
glBindProc( glDisableVertexAttribArray )
glBindProc( glVertexAttribPointer )
glBindProc( glCreateShader )
glBindProc( glShaderSource )
glBindProc( glCompileShader )
glBindProc( glGetShaderiv )
glBindProc( glGetShaderInfoLog )
glBindProc( glDeleteShader )
glBindProc( glCreateProgram )
glBindProc( glAttachShader )
glBindProc( glLinkProgram )
glBindProc( glDetachShader )
glBindProc( glGetProgramiv )
glBindProc( glGetProgramInfoLog )
glBindProc( glUseProgram )
glBindProc( glDeleteProgram )
glBindProc( glDeleteBuffers )
glBindProc( glDeleteVertexArrays )
glBindProc( glGetUniformLocation )
glBindProc( glUniform1f )
glBindProc( glUniform1i )
glBindProc( glUniform2f )
glBindProc( glUniform2fv )
glBindProc( glUniform3fv )
glBindProc( glActiveTexture )
glBindProc( glUniformMatrix3fv )
glBindProc( glDrawArraysInstanced )

'==============================================================
'main

type vertex
    dim as glfloat x,y,z
    dim as glfloat r,g,b
    dim as glfloat nx,ny,nz
end type

declare sub CreateCubes()

dim as GLuint vboId
dim shared box as uinteger

dim shared as vertex vertices(6*(nc*nc))
dim as vertex ptr verticess=@vertices(0)
declare sub cube(vertices() as vertex,i as integer,x as integer,y as integer,z as integer,r as integer, g as integer,b as integer)
dim as integer i
for x as integer=0 to nc-1
    for z as integer=0 to nc-1
        cube(vertices(),i,x*2,(rnd(1)*2),z*2,rnd(1),rnd(1),rnd(1))
        i+=1
    next
next
'=========================================================
'-------------------Vbo setup-----------------------------
if vbomode=true then
glGenBuffers(1, @vboId)
glBindBuffer(GL_ARRAY_BUFFER, vboId)
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), verticess, GL_STATIC_DRAW)
glVertexPointer(3, GL_FLOAT, 0, 0)
glColorPointer(  3,GL_FLOAT,0,0)
glNormalPointer( GL_FLOAT,0,0)
else
'========================================================
'---------------------normal setup-----------------------
glVertexPointer(3,GL_FLOAT,sizeof(vertex),verticess)
glColorPointer(  3,GL_FLOAT,sizeof(vertex),@Vertices(0).r)
glNormalPointer( GL_FLOAT,sizeof(vertex),@Vertices(0).nx)
end if
'=========================================================

dim as integer num_indices = 6*i

do
glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
glLoadIdentity
glRotatef 30, 1.0, 0,0
glRotatef 0, 0, 1.0, 0
glTranslatef -200,-20,-200
glDrawArraysInstanced(GL_TRIANGLES, 0, num_indices,1)
flip
loop until multikey(1)







sub cube(vertices() as vertex,i as integer,x as integer,y as integer,z as integer,r as integer, g as integer,b as integer)
i=i*6

for j as integer=0 to 5
    vertices(i+j).r=r
    vertices(i+j).g=g
    vertices(i+j).b=b
    vertices(i+j).nx=0
    vertices(i+j).ny=1
    vertices(i+j).nz=0
next


'top
vertices(i+0).x = -1.0+x
vertices(i+0).y = 1.0+y
vertices(i+0).z = -1.0+z

vertices(i+1).x = -1.0+x
vertices(i+1).y = 1.0+y
vertices(i+1).z = 1.0+z

vertices(i+2).x = 1.0+x
vertices(i+2).y = 1.0+y
vertices(i+2).z = -1.0+z

vertices(i+3).x = -1.0+x
vertices(i+3).y = 1.0+y
vertices(i+3).z = 1.0+z

vertices(i+4).x = 1.0+x
vertices(i+4).y = 1.0+y
vertices(i+4).z = 1.0+z

vertices(i+5).x = 1.0+x
vertices(i+5).y = 1.0+y
vertices(i+5).z = -1.0+z

end sub

paul doe
Moderator
Posts: 1732
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Are VBOs and Instancing not available in freebasic's OpenGL?

Post by paul doe »

Boromir wrote:Also how can I make the instanced drawing mode not stack them all in the same spot
You're welcome. Mmm, seems like you're mixing the old fixed function pipeline with the new programable pipeline. All the glDrawSomething() functions are meant to be used with shaders. See here for an explanation on how this works.

If you don't know how to use shaders, look at the demo I pointed you before. It's used for 2D drawing, but in 3D it's the same. Do note, however, that all the calls to glRotate(), glTranslate() and such don't work anymore (also, GLUT is deprecated too, so beware), you'll have to provide your own matrices to the shader to do that. Also, pay attention that the matrices are to be uploaded TRANSPOSED to the GPU from FreeBasic via the glUniformMatrix3fv() call, like this:

Code: Select all

glUniformMatrix3fv( location, 1, GL_TRUE, @rotation.a )
The third parameter is set to GL_TRUE to transpose them. Do note, however, that this transposes the matrices on GPU memory, you'll still have to treat them like column matrices in the shader and post-multiply them with the vertices to transform them (look at the code for the vertex shader, the files with a '.vert' extension).

Have a look at this: https://github.com/glasyalabolas/fb-3d-playground. There you'll find the math you'll need (4x4 matrices and vectors, no quaternions as I didn't get to add them yet).
Post Reply