Skip to content

Commit

Permalink
replace GLFW with MiniFB
Browse files Browse the repository at this point in the history
  • Loading branch information
jebej committed Sep 10, 2020
1 parent cbd4030 commit 15ba002
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 79 deletions.
3 changes: 1 addition & 2 deletions Project.toml
Expand Up @@ -6,7 +6,6 @@ version = "0.1.0"
[deps]
ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
GLFW = "f7f18e0c-5ee9-5ccd-a5bf-e8befd85ed98"
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
ModernGL = "66fc600b-dfda-50eb-8b99-91cfa97b1301"
MiniFB = "cc649173-1898-4bb8-8a16-ca5f88bd87d9"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
55 changes: 13 additions & 42 deletions src/VoxelSpace.jl
@@ -1,5 +1,6 @@
# strongly inspired by https://github.com/s-macke/VoxelSpace
module VoxelSpace
using GLFW, ModernGL, Printf, ColorTypes, FileIO
using MiniFB, Printf, ColorTypes, FileIO
using Base: unsafe_trunc

include("map_utils.jl")
Expand All @@ -15,33 +16,17 @@ const MAP_HEIGHT = 1024
function run(map="C1W")
# Load map and create buffer
datac, datah = read_map(map)
data = similar(datac, WIN_WIDTH, WIN_HEIGHT)
data = Matrix{RGB24}(undef, WIN_WIDTH, WIN_HEIGHT)
hbuffer = Vector{Int}(undef, WIN_WIDTH)

# Create window
GLFW.WindowHint(GLFW.DOUBLEBUFFER, false)
window = GLFW.CreateWindow(WIN_WIDTH, WIN_HEIGHT, "VoxelSpace.jl")
GLFW.MakeContextCurrent(window)

# Generate texture
tex = glGenTextures()
glBindTexture(GL_TEXTURE_2D,tex)
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,WIN_WIDTH,WIN_HEIGHT,0,GL_RGB,GL_UNSIGNED_BYTE,data)

# Generate and bind the read framebuffer
readFboId = Ref{Cuint}(0)
glGenFramebuffers(1,readFboId)
glBindFramebuffer(GL_READ_FRAMEBUFFER,readFboId[])

# Bind texture to the read framebuffer
glFramebufferTexture2D(GL_READ_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,tex,0)

# Bind the default framebuffer (0) to draw
glBindFramebuffer(GL_DRAW_FRAMEBUFFER,0)
window = mfb_open_ex("VoxelSpace.jl", WIN_WIDTH, WIN_HEIGHT, MiniFB.WF_RESIZABLE);

# Set up input callbacks
GLFW.SetKeyCallback(window,key_cb_fun)
GLFW.SetCursorPosCallback(window,mouse_cb_fun)
key_cb_fun_c = @cfunction(key_cb_fun, Cvoid, (Ptr{Cvoid}, mfb_key, mfb_key_mod, Bool))
mouse_cb_fun_c = @cfunction(mouse_cb_fun, Cvoid, (Ptr{Cvoid}, Int32, Int32))
mfb_set_keyboard_callback(window, key_cb_fun_c)
mfb_set_mouse_move_callback(window, mouse_cb_fun_c)

# Set up FPS counter
t1 = time_ns()
Expand All @@ -54,10 +39,7 @@ function run(map="C1W")
θ = 0f0

try
while !GLFW.WindowShouldClose(window)
# Check for inputs
GLFW.PollEvents()

while mfb_wait_sync(window)
# Compute movement direction and apply to pos
dx,dy,dz,dθ = compute_movement!(θ)
px += dx
Expand All @@ -66,32 +48,21 @@ function run(map="C1W")
θ +=

# Update texture
render!(data,hbuffer,datac,datah,(px,py,pz),θ[],WIN_HEIGHT÷2,WIN_HEIGHT÷2)
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,WIN_WIDTH,WIN_HEIGHT,GL_RGB,GL_UNSIGNED_BYTE,data)
render!(data,hbuffer,datac,datah,(px,py,pz),θ,WIN_HEIGHT>>1,WIN_HEIGHT>>1)

# Blit read framebuffer (texture) to draw framebuffer (display)
glBlitFramebuffer(0,0,WIN_WIDTH,WIN_HEIGHT,0,0,WIN_WIDTH,WIN_HEIGHT,GL_COLOR_BUFFER_BIT,GL_LINEAR)
glFlush()
#GLFW.SwapBuffers(window)
state = mfb_update(window, data)
state == MiniFB.STATE_OK || break

# Compute FPS
t2 = time_ns()
@printf("\b\b\b%3.0f", 1E9/(t2-t1))
t1 = t2
end
finally
GLFW.DestroyWindow(window)
mfb_close(window)
println()
end
return nothing
end


function ModernGL.glGenTextures()
id = Ref{GLuint}(0)
glGenTextures(1, id)
id[] <= 0 && @error "glGenTextures returned an invalid id. Is the OpenGL context active?"
return id[]
end

end # module
51 changes: 23 additions & 28 deletions src/input.jl
Expand Up @@ -7,45 +7,40 @@ const mov_up = Ref(false)
const mov_down = Ref(false)
const mov_boost = Ref(false)

function key_cb_fun(_, key, scancode, action, mods)
if key == GLFW.KEY_W # up
action == GLFW.PRESS && (mov_forward[] = true)
action == GLFW.RELEASE && (mov_forward[] = false)
elseif key == GLFW.KEY_A # left
action == GLFW.PRESS && (mov_left[] = true)
action == GLFW.RELEASE && (mov_left[] = false)
elseif key == GLFW.KEY_D # right
action == GLFW.PRESS && (mov_right[] = true)
action == GLFW.RELEASE && (mov_right[] = false)
elseif key == GLFW.KEY_S # down
action == GLFW.PRESS && (mov_back[] = true)
action == GLFW.RELEASE && (mov_back[] = false)
elseif scancode == 57 # space
action == GLFW.PRESS && (mov_up[] = true)
action == GLFW.RELEASE && (mov_up[] = false)
elseif scancode == 29 # l-ctrl
action == GLFW.PRESS && (mov_down[] = true)
action == GLFW.RELEASE && (mov_down[] = false)
elseif scancode == 42 # l-shift
action == GLFW.PRESS && (mov_boost[] = true)
action == GLFW.RELEASE && (mov_boost[] = false)
function key_cb_fun(_, key, _, action)
if key == MiniFB.KB_KEY_W # up
mov_forward[] = action
elseif key == MiniFB.KB_KEY_A # left
mov_left[] = action
elseif key == MiniFB.KB_KEY_D # right
mov_right[] = action
elseif key == MiniFB.KB_KEY_S # down
mov_back[] = action
elseif key == MiniFB.KB_KEY_SPACE # space
mov_up[] = action
elseif key == MiniFB.KB_KEY_LEFT_CONTROL # l-ctrl
mov_down[] = action
elseif key == MiniFB.KB_KEY_LEFT_SHIFT # l-shift
mov_boost[] = action
end
return nothing
end

const mouse_pos = Float32[0,0]
const mouse_move = Float32[0,0]

const mouse_pos = Int32[0,0]
const mouse_mov = Int32[0,0]

function mouse_cb_fun(_, x, y)
mouse_move .= (x,y) .- mouse_pos
mouse_pos .= (x,y)
mouse_mov .= (x,y) .- mouse_pos
mouse_pos .= (x,y)
return nothing
end


function compute_movement!(θ)
# mouse
= -mouse_move[1]*1f-2
mouse_move .= 0f0
= -mouse_mov[1]*1f-2
fill!(mouse_mov, zero(Int32))
# keyboard; signs empirically adjusted...
sθ, cθ = sincos(θ)
dx = (mov_right[]-mov_left[])*- (mov_forward[]-mov_back[])*
Expand Down
2 changes: 1 addition & 1 deletion src/map_utils.jl
@@ -1,6 +1,6 @@
function read_map(map::String)
map_dir = joinpath(dirname(@__DIR__), "maps")
datac = load(joinpath(map_dir,map)*".png")
datac = RGB24.(load(joinpath(map_dir,map)*".png"))
datah = Float32.(reinterpret(UInt8,load(joinpath(map_dir,MAP_LIST[map])*".png")))
return datac, datah
end
Expand Down
12 changes: 6 additions & 6 deletions src/render.jl
@@ -1,7 +1,7 @@
function render!(data, hbuffer, colormap, heightmap, pos::Tuple{T,T,T}, θ::T, horizon, zscale) where T<:Number
function render!(data, hbuffer, colormap, heightmap, pos::Tuple{T,T,T}, θ::T, horizon, zscale) where T<:Real
# draw background (light blue sky)
fill!(data, RGB(0.529f0, 0.808f0, 0.980f0))
fill!(hbuffer, 1)
fill!(data, RGB24(0.529f0, 0.808f0, 0.980f0))
fill!(hbuffer, WIN_HEIGHT)
# precompute some constants
w = Base.multiplicativeinverse(MAP_WIDTH%UInt)
h = Base.multiplicativeinverse(MAP_HEIGHT%UInt)
Expand All @@ -21,14 +21,14 @@ function render!(data, hbuffer, colormap, heightmap, pos::Tuple{T,T,T}, θ::T, h
pxi = 1 + rem(unsafe_trunc(Int, px)%UInt, w)
pyi = 1 + rem(unsafe_trunc(Int, py)%UInt, h)
# compute the on-screen height of the feature
height = min(WIN_HEIGHT, unsafe_trunc(Int, (heightmap[pxi,pyi]-pos[3])*scale) + horizon)
height = unsafe_trunc(Int, (pos[3]-heightmap[pxi,pyi])*scale) + horizon
# assign color to column
color = colormap[pxi,pyi]
for i in hbuffer[j] : height
for i in max(height,1) : hbuffer[j]
data[j,i] = color
end
# keep track of feature height
if height > hbuffer[j]
if height < hbuffer[j]
hbuffer[j] = height
end
# move to next view-line feature position
Expand Down

0 comments on commit 15ba002

Please sign in to comment.