summaryrefslogtreecommitdiffstats
path: root/progs/demos/pixeltex.c
blob: 03e653a45439b7507290737c1b14e638e3ee6f21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * GL_SGIS_pixel_texture demo
 *
 * Brian Paul
 * 6 Apr 2000
 *
 * Copyright (C) 2000  Brian Paul   All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


/*
 * How this works:
 * 1. We load the image into a 2D texture.
 * 2. We generate a sequence of RGB images in which the R component
 *    is really the S texture coordinate and the G component is really
 *    the T texture coordinate.
 *    By warping the mapping from R to S and G to T we can get non-linear
 *    distortions.
 * 3. Draw the warped image (a 2-D warping function) with pixel texgen
 *    enabled.
 * 4. Loop over the warped images to animate.
 *
 * The pixel texgen extension can also be used to do color-space
 * conversions.  For example, we could convert YCR to RGB with a
 * 3D texture map which takes YCR as the S,T,R texture coordinate and
 * returns RGB texel values.
 *
 * You can use this extension in (at least) two ways:
 * 1. glDrawPixels w/ color space conversion/warping
 * 2. glDrawPixels to spatially warp another image in texture memory
 *
 * We're basically using glDrawPixels to draw a texture coordinate image.
 */


#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GL/glut.h>
#include <GL/glext.h>
#include "readtex.h"

#define TEXTURE_FILE "../images/girl.rgb"

static int ImgWidth = 300, ImgHeight = 300;
#define FRAMES 20
static GLubyte *ImgData[FRAMES];
static GLint Frame = 0;

static GLboolean TextureFlag = GL_TRUE;


static void Display( void )
{
   glClear( GL_COLOR_BUFFER_BIT );

   if (TextureFlag) {
      glEnable(GL_PIXEL_TEXTURE_SGIS);
      glEnable(GL_TEXTURE_2D);
   }
   else {
      glDisable(GL_PIXEL_TEXTURE_SGIS);
      glDisable(GL_TEXTURE_2D);
   }

   glColor3f(1, 1, 1);
   glRasterPos2f(10, 10);
   glDrawPixels(ImgWidth, ImgHeight, GL_RGB, GL_UNSIGNED_BYTE, ImgData[Frame]);

   glutSwapBuffers();
}


static void Reshape( int width, int height )
{
   glViewport( 0, 0, width, height );
   glMatrixMode( GL_PROJECTION );
   glLoadIdentity();
   glOrtho(0, width, 0, height, -1, 1);
   glMatrixMode( GL_MODELVIEW );
   glLoadIdentity();
}


static void Key( unsigned char key, int x, int y )
{
   (void) x;
   (void) y;
   switch (key) {
      case ' ':
         TextureFlag = !TextureFlag;
         break;
      case 27:
         exit(0);
         break;
   }
   glutPostRedisplay();
}


static void Idle(void)
{
   Frame++;
   if (Frame >= FRAMES)
      Frame = 0;
   glutPostRedisplay();
}


static GLubyte warp(GLfloat s, int frame)
{
   static const GLfloat pi = 3.14159265;
   static int halfFrame = FRAMES / 2;
   GLfloat y, weight, v;
   if (frame >= halfFrame)
      frame = halfFrame - (frame - halfFrame);
   y = sin(s * pi);
   weight = (float) frame / (FRAMES-1);
   v = y * (0.8 * weight + 0.2);
   return (GLint) (v * 255.0F);
}


static void InitImage(void)
{
   int i, j, frame;
   for (frame = 0; frame < FRAMES; frame++) {
      ImgData[frame] = (GLubyte *) malloc(ImgWidth * ImgHeight * 3);
      for (i = 0; i < ImgHeight; i++) {
         for (j = 0; j < ImgWidth; j++) {
            GLubyte *pixel = ImgData[frame] + (i * ImgWidth + j) * 3;
            pixel[0] = warp((float) j / (ImgWidth - 0), frame);
            pixel[1] = warp((float) i / (ImgHeight - 0), frame);
            pixel[2] = 0.0;
         }
      }
   }
}


static void Init( int argc, char *argv[] )
{
   const char *exten = (const char *) glGetString(GL_EXTENSIONS);
   if (!strstr(exten, "GL_SGIS_pixel_texture")) {
      printf("Sorry, GL_SGIS_pixel_texture not supported by this renderer.\n");
      exit(1);
   }

   /* linear filtering looks nicer, but it's slower, since it's in software */
#if 1
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
#else
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
#endif

   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

   if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
      printf("Error: couldn't load texture image\n");
      exit(1);
   }

   glClearColor(0.3, 0.3, 0.4, 1.0);

   InitImage();

   printf("Hit SPACE to toggle pixel texgen\n");
}


int main( int argc, char *argv[] )
{
   glutInit( &argc, argv );
   glutInitWindowSize( 330, 330 );
   glutInitWindowPosition( 0, 0 );
   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
   glutCreateWindow(argv[0] );

   Init( argc, argv );

   glutKeyboardFunc( Key );
   glutReshapeFunc( Reshape );
   glutDisplayFunc( Display );
   glutIdleFunc( Idle );

   glutMainLoop();
   return 0;
}