-
Notifications
You must be signed in to change notification settings - Fork 1
/
opengl.lua
321 lines (254 loc) · 8.24 KB
/
opengl.lua
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
---@diagnostic disable: lowercase-global
--================================================
local ffi = require("ffi")
--[[
/* this is includes.c, processed with:
gcc -E includes.c | grep -v '^#' > ffi_defs_gl.h
*/
#include <SDL2/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
]]
ffi.cdef(io.open('ffi_defs_gl.h', 'r'):read('*a'))
local SDL = ffi.load('SDL2')
local libraries = {}
libraries.GL = { Linux = 'GL', Windows = 'openGL32' }
libraries.GLU = { Linux = 'GLU', Windows = 'GLU32' }
local GL = ffi.load(libraries.GL[ffi.os])
local GLU = ffi.load(libraries.GLU[ffi.os])
--require("sdl-defs")(SDL)
require("opengl-defs")(GL, GLU, SDL)
--====================================
SDL_Init(0)
local window_width, window_height = 400, 300
local window = SDL_CreateWindow("opengl in sdl", 50, 50, window_width, window_height, SDL_WINDOW_OPENGL)
context = SDL_GL_CreateContext(window)
function update(mouse_position, mouse_down)
-- to be defined
end
local bit = require("bit")
local binary_or = bit.bor
local angle = 0
function draw()
-- update scene
angle = angle + 1
-- draw scene
local lightPosition = ffi.new("float[4]", 15, 10, 5, 1)
local lightAmbient = ffi.new("float[4]", 0.1, 0.1, 0.1, 1)
local lightDiffuse = ffi.new("float[4]", 0.9, 0.9, 0.9, 1)
local redMaterial = ffi.new("float[4]", 1, 0, 0, 1)
local blueMaterial = ffi.new("float[4]", 0, 0, 1, 1)
local greyMaterial = ffi.new("float[4]", 0.5, 0.5, 0.5, 1)
local greenMaterial = ffi.new("float[4]", 0, 1, 0, 1)
glViewport(0, 0, window_width, window_height);
glClearColor(1, 1, 1, 0);
glClear(binary_or(GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT));
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30, window_width / window_height, 1, 200);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(20, 5, 40, 0, 0, 0, 0, 1, 0);
glShadeModel(GL_SMOOTH);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glMaterialfv(GL_FRONT, GL_AMBIENT, greyMaterial);
glMaterialfv(GL_FRONT, GL_DIFFUSE, greenMaterial);
glPushMatrix();
glRotated(angle, 0., 1., 0.);
---drawBox(-1, -1, -1, 1, 1, 1);
glPushMatrix();
local factor = 5
glScalef(factor, factor, factor);
glTranslatef(.3, 0, 0);
draw_model(model)
glPopMatrix();
glPushMatrix();
local factor = 5
glScalef(factor, factor, factor);
glTranslatef(0, -0.6, 0);
draw_model(model2)
glPopMatrix();
glMaterialfv(GL_FRONT, GL_AMBIENT, greyMaterial);
glMaterialfv(GL_FRONT, GL_DIFFUSE, blueMaterial);
glPushMatrix();
local factor = .1
glScalef(factor, factor, factor);
glTranslatef(0, 0, 0);
draw_model(model_axes)
glPopMatrix();
--[[
glMaterialfv(GL_FRONT, GL_AMBIENT, greyMaterial);
glMaterialfv(GL_FRONT, GL_DIFFUSE, redMaterial);
glPushMatrix();
glTranslated(0.,1.75,0.);
glRotated(angle, 0., 1., 0.);
drawBox(-.5,-.5,-.5,.5,.5,.5);
glPopMatrix();
glPushMatrix();
glTranslated(0.,-1.75,0.);
glRotated(angle, 0., 1., 0.);
drawBox(-.5,-.5,-.5,.5,.5,.5);
glPopMatrix();
glPushMatrix();
glRotated(90., 1., 0., 0.);
glTranslated(0.,1.75,0.);
glRotated(angle, 0., 1., 0.);
drawBox(-.5,-.5,-.5,.5,.5,.5);
glPopMatrix();
glPushMatrix();
glRotated(90., -1., 0., 0.);
glTranslated(0.,1.75,0.);
glRotated(angle, 0., 1., 0.);
drawBox(-.5,-.5,-.5,.5,.5,.5);
glPopMatrix();
glPushMatrix();
glRotated(90., 0., 0., 1.);
glTranslated(0.,1.75,0.);
glRotated(angle, 0., 1., 0.);
drawBox(-.5,-.5,-.5,.5,.5,.5);
glPopMatrix();
glPushMatrix();
glRotated(90., 0., 0., -1.);
glTranslated(0.,1.75,0.);
glRotated(angle, 0., 1., 0.);
drawBox(-.5,-.5,-.5,.5,.5,.5);
glPopMatrix();
--]]
glPopMatrix();
end
----------------------------------------------
require("loader")
model_axes = load_obj_file("assets/axes.obj") -- you can comment this line and viceversa
print("loading model_axes: " .. (model_axes and "OK" or "failed!"))
-- "assets/cube.obj" was corrected in loading (previous loader was conflicting vertex normals, super-imposed, re-assigned)
-- "assets/head.obj" more complex and more memory intensive also (memory use improvement)
model = load_obj_file("assets/head.obj")
print("loading model: " .. (model and "OK" or "failed!"))
model2 = load_obj_file("assets/cube.obj")
print("loading model2: " .. (model2 and "OK" or "failed!"))
function draw_model(model)
--[[
glBegin(GL_TRIANGLE_STRIP);
glNormal3f(0.,0.,-1.);
glVertex3f(xmin, ymin, zmin);
glVertex3f(xmin, ymax, zmin);
glVertex3f(xmax, ymin, zmin);
glVertex3f(xmax, ymax, zmin);
glEnd();
--]]
glBegin(GL_TRIANGLES)
-- glNormal3f, glVertex3f
for _, triangle in ipairs(model) do
local function normal(xyz) glNormal3f(xyz.x, xyz.y, xyz.z) end
local function vertex(xyz) glVertex3f(xyz.x, xyz.y, xyz.z) end
local function vertex_and_normal(xyz)
normal(xyz.normal); vertex(xyz)
end
vertex_and_normal(triangle[1])
vertex_and_normal(triangle[2])
vertex_and_normal(triangle[3])
end
glEnd()
end
function draw_model_2(model)
if not model then
return
end
local triangles, indexable_vertex_position_xyz, indexable_vertex_normal_xyz = model[1], model[2], model[3]
glBegin(GL_TRIANGLES)
for _, triangle in ipairs(triangles) do
local position_indices = triangle.vertex_position_indices
local normal_indices = triangle.vertex_normal_indices
-- vertex 1
local normal1 = indexable_vertex_normal_xyz[normal_indices[1]]
glNormal3f(normal1[1], normal1[2], normal1[3])
local position1 = indexable_vertex_position_xyz[position_indices[1]]
glVertex3f(position1[1], position1[2], position1[3])
-- vertex 2
local normal2 = indexable_vertex_normal_xyz[normal_indices[2]]
glNormal3f(normal2[1], normal2[2], normal2[3])
local position2 = indexable_vertex_position_xyz[position_indices[2]]
glVertex3f(position2[1], position2[2], position2[3])
-- vertex 3
local normal3 = indexable_vertex_normal_xyz[normal_indices[3]]
glNormal3f(normal3[1], normal3[2], normal3[3])
local position3 = indexable_vertex_position_xyz[position_indices[3]]
glVertex3f(position3[1], position3[2], position3[3])
end
glEnd()
end
-- // (cleaner) code import from gltest.cpp (part of https://fox-toolkit.org/)
-- // Draws a simple box using the given corners
function drawBox(xmin, ymin, zmin, xmax, ymax, zmax)
glBegin(GL_TRIANGLE_STRIP);
glNormal3f(0., 0., -1.);
glVertex3f(xmin, ymin, zmin);
glVertex3f(xmin, ymax, zmin);
glVertex3f(xmax, ymin, zmin);
glVertex3f(xmax, ymax, zmin);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glNormal3f(1., 0., 0.);
glVertex3f(xmax, ymin, zmin);
glVertex3f(xmax, ymax, zmin);
glVertex3f(xmax, ymin, zmax);
glVertex3f(xmax, ymax, zmax);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glNormal3f(0., 0., 1.);
glVertex3f(xmax, ymin, zmax);
glVertex3f(xmax, ymax, zmax);
glVertex3f(xmin, ymin, zmax);
glVertex3f(xmin, ymax, zmax);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glNormal3f(-1., 0., 0.);
glVertex3f(xmin, ymin, zmax);
glVertex3f(xmin, ymax, zmax);
glVertex3f(xmin, ymin, zmin);
glVertex3f(xmin, ymax, zmin);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glNormal3f(0., 1., 0.);
glVertex3f(xmin, ymax, zmin);
glVertex3f(xmin, ymax, zmax);
glVertex3f(xmax, ymax, zmin);
glVertex3f(xmax, ymax, zmax);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glNormal3f(0., -1., 0.);
glVertex3f(xmax, ymin, zmax);
glVertex3f(xmax, ymin, zmin);
glVertex3f(xmin, ymin, zmax);
glVertex3f(xmin, ymin, zmin);
glEnd();
end
--=============================
local event = ffi.new("SDL_Event")
local looping = true
local mouse_position = { 0, 0 }
local mouse_down = false
while looping do
while SDL_PollEvent(event) ~= 0 do
if event.type == SDL_QUIT or
(event.type == SDL_KEYDOWN and event.key.keysym.sym == SDLK_ESCAPE)
then
looping = false
elseif event.type == SDL_MOUSEBUTTONDOWN then
mouse_down = true
elseif event.type == SDL_MOUSEBUTTONUP then
mouse_down = false
elseif event.type == SDL_MOUSEMOTION then
mouse_position = { event.button.x, event.button.y }
end
end
update(mouse_position, mouse_down)
draw()
SDL_GL_SwapWindow(window)
end
SDL_Quit()