home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <string.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <math.h>
- #include <stdlib.h>
- #include "tk.h"
-
- #if defined(__MWERKS__) || defined(__SC__)
- #include <console.h> /* ccommand */
- #endif
-
- GLenum doubleBuffer, directRender;
-
- char *texFileName = 0;
- TK_RGBImageRec *image;
-
- float *minFilter, *magFilter, *sWrapMode, *tWrapMode;
- float decal[] = {GL_REPLACE};
- float modulate[] = {GL_MODULATE};
- float repeat[] = {GL_REPEAT};
- float clamp[] = {GL_CLAMP};
- float nr[] = {GL_NEAREST};
- float ln[] = {GL_LINEAR};
- float nr_mipmap_nr[] = {GL_NEAREST_MIPMAP_NEAREST};
- float nr_mipmap_ln[] = {GL_NEAREST_MIPMAP_LINEAR};
- float ln_mipmap_nr[] = {GL_LINEAR_MIPMAP_NEAREST};
- float ln_mipmap_ln[] = {GL_LINEAR_MIPMAP_LINEAR};
- GLint sphereMap[] = {GL_SPHERE_MAP};
-
- GLenum doSphere = GL_FALSE;
- GLboolean autoRotation = GL_FALSE;
- GLboolean fillrate_timing = GL_FALSE;
- float xRotation = 0.0, yRotation = 0.0, zTranslate = -3.125;
-
- GLint w_win = 800;
- GLint h_win = 600;
-
- GLint cube;
-
- float c[6][4][3] = {
- {
- {
- 1.0, 1.0, -1.0
- },
- {
- -1.0, 1.0, -1.0
- },
- {
- -1.0, -1.0, -1.0
- },
- {
- 1.0, -1.0, -1.0
- }
- },
- {
- {
- 1.0, 1.0, 1.0
- },
- {
- 1.0, 1.0, -1.0
- },
- {
- 1.0, -1.0, -1.0
- },
- {
- 1.0, -1.0, 1.0
- }
- },
- {
- {
- -1.0, 1.0, 1.0
- },
- {
- 1.0, 1.0, 1.0
- },
- {
- 1.0, -1.0, 1.0
- },
- {
- -1.0, -1.0, 1.0
- }
- },
- {
- {
- -1.0, 1.0, -1.0
- },
- {
- -1.0, 1.0, 1.0
- },
- {
- -1.0, -1.0, 1.0
- },
- {
- -1.0, -1.0, -1.0
- }
- },
- {
- {
- -1.0, 1.0, 1.0
- },
- {
- -1.0, 1.0, -1.0
- },
- {
- 1.0, 1.0, -1.0
- },
- {
- 1.0, 1.0, 1.0
- }
- },
- {
- {
- -1.0, -1.0, -1.0
- },
- {
- -1.0, -1.0, 1.0
- },
- {
- 1.0, -1.0, 1.0
- },
- {
- 1.0, -1.0, -1.0
- }
- }
- };
- static float n[6][3] = {
- {
- 0.0, 0.0, -1.0
- },
- {
- 1.0, 0.0, 0.0
- },
- {
- 0.0, 0.0, 1.0
- },
- {
- -1.0, 0.0, 0.0
- },
- {
- 0.0, 1.0, 0.0
- },
- {
- 0.0, -1.0, 0.0
- }
- };
-
- static float t[6][4][2] = {
- {
- {
- 1.1, 1.1
- },
- {
- -0.1, 1.1
- },
- {
- -0.1, -0.1
- },
- {
- 1.1, -0.1
- }
- },
- {
- {
- 1.1, 1.1
- },
- {
- -0.1, 1.1
- },
- {
- -0.1, -0.1
- },
- {
- 1.1, -0.1
- }
- },
- {
- {
- -0.1, 1.1
- },
- {
- 1.1, 1.1
- },
- {
- 1.1, -0.1
- },
- {
- -0.1, -0.1
- }
- },
- {
- {
- 1.1, 1.1
- },
- {
- -0.1, 1.1
- },
- {
- -0.1, -0.1
- },
- {
- 1.1, -0.1
- }
- },
- {
- {
- 1.1, 1.1
- },
- {
- -0.1, 1.1
- },
- {
- -0.1, -0.1
- },
- {
- 1.1, -0.1
- }
- },
- {
- {
- 1.1, 1.1
- },
- {
- -0.1, 1.1
- },
- {
- -0.1, -0.1
- },
- {
- 1.1, -0.1
- }
- },
- };
-
- static double filter(double in, double *save)
- {
- static double k1 = 0.9;
- static double k2 = 0.05;
-
- save[3] = in;
- save[1] = save[0]*k1 + k2*(save[3] + save[2]);
-
- save[0]=save[1];
- save[2]=save[3];
-
- return(save[1]);
- }
-
-
- static void DrawStr(char *str)
- {
- static GLuint bitmapBase = 0;
-
- if(bitmapBase == 0)
- {
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- bitmapBase = glGenLists(256);
- aglUseFont(aglGetCurrentContext(), kFontIDNewYork, normal, 10, 0, 256, bitmapBase);
- }
-
- glListBase(bitmapBase);
- glCallLists(strlen(str), GL_UNSIGNED_BYTE, (GLubyte *) str);
- }
-
- static void BuildCube(void)
- {
- GLint i;
-
- glNewList(cube, GL_COMPILE);
- for (i = 0; i < 6; i++) {
- glBegin(GL_POLYGON);
- glNormal3fv(n[i]); glTexCoord2fv(t[i][0]); glVertex3fv(c[i][0]);
- glNormal3fv(n[i]); glTexCoord2fv(t[i][1]); glVertex3fv(c[i][1]);
- glNormal3fv(n[i]); glTexCoord2fv(t[i][2]); glVertex3fv(c[i][2]);
- glNormal3fv(n[i]); glTexCoord2fv(t[i][3]); glVertex3fv(c[i][3]);
- glEnd();
- }
- glEndList();
- }
-
- static void BuildLists(void)
- {
- cube = glGenLists(1);
- BuildCube();
- }
-
- static void Init(void)
- {
- GLfloat bcolor[4] = {1.0, 1.0, 1.0, 1.0};
-
- magFilter = nr;
- minFilter = nr;
- sWrapMode = repeat;
- tWrapMode = repeat;
-
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-
- glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, sWrapMode);
- glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tWrapMode);
- glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
- glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
-
- glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, decal);
- glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, bcolor);
-
- gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image->sizeX, image->sizeY,
- GL_RGB, GL_UNSIGNED_BYTE, image->data);
-
- glEnable(GL_TEXTURE_2D);
-
- glFrontFace(GL_CCW);
- glCullFace(GL_FRONT);
- glEnable(GL_CULL_FACE);
-
- glShadeModel(GL_FLAT);
- glDisable(GL_DITHER);
-
- BuildLists();
-
- glClearColor(0.0, 0.0, 0.0, 0.0);
- }
-
- static void Reshape(int width, int height)
- {
-
- glViewport(0, 0, (GLint)width, (GLint)height);
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60.0, 1.0, 0.01, 1000);
- //gluPerspective(120.0, 1.0, 0.01, 1000);
- //glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
- glMatrixMode(GL_MODELVIEW);
-
- w_win = width;
- h_win = height;
- }
-
- static GLenum Key(int key, GLenum mask)
- {
-
- switch (key) {
- case TK_ESCAPE:
- tkQuit();
- case TK_a:
- autoRotation = !autoRotation;
- break;
- case TK_LEFT:
- yRotation -= 5.0;
- break;
- case TK_RIGHT:
- yRotation += 5.0;
- break;
- case TK_UP:
- xRotation -= 5.0;
- break;
- case TK_DOWN:
- xRotation += 5.0;
- break;
- case TK_T:
- zTranslate += 0.25;
- break;
- case TK_t:
- zTranslate -= 0.25;
- break;
- case TK_r:
- sWrapMode = repeat;
- tWrapMode = repeat;
- break;
- case TK_c:
- sWrapMode = clamp;
- tWrapMode = clamp;
- break;
- case TK_s:
- doSphere = !doSphere;
- if (doSphere) {
- glTexGeniv(GL_S, GL_TEXTURE_GEN_MODE, sphereMap);
- glTexGeniv(GL_T, GL_TEXTURE_GEN_MODE, sphereMap);
- glEnable(GL_TEXTURE_GEN_S);
- glEnable(GL_TEXTURE_GEN_T);
- } else {
- glDisable(GL_TEXTURE_GEN_S);
- glDisable(GL_TEXTURE_GEN_T);
- }
- break;
- case TK_f:
- fillrate_timing = !fillrate_timing;
- break;
-
- case TK_0:
- magFilter = nr;
- break;
- case TK_1:
- magFilter = ln;
- break;
- case TK_2:
- minFilter = nr;
- break;
- case TK_3:
- minFilter = ln;
- break;
- case TK_4:
- minFilter = nr_mipmap_nr;
- break;
- case TK_5:
- minFilter = nr_mipmap_ln;
- break;
- case TK_6:
- minFilter = ln_mipmap_nr;
- break;
- case TK_7:
- minFilter = ln_mipmap_ln;
- break;
-
- default:
- return GL_FALSE;
- }
- return GL_TRUE;
- }
-
- static void Draw(void)
- {
- int i;
- static double t1 = 0.0, t2 = 0.0, t;
- static double th[4] = {0.0, 0.0, 0.0, 0.0};
- char num_str[200];
-
- t1 = t2;
-
- glClear(GL_COLOR_BUFFER_BIT);
-
- glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, sWrapMode);
- glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tWrapMode);
- glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
- glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
-
- glPushMatrix();
-
- glTranslatef(0.0, 0.0, zTranslate);
- glRotatef(xRotation, 1, 0, 0);
- glRotatef(yRotation, 0, 1, 0);
-
- if(fillrate_timing)
- {
- for(i = 0; i < 100; i++)
- glCallList(cube);
- }
- else
- {
- glCallList(cube);
- }
-
- glPopMatrix();
-
- t2 = tkNow();
- t = t2 - t1;
- if(t > 0.0001) t = 1.0 / t;
-
- glColor3f(1.0, 0.0, 0.0);
-
- glMatrixMode (GL_PROJECTION);
- glPushMatrix();
- glLoadIdentity();
- glOrtho(0, w_win, 0, h_win, -10.0, 10.0);
-
- glDisable(GL_TEXTURE_2D);
-
- glRasterPos2f(5.0, 5.0);
-
- if(fillrate_timing)
- sprintf(num_str, "%0.2f Pixels/Sec, %dx%d", t * 100.0 * w_win * h_win, w_win, h_win);
- else
- sprintf(num_str, "%0.2f Hz, %dx%d", filter(t, th), w_win, h_win);
-
- DrawStr(num_str);
-
- glPopMatrix();
- glMatrixMode(GL_MODELVIEW);
-
- glEnable(GL_TEXTURE_2D);
-
- tkSwapBuffers();
- }
-
- static void Idle(void)
- {
- if(autoRotation)
- {
- yRotation += 5.0;
- if(yRotation < -180.0) yRotation += 360.0;
- else if(yRotation > 180.0) yRotation -= 360.0;
-
- xRotation -= 5.0;
- if(xRotation < -180.0) xRotation += 360.0;
- else if(xRotation > 180.0) xRotation -= 360.0;
- }
- }
-
- static GLenum Args(int argc, char **argv)
- {
- GLint i;
-
- doubleBuffer = GL_TRUE;
- directRender = GL_TRUE;
-
- for (i = 1; i < argc; i++) {
- if (strcmp(argv[i], "-sb") == 0) {
- doubleBuffer = GL_FALSE;
- } else if (strcmp(argv[i], "-db") == 0) {
- doubleBuffer = GL_TRUE;
- } else if (strcmp(argv[i], "-dr") == 0) {
- directRender = GL_TRUE;
- } else if (strcmp(argv[i], "-ir") == 0) {
- directRender = GL_FALSE;
- } else if (strcmp(argv[i], "-f") == 0) {
- if (i+1 >= argc || argv[i+1][0] == '-') {
- printf("-f (No file name).\n");
- return GL_FALSE;
- } else {
- texFileName = argv[++i];
- }
- } else {
- printf("%s (Bad option).\n", argv[i]);
- return GL_FALSE;
- }
- }
- return GL_TRUE;
- }
-
- void main(int argc, char **argv)
- {
- GLenum type;
-
- #if defined(__MWERKS__) || defined(__SC__)
- argc = ccommand(&argv);
- #endif
-
- if (Args(argc, argv) == GL_FALSE) {
- tkQuit();
- }
-
- if (texFileName == 0) {
- printf("No image file.\n");
- tkQuit();
- }
-
- image = tkRGBImageLoad(texFileName);
-
- tkInitPosition(30, 60, w_win, h_win);
-
- type = TK_RGB;
- type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
- type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
- tkInitDisplayMode(type);
-
- if (tkInitWindow("Texture Test") == GL_FALSE) {
- tkQuit();
- }
-
- Init();
-
- tkExposeFunc(Reshape);
- tkReshapeFunc(Reshape);
- tkIdleFunc(Idle);
- tkKeyDownFunc(Key);
- tkDisplayFunc(Draw);
- tkExec();
- }
-