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
35 #include <EGL/eglext.h>
37 #include <linux/input.h>
38 #include <wayland-client.h>
43 struct window *window;
44 struct widget *widget;
67 struct gear_template {
76 static const struct gear_template gear_templates[] = {
77 { { 0.8, 0.1, 0.0, 1.0 }, 1.0, 4.0, 1.0, 20, 0.7 },
78 { { 0.0, 0.8, 0.2, 1.0 }, 0.5, 2.0, 2.0, 10, 0.7 },
79 { { 0.2, 0.2, 1.0, 1.0 }, 1.3, 2.0, 0.5, 10, 0.7 },
82 static GLfloat light_pos[4] = {5.0, 5.0, 10.0, 0.0};
84 static void die(const char *msg)
86 fprintf(stderr, "%s", msg);
91 make_gear(const struct gear_template *t)
98 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, t->material);
100 r0 = t->inner_radius;
101 r1 = t->outer_radius - t->tooth_depth / 2.0;
102 r2 = t->outer_radius + t->tooth_depth / 2.0;
104 da = 2.0 * M_PI / t->teeth / 4.0;
106 glShadeModel(GL_FLAT);
108 glNormal3f(0.0, 0.0, 1.0);
110 /* draw front face */
111 glBegin(GL_QUAD_STRIP);
112 for (i = 0; i <= t->teeth; i++) {
113 angle = i * 2.0 * M_PI / t->teeth;
114 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
115 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
117 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
118 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
123 /* draw front sides of teeth */
125 da = 2.0 * M_PI / t->teeth / 4.0;
126 for (i = 0; i < t->teeth; i++) {
127 angle = i * 2.0 * M_PI / t->teeth;
129 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
130 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
131 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
132 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
136 glNormal3f(0.0, 0.0, -1.0);
139 glBegin(GL_QUAD_STRIP);
140 for (i = 0; i <= t->teeth; i++) {
141 angle = i * 2.0 * M_PI / t->teeth;
142 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
143 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
145 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
146 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
151 /* draw back sides of teeth */
153 da = 2.0 * M_PI / t->teeth / 4.0;
154 for (i = 0; i < t->teeth; i++) {
155 angle = i * 2.0 * M_PI / t->teeth;
157 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
158 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
159 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
160 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
164 /* draw outward faces of teeth */
165 glBegin(GL_QUAD_STRIP);
166 for (i = 0; i < t->teeth; i++) {
167 angle = i * 2.0 * M_PI / t->teeth;
169 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
170 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
171 u = r2 * cos(angle + da) - r1 * cos(angle);
172 v = r2 * sin(angle + da) - r1 * sin(angle);
173 len = sqrt(u * u + v * v);
176 glNormal3f(v, -u, 0.0);
177 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
178 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
179 glNormal3f(cos(angle), sin(angle), 0.0);
180 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
181 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
182 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
183 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
184 glNormal3f(v, -u, 0.0);
185 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
186 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
187 glNormal3f(cos(angle), sin(angle), 0.0);
190 glVertex3f(r1 * cos(0), r1 * sin(0), t->width * 0.5);
191 glVertex3f(r1 * cos(0), r1 * sin(0), -t->width * 0.5);
195 glShadeModel(GL_SMOOTH);
197 /* draw inside radius cylinder */
198 glBegin(GL_QUAD_STRIP);
199 for (i = 0; i <= t->teeth; i++) {
200 angle = i * 2.0 * M_PI / t->teeth;
201 glNormal3f(-cos(angle), -sin(angle), 0.0);
202 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
203 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
209 update_fps(struct gears *gears, uint32_t time)
212 static bool first_call = true;
215 gears->last_fps = time;
220 diff_ms = time - gears->last_fps;
222 if (diff_ms > 5000) {
223 float seconds = diff_ms / 1000.0;
224 float fps = gears->frames / seconds;
226 printf("%d frames in %6.3f seconds = %6.3f FPS\n", gears->frames, seconds, fps);
230 gears->last_fps = time;
235 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
237 struct gears *gears = data;
239 update_fps(gears, time);
241 gears->angle = (GLfloat) (time % 8192) * 360 / 8192.0;
243 window_schedule_redraw(gears->window);
246 wl_callback_destroy(callback);
249 static const struct wl_callback_listener listener = {
254 motion_handler(struct widget *widget, struct input *input,
255 uint32_t time, float x, float y, void *data)
257 struct gears *gears = data;
258 int offset_x, offset_y;
261 if (gears->button_down) {
262 offset_x = x - gears->last_x;
263 offset_y = y - gears->last_y;
266 gears->view.roty += offset_x * step;
267 gears->view.rotx += offset_y * step;
268 if (gears->view.roty >= 360)
269 gears->view.roty = gears->view.roty - 360;
270 if (gears->view.roty <= 0)
271 gears->view.roty = gears->view.roty + 360;
272 if (gears->view.rotx >= 360)
273 gears->view.rotx = gears->view.rotx - 360;
274 if (gears->view.rotx <= 0)
275 gears->view.rotx = gears->view.rotx + 360;
278 return CURSOR_LEFT_PTR;
282 button_handler(struct widget *widget, struct input *input,
283 uint32_t time, uint32_t button,
284 enum wl_pointer_button_state state, void *data)
286 struct gears *gears = data;
288 if (button == BTN_LEFT) {
289 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
290 gears->button_down = 1;
291 input_get_position(input,
292 &gears->last_x, &gears->last_y);
294 gears->button_down = 0;
300 redraw_handler(struct widget *widget, void *data)
302 struct rectangle window_allocation;
303 struct rectangle allocation;
304 struct wl_callback *callback;
305 struct gears *gears = data;
307 widget_get_allocation(gears->widget, &allocation);
308 window_get_allocation(gears->window, &window_allocation);
310 if (display_acquire_window_surface(gears->d,
312 gears->context) < 0) {
313 die("Unable to acquire window surface, "
314 "compiled without cairo-egl?\n");
317 glViewport(allocation.x,
318 window_allocation.height - allocation.height - allocation.y,
319 allocation.width, allocation.height);
320 glScissor(allocation.x,
321 window_allocation.height - allocation.height - allocation.y,
322 allocation.width, allocation.height);
324 glEnable(GL_SCISSOR_TEST);
325 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
329 glTranslatef(0.0, 0.0, -50);
331 glRotatef(gears->view.rotx, 1.0, 0.0, 0.0);
332 glRotatef(gears->view.roty, 0.0, 1.0, 0.0);
335 glTranslatef(-3.0, -2.0, 0.0);
336 glRotatef(gears->angle, 0.0, 0.0, 1.0);
337 glCallList(gears->gear_list[0]);
341 glTranslatef(3.1, -2.0, 0.0);
342 glRotatef(-2.0 * gears->angle - 9.0, 0.0, 0.0, 1.0);
343 glCallList(gears->gear_list[1]);
347 glTranslatef(-3.1, 4.2, 0.0);
348 glRotatef(-2.0 * gears->angle - 25.0, 0.0, 0.0, 1.0);
349 glCallList(gears->gear_list[2]);
356 display_release_window_surface(gears->d, gears->window);
358 callback = wl_surface_frame(window_get_wl_surface(gears->window));
359 wl_callback_add_listener(callback, &listener, gears);
363 resize_handler(struct widget *widget,
364 int32_t width, int32_t height, void *data)
366 struct gears *gears = data;
367 int32_t size, big, small;
369 /* Constrain child size to be square and at least 300x300 */
370 if (width < height) {
378 if (gears->fullscreen)
383 widget_set_size(gears->widget, size, size);
387 keyboard_focus_handler(struct window *window,
388 struct input *device, void *data)
390 window_schedule_redraw(window);
394 fullscreen_handler(struct window *window, void *data)
396 struct gears *gears = data;
398 gears->fullscreen ^= 1;
399 window_set_fullscreen(window, gears->fullscreen);
402 static struct gears *
403 gears_create(struct display *display)
405 const int width = 450, height = 500;
409 gears = zalloc(sizeof *gears);
411 gears->window = window_create(display);
412 gears->widget = window_frame_create(gears->window, gears);
413 window_set_title(gears->window, "Wayland Gears");
415 gears->display = display_get_egl_display(gears->d);
416 if (gears->display == NULL)
417 die("failed to create egl display\n");
419 eglBindAPI(EGL_OPENGL_API);
421 gears->config = display_get_argb_egl_config(gears->d);
423 gears->context = eglCreateContext(gears->display, gears->config,
424 EGL_NO_CONTEXT, NULL);
425 if (gears->context == NULL)
426 die("failed to create context\n");
428 if (!eglMakeCurrent(gears->display, NULL, NULL, gears->context))
429 die("failed to make context current\n");
431 for (i = 0; i < 3; i++) {
432 gears->gear_list[i] = glGenLists(1);
433 glNewList(gears->gear_list[i], GL_COMPILE);
434 make_gear(&gear_templates[i]);
438 gears->button_down = 0;
442 gears->view.rotx = 20.0;
443 gears->view.roty = 30.0;
445 printf("Warning: FPS count is limited by the wayland compositor or monitor refresh rate\n");
447 glEnable(GL_NORMALIZE);
449 glMatrixMode(GL_PROJECTION);
451 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 200.0);
452 glMatrixMode(GL_MODELVIEW);
454 glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
455 glEnable(GL_CULL_FACE);
456 glEnable(GL_LIGHTING);
458 glEnable(GL_DEPTH_TEST);
459 glClearColor(0, 0, 0, 0.92);
461 window_set_user_data(gears->window, gears);
462 widget_set_resize_handler(gears->widget, resize_handler);
463 widget_set_redraw_handler(gears->widget, redraw_handler);
464 widget_set_button_handler(gears->widget, button_handler);
465 widget_set_motion_handler(gears->widget, motion_handler);
466 window_set_keyboard_focus_handler(gears->window,
467 keyboard_focus_handler);
468 window_set_fullscreen_handler(gears->window, fullscreen_handler);
470 window_schedule_resize(gears->window, width, height);
476 gears_destroy(struct gears *gears)
478 widget_destroy(gears->widget);
479 window_destroy(gears->window);
483 int main(int argc, char *argv[])
488 d = display_create(&argc, argv);
490 fprintf(stderr, "failed to create display: %m\n");
493 gears = gears_create(d);
496 gears_destroy(gears);