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
36 #define GL_GLEXT_PROTOTYPES
37 #define EGL_EGLEXT_PROTOTYPES
40 #include <EGL/eglext.h>
42 #include "wayland-util.h"
43 #include "wayland-client.h"
44 #include "wayland-glib.h"
49 struct window *window;
56 cairo_surface_t *cairo_surface;
59 GLuint fbo, color_rbo[2], depth_rbo;
60 cairo_surface_t *surface[2];
64 struct gear_template {
73 const static struct gear_template gear_templates[] = {
74 { { 0.8, 0.1, 0.0, 1.0 }, 1.0, 4.0, 1.0, 20, 0.7 },
75 { { 0.0, 0.8, 0.2, 1.0 }, 0.5, 2.0, 2.0, 10, 0.7 },
76 { { 0.2, 0.2, 1.0, 1.0 }, 1.3, 2.0, 0.5, 10, 0.7 },
79 static GLfloat light_pos[4] = {5.0, 5.0, 10.0, 0.0};
81 static void die(const char *msg)
83 fprintf(stderr, "%s", msg);
88 make_gear(const struct gear_template *t)
95 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, t->material);
98 r1 = t->outer_radius - t->tooth_depth / 2.0;
99 r2 = t->outer_radius + t->tooth_depth / 2.0;
101 da = 2.0 * M_PI / t->teeth / 4.0;
103 glShadeModel(GL_FLAT);
105 glNormal3f(0.0, 0.0, 1.0);
107 /* draw front face */
108 glBegin(GL_QUAD_STRIP);
109 for (i = 0; i <= t->teeth; i++) {
110 angle = i * 2.0 * M_PI / t->teeth;
111 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
112 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
114 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
115 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
120 /* draw front sides of teeth */
122 da = 2.0 * M_PI / t->teeth / 4.0;
123 for (i = 0; i < t->teeth; i++) {
124 angle = i * 2.0 * M_PI / t->teeth;
126 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
127 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
128 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
129 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
133 glNormal3f(0.0, 0.0, -1.0);
136 glBegin(GL_QUAD_STRIP);
137 for (i = 0; i <= t->teeth; i++) {
138 angle = i * 2.0 * M_PI / t->teeth;
139 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
140 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
142 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
143 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
148 /* draw back sides of teeth */
150 da = 2.0 * M_PI / t->teeth / 4.0;
151 for (i = 0; i < t->teeth; i++) {
152 angle = i * 2.0 * M_PI / t->teeth;
154 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
155 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
156 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
157 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
161 /* draw outward faces of teeth */
162 glBegin(GL_QUAD_STRIP);
163 for (i = 0; i < t->teeth; i++) {
164 angle = i * 2.0 * M_PI / t->teeth;
166 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
167 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
168 u = r2 * cos(angle + da) - r1 * cos(angle);
169 v = r2 * sin(angle + da) - r1 * sin(angle);
170 len = sqrt(u * u + v * v);
173 glNormal3f(v, -u, 0.0);
174 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
175 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
176 glNormal3f(cos(angle), sin(angle), 0.0);
177 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
178 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
179 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
180 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
181 glNormal3f(v, -u, 0.0);
182 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
183 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
184 glNormal3f(cos(angle), sin(angle), 0.0);
187 glVertex3f(r1 * cos(0), r1 * sin(0), t->width * 0.5);
188 glVertex3f(r1 * cos(0), r1 * sin(0), -t->width * 0.5);
192 glShadeModel(GL_SMOOTH);
194 /* draw inside radius cylinder */
195 glBegin(GL_QUAD_STRIP);
196 for (i = 0; i <= t->teeth; i++) {
197 angle = i * 2.0 * M_PI / t->teeth;
198 glNormal3f(-cos(angle), -sin(angle), 0.0);
199 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
200 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
206 allocate_buffer(struct gears *gears)
209 struct rectangle allocation;
211 window_draw(gears->window);
213 gears->surface[gears->current] = window_get_surface(gears->window);
214 #ifdef HAVE_CAIRO_EGL
215 image = display_get_image_for_egl_image_surface(gears->display,
216 gears->surface[gears->current]);
217 #else /* XXX: hack to make Wayland compile, even if this example doesn't run */
218 die("gears cannot allocate buffer: it was compiled without cairo-gl\n");
221 if (!eglMakeCurrent(gears->display, NULL, NULL, gears->context))
222 die("faile to make context current\n");
224 glBindRenderbuffer(GL_RENDERBUFFER_EXT,
225 gears->color_rbo[gears->current]);
226 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER, image);
228 glBindRenderbuffer(GL_RENDERBUFFER_EXT, gears->depth_rbo);
229 window_get_child_allocation(gears->window, &allocation);
230 glRenderbufferStorage(GL_RENDERBUFFER_EXT,
232 allocation.width + 20 + 32,
233 allocation.height + 60 + 32);
237 draw_gears(struct gears *gears)
239 GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
240 struct rectangle allocation;
242 if (gears->surface[gears->current] == NULL)
243 allocate_buffer(gears);
245 glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT,
246 GL_COLOR_ATTACHMENT0_EXT,
248 gears->color_rbo[gears->current]);
250 window_get_child_allocation(gears->window, &allocation);
251 glViewport(allocation.x, allocation.y,
252 allocation.width, allocation.height);
253 glScissor(allocation.x, allocation.y,
254 allocation.width, allocation.height);
256 glEnable(GL_SCISSOR_TEST);
257 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
261 glTranslatef(0.0, 0.0, -50);
263 glRotatef(view_rotx, 1.0, 0.0, 0.0);
264 glRotatef(view_roty, 0.0, 1.0, 0.0);
265 glRotatef(view_rotz, 0.0, 0.0, 1.0);
268 glTranslatef(-3.0, -2.0, 0.0);
269 glRotatef(gears->angle, 0.0, 0.0, 1.0);
270 glCallList(gears->gear_list[0]);
274 glTranslatef(3.1, -2.0, 0.0);
275 glRotatef(-2.0 * gears->angle - 9.0, 0.0, 0.0, 1.0);
276 glCallList(gears->gear_list[1]);
280 glTranslatef(-3.1, 4.2, 0.0);
281 glRotatef(-2.0 * gears->angle - 25.0, 0.0, 0.0, 1.0);
282 glCallList(gears->gear_list[2]);
289 window_set_surface(gears->window, gears->surface[gears->current]);
290 window_flush(gears->window);
294 resize_handler(struct window *window,
295 int32_t width, int32_t height, void *data)
297 struct gears *gears = data;
299 cairo_surface_destroy(gears->surface[0]);
300 gears->surface[0] = NULL;
301 cairo_surface_destroy(gears->surface[1]);
302 gears->surface[1] = NULL;
304 /* Constrain child size to be square and at least 300x300 */
314 window_set_child_size(gears->window, width, height);
318 keyboard_focus_handler(struct window *window,
319 struct input *device, void *data)
321 struct gears *gears = data;
322 struct rectangle allocation;
324 window_get_child_allocation(gears->window, &allocation);
325 resize_handler(window, allocation.width, allocation.height, gears);
329 redraw_handler(struct window *window, void *data)
331 struct gears *gears = data;
337 frame_callback(void *data, uint32_t time)
339 struct gears *gears = data;
341 gears->current = 1 - gears->current;
343 gears->angle = (GLfloat) (time % 8192) * 360 / 8192.0;
345 window_schedule_redraw(gears->window);
346 wl_display_frame_callback(display_get_display(gears->d),
347 frame_callback, gears);
350 static struct gears *
351 gears_create(struct display *display)
353 const int width = 450, height = 500;
357 gears = malloc(sizeof *gears);
358 memset(gears, 0, sizeof *gears);
360 gears->window = window_create(display, width, height);
361 window_set_title(gears->window, "Wayland Gears");
363 gears->display = display_get_egl_display(gears->d);
364 if (gears->display == NULL)
365 die("failed to create egl display\n");
367 eglBindAPI(EGL_OPENGL_API);
369 gears->context = eglCreateContext(gears->display,
370 NULL, EGL_NO_CONTEXT, NULL);
371 if (gears->context == NULL)
372 die("failed to create context\n");
374 if (!eglMakeCurrent(gears->display, NULL, NULL, gears->context))
375 die("faile to make context current\n");
377 glGenFramebuffers(1, &gears->fbo);
378 glBindFramebuffer(GL_FRAMEBUFFER_EXT, gears->fbo);
380 glGenRenderbuffers(2, gears->color_rbo);
381 glGenRenderbuffers(1, &gears->depth_rbo);
382 glBindRenderbuffer(GL_RENDERBUFFER_EXT, gears->depth_rbo);
383 glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT,
384 GL_DEPTH_ATTACHMENT_EXT,
387 for (i = 0; i < 3; i++) {
388 gears->gear_list[i] = glGenLists(1);
389 glNewList(gears->gear_list[i], GL_COMPILE);
390 make_gear(&gear_templates[i]);
394 glEnable(GL_NORMALIZE);
396 glMatrixMode(GL_PROJECTION);
398 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 200.0);
399 glMatrixMode(GL_MODELVIEW);
401 glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
402 glEnable(GL_CULL_FACE);
403 glEnable(GL_LIGHTING);
405 glEnable(GL_DEPTH_TEST);
406 glClearColor(0, 0, 0, 0.92);
408 window_set_user_data(gears->window, gears);
409 window_set_resize_handler(gears->window, resize_handler);
410 window_set_keyboard_focus_handler(gears->window, keyboard_focus_handler);
411 window_set_redraw_handler(gears->window, redraw_handler);
414 wl_display_frame_callback(display_get_display(gears->d),
415 frame_callback, gears);
420 int main(int argc, char *argv[])
424 d = display_create(&argc, &argv, NULL);
426 fprintf(stderr, "failed to create display: %m\n");