' https://learnopengl.com/SuperStrictFramework GLFW.GLFWWindowImport GLFW.GLFWImport GLFW.GLFWOpenGLImport GLFW.GLFWSystemImport BRL.StandardIOLocal app_name:String = "Shaders Interpolation"Const SCR_WIDTH:UInt = 800Const SCR_HEIGHT:UInt = 600Type TGameWindow Extends TGLFWWindow Method OnFrameBufferSize (width:Int, height:Int) glViewport (0, 0, width, height) EndMethod EndTypeFunction ProcessInput (window:TGLFWWindow) If window.IsKeyDown (GLFW_KEY_ESCAPE) window.SetShouldClose (True) EndIf EndFunctionTGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MAJOR, 3)TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MINOR, 3)TGLFWWindow.Hint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)?MacOS ' Ewww... TGLFWWindow.Hint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)?Local window:TGLFWWindow = New TGameWindow.Create (SCR_WIDTH, SCR_HEIGHT, app_name)If Not window Print "Failed to create GLFW window!" EndEndIfwindow.MakeContextCurrent ()gladLoadGL (glfwGetProcAddress)Local vertexShaderSource:StringvertexShaderSource :+ "#version 330 core~n"vertexShaderSource :+ "layout (location = 0) in vec3 aPos;~n"vertexShaderSource :+ "layout (location = 1) in vec3 aColor;~n"vertexShaderSource :+ "out vec3 ourColor;~n"vertexShaderSource :+ "void main()~n"vertexShaderSource :+ "{~n"vertexShaderSource :+ " gl_Position = vec4(aPos, 1.0);~n"vertexShaderSource :+ " ourColor = aColor;~n"vertexShaderSource :+ "}~n"Local vertexShader:Int = glCreateShader (GL_VERTEX_SHADER)glShaderSource (vertexShader, 1, vertexShaderSource)glCompileShader (vertexShader)Local success:IntLocal infoLog:Byte [512]glGetShaderiv (vertexShader, GL_COMPILE_STATUS, Varptr success)If Not success glGetShaderInfoLog (vertexShader, 512, Null, infoLog) Print "Vertex shader compilation failed: " + String.FromCString (infoLog)EndIfLocal fragmentShaderSource:StringfragmentShaderSource :+ "#version 330 core~n"fragmentShaderSource :+ "out vec4 FragColor;~n"fragmentShaderSource :+ "in vec3 ourColor;~n"fragmentShaderSource :+ "void main()~n"fragmentShaderSource :+ "{~n"fragmentShaderSource :+ " FragColor = vec4(ourColor, 1.0f);~n"fragmentShaderSource :+ "}~n"Local fragmentShader:Int = glCreateShader (GL_FRAGMENT_SHADER)glShaderSource (fragmentShader, 1, fragmentShaderSource)glCompileShader (fragmentShader)glGetShaderiv (fragmentShader, GL_COMPILE_STATUS, Varptr success)If Not success glGetShaderInfoLog (fragmentShader, 512, Null, infoLog) Print "Fragment shader compilation failed: " + String.FromCString (infoLog)EndIfLocal shaderProgram:Int = glCreateProgram ()glAttachShader (shaderProgram, vertexShader)glAttachShader (shaderProgram, fragmentShader)glLinkProgram (shaderProgram)glGetProgramiv (shaderProgram, GL_LINK_STATUS, Varptr success)If Not success glGetProgramInfoLog (shaderProgram, 512, Null, infoLog) Print "Shader program linking failed: " + String.FromCString (infoLog)EndIfglDeleteShader (vertexShader)glDeleteShader (fragmentShader)Local vertices:Float [] = [.. 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, .. -0.5, -0.5, 0.0, 0.0, 1.0, 0.0, .. 0.0, 0.5, 0.0, 0.0, 0.0, 1.0]Local VBO:UIntLocal VAO:UIntglGenVertexArrays (1, Varptr VAO)glGenBuffers (1, Varptr VBO)glBindVertexArray (VAO)glBindBuffer (GL_ARRAY_BUFFER, VBO)glBufferData (GL_ARRAY_BUFFER, vertices.length * SizeOf (0:Float), vertices, GL_STATIC_DRAW)glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 6 * Sizeof (0:Float), 0:Byte Ptr)glEnableVertexAttribArray (0)Local attribute_offset:Int = 3 * Sizeof (0:Float)glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, 6 * Sizeof (0:Float), Byte Ptr (attribute_offset))glEnableVertexAttribArray (1)glUseProgram (shaderProgram)While Not window.ShouldClose () ProcessInput (window) glClearColor (0.2, 0.3, 0.3, 1.0) glClear (GL_COLOR_BUFFER_BIT) glBindVertexArray (VAO) glDrawArrays (GL_TRIANGLES, 0, 3) window.SwapBuffers () PollSystem () WendglDeleteVertexArrays (1, Varptr VAO)glDeleteBuffers (1, Varptr VBO)End
I like this glfw.mod. I have some codes in glfw3 which I like to convert to Blitzmax. I downloaded glfw.mod, but it seems bmk makemods -a glfw.mod is not the way. Can someone help me how to use glfw.mod?
bmk makemods -a glfw
Function readShaderFile:String(file:String) Local shaderSource:String Local lineFromFile:String Local in:TStream = OpenStream(file) While Not Eof(in) lineFromFile = ReadLine(in) shaderSource :+ lineFromFile + "~n" Wend CloseStream(in) Return shaderSourceEnd Function
#version 330 coreout vec4 FragColor;in vec3 ourColor;void main(){ FragColor = vec4(ourColor, 1.0f);}
#version 330 corelayout (location = 0) in vec3 aPos;layout (location = 1) in vec3 aColor;uniform mat4 projection;uniform mat4 camera;out vec3 ourColor;void main(){ gl_Position = projection * camera * vec4(aPos, 1.0); ourColor = aColor;}
SuperStrictFramework GLFW.GLFWWindowImport GLFW.GLFWImport GLFW.GLFWMonitorImport GLFW.GLFWOpenGLImport GLFW.GLFWSystemImport BRL.StandardIOImport BRL.LinkedListImport BRL.MatrixImport BRL.QuaternionImport BRL.Vector' CAMERA STUFFLocal view_matrix:SMat4FLocal projection_matrix:SMat4F' -----------------------------------------------------------------------------Const HACK:Int = False ' H A C K M E !' -----------------------------------------------------------------------------Local near:FloatLocal far:FloatLocal fov:FloatLocal position:SVec3FLocal tri_scale:FloatIf Not HACK ' Settings used in tutorials near = 0.1 far = 10.0 fov = Radians (50.0) position = New SVec3F (3.0, 3.0, 3.0) tri_scale = 1.0Else ' Settings that render something! near = 0.1 far = 250.0 fov = Radians (50.0) position = New SVec3F (3.5, 3.5, 3.5) tri_scale = 0.5EndIf' https://learnopengl.com/Getting-started/Transformations' https://www.tomdalling.com/blog/modern-opengl/03-matrices-depth-buffering-animation/' https://github.com/tomdalling/opengl-series/blob/master/source/03_matrices/source/main.cppLocal TMP_BasicCube:Float [] = [.. .. .. .. ' x y z -tri_scale, tri_scale, tri_scale, 1.0, 0.0, 0.0, .. ' Near - Red tri_scale, -tri_scale, tri_scale, 1.0, 0.0, 0.0, .. -tri_scale, -tri_scale, tri_scale, 1.0, 0.0, 0.0, .. -tri_scale, tri_scale, tri_scale, 0.5, 0.0, 0.0, .. tri_scale, tri_scale, tri_scale, 0.5, 0.0, 0.0, .. tri_scale, -tri_scale, tri_scale, 0.5, 0.0, 0.0, .. tri_scale, tri_scale, -tri_scale, 0.0, 1.0, 0.0, .. ' Far - Green -tri_scale, -tri_scale, -tri_scale, 0.0, 1.0, 0.0, .. tri_scale, -tri_scale, -tri_scale, 0.0, 1.0, 0.0, .. tri_scale, tri_scale, -tri_scale, 0.0, 0.5, 0.0, .. -tri_scale, tri_scale, -tri_scale, 0.0, 0.5, 0.0, .. -tri_scale, -tri_scale, -tri_scale, 0.0, 0.5, 0.0, .. -tri_scale, tri_scale, -tri_scale, 0.0, 0.0, 1.0, .. ' Left - Blue -tri_scale, -tri_scale, tri_scale, 0.0, 0.0, 1.0, .. -tri_scale, -tri_scale, -tri_scale, 0.0, 0.0, 1.0, ..' -tri_scale, tri_scale, -tri_scale, 0.0, 0.0, 0.5, .. -tri_scale, tri_scale, tri_scale, 0.0, 0.0, 0.5, .. -tri_scale, -tri_scale, tri_scale, 0.0, 0.0, 0.5, .. tri_scale, tri_scale, tri_scale, 1.0, 1.0, 0.0, .. ' Right - Yellow tri_scale, -tri_scale, -tri_scale, 1.0, 1.0, 0.0, .. tri_scale, -tri_scale, tri_scale, 1.0, 1.0, 0.0, ..' tri_scale, tri_scale, tri_scale, 0.5, 0.5, 0.0, .. tri_scale, tri_scale, -tri_scale, 0.5, 0.5, 0.0, .. tri_scale, -tri_scale, -tri_scale, 0.5, 0.5, 0.0, .. -tri_scale, tri_scale, -tri_scale, 0.0, 1.0, 1.0, .. ' Top - Cyan tri_scale, tri_scale, tri_scale, 0.0, 1.0, 1.0, .. -tri_scale, tri_scale, tri_scale, 0.0, 1.0, 1.0, .. -tri_scale, tri_scale, -tri_scale, 0.0, 0.5, 0.5, .. tri_scale, tri_scale, -tri_scale, 0.0, 0.5, 0.5, .. tri_scale, tri_scale, tri_scale, 0.0, 0.5, 0.5, .. -tri_scale, -tri_scale, tri_scale, 1.0, 0.0, 1.0, .. ' Bottom - Magenta tri_scale, -tri_scale, -tri_scale, 1.0, 0.0, 1.0, .. -tri_scale, -tri_scale, -tri_scale, 1.0, 0.0, 1.0, .. -tri_scale, -tri_scale, tri_scale, 0.5, 0.0, 0.5, .. tri_scale, -tri_scale, tri_scale, 0.5, 0.0, 0.5, .. tri_scale, -tri_scale, -tri_scale, 0.5, 0.0, 0.5]' Demo window control...Global AppWindow:TGLFWWindow ' Application windowGlobal _RetroGraphicsWidth:Int = 800 ' Display widthGlobal _RetroGraphicsHeight:Int = 600 ' Display height'Retro3D (UInt (_RetroGraphicsWidth), UInt (_RetroGraphicsHeight))'TGLFWWindow.Hint (GLFW_SAMPLES, msaa_level)TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MAJOR, 3)TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MINOR, 3)TGLFWWindow.Hint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)?MacOS ' Ewww... TGLFWWindow.Hint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)?Local monitor:TGLFWMonitor = Null'If full_screen Then monitor = TGLFWMonitor.GetPrimaryMonitor ()AppWindow = New RetroGameWindow.Create (_RetroGraphicsWidth, _RetroGraphicsHeight, "Demo", monitor)If Not AppWindow Print "Failed to create GLFW window!" EndEndIfAppWindow.MakeContextCurrent ()gladLoadGL (glfwGetProcAddress)' DEPTH TESTINGglEnable (GL_DEPTH_TEST)glDepthFunc (GL_LESS)AppWindow.GetFrameBufferSize (_RetroGraphicsWidth, _RetroGraphicsHeight)' SHADER PROGRAMLocal program:Int' MESHLocal vertex_data:Float [] ' Mesh vertices (3D points)Local vao:UInt, vbo:UInt ' VAO and VBOLocal triangle_count:Int ' Pre-calc number of trianglesLocal vertex_count:Int ' Pre-calc number of vertices' ASSIGN CUBE DATA TO MESHvertex_data = TMP_BasicCubetriangle_count = (vertex_data.length / ATTRIBUTES_PER_VERTEX) / FLOATS_PER_ATTRIBUTE / VERTICES_PER_TRIANGLEvertex_count = triangle_count * VERTICES_PER_TRIANGLEConst FLOATS_PER_ATTRIBUTE:Int = 3 ' R, G, B and X, Y, Z each contain 3 floats... for now.Const ATTRIBUTES_PER_VERTEX:Int = 2 ' Vertex data contains both RGB and XYZ attributesConst VERTICES_PER_TRIANGLE:Int = 3 ' Just to avoid Magic Number in code!Global RetroFloatSize:Int = Sizeof (0:Float) ' Avoiding reallocating a 0 for obtaining pointer each time!Global VA_Offset:Int = FLOATS_PER_ATTRIBUTE * RetroFloatSizeGlobal VA_Stride:Int = VA_Offset * ATTRIBUTES_PER_VERTEX' LOAD SHADERSLocal vertex_source_file:String = "vertex.glsl"Local fragment_source_file:String = "fragment.glsl"If Not FileType (vertex_source_file) Then vertex_source_file = "shaders/" + vertex_source_fileIf Not FileType (fragment_source_file) Then fragment_source_file = "shaders/" + fragment_source_fileLocal vertex_source:String = LoadString (vertex_source_file)Local fragment_source:String = LoadString (fragment_source_file)' ---------------------------------------------------------------------' Create vertex shader object...' ---------------------------------------------------------------------Local vertex_shader:Int = glCreateShader (GL_VERTEX_SHADER)' ---------------------------------------------------------------------' Build and check vertex shader...' ---------------------------------------------------------------------glShaderSource (vertex_shader, 1, vertex_source)glCompileShader (vertex_shader)Local success:IntglGetShaderiv (vertex_shader, GL_COMPILE_STATUS, Varptr success)If Not success 'ShaderCompilationError (fragment_shader, fragment_source, vertex_source_file) Print "Failed to compile shader" EndEndIf' ---------------------------------------------------------------------' Create vertex shader object...' ---------------------------------------------------------------------Local fragment_shader:Int = glCreateShader (GL_FRAGMENT_SHADER)' ---------------------------------------------------------------------' Build and check fragment shader...' ---------------------------------------------------------------------glShaderSource (fragment_shader, 1, fragment_source)glCompileShader (fragment_shader)glGetShaderiv (fragment_shader, GL_COMPILE_STATUS, Varptr success)If Not success 'ShaderCompilationError (fragment_shader, fragment_source, vertex_source_file) Print "Failed to compile shader" EndEndIf' ---------------------------------------------------------------------' Create shader program and attach compiled shader objects...' ---------------------------------------------------------------------program:Int = glCreateProgram ()glAttachShader (program, vertex_shader)glAttachShader (program, fragment_shader)' ---------------------------------------------------------------------' Link and check shader program...' ---------------------------------------------------------------------glLinkProgram (program)glGetProgramiv (program, GL_LINK_STATUS, Varptr success)If Not success Print "" Print "Shader program linking failed: " + glGetProgramInfoLog (program) EndEndIf' ---------------------------------------------------------------------' Detach and delete shaders as no longer needed, now part of program...' ---------------------------------------------------------------------glDetachShader (program, vertex_shader)glDetachShader (program, fragment_shader)glDeleteShader (vertex_shader)glDeleteShader (fragment_shader)' ---------------------------------------------------------------------' Needs to be active before uniforms can be used!' ---------------------------------------------------------------------glUseProgram (program) ' ---------------------------------------------------------------------' VAO: Container for raw data...' ---------------------------------------------------------------------' Create and bind as current VAO...glGenVertexArrays (1, Varptr vao) ' NB. 1 is NUMBER of buffers, not an ID!glBindVertexArray (vao)' ---------------------------------------------------------------------' VBO: Raw data...' ---------------------------------------------------------------------' Create and bind as current VBO for currently-bound VAO...glGenBuffers (1, Varptr vbo) ' NB. 1 is NUMBER of buffers, not an ID!glBindBuffer (GL_ARRAY_BUFFER, vbo)' Set buffer data for currently-bound VBO...glBufferData (GL_ARRAY_BUFFER, SizeOf (vertex_data), vertex_data, GL_STATIC_DRAW)glEnableVertexAttribArray (0)glEnableVertexAttribArray (1)' ---------------------------------------------------------------------' Vertex attribute pointers operate on the currently-bound VAO...' ---------------------------------------------------------------------' Vertex attribute pointers below refer to those declared in vertex.glsl:' layout (location = 0) in vec3 aPos;' layout (location = 1) in vec3 aColor;' Vertices data format description (aPos)...glVertexAttribPointer (0, FLOATS_PER_ATTRIBUTE, GL_FLOAT, GL_FALSE, VA_Stride, Null)' Colour data format description (aColor)...glVertexAttribPointer (1, FLOATS_PER_ATTRIBUTE, GL_FLOAT, GL_FALSE, VA_Stride, Byte Ptr (VA_Offset))glBindBuffer (GL_ARRAY_BUFFER, 0)glBindVertexArray (0)glDisableVertexAttribArray (1)glDisableVertexAttribArray (0)' WINDING (clockwise) AND CULLINGglFrontFace (GL_CW)glEnable (GL_CULL_FACE)' CAMERAview_matrix = SMat4F.LookAt (position, New SVec3f (0.0, 0.0, 0.0), New SVec3f (0.0, 1.0, 0.0))projection_matrix = SMat4F.Perspective (fov, _RetroGraphicsWidth, _RetroGraphicsHeight, near, far)Local uni_cam:Int = glGetUniformLocation (program, "camera")glUniformMatrix4fv (uni_cam, 1, False, Varptr view_matrix.a)Local uni_prj:Int = glGetUniformLocation (program, "projection")glUniformMatrix4fv (uni_prj, 1, False, Varptr projection_matrix.a)' SET CLS COLOURglClearColor (0.1, 0.2, 0.75, 1.0)' MAIN LOOPWhile Not AppWindow.GetKey (GLFW_KEY_ESCAPE) ' CLEAR SCREEN/DEPTH BUFFER glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glUseProgram (program) ' Using vertex attributes 0 and 1 from vertex.glsl... glEnableVertexAttribArray (0) glEnableVertexAttribArray (1) glBindVertexArray (vao) ' Draw using currently-bound VAO... glDrawArrays (GL_TRIANGLES, 0, vertex_count) glBindVertexArray (0) ' Done using vertex attributes 0 and 1... glDisableVertexAttribArray (1) glDisableVertexAttribArray (0) AppWindow.SwapBuffers () PollSystem () WendAppWindow.SetShouldClose (True)glDeleteBuffers (1, Varptr vbo)glDeleteVertexArrays (1, Varptr vao)End' WINDOWFunction Retro3D:TGLFWWindow (width:UInt, height:UInt, full_screen:Int = False, msaa_level:Int = 0) TGLFWWindow.Hint (GLFW_SAMPLES, msaa_level) TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MAJOR, 3) TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MINOR, 3) TGLFWWindow.Hint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE) ?MacOS ' Ewww... TGLFWWindow.Hint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE) ? Local monitor:TGLFWMonitor = Null If full_screen Then monitor = TGLFWMonitor.GetPrimaryMonitor () AppWindow = New RetroGameWindow.Create (width, height, "Demo", monitor) If Not AppWindow Print "Failed to create GLFW window!" End EndIf AppWindow.MakeContextCurrent () gladLoadGL (glfwGetProcAddress) ' DEPTH TESTING glEnable (GL_DEPTH_TEST) glDepthFunc (GL_LESS) AppWindow.GetFrameBufferSize (_RetroGraphicsWidth, _RetroGraphicsHeight) Return AppWindowEndFunctionType RetroGameWindow Extends TGLFWWindow Method OnFrameBufferSize (width:Int, height:Int) glViewport (0, 0, width, height) AppWindow.GetFrameBufferSize (_RetroGraphicsWidth, _RetroGraphicsHeight) EndMethod EndTypeConst RETRO_RAD_DIVIDER:Float = 180.0 / PiConst RETRO_DEG_DIVIDER:Float = Pi / 180.0Function Degrees:Float (radians:Float) Return radians * RETRO_RAD_DIVIDEREndFunctionFunction Radians:Float (degrees:Float) Return degrees * RETRO_DEG_DIVIDEREndFunction
Function Perspective:SMat4F(fov:Float, w:Float, h:Float, n:Float, f:Float) Local ft:Float = 1.0 / Tan(fov * 0.5) Local nf:Float = 1.0 / (n - f) Return New SMat4F(ft, 0, 0, 0, .. 0, ft * w / h, 0, 0, .. 0, 0, (f + n) * nf, -1, .. 0, 0, (2.0 * f * n) * nf, 0) End Function
SuperStrictFramework GLFW.GLFWWindowImport GLFW.GLFWImport GLFW.GLFWOpenGLImport GLFW.GLFWSystemImport BRL.StandardIOImport BRL.Matrix'------------------------- gl & window setup --------------Type TGameWindow Extends TGLFWWindow Method OnFrameBufferSize (width:Int, height:Int) glViewport (0, 0, width, height) EndMethod EndTypeFunction ProcessInput (window:TGLFWWindow) If window.IsKeyDown (GLFW_KEY_ESCAPE) window.SetShouldClose (True) EndIf EndFunctionTGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MAJOR, 3)TGLFWWindow.Hint (GLFW_CONTEXT_VERSION_MINOR, 3)TGLFWWindow.Hint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)?MacOS TGLFWWindow.Hint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)?Local width:Int = 800, height:Int = 600Local app_name:String = "Medi Cube"Local window:TGLFWWindow = New TGameWindow.Create (width, height, app_name)If Not window Print "Failed to create GLFW window!" EndEndIfwindow.MakeContextCurrent ()gladLoadGL (glfwGetProcAddress)'-----------------------------------------------------Local numVAOs:Int=1Local numVBOs:Int=2Local cameraX:Float= 0, cameraY:Float=0, cameraZ:Float=8Local cubeLocX:Float = 0.0, cubeLocY:Float = 0.0, cubeLocZ:Float = 0.0 Local renderProgram:IntLocal vbo:UInt[numVBOs]Local vao:UInt[numVAOs]Local mvLoc:UInt, projLoc:UIntLocal aspect:FloatLocal pMat:SMat4F, vMat:SMat4F, mMat:SMat4F, mvMat:SMat4F'--------------------------- setup vertecies -------------------renderProgram = CreateShaderProgram("vshader.glsl", "fshader.glsl")Local vertexPositions:Float[] = [.. -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, .. 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, .. 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, -1.0, .. 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, .. 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, .. -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, .. -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, .. -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, .. -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, .. 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, .. -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, .. 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0]glGenVertexArrays (1, Varptr vao)glBindVertexArray (vao[0])glGenBuffers (numVBOs, Varptr vbo)glBindBuffer (GL_ARRAY_BUFFER, vbo[0])glBufferData (GL_ARRAY_BUFFER, SizeOf (vertexPositions ), vertexPositions, GL_STATIC_DRAW)' -------------- ------------- main loop -----------------------------------While Not window.ShouldClose () ProcessInput (window) glClearColor (0.2, 0.3, 0.3, 1.0) glClear (GL_COLOR_BUFFER_BIT) glUseProgram (renderProgram) mvLoc = glGetUniformLocation(renderProgram, "mv_matrix") projLoc = glGetUniformLocation(renderProgram, "proj_matrix") pMat = SMat4F.Perspective(60, width , height, 0.1, 1000.0) vMat = SMat4F.Translation(New SVec3F(-cameraX, -cameraY, -cameraZ)) mMat = SMat4F.Translation(New SVec3F(cubeLocX, cubeLocY, cubeLocZ)) mvMat = vMat * mMat; 'model-view glUniformMatrix4fv(mvLoc, 1, GL_FALSE, Varptr mvMat.a) glUniformMatrix4fv(projLoc, 1, GL_FALSE, Varptr pMat.a) glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); glVertexAttribPointer(0, 3, GL_FLOAT, False, 0, 0); glEnableVertexAttribArray(0); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glDrawArrays(GL_TRIANGLES, 0, 36); window.SwapBuffers () PollSystem () WendglDeleteVertexArrays (1, Varptr vao)glDeleteBuffers (1, Varptr vbo)End'------------------------- end --------------------------------'------------ function read shader file ----------------------Function readShaderFile:String(file:String) Local shaderSource:String Local lineFromFile:String Local in:TStream = OpenStream(file) While Not Eof(in) lineFromFile = ReadLine(in) shaderSource :+ lineFromFile + "~n" Wend CloseStream(in) Return shaderSourceEnd Function'------------- function create shader program ------------------Function CreateShaderProgram:Int(vsFile:String, frFile:String) ' read and compile vertex shader Local infoLog:String Local success:Int Local vertexShaderSource:String vertexShaderSource = readShaderFile(vsFile) Local vertexShader:Int = glCreateShader (GL_VERTEX_SHADER) glShaderSource (vertexShader, 1, vertexShaderSource) glCompileShader (vertexShader) glGetShaderiv (vertexShader, GL_COMPILE_STATUS, Varptr success) If Not success infoLog = glGetShaderInfoLog (vertexShader ) Print "Vertex shader compilation failed: " + String.FromCString (infoLog) EndIf ' read and compile fragment shader Local fragmentShaderSource:String fragmentShaderSource = readShaderFile("fshader.glsl") Local fragmentShader:Int = glCreateShader (GL_FRAGMENT_SHADER) glShaderSource (fragmentShader, 1, fragmentShaderSource) glCompileShader (fragmentShader) glGetShaderiv (fragmentShader, GL_COMPILE_STATUS, Varptr success) If Not success infoLog = glGetShaderInfoLog (fragmentShader) Print "Fragment shader compilation failed: " + String.FromCString (infoLog) EndIf ' link, attach and check Local shaderProgram:Int = glCreateProgram () glAttachShader (shaderProgram, vertexShader) glAttachShader (shaderProgram, fragmentShader) glLinkProgram (shaderProgram) glGetProgramiv (shaderProgram, GL_LINK_STATUS, Varptr success) If Not success infoLog = glGetProgramInfoLog (shaderProgram) Print "Shader program linking failed: " + String.FromCString (infoLog) EndIf ' have the program, don't need anymore glDeleteShader (vertexShader) glDeleteShader (fragmentShader) Return shaderProgramEnd Function
#version 330layout (location=0) in vec3 position;uniform mat4 mv_matrix;uniform mat4 proj_matrix;void main(void){ gl_Position = proj_matrix * mv_matrix * vec4(position,1.0);}
#version 330out vec4 color;void main(void){ color = vec4(1.0, 0.0, 1.0, 1.0);}