[Library] EGL4 Easy Opengl 4 - Easy access to new Opengl functions! [Win x64]

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
Mijikai
Addict
Addict
Posts: 1359
Joined: Sun Sep 11, 2016 2:17 pm

[Library] EGL4 Easy Opengl 4 - Easy access to new Opengl functions! [Win x64]

Post by Mijikai »

How to get access to all those fancy new Opengl functions ?

Solution: let egl4 create a Opengl 4.5 context and set up all functions for you.

Does that mean egl4 is "slow" and simply calls the Opengl functions ?
The answer is no - egl4 patches the import table so all calls will be direct !

Example:

Code: Select all

EnableExplicit

XIncludeFile "egl4.pbi"

Procedure.i ShaderFromString(Shader.s,Type.i)
  Protected *src
  Protected sdr.i
  Protected state.i
  If Shader
    *src = Ascii(Shader)
    If *src
      sdr = glCreateShader(Type)
      If sdr
        glShaderSource(sdr,1,@*src,#Null)
        glCompileShader(sdr)
        glGetShaderiv(sdr,#GL_COMPILE_STATUS,@state)
        If Not state = #GL_FALSE
          FreeMemory(*src)
          ProcedureReturn sdr
        EndIf
        glDeleteShader(sdr)
      EndIf
      FreeMemory(*src)
    EndIf
  EndIf
  ProcedureReturn #Null
EndProcedure

Procedure.i Main()
  Protected vbo.i
  Protected vao.i
  Protected exit.i
  Protected sdr_vert.s
  Protected sdr_frag.s
  Protected sdr_v.i
  Protected sdr_f.i
  Protected prog.i
  Protected hdc.i
  Protected va.i
  Protected vb.i
  If OpenWindow(0,0,0,800,600,#Null$,#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)
    If egl4ContextCreate(WindowID(0))
      egl4ContextApi(GetModuleHandle_(#Null))
      glGenVertexArrays(1,@vao)
      glGenBuffers(1,@vbo)
      glBindVertexArray(vao)
      glBindBuffer(#GL_ARRAY_BUFFER,vbo)
      glBufferData(#GL_ARRAY_BUFFER,?eod - ?vertices,?vertices,#GL_STATIC_DRAW)
      glVertexAttribPointer(0, 3, #GL_FLOAT, #GL_FALSE, 6 * 4,#Null)
      glEnableVertexAttribArray(0)
      glVertexAttribPointer(1, 3, #GL_FLOAT, #GL_FALSE, 6 * 4,3 * 4)
      glEnableVertexAttribArray(1)
      sdr_vert + "#version 330 core" + #CRLF$
      sdr_vert + "layout (location = 0) in vec3 aPos;" + #CRLF$
      sdr_vert + "layout (location = 1) in vec3 aColor;" + #CRLF$
      sdr_vert + "out vec3 ourColor;" + #CRLF$
      sdr_vert + "void main()" + #CRLF$
      sdr_vert + "{" + #CRLF$
      sdr_vert + "gl_Position = vec4(aPos, 1.0);" + #CRLF$
      sdr_vert + "ourColor = aColor;" + #CRLF$
      sdr_vert + "}" + #CRLF$
      sdr_frag + "#version 330 core" + #CRLF$
      sdr_frag + "out vec4 FragColor;" + #CRLF$
      sdr_frag + "in vec3 ourColor;" + #CRLF$
      sdr_frag + "void main()" + #CRLF$
      sdr_frag + "{" + #CRLF$
      sdr_frag + "FragColor = vec4(ourColor, 1.0f);" + #CRLF$
      sdr_frag + "}" + #CRLF$
      prog = glCreateProgram()
      sdr_v = ShaderFromString(sdr_vert,#GL_VERTEX_SHADER)
      sdr_f = ShaderFromString(sdr_frag,#GL_FRAGMENT_SHADER)
      glAttachShader(prog,sdr_v)
      glAttachShader(prog,sdr_f)
      glLinkProgram(prog)
      glUseProgram(prog) 
      hdc = egl4Device()
      SetWindowTitle(0,"OpenGL Version: " + PeekS(glGetString_(#GL_VERSION),-1,#PB_Ascii))
      Repeat
        Repeat
          Select WindowEvent()
            Case #Null
              Break
            Case #PB_Event_CloseWindow
              exit = #True
          EndSelect
        ForEver
        glClearColor_(0.2, 0.3, 0.3, 1.0)
        glClear_(#GL_COLOR_BUFFER_BIT)
        glBindVertexArray(VAO);
        glDrawArrays_(#GL_TRIANGLES, 0, 3);
        SwapBuffers_(hdc)
      Until exit Or GetAsyncKeyState_(#VK_ESCAPE) & $8000
      egl4ContextDestroy()
    EndIf
    CloseWindow(0)  
  EndIf  
  ProcedureReturn #Null
EndProcedure

Main()

End

DataSection
  vertices:
  Data.f  0.5,-0.5,0.0,1.0,0.0,0.0
  Data.f -0.5,-0.5,0.0,0.0,1.0,0.0
  Data.f  0.0, 0.5,0.0,0.0,0.0,1.0
  eod:
EndDataSection
Download alpha 1:
https://www.dropbox.com/scl/fi/v6aiqhge ... bbgvv&dl=0
User avatar
idle
Addict
Addict
Posts: 4976
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: [Library] EGL4 Easy Opengl 4 - Easy access to new Opengl functions! [Win x64]

Post by idle »

great very useful
threedslider
User
User
Posts: 75
Joined: Sat Feb 12, 2022 7:15 pm

Re: [Library] EGL4 Easy Opengl 4 - Easy access to new Opengl functions! [Win x64]

Post by threedslider »

Wow ! Fantastic your Lib is great and very useful too :D

Thank you Mijikai !
Post Reply