Last active
June 8, 2020 08:47
-
-
Save hujewelz/bab7120348da8d568da5ff95bd376185 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Modules should be imported | |
import Foundation | |
import Cglew | |
import Cglfw | |
import OpenGL.GL3 | |
// Shader source code | |
fileprivate var vShaderCode = """ | |
#version 330 core\n | |
layout (location = 0) in vec3 vertex;\n | |
void main() {\n | |
gl_Position = vec4(vertex, 1.0);\n | |
}\0 | |
""" | |
fileprivate var fShaderCode = """ | |
#version 330 core\n | |
out lowp vec4 FragColor;\n | |
void main() {\n | |
FragColor = vec4(1.0, 0.1, 0.0, 1.0);\n | |
}\0 | |
""" | |
public class GLApp { | |
// Setup vertices data | |
private var VBO: UInt32 = 0 | |
private var VAO: UInt32 = 0 | |
private var shader: Shader! | |
func setup() { | |
var vertices: [Float] = [ | |
-0.5, -0.5, 0.0, // left | |
0.5, -0.5, 0.0, // right | |
0.0, 0.5, 0.0 // top | |
] | |
shader = Shader(vShaderCode: vShaderCode, fShaderCode: fShaderCode) | |
shader.compileShader() | |
glGenVertexArrays(1, &VAO) | |
glBindVertexArray(VAO) | |
glGenBuffers(1, &VBO) | |
glBindBuffer(GLenum(GL_ARRAY_BUFFER), VBO) | |
glBufferData(GLenum(GL_ARRAY_BUFFER), vertices.count * MemoryLayout<Float>.size, &vertices, GLenum(GL_STATIC_DRAW)) | |
glEnableVertexAttribArray(0) | |
glVertexAttribPointer(0, 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(3 * MemoryLayout<Float>.size), nil) | |
glBindVertexArray(0) | |
glBindBuffer(GLenum(GL_ARRAY_BUFFER), 0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment