drawtex.c revision 32001f49
1/*
2 * Copyright (C) 2008  Tunsgten Graphics,Inc.   All Rights Reserved.
3 */
4
5/*
6 * Test GL_OES_draw_texture
7 * Brian Paul
8 * August 2008
9 */
10
11#define GL_GLEXT_PROTOTYPES
12
13#include <math.h>
14#include <stdlib.h>
15#include <stdio.h>
16#include <string.h>
17#include <GLES/gl.h>
18#include <GLES/glext.h>
19#include <EGL/egl.h>
20
21#include "eglut.h"
22
23
24static GLfloat view_posx = 10.0, view_posy = 20.0;
25static GLfloat width = 200, height = 200;
26static GLboolean animate = GL_FALSE;
27static int win;
28
29static PFNGLDRAWTEXFOESPROC glDrawTexfOES_func = NULL;
30
31static void
32draw(void)
33{
34   glClear(GL_COLOR_BUFFER_BIT);
35
36   glDrawTexfOES_func(view_posx, view_posy, 0.0, width, height);
37}
38
39
40/* new window size or exposure */
41static void
42reshape(int width, int height)
43{
44   GLfloat ar = (GLfloat) width / (GLfloat) height;
45
46   glViewport(0, 0, (GLint) width, (GLint) height);
47
48   glMatrixMode(GL_PROJECTION);
49   glLoadIdentity();
50
51#ifdef GL_VERSION_ES_CM_1_0
52   glFrustumf(-ar, ar, -1, 1, 5.0, 60.0);
53#else
54   glFrustum(-ar, ar, -1, 1, 5.0, 60.0);
55#endif
56
57   glMatrixMode(GL_MODELVIEW);
58   glLoadIdentity();
59   glTranslatef(0.0, 0.0, -15.0);
60}
61
62
63static float
64dist(GLuint i, GLuint j, float x, float y)
65{
66   return sqrt((i-x) * (i-x) + (j-y) * (j-y));
67}
68
69static void
70make_smile_texture(void)
71{
72#define SZ 128
73   GLenum Filter = GL_LINEAR;
74   GLubyte image[SZ][SZ][4];
75   GLuint i, j;
76   GLint cropRect[4];
77
78   for (i = 0; i < SZ; i++) {
79      for (j = 0; j < SZ; j++) {
80         GLfloat d_mouth = dist(i, j, SZ/2, SZ/2);
81         GLfloat d_rt_eye = dist(i, j, SZ*3/4, SZ*3/4);
82         GLfloat d_lt_eye = dist(i, j, SZ*3/4, SZ*1/4);
83         if (d_rt_eye < SZ / 8 || d_lt_eye < SZ / 8) {
84            image[i][j][0] = 20;
85            image[i][j][1] = 50;
86            image[i][j][2] = 255;
87            image[i][j][3] = 255;
88         }
89         else if (i < SZ/2 && d_mouth < SZ/3) {
90            image[i][j][0] = 255;
91            image[i][j][1] = 20;
92            image[i][j][2] = 20;
93            image[i][j][3] = 255;
94         }
95         else {
96            image[i][j][0] = 200;
97            image[i][j][1] = 200;
98            image[i][j][2] = 200;
99            image[i][j][3] = 255;
100         }
101      }
102   }
103
104   glActiveTexture(GL_TEXTURE0); /* unit 0 */
105   glBindTexture(GL_TEXTURE_2D, 42);
106   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SZ, SZ, 0,
107                GL_RGBA, GL_UNSIGNED_BYTE, image);
108   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, Filter);
109   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, Filter);
110   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
111   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
112
113   cropRect[0] = 0;
114   cropRect[1] = 0;
115   cropRect[2] = SZ;
116   cropRect[3] = SZ;
117   glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
118#undef SZ
119}
120
121
122
123static void
124init(void)
125{
126   const char *ext = (char *) glGetString(GL_EXTENSIONS);
127
128   if (!strstr(ext, "GL_OES_draw_texture")) {
129      fprintf(stderr, "Sorry, this program requires GL_OES_draw_texture\n");
130      exit(1);
131   }
132
133   glDrawTexfOES_func = (PFNGLDRAWTEXFOESPROC) eglGetProcAddress("glDrawTexfOES");
134
135   if (!glDrawTexfOES_func) {
136      fprintf(stderr, "Sorry, failed to resolve glDrawTexfOES function\n");
137      exit(1);
138   }
139
140   glClearColor(0.4, 0.4, 0.4, 0.0);
141
142   make_smile_texture();
143   glEnable(GL_TEXTURE_2D);
144}
145
146
147static void
148idle(void)
149{
150   if (animate) {
151      view_posx += 1.0;
152      view_posy += 2.0;
153      eglutPostRedisplay();
154   }
155}
156
157static void
158key(unsigned char key)
159{
160   switch (key) {
161   case ' ':
162      animate = !animate;
163      break;
164   case 'w':
165      width -= 1.0f;
166      break;
167   case 'W':
168      width += 1.0f;
169      break;
170   case 'h':
171      height -= 1.0f;
172      break;
173   case 'H':
174      height += 1.0f;
175      break;
176   case 27:
177      eglutDestroyWindow(win);
178      exit(0);
179      break;
180   default:
181      break;
182   }
183}
184
185static void
186special_key(int key)
187{
188   switch (key) {
189   case EGLUT_KEY_LEFT:
190      view_posx -= 1.0;
191      break;
192   case EGLUT_KEY_RIGHT:
193      view_posx += 1.0;
194      break;
195   case EGLUT_KEY_UP:
196      view_posy += 1.0;
197      break;
198   case EGLUT_KEY_DOWN:
199      view_posy -= 1.0;
200      break;
201   default:
202      break;
203   }
204}
205
206int
207main(int argc, char *argv[])
208{
209   eglutInitWindowSize(400, 300);
210   eglutInitAPIMask(EGLUT_OPENGL_ES1_BIT);
211   eglutInit(argc, argv);
212
213   win = eglutCreateWindow("drawtex");
214
215   eglutIdleFunc(idle);
216   eglutReshapeFunc(reshape);
217   eglutDisplayFunc(draw);
218   eglutKeyboardFunc(key);
219   eglutSpecialFunc(special_key);
220
221   init();
222
223   eglutMainLoop();
224
225   return 0;
226}
227