2 * Copyright © 2008 Kristian Høgsberg
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
34 #include <EGL/eglext.h>
36 #include <linux/input.h>
37 #include <wayland-client.h>
42 struct window *window;
43 struct widget *widget;
63 struct gear_template {
72 static const struct gear_template gear_templates[] = {
73 { { 0.8, 0.1, 0.0, 1.0 }, 1.0, 4.0, 1.0, 20, 0.7 },
74 { { 0.0, 0.8, 0.2, 1.0 }, 0.5, 2.0, 2.0, 10, 0.7 },
75 { { 0.2, 0.2, 1.0, 1.0 }, 1.3, 2.0, 0.5, 10, 0.7 },
78 static GLfloat light_pos[4] = {5.0, 5.0, 10.0, 0.0};
80 static void die(const char *msg)
82 fprintf(stderr, "%s", msg);
87 make_gear(const struct gear_template *t)
94 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, t->material);
97 r1 = t->outer_radius - t->tooth_depth / 2.0;
98 r2 = t->outer_radius + t->tooth_depth / 2.0;
100 da = 2.0 * M_PI / t->teeth / 4.0;
102 glShadeModel(GL_FLAT);
104 glNormal3f(0.0, 0.0, 1.0);
106 /* draw front face */
107 glBegin(GL_QUAD_STRIP);
108 for (i = 0; i <= t->teeth; i++) {
109 angle = i * 2.0 * M_PI / t->teeth;
110 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
111 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
113 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
114 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
119 /* draw front sides of teeth */
121 da = 2.0 * M_PI / t->teeth / 4.0;
122 for (i = 0; i < t->teeth; i++) {
123 angle = i * 2.0 * M_PI / t->teeth;
125 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
126 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
127 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
128 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
132 glNormal3f(0.0, 0.0, -1.0);
135 glBegin(GL_QUAD_STRIP);
136 for (i = 0; i <= t->teeth; i++) {
137 angle = i * 2.0 * M_PI / t->teeth;
138 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
139 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
141 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
142 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
147 /* draw back sides of teeth */
149 da = 2.0 * M_PI / t->teeth / 4.0;
150 for (i = 0; i < t->teeth; i++) {
151 angle = i * 2.0 * M_PI / t->teeth;
153 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
154 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
155 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
156 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
160 /* draw outward faces of teeth */
161 glBegin(GL_QUAD_STRIP);
162 for (i = 0; i < t->teeth; i++) {
163 angle = i * 2.0 * M_PI / t->teeth;
165 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
166 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
167 u = r2 * cos(angle + da) - r1 * cos(angle);
168 v = r2 * sin(angle + da) - r1 * sin(angle);
169 len = sqrt(u * u + v * v);
172 glNormal3f(v, -u, 0.0);
173 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
174 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
175 glNormal3f(cos(angle), sin(angle), 0.0);
176 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
177 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
178 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
179 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
180 glNormal3f(v, -u, 0.0);
181 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
182 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
183 glNormal3f(cos(angle), sin(angle), 0.0);
186 glVertex3f(r1 * cos(0), r1 * sin(0), t->width * 0.5);
187 glVertex3f(r1 * cos(0), r1 * sin(0), -t->width * 0.5);
191 glShadeModel(GL_SMOOTH);
193 /* draw inside radius cylinder */
194 glBegin(GL_QUAD_STRIP);
195 for (i = 0; i <= t->teeth; i++) {
196 angle = i * 2.0 * M_PI / t->teeth;
197 glNormal3f(-cos(angle), -sin(angle), 0.0);
198 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
199 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
205 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
207 struct gears *gears = data;
209 gears->angle = (GLfloat) (time % 8192) * 360 / 8192.0;
211 window_schedule_redraw(gears->window);
214 wl_callback_destroy(callback);
217 static const struct wl_callback_listener listener = {
222 motion_handler(struct widget *widget, struct input *input,
223 uint32_t time, float x, float y, void *data)
225 struct gears *gears = data;
226 int offset_x, offset_y;
229 if (gears->button_down) {
230 offset_x = x - gears->last_x;
231 offset_y = y - gears->last_y;
234 gears->view.roty += offset_x * step;
235 gears->view.rotx += offset_y * step;
236 if (gears->view.roty >= 360)
237 gears->view.roty = gears->view.roty - 360;
238 if (gears->view.roty <= 0)
239 gears->view.roty = gears->view.roty + 360;
240 if (gears->view.rotx >= 360)
241 gears->view.rotx = gears->view.rotx - 360;
242 if (gears->view.rotx <= 0)
243 gears->view.rotx = gears->view.rotx + 360;
246 return POINTER_LEFT_PTR;
250 button_handler(struct widget *widget, struct input *input,
251 uint32_t time, uint32_t button, uint32_t state, void *data)
253 struct gears *gears = data;
255 if (button == BTN_LEFT) {
257 gears->button_down = 1;
258 input_get_position(input,
259 &gears->last_x, &gears->last_y);
261 gears->button_down = 0;
267 redraw_handler(struct widget *widget, void *data)
269 struct rectangle window_allocation;
270 struct rectangle allocation;
271 struct wl_callback *callback;
272 struct gears *gears = data;
274 widget_get_allocation(gears->widget, &allocation);
275 window_get_allocation(gears->window, &window_allocation);
277 if (display_acquire_window_surface(gears->d,
279 gears->context) < 0) {
280 die("Unable to acquire window surface, "
281 "compiled without cairo-egl?\n");
284 glViewport(allocation.x,
285 window_allocation.height - allocation.height - allocation.y,
286 allocation.width, allocation.height);
287 glScissor(allocation.x,
288 window_allocation.height - allocation.height - allocation.y,
289 allocation.width, allocation.height);
291 glEnable(GL_SCISSOR_TEST);
292 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
296 glTranslatef(0.0, 0.0, -50);
298 glRotatef(gears->view.rotx, 1.0, 0.0, 0.0);
299 glRotatef(gears->view.roty, 0.0, 1.0, 0.0);
302 glTranslatef(-3.0, -2.0, 0.0);
303 glRotatef(gears->angle, 0.0, 0.0, 1.0);
304 glCallList(gears->gear_list[0]);
308 glTranslatef(3.1, -2.0, 0.0);
309 glRotatef(-2.0 * gears->angle - 9.0, 0.0, 0.0, 1.0);
310 glCallList(gears->gear_list[1]);
314 glTranslatef(-3.1, 4.2, 0.0);
315 glRotatef(-2.0 * gears->angle - 25.0, 0.0, 0.0, 1.0);
316 glCallList(gears->gear_list[2]);
323 display_release_window_surface(gears->d, gears->window);
325 callback = wl_surface_frame(window_get_wl_surface(gears->window));
326 wl_callback_add_listener(callback, &listener, gears);
330 resize_handler(struct widget *widget,
331 int32_t width, int32_t height, void *data)
333 struct gears *gears = data;
335 /* Constrain child size to be square and at least 300x300 */
345 widget_set_size(gears->widget, width, height);
349 keyboard_focus_handler(struct window *window,
350 struct input *device, void *data)
352 window_schedule_redraw(window);
355 static struct gears *
356 gears_create(struct display *display)
358 const int width = 450, height = 500;
362 gears = malloc(sizeof *gears);
363 memset(gears, 0, sizeof *gears);
365 gears->window = window_create(display);
366 gears->widget = frame_create(gears->window, gears);
367 window_set_title(gears->window, "Wayland Gears");
369 gears->display = display_get_egl_display(gears->d);
370 if (gears->display == NULL)
371 die("failed to create egl display\n");
373 eglBindAPI(EGL_OPENGL_API);
375 gears->config = display_get_argb_egl_config(gears->d);
377 gears->context = eglCreateContext(gears->display, gears->config,
378 EGL_NO_CONTEXT, NULL);
379 if (gears->context == NULL)
380 die("failed to create context\n");
382 if (!eglMakeCurrent(gears->display, NULL, NULL, gears->context))
383 die("failed to make context current\n");
385 for (i = 0; i < 3; i++) {
386 gears->gear_list[i] = glGenLists(1);
387 glNewList(gears->gear_list[i], GL_COMPILE);
388 make_gear(&gear_templates[i]);
392 gears->button_down = 0;
396 gears->view.rotx = 20.0;
397 gears->view.roty = 30.0;
399 glEnable(GL_NORMALIZE);
401 glMatrixMode(GL_PROJECTION);
403 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 200.0);
404 glMatrixMode(GL_MODELVIEW);
406 glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
407 glEnable(GL_CULL_FACE);
408 glEnable(GL_LIGHTING);
410 glEnable(GL_DEPTH_TEST);
411 glClearColor(0, 0, 0, 0.92);
413 window_set_user_data(gears->window, gears);
414 widget_set_resize_handler(gears->widget, resize_handler);
415 widget_set_redraw_handler(gears->widget, redraw_handler);
416 widget_set_button_handler(gears->widget, button_handler);
417 widget_set_motion_handler(gears->widget, motion_handler);
418 window_set_keyboard_focus_handler(gears->window,
419 keyboard_focus_handler);
421 window_schedule_resize(gears->window, width, height);
426 int main(int argc, char *argv[])
430 d = display_create(argc, argv);
432 fprintf(stderr, "failed to create display: %m\n");