summaryrefslogtreecommitdiff
path: root/html/examples/viewGL.wlua
blob: a5b2ebb5439eb315513a44d0c289fbe1bbad3361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require("iuplua")
require("iupluagl")
require("luagl")
require("imlua")

iup.key_open()

texture = 0

cnv = iup.glcanvas{buffer="DOUBLE", rastersize = "640x480"}

function cnv:resize_cb(width, height)
  iup.GLMakeCurrent(self)
  gl.Viewport(0, 0, width, height)
end

function cnv:action(x, y)
  iup.GLMakeCurrent(self)
  gl.PixelStore(gl.UNPACK_ALIGNMENT, 1)
  gl.Clear('COLOR_BUFFER_BIT,DEPTH_BUFFER_BIT') -- Clear Screen And Depth Buffer
  
  gl.DrawPixelsRaw (image:Width(), image:Height(), glformat, gl.UNSIGNED_BYTE, gldata)
  
  iup.GLSwapBuffers(self)
end              

function cnv:k_any(c)
  if c == iup.K_q or c == iup.K_ESC then
    return iup.CLOSE
  end
  
  if c == iup.K_F1 then
    if fullscreen then
      fullscreen = false
      dlg.fullscreen = "No"
    else
      fullscreen = true
      dlg.fullscreen = "Yes"
    end
  end
  
  if c == iup.K_F2 then
    fileName = iup.GetFile("*.*")
    new_image = im.FileImageLoadBitmap(fileName)
    if (not new_image) then
      iup.Message("Error", "LoadBitmap failed.")
    else
      gldata, glformat = new_image:GetOpenGLData()
      if (image) then image:Destroy() end
      image = new_image
      iup.Update(cnv)
    end
  end
  
end

if arg and arg[1] ~= nil then
  fileName = arg[1]
else
  fileName = iup.GetFile("*.*")
end

image = im.FileImageLoadBitmap(fileName)
if (not image) then
  error("LoadBitmap failed.")
end
gldata, glformat = image:GetOpenGLData()

dlg = iup.dialog{cnv; title="LuaGL/IUP/IM Loader"}

dlg:show()
cnv.rastersize = nil -- reset minimum limitation

if (iup.MainLoopLevel()==0) then
  iup.MainLoop()
end