aboutsummaryrefslogtreecommitdiff
path: root/examples/cube.c
blob: ec47b1351753830ac7e205abad447bfe34c6c34e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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;
}