PageRenderTime 59ms CodeModel.GetById 37ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/opengles/redbook/wrap.c

http://ftk.googlecode.com/
C | 192 lines | 130 code | 16 blank | 46 comment | 3 complexity | 378729bc8406d2727b3589505569e44e 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/*  wrap.c
 37 *  This program texture maps a checkerboard image onto
 38 *  two rectangles.  This program demonstrates the wrapping
 39 *  modes, if the texture coordinates fall outside 0.0 and 1.0.
 40 *  Interaction: Pressing the 's' and 'S' keys switch the
 41 *  wrapping between clamping and repeating for the s parameter.
 42 *  The 't' and 'T' keys control the wrapping for the t parameter.
 43 *
 44 *  If running this program on OpenGL 1.0, texture objects are
 45 *  not used.
 46 */
 47#include <stdlib.h>
 48#include <stdio.h>
 49#include "ug.h"
 50
 51/*	Create checkerboard texture	*/
 52#define	checkImageWidth 64
 53#define	checkImageHeight 64
 54static GLubyte checkImage[checkImageHeight][checkImageWidth][4];
 55
 56#ifdef GL_VERSION_1_1
 57static GLuint texName;
 58#endif
 59
 60void makeCheckImage(void)
 61{
 62   int i, j, c;
 63    
 64   for (i = 0; i < checkImageHeight; i++) {
 65      for (j = 0; j < checkImageWidth; j++) {
 66         c = ((((i&0x8)==0)^((j&0x8))==0))*255;
 67         checkImage[i][j][0] = (GLubyte) c;
 68         checkImage[i][j][1] = (GLubyte) c;
 69         checkImage[i][j][2] = (GLubyte) c;
 70         checkImage[i][j][3] = (GLubyte) 255;
 71      }
 72   }
 73}
 74
 75void init(void)
 76{    
 77   static const GLfloat v[] = {
 78       -2.0, -1.0, 0.0,
 79       -2.0, 1.0, 0.0,
 80       0.0, -1.0, 0.0,
 81       0.0, 1.0, 0.0,
 82
 83       1.0, -1.0, 0.0,
 84       1.0, 1.0, 0.0,
 85       2.41421, -1.0, -1.41421,
 86       2.41421, 1.0, -1.41421,
 87   };
 88   static const GLfloat t[] = {
 89       0.0, 0.0,
 90       0.0, 3.0,
 91       3.0, 0.0,
 92       3.0, 3.0,
 93
 94       0.0, 0.0,
 95       0.0, 3.0,
 96       3.0, 0.0,
 97       3.0, 3.0,
 98   };
 99   glClearColor (0.0, 0.0, 0.0, 0.0);
100   glShadeModel(GL_FLAT);
101   glEnable(GL_DEPTH_TEST);
102
103   makeCheckImage();
104   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
105
106#ifdef GL_VERSION_1_1
107   glGenTextures(1, &texName);
108   glBindTexture(GL_TEXTURE_2D, texName);
109#endif
110
111   glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
112   glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
113   glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
114   glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
115#ifdef GL_VERSION_1_1
116   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight, 
117                0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
118#else
119   glTexImage2D(GL_TEXTURE_2D, 0, 4, checkImageWidth, checkImageHeight, 
120                0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
121#endif
122   glVertexPointer(3, GL_FLOAT, 0, v);
123   glTexCoordPointer(2, GL_FLOAT, 0, t);
124   glEnableClientState(GL_VERTEX_ARRAY);
125   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
126}
127
128void display(UGWindow uwin)
129{
130   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
131   glEnable(GL_TEXTURE_2D);
132   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
133#ifdef GL_VERSION_1_1
134   glBindTexture(GL_TEXTURE_2D, texName);
135#endif
136
137   glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
138   glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
139   glFlush();
140   ugSwapBuffers(uwin);
141   glDisable(GL_TEXTURE_2D);
142}
143
144void reshape(UGWindow uwin, int w, int h)
145{
146   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
147   glMatrixMode(GL_PROJECTION);
148   glLoadIdentity();
149   ugluPerspectivef(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
150   glMatrixMode(GL_MODELVIEW);
151   glLoadIdentity();
152   glTranslatef(0.0, 0.0, -3.6);
153}
154
155void keyboard (UGWindow uwin, int key, int x, int y)
156{
157   switch (key) {
158      case 's':
159         glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
160         ugPostRedisplay(uwin);
161         break;
162      case 'S':
163         glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
164         ugPostRedisplay(uwin);
165         break;
166      case 't':
167         glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
168         ugPostRedisplay(uwin);
169         break;
170      case 'T':
171         glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
172         ugPostRedisplay(uwin);
173         break;
174      case 27:
175         exit(0);
176         break;
177      default:
178         break;
179   }
180}
181
182int main(int argc, char** argv)
183{
184   UGCtx ug = ugInit();
185   UGWindow uwin = ugCreateWindow (ug, "UG_DEPTH", argv[0], 250, 250, 100, 100);
186   init ();
187   ugDisplayFunc(uwin, display); 
188   ugReshapeFunc(uwin, reshape);
189   ugKeyboardFunc(uwin, keyboard);
190   ugMainLoop(ug);
191   return 0; 
192}