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