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;
66 struct gear_template {
75 static const struct gear_template gear_templates[] = {
76 { { 0.8, 0.1, 0.0, 1.0 }, 1.0, 4.0, 1.0, 20, 0.7 },
77 { { 0.0, 0.8, 0.2, 1.0 }, 0.5, 2.0, 2.0, 10, 0.7 },
78 { { 0.2, 0.2, 1.0, 1.0 }, 1.3, 2.0, 0.5, 10, 0.7 },
81 static GLfloat light_pos[4] = {5.0, 5.0, 10.0, 0.0};
83 static void die(const char *msg)
85 fprintf(stderr, "%s", msg);
90 make_gear(const struct gear_template *t)
97 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, t->material);
100 r1 = t->outer_radius - t->tooth_depth / 2.0;
101 r2 = t->outer_radius + t->tooth_depth / 2.0;
103 da = 2.0 * M_PI / t->teeth / 4.0;
105 glShadeModel(GL_FLAT);
107 glNormal3f(0.0, 0.0, 1.0);
109 /* draw front face */
110 glBegin(GL_QUAD_STRIP);
111 for (i = 0; i <= t->teeth; i++) {
112 angle = i * 2.0 * M_PI / t->teeth;
113 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
114 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
116 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
117 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
122 /* draw front sides of teeth */
124 da = 2.0 * M_PI / t->teeth / 4.0;
125 for (i = 0; i < t->teeth; i++) {
126 angle = i * 2.0 * M_PI / t->teeth;
128 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
129 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
130 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
131 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
135 glNormal3f(0.0, 0.0, -1.0);
138 glBegin(GL_QUAD_STRIP);
139 for (i = 0; i <= t->teeth; i++) {
140 angle = i * 2.0 * M_PI / t->teeth;
141 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
142 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
144 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
145 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
150 /* draw back sides of teeth */
152 da = 2.0 * M_PI / t->teeth / 4.0;
153 for (i = 0; i < t->teeth; i++) {
154 angle = i * 2.0 * M_PI / t->teeth;
156 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
157 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
158 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
159 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
163 /* draw outward faces of teeth */
164 glBegin(GL_QUAD_STRIP);
165 for (i = 0; i < t->teeth; i++) {
166 angle = i * 2.0 * M_PI / t->teeth;
168 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
169 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
170 u = r2 * cos(angle + da) - r1 * cos(angle);
171 v = r2 * sin(angle + da) - r1 * sin(angle);
172 len = sqrt(u * u + v * v);
175 glNormal3f(v, -u, 0.0);
176 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
177 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
178 glNormal3f(cos(angle), sin(angle), 0.0);
179 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
180 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
181 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
182 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
183 glNormal3f(v, -u, 0.0);
184 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
185 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
186 glNormal3f(cos(angle), sin(angle), 0.0);
189 glVertex3f(r1 * cos(0), r1 * sin(0), t->width * 0.5);
190 glVertex3f(r1 * cos(0), r1 * sin(0), -t->width * 0.5);
194 glShadeModel(GL_SMOOTH);
196 /* draw inside radius cylinder */
197 glBegin(GL_QUAD_STRIP);
198 for (i = 0; i <= t->teeth; i++) {
199 angle = i * 2.0 * M_PI / t->teeth;
200 glNormal3f(-cos(angle), -sin(angle), 0.0);
201 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
202 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
208 update_fps(struct gears *gears, uint32_t time)
214 diff_ms = time - gears->last_fps;
216 if (diff_ms > 5000) {
217 float seconds = diff_ms / 1000.0;
218 float fps = gears->frames / seconds;
220 printf("%d frames in %6.3f seconds = %6.3f FPS\n", gears->frames, seconds, fps);
224 gears->last_fps = time;
229 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
231 struct gears *gears = data;
233 update_fps(gears, time);
235 gears->angle = (GLfloat) (time % 8192) * 360 / 8192.0;
237 window_schedule_redraw(gears->window);
240 wl_callback_destroy(callback);
243 static const struct wl_callback_listener listener = {
248 motion_handler(struct widget *widget, struct input *input,
249 uint32_t time, float x, float y, void *data)
251 struct gears *gears = data;
252 int offset_x, offset_y;
255 if (gears->button_down) {
256 offset_x = x - gears->last_x;
257 offset_y = y - gears->last_y;
260 gears->view.roty += offset_x * step;
261 gears->view.rotx += offset_y * step;
262 if (gears->view.roty >= 360)
263 gears->view.roty = gears->view.roty - 360;
264 if (gears->view.roty <= 0)
265 gears->view.roty = gears->view.roty + 360;
266 if (gears->view.rotx >= 360)
267 gears->view.rotx = gears->view.rotx - 360;
268 if (gears->view.rotx <= 0)
269 gears->view.rotx = gears->view.rotx + 360;
272 return CURSOR_LEFT_PTR;
276 button_handler(struct widget *widget, struct input *input,
277 uint32_t time, uint32_t button,
278 enum wl_pointer_button_state state, void *data)
280 struct gears *gears = data;
282 if (button == BTN_LEFT) {
283 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
284 gears->button_down = 1;
285 input_get_position(input,
286 &gears->last_x, &gears->last_y);
288 gears->button_down = 0;
294 redraw_handler(struct widget *widget, void *data)
296 struct rectangle window_allocation;
297 struct rectangle allocation;
298 struct wl_callback *callback;
299 struct gears *gears = data;
301 widget_get_allocation(gears->widget, &allocation);
302 window_get_allocation(gears->window, &window_allocation);
304 if (display_acquire_window_surface(gears->d,
306 gears->context) < 0) {
307 die("Unable to acquire window surface, "
308 "compiled without cairo-egl?\n");
311 glViewport(allocation.x,
312 window_allocation.height - allocation.height - allocation.y,
313 allocation.width, allocation.height);
314 glScissor(allocation.x,
315 window_allocation.height - allocation.height - allocation.y,
316 allocation.width, allocation.height);
318 glEnable(GL_SCISSOR_TEST);
319 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
323 glTranslatef(0.0, 0.0, -50);
325 glRotatef(gears->view.rotx, 1.0, 0.0, 0.0);
326 glRotatef(gears->view.roty, 0.0, 1.0, 0.0);
329 glTranslatef(-3.0, -2.0, 0.0);
330 glRotatef(gears->angle, 0.0, 0.0, 1.0);
331 glCallList(gears->gear_list[0]);
335 glTranslatef(3.1, -2.0, 0.0);
336 glRotatef(-2.0 * gears->angle - 9.0, 0.0, 0.0, 1.0);
337 glCallList(gears->gear_list[1]);
341 glTranslatef(-3.1, 4.2, 0.0);
342 glRotatef(-2.0 * gears->angle - 25.0, 0.0, 0.0, 1.0);
343 glCallList(gears->gear_list[2]);
350 display_release_window_surface(gears->d, gears->window);
352 callback = wl_surface_frame(window_get_wl_surface(gears->window));
353 wl_callback_add_listener(callback, &listener, gears);
357 resize_handler(struct widget *widget,
358 int32_t width, int32_t height, void *data)
360 struct gears *gears = data;
362 /* Constrain child size to be square and at least 300x300 */
372 widget_set_size(gears->widget, width, height);
376 keyboard_focus_handler(struct window *window,
377 struct input *device, void *data)
379 window_schedule_redraw(window);
383 fullscreen_handler(struct window *window, void *data)
385 struct gears *gears = data;
387 gears->fullscreen ^= 1;
388 window_set_fullscreen(window, gears->fullscreen);
391 static struct gears *
392 gears_create(struct display *display)
394 const int width = 450, height = 500;
399 gears = malloc(sizeof *gears);
400 memset(gears, 0, sizeof *gears);
402 gears->window = window_create(display);
403 gears->widget = frame_create(gears->window, gears);
404 window_set_title(gears->window, "Wayland Gears");
406 gears->display = display_get_egl_display(gears->d);
407 if (gears->display == NULL)
408 die("failed to create egl display\n");
410 eglBindAPI(EGL_OPENGL_API);
412 gears->config = display_get_argb_egl_config(gears->d);
414 gears->context = eglCreateContext(gears->display, gears->config,
415 EGL_NO_CONTEXT, NULL);
416 if (gears->context == NULL)
417 die("failed to create context\n");
419 if (!eglMakeCurrent(gears->display, NULL, NULL, gears->context))
420 die("failed to make context current\n");
422 for (i = 0; i < 3; i++) {
423 gears->gear_list[i] = glGenLists(1);
424 glNewList(gears->gear_list[i], GL_COMPILE);
425 make_gear(&gear_templates[i]);
429 gears->button_down = 0;
433 gears->view.rotx = 20.0;
434 gears->view.roty = 30.0;
436 gettimeofday(&tv, NULL);
437 gears->last_fps = tv.tv_sec * 1000 + tv.tv_usec / 1000;
438 printf("Warning: FPS count is limited by the wayland compositor or monitor refresh rate\n");
440 glEnable(GL_NORMALIZE);
442 glMatrixMode(GL_PROJECTION);
444 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 200.0);
445 glMatrixMode(GL_MODELVIEW);
447 glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
448 glEnable(GL_CULL_FACE);
449 glEnable(GL_LIGHTING);
451 glEnable(GL_DEPTH_TEST);
452 glClearColor(0, 0, 0, 0.92);
454 window_set_user_data(gears->window, gears);
455 widget_set_resize_handler(gears->widget, resize_handler);
456 widget_set_redraw_handler(gears->widget, redraw_handler);
457 widget_set_button_handler(gears->widget, button_handler);
458 widget_set_motion_handler(gears->widget, motion_handler);
459 window_set_keyboard_focus_handler(gears->window,
460 keyboard_focus_handler);
461 window_set_fullscreen_handler(gears->window, fullscreen_handler);
463 window_schedule_resize(gears->window, width, height);
468 int main(int argc, char *argv[])
472 d = display_create(argc, argv);
474 fprintf(stderr, "failed to create display: %m\n");