http://antongerdelan.net/opengl/hellotriangle.html
so your card should support OpenGL 4.x and have its drivers installed
i have considered C++ as a pseudo code
what we need is the
"GLEXT.PBI" from luis here: http://purebasic.fr/english/viewtopic.p ... 87#p348385
"MoreFunctions.pbi" attached below which contains functions some from xorc1zt include file in the OpenGL 3.3 Base (windows) and what is related to opengl 4 i have used the definitions in http://www.rastertek.com/gl40tut03.html
native "OpenGL.pbi" refered to remotely
it is tutorial number one in that page from many goodies available
http://antongerdelan.net/opengl/index.html
MoreFunctions.pbi _ also include the "GLEXT.PBI" mentioned above
Code: Select all
;from luis reply http://purebasic.fr/english/viewtopic.php?f=13&t=45687&start=15#p446875
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 PFNGLGENVERTEXARRAYSPROC (n.i, *arrays)
Global glGenVertexArrays.PFNGLGENVERTEXARRAYSPROC
glGenVertexArrays = wglGetProcAddress_( "glGenVertexArrays" )
Prototype PFNGLBINDVERTEXARRAYPROC(n.i)
Global glBindVertexArray.PFNGLBINDVERTEXARRAYPROC
glBindVertexArray = wglGetProcAddress_( "glBindVertexArray" )
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.i PFNGLCREATESHADERPROC ( type.l )
Global glCreateShader.PFNGLCREATESHADERPROC
glCreateShader = wglGetProcAddress_( "glCreateShader" )
Prototype PFNGLSHADERSOURCEPROC ( shader.i, count.i, *stringBuffer, *length )
Global glShaderSource.PFNGLSHADERSOURCEPROC
glShaderSource = wglGetProcAddress_( "glShaderSource" )
Prototype PFNGLCOMPILESHADERPROC ( shader.i )
Global glCompileShader.PFNGLCOMPILESHADERPROC
glCompileShader = wglGetProcAddress_( "glCompileShader" )
Prototype PFNGLATTACHSHADERPROC ( program.i, shader.i )
Global glAttachShader.PFNGLATTACHSHADERPROC
glAttachShader = wglGetProcAddress_( "glAttachShader" )
Prototype PFNGLLINKPROGRAMPROC ( program.i )
Global glLinkProgram.PFNGLLINKPROGRAMPROC
glLinkProgram = wglGetProcAddress_( "glLinkProgram" )
Prototype.i PFNGLCREATEPROGRAMPROC ( )
Global glCreateProgram.PFNGLCREATEPROGRAMPROC
glCreateProgram = wglGetProcAddress_( "glCreateProgram" )
Prototype PFNGLUSEPROGRAMPROC ( program.i )
Global glUseProgram.PFNGLUSEPROGRAMPROC
glUseProgram = wglGetProcAddress_( "glUseProgram" )
Prototype PFNGLDRAWARRAYSPROC ( mode.l, first.i, count.i )
Global glDrawArrays.PFNGLDRAWARRAYSPROC
glDrawArrays = wglGetProcAddress_( "glDrawArrays" )
Code: Select all
OpenWindow(0, 10, 10, 640, 480, "OpenGL demo")
SetWindowColor(0, RGB(200,220,200))
OpenGLGadget(0, 20, 10, WindowWidth(0)-40 , WindowHeight(0)-20)
IncludeFile "MoreFunctions.pbi"
IncludeFile "GLEXT.pbi"
Global Dim points.f(8)
points(0)= 0.0 : points(1)= 0.5 : points(2)=0.0
points(3)= 0.5 : points(4)= -0.5 : points(5)= 0.0
points(6)= -0.5 : points(7)= -0.5 : points(8)= 0.0
Define vertex_shader.s
Define fragment_shader.s
Define *vbuff
;vertex shader source code
vertex_shader = "#version 400"+#CRLF$
vertex_shader + "in vec3 vp;"+#CRLF$
vertex_shader + "void main() { "+#CRLF$
vertex_shader + "gl_Position = vec4 (vp, 1.0);"+#CRLF$
vertex_shader + "}"
;fragment shader source code
fragment_shader = "#version 400"+#CRLF$
fragment_shader + "out vec4 frag_colour;"+#CRLF$
fragment_shader + "void main() {"+#CRLF$
fragment_shader + "frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);"+#CRLF$
fragment_shader + "}"
*vbuff = @vertex_shader
*fbuff = @fragment_shader
glEnable_(#GL_DEPTH_TEST); // enable depth-testing
glDepthFunc_(#GL_LESS); // depth-testing interprets a smaller value as "closer"
Global vbo.i,vao.i
;=================================================================================
glGenBuffers( 1, @vbo )
glBindBuffer(#GL_ARRAY_BUFFER, vbo )
glBufferData(#GL_ARRAY_BUFFER,9 * SizeOf(float), @points(0), #GL_STATIC_DRAW)
glGenVertexArrays(1, @vao);
glBindVertexArray (vao) ;
glEnableVertexAttribArray (0);
glBindBuffer (#GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer (0, 3, #GL_FLOAT, #GL_FALSE, 0, #Null);
Global vs = glCreateShader(#GL_VERTEX_SHADER);
glShaderSource(vs, 1, @*vbuff, #Null) ;
glCompileShader(vs);
Global fs = glCreateShader(#GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, @*fbuff, #Null);
glCompileShader(fs) ;
Global shader_programme = glCreateProgram();
glAttachShader(shader_programme, fs);
glAttachShader(shader_programme, vs);
glLinkProgram(shader_programme) ;
Repeat
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT);
glUseProgram(shader_programme)
glBindVertexArray(vao);
;draw points 0-3 from the currently bound VAO With current in-use shader
glDrawArrays(#GL_TRIANGLES, 0, 3);
;SwapBuffers_(hdc)
;Delay(16)
SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
Until WindowEvent() = #PB_Event_CloseWindow
is a rotating rectangle, and easier than the first example.
so we can use purebasic to learn opengl 4+ much more easily than the complexities in C/C++
Code: Select all
Structure TVertex
x.f
y.f
z.f
EndStructure
OpenWindow(0, 10, 10, 640, 480, "OpenGL demo")
SetWindowColor(0, RGB(200,220,200))
OpenGLGadget(0, 20, 10, WindowWidth(0)-40 , WindowHeight(0)-20)
IncludeFile "MoreFunctions.pbi"
IncludeFile "GLEXT.pbi"
Define vertex_shader.s
Define fragment_shader.s
glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_();
gluPerspective_(45.0, 800/600, 1.0, 60.0)
glMatrixMode_(#GL_MODELVIEW)
glTranslatef_(0, 0, -5)
glShadeModel_(#GL_SMOOTH)
glEnable_(#GL_DEPTH_TEST)
;glEnable_(#GL_CULL_FACE)
glColor3f_(1.0, 0.3, 0.0)
glViewport_(0, 0, 800, 600)
Global vbo.i,vao.i
Dim Vertex.TVertex(3)
Vertex(0)\x = 1
Vertex(0)\y = -1
Vertex(0)\z = 0
Vertex(1)\x = -1
Vertex(1)\y = -1
Vertex(1)\z = 0
Vertex(2)\x = 1
Vertex(2)\y = 1
Vertex(2)\z = 0
Vertex(3)\x = -1
Vertex(3)\y = 1
Vertex(3)\z = 0
;=================================================================================
;=================================================================================
Dim index.l(5)
index(0) = 1
index(1) = 0
index(2) = 2
index(3) = 3
index(4) = 1
index(5) = 2
;indexsize = 6 ;#GL_UNSIGNED_BYTE
indexsize = 6*2 ;#GL_UNSIGNED_SHORT
indexsize = 6*4 ;#GL_UNSIGNED_INT
glGenBuffers( 1, @vbo ) ; Vertex Buffer Object
glBindBuffer(#GL_ARRAY_BUFFER, vbo )
glBufferData(#GL_ARRAY_BUFFER,SizeOf(TVertex)*4,@Vertex(0), #GL_STATIC_DRAW)
glBindBuffer(#GL_ARRAY_BUFFER,0);
glGenBuffers( 1, @vao ); Vertex Attribute Object
glBindBuffer(#GL_ELEMENT_ARRAY_BUFFER, vao);
glBufferData(#GL_ELEMENT_ARRAY_BUFFER, indexsize,@index(0),#GL_STATIC_DRAW);
glBindBuffer(#GL_ELEMENT_ARRAY_BUFFER, 0);
rot.f = 1
Repeat
glViewport_(0, 0, WindowWidth(0), WindowHeight(0))
glClearColor_(0.2, 0.9, 0.2, 1)
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
glEnableClientState_(#GL_VERTEX_ARRAY )
glRotatef_(rot, 0, 1, 0);
glBindBuffer(#GL_ARRAY_BUFFER, vbo)
glVertexPointer_(3, #GL_FLOAT,0,0)
glBindBuffer(#GL_ELEMENT_ARRAY_BUFFER, vao)
glDrawElements_(#GL_TRIANGLES,indexsize,#GL_UNSIGNED_INT,0)
glBindBuffer(#GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(#GL_ARRAY_BUFFER,0);
glDisableClientState_(#GL_VERTEX_ARRAY);
;SwapBuffers_(hdc)
;Delay(16)
SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
Until WindowEvent() = #PB_Event_CloseWindow