Skip to content

Commit

Permalink
updates to make tutorials work again
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDanisch committed Jan 2, 2017
1 parent 3cc30a7 commit f28f7c8
Show file tree
Hide file tree
Showing 22 changed files with 75 additions and 24 deletions.
12 changes: 9 additions & 3 deletions src/GLRenderObject.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
RenderObject(data::Dict{Symbol}, program, bbs=Signal(AABB{Float32}(Vec3f0(0),Vec3f0(1))), main=nothing) = RenderObject(convert(Dict{Symbol,Any}, data), program, bbs, main)
function RenderObject(
data::Dict{Symbol}, program, pre,
bbs = Signal(AABB{Float32}(Vec3f0(0),Vec3f0(1))),
main = nothing
)
RenderObject(convert(Dict{Symbol,Any}, data), program, pre, bbs, main)
end

function Base.show(io::IO, obj::RenderObject)
println(io, "RenderObject with ID: ", obj.id)
Expand Down Expand Up @@ -93,14 +99,14 @@ export prerendertype

function instanced_renderobject(data, program, bb = Signal(AABB(Vec3f0(0), Vec3f0(1))), primitive::GLenum=GL_TRIANGLES, main=nothing)
pre = StandardPrerender()
robj = RenderObject(data, program, pre, nothing, bb, main)
robj = RenderObject(convert(Dict{Symbol,Any}, data), program, pre, nothing, bb, main)
robj.postrenderfunction = StandardPostrenderInstanced(main, robj.vertexarray, primitive)
robj
end

function std_renderobject(data, program, bb = Signal(AABB(Vec3f0(0), Vec3f0(1))), primitive=GL_TRIANGLES, main=nothing)
pre = StandardPrerender()
robj = RenderObject(data, program, pre, nothing, bb, main)
robj = RenderObject(convert(Dict{Symbol,Any}, data), program, pre, nothing, bb, main)
robj.postrenderfunction = StandardPostrender(robj.vertexarray, primitive)
robj
end
Expand Down
24 changes: 19 additions & 5 deletions src/GLShader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ end

#Implement File IO interface
function load(f::File{format"GLSLShader"})
Shader(f)
fname = filename(f)
source = open(readall, fname)
compile_shader(fname, source)
end
function save(f::File{format"GLSLShader"}, data::Shader)
s = open(f, "w")
Expand Down Expand Up @@ -134,17 +136,24 @@ function __init__()
Base.rehash!(_shader_cache)
Base.rehash!(_program_cache)
end
function compile_shader(path, source_str::AbstractString)
typ = GLAbstraction.shadertype(query(path))
source = Vector{UInt8}(source_str)

# TODO remove this silly constructor
function compile_shader(source::Vector{UInt8}, typ, name)
shaderid = GLAbstraction.createshader(typ)
glShaderSource(shaderid, source)
glCompileShader(shaderid)
if !GLAbstraction.iscompiled(shaderid)
GLAbstraction.print_with_lines(source_str)
warn("shader $(path) didn't compile. \n$(GLAbstraction.getinfolog(shaderid))")
end
Shader(Symbol(path), source, typ, shaderid)
Shader(name, source, typ, shaderid)
end

function compile_shader(path, source_str::AbstractString)
typ = GLAbstraction.shadertype(query(path))
source = Vector{UInt8}(source_str)
name = Symbol(path)
compile_shader(source, typ, name)
end

function get_shader!(path, template_replacement, view, attributes)
Expand Down Expand Up @@ -217,6 +226,11 @@ end
function gl_convert(lazyshader::AbstractLazyShader, data)
kw_dict = lazyshader.kw_args
paths = lazyshader.paths
if all(x-> isa(x, Shader), paths)
fragdatalocation = get(kw_dict, :fragdatalocation, Tuple{Int, String}[])
return compile_program([paths...], fragdatalocation)
end

v = get_view(kw_dict)
template_keys = Array(Vector{String}, length(paths))
replacements = Array(Vector{String}, length(paths))
Expand Down
4 changes: 3 additions & 1 deletion src/GLTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ immutable Shader
new(name, source, typ, id, current_context())
end
end
Shader(name, source, typ) = Shader(name, source, typ, 0)
function Shader(name, source::Vector{UInt8}, typ)
compile_shader(source, typ, name)
end
name(s::Shader) = s.name

import Base: ==
Expand Down
2 changes: 1 addition & 1 deletion tutorials/depth_stencils1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ ro = std_renderobject(bufferdict,
glClearColor(0,0,0,1)
while !GLFW.WindowShouldClose(window)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
render(ro)
GLAbstraction.render(ro)
GLFW.SwapBuffers(window)
GLFW.PollEvents()
if GLFW.GetKey(window, GLFW.KEY_ESCAPE) == GLFW.PRESS
Expand Down
2 changes: 2 additions & 0 deletions tutorials/drawing_polygons1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ glVertexAttribPointer(pos_attribute, length(eltype(vertices)),
glEnableVertexAttribArray(pos_attribute)

# Draw while waiting for a close event
glClearColor(0,0,0,0)
while !GLFW.WindowShouldClose(window)
glClear(GL_COLOR_BUFFER_BIT)
glDrawArrays(GL_TRIANGLES, 0, length(vertices))
GLFW.SwapBuffers(window)
GLFW.PollEvents()
Expand Down
2 changes: 2 additions & 0 deletions tutorials/drawing_polygons2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ glEnableVertexAttribArray(pos_attribute)
uni_color = glGetUniformLocation(shader_program, "triangleColor")

# Draw while waiting for a close event
glClearColor(0,0,0,0)
while !GLFW.WindowShouldClose(window)
glClear(GL_COLOR_BUFFER_BIT)
r = (sin(4*time()) + 1)/2
glUniform3f(uni_color, Float32(r), 0.0f0, 0.0f0)
glDrawArrays(GL_TRIANGLES, 0, length(vertices))
Expand Down
2 changes: 2 additions & 0 deletions tutorials/drawing_polygons3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ glVertexAttribPointer(col_attribute, 3,
GL_FLOAT, GL_FALSE, 5*sizeof(Float32), Ptr{Void}(2*sizeof(Float32)))

# Draw while waiting for a close event
glClearColor(0,0,0,0)
while !GLFW.WindowShouldClose(window)
glClear(GL_COLOR_BUFFER_BIT)
glDrawArrays(GL_TRIANGLES, 0, length(vertices))
GLFW.SwapBuffers(window)
GLFW.PollEvents()
Expand Down
2 changes: 2 additions & 0 deletions tutorials/drawing_polygons4.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ glVertexAttribPointer(col_attribute, 3,
GL_FLOAT, GL_FALSE, 5*sizeof(Float32), Ptr{Void}(2*sizeof(Float32)))

# Draw while waiting for a close event
glClearColor(0,0,0,0)
while !GLFW.WindowShouldClose(window)
glClear(GL_COLOR_BUFFER_BIT)
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, C_NULL)
GLFW.SwapBuffers(window)
GLFW.PollEvents()
Expand Down
4 changes: 3 additions & 1 deletion tutorials/drawing_polygons5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ ro = std_renderobject(bufferdict,
LazyShader(vertex_shader, fragment_shader))

# Draw until we receive a close event
glClearColor(0,0,0,0)
while !GLFW.WindowShouldClose(window)
render(ro)
glClear(GL_COLOR_BUFFER_BIT)
GLAbstraction.render(ro)
GLFW.SwapBuffers(window)
GLFW.PollEvents()
if GLFW.GetKey(window, GLFW.KEY_ESCAPE) == GLFW.PRESS
Expand Down
4 changes: 3 additions & 1 deletion tutorials/drawing_polygons_exercise1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ ro = std_renderobject(bufferdict,
LazyShader(vertex_shader, fragment_shader))

# Draw until we receive a close event
glClearColor(0,0,0,0)
while !GLFW.WindowShouldClose(window)
render(ro)
glClear(GL_COLOR_BUFFER_BIT)
GLAbstraction.render(ro)
GLFW.SwapBuffers(window)
GLFW.PollEvents()
if GLFW.GetKey(window, GLFW.KEY_ESCAPE) == GLFW.PRESS
Expand Down
4 changes: 3 additions & 1 deletion tutorials/drawing_polygons_exercise2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ ro = std_renderobject(bufferdict,
LazyShader(vertex_shader, fragment_shader))

# Draw until we receive a close event
glClearColor(0,0,0,0)
while !GLFW.WindowShouldClose(window)
render(ro)
glClear(GL_COLOR_BUFFER_BIT)
GLAbstraction.render(ro)
GLFW.SwapBuffers(window)
GLFW.PollEvents()
if GLFW.GetKey(window, GLFW.KEY_ESCAPE) == GLFW.PRESS
Expand Down
4 changes: 3 additions & 1 deletion tutorials/drawing_polygons_exercise3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ ro = std_renderobject(bufferdict,
LazyShader(vertex_shader, fragment_shader))

# Draw until we receive a close event
glClearColor(0,0,0,0)
while !GLFW.WindowShouldClose(window)
render(ro)
glClear(GL_COLOR_BUFFER_BIT)
GLAbstraction.render(ro)
GLFW.SwapBuffers(window)
GLFW.PollEvents()
if GLFW.GetKey(window, GLFW.KEY_ESCAPE) == GLFW.PRESS
Expand Down
14 changes: 14 additions & 0 deletions tutorials/run_all.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
include("downloads.jl")
using GLFW
tutorials = readdir(dirname(@__FILE__))
filter!(tutorials) do file
endswith(file, ".jl") &&
basename(file) != "downloads.jl" &&
basename(file) != "run_all.jl"
end
for tutorial in tutorials
GLFW.Init()
println("Now showing: ", tutorial)
include(tutorial)
GLFW.Terminate() # clean up gl context
end
2 changes: 1 addition & 1 deletion tutorials/textures1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ ro = std_renderobject(bufferdict,
# Draw until we receive a close event
while !GLFW.WindowShouldClose(window)
glClear(GL_COLOR_BUFFER_BIT)
render(ro)
GLAbstraction.render(ro)
GLFW.SwapBuffers(window)
GLFW.PollEvents()
if GLFW.GetKey(window, GLFW.KEY_ESCAPE) == GLFW.PRESS
Expand Down
2 changes: 1 addition & 1 deletion tutorials/textures2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ ro = std_renderobject(bufferdict,
# Draw until we receive a close event
while !GLFW.WindowShouldClose(window)
glClear(GL_COLOR_BUFFER_BIT)
render(ro)
GLAbstraction.render(ro)
GLFW.SwapBuffers(window)
GLFW.PollEvents()
if GLFW.GetKey(window, GLFW.KEY_ESCAPE) == GLFW.PRESS
Expand Down
2 changes: 1 addition & 1 deletion tutorials/textures_exercise1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ glClearColor(0.0, 0.0, 0.0, 1.0)
while !GLFW.WindowShouldClose(window)
glClear(GL_COLOR_BUFFER_BIT)
ro[:blend_f] = Float32(mod(time(), period)/period)
render(ro)
GLAbstraction.render(ro)
GLFW.SwapBuffers(window)
GLFW.PollEvents()
if GLFW.GetKey(window, GLFW.KEY_ESCAPE) == GLFW.PRESS
Expand Down
2 changes: 1 addition & 1 deletion tutorials/textures_exercise2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ ro = std_renderobject(bufferdict,
# Draw until we receive a close event
while !GLFW.WindowShouldClose(window)
glClear(GL_COLOR_BUFFER_BIT)
render(ro)
GLAbstraction.render(ro)
GLFW.SwapBuffers(window)
GLFW.PollEvents()
if GLFW.GetKey(window, GLFW.KEY_ESCAPE) == GLFW.PRESS
Expand Down
2 changes: 1 addition & 1 deletion tutorials/textures_exercise3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ while !GLFW.WindowShouldClose(window)
glUniform1f(phase_loc, Float32(phase))
glUniform1f(yperiod_loc, Float32(0.02))

render(ro)
GLAbstraction.render(ro)
GLFW.SwapBuffers(window)
GLFW.PollEvents()
if GLFW.GetKey(window, GLFW.KEY_ESCAPE) == GLFW.PRESS
Expand Down
3 changes: 1 addition & 2 deletions tutorials/transformations1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,13 @@ bufferdict = Dict(:position=>GLBuffer(vertex_positions),

ro = std_renderobject(bufferdict,
LazyShader(vertex_shader, fragment_shader))

# Do the rendering
glClearColor(0,0,0,1)
while !GLFW.WindowShouldClose(window)
push!(trans, rotationmatrix_z(time()*deg2rad(180)))
Reactive.run_till_now()
glClear(GL_COLOR_BUFFER_BIT)
render(ro)
GLAbstraction.render(ro)
GLFW.SwapBuffers(window)
GLFW.PollEvents()
if GLFW.GetKey(window, GLFW.KEY_ESCAPE) == GLFW.PRESS
Expand Down
2 changes: 1 addition & 1 deletion tutorials/transformations2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ while !GLFW.WindowShouldClose(window)
push!(model, rotationmatrix_z(time()*deg2rad(180)))
Reactive.run_till_now()
glClear(GL_COLOR_BUFFER_BIT)
render(ro)
GLAbstraction.render(ro)
GLFW.SwapBuffers(window)
GLFW.PollEvents()
if GLFW.GetKey(window, GLFW.KEY_ESCAPE) == GLFW.PRESS
Expand Down
2 changes: 1 addition & 1 deletion tutorials/transformations_exercise1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ while !GLFW.WindowShouldClose(window)
push!(model, scalematrix((0.75+0.25*sin(5*time()))*Vec3((1,1,1))) * rotationmatrix_z(time()*deg2rad(180)))
Reactive.run_till_now()
glClear(GL_COLOR_BUFFER_BIT)
render(ro)
GLAbstraction.render(ro)
GLFW.SwapBuffers(window)
GLFW.PollEvents()
if GLFW.GetKey(window, GLFW.KEY_ESCAPE) == GLFW.PRESS
Expand Down
2 changes: 1 addition & 1 deletion tutorials/transformations_exercise2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ while !GLFW.WindowShouldClose(window)
flipinc *= (1-flipdecay)
flipangle += flipinc
glClear(GL_COLOR_BUFFER_BIT)
render(ro)
GLAbstraction.render(ro)
GLFW.SwapBuffers(window)
GLFW.PollEvents()
if GLFW.GetKey(window, GLFW.KEY_ESCAPE) == GLFW.PRESS
Expand Down

0 comments on commit f28f7c8

Please sign in to comment.