Last active
December 25, 2021 23:30
-
-
Save jphdotam/f5e2c89e13615c8755fd961ce3e16502 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
import nimgl/[glfw, opengl] | |
import shader | |
type Vertex {.packed.} = object | |
color: array[4, GLfloat] | |
position: array[3, GLfloat] | |
proc initVertex(color: array[4, GLfloat], position: array[3, GLfloat]): Vertex = Vertex(color: color, position: position) | |
var verts: array[4, Vertex] = [ | |
initVertex([0.11f, 0.8f, 0.76f, 1.0f], [-0.5f, -0.5f, 0.0f]), | |
initVertex([0.1f, 0.9f, 0.12f, 1.0f], [-0.5f, 0.5f, 0.0f]), | |
initVertex([0.12f, 0.9f, 0.1f, 1.0f], [0.5f, 0.5f, 0.0f]), | |
initVertex([0.12f, 0.1f, 0.9f, 1.0f], [0.5f, -0.5f, 0.0f]) | |
] | |
var elems: array[6, GLuint] = [ | |
0'u32, 1'u32, 2'u32, | |
0'u32, 2'u32, 3'u32 | |
] | |
proc main: void = | |
assert glfwInit() | |
glfwWindowHint(GLFWContextVersionMajor, 3) | |
glfwWindowHint(GLFWContextVersionMinor, 3) | |
glfwWindowHint(GLFWOpenglForwardCompat, GLFW_TRUE) # Used for Mac | |
glfwWindowHint(GLFWOpenglProfile, GLFW_OPENGL_CORE_PROFILE) | |
glfwWindowHint(GLFWResizable, GLFW_FALSE) | |
let w: GLFWWindow = glfwCreateWindow(800, 600, "NimGL", nil, nil) | |
assert w != nil | |
w.makeContextCurrent() | |
assert glInit() | |
# create vertex array object | |
var vao: GLuint | |
glGenVertexArrays(1, vao.addr) | |
glBindVertexArray(vao) | |
# create vertex buffer object, and copy vertex data | |
var vbo: GLuint | |
glGenBuffers(1, vbo.addr) | |
glBindBuffer(GL_ARRAY_BUFFER, vbo) | |
glBufferData(GL_ARRAY_BUFFER, cint(Vertex.sizeof * verts.len), verts.addr, GL_STATIC_DRAW) | |
# create an element arrray | |
var ebo: GLuint | |
glGenBuffers(1, ebo.addr) | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo) | |
glBufferData(GL_ELEMENT_ARRAY_BUFFER, cint(GLuint.sizeof * elems.len), elems[0].addr, GL_STATIC_DRAW) | |
var pid = compileShader() | |
glUseProgram(pid) | |
# Colour | |
glEnableVertexAttribArray(0) | |
glVertexAttribPointer(0, 4, EGL_FLOAT, false, GLfloat.sizeof * 7, cast[pointer](offsetOf(Vertex, color))) | |
# Position | |
glEnableVertexAttribArray(1) | |
glVertexAttribPointer(1, 3, EGL_FLOAT, false, GLfloat.sizeof * 7, cast[pointer](offsetOf(Vertex, position))) | |
while not w.windowShouldClose: | |
glClearColor(0.266f, 0.466f, 0.698f, 1.0f) | |
glClear(GL_COLOR_BUFFER_BIT) | |
glBindVertexArray(vao) | |
glDrawElements(GL_TRIANGLES, elems.len.cint, GL_UNSIGNED_INT, cast[pointer](0)) | |
w.swapBuffers() | |
glfwPollEvents() | |
w.destroyWindow() | |
glfwTerminate() | |
glDeleteVertexArrays(1, vao.addr) | |
glDeleteBuffers(1, vbo.addr) | |
main() |
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
import nimgl/[glfw, opengl] | |
var vertexShaderSource*: cstring = r""" | |
#version 330 core | |
layout (location = 0) in vec4 aColor; | |
layout (location = 1) in vec3 aPosition; | |
out vec4 fColor; | |
void main() | |
{ | |
fColor = aColor; | |
gl_Position = vec4(aPosition, 1.0); | |
}""" | |
var fragmentShaderSource*: cstring = r""" | |
#version 330 core | |
out vec4 FragColor; | |
in vec4 fColor; | |
void main() | |
{ | |
FragColor = fColor; | |
}""" | |
proc compileShader*(): GLuint = | |
var success: GLint | |
var infoLog: array[512, char] | |
# Vertex shader | |
var vertexShader: uint32 = glCreateShader(GL_VERTEX_SHADER) | |
glShaderSource(vertexShader, 1, vertexShaderSource.addr, nil) | |
glCompileShader(vertexShader) | |
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, success.addr) | |
if success == 0: | |
glGetShaderInfoLog(vertexShader, 512, nil, infoLog.addr) | |
echo "ERROR::SHADER::VERTEX::COMPILATION FAILED\n", $infoLog | |
# Fragment shader | |
var fragmentShader = glCreateShader(GL_FRAGMENT_SHADER) | |
glShaderSource(fragmentShader, 1, fragmentShaderSource.addr, nil) | |
glCompileShader(fragmentShader) | |
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, success.addr) | |
if success == 0: | |
glGetShaderInfoLog(fragmentShader, 512, nil, infoLog.addr) | |
echo "ERROR::SHADER::FRAGMENT::COMPILATION FAILED\n", $infoLog | |
# Link shaders | |
var programId = glCreateProgram() | |
glAttachShader(programId, vertexShader) | |
glAttachShader(programId, fragmentShader) | |
glLinkProgram(programId) | |
glGetProgramiv(programId, GL_LINK_STATUS, success.addr) | |
if success != GL_TRUE.ord: | |
glGetProgramInfoLog(programId, 512, nil, infoLog.addr) | |
echo "ERROR::SHADER::PROGRAM::LINKING FAILED\n", $infoLog | |
glDeleteShader(vertexShader) | |
glDeleteShader(fragmentShader) | |
return programId |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment