aboutsummaryrefslogtreecommitdiff
path: root/examples/cube.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/cube.c')
-rw-r--r--examples/cube.c112
1 files changed, 112 insertions, 0 deletions
diff --git a/examples/cube.c b/examples/cube.c
new file mode 100644
index 0000000..ec47b13
--- /dev/null
+++ b/examples/cube.c
@@ -0,0 +1,112 @@
+/*
+ * mesacube - THE spinning cube
+ *
+ * Build:
+ * cc -O2 -I$HOME/mesa/include -I$HOME -o cube cube.c \
+ * $HOME/mesa/lib/libGL.a -lm
+ *
+ * Run: VERITE_UCODE=$HOME/v2000gl.uc \
+ * ./cube [seconds] (0 = spin until ^C)
+ */
+#include <sys/time.h>
+#include <math.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <GL/gl.h>
+#include <GL/vrmesa.h>
+
+static volatile sig_atomic_t stop;
+
+static void
+onint(int sig)
+{
+
+ (void)sig;
+ stop = 1;
+}
+
+static const GLfloat faces[6][4][3] = {
+ { {-1,-1,-1},{-1, 1,-1},{ 1, 1,-1},{ 1,-1,-1} }, /* -Z */
+ { {-1,-1, 1},{ 1,-1, 1},{ 1, 1, 1},{-1, 1, 1} }, /* +Z */
+ { {-1,-1,-1},{-1,-1, 1},{-1, 1, 1},{-1, 1,-1} }, /* -X */
+ { { 1,-1,-1},{ 1, 1,-1},{ 1, 1, 1},{ 1,-1, 1} }, /* +X */
+ { {-1,-1,-1},{ 1,-1,-1},{ 1,-1, 1},{-1,-1, 1} }, /* -Y */
+ { {-1, 1,-1},{-1, 1, 1},{ 1, 1, 1},{ 1, 1,-1} }, /* +Y */
+};
+
+int
+main(int argc, char **argv)
+{
+ vrMesaContext ctx;
+ struct timeval t0, t1, tstart;
+ float a = 20.0f, b = 30.0f;
+ double secs = 30.0;
+ int frame = 0, f, i;
+
+ if (argc > 1)
+ secs = atof(argv[1]);
+ signal(SIGINT, onint);
+
+ ctx = vrMesaCreateContext(NULL);
+ if (ctx == NULL)
+ return 1;
+ vrMesaMakeCurrent(ctx);
+ printf("GL_RENDERER: %s (%s)\n", glGetString(GL_RENDERER),
+ glGetString(GL_VERSION));
+
+ glEnable(GL_DEPTH_TEST);
+ glEnable(GL_CULL_FACE);
+ glClearColor(0.06f, 0.07f, 0.12f, 1);
+ glViewport(0, 0, 640, 480);
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glFrustum(-1.0, 1.0, -0.75, 0.75, 1.0, 50.0);
+
+ gettimeofday(&tstart, NULL);
+ t0 = tstart;
+ while (!stop) {
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+ glTranslatef(0, 0, -4.5f);
+ glRotatef(a, 1, 0, 0);
+ glRotatef(b, 0, 1, 0);
+
+ glBegin(GL_QUADS);
+ for (f = 0; f < 6; f++)
+ for (i = 0; i < 4; i++) {
+ const GLfloat *v = faces[f][i];
+
+ /* corner color = position in RGB space */
+ glColor3f((v[0] + 1) * 0.5f,
+ (v[1] + 1) * 0.5f, (v[2] + 1) * 0.5f);
+ glVertex3fv(v);
+ }
+ glEnd();
+
+ vrMesaSwapBuffers();
+ a += 1.1f;
+ b += 1.7f;
+ if (++frame % 100 == 0) {
+ double dt;
+
+ gettimeofday(&t1, NULL);
+ dt = (t1.tv_sec - t0.tv_sec) +
+ (t1.tv_usec - t0.tv_usec) / 1e6;
+ printf("%d frames, %.1f fps\n", frame, 100.0 / dt);
+ t0 = t1;
+ }
+ if (secs > 0) {
+ gettimeofday(&t1, NULL);
+ if ((t1.tv_sec - tstart.tv_sec) +
+ (t1.tv_usec - tstart.tv_usec) / 1e6 >= secs)
+ break;
+ }
+ }
+
+ printf("mesacube done after %d frames\n", frame);
+ vrMesaDestroyContext(ctx);
+ return 0;
+}