JuliaGL / GLVisualize.jl

Visualization library written in Julia and OpenGL
Other
247 stars 34 forks source link

1) Orthographic camera example, 2) Default camera too close, and 3) Custom camera disrupts zooming and rotation #244

Closed mohamed82008 closed 6 years ago

mohamed82008 commented 6 years ago

This is multiple issues related to each other so sorry about that. If you want I can split them. I am trying to make a custom orthographic view to include the whole mesh in the window. The default _view makes the camera too close. When I dug into the examples, I found a way to make a PerspectiveCamera but not an OrthographicCamera. I believe both of these are in GLAbstraction in which I couldn't find usage examples also. Also when I made a custom PerspectiveCamera, it worked but the zoom and rotation stopped working, so is there a way to fix that? I am on Windows and v0.6.1 btw.

SimonDanisch commented 6 years ago

OrthographicCamera is kind of deprecated, since PerspectiveCamera can change the projection to orthographic. just do:


w = glscreen(); @async renderloop(w)
_view(visualize(rand(Float32, 32, 32)))
cam = w.cameras[:perspective]
push!(cam.projectiontype, GLAbstraction.ORTHOGRAPHIC)
GLAbstraction.center!(w) # center to all objects in scene

Note that you might need to set the position/lookat field as well, since center changes those at the moment...

mohamed82008 commented 6 years ago

Great, works with a reshuffle of lines!

isdefined(:window) && destroy!(window)
window = glscreen()
@async renderloop(window)
all_render_objects = RenderObject[]

s = 1
n = 10
rectangles = vec([(HyperRectangle{3,Float32}(Vec3f0((i-1)*s, (j-1)*s, (k-1)*s), Vec3f0(i*s, j*s, k*s)), 
                        RGBA(0.5f0,0.5f0,0.5f0,1f0)) for i in 1:n, j in 1:n, k in 1:n]);
meshes = map(GLNormalMesh, rectangles);
colored_mesh = GeometryTypes.merge(meshes);

robjs = extract_renderable(visualize(colored_mesh))
_view(robjs, window)
append!(all_render_objects, robjs);

cam = window.cameras[:perspective]
push!(cam.projectiontype, GLAbstraction.ORTHOGRAPHIC)
GLAbstraction.center!(window)

The window.cameras[:perspective] is not defined before running _view. Also for some reason, when I was making the camera object and assigning window.cameras[:perspective] = cam explicitly, it was not working as expected. Anyways, I am glad I found a workflow, thank you!

mohamed82008 commented 6 years ago

Is there anything that refreshes the camera? I try changing the camera attributes but it is not updating.

SimonDanisch commented 6 years ago

you might need a yield() to allow the event loop to update the values... the camera gets added to the renderobject when calling _view, so changing window.camera after _view wont affect the objects

mohamed82008 commented 6 years ago

That's what I am trying with no luck.

_eye = Vec3f0(2n,2n,2n); eyeposition = Signal(_eye)
_lookat = Vec3f0(n,n,n); lookatposition = Signal(_lookat)
_up = Vec3f0(_lookat[1]-_eye[1], _lookat[1]-_eye[1], 2*_eye[1]-2*_lookat[1])
upvector = Signal(_up)

cam.eyeposition = eyeposition
cam.lookat = lookatposition
cam.up = upvector
cam.nearclip = Signal(0f0)

yield()
SimonDanisch commented 6 years ago

ah, you need to push the attributes to the signal! so do push!(cam.nearclip, 0.0)

mohamed82008 commented 6 years ago

Great, works now, even without yield! Thanks a lot!