blob: c432bc584ca3003893fe386e60c9929e0c89e677 [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
Scott Moreau7890c4d2012-04-24 11:28:26 -060039#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020040#include <wayland-client.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050041
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050042#include "window.h"
43
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050044struct gears {
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050045 struct window *window;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050046 struct widget *widget;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050047
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050048 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050049
50 EGLDisplay display;
Benjamin Franzkecff904e2011-02-18 23:00:55 +010051 EGLDisplay config;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050052 EGLContext context;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050053 GLfloat angle;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050054
Scott Moreau7890c4d2012-04-24 11:28:26 -060055 struct {
56 GLfloat rotx;
57 GLfloat roty;
58 } view;
59
60 int button_down;
61 int last_x, last_y;
62
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050063 GLint gear_list[3];
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050064};
65
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050066struct gear_template {
67 GLfloat material[4];
68 GLfloat inner_radius;
69 GLfloat outer_radius;
70 GLfloat width;
71 GLint teeth;
72 GLfloat tooth_depth;
73};
74
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -040075static const struct gear_template gear_templates[] = {
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050076 { { 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 },
79};
80
81static GLfloat light_pos[4] = {5.0, 5.0, 10.0, 0.0};
82
83static void die(const char *msg)
84{
85 fprintf(stderr, "%s", msg);
86 exit(EXIT_FAILURE);
87}
88
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050089static void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050090make_gear(const struct gear_template *t)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050091{
92 GLint i;
93 GLfloat r0, r1, r2;
94 GLfloat angle, da;
95 GLfloat u, v, len;
96
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050097 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, t->material);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050098
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050099 r0 = t->inner_radius;
100 r1 = t->outer_radius - t->tooth_depth / 2.0;
101 r2 = t->outer_radius + t->tooth_depth / 2.0;
102
103 da = 2.0 * M_PI / t->teeth / 4.0;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500104
105 glShadeModel(GL_FLAT);
106
107 glNormal3f(0.0, 0.0, 1.0);
108
109 /* draw front face */
110 glBegin(GL_QUAD_STRIP);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500111 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);
115 if (i < t->teeth) {
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);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500118 }
119 }
120 glEnd();
121
122 /* draw front sides of teeth */
123 glBegin(GL_QUADS);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500124 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;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500127
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500128 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);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500132 }
133 glEnd();
134
135 glNormal3f(0.0, 0.0, -1.0);
136
137 /* draw back face */
138 glBegin(GL_QUAD_STRIP);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500139 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);
143 if (i < t->teeth) {
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);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500146 }
147 }
148 glEnd();
149
150 /* draw back sides of teeth */
151 glBegin(GL_QUADS);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500152 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;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500155
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500156 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);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500160 }
161 glEnd();
162
163 /* draw outward faces of teeth */
164 glBegin(GL_QUAD_STRIP);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500165 for (i = 0; i < t->teeth; i++) {
166 angle = i * 2.0 * M_PI / t->teeth;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500167
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500168 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
169 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500170 u = r2 * cos(angle + da) - r1 * cos(angle);
171 v = r2 * sin(angle + da) - r1 * sin(angle);
172 len = sqrt(u * u + v * v);
173 u /= len;
174 v /= len;
175 glNormal3f(v, -u, 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500176 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);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500178 glNormal3f(cos(angle), sin(angle), 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500179 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);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500181 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);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500184 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);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500186 glNormal3f(cos(angle), sin(angle), 0.0);
187 }
188
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500189 glVertex3f(r1 * cos(0), r1 * sin(0), t->width * 0.5);
190 glVertex3f(r1 * cos(0), r1 * sin(0), -t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500191
192 glEnd();
193
194 glShadeModel(GL_SMOOTH);
195
196 /* draw inside radius cylinder */
197 glBegin(GL_QUAD_STRIP);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500198 for (i = 0; i <= t->teeth; i++) {
199 angle = i * 2.0 * M_PI / t->teeth;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500200 glNormal3f(-cos(angle), -sin(angle), 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500201 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
202 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500203 }
204 glEnd();
205}
206
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500207static void
Kristian Høgsberg5d129902012-01-10 10:49:41 -0500208frame_callback(void *data, struct wl_callback *callback, uint32_t time)
209{
210 struct gears *gears = data;
211
212 gears->angle = (GLfloat) (time % 8192) * 360 / 8192.0;
213
214 window_schedule_redraw(gears->window);
215
216 if (callback)
217 wl_callback_destroy(callback);
218}
219
220static const struct wl_callback_listener listener = {
221 frame_callback
222};
223
Scott Moreau7890c4d2012-04-24 11:28:26 -0600224static int
225motion_handler(struct widget *widget, struct input *input,
226 uint32_t time, int32_t x, int32_t y, void *data)
227{
228 struct gears *gears = data;
229 int offset_x, offset_y;
230 float step = 0.5;
231
232 if (gears->button_down) {
233 offset_x = x - gears->last_x;
234 offset_y = y - gears->last_y;
235 gears->last_x = x;
236 gears->last_y = y;
237 gears->view.roty += offset_x * step;
238 gears->view.rotx += offset_y * step;
239 if (gears->view.roty >= 360)
240 gears->view.roty = gears->view.roty - 360;
241 if (gears->view.roty <= 0)
242 gears->view.roty = gears->view.roty + 360;
243 if (gears->view.rotx >= 360)
244 gears->view.rotx = gears->view.rotx - 360;
245 if (gears->view.rotx <= 0)
246 gears->view.rotx = gears->view.rotx + 360;
247 }
248
249 return POINTER_LEFT_PTR;
250}
251
252static void
253button_handler(struct widget *widget, struct input *input,
254 uint32_t time, int button, int state, void *data)
255{
256 struct gears *gears = data;
257
258 if (button == BTN_LEFT) {
259 if (state) {
260 gears->button_down = 1;
261 input_get_position(input,
262 &gears->last_x, &gears->last_y);
263 } else {
264 gears->button_down = 0;
265 }
266 }
267}
268
Kristian Høgsberg5d129902012-01-10 10:49:41 -0500269static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500270redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500271{
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100272 struct rectangle window_allocation;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500273 struct rectangle allocation;
Kristian Høgsberg5d129902012-01-10 10:49:41 -0500274 struct wl_callback *callback;
275 struct gears *gears = data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500276
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500277 widget_get_allocation(gears->widget, &allocation);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100278 window_get_allocation(gears->window, &window_allocation);
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500279
Benjamin Franzke1a89f282011-10-07 09:33:06 +0200280 if (display_acquire_window_surface(gears->d,
281 gears->window,
282 gears->context) < 0) {
283 die("Unable to acquire window surface, "
284 "compiled without cairo-egl?\n");
285 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100286
287 glViewport(allocation.x,
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500288 window_allocation.height - allocation.height - allocation.y,
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500289 allocation.width, allocation.height);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100290 glScissor(allocation.x,
291 window_allocation.height - allocation.height - allocation.y,
292 allocation.width, allocation.height);
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500293
294 glEnable(GL_SCISSOR_TEST);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500295 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Kristian Høgsbergb8bf19b2008-11-05 07:38:46 -0500296
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500297 glPushMatrix();
298
299 glTranslatef(0.0, 0.0, -50);
300
Scott Moreau7890c4d2012-04-24 11:28:26 -0600301 glRotatef(gears->view.rotx, 1.0, 0.0, 0.0);
302 glRotatef(gears->view.roty, 0.0, 1.0, 0.0);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500303
304 glPushMatrix();
305 glTranslatef(-3.0, -2.0, 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500306 glRotatef(gears->angle, 0.0, 0.0, 1.0);
307 glCallList(gears->gear_list[0]);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500308 glPopMatrix();
309
310 glPushMatrix();
311 glTranslatef(3.1, -2.0, 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500312 glRotatef(-2.0 * gears->angle - 9.0, 0.0, 0.0, 1.0);
313 glCallList(gears->gear_list[1]);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500314 glPopMatrix();
315
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500316 glPushMatrix();
317 glTranslatef(-3.1, 4.2, 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500318 glRotatef(-2.0 * gears->angle - 25.0, 0.0, 0.0, 1.0);
319 glCallList(gears->gear_list[2]);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500320 glPopMatrix();
321
322 glPopMatrix();
323
324 glFlush();
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500325
Benjamin Franzke0c991632011-09-27 21:57:31 +0200326 display_release_window_surface(gears->d, gears->window);
Kristian Høgsberg5d129902012-01-10 10:49:41 -0500327
328 callback = wl_surface_frame(window_get_wl_surface(gears->window));
329 wl_callback_add_listener(callback, &listener, gears);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500330}
331
332static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500333resize_handler(struct widget *widget,
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500334 int32_t width, int32_t height, void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500335{
336 struct gears *gears = data;
337
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500338 /* Constrain child size to be square and at least 300x300 */
339 if (width > height)
340 height = width;
341 else
342 width = height;
343 if (width < 300) {
344 width = 300;
345 height = 300;
346 }
347
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500348 widget_set_size(gears->widget, width, height);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500349}
350
351static void
Kristian Høgsberg9ae561d2009-09-21 13:47:53 -0400352keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -0400353 struct input *device, void *data)
Kristian Høgsberg9ae561d2009-09-21 13:47:53 -0400354{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500355 window_schedule_redraw(window);
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500356}
357
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500358static struct gears *
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400359gears_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500360{
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500361 const int width = 450, height = 500;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500362 struct gears *gears;
363 int i;
364
365 gears = malloc(sizeof *gears);
366 memset(gears, 0, sizeof *gears);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500367 gears->d = display;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500368 gears->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500369 gears->widget = frame_create(gears->window, gears);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500370 window_set_title(gears->window, "Wayland Gears");
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500371
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400372 gears->display = display_get_egl_display(gears->d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500373 if (gears->display == NULL)
374 die("failed to create egl display\n");
375
Kristian Høgsbergf88ae452010-06-05 10:17:55 -0400376 eglBindAPI(EGL_OPENGL_API);
377
Kristian Høgsberg5f5324e2012-01-19 14:05:21 -0500378 gears->config = display_get_argb_egl_config(gears->d);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100379
380 gears->context = eglCreateContext(gears->display, gears->config,
381 EGL_NO_CONTEXT, NULL);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500382 if (gears->context == NULL)
383 die("failed to create context\n");
384
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500385 if (!eglMakeCurrent(gears->display, NULL, NULL, gears->context))
Tiago Vignatti0a266412012-02-09 19:06:56 +0200386 die("failed to make context current\n");
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500387
388 for (i = 0; i < 3; i++) {
389 gears->gear_list[i] = glGenLists(1);
390 glNewList(gears->gear_list[i], GL_COMPILE);
391 make_gear(&gear_templates[i]);
392 glEndList();
393 }
394
Scott Moreau7890c4d2012-04-24 11:28:26 -0600395 gears->button_down = 0;
396 gears->last_x = 0;
397 gears->last_y = 0;
398
399 gears->view.rotx = 20.0;
400 gears->view.roty = 30.0;
401
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500402 glEnable(GL_NORMALIZE);
403
404 glMatrixMode(GL_PROJECTION);
405 glLoadIdentity();
406 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 200.0);
407 glMatrixMode(GL_MODELVIEW);
408
409 glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
410 glEnable(GL_CULL_FACE);
411 glEnable(GL_LIGHTING);
412 glEnable(GL_LIGHT0);
413 glEnable(GL_DEPTH_TEST);
414 glClearColor(0, 0, 0, 0.92);
415
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400416 window_set_user_data(gears->window, gears);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500417 widget_set_resize_handler(gears->widget, resize_handler);
418 widget_set_redraw_handler(gears->widget, redraw_handler);
Scott Moreau7890c4d2012-04-24 11:28:26 -0600419 widget_set_button_handler(gears->widget, button_handler);
420 widget_set_motion_handler(gears->widget, motion_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500421 window_set_keyboard_focus_handler(gears->window,
422 keyboard_focus_handler);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400423
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500424 window_schedule_resize(gears->window, width, height);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500425
426 return gears;
427}
428
429int main(int argc, char *argv[])
430{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500431 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500432
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400433 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200434 if (d == NULL) {
435 fprintf(stderr, "failed to create display: %m\n");
436 return -1;
437 }
Kristian Høgsberg00439612011-01-25 15:16:01 -0500438 gears_create(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400439 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500440
441 return 0;
442}