5 static gdouble rotate = 0;
7 gboolean on_spin(GtkWidget *spin)
9 /* Spin the cube when the toggle button is pressed */
10 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spin)))
12 gtk_widget_queue_draw(gtk_widget_get_toplevel(spin));
16 gboolean on_draw(GtkWidget *draw)
19 gtk_widget_get_allocation(draw, &alloc);
21 /* Start OpenGL Commands */
25 glViewport(0, 0, alloc.width, alloc.height);
27 glMatrixMode(GL_PROJECTION);
29 gluPerspective(45, (gdouble)alloc.width/alloc.height, 0.01, 10);
31 glMatrixMode(GL_MODELVIEW);
33 glTranslated(0, 0, -3);
34 glRotated(rotate+20, 1, 0, 0);
35 glRotated(rotate+30, 0, 1, 0);
38 glClearColor(0, 0, 0.15, 0);
39 glClear(GL_COLOR_BUFFER_BIT);
48 /* Done with OpenGL */
54 int main(int argc, char **argv)
56 /* Library initialization */
57 gtk_init(&argc, &argv);
58 glutInit(&argc, argv);
61 GtkWidget *win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
62 GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
63 GtkWidget *draw = gtk_drawing_area_new();
64 GtkWidget *spin = gtk_toggle_button_new_with_label("Spin");
66 /* Pack everything together */
67 gtk_container_add(GTK_CONTAINER(win), box);
68 gtk_box_pack_start(GTK_BOX(box), draw, TRUE, TRUE, 0);
69 gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
72 g_signal_connect(win, "destroy", gtk_main_quit, NULL);
73 g_signal_connect(draw, "draw", G_CALLBACK(on_draw), NULL);
74 g_timeout_add(1000/60, (GSourceFunc)on_spin, spin);
76 /* Enable OpenGL on drawing frame */
79 /* Show and run main loop */
80 gtk_widget_show_all(win);