Skip to content

Instantly share code, notes, and snippets.

@SimonDanisch
Created August 5, 2014 15:47
Show Gist options
  • Save SimonDanisch/6095edee143b4441be4f to your computer and use it in GitHub Desktop.
Save SimonDanisch/6095edee143b4441be4f to your computer and use it in GitHub Desktop.
using ModernGL, GLPlot, GLAbstraction, GLWindow, GLFW, ImmutableArrays, Images, React, Quaternions
window = createwindow("test", 1000, 800)
mouseposition = window.inputs[:mouseposition]
clicked = window.inputs[:mousebuttonspressed]
keypressed = window.inputs[:buttonspressed]
clickedwithoutkeyL = lift((mb, kb) -> in(0, mb) && isempty(kb), Bool, clicked, keypressed)
clickedwithoutkeyM = lift((mb, kb) -> in(2, mb) && isempty(kb), Bool, clicked, keypressed)
nokeydown = lift((kb) -> isempty(kb), Bool, keypressed)
anymousedown = lift((mb) -> !isempty(mb), Bool, clicked)
function mousediff(v0::(Bool, Vector2{Float64}, Vector2{Float64}), clicked::Bool, pos::Vector2{Float64})
clicked0, pos0, pos0diff = v0
if clicked0 && clicked
return (clicked, pos, pos0 - pos)
end
return (clicked, pos, Vector2(0.0))
end
mousedraggdiffL = lift(x->x[3], Vector2{Float64}, foldl(mousediff, (false, Vector2(0.0), Vector2(0.0)), clickedwithoutkeyL, mouseposition))
mousedraggdiffM = lift(x->x[3], Vector2{Float64}, foldl(mousediff, (false, Vector2(0.0), Vector2(0.0)), clickedwithoutkeyM, mouseposition))
#mousedraggdiffL = keepwhen(clickedwithoutkeyL, Vector2(0.0), mousedraggdiffL)
#mousedraggdiffM = keepwhen(clickedwithoutkeyM, Vector2(0.0), mousedraggdiffM)
speed = 50f0
xtheta = Input(0f0)
ytheta = lift(x-> float32(-x[2]) / speed, Float32, mousedraggdiffL)
ztheta = lift(x-> float32(x[1]) / speed, Float32, mousedraggdiffL)
xtrans = lift(x-> float32(x*0.1f0), Float32, window.inputs[:scroll_y])
ytrans = lift(x-> -float32(x[1]) / speed, Float32, mousedraggdiffM)
ztrans = lift(x-> float32(x[2]) / speed, Float32, mousedraggdiffM)
fov = Input(41f0)
cam = Cam(
window.inputs[:window_size],# = iVec2(50,50),
Input(Vec3(1,0,0)),
Input(Vec3(0)),
xtheta,
ytheta,
ztheta,
xtrans,
ytrans,
ztrans,
Input(41f0),
Input(1f0),
Input(100f0)
)
initplotting()
shader = TemplateProgram(shaderdir*"standard.vert", shaderdir*"phongblinn.frag")
xaxis = gencubenormals(Vec3(0), Vec3(0.05, 0, 0), Vec3(0, 0.01, 0), Vec3(0,0,0.01))
yaxis = gencubenormals(Vec3(0), Vec3(0.01, 0, 0), Vec3(0, 0.05, 0), Vec3(0,0,0.01))
zaxis = gencubenormals(Vec3(0), Vec3(0.01, 0, 0), Vec3(0, 0.01, 0), Vec3(0,0,0.05))
xcube = RenderObject([
:vertex => GLBuffer(xaxis[1]),
:index => indexbuffer(xaxis[4]),
:normal => GLBuffer(normals),
:projection => cam.projection,
:view => cam.view,
:normalmatrix => eye(Mat3),
:model => lift(x-> rotationmatrix4(x.rotation), cam.pivot),
:light_position => Vec3(20, 20, -20)
], shader)
ycube = RenderObject([
:vertex => GLBuffer(yaxis[1]),
:index => indexbuffer(yaxis[4]),
:normal => GLBuffer(normals),
:projection => cam.projection,
:view => cam.view,
:normalmatrix => eye(Mat3),
:model => lift(x-> rotationmatrix4(x.rotation), cam.pivot),
:light_position => Vec3(20, 20, -20)
], shader)
zcube = RenderObject([
:vertex => GLBuffer(zaxis[1]),
:index => indexbuffer(zaxis[4]),
:normal => GLBuffer(normals),
:projection => cam.projection,
:view => cam.view,
:normalmatrix => eye(Mat3),
:model => lift(x-> rotationmatrix4(x.rotation), cam.pivot),
:light_position => Vec3(20, 20, -20)
], shader)
prerender!(xcube, glEnable, GL_DEPTH_TEST)
postrender!(xcube, render, xcube.vertexarray)
prerender!(ycube, glEnable, GL_DEPTH_TEST)
postrender!(ycube, render, ycube.vertexarray)
prerender!(zcube, glEnable, GL_DEPTH_TEST)
postrender!(zcube, render, zcube.vertexarray)
glClearColor(1,1,1,1)
while !GLFW.WindowShouldClose(window.glfwWindow)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
render(xcube)
render(ycube)
render(zcube)
render(GRID)
yield() # this is needed for react to work
GLFW.SwapBuffers(window.glfwWindow)
GLFW.PollEvents()
end
GLFW.Terminate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment