Commit ae8efe9a authored by Charles Chamberlain's avatar Charles Chamberlain

Making a rainbow triangle!

parent 33e47162
......@@ -21,6 +21,7 @@ use @SDL_CreateWindow[Pointer[SDLWindow]](
use @SDL_DestroyWindow[None](window: Pointer[SDLWindow] box)
use @SDL_Quit[None]()
use @SDL_PollEvent[U32](i: Pointer[SDLEvent])
use @SDL_GetTicks[U32]()
use @SDL_GL_SwapWindow[None](window: Pointer[SDLWindow])
use @SDL_GL_CreateContext[Pointer[SDLWindow]](window: Pointer[SDLWindow])
......@@ -43,13 +44,15 @@ use @glGenBuffers[None](n: I32, pointer: Pointer[U32])
use @glBindBuffer[None](kind: U32, buffer: U32)
use @glBufferData[None](kind: U32, size: USize, array: Pointer[F32] tag, mode: U32)
use @glDrawArrays[None](kind: U32, buffer: U32, number: U32)
use @glGetAttribLocation[U32](program_id: U32, attribute: Pointer[U8] tag)
use @glGetUniformLocation[U32](program_id: U32, attr: Pointer[U8] tag)
use @glGetAttribLocation[U32](program_id: U32, attr: Pointer[U8] tag)
use @glVertexAttribPointer[None](
index: U32, size: U32, type': U32,
normalized: U8, stride: USize, pointer: Pointer[None]
normalized: U8, stride: I32, pointer: U32
)
use @glEnableVertexAttribArray[None](attribute_id: U32)
use @glEnable[None](mode: U32)
use @glUniform1f[None](id: U32, f: F32)
primitive NULL fun apply(): Pointer[None] => Pointer[None].create()
......@@ -87,11 +90,20 @@ primitive Errors
fun process() =>
@process_errors[None]()
class Uniform
var _id: U32
new create(id: U32) =>
_id = id
fun set_float(f: F32) =>
@glUniform1f(_id, f)
class ShaderProgram
var _program: U32
var _env: Env
var _uniforms: Array[String] = Array[String].create()
new create(env: Env) =>
_env = env
......@@ -128,8 +140,16 @@ class ShaderProgram
// exit with error
end
fun ref uniform(name: String) =>
_uniforms.push(name)
fun ref attribute(name: String, number: U32, stride: I32, index: U32) =>
let attr = @glGetAttribLocation(_program, name.cstring())
if attr == -1 then _env.out.print("Attribute " + name + " not active") end
let unit = @float_size[I32]()
@glVertexAttribPointer(attr, number, GLFloat(), GLFalse(), stride * unit, index * unit.abs())
@glEnableVertexAttribArray(attr)
fun uniform(name: String): Uniform box =>
let id = @glGetUniformLocation(_program, name.cstring())
Uniform(id)
fun apply() =>
@glLinkProgram(_program)
......@@ -143,12 +163,6 @@ class ShaderProgram
@glUseProgram(_program)
for name in _uniforms.values() do
let attribute = @glGetAttribLocation(_program, name.cstring())
@glVertexAttribPointer(attribute, 2, GLFloat(), GLFalse(), 0, NULL())
@glEnableVertexAttribArray(attribute)
end
class Shape
var _id: U32 = 0
......@@ -157,7 +171,6 @@ class Shape
new create(vertices: Array[F32]) =>
_vertices = vertices
@glGenBuffers(1, addressof _id)
@printf[None]("id: %d\n".cstring(), _id)
@glBindBuffer(GLArrayBuffer(), _id)
@glBufferData(
GLArrayBuffer(),
......@@ -173,12 +186,16 @@ class Shape
// assume it is a triangle
@glDrawArrays(GLTriangles(), 0, 3)
class Graphics
primitive Graphics
fun clear(r: F32, g: F32, b: F32, a: F32) =>
@glClearColor(r, g, b, a)
@glClear(GLColorBufferBit() or GLDepthBufferBit())
fun time(): F32 =>
@SDL_GetTicks().f32() / 1000.0
class Window
var sdl_window: Pointer[SDLWindow] ref
......@@ -219,6 +236,12 @@ class Window
end
@glewInit()
// MAKE THIS BETTER:
var vao: U32 = 0
@glGenVertexArrays[None](U32(1), addressof vao)
@glBindVertexArray[None](vao)
@glEnable(GLDebugOutput())
@gl_debugging[None]()
......
#version 130
out vec4 outColor;
in vec3 Color;
in float Time;
void main()
{
outColor = vec4(1.0, 1.0, 1.0, 1.0);
outColor = vec4(Color, 1.0);
}
#version 130
in vec2 position;
in vec3 color;
uniform float time;
out vec3 Color;
out float Time;
void main()
{
gl_Position = vec4(position, 0.0, 1.0);
Time = time;
Color = color;
gl_Position = vec4(position.x + cos(time * 2), position.y + sin(time * 2), 1.0, 2);
}
......@@ -3,8 +3,10 @@
primitive Game
fun apply(env: Env, triangle: Shape box) =>
Graphics.clear(0.39, 0.58, 0.93, 1.0)
fun apply(env: Env, triangle: Shape box, time: Uniform box) =>
Graphics.clear(0.1, 0.1, 0.1, 1.0)
let t = Graphics.time()
time.set_float(t)
triangle.draw()
actor Main
......@@ -17,8 +19,11 @@ actor Main
let triangle = Shape([
0.0; 0.5
1.0; 1.0; 0.0
0.5; -0.5
0.0; 1.0; 1.0
-0.5; -0.5
1.0; 0.0; 1.0
])
try
......@@ -28,8 +33,11 @@ actor Main
env.out.print("Could not compile shaders")
end
shader_program.uniform("position")
shader_program()
shader_program.attribute("position", 2, 5, 0)
shader_program.attribute("color", 3, 5, 2)
window.loop({()(env, triangle) => Game(env, triangle)})
let time = shader_program.uniform("time")
window.loop({()(env, triangle, time) => Game(env, triangle, time)})
window.finish()
......@@ -56,3 +56,7 @@ void debug_callback(GLenum source, GLenum type, GLuint id,
void gl_debugging(void) {
glDebugMessageCallback(debug_callback, NULL);
}
int float_size(void) {
return sizeof(float);
}
......@@ -6,3 +6,4 @@ SDL_Event *make_sdl_event(void);
void free_sdl_event(SDL_Event *e);
int is_sdl_quit_event(SDL_Event *e);
void process_errors(void);
void gl_debugging(void);
Markdown is supported
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