txfdemo.c
上传用户:xk288cn
上传日期:2007-05-28
资源大小:4876k
文件大小:9k
- /* Copyright (c) Mark J. Kilgard, 1997. */
- /* This program is freely distributable without licensing fees and is
- provided without guarantee or warrantee expressed or implied. This
- program is -not- in the public domain. */
- /* X compile line: cc -o txfdemo txfdemo.c -lglut -lGLU -lGL -lXmu -lXext -lX11 -lm */
- #include <math.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <GL/glut.h>
- #include "TexFont.h"
- /* Uncomment to debug various scenarios. */
- #if 0
- #undef GL_VERSION_1_1
- #undef GL_EXT_polygon_offset
- #endif
- #ifndef GL_VERSION_1_1
- #ifdef GL_EXT_polygon_offset
- #define GL_POLYGON_OFFSET_FILL GL_POLYGON_OFFSET_EXT
- #define glPolygonOffset(s,b) glPolygonOffsetEXT(s,b*0.001);
- #else
- /* Gag. No polygon offset? Artifacts will exist. */
- #define glPolygonOffset(s,b) /* nothing */
- #endif
- #endif
- #ifndef M_PI
- #define M_PI 3.14159265358979323846
- #endif
- static int doubleBuffer = 1;
- static char *filename = "rockfont.txf";
- static GLfloat angle = 20;
- static TexFont *txf;
- static int usePolygonOffset = 1;
- static int animation = 1;
- static int fullscreen = 0;
- void
- idle(void)
- {
- angle += 4;
- glutPostRedisplay();
- }
- void
- visible(int vis)
- {
- if (vis == GLUT_VISIBLE) {
- if (animation) {
- glutIdleFunc(idle);
- }
- } else {
- glutIdleFunc(NULL);
- }
- }
- void reshape(int w, int h)
- {
- glViewport(0, 0, (GLsizei) w, (GLsizei) h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
- glMatrixMode(GL_MODELVIEW);
- }
- void
- cubeSide(void)
- {
- glDisable(GL_TEXTURE_2D);
- glDisable(GL_BLEND);
- glDisable(GL_ALPHA_TEST);
- glColor3f(0.3, 0.7, 0.3);
- glRectf(-1.0, -1.0, 1.0, 1.0);
- }
- int alphaMode;
- void
- alphaModeSet(void)
- {
- switch (alphaMode) {
- case GL_ALPHA_TEST:
- glDisable(GL_BLEND);
- glEnable(GL_ALPHA_TEST);
- glAlphaFunc(GL_GEQUAL, 0.5);
- break;
- case GL_BLEND:
- glDisable(GL_ALPHA_TEST);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- break;
- case GL_ALPHA_TEST + GL_BLEND:
- glEnable(GL_ALPHA_TEST);
- glAlphaFunc(GL_GEQUAL, 0.0625);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- break;
- case GL_NONE:
- glDisable(GL_ALPHA_TEST);
- glDisable(GL_BLEND);
- break;
- }
- }
- void
- cubeSideWithOpenGLcircle(void)
- {
- int w, ow, a, d;
- char *text;
- int len;
- int i;
- GLfloat flen;
- cubeSide();
- glPushMatrix();
- alphaModeSet();
- glEnable(GL_TEXTURE_2D);
- if (usePolygonOffset) {
- #if defined(GL_EXT_polygon_offset) || defined(GL_VERSION_1_1)
- glEnable(GL_POLYGON_OFFSET_FILL);
- glPolygonOffset(0.0, -3);
- #endif
- }
- glColor3f(0.2, 0.2, 0.9);
- txfGetStringMetrics(txf, "OpenGL", 6, &w, &a, &d);
- text = "OpenGL OpenGL ";
- len = (int) strlen(text);
- txfGetStringMetrics(txf, text, len, &w, &a, &d);
- txfGetStringMetrics(txf, "O", 1, &ow, &a, &d);
- glScalef(5.6/w, 5.6/w, 5.6/w);
- flen = len;
- glTranslatef(-ow/2.0, -w/(M_PI*2.0), 0.0);
- for (i=0; i<len; i++) {
- if (text[i] == 'L' && usePolygonOffset) {
- /* Hack. The "L" in OpenGL slightly overlaps the "G". Slightly
- raise the "L" so that it will overlap the "G" in the depth
- buffer to avoid a double blend.. */
- glPolygonOffset(0.0, -4);
- txfRenderGlyph(txf, text[i]);
- glPolygonOffset(0.0, -3);
- } else {
- txfRenderGlyph(txf, text[i]);
- }
- glRotatef(360.0/flen, 0, 0, 1);
- }
- if (usePolygonOffset) {
- #if defined(GL_EXT_polygon_offset) || defined(GL_VERSION_1_1)
- glDisable(GL_POLYGON_OFFSET_FILL);
- #endif
- }
- glPopMatrix();
- }
- void
- cubeSideWithText(char *text, int len)
- {
- int w, a, d;
- cubeSide();
- glPushMatrix();
- glEnable(GL_TEXTURE_2D);
- alphaModeSet();
- if (usePolygonOffset) {
- #if defined(GL_EXT_polygon_offset) || defined(GL_VERSION_1_1)
- glEnable(GL_POLYGON_OFFSET_FILL);
- glPolygonOffset(0.0, -3);
- #endif
- }
- glColor3f(0.2, 0.2, 0.9);
- txfGetStringMetrics(txf, text, len, &w, &a, &d);
- glScalef(1.8/w, 1.8/w, 1.8/w);
- glTranslatef(-w/2.0, d-(a+d)/2.0, 0.0);
- txfRenderFancyString(txf, text, len);
- if (usePolygonOffset) {
- #if defined(GL_EXT_polygon_offset) || defined(GL_VERSION_1_1)
- glDisable(GL_POLYGON_OFFSET_FILL);
- #endif
- }
- glPopMatrix();
- }
- void
- display(void)
- {
- char *str;
- /* Clear the color buffer. */
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- glRotatef(-angle, 0, 1, 0);
- glPushMatrix();
- glTranslatef(0.0, 0.0, 1.0);
- cubeSideWithOpenGLcircle();
- glPopMatrix();
- glPushMatrix();
- glRotatef(90.0, 0, 1, 0);
- glTranslatef(0.0, 0.0, 1.0);
- str = "MAkes";
- cubeSideWithText(str, (int) strlen(str));
- glPopMatrix();
- glPushMatrix();
- glRotatef(180.0, 0, 1, 0);
- glTranslatef(0.0, 0.0, 1.0);
- str = "Text";
- cubeSideWithText(str, (int) strlen(str));
- glPopMatrix();
- glPushMatrix();
- glRotatef(270.0, 0, 1, 0);
- glTranslatef(0.0, 0.0, 1.0);
- str = "