Commit 097519f8 authored by minus's avatar minus
Browse files

move shader compilation to extra module

parent 5cb97dbb
......@@ -10,6 +10,8 @@ import math
import strutils
import times
import shader
discard sdl2.init(INIT_VIDEO)
discard sdl2.glSetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
......@@ -52,50 +54,7 @@ glBindVertexArray(vao)
glClearColor(0.0, 0.0, 0.0, 1.0) # Set background color to black and opaque
glClearDepth(1.0) # Set background depth to farthest
#glEnable(GL_DEPTH_TEST) # Enable depth testing for z-culling
#glDepthFunc(GL_LEQUAL) # Set the type of depth-test
#glShadeModel(GL_SMOOTH) # Enable smooth shading
#glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) # Nice perspective corrections
type ShaderCompilerError = object of type Exception
proc load_shader(source_file: string, typ: GLenum): GLuint =
var shader = glCreateShader(typ)
var source = source_file.readFile
var sourcePtr = addr source[0]
shader.glShaderSource(1, cast[cstringArray](addr sourcePtr), nil)
shader.glCompileShader()
var status: GLint
shader.glGetShaderiv(GL_COMPILE_STATUS, addr status)
if status == GL_FALSE:
var buf: array[4096, char]
shader.glGetShaderInfoLog(GLsizei(buf.len), nil, buf)
raise newException(ShaderCompilerError, $buf)
shader
type ShaderLinkerError = object of type Exception
proc build_shader_program(shaders: varargs[GLuint]): GLuint =
var program = glCreateProgram()
for shader in shaders:
program.glAttachShader(shader)
# Shouldn't be needed, as the location for color is 0 by default
# https://gamedev.stackexchange.com/questions/72756/how-does-opengl-determine-different-outputs-with-shaders
#program.glBindFragDataLocation(0, "color")
program.glLinkProgram()
var status: GLint
program.glGetProgramiv(GL_LINK_STATUS, addr status)
if status == GL_FALSE:
var buf: array[4096, char]
program.glGetProgramInfoLog(GLsizei(buf.len), nil, buf)
raise newException(ShaderLinkerError, $buf)
for shader in shaders:
program.glDetachShader(shader)
shader.glDeleteShader()
program.glUseProgram()
program
glEnable(GL_DEPTH_TEST) # Enable depth testing for z-culling
var vertex_shader = load_shader("shaders/vertex.glsl", GL_VERTEX_SHADER)
var fragment_shader = load_shader("shaders/fragment.glsl", GL_FRAGMENT_SHADER)
......
import opengl
type ShaderCompilerError* = object of type Exception
proc load_shader*(source_file: string, typ: GLenum): GLuint =
var shader = glCreateShader(typ)
var source = source_file.readFile
var sourcePtr = addr source[0]
shader.glShaderSource(1, cast[cstringArray](addr sourcePtr), nil)
shader.glCompileShader()
var status: GLint
shader.glGetShaderiv(GL_COMPILE_STATUS, addr status)
if status == GL_FALSE:
var buf: array[4096, char]
shader.glGetShaderInfoLog(GLsizei(buf.len), nil, buf)
raise newException(ShaderCompilerError, $buf)
shader
type ShaderLinkerError* = object of type Exception
proc build_shader_program*(shaders: varargs[GLuint]): GLuint =
var program = glCreateProgram()
for shader in shaders:
program.glAttachShader(shader)
# Shouldn't be needed, as the location for color is 0 by default
# https://gamedev.stackexchange.com/questions/72756/how-does-opengl-determine-different-outputs-with-shaders
#program.glBindFragDataLocation(0, "color")
program.glLinkProgram()
var status: GLint
program.glGetProgramiv(GL_LINK_STATUS, addr status)
if status == GL_FALSE:
var buf: array[4096, char]
program.glGetProgramInfoLog(GLsizei(buf.len), nil, buf)
raise newException(ShaderLinkerError, $buf)
for shader in shaders:
program.glDetachShader(shader)
shader.glDeleteShader()
program.glUseProgram()
program
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment