blob: b5e7bd43de31798ebb4717475613a84db8ee9ce6 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002 * Copyright © 2008 Kristian Høgsberg
3 *
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.
13 *
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
20 * OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050021 */
22
Kristian Høgsberg96ce9682011-01-07 14:42:49 -050023#include "config.h"
24
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050025#include <stdint.h>
26#include <stdio.h>
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050027#include <stdlib.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050028#include <string.h>
29#include <fcntl.h>
30#include <unistd.h>
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050031#include <math.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050032#include <time.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050033#include <glib.h>
34
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050035#include <GL/gl.h>
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -050036#include <EGL/egl.h>
37#include <EGL/eglext.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050038
Pekka Paalanen50719bc2011-11-22 14:18:50 +020039#include <wayland-client.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050040
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050041#include "window.h"
42
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050043struct gears {
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050044 struct window *window;
45
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050046 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050047
48 EGLDisplay display;
Benjamin Franzkecff904e2011-02-18 23:00:55 +010049 EGLDisplay config;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050050 EGLContext context;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050051 GLfloat angle;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050052
53 GLint gear_list[3];
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050054};
55
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050056struct gear_template {
57 GLfloat material[4];
58 GLfloat inner_radius;
59 GLfloat outer_radius;
60 GLfloat width;
61 GLint teeth;
62 GLfloat tooth_depth;
63};
64
65const static struct gear_template gear_templates[] = {
66 { { 0.8, 0.1, 0.0, 1.0 }, 1.0, 4.0, 1.0, 20, 0.7 },
67 { { 0.0, 0.8, 0.2, 1.0 }, 0.5, 2.0, 2.0, 10, 0.7 },
68 { { 0.2, 0.2, 1.0, 1.0 }, 1.3, 2.0, 0.5, 10, 0.7 },
69};
70
71static GLfloat light_pos[4] = {5.0, 5.0, 10.0, 0.0};
72
73static void die(const char *msg)
74{
75 fprintf(stderr, "%s", msg);
76 exit(EXIT_FAILURE);
77}
78
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050079static void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050080make_gear(const struct gear_template *t)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050081{
82 GLint i;
83 GLfloat r0, r1, r2;
84 GLfloat angle, da;
85 GLfloat u, v, len;
86
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050087 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, t->material);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050088
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050089 r0 = t->inner_radius;
90 r1 = t->outer_radius - t->tooth_depth / 2.0;
91 r2 = t->outer_radius + t->tooth_depth / 2.0;
92
93 da = 2.0 * M_PI / t->teeth / 4.0;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050094
95 glShadeModel(GL_FLAT);
96
97 glNormal3f(0.0, 0.0, 1.0);
98
99 /* draw front face */
100 glBegin(GL_QUAD_STRIP);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500101 for (i = 0; i <= t->teeth; i++) {
102 angle = i * 2.0 * M_PI / t->teeth;
103 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
104 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
105 if (i < t->teeth) {
106 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
107 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500108 }
109 }
110 glEnd();
111
112 /* draw front sides of teeth */
113 glBegin(GL_QUADS);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500114 da = 2.0 * M_PI / t->teeth / 4.0;
115 for (i = 0; i < t->teeth; i++) {
116 angle = i * 2.0 * M_PI / t->teeth;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500117
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500118 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
119 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
120 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
121 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500122 }
123 glEnd();
124
125 glNormal3f(0.0, 0.0, -1.0);
126
127 /* draw back face */
128 glBegin(GL_QUAD_STRIP);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500129 for (i = 0; i <= t->teeth; i++) {
130 angle = i * 2.0 * M_PI / t->teeth;
131 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
132 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
133 if (i < t->teeth) {
134 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
135 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500136 }
137 }
138 glEnd();
139
140 /* draw back sides of teeth */
141 glBegin(GL_QUADS);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500142 da = 2.0 * M_PI / t->teeth / 4.0;
143 for (i = 0; i < t->teeth; i++) {
144 angle = i * 2.0 * M_PI / t->teeth;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500145
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500146 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
147 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
148 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
149 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500150 }
151 glEnd();
152
153 /* draw outward faces of teeth */
154 glBegin(GL_QUAD_STRIP);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500155 for (i = 0; i < t->teeth; i++) {
156 angle = i * 2.0 * M_PI / t->teeth;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500157
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500158 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
159 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500160 u = r2 * cos(angle + da) - r1 * cos(angle);
161 v = r2 * sin(angle + da) - r1 * sin(angle);
162 len = sqrt(u * u + v * v);
163 u /= len;
164 v /= len;
165 glNormal3f(v, -u, 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500166 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
167 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500168 glNormal3f(cos(angle), sin(angle), 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500169 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
170 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500171 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
172 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
173 glNormal3f(v, -u, 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500174 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
175 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500176 glNormal3f(cos(angle), sin(angle), 0.0);
177 }
178
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500179 glVertex3f(r1 * cos(0), r1 * sin(0), t->width * 0.5);
180 glVertex3f(r1 * cos(0), r1 * sin(0), -t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500181
182 glEnd();
183
184 glShadeModel(GL_SMOOTH);
185
186 /* draw inside radius cylinder */
187 glBegin(GL_QUAD_STRIP);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500188 for (i = 0; i <= t->teeth; i++) {
189 angle = i * 2.0 * M_PI / t->teeth;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500190 glNormal3f(-cos(angle), -sin(angle), 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500191 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
192 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500193 }
194 glEnd();
195}
196
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500197static void
Kristian Høgsberg5d129902012-01-10 10:49:41 -0500198frame_callback(void *data, struct wl_callback *callback, uint32_t time)
199{
200 struct gears *gears = data;
201
202 gears->angle = (GLfloat) (time % 8192) * 360 / 8192.0;
203
204 window_schedule_redraw(gears->window);
205
206 if (callback)
207 wl_callback_destroy(callback);
208}
209
210static const struct wl_callback_listener listener = {
211 frame_callback
212};
213
214static void
215redraw_handler(struct window *window, void *data)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500216{
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100217 GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
218 struct rectangle window_allocation;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500219 struct rectangle allocation;
Kristian Høgsberg5d129902012-01-10 10:49:41 -0500220 struct wl_callback *callback;
221 struct gears *gears = data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500222
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500223 window_get_child_allocation(gears->window, &allocation);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100224 window_get_allocation(gears->window, &window_allocation);
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500225
Benjamin Franzke1a89f282011-10-07 09:33:06 +0200226 if (display_acquire_window_surface(gears->d,
227 gears->window,
228 gears->context) < 0) {
229 die("Unable to acquire window surface, "
230 "compiled without cairo-egl?\n");
231 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100232
233 glViewport(allocation.x,
234 window_allocation.height - allocation.height - allocation.x,
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500235 allocation.width, allocation.height);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100236 glScissor(allocation.x,
237 window_allocation.height - allocation.height - allocation.y,
238 allocation.width, allocation.height);
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500239
240 glEnable(GL_SCISSOR_TEST);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500241 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Kristian Høgsbergb8bf19b2008-11-05 07:38:46 -0500242
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500243 glPushMatrix();
244
245 glTranslatef(0.0, 0.0, -50);
246
247 glRotatef(view_rotx, 1.0, 0.0, 0.0);
248 glRotatef(view_roty, 0.0, 1.0, 0.0);
249 glRotatef(view_rotz, 0.0, 0.0, 1.0);
250
251 glPushMatrix();
252 glTranslatef(-3.0, -2.0, 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500253 glRotatef(gears->angle, 0.0, 0.0, 1.0);
254 glCallList(gears->gear_list[0]);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500255 glPopMatrix();
256
257 glPushMatrix();
258 glTranslatef(3.1, -2.0, 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500259 glRotatef(-2.0 * gears->angle - 9.0, 0.0, 0.0, 1.0);
260 glCallList(gears->gear_list[1]);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500261 glPopMatrix();
262
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500263 glPushMatrix();
264 glTranslatef(-3.1, 4.2, 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500265 glRotatef(-2.0 * gears->angle - 25.0, 0.0, 0.0, 1.0);
266 glCallList(gears->gear_list[2]);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500267 glPopMatrix();
268
269 glPopMatrix();
270
271 glFlush();
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500272
Benjamin Franzke0c991632011-09-27 21:57:31 +0200273 display_release_window_surface(gears->d, gears->window);
Kristian Høgsberg5d129902012-01-10 10:49:41 -0500274
275 callback = wl_surface_frame(window_get_wl_surface(gears->window));
276 wl_callback_add_listener(callback, &listener, gears);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500277}
278
279static void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500280resize_handler(struct window *window,
281 int32_t width, int32_t height, void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500282{
283 struct gears *gears = data;
284
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500285 /* Constrain child size to be square and at least 300x300 */
286 if (width > height)
287 height = width;
288 else
289 width = height;
290 if (width < 300) {
291 width = 300;
292 height = 300;
293 }
294
295 window_set_child_size(gears->window, width, height);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500296}
297
298static void
Kristian Høgsberg9ae561d2009-09-21 13:47:53 -0400299keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -0400300 struct input *device, void *data)
Kristian Høgsberg9ae561d2009-09-21 13:47:53 -0400301{
302 struct gears *gears = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500303 struct rectangle allocation;
Kristian Høgsberg9ae561d2009-09-21 13:47:53 -0400304
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500305 window_get_child_allocation(gears->window, &allocation);
306 resize_handler(window, allocation.width, allocation.height, gears);
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500307}
308
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500309static struct gears *
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400310gears_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500311{
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500312 const int width = 450, height = 500;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500313 struct gears *gears;
314 int i;
315
316 gears = malloc(sizeof *gears);
317 memset(gears, 0, sizeof *gears);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500318 gears->d = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500319 gears->window = window_create(display, width, height);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200320 window_set_transparent(gears->window, 0);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500321 window_set_title(gears->window, "Wayland Gears");
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500322
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400323 gears->display = display_get_egl_display(gears->d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500324 if (gears->display == NULL)
325 die("failed to create egl display\n");
326
Kristian Høgsbergf88ae452010-06-05 10:17:55 -0400327 eglBindAPI(EGL_OPENGL_API);
328
Benjamin Franzke0c991632011-09-27 21:57:31 +0200329 gears->config = display_get_rgb_egl_config(gears->d);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100330
331 gears->context = eglCreateContext(gears->display, gears->config,
332 EGL_NO_CONTEXT, NULL);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500333 if (gears->context == NULL)
334 die("failed to create context\n");
335
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500336 if (!eglMakeCurrent(gears->display, NULL, NULL, gears->context))
337 die("faile to make context current\n");
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500338
339 for (i = 0; i < 3; i++) {
340 gears->gear_list[i] = glGenLists(1);
341 glNewList(gears->gear_list[i], GL_COMPILE);
342 make_gear(&gear_templates[i]);
343 glEndList();
344 }
345
346 glEnable(GL_NORMALIZE);
347
348 glMatrixMode(GL_PROJECTION);
349 glLoadIdentity();
350 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 200.0);
351 glMatrixMode(GL_MODELVIEW);
352
353 glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
354 glEnable(GL_CULL_FACE);
355 glEnable(GL_LIGHTING);
356 glEnable(GL_LIGHT0);
357 glEnable(GL_DEPTH_TEST);
358 glClearColor(0, 0, 0, 0.92);
359
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400360 window_set_user_data(gears->window, gears);
361 window_set_resize_handler(gears->window, resize_handler);
Kristian Høgsberg5d129902012-01-10 10:49:41 -0500362 window_set_redraw_handler(gears->window, redraw_handler);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400363 window_set_keyboard_focus_handler(gears->window, keyboard_focus_handler);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400364
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400365 frame_callback(gears, NULL, 0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500366
367 return gears;
368}
369
370int main(int argc, char *argv[])
371{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500372 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500373
Kristian Høgsberg9de79a92011-08-24 11:09:53 -0400374 d = display_create(&argc, &argv, NULL);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200375 if (d == NULL) {
376 fprintf(stderr, "failed to create display: %m\n");
377 return -1;
378 }
Kristian Høgsberg00439612011-01-25 15:16:01 -0500379 gears_create(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400380 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500381
382 return 0;
383}