-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgears.nt
218 lines (202 loc) · 6.5 KB
/
gears.nt
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
module gears;
import std.(file, string, math, lib.opengl, lib.glfw3, macros.switchover);
platform(default) {
pragma(lib, "Xxf86vm");
}
void main(string[] args) {
bool running = true;
int autoexit;
void gear(float inner-radius, outer-radius, width, int teeth, float tooth-depth) {
auto
r0 = inner-radius,
r1 = outer-radius - tooth-depth / 2f,
r2 = outer-radius + tooth-depth / 2f;
auto da = τ / teeth / 4f;
using mode GL {
ShadeModel FLAT;
Normal3f(0, 0, 1);
void vert1(float a, b) Vertex3f(a, b, width * 0.5);
void vert2(float a, b) Vertex3f(a, b, - width * 0.5);
vec2f cossin(vec2f v) return vec2f(v.(cos x, sin y));
vec2f cossin(float f) return cossin vec2f(f);
// front face
using QuadStrip for int i <- 0..teeth+1 {
auto angle = i * τ / teeth;
vert1 $ r0 * cossin $ angle;
vert1 $ r1 * cossin $ angle;
if (i < teeth) {
vert1 $ r0 * cossin $ angle;
vert1 $ r1 * cossin $ angle + 3 * da;
}
}
// draw front sides of teeth
using Quads for int i <- 0..teeth {
auto angle = i * τ / teeth;
vert1 $ r1 * cossin $ angle;
vert1 $ r2 * cossin $ angle + da;
vert1 $ r2 * cossin $ angle + 2 * da;
vert1 $ r1 * cossin $ angle + 3 * da;
}
Normal3f (0, 0, -1);
// draw back face
using QuadStrip for int i <- 0..teeth+1 {
auto angle = i * τ / teeth;
vert2 $ r1 * cossin angle;
vert2 $ r0 * cossin angle;
if (i < teeth) {
vert2 $ r1 * cossin $ angle + 3 * da;
vert2 $ r0 * cossin $ angle;
}
}
// draw back sides of teeth
using Quads for int i <- 0..teeth {
auto angle = i * τ / teeth;
vert2 $ r1 * cossin $ angle + 3 * da;
vert2 $ r2 * cossin $ angle + 2 * da;
vert2 $ r2 * cossin $ angle + da;
vert2 $ r1 * cossin $ angle;
}
// draw outward faces of teeth
using QuadStrip {
for int i <- 0..teeth {
auto angle = i * τ / teeth;
vert1 $ r1 * cossin angle;
vert2 $ r1 * cossin angle;
auto uv = r2 * cossin (angle + da) - r1 * cossin angle;
uv /= |uv|;
Normal3f(uv.(y, -x), 0);
vert1 $ r2 * cossin $ angle + da;
vert2 $ r2 * cossin $ angle + da;
Normal3f(cossin angle, 0);
vert1 $ r2 * cossin $ angle + 2 * da;
vert2 $ r2 * cossin $ angle + 2 * da;
uv = r1 * cossin (angle + 3 * da) - r2 * cossin (angle + 2 * da);
Normal3f(uv.(y, -x), 0);
vert1 $ r1 * cossin $ angle + 3 * da;
vert2 $ r1 * cossin $ angle + 3 * da;
Normal3f(cossin angle, 0);
}
vert1 $ r1 * cossin 0;
vert2 $ r1 * cossin 0;
}
ShadeModel SMOOTH;
// draw inside radius cylinder
using QuadStrip for int i <- 0..teeth+1 {
auto angle = i * τ / teeth;
Normal3f(-cossin angle, 0f);
vert2 $ r0 * cossin angle;
vert1 $ r0 * cossin angle;
}
}
}
auto view_rot = vec3f(20, 30, 0);
int x 3 gears;
float angle = 0;
void draw() using mode GL {
ClearColor (0, 0, 0, 0);
ClearDepth 1;
Clear $ COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT;
using Matrix {
Rotatef (view_rot.x, vec3f.X);
Rotatef (view_rot.y, vec3f.Y);
Rotatef (view_rot.z, vec3f.Z);
for (int i <- 0..3) &&
auto t <- [vec3f(-3, -2, 0), vec3f(3.1, -2, 0), vec3f(-3.1, 4.2, 0)] &&
auto rot <- [angle, -2 * angle - 9, -2 * angle - 25]
{
using Matrix {
Translatef t;
Rotatef (rot, vec3f.Z);
// TODO: better error message
// CallList gears[id];
CallList gears[i];
}
}
}
}
void animate() angle = 100 * float:glfwGetTime();
void key(GLFWwindow* window, int k, scancode, action, mods) {
if (action != GLFW_PRESS && action != GLFW_REPEAT) return;
using prefix GLFW_KEY_ switch int i over i == k {
case ESCAPE: running = 0;
case UP: view_rot.x += 5;
case DOWN: view_rot.x -= 5;
case LEFT: view_rot.y += 5;
case RIGHT: view_rot.y -= 5;
}
}
void handleChar(GLFWwindow* window, int k) {
if (k == "z") view_rot.z += 5;
if (k == "Z") view_rot.z -= 5;
}
void reshape(GLFWwindow* window, int width, height) using mode GL {
auto ratio = float:height / width;
float znear = 5, zfar = 30, xmax = znear * 0.5;
Viewport(0, 0, width, height);
MatrixMode PROJECTION; LoadIdentity;
Frustum (-xmax, xmax, -xmax*ratio, xmax*ratio, znear, zfar);
MatrixMode MODELVIEW; LoadIdentity;
Translatef $ -20 * vec3f.Z;
}
void init() using mode GL {
auto
pos = [5f, 5f, 10f, 0],
red = [.8f, .1f, 0f, 1f],
green = [0f, .8f, .2f, 1f],
blue = [.2f, .2f, 1f, 1f];
LIGHT0.Lightfv (POSITION, pos.ptr);
Enable CULL_FACE;
Enable LIGHTING;
Enable LIGHT0;
Enable DEPTH_TEST;
for (int id <- 0..3) &&
auto tup <- [(1f, 4f, 1f, 20), (.5f, 2f, 2f, 10), (1.3f, 2f, 0.5f, 10)] &&
auto color <- [&red, &green, &blue] {
gears[id] = GenLists(1);
gears[id].NewList COMPILE;
FRONT.Materialfv(AMBIENT_AND_DIFFUSE, color.ptr);
gear (tup, 0.7);
EndList;
}
Enable NORMALIZE;
for auto arg <- args {
if (arg == "-info") {
writeln "GL_RENDERER\t= $(CToString GetString RENDERER)";
writeln "GL_VERSION\t= $(CToString GetString VERSION)";
writeln "GL_VENDOR\t= $(CToString GetString VENDOR)";
writeln "GL_EXTENSIONS\t= $(CToString GetString EXTENSIONS)";
}
if (arg == "-exit") {
autoexit = 30;
writeln "close after $autoexit s";
}
}
}
using mode GLFW {
if (!Init()) raise new Error "Failed to init GLFW";
onExit Terminate;
SetErrorCallback &HandleError;
WindowHint(DEPTH_BITS, 16);
auto wnd = CreateWindow (300, 300, "Gears", null, null);
if (!wnd) raise new Error "Failed to open GLFW window";
MakeContextCurrent wnd;
SwapInterval 1;
GetWindowSize(wnd, &int width, &int height);
reshape(wnd, width, height);
// lolwat
// wnd.SetInputMode (KEY_REPEAT, true);
SwapInterval 1;
init();
windowSizeCallback = &reshape;
keyCallback = &key;
charCallback = &handleChar;
initCallbacks(wnd);
while running {
draw;
animate;
SwapBuffers wnd;
PollEvents;
// if (!IsWindow(wnd)) running = false;
}
}
}