Page 1 of 1

OpenGL glMapBuffer question

Posted: Wed Apr 29, 2015 7:50 pm
by Samuel
I'm back with yet another OpenGL question. OK, glMapBuffer returns a pointer to the targeted buffer. You then can read or write to this buffer depending on how you access it.

In my case I'm trying to read the vertex data from the Array Buffer. I'm able to return the pointer with glMapBuffer, but I'm not sure how to use it for reading the data.

Here's the basic idea of what I'm trying to do. Instead of debugging, I assume you'd want to assign the pointer to something.

Code: Select all

Debug glMapBuffer(#GL_ARRAY_BUFFER, #GL_READ_ONLY)

;......READ ARRAY BUFFER DATA.

glUnmapBuffer(#GL_ARRAY_BUFFER)
Here's a basic example for testing and any help is appreciated.

Code: Select all

;Requires OpenGL 2.0 at least if I'm remembering correctly and windows.

;-CONSTANTS
#GL_ARRAY_BUFFER = $8892
#GL_STATIC_DRAW  = $88E4

#GL_READ_ONLY  = $88B8
#GL_WRITE_ONLY = $88B9
#GL_READ_WRITE = $88BA

;-STRUCTURES
Structure VertexData
  X.f
  Y.f
  Z.f
EndStructure
Dim Vertex.VertexData(100)
  Vertex(0)\X = 1.5
  Vertex(0)\Y = 0.2
  Vertex(0)\Z = 2.0 
  
  
;-DECLARES
Declare OGL_Render()


;-VARIABLES
Define.i argc = 0
  
Dim argv.s(0)
argv(0) = ""

Global.i VBO


;-MAIN WINDOW
OpenWindow(0, 0, 0, 800, 600, "Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenGLGadget(0, 0, 0, 800, 600)

;IncludeFile "Functions.pbi"

CompilerIf (#PB_Compiler_Processor = #PB_Processor_x86)
  Import "Opengl32.lib"
    wglGetProcAddress_(s.p-ascii) As "_wglGetProcAddress@4"
    glDrawArrays(mode.l, first.i, count.i) As "_glDrawArrays@12"
  EndImport
CompilerElse
  Import "Opengl32.lib"
    wglGetProcAddress_(s.p-ascii) As "wglGetProcAddress"
    glDrawArrays(mode.l, first.i, count.i) As "glDrawArrays"
  EndImport
CompilerEndIf

Prototype PFNGLGENBUFFERSPROC(n.i, *buffers)
Global glGenBuffers.PFNGLGENBUFFERSPROC
glGenBuffers = wglGetProcAddress_("glGenBuffers")

Prototype PFNGLBINDBUFFERPROC(target.l, buffer.i)
Global glBindBuffer.PFNGLBINDBUFFERPROC
glBindBuffer = wglGetProcAddress_("glBindBuffer")

Prototype PFNGLBUFFERDATAPROC(target.l, size.i, *Data_, usage.l)
Global glBufferData.PFNGLBUFFERDATAPROC
glBufferData = wglGetProcAddress_("glBufferData")

Prototype PFNGLENABLEVERTEXATTRIBARRAYPROC(index.i)
Global glEnableVertexAttribArray.PFNGLENABLEVERTEXATTRIBARRAYPROC
glEnableVertexAttribArray = wglGetProcAddress_("glEnableVertexAttribArray")

Prototype PFNGLVERTEXATTRIBPOINTERPROC(index.i, size.i, type.l, normalized.b, stride.i, *pointer)
Global glVertexAttribPointer.PFNGLVERTEXATTRIBPOINTERPROC
glVertexAttribPointer = wglGetProcAddress_("glVertexAttribPointer")

Prototype PFNGLDISABLEVERTEXATTRIBARRAYPROC(index.i)
Global glDisableVertexAttribArray.PFNGLDISABLEVERTEXATTRIBARRAYPROC
glDisableVertexAttribArray = wglGetProcAddress_("glDisableVertexAttribArray")

Prototype PFNGLMAPBUFFERPROC(Target.i, Access.i)
Global glMapBuffer.PFNGLMAPBUFFERPROC
glMapBuffer = wglGetProcAddress_("glMapBuffer")

Prototype PFNGLUNMAPBUFFERPROC(Target.i)
Global glUnmapBuffer.PFNGLUNMAPBUFFERPROC
glUnmapBuffer = wglGetProcAddress_("glUnmapBuffer")

glGenBuffers(1, @VBO)
glBindBuffer(#GL_ARRAY_BUFFER, VBO)
glBufferData(#GL_ARRAY_BUFFER, SizeOf(VertexData)*100, @Vertex(0), #GL_STATIC_DRAW)



Debug glMapBuffer(#GL_ARRAY_BUFFER, #GL_READ_ONLY)

;......READ ARRAY BUFFER DATA?

glUnmapBuffer(#GL_ARRAY_BUFFER)



glClearColor_(0.0, 0.0, 0.0, 1.0)

;-MAIN REPEAT
Repeat
  
  Event = WindowEvent()
  
  OGL_Render()
  
Until Event = #PB_Event_CloseWindow

End

Procedure OGL_Render()
  
  SetGadgetAttribute(0, #PB_OpenGL_SetContext, #True)
  
  glClear_(#GL_COLOR_BUFFER_BIT)
  
  glEnableVertexAttribArray(0)
  glBindBuffer(#GL_ARRAY_BUFFER, VBO)
  glVertexAttribPointer(0, 3, #GL_FLOAT, #GL_FALSE, 0, 0)
    
  glDrawArrays(#GL_POINTS, 0, 1)

  glDisableVertexAttribArray(0)
  
  SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
  
EndProcedure

Re: OpenGL glMapBuffer question

Posted: Thu Apr 30, 2015 12:31 am
by luis
Do you mean this ?

Code: Select all


*p.VertexData = glMapBuffer(#GL_ARRAY_BUFFER, #GL_READ_ONLY)

For k = 0 To 10
 Debug *p\X
 Debug *p\Y
 Debug *p\Z
 *p + SizeOf(VertexData)
Next


Re: OpenGL glMapBuffer question

Posted: Thu Apr 30, 2015 12:57 am
by Samuel
Thanks, Luis. That's exactly what I'm looking for.

I'm optimizing my render routine by creating large buffers (maybe 2 or 3 mbs) that can contain many entities and I was trying to figure out how to edit the buffers when new entities are created.

I'm not sure if glMapBuffer is the fastest way, but it is a start. I still want to look into glMapBufferRange and glBufferSubData for possibly faster options.