Commit 4c0c11b7 authored by minus's avatar minus
Browse files

WIP migration to OpenGL 3

done so far:
- initialization
- shader loading
missing:
- shader linking
- setting uniforms, etc
- updating drawing code
- get something to do matrix calculations
parent 6fa61de2
......@@ -4,9 +4,21 @@
import sdl2
import opengl
import glu
import strutils
import times
discard sdl2.init(INIT_EVERYTHING)
discard sdl2.init(INIT_VIDEO)
discard sdl2.glSetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
discard sdl2.glSetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
discard sdl2.glSetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
discard sdl2.glSetAttribute(SDL_GL_DOUBLEBUFFER, 1);
discard sdl2.glSetAttribute(SDL_GL_DEPTH_SIZE, 24);
#
# Anti-aliasing
discard sdl2.glSetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
discard sdl2.glSetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
var screenWidth: cint = 640
var screenHeight: cint = 480
......@@ -16,18 +28,55 @@ var context = window.glCreateContext()
# Initialize OpenGL
loadExtensions()
proc printGlDebug(
source: GLenum,
typ: GLenum,
id: GLuint,
severity: GLenum,
length: GLsizei,
message: ptr GLchar,
userParam: pointer) {.stdcall.} =
echo "source=" & $source & " type=" & $typ & " id=" & $id & " severity=" & $severity & ": " & $message
glDebugMessageCallback(printGlDebug, nil)
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS)
glEnable(GL_DEBUG_OUTPUT)
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
#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 GL_VERTEX_SHADER
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(cast[GLsizei](buf.len), nil, buf)
raise newException(ShaderCompilerError, $buf)
shader
proc build_shader_program(shaders: varargs[GLuint]): GLuint =
for shader in shaders:
echo shader
0
var vertex_shader = load_shader("shaders/vertex.glsl", GL_VERTEX_SHADER)
var fragment_shader = load_shader("shaders/fragment.glsl", GL_FRAGMENT_SHADER)
var shader_program = build_shader_program(vertex_shader, fragment_shader)
proc reshape(newWidth: cint, newHeight: cint) =
glViewport(0, 0, newWidth, newHeight) # Set the viewport to cover the new window
glMatrixMode(GL_PROJECTION) # To operate on the projection matrix
glLoadIdentity() # Reset
gluPerspective(45.0, newWidth / newHeight, 0.1, 100.0) # Enable perspective projection with fovy, aspect, zNear and zFar
#glMatrixMode(GL_PROJECTION) # To operate on the projection matrix
#glLoadIdentity() # Reset
#gluPerspective(45.0, newWidth / newHeight, 0.1, 100.0) # Enable perspective projection with fovy, aspect, zNear and zFar
proc render(t: float) =
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT) # Clear color and depth buffers
......@@ -133,7 +182,7 @@ while runGame:
let newHeight = windowEvent.data2
reshape(newWidth, newHeight)
render(epochTime() - t0)
#render(epochTime() - t0)
limitFrameRate()
......
#version 150
in vec3 color;
in vec3 pos;
out vec4 out_color;
void main()
{
out_color = vec4(color, 1.0);
//out_color.rgb = vec3(pos.z/8 + 0.5);
}
#version 150
in vec3 in_position;
in vec3 in_color;
in vec3 in_normal;
out vec3 color;
out vec3 pos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;
void main()
{
color = in_color;
pos = in_position;
gl_Position = proj * view * model * vec4(in_position, 1.0);
}
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