53#define _USE_MATH_DEFINES
63#include <pedigree/log.h>
71static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
72static GLint gear1, gear2, gear3;
73static GLfloat angle = 0.0;
75static unsigned int frames = 0;
76static unsigned int start_time = 0;
81 gettimeofday(&now, NULL);
85 start_time = now.tv_sec;
87 else if ((now.tv_sec - start_time) >= 5)
89 GLfloat seconds = now.tv_sec - start_time;
90 GLfloat fps = frames / seconds;
91 pedigree_log(LOG_INFO,
"%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds, fps);
92 start_time = now.tv_sec;
98gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
99 GLint teeth, GLfloat tooth_depth)
107 r1 = outer_radius - tooth_depth / 2.0;
108 r2 = outer_radius + tooth_depth / 2.0;
110 da = 2.0 * M_PI / teeth / 4.0;
112 glShadeModel(GL_FLAT);
114 glNormal3f(0.0, 0.0, 1.0);
117 glBegin(GL_QUAD_STRIP);
118 for (i = 0; i <= teeth; i++) {
119 angle = i * 2.0 * M_PI / teeth;
120 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
121 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
123 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
124 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
132 da = 2.0 * M_PI / teeth / 4.0;
133 for (i = 0; i < teeth; i++) {
134 angle = i * 2.0 * M_PI / teeth;
136 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
137 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
138 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
140 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
145 glNormal3f(0.0, 0.0, -1.0);
148 glBegin(GL_QUAD_STRIP);
149 for (i = 0; i <= teeth; i++) {
150 angle = i * 2.0 * M_PI / teeth;
151 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
152 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
154 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
156 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
163 da = 2.0 * M_PI / teeth / 4.0;
164 for (i = 0; i < teeth; i++) {
165 angle = i * 2.0 * M_PI / teeth;
167 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
169 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
171 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
172 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
177 glBegin(GL_QUAD_STRIP);
178 for (i = 0; i < teeth; i++) {
179 angle = i * 2.0 * M_PI / teeth;
181 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
182 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
183 u = r2 * cos(angle + da) - r1 * cos(angle);
184 v = r2 * sin(angle + da) - r1 * sin(angle);
185 len = sqrt(u * u + v * v);
188 glNormal3f(v, -u, 0.0);
189 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
190 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
191 glNormal3f(cos(angle), sin(angle), 0.0);
192 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
194 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
196 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
197 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
198 glNormal3f(v, -u, 0.0);
199 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
201 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
203 glNormal3f(cos(angle), sin(angle), 0.0);
206 glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
207 glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
211 glShadeModel(GL_SMOOTH);
214 glBegin(GL_QUAD_STRIP);
215 for (i = 0; i <= teeth; i++) {
216 angle = i * 2.0 * M_PI / teeth;
217 glNormal3f(-cos(angle), -sin(angle), 0.0);
218 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
219 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
228 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
231 glRotatef(view_rotx, 1.0, 0.0, 0.0);
232 glRotatef(view_roty, 0.0, 1.0, 0.0);
233 glRotatef(view_rotz, 0.0, 0.0, 1.0);
236 glTranslatef(-3.0, -2.0, 0.0);
237 glRotatef(angle, 0.0, 0.0, 1.0);
242 glTranslatef(3.1, -2.0, 0.0);
243 glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
248 glTranslatef(-3.1, 4.2, 0.0);
249 glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
260reshape(
int width,
int height)
262 GLfloat h = (GLfloat) height / (GLfloat) width;
264 glViewport(0, 0, (GLint) width, (GLint) height);
265 glMatrixMode(GL_PROJECTION);
267 glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
268 glMatrixMode(GL_MODELVIEW);
270 glTranslatef(0.0, 0.0, -40.0);
278 static GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 };
279 static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
280 static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
281 static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
283 glLightfv(GL_LIGHT0, GL_POSITION, pos);
284 glEnable(GL_CULL_FACE);
285 glEnable(GL_LIGHTING);
287 glEnable(GL_DEPTH_TEST);
290 gear1 = glGenLists(1);
291 glNewList(gear1, GL_COMPILE);
292 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
293 gear(1.0, 4.0, 1.0, 20, 0.7);
296 gear2 = glGenLists(1);
297 glNewList(gear2, GL_COMPILE);
298 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
299 gear(0.5, 2.0, 2.0, 10, 0.7);
302 gear3 = glGenLists(1);
303 glNewList(gear3, GL_COMPILE);
304 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
305 gear(1.3, 2.0, 0.5, 10, 0.7);
308 glEnable(GL_NORMALIZE);
313 explicit Gears(std::uint32_t background)
314 : m_Context(OSMesaCreateContext(OSMESA_BGRA, NULL)),
315 m_Background(libui::unpackColor(background)) {}
319 OSMesaDestroyContext(m_Context);
324 return m_Context != NULL;
327 bool render(libui::client::PaintContext& paint) {
328 cairo_surface_t* surface = cairo_get_target(paint.cairo());
329 if (!surface || cairo_surface_get_type(surface) != CAIRO_SURFACE_TYPE_IMAGE ||
330 cairo_image_surface_get_format(surface) != CAIRO_FORMAT_ARGB32) {
331 fprintf(stderr,
"gears requires an ARGB32 image surface.\n");
335 const int width = cairo_image_surface_get_width(surface);
336 const int height = cairo_image_surface_get_height(surface);
337 const int stride = cairo_image_surface_get_stride(surface);
338 if (width <= 0 || height <= 0 || stride <= 0 ||
339 (stride %
static_cast<int>(
sizeof(std::uint32_t))) != 0) {
340 fprintf(stderr,
"gears received an invalid image surface.\n");
344 cairo_surface_flush(surface);
345 unsigned char* framebuffer = cairo_image_surface_get_data(surface);
347 !OSMesaMakeCurrent(m_Context, framebuffer, GL_UNSIGNED_BYTE, width, height)) {
348 fprintf(stderr,
"OSMesaMakeCurrent failed.\n");
353 OSMesaPixelStore(OSMESA_ROW_LENGTH, stride /
static_cast<int>(
sizeof(std::uint32_t)));
354 OSMesaPixelStore(OSMESA_Y_UP, 0);
356 if (width != m_Width || height != m_Height) {
357 reshape(width, height);
361 if (!m_Initialized) {
362 glClearColor(
static_cast<GLfloat
>(m_Background.r) / 255.0F,
363 static_cast<GLfloat
>(m_Background.g) / 255.0F,
364 static_cast<GLfloat
>(m_Background.b) / 255.0F,
365 static_cast<GLfloat
>(m_Background.a) / 255.0F);
367 m_Initialized =
true;
373 cairo_surface_mark_dirty(surface);
377 bool keyDown(
int keycode) {
401 OSMesaContext m_Context;
402 libui::Color m_Background;
403 bool m_Initialized =
false;
408int main(
int argc,
char** argv) {
409 const std::optional<demo::Options>
options = demo::parseOptions(argc, argv);
414 libui::client::Client client = libui::client::Client::connect(
options->socketPath);
415 if (!client.valid()) {
416 fprintf(stderr,
"gears could not connect to compositor.\n");
420 std::optional<libui::client::Window> createResult = demo::createWindow(client, *
options);
422 fprintf(stderr,
"gears could not create a window.\n");
427 if (!gears.valid()) {
428 fprintf(stderr,
"gears could not create an OSMesa context.\n");
432 libui::client::Window& window = createResult.value();
434 bool renderFailed =
false;
435 window.setWindowProc([&gears, &closed, &renderFailed](libui::client::Window& window,
436 const libui::client::Event& event) {
437 if (event.type() == libui::client::Event::Type::Close) {
441 if (event.type() == libui::client::Event::Type::Key) {
442 const libui::client::KeyEvent* key = event.get<libui::client::KeyEvent>();
443 return key && key->isDown && gears.keyDown(key->keycode);
445 if (event.type() == libui::client::Event::Type::Paint) {
446 libui::client::PaintContext* paint = window.back();
447 if (!paint || !gears.render(*paint)) {
456 if (!client.setNonBlocking(
true)) {
460 constexpr int MaxEventsPerFrame = 64;
461 while (!closed && client.valid()) {
462 for (
int count = 0; count < MaxEventsPerFrame && !closed; ++count) {
463 if (!client.dispatch(window,
false,
false)) {
468 if (!closed && !client.paint(window)) {
476 return (renderFailed || (!closed && !client.valid())) ? 1 : 0;