Skip to content

Instantly share code, notes, and snippets.

@hujewelz

hujewelz/GLApp.swift

Last active Jun 8, 2020
Embed
What would you like to do?
// 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