quads.c revision 32001f49
1/** 2 * Draw colored quads. 3 */ 4 5 6#include <stdio.h> 7#include <stdlib.h> 8#include <math.h> 9#include <GL/glew.h> 10#include "glut_wrap.h" 11 12#define NUM_QUADS 20 13 14 15static int Win; 16static GLfloat Xrot = 40, Yrot = 0, Zrot = 0; 17static GLboolean Anim = GL_TRUE; 18static GLuint Vbuffer = 0; 19 20#if 1 21#else 22static GLfloat buf[NUM_QUADS * 6 * 4]; 23#endif 24 25static GLboolean doSwapBuffers = GL_TRUE; 26 27static GLint Frames = 0, T0 = 0; 28 29 30static void 31Idle(void) 32{ 33 Xrot += 3.0; 34 Yrot += 4.0; 35 Zrot += 2.0; 36 glutPostRedisplay(); 37} 38 39 40static void 41Draw(void) 42{ 43 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 44 45 glPushMatrix(); 46 glRotatef(Xrot, 1, 0, 0); 47 glRotatef(Yrot, 0, 1, 0); 48 glRotatef(Zrot, 0, 0, 1); 49 50 glDrawArrays(GL_QUADS, 0, NUM_QUADS*4); 51 52 glPopMatrix(); 53 54 if (doSwapBuffers) 55 glutSwapBuffers(); 56 /* 57 else 58 glFinish(); 59 */ 60 61 { 62 GLint t = glutGet(GLUT_ELAPSED_TIME); 63 Frames++; 64 if (t - T0 >= 5000) { 65 GLfloat seconds = (t - T0) / 1000.0; 66 GLfloat fps = Frames / seconds; 67 printf("%d frames in %6.3f seconds = %6.3f FPS\n", 68 Frames, seconds, fps); 69 T0 = t; 70 Frames = 0; 71 } 72 } 73} 74 75 76static void 77Reshape(int width, int height) 78{ 79 glViewport(0, 0, width, height); 80 glMatrixMode(GL_PROJECTION); 81 glLoadIdentity(); 82 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); 83 glMatrixMode(GL_MODELVIEW); 84 glLoadIdentity(); 85 glTranslatef(0.0, 0.0, -8.0); 86} 87 88 89static void 90Key(unsigned char key, int x, int y) 91{ 92 const GLfloat step = 3.0; 93 (void) x; 94 (void) y; 95 switch (key) { 96 case 's': 97 doSwapBuffers = !doSwapBuffers; 98 break; 99 case 'a': 100 Anim = !Anim; 101 if (Anim) 102 glutIdleFunc(Idle); 103 else 104 glutIdleFunc(NULL); 105 break; 106 case 'z': 107 Zrot -= step; 108 break; 109 case 'Z': 110 Zrot += step; 111 break; 112 case 27: 113 glutDestroyWindow(Win); 114 exit(0); 115 break; 116 } 117 glutPostRedisplay(); 118} 119 120 121static void 122SpecialKey(int key, int x, int y) 123{ 124 const GLfloat step = 3.0; 125 (void) x; 126 (void) y; 127 switch (key) { 128 case GLUT_KEY_UP: 129 Xrot -= step; 130 break; 131 case GLUT_KEY_DOWN: 132 Xrot += step; 133 break; 134 case GLUT_KEY_LEFT: 135 Yrot -= step; 136 break; 137 case GLUT_KEY_RIGHT: 138 Yrot += step; 139 break; 140 } 141 glutPostRedisplay(); 142} 143 144 145static void 146quad(float x, float y, float z, float *v) 147{ 148 int k = 0; 149 150 /* color */ 151 v[k++] = x * 0.5 + 0.5; 152 v[k++] = y * 0.5 + 0.5; 153 v[k++] = z * 0.5 + 0.5; 154 /* vert */ 155 v[k++] = x; 156 v[k++] = y; 157 v[k++] = z; 158 159 /* color */ 160 v[k++] = -x * 0.5 + 0.5; 161 v[k++] = -y * 0.5 + 0.5; 162 v[k++] = z * 0.5 + 0.5; 163 /* vert */ 164 v[k++] = -x; 165 v[k++] = -y; 166 v[k++] = z; 167 168 /* color */ 169 v[k++] = -x * 0.5 + 0.5; 170 v[k++] = -y * 0.5 + 0.5; 171 v[k++] = -z * 0.5 + 0.5; 172 /* vert */ 173 v[k++] = -x; 174 v[k++] = -y; 175 v[k++] = -z; 176 177 /* color */ 178 v[k++] = x * 0.5 + 0.5; 179 v[k++] = y * 0.5 + 0.5; 180 v[k++] = -z * 0.5 + 0.5; 181 /* vert */ 182 v[k++] = x; 183 v[k++] = y; 184 v[k++] = -z; 185} 186 187static void 188gen_quads(GLfloat *buf) 189{ 190 float *v = buf; 191 float r = 1.0; 192 int i; 193 194 for (i = 0; i < NUM_QUADS; i++) { 195 float angle = i / (float) NUM_QUADS * M_PI; 196 float x = r * cos(angle); 197 float y = r * sin(angle); 198 float z = 1.10; 199 quad(x, y, z, v); 200 v += 24; 201 } 202 203 if (0) { 204 float *p = buf; 205 for (i = 0; i < NUM_QUADS * 4 * 2; i++) { 206 printf("%d: %f %f %f\n", i, p[0], p[1], p[2]); 207 p += 3; 208 } 209 } 210} 211 212 213static void 214Init(void) 215{ 216 int bytes = NUM_QUADS * 4 * 2 * 3 * sizeof(float); 217 GLfloat *f; 218 219#if 1 220 glGenBuffers(1, &Vbuffer); 221 glBindBuffer(GL_ARRAY_BUFFER, Vbuffer); 222 glBufferData(GL_ARRAY_BUFFER_ARB, bytes, NULL, GL_STATIC_DRAW_ARB); 223 f = (float *) glMapBuffer(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB); 224 gen_quads(f); 225 glUnmapBuffer(GL_ARRAY_BUFFER_ARB); 226 glColorPointer(3, GL_FLOAT, 6*sizeof(float), (void *) 0); 227 glVertexPointer(3, GL_FLOAT, 6*sizeof(float), (void *) 12); 228#else 229 f = buf; 230 gen_quads(f); 231 glColorPointer(3, GL_FLOAT, 6*sizeof(float), buf); 232 glVertexPointer(3, GL_FLOAT, 6*sizeof(float), buf + 3); 233#endif 234 235 glEnableClientState(GL_COLOR_ARRAY); 236 glEnableClientState(GL_VERTEX_ARRAY); 237 238 glEnable(GL_DEPTH_TEST); 239 240 glClearColor(0.5, 0.5, 0.5, 0.0); 241} 242 243 244int 245main(int argc, char *argv[]) 246{ 247 glutInit(&argc, argv); 248 glutInitWindowPosition(0, 0); 249 glutInitWindowSize(600, 600); 250 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); 251 Win = glutCreateWindow(argv[0]); 252 glewInit(); 253 glutReshapeFunc(Reshape); 254 glutKeyboardFunc(Key); 255 glutSpecialFunc(SpecialKey); 256 glutDisplayFunc(Draw); 257 if (Anim) 258 glutIdleFunc(Idle); 259 Init(); 260 glutMainLoop(); 261 return 0; 262} 263