Skip to content

Commit

Permalink
Merge pull request #66 from JuliaGL/sd/examples
Browse files Browse the repository at this point in the history
Sd/examples
  • Loading branch information
SimonDanisch authored Mar 24, 2017
2 parents 7d1ee30 + 4f66928 commit fb559ab
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 31 deletions.
2 changes: 1 addition & 1 deletion example/compute_shader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ postrender!(texobj, render, texobj.vertexarray)
glClearColor(0,0,0,1)
frame = 0f0
while !GLFW.WindowShouldClose(window.nativewindow)

yield()
render(ro)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
Expand All @@ -110,3 +109,4 @@ while !GLFW.WindowShouldClose(window.nativewindow)
GLFW.PollEvents()
sleep(0.01)
end
GLFW.DestroyWindow(window)
11 changes: 7 additions & 4 deletions example/geometry_shader.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GLAbstraction, GLWindow, GeometryTypes, ColorTypes, GLFW, Reactive, ModernGL

const vert = vert"""
{{GLSL_VERSION}}
in vec2 pos;
Expand Down Expand Up @@ -48,6 +49,7 @@ void main(void)
EndPrimitive();
}
"""

const window = GLWindow.create_glcontext("Geometry Shader")

const b = Point2f0[(-0.5,0),(0.0, 0.0),(0.4, 0.3)]
Expand All @@ -58,14 +60,15 @@ data = Dict{Symbol, Any}(

program = GLAbstraction.LazyShader(vert, geom, frag)
robj = std_renderobject(data, program, Signal(AABB(Vec3f0(0), Vec3f0(1))), GL_POINTS)
println(robj)

glClearColor(0,0,0,1)
glClearColor(0, 0, 0, 1)


while isopen(window)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
render(robj)
swapbuffers(window)
poll_glfw()
swapbuffers(window)
poll_glfw()
end
GLFW.DestroyWindow(window)
18 changes: 8 additions & 10 deletions example/simple_triangle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const vsh = vert"""
in vec2 position;
void main(){
gl_Position = vec4(position, 0, 1.0);
gl_Position = vec4(position, 0, 1.0);
}
"""

Expand All @@ -17,25 +17,23 @@ const fsh = frag"""
out vec4 outColor;
void main() {
outColor = vec4(1.0, 1.0, 1.0, 1.0);
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}
"""

const triangle = std_renderobject(
Dict{Symbol, Any}(
Dict{Symbol, Any}(
:position => GLBuffer(Point2f0[(0.0, 0.5), (0.5, -0.5), (-0.5,-0.5)]),
),
LazyShader(vsh, fsh)
LazyShader(vsh, fsh)
)

glClearColor(0, 0, 0, 1)


while isopen(window)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
render(triangle)

swapbuffers(window)
poll_glfw()
swapbuffers(window)
poll_glfw()
end
GLFW.DestroyWindow(window)
18 changes: 9 additions & 9 deletions example/texture_example.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ModernGL, GLWindow, GLAbstraction, GLFW, ColorTypes, Reactive, GeometryTypes
import GLAbstraction: N0f8

const window = create_glcontext("Example")

Expand All @@ -11,8 +12,8 @@ in vec2 texturecoordinates; // must be this name, because collect_for_gl assumes
out vec2 f_uv;
void main() {
f_uv = texturecoordinates;
gl_Position = vec4(vertices, 0.0, 1.0);
f_uv = texturecoordinates;
gl_Position = vec4(vertices, 0.0, 1.0);
}
"""

Expand All @@ -27,14 +28,14 @@ in vec2 f_uv;
void main() {
outColor = texture(image, f_uv);
outColor = texture(image, f_uv);
}
"""
program = LazyShader(vert, frag) #= {{GLSL_VERSION}} is a template, you could add your own with the kwarg view=Dict{Compat.UTF8String, Compat.UTF8String}(key->replacement) =#

tex = Texture([RGBA{N0f8}(x,y,sin(x*pi), 1.0) for x=0:0.1:1., y=0:0.1:1.]) #automatically creates the correct texture
data = merge(Dict(
:image => tex,
:image => tex,
:primitive => GLUVMesh2D(SimpleRectangle{Float32}(-1,-1,2,2))
)) # Transforms the rectangle into a 2D mesh with uv coordinates and then extracts the buffers for the shader

Expand All @@ -44,10 +45,9 @@ glClearColor(0, 0, 0, 1)


while isopen(window)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
render(robj)

swapbuffers(window)
poll_glfw()
swapbuffers(window)
poll_glfw()
end
GLFW.DestroyWindow(window)
33 changes: 26 additions & 7 deletions src/GLShader.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# Different shader string literals- usage: e.g. frag" my shader code"
macro frag_str(source::AbstractString)
quote
Shader(Symbol(@__FILE__), $(Vector{UInt8}(ascii(source))), GL_FRAGMENT_SHADER)
($source, GL_FRAGMENT_SHADER)
end
end
macro vert_str(source::AbstractString)
quote
Shader(Symbol(@__FILE__), $(Vector{UInt8}(ascii(source))), GL_VERTEX_SHADER)
($source, GL_VERTEX_SHADER)
end
end
macro geom_str(source::AbstractString)
quote
Shader(Symbol(@__FILE__), $(Vector{UInt8}(ascii(source))), GL_GEOMETRY_SHADER)
($source, GL_GEOMETRY_SHADER)
end
end
macro comp_str(source::AbstractString)
quote
Shader(Symbol(@__FILE__), $(Vector{UInt8}(ascii(source))), GL_COMPUTE_SHADER)
($source, GL_COMPUTE_SHADER)
end
end

Expand Down Expand Up @@ -230,14 +230,34 @@ function gl_convert(lazyshader::AbstractLazyShader, data)
fragdatalocation = get(kw_dict, :fragdatalocation, Tuple{Int, String}[])
return compile_program([paths...], fragdatalocation)
end

v = get_view(kw_dict)
fragdatalocation = get(kw_dict, :fragdatalocation, Tuple{Int, String}[])

# Tuple(Source, ShaderType)
if all(paths) do x
isa(x, Tuple) && length(x) == 2 &&
isa(first(x), String) &&
isa(last(x), GLenum)
end
# we don't cache view & templates for shader strings!
shaders = map(paths) do source_typ
source, typ = source_typ
src, _ = template2source(source, v, data)
compile_shader(Vector{UInt8}(src), typ, :from_string)
end
return compile_program([shaders...], fragdatalocation)
end
if !all(x-> isa(x, String), paths)
error("Please supply only paths or tuples of (source, typ) for Lazy Shader
Found: $paths"
)
end
template_keys = Array(Vector{String}, length(paths))
replacements = Array(Vector{String}, length(paths))
for (i, path) in enumerate(paths)
template = get_template!(path, v, data)
template_keys[i] = template
replacements[i] = String[mustache2replacement(k, v, data) for k in template]
replacements[i] = String[mustache2replacement(t, v, data) for t in template]
end
program = get!(_program_cache, (paths, replacements)) do
# when we're here, this means there were uncached shaders, meaning we definitely have
Expand All @@ -247,7 +267,6 @@ function gl_convert(lazyshader::AbstractLazyShader, data)
tr = Dict(zip(template_keys[i], replacements[i]))
shaders[i] = get_shader!(path, tr, v, data)
end
fragdatalocation = get(kw_dict, :fragdatalocation, Tuple{Int, String}[])
compile_program(shaders, fragdatalocation)
end
end
Expand Down

0 comments on commit fb559ab

Please sign in to comment.