132001f49Smrg/**
232001f49Smrg * Draw colored quads.
332001f49Smrg */
432001f49Smrg
532001f49Smrg
632001f49Smrg#include <stdio.h>
732001f49Smrg#include <stdlib.h>
832001f49Smrg#include <math.h>
932001f49Smrg#include <GL/glew.h>
1032001f49Smrg#include "glut_wrap.h"
1132001f49Smrg
1232001f49Smrg#define NUM_QUADS 20
1332001f49Smrg
1432001f49Smrg
1532001f49Smrgstatic int Win;
1632001f49Smrgstatic GLfloat Xrot = 40, Yrot = 0, Zrot = 0;
1732001f49Smrgstatic GLboolean Anim = GL_TRUE;
1832001f49Smrgstatic GLuint Vbuffer = 0;
1932001f49Smrg
2032001f49Smrg#if 1
2132001f49Smrg#else
2232001f49Smrgstatic GLfloat buf[NUM_QUADS * 6 * 4];
2332001f49Smrg#endif
2432001f49Smrg
2532001f49Smrgstatic GLboolean doSwapBuffers = GL_TRUE;
2632001f49Smrg
2732001f49Smrgstatic GLint Frames = 0, T0 = 0;
2832001f49Smrg
2932001f49Smrg
3032001f49Smrgstatic void
3132001f49SmrgIdle(void)
3232001f49Smrg{
3332001f49Smrg   Xrot += 3.0;
3432001f49Smrg   Yrot += 4.0;
3532001f49Smrg   Zrot += 2.0;
3632001f49Smrg   glutPostRedisplay();
3732001f49Smrg}
3832001f49Smrg
3932001f49Smrg
4032001f49Smrgstatic void
4132001f49SmrgDraw(void)
4232001f49Smrg{
4332001f49Smrg   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
4432001f49Smrg
4532001f49Smrg   glPushMatrix();
4632001f49Smrg   glRotatef(Xrot, 1, 0, 0);
4732001f49Smrg   glRotatef(Yrot, 0, 1, 0);
4832001f49Smrg   glRotatef(Zrot, 0, 0, 1);
4932001f49Smrg
5032001f49Smrg   glDrawArrays(GL_QUADS, 0, NUM_QUADS*4);
5132001f49Smrg
5232001f49Smrg   glPopMatrix();
5332001f49Smrg
5432001f49Smrg   if (doSwapBuffers)
5532001f49Smrg      glutSwapBuffers();
5632001f49Smrg   /*
5732001f49Smrg   else
5832001f49Smrg      glFinish();
5932001f49Smrg   */
6032001f49Smrg
6132001f49Smrg   {
6232001f49Smrg      GLint t = glutGet(GLUT_ELAPSED_TIME);
6332001f49Smrg      Frames++;
6432001f49Smrg      if (t - T0 >= 5000) {
6532001f49Smrg         GLfloat seconds = (t - T0) / 1000.0;
6632001f49Smrg         GLfloat fps = Frames / seconds;
6732001f49Smrg         printf("%d frames in %6.3f seconds = %6.3f FPS\n",
6832001f49Smrg                Frames, seconds, fps);
6932001f49Smrg         T0 = t;
7032001f49Smrg         Frames = 0;
7132001f49Smrg      }
7232001f49Smrg   }
7332001f49Smrg}
7432001f49Smrg
7532001f49Smrg
7632001f49Smrgstatic void
7732001f49SmrgReshape(int width, int height)
7832001f49Smrg{
7932001f49Smrg   glViewport(0, 0, width, height);
8032001f49Smrg   glMatrixMode(GL_PROJECTION);
8132001f49Smrg   glLoadIdentity();
8232001f49Smrg   glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
8332001f49Smrg   glMatrixMode(GL_MODELVIEW);
8432001f49Smrg   glLoadIdentity();
8532001f49Smrg   glTranslatef(0.0, 0.0, -8.0);
8632001f49Smrg}
8732001f49Smrg
8832001f49Smrg
8932001f49Smrgstatic void
9032001f49SmrgKey(unsigned char key, int x, int y)
9132001f49Smrg{
9232001f49Smrg   const GLfloat step = 3.0;
9332001f49Smrg   (void) x;
9432001f49Smrg   (void) y;
9532001f49Smrg   switch (key) {
9632001f49Smrg      case 's':
9732001f49Smrg         doSwapBuffers = !doSwapBuffers;
9832001f49Smrg         break;
9932001f49Smrg      case 'a':
10032001f49Smrg         Anim = !Anim;
10132001f49Smrg         if (Anim)
10232001f49Smrg            glutIdleFunc(Idle);
10332001f49Smrg         else
10432001f49Smrg            glutIdleFunc(NULL);
10532001f49Smrg         break;
10632001f49Smrg      case 'z':
10732001f49Smrg         Zrot -= step;
10832001f49Smrg         break;
10932001f49Smrg      case 'Z':
11032001f49Smrg         Zrot += step;
11132001f49Smrg         break;
11232001f49Smrg      case 27:
11332001f49Smrg         glutDestroyWindow(Win);
11432001f49Smrg         exit(0);
11532001f49Smrg         break;
11632001f49Smrg   }
11732001f49Smrg   glutPostRedisplay();
11832001f49Smrg}
11932001f49Smrg
12032001f49Smrg
12132001f49Smrgstatic void
12232001f49SmrgSpecialKey(int key, int x, int y)
12332001f49Smrg{
12432001f49Smrg   const GLfloat step = 3.0;
12532001f49Smrg   (void) x;
12632001f49Smrg   (void) y;
12732001f49Smrg   switch (key) {
12832001f49Smrg      case GLUT_KEY_UP:
12932001f49Smrg         Xrot -= step;
13032001f49Smrg         break;
13132001f49Smrg      case GLUT_KEY_DOWN:
13232001f49Smrg         Xrot += step;
13332001f49Smrg         break;
13432001f49Smrg      case GLUT_KEY_LEFT:
13532001f49Smrg         Yrot -= step;
13632001f49Smrg         break;
13732001f49Smrg      case GLUT_KEY_RIGHT:
13832001f49Smrg         Yrot += step;
13932001f49Smrg         break;
14032001f49Smrg   }
14132001f49Smrg   glutPostRedisplay();
14232001f49Smrg}
14332001f49Smrg
14432001f49Smrg
14532001f49Smrgstatic void
14632001f49Smrgquad(float x, float y, float z, float *v)
14732001f49Smrg{
14832001f49Smrg   int k = 0;
14932001f49Smrg
15032001f49Smrg   /* color */
15132001f49Smrg   v[k++] = x * 0.5 + 0.5;
15232001f49Smrg   v[k++] = y * 0.5 + 0.5;
15332001f49Smrg   v[k++] = z * 0.5 + 0.5;
15432001f49Smrg   /* vert */
15532001f49Smrg   v[k++] = x;
15632001f49Smrg   v[k++] = y;
15732001f49Smrg   v[k++] = z;
15832001f49Smrg
15932001f49Smrg   /* color */
16032001f49Smrg   v[k++] = -x * 0.5 + 0.5;
16132001f49Smrg   v[k++] = -y * 0.5 + 0.5;
16232001f49Smrg   v[k++] = z * 0.5 + 0.5;
16332001f49Smrg   /* vert */
16432001f49Smrg   v[k++] = -x;
16532001f49Smrg   v[k++] = -y;
16632001f49Smrg   v[k++] = z;
16732001f49Smrg
16832001f49Smrg   /* color */
16932001f49Smrg   v[k++] = -x * 0.5 + 0.5;
17032001f49Smrg   v[k++] = -y * 0.5 + 0.5;
17132001f49Smrg   v[k++] = -z * 0.5 + 0.5;
17232001f49Smrg   /* vert */
17332001f49Smrg   v[k++] = -x;
17432001f49Smrg   v[k++] = -y;
17532001f49Smrg   v[k++] = -z;
17632001f49Smrg
17732001f49Smrg   /* color */
17832001f49Smrg   v[k++] = x * 0.5 + 0.5;
17932001f49Smrg   v[k++] = y * 0.5 + 0.5;
18032001f49Smrg   v[k++] = -z * 0.5 + 0.5;
18132001f49Smrg   /* vert */
18232001f49Smrg   v[k++] = x;
18332001f49Smrg   v[k++] = y;
18432001f49Smrg   v[k++] = -z;
18532001f49Smrg}
18632001f49Smrg
18732001f49Smrgstatic void
18832001f49Smrggen_quads(GLfloat *buf)
18932001f49Smrg{
19032001f49Smrg   float *v = buf;
19132001f49Smrg   float r = 1.0;
19232001f49Smrg   int i;
19332001f49Smrg
19432001f49Smrg   for (i = 0; i < NUM_QUADS; i++) {
19532001f49Smrg      float angle = i / (float) NUM_QUADS * M_PI;
19632001f49Smrg      float x = r * cos(angle);
19732001f49Smrg      float y = r * sin(angle);
19832001f49Smrg      float z = 1.10;
19932001f49Smrg      quad(x, y, z, v);
20032001f49Smrg      v += 24;
20132001f49Smrg   }
20232001f49Smrg
20332001f49Smrg   if (0) {
20432001f49Smrg      float *p = buf;
20532001f49Smrg      for (i = 0; i < NUM_QUADS * 4 * 2; i++) {
20632001f49Smrg         printf("%d: %f %f %f\n", i, p[0], p[1], p[2]);
20732001f49Smrg         p += 3;
20832001f49Smrg      }
20932001f49Smrg   }
21032001f49Smrg}
21132001f49Smrg
21232001f49Smrg
21332001f49Smrgstatic void
21432001f49SmrgInit(void)
21532001f49Smrg{
21632001f49Smrg   int bytes = NUM_QUADS * 4 * 2 * 3 * sizeof(float);
21732001f49Smrg   GLfloat *f;
21832001f49Smrg
21932001f49Smrg#if 1
22032001f49Smrg   glGenBuffers(1, &Vbuffer);
22132001f49Smrg   glBindBuffer(GL_ARRAY_BUFFER, Vbuffer);
22232001f49Smrg   glBufferData(GL_ARRAY_BUFFER_ARB, bytes, NULL, GL_STATIC_DRAW_ARB);
22332001f49Smrg   f = (float *) glMapBuffer(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB);
22432001f49Smrg   gen_quads(f);
22532001f49Smrg   glUnmapBuffer(GL_ARRAY_BUFFER_ARB);
22632001f49Smrg   glColorPointer(3, GL_FLOAT, 6*sizeof(float), (void *) 0);
22732001f49Smrg   glVertexPointer(3, GL_FLOAT, 6*sizeof(float), (void *) 12);
22832001f49Smrg#else
22932001f49Smrg   f = buf;
23032001f49Smrg   gen_quads(f);
23132001f49Smrg   glColorPointer(3, GL_FLOAT, 6*sizeof(float), buf);
23232001f49Smrg   glVertexPointer(3, GL_FLOAT, 6*sizeof(float), buf + 3);
23332001f49Smrg#endif
23432001f49Smrg
23532001f49Smrg   glEnableClientState(GL_COLOR_ARRAY);
23632001f49Smrg   glEnableClientState(GL_VERTEX_ARRAY);
23732001f49Smrg
23832001f49Smrg   glEnable(GL_DEPTH_TEST);
23932001f49Smrg
24032001f49Smrg   glClearColor(0.5, 0.5, 0.5, 0.0);
24132001f49Smrg}
24232001f49Smrg
24332001f49Smrg
24432001f49Smrgint
24532001f49Smrgmain(int argc, char *argv[])
24632001f49Smrg{
24732001f49Smrg   glutInit(&argc, argv);
24832001f49Smrg   glutInitWindowPosition(0, 0);
24932001f49Smrg   glutInitWindowSize(600, 600);
25032001f49Smrg   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
25132001f49Smrg   Win = glutCreateWindow(argv[0]);
25232001f49Smrg   glewInit();
25332001f49Smrg   glutReshapeFunc(Reshape);
25432001f49Smrg   glutKeyboardFunc(Key);
25532001f49Smrg   glutSpecialFunc(SpecialKey);
25632001f49Smrg   glutDisplayFunc(Draw);
25732001f49Smrg   if (Anim)
25832001f49Smrg      glutIdleFunc(Idle);
25932001f49Smrg   Init();
26032001f49Smrg   glutMainLoop();
26132001f49Smrg   return 0;
26232001f49Smrg}
263