diff options
author | ftrapero <frantracer@gmail.com> | 2017-06-15 14:15:08 +0200 |
---|---|---|
committer | ftrapero <frantracer@gmail.com> | 2017-06-15 14:15:08 +0200 |
commit | 459021c165c7023ee75f524060ca270985b547c1 (patch) | |
tree | ed3dfb281a1c00a9cf59ebf9d165e5a7a0f346bf /nx-X11/extras/Mesa_6.4.1/progs/demos/pixeltex.c | |
parent | 4f216e7cf7571885f2995433580c9a3ec068eef4 (diff) | |
parent | 8667d4d97a3e958a7f63c18c34a62469ba5d2079 (diff) | |
download | nx-libs-459021c165c7023ee75f524060ca270985b547c1.tar.gz nx-libs-459021c165c7023ee75f524060ca270985b547c1.tar.bz2 nx-libs-459021c165c7023ee75f524060ca270985b547c1.zip |
Include mesa-6.4.1 project
Diffstat (limited to 'nx-X11/extras/Mesa_6.4.1/progs/demos/pixeltex.c')
-rw-r--r-- | nx-X11/extras/Mesa_6.4.1/progs/demos/pixeltex.c | 210 |
1 files changed, 210 insertions, 0 deletions
diff --git a/nx-X11/extras/Mesa_6.4.1/progs/demos/pixeltex.c b/nx-X11/extras/Mesa_6.4.1/progs/demos/pixeltex.c new file mode 100644 index 000000000..03e653a45 --- /dev/null +++ b/nx-X11/extras/Mesa_6.4.1/progs/demos/pixeltex.c @@ -0,0 +1,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; +} |