PageRenderTime 511ms CodeModel.GetById 163ms app.highlight 214ms RepoModel.GetById 124ms app.codeStats 1ms

/opengles/redbook/texbind.c

http://ftk.googlecode.com/
C | 183 lines | 128 code | 16 blank | 39 comment | 4 complexity | 2b54b9cd77f9e6a51ff33bf9c73de361 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/*  texbind.c
 37 *  This program demonstrates using glBindTexture() by 
 38 *  creating and managing two textures.
 39 */
 40#include <stdlib.h>
 41#include <stdio.h>
 42#include "ug.h"
 43
 44#if defined(GL_VERSION_1_1) || defined(GL_OES_VERSION_1_0)
 45/*	Create checkerboard texture	*/
 46#define	checkImageWidth 64
 47#define	checkImageHeight 64
 48static GLubyte checkImage[checkImageHeight][checkImageWidth][4];
 49static GLubyte otherImage[checkImageHeight][checkImageWidth][4];
 50
 51static GLuint texName[2];
 52
 53void makeCheckImages(void)
 54{
 55   int i, j, c;
 56    
 57   for (i = 0; i < checkImageHeight; i++) {
 58      for (j = 0; j < checkImageWidth; j++) {
 59         c = ((((i&0x8)==0)^((j&0x8))==0))*255;
 60         checkImage[i][j][0] = (GLubyte) c;
 61         checkImage[i][j][1] = (GLubyte) c;
 62         checkImage[i][j][2] = (GLubyte) c;
 63         checkImage[i][j][3] = (GLubyte) 255;
 64         c = ((((i&0x10)==0)^((j&0x10))==0))*255;
 65         otherImage[i][j][0] = (GLubyte) c;
 66         otherImage[i][j][1] = (GLubyte) 0;
 67         otherImage[i][j][2] = (GLubyte) 0;
 68         otherImage[i][j][3] = (GLubyte) 255;
 69      }
 70   }
 71}
 72
 73void init(void)
 74{    
 75   static const GLfloat v[] = {
 76       -2.0, -1.0, 0.0,
 77       -2.0, 1.0, 0.0,
 78       0.0, -1.0, 0.0,
 79       0.0, 1.0, 0.0,
 80
 81       1.0, -1.0, 0.0,
 82       1.0, 1.0, 0.0,
 83       2.41421, -1.0, -1.41421,
 84       2.41421, 1.0, -1.41421,
 85   };
 86   static const GLfloat t[] = {
 87       0.0, 0.0,
 88       0.0, 1.0,
 89       1.0, 0.0,
 90       1.0, 1.0,
 91
 92       0.0, 0.0,
 93       0.0, 1.0,
 94       1.0, 0.0,
 95       1.0, 1.0,
 96   };
 97   glClearColor (0.0, 0.0, 0.0, 0.0);
 98   glShadeModel(GL_FLAT);
 99   glEnable(GL_DEPTH_TEST);
100
101   makeCheckImages();
102   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
103
104   glGenTextures(2, texName);
105   glBindTexture(GL_TEXTURE_2D, texName[0]);
106   glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
107   glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
108   glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 
109                   GL_NEAREST);
110   glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
111                   GL_NEAREST);
112   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,
113                checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
114                checkImage);
115
116   glBindTexture(GL_TEXTURE_2D, texName[1]);
117   glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
118   glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
119   glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
120   glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
121   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
122   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, 
123                checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 
124                otherImage);
125   glEnable(GL_TEXTURE_2D);
126   glVertexPointer(3, GL_FLOAT, 0, v);
127   glTexCoordPointer(2, GL_FLOAT, 0, t);
128   glEnableClientState(GL_VERTEX_ARRAY);
129   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
130}
131
132void display(UGWindow uwin)
133{
134   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
135   glBindTexture(GL_TEXTURE_2D, texName[0]);
136   glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
137   glBindTexture(GL_TEXTURE_2D, texName[1]);
138   glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
139   glFlush();
140   ugSwapBuffers(uwin);
141}
142
143void reshape(UGWindow uwin, int w, int h)
144{
145   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
146   glMatrixMode(GL_PROJECTION);
147   glLoadIdentity();
148   ugluPerspectivef(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
149   glMatrixMode(GL_MODELVIEW);
150   glLoadIdentity();
151   glTranslatef(0.0, 0.0, -3.6);
152}
153
154void keyboard(UGWindow uwin, int key, int x, int y)
155{
156   switch (key) {
157      case 27:
158         exit(0);
159         break;
160   }
161}
162
163int main(int argc, char** argv)
164{
165   UGCtx ug = ugInit();
166   UGWindow uwin = ugCreateWindow (ug, "UG_DEPTH", argv[0], 250, 250, 100, 100);
167   init();
168   ugDisplayFunc(uwin, display); 
169   ugReshapeFunc(uwin, reshape);
170   ugKeyboardFunc(uwin, keyboard);
171   ugMainLoop(ug);
172   return 0; 
173}
174#else
175int main(int argc, char** argv)
176{
177    fprintf (stderr, "This program demonstrates a feature which is not in OpenGL Version 1.0.\n");
178    fprintf (stderr, "If your implementation of OpenGL Version 1.0 has the right extensions,\n");
179    fprintf (stderr, "you may be able to modify this program to make it run.\n");
180    return 0;
181}
182#endif
183