PageRenderTime 295ms CodeModel.GetById 161ms app.highlight 14ms RepoModel.GetById 117ms app.codeStats 0ms

/opengles/redbook/texsub.c

http://ftk.googlecode.com/
C | 199 lines | 141 code | 15 blank | 43 comment | 6 complexity | aa0c782f3229ad9d59b62789d5b9b9eb MD5 | raw file
  1/*
  2 * License Applicability. Except to the extent portions of this file are
  3 * made subject to an alternative license as permitted in the SGI Free
  4 * Software License B, Version 1.1 (the "License"), the contents of this
  5 * file are subject only to the provisions of the License. You may not use
  6 * this file except in compliance with the License. You may obtain a copy
  7 * of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
  8 * Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
  9 * 
 10 * http://oss.sgi.com/projects/FreeB
 11 * 
 12 * Note that, as provided in the License, the Software is distributed on an
 13 * "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
 14 * DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
 15 * CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
 16 * PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
 17 * 
 18 * Original Code. The Original Code is: OpenGL Sample Implementation,
 19 * Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
 20 * Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
 21 * Copyright in any portions created by third parties is as indicated
 22 * elsewhere herein. All Rights Reserved.
 23 * 
 24 * Additional Notice Provisions: The application programming interfaces
 25 * established by SGI in conjunction with the Original Code are The
 26 * OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
 27 * April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
 28 * 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
 29 * Window System(R) (Version 1.3), released October 19, 1998. This software
 30 * was created using the OpenGL(R) version 1.2.1 Sample Implementation
 31 * published by SGI, but has not been independently verified as being
 32 * compliant with the OpenGL(R) version 1.2.1 Specification.
 33 *
 34 */
 35
 36/*  texsub.c
 37 *  This program texture maps a checkerboard image onto
 38 *  two rectangles.  This program clamps the texture, if
 39 *  the texture coordinates fall outside 0.0 and 1.0.
 40 *  If the s key is pressed, a texture subimage is used to
 41 *  alter the original texture.  If the r key is pressed, 
 42 *  the original texture is restored.
 43 */
 44#include <stdlib.h>
 45#include <stdio.h>
 46#include "ug.h"
 47
 48#if defined(GL_VERSION_1_1) || defined(GL_OES_VERSION_1_0)
 49/*  Create checkerboard textures  */
 50#define checkImageWidth 64
 51#define checkImageHeight 64
 52#define subImageWidth 16
 53#define subImageHeight 16
 54static GLubyte checkImage[checkImageHeight][checkImageWidth][4];
 55static GLubyte subImage[subImageHeight][subImageWidth][4];
 56
 57static GLuint texName;
 58
 59void makeCheckImages(void)
 60{
 61   int i, j, c;
 62    
 63   for (i = 0; i < checkImageHeight; i++) {
 64      for (j = 0; j < checkImageWidth; j++) {
 65         c = ((((i&0x8)==0)^((j&0x8))==0))*255;
 66         checkImage[i][j][0] = (GLubyte) c;
 67         checkImage[i][j][1] = (GLubyte) c;
 68         checkImage[i][j][2] = (GLubyte) c;
 69         checkImage[i][j][3] = (GLubyte) 255;
 70      }
 71   }
 72   for (i = 0; i < subImageHeight; i++) {
 73      for (j = 0; j < subImageWidth; j++) {
 74         c = ((((i&0x4)==0)^((j&0x4))==0))*255;
 75         subImage[i][j][0] = (GLubyte) c;
 76         subImage[i][j][1] = (GLubyte) 0;
 77         subImage[i][j][2] = (GLubyte) 0;
 78         subImage[i][j][3] = (GLubyte) 255;
 79      }
 80   }
 81}
 82
 83void init(void)
 84{    
 85   static const GLfloat v[] = {
 86       -2.0, -1.0, 0.0,
 87       -2.0, 1.0, 0.0,
 88       0.0, -1.0, 0.0,
 89       0.0, 1.0, 0.0,
 90
 91       1.0, -1.0, 0.0,
 92       1.0, 1.0, 0.0,
 93       2.41421, -1.0, -1.41421,
 94       2.41421, 1.0, -1.41421,
 95   };
 96   static const GLfloat t[] = {
 97       0.0, 0.0,
 98       0.0, 1.0,
 99       1.0, 0.0,
100       1.0, 1.0,
101
102       0.0, 0.0,
103       0.0, 1.0,
104       1.0, 0.0,
105       1.0, 1.0,
106   };
107   glClearColor (0.0, 0.0, 0.0, 0.0);
108   glShadeModel(GL_FLAT);
109   glEnable(GL_DEPTH_TEST);
110
111   makeCheckImages();
112   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
113
114   glGenTextures(1, &texName);
115   glBindTexture(GL_TEXTURE_2D, texName);
116
117   glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
118   glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
119   glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
120   glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
121   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight, 
122                0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
123   glVertexPointer(3, GL_FLOAT, 0, v);
124   glTexCoordPointer(2, GL_FLOAT, 0, t);
125   glEnableClientState(GL_VERTEX_ARRAY);
126   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
127}
128
129void display(UGWindow uwin)
130{
131   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
132   glEnable(GL_TEXTURE_2D);
133   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
134   glBindTexture(GL_TEXTURE_2D, texName);
135   glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
136   glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
137   glFlush();
138   glDisable(GL_TEXTURE_2D);
139   ugSwapBuffers(uwin);
140}
141
142void reshape(UGWindow uwin, int w, int h)
143{
144   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
145   glMatrixMode(GL_PROJECTION);
146   glLoadIdentity();
147   ugluPerspectivef(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
148   glMatrixMode(GL_MODELVIEW);
149   glLoadIdentity();
150   glTranslatef(0.0, 0.0, -3.6);
151}
152
153void keyboard (UGWindow uwin, int key, int x, int y)
154{
155   switch (key) {
156      case 's':
157      case 'S':
158         glBindTexture(GL_TEXTURE_2D, texName);
159         glTexSubImage2D(GL_TEXTURE_2D, 0, 12, 44, subImageWidth,
160                         subImageHeight, GL_RGBA,
161                         GL_UNSIGNED_BYTE, subImage);
162         ugPostRedisplay(uwin);
163         break;
164      case 'r':
165      case 'R':
166         glBindTexture(GL_TEXTURE_2D, texName);
167         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,
168                      checkImageHeight, 0, GL_RGBA,
169                      GL_UNSIGNED_BYTE, checkImage);
170         ugPostRedisplay(uwin);
171         break;
172      case 27:
173         exit(0);
174         break;
175      default:
176         break;
177   }
178}
179
180int main(int argc, char** argv)
181{
182   UGCtx ug = ugInit();
183   UGWindow uwin = ugCreateWindow (ug, "UG_DEPTH", argv[0], 250, 250, 100, 100);
184   init();
185   ugDisplayFunc(uwin, display); 
186   ugReshapeFunc(uwin, reshape);
187   ugKeyboardFunc(uwin, keyboard);
188   ugMainLoop(ug);
189   return 0; 
190}
191#else
192int main(int argc, char** argv)
193{
194    fprintf (stderr, "This program demonstrates a feature which is not in OpenGL Version 1.0.\n");
195    fprintf (stderr, "If your implementation of OpenGL Version 1.0 has the right extensions,\n");
196    fprintf (stderr, "you may be able to modify this program to make it run.\n");
197    return 0;
198}
199#endif