This repository was archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.lua
More file actions
137 lines (112 loc) · 3.27 KB
/
test.lua
File metadata and controls
137 lines (112 loc) · 3.27 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
local lujgl = require "lujgl"
local ffi = require "ffi"
local bit = require "bit"
local CubeVerticies = {}
CubeVerticies.v = ffi.new("const float[8][3]", {
{0,0,1}, {0,0,0}, {0,1,0}, {0,1,1},
{1,0,1}, {1,0,0}, {1,1,0}, {1,1,1}
})
CubeVerticies.n = ffi.new("const float[6][3]", {
{-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
{0.0, -1.0, 0.0}, {0.0, 0.0, -1.0}, {0.0, 0.0, 1.0}
})
CubeVerticies.f = ffi.new("const float[6][4]", {
{0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
{4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
})
print("Initializing window")
lujgl.initialize("Test App",nil,nil,{[lujgl.glfwconst.GLFW_RESIZABLE]=true,[lujgl.glfwconst.GLFW_SAMPLES]=8})
local gl = lujgl.gl
local glconst = lujgl.glconst
local glu = lujgl.glu
local imgtx = lujgl.loadTexture("test.png", nil, false, false)
gl.glEnable(glconst.GL_DEPTH_TEST);
gl.glMatrixMode(glconst.GL_PROJECTION)
glu.gluPerspective(60,lujgl.width / lujgl.height,0.01, 1000)
gl.glMatrixMode(glconst.GL_MODELVIEW)
glu.gluLookAt(0,0,5,
0,0,0,
0,1,0)
gl.glEnable(glconst.GL_DEPTH_TEST)
gl.glEnable(glconst.GL_COLOR_MATERIAL)
gl.glEnable(glconst.GL_LIGHTING)
gl.glEnable(glconst.GL_LIGHT0)
lujgl.glLight(glconst.GL_LIGHT0, glconst.GL_AMBIENT, 0.2, 0.2, 0.2)
lujgl.setIdleCallback(function() end)
local rotx, roty, rotz = 1/math.sqrt(2), 1/math.sqrt(2), 0
local boxx, boxy, boxz = -0.5,-0.5,2
print("Rotation Axis:", rotx, roty, rotz)
print("Box Position:", boxx, boxy, boxz)
-- Try loading an extension
do
if lujgl.glfw.glfwExtensionSupported("GL_ARB_shader_objects") ~= 0 then
print("Shader objects supported. Trying to fetch address for glLinkProgramARB.")
local glLinkProgramARB = lujgl.glext.glLinkProgramARB
if glLinkProgramARB ~= nil then
print("Address:", tostring(glLinkProgramARB))
else
print("Couldn't find or address is null.")
end
else
print("Shader objects not supported.")
end
end
function render()
gl.glClear(bit.bor(glconst.GL_COLOR_BUFFER_BIT, glconst.GL_DEPTH_BUFFER_BIT));
-- 3D stuff
gl.glEnable(glconst.GL_TEXTURE_2D)
gl.glBindTexture(glconst.GL_TEXTURE_2D,imgtx)
gl.glPushMatrix()
gl.glColor3d(1,1,1)
gl.glScaled(3,3,1)
gl.glBegin(glconst.GL_QUADS)
gl.glNormal3d(0,0,-1)
gl.glTexCoord2d(0,0)
gl.glVertex3d(-1,-1,0)
gl.glTexCoord2d(1,0)
gl.glVertex3d(1,-1,0)
gl.glTexCoord2d(1,1)
gl.glVertex3d(1,1,0)
gl.glTexCoord2d(0,1)
gl.glVertex3d(-1,1,0)
gl.glEnd()
gl.glPopMatrix()
gl.glDisable(glconst.GL_TEXTURE_2D)
gl.glPushMatrix()
gl.glTranslated(boxx, boxy, boxz)
gl.glRotated(lujgl.getTime()*10, rotx, roty, rotz)
for i=0,5 do
gl.glBegin(glconst.GL_QUADS)
gl.glNormal3fv(CubeVerticies.n[i])
for j=0,3 do
gl.glVertex3fv(CubeVerticies.v[CubeVerticies.f[i][j]])
end
gl.glEnd()
end
gl.glPopMatrix()
-- 2D stuff
gl.glDisable(glconst.GL_TEXTURE_2D)
lujgl.begin2D()
gl.glBegin(glconst.GL_QUADS)
gl.glVertex2i(0, 0)
gl.glVertex2i(50, 0)
gl.glVertex2i(50, 50)
gl.glVertex2i(0, 50)
gl.glEnd()
lujgl.end2D()
lujgl.checkError()
end
lujgl.setRenderCallback(render)
function think()
if lujgl.frameCount % 1000 == 1000-1 then
print(string.format("FPS: %f", lujgl.fps()))
end
end
lujgl.setIdleCallback(think)
function event(ev,...)
print("Event", ev, ...)
end
lujgl.setEventCallback(event)
print("Entering main loop")
lujgl.mainLoop()
print("Exiting normally")