glDrawArrays Invalid Memory Access
Posted: Thu Apr 23, 2015 11:30 pm
I'm in the process of converting some of my C++ code to Purebasic, but I hit a snag pretty early with glDrawArrays giving a memory error (caused by my incompetence).
I converted the freeglut window to Purebasic's OpenglGadget() so then you guys won't have to download any external files.
If you can get the example working the window should have a red background with a single white pixel in the center.
Any help is appreciated.
I converted the freeglut window to Purebasic's OpenglGadget() so then you guys won't have to download any external files.
If you can get the example working the window should have a red background with a single white pixel in the center.
Any help is appreciated.
Code: Select all
;-CONSTANTS
#GL_ARRAY_BUFFER = $8892
#GL_STATIC_DRAW = $88E4
;-STRUCTURES
Structure VertexData
X.f
Y.f
Z.f
EndStructure
Dim Vertex.VertexData(10)
Vertex(0)\X = 0.0
Vertex(0)\Y = 0.0
Vertex(0)\Z = 0.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"
EndImport
CompilerElse
Import "Opengl32.lib"
wglGetProcAddress_(s.p-ascii) As "wglGetProcAddress"
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 PFNGLDRAWARRAYSPROC(mode.l, first.i, count.i)
Global glDrawArrays.PFNGLDRAWARRAYSPROC
glDrawArrays = wglGetProcAddress_("glDrawArrays")
;-Debug Return Values
Debug "glGenBuffers = " + Str(glGenBuffers)
Debug "glBindBuffer = " + Str(glBindBuffer)
Debug "glBufferData = " + Str(glBufferData)
Debug "glEnableVertexAttribArray = " + Str(glEnableVertexAttribArray)
Debug "glVertexAttribPointer = " + Str(glVertexAttribPointer)
Debug "glDisableVertexAttribArray = " + Str(glDisableVertexAttribArray)
Debug "glDrawArrays = " + Str(glDrawArrays)
glGenBuffers(1, @VBO)
glBindBuffer(#GL_ARRAY_BUFFER, VBO)
glBufferData(#GL_ARRAY_BUFFER, SizeOf(VertexData), @Vertex(0), #GL_STATIC_DRAW)
glClearColor_(1.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