Commit dfe0df2e authored by Charles Chamberlain's avatar Charles Chamberlain

Rendering two shapes

parent 3d75e9d3
......@@ -39,10 +39,12 @@ use @glShaderSource[U32](shader: U32, count: USize, string: Pointer[Pointer[U8]
use @glAttachShader[None](program: U32, shader: U32)
use @glLinkProgram[None](program: U32)
use @glUseProgram[None](program: U32)
use @glBindVertexArray[None](id: U32)
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 @glBufferData[None](kind: U32, size: USize, array: Pointer[None] tag, mode: U32)
use @glDrawArrays[None](kind: U32, buffer: U32, number: U32)
use @glDrawElements[None](kind: U32, number: U32, datatype: U32, indices: U32)
use @glGetUniformLocation[U32](program_id: U32, attr: Pointer[U8] tag)
use @glGetAttribLocation[U32](program_id: U32, attr: Pointer[U8] tag)
use @glVertexAttribPointer[None](
......@@ -59,12 +61,14 @@ primitive NULL fun apply(): Pointer[None] => Pointer[None].create()
primitive GLColorBufferBit fun apply(): U32 => 0x4000
primitive GLDepthBufferBit fun apply(): U32 => 0x100
primitive GLArrayBuffer fun apply(): U32 => 0x8892
primitive GLElementBuffer fun apply(): U32 => 0x8893
primitive GLStaticDraw fun apply(): U32 => 0x88E4
primitive GLTriangles fun apply(): U32 => 0x0004
primitive GLFragmentShader fun apply(): U32 => 0x8B30
primitive GLVertexShader fun apply(): U32 => 0x8B31
primitive GLCompileStatus fun apply(): U32 => 0x8B81
primitive GLLinkStatus fun apply(): U32 => 0x8B82
primitive GLUnsignedInt fun apply(): U32 => 0x1405
primitive GLFloat fun apply(): U32 => 0x1406
primitive GLFalse fun apply(): U8 => 0
primitive GLTrue fun apply(): U8 => 1
......
......@@ -61,7 +61,7 @@ class ShaderProgram
// exit with error
end
fun ref attribute(name: String, number: U32, stride: I32, index: U32) =>
fun 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]()
......
build-pony: build-c
ponyc
run: ./stars
./stars
build-c:
./stars: ./stars_extension.o *.pony makefile
ponyc -d
./stars_extension.o: stars_extension.c
clang -fPIC -Wall -Wextra -O3 -g -MM stars_extension.c >stars_extension.d
clang -fPIC -Wall -Wextra -O3 -g -c -o stars_extension.o stars_extension.c
clang -shared -o libstars_extension.so stars_extension.o
......
// shapes.pony
class Shape
class ShapeSystem
var _shapes: Array[Shape]
new create(shapes: Array[Shape], set_attributes: {()}) =>
_shapes = shapes
for shape in _shapes.values() do
@glGenVertexArrays[None](U32(1), addressof shape.vao)
@glBindVertexArray[None](shape.vao)
@glGenBuffers(1, addressof shape.vbo)
@glBindBuffer(GLArrayBuffer(), shape.vbo)
@glBufferData(
GLArrayBuffer(),
// SIZEOF != SIZE
// * 32 is the size of the float...
// make this cleaner please
shape.vertices.size() * 32,
shape.vertices.cpointer(),
GLStaticDraw()
)
var _id: U32 = 0
var _vertices: Array[F32]
new create(vertices: Array[F32]) =>
_vertices = vertices
@glGenBuffers(1, addressof _id)
@glBindBuffer(GLArrayBuffer(), _id)
@glBufferData(
GLArrayBuffer(),
// SIZEOF != SIZE
// * 32 is the size of the float...
// make this cleaner please
vertices.size() * 32,
vertices.cpointer(),
GLStaticDraw()
)
@glGenBuffers(1, addressof shape.ebo)
@glBindBuffer(GLElementBuffer(), shape.ebo)
@glBufferData(
GLElementBuffer(),
shape.elements.size() * 32,
shape.elements.cpointer(),
GLStaticDraw()
)
set_attributes()
end
fun draw() =>
// assume it is a triangle
@glDrawArrays(GLTriangles(), 0, 3)
for shape in _shapes.values() do
@glBindVertexArray[None](shape.vao)
@glBindBuffer(GLArrayBuffer(), shape.vbo)
@glBindBuffer(GLElementBuffer(), shape.ebo)
@glDrawElements(
GLTriangles(),
shape.elements.size().u32(),
GLUnsignedInt(),
0
)
end
class Shape
"""
Basically just a container
for vertices and elements.
The idea is that a `Shape`
is a collection of triangles
that may or may not share
vertices.
"""
var vao: U32 = 0
var vbo: U32 = 0
var ebo: U32 = 0
var vertices: Array[F32]
var elements: Array[U32]
new create(vertices': Array[F32], elements': Array[U32]) =>
vertices = vertices'
elements = elements'
......@@ -4,7 +4,7 @@
actor Game
var _env: Env
var _triangle: Shape
var _shape_system: ShapeSystem
var _time: Uniform
var _window: Window
......@@ -13,26 +13,37 @@ actor Game
_window = Window.create(_env)
_window.init("Stars", 1200, 1000)
_triangle = Shape([
0.0; 0.5
1.0; 1.0; 0.0
0.5; -0.5
0.0; 1.0; 1.0
let quad = Shape([
0; 1
0.3; 0.3; 0.0
1; 1
0.0; 0.8; 0.0
1; 0
0.8; 0.0; 0.4
0; 0
1.0; 0.0; 0.0
], [
0; 1; 2
2; 3; 0
])
let triangle = Shape([
-2.5; -0.5
1.0; 0.5; 0.0
-0.5; -0.5
1.0; 0.0; 1.0
2.0; 1.5
1.0; 1.0; 0.0
1.5; 0.5
0.0; 1.0; 1.0
0.5; 0.5
-0.5; -2.5
1.0; 0.0; 1.0
])
], [0; 1; 2])
let shader_program = ShaderProgram(_env, "shader.vert", "shader.frag")
// use this program
shader_program()
shader_program.attribute("position", 2, 5, 0)
shader_program.attribute("color", 3, 5, 2)
_shape_system = ShapeSystem([quad; triangle], {()(shader_program) =>
shader_program.attribute("position", 2, 5, 0)
shader_program.attribute("color", 3, 5, 2)
})
_time = shader_program.uniform("time")
......@@ -40,7 +51,7 @@ actor Game
Graphics.clear(0.1, 0.1, 0.1, 1.0)
let t = Graphics.time()
_time.set_float(t)
_triangle.draw()
_shape_system.draw()
_window.swap()
_window.process_errors()
......
......@@ -44,11 +44,6 @@ class Window
@glewInit()
// MAKE THIS BETTER:
var vao: U32 = 0
@glGenVertexArrays[None](U32(1), addressof vao)
@glBindVertexArray[None](vao)
@glEnable(GLDebugOutput())
@gl_debugging[None]()
......
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