tri-tex-1d.c revision 32001f49
1/*
2 * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that (i) the above copyright notices and this permission notice appear in
7 * all copies of the software and related documentation, and (ii) the name of
8 * Silicon Graphics may not be used in any advertising or
9 * publicity relating to the software without the specific, prior written
10 * permission of Silicon Graphics.
11 *
12 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
13 * ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25#include <stdio.h>
26#include <string.h>
27#include <stdlib.h>
28#include <GL/glew.h>
29#include "glut_wrap.h"
30
31static GLenum Target = GL_TEXTURE_1D;
32static GLenum Filter = GL_NEAREST;
33GLenum doubleBuffer;
34static float Rot = 0;
35static int win = 0;
36
37static void Init(void)
38{
39   fprintf(stderr, "GL_RENDERER   = %s\n", (char *) glGetString(GL_RENDERER));
40   fprintf(stderr, "GL_VERSION    = %s\n", (char *) glGetString(GL_VERSION));
41   fprintf(stderr, "GL_VENDOR     = %s\n", (char *) glGetString(GL_VENDOR));
42   fflush(stderr);
43
44   glClearColor(0.0, 0.0, 1.0, 0.0);
45
46#define SIZE 256
47   {
48      GLubyte tex1d[SIZE][4];
49      GLint s;
50
51      for (s = 0; s < SIZE; s++) {
52         tex1d[s][0] = 255-s;
53         tex1d[s][1] = s;
54         tex1d[s][2] = 0;
55         tex1d[s][3] = 255;
56      }
57
58      glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
59
60      glTexImage1D(Target, 0, 4, SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex1d);
61
62      glEnable(Target);
63      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
64      glTexParameterf(Target, GL_TEXTURE_WRAP_R, GL_REPEAT);
65
66      glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
67   }
68}
69
70static void Reshape(int width, int height)
71{
72   glViewport(0, 0, (GLint)width, (GLint)height);
73   glMatrixMode(GL_PROJECTION);
74   glLoadIdentity();
75#if 0
76   glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
77#else
78   glFrustum(-1, 1, -1, 1, 10, 20);
79#endif
80   glMatrixMode(GL_MODELVIEW);
81   glLoadIdentity();
82   glTranslatef(0, 0, -15);
83}
84
85static void Key(unsigned char key, int x, int y)
86{
87   switch (key) {
88   case 'r':
89      Rot += 10.0;
90      break;
91   case 'R':
92      Rot -= 10.0;
93      break;
94   case 'f':
95      if (Filter == GL_NEAREST)
96         Filter = GL_LINEAR;
97      else
98         Filter = GL_NEAREST;
99      break;
100   case 27:
101      glutDestroyWindow(win);
102      exit(0);
103   default:
104      return;
105   }
106   glutPostRedisplay();
107}
108
109static void Draw(void)
110{
111   glTexParameterf(Target, GL_TEXTURE_MIN_FILTER, Filter);
112   glTexParameterf(Target, GL_TEXTURE_MAG_FILTER, Filter);
113
114   glClear(GL_COLOR_BUFFER_BIT);
115
116   glPushMatrix();
117   glRotatef(Rot, 0, 1, 0);
118
119   glBegin(GL_QUADS);
120   glTexCoord2f(1,0);
121   glVertex3f( 0.9, -0.9, 0.0);
122   glTexCoord2f(1,1);
123   glVertex3f( 0.9,  0.9, 0.0);
124   glTexCoord2f(0,1);
125   glVertex3f(-0.9,  0.9, 0.0);
126   glTexCoord2f(0,0);
127   glVertex3f(-0.9,  -0.9, 0.0);
128   glEnd();
129
130   glPopMatrix();
131
132   glFlush();
133
134   if (doubleBuffer) {
135      glutSwapBuffers();
136   }
137}
138
139static GLenum Args(int argc, char **argv)
140{
141    GLint i;
142
143    doubleBuffer = GL_FALSE;
144
145    for (i = 1; i < argc; i++) {
146        if (strcmp(argv[i], "-sb") == 0) {
147	    doubleBuffer = GL_FALSE;
148	} else if (strcmp(argv[i], "-db") == 0) {
149	    doubleBuffer = GL_TRUE;
150	} else {
151	    fprintf(stderr, "%s (Bad option).\n", argv[i]);
152	    return GL_FALSE;
153	}
154    }
155    return GL_TRUE;
156}
157
158int main(int argc, char **argv)
159{
160    GLenum type;
161
162    glutInit(&argc, argv);
163
164    if (Args(argc, argv) == GL_FALSE) {
165	exit(1);
166    }
167
168    glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250);
169
170    type = GLUT_RGB;
171    type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
172    glutInitDisplayMode(type);
173
174    win = glutCreateWindow(*argv);
175    if (!win) {
176	exit(1);
177    }
178
179    Init();
180
181    glutReshapeFunc(Reshape);
182    glutKeyboardFunc(Key);
183    glutDisplayFunc(Draw);
184    glutMainLoop();
185    return 0;
186}
187