quad-tex-2d.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_2D;
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 32
47   {
48      GLubyte tex2d[SIZE][SIZE][3];
49      GLint s, t;
50
51      for (s = 0; s < SIZE; s++) {
52	 for (t = 0; t < SIZE; t++) {
53#if 0
54	    tex2d[t][s][0] = (s < SIZE/2) ? 0 : 255;
55	    tex2d[t][s][1] = (t < SIZE/2) ? 0 : 255;
56	    tex2d[t][s][2] = 0;
57#else
58	    tex2d[t][s][0] = s*255/(SIZE-1);
59	    tex2d[t][s][1] = t*255/(SIZE-1);
60	    tex2d[t][s][2] = 0;
61#endif
62	 }
63      }
64
65      glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
66
67      if (Target == GL_TEXTURE_1D)
68         glTexImage1D(Target, 0, 3, SIZE, 0, GL_RGB, GL_UNSIGNED_BYTE, tex2d);
69      else
70         glTexImage2D(Target, 0, 3, SIZE, SIZE, 0,
71                      GL_RGB, GL_UNSIGNED_BYTE, tex2d);
72
73      glEnable(Target);
74      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
75      glTexParameterf(Target, GL_TEXTURE_WRAP_R, GL_REPEAT);
76      glTexParameterf(Target, GL_TEXTURE_MIN_FILTER, Filter);
77      glTexParameterf(Target, GL_TEXTURE_MAG_FILTER, Filter);
78
79      glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
80   }
81}
82
83static void Reshape(int width, int height)
84{
85   glViewport(0, 0, (GLint)width, (GLint)height);
86   glMatrixMode(GL_PROJECTION);
87   glLoadIdentity();
88#if 0
89   glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
90#else
91   glFrustum(-1, 1, -1, 1, 10, 20);
92#endif
93   glMatrixMode(GL_MODELVIEW);
94   glLoadIdentity();
95   glTranslatef(0, 0, -15);
96}
97
98static void Key(unsigned char key, int x, int y)
99{
100   switch (key) {
101   case 'r':
102      Rot += 10.0;
103      break;
104   case 'R':
105      Rot -= 10.0;
106      break;
107   case 27:
108      glutDestroyWindow(win);
109      exit(0);
110   default:
111      return;
112   }
113   glutPostRedisplay();
114}
115
116static void Draw(void)
117{
118   glClear(GL_COLOR_BUFFER_BIT);
119
120   glPushMatrix();
121   glRotatef(Rot, 0, 1, 0);
122
123   glBegin(GL_QUADS);
124   glTexCoord2f(1,0);
125   glVertex3f( 0.9, -0.9, 0.0);
126   glTexCoord2f(1,1);
127   glVertex3f( 0.9,  0.9, 0.0);
128   glTexCoord2f(0,1);
129   glVertex3f(-0.9,  0.9, 0.0);
130   glTexCoord2f(0,0);
131   glVertex3f(-0.9,  -0.9, 0.0);
132   glEnd();
133
134   glPopMatrix();
135
136   glFlush();
137
138   if (doubleBuffer) {
139      glutSwapBuffers();
140   }
141}
142
143static GLenum Args(int argc, char **argv)
144{
145    GLint i;
146
147    doubleBuffer = GL_FALSE;
148
149    for (i = 1; i < argc; i++) {
150        if (strcmp(argv[i], "-sb") == 0) {
151	    doubleBuffer = GL_FALSE;
152	} else if (strcmp(argv[i], "-db") == 0) {
153	    doubleBuffer = GL_TRUE;
154	} else {
155	    fprintf(stderr, "%s (Bad option).\n", argv[i]);
156	    return GL_FALSE;
157	}
158    }
159    return GL_TRUE;
160}
161
162int main(int argc, char **argv)
163{
164    GLenum type;
165
166    glutInit(&argc, argv);
167
168    if (Args(argc, argv) == GL_FALSE) {
169	exit(1);
170    }
171
172    glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250);
173
174    type = GLUT_RGB;
175    type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
176    glutInitDisplayMode(type);
177
178    win = glutCreateWindow(*argv);
179    if (!win) {
180	exit(1);
181    }
182
183    Init();
184
185    glutReshapeFunc(Reshape);
186    glutKeyboardFunc(Key);
187    glutDisplayFunc(Draw);
188    glutMainLoop();
189    return 0;
190}
191