blob: aabbb9edce9ed6423bffa9e0f606a654e3f96eb5 [file] [log] [blame]
Jakob Bornecrantz87bcb322009-03-18 17:15:40 +01001/*
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 <GL/glut.h>
30
31
32#define CI_OFFSET_1 16
33#define CI_OFFSET_2 32
34
35#define SIZE 16
36GLenum doubleBuffer;
37GLint line = 0;
38
39static void MakeImage()
40{
41 GLubyte tex2d[SIZE][SIZE][3];
42 GLint s, t;
43
44 for (s = 0; s < SIZE; s++) {
45 for (t = 0; t < SIZE; t++) {
Jakob Bornecrantz3f170302009-03-18 17:24:40 +010046 tex2d[t][s][0] = s*255/(SIZE-1);
47 tex2d[t][s][1] = t*255/(SIZE-1);
48 tex2d[t][s][2] = 0*255/(SIZE-1);
Jakob Bornecrantz87bcb322009-03-18 17:15:40 +010049 }
50 }
51
52 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
53 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
Jakob Bornecrantz3f170302009-03-18 17:24:40 +010054 SIZE, SIZE,
55 0,
56 GL_RGB, GL_UNSIGNED_BYTE, tex2d);
Jakob Bornecrantz87bcb322009-03-18 17:15:40 +010057 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
58 glEnable(GL_TEXTURE_2D);
59}
60
61static void UpdateLine()
62{
63 GLubyte tex[SIZE][3];
Jakob Bornecrantz3f170302009-03-18 17:24:40 +010064 GLubyte b = 0;
Jakob Bornecrantz87bcb322009-03-18 17:15:40 +010065 GLint s, t;
66
67 t = line % SIZE;
Jakob Bornecrantz3f170302009-03-18 17:24:40 +010068 if (line % (SIZE * 2) < SIZE)
69 b = 255;
70 else
71 b = 0;
Jakob Bornecrantz87bcb322009-03-18 17:15:40 +010072
73 for (s = 0; s < SIZE; s++) {
74 tex[s][0] = s*255/(SIZE-1);
75 tex[s][1] = t*255/(SIZE-1);
Jakob Bornecrantz3f170302009-03-18 17:24:40 +010076 tex[s][2] = b;
Jakob Bornecrantz87bcb322009-03-18 17:15:40 +010077 }
78
79 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
80 glTexSubImage2D(GL_TEXTURE_2D, 0,
81 0, t,
82 SIZE, 1,
83 GL_RGB, GL_UNSIGNED_BYTE, tex);
84 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
85}
86
87static void Init(void)
88{
89 fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
90 fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
91 fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
92
93 glClearColor(0.0, 0.0, 1.0, 0.0);
94
95 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
96 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
97 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
98 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
99 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
100
101 MakeImage();
102 UpdateLine();
103 line++;
104}
105
106static void Reshape(int width, int height)
107{
108
109 glViewport(0, 0, (GLint)width, (GLint)height);
110
111 glMatrixMode(GL_PROJECTION);
112 glLoadIdentity();
113 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
114 glMatrixMode(GL_MODELVIEW);
115}
116
117static void Key(unsigned char key, int x, int y)
118{
119
120 switch (key) {
121 case 27:
122 exit(1);
123 default:
124 UpdateLine();
125 line++;
126 break;
127 }
128
129 glutPostRedisplay();
130}
131
132static void Draw(void)
133{
134 glClear(GL_COLOR_BUFFER_BIT);
135
136 glBegin(GL_QUADS);
137 glTexCoord2f(1,0);
138 glVertex3f( 0.9, -0.9, -30.0);
139 glTexCoord2f(1,1);
140 glVertex3f( 0.9, 0.9, -30.0);
141 glTexCoord2f(0,1);
142 glVertex3f(-0.9, 0.9, -30.0);
143 glTexCoord2f(0,0);
144 glVertex3f(-0.9, -0.9, -30.0);
145 glEnd();
146
147 glFlush();
148
149 if (doubleBuffer) {
150 glutSwapBuffers();
151 }
152}
153
154static GLenum Args(int argc, char **argv)
155{
156 GLint i;
157
158 doubleBuffer = GL_FALSE;
159
160 for (i = 1; i < argc; i++) {
161 if (strcmp(argv[i], "-sb") == 0) {
162 doubleBuffer = GL_FALSE;
163 } else if (strcmp(argv[i], "-db") == 0) {
164 doubleBuffer = GL_TRUE;
165 } else {
166 fprintf(stderr, "%s (Bad option).\n", argv[i]);
167 return GL_FALSE;
168 }
169 }
170 return GL_TRUE;
171}
172
173int main(int argc, char **argv)
174{
175 GLenum type;
176
177 glutInit(&argc, argv);
178
179 if (Args(argc, argv) == GL_FALSE) {
180 exit(1);
181 }
182
183 glutInitWindowPosition(0, 0);
184 glutInitWindowSize(250, 250);
185
186 type = GLUT_RGB | GLUT_ALPHA;
187 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
188 glutInitDisplayMode(type);
189
190 if (glutCreateWindow(*argv) == GL_FALSE) {
191 exit(1);
192 }
193
194 glewInit();
195
196 Init();
197
198 glutReshapeFunc(Reshape);
199 glutKeyboardFunc(Key);
200 glutDisplayFunc(Draw);
201 glutMainLoop();
202 return 0;
203}