-
Notifications
You must be signed in to change notification settings - Fork 0
/
glutils.t
181 lines (151 loc) · 4.72 KB
/
glutils.t
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
local m = require("mem")
local util = require("util")
local Vec = require("linalg").Vec
local gl = require("gl")
local Vec3d = Vec(double, 3)
local Color4d = Vec(double, 4)
-- Simple camera class that packages up data needed to establish 3D
-- viewing / projection transforms
local struct Camera
{
eye: Vec3d,
target: Vec3d,
up: Vec3d,
fovy: double, -- in degrees
aspect: double,
znear: double,
zfar: double
}
-- Default camera is looking down -z
terra Camera:__construct()
self.eye = Vec3d.stackAlloc(0.0, 0.0, 0.0)
self.target = Vec3d.stackAlloc(0.0, 0.0, -1.0)
self.up = Vec3d.stackAlloc(0.0, 1.0, 0.0)
self.fovy = 45.0
self.aspect = 1.0
self.znear = 1.0
self.zfar = 100.0
end
terra Camera:__construct(eye: Vec3d, target: Vec3d, up: Vec3d, fovy: double, aspect: double, znear: double, zfar: double)
self.eye = eye
self.target = target
self.up = up
self.fovy = fovy
self.aspect = aspect
self.znear = znear
self.zfar = zfar
end
-- OpenGL 1.1 style
terra Camera:setupGLPerspectiveView()
gl.glMatrixMode(gl.mGL_MODELVIEW())
gl.glLoadIdentity()
gl.gluLookAt(self.eye(0), self.eye(1), self.eye(2),
self.target(0), self.target(1), self.target(2),
self.up(0), self.up(1), self.up(2))
gl.glMatrixMode(gl.mGL_PROJECTION())
gl.glLoadIdentity()
gl.gluPerspective(self.fovy, self.aspect, self.znear, self.zfar)
end
m.addConstructors(Camera)
-- Simple light class that packages up parameters about lights
local LightType = uint
local Directional = 0
local Point = 1
local struct Light
{
type: LightType,
union
{
pos: Vec3d,
dir: Vec3d
},
ambient: Color4d,
diffuse: Color4d,
specular: Color4d
}
Light.LightType = LightType
Light.Point = Point
Light.Directional = Directional
terra Light:__construct()
self.type = Directional
self.dir = Vec3d.stackAlloc(1.0, 1.0, 1.0)
self.ambient = Color4d.stackAlloc(0.3, 0.3, 0.3, 1.0)
self.diffuse = Color4d.stackAlloc(1.0, 1.0, 1.0, 1.0)
self.specular = Color4d.stackAlloc(1.0, 1.0, 1.0, 1.0)
end
terra Light:__construct(type: LightType, posOrDir: Vec3d, ambient: Color4d, diffuse: Color4d, specular: Color4d) : {}
self.type = type
self.pos = posOrDir
self.ambient = ambient
self.diffuse = diffuse
self.specular = specular
end
terra Light:__construct(type: LightType, posOrDir: Vec3d, diffuse: Color4d, ambAmount: double, specular: Color4d) : {}
self.type = type
self.pos = posOrDir
self.ambient = ambAmount * diffuse; self.ambient(3) = self.diffuse(3)
self.diffuse = diffuse
self.specular = specular
end
-- OpenGL 1.1 style
terra Light:setupGLLight(lightID: int)
util.assert(lightID >= 0 and lightID < gl.mGL_MAX_LIGHTS(),
"lightID must be in the range [0,%d); got %d instead\n", 0, gl.mGL_MAX_LIGHTS(), lightID)
var lightNumFlag = gl.mGL_LIGHT0() + lightID
gl.glEnable(lightNumFlag)
var floatArr = arrayof(float, [Color4d.elements(`self.ambient)])
gl.glLightfv(lightNumFlag, gl.mGL_AMBIENT(), floatArr)
floatArr = arrayof(float, [Color4d.elements(`self.diffuse)])
gl.glLightfv(lightNumFlag, gl.mGL_DIFFUSE(), floatArr)
floatArr = arrayof(float, [Color4d.elements(`self.specular)])
gl.glLightfv(lightNumFlag, gl.mGL_SPECULAR(), floatArr)
-- Leverage the fact that the light type flags correspond to the value of the w coordinate
floatArr = arrayof(float, [Vec3d.elements(`self.pos)], self.type)
gl.glLightfv(lightNumFlag, gl.mGL_POSITION(), floatArr)
end
m.addConstructors(Light)
-- Simple material class to package up material params
struct Material
{
ambient: Color4d,
diffuse: Color4d,
specular: Color4d,
shininess: double
}
terra Material:__construct()
self.ambient = Color4d.stackAlloc(0.8, 0.8, 0.8, 1.0)
self.diffuse = Color4d.stackAlloc(0.8, 0.8, 0.8, 1.0)
self.specular = Color4d.stackAlloc(0.0, 0.0, 0.0, 1.0)
self.shininess = 0.0
end
terra Material:__construct(ambient: Color4d, diffuse: Color4d, specular: Color4d, shininess: double)
self.ambient = ambient
self.diffuse = diffuse
self.specular = specular
self.shininess = shininess
end
terra Material:__construct(diffuse: Color4d, specular: Color4d, shininess: double)
self.ambient = diffuse
self.diffuse = diffuse
self.specular = specular
self.shininess = shininess
end
-- OpenGL 1.1 style
terra Material:setupGLMaterial()
-- Just default everything to only affecting the front faces
var flag = gl.mGL_FRONT()
var floatArr = arrayof(float, [Color4d.elements(`self.ambient)])
gl.glMaterialfv(flag, gl.mGL_AMBIENT(), floatArr)
floatArr = arrayof(float, [Color4d.elements(`self.diffuse)])
gl.glMaterialfv(flag, gl.mGL_DIFFUSE(), floatArr)
floatArr = arrayof(float, [Color4d.elements(`self.specular)])
gl.glMaterialfv(flag, gl.mGL_SPECULAR(), floatArr)
gl.glMaterialf(flag, gl.mGL_SHININESS(), self.shininess)
end
m.addConstructors(Material)
return
{
Camera = Camera,
Light = Light,
Material = Material
}