2 * Copyright (C) 2008 Tunsgten Graphics,Inc. All Rights Reserved.
6 * Test GL_OES_draw_texture
11 #define GL_GLEXT_PROTOTYPES
19 #include <X11/Xutil.h>
20 #include <X11/keysym.h>
22 #include <GLES/glext.h>
27 static GLfloat view_posx
= 10.0, view_posy
= 20.0;
28 static GLfloat width
= 200, height
= 200;
34 glClear(GL_COLOR_BUFFER_BIT
);
36 glDrawTexfOES(view_posx
, view_posy
, 0.0, width
, height
);
40 /* new window size or exposure */
42 reshape(int width
, int height
)
44 GLfloat ar
= (GLfloat
) width
/ (GLfloat
) height
;
46 glViewport(0, 0, (GLint
) width
, (GLint
) height
);
48 glMatrixMode(GL_PROJECTION
);
51 #ifdef GL_VERSION_ES_CM_1_0
52 glFrustumf(-ar
, ar
, -1, 1, 5.0, 60.0);
54 glFrustum(-ar
, ar
, -1, 1, 5.0, 60.0);
57 glMatrixMode(GL_MODELVIEW
);
59 glTranslatef(0.0, 0.0, -15.0);
64 dist(GLuint i
, GLuint j
, float x
, float y
)
66 return sqrt((i
-x
) * (i
-x
) + (j
-y
) * (j
-y
));
70 make_smile_texture(void)
73 GLenum Filter
= GL_LINEAR
;
74 GLubyte image
[SZ
][SZ
][4];
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) {
89 else if (i
< SZ
/2 && d_mouth
< SZ
/3) {
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
);
117 glTexParameteriv(GL_TEXTURE_2D
, GL_TEXTURE_CROP_RECT_OES
, cropRect
);
126 const char *ext
= (char *) glGetString(GL_EXTENSIONS
);
128 if (!strstr(ext
, "GL_OES_draw_texture")) {
129 fprintf(stderr
, "Sorry, this program requires GL_OES_draw_texture");
133 glClearColor(0.4, 0.4, 0.4, 0.0);
135 make_smile_texture();
136 glEnable(GL_TEXTURE_2D
);
141 * Create an RGB, double-buffered X window.
142 * Return the window and context handles.
145 make_x_window(Display
*x_dpy
, EGLDisplay egl_dpy
,
147 int x
, int y
, int width
, int height
,
152 static const EGLint attribs
[] = {
160 XSetWindowAttributes attr
;
164 XVisualInfo
*visInfo
, visTemplate
;
171 scrnum
= DefaultScreen( x_dpy
);
172 root
= RootWindow( x_dpy
, scrnum
);
174 if (!eglChooseConfig( egl_dpy
, attribs
, &config
, 1, &num_configs
)) {
175 printf("Error: couldn't get an EGL visual config\n");
180 assert(num_configs
> 0);
182 if (!eglGetConfigAttrib(egl_dpy
, config
, EGL_NATIVE_VISUAL_ID
, &vid
)) {
183 printf("Error: eglGetConfigAttrib() failed\n");
187 /* The X window visual must match the EGL config */
188 visTemplate
.visualid
= vid
;
189 visInfo
= XGetVisualInfo(x_dpy
, VisualIDMask
, &visTemplate
, &num_visuals
);
191 printf("Error: couldn't get X visual\n");
195 /* window attributes */
196 attr
.background_pixel
= 0;
197 attr
.border_pixel
= 0;
198 attr
.colormap
= XCreateColormap( x_dpy
, root
, visInfo
->visual
, AllocNone
);
199 attr
.event_mask
= StructureNotifyMask
| ExposureMask
| KeyPressMask
;
200 mask
= CWBackPixel
| CWBorderPixel
| CWColormap
| CWEventMask
;
202 win
= XCreateWindow( x_dpy
, root
, 0, 0, width
, height
,
203 0, visInfo
->depth
, InputOutput
,
204 visInfo
->visual
, mask
, &attr
);
206 /* set hints and properties */
208 XSizeHints sizehints
;
211 sizehints
.width
= width
;
212 sizehints
.height
= height
;
213 sizehints
.flags
= USSize
| USPosition
;
214 XSetNormalHints(x_dpy
, win
, &sizehints
);
215 XSetStandardProperties(x_dpy
, win
, name
, name
,
216 None
, (char **)NULL
, 0, &sizehints
);
219 eglBindAPI(EGL_OPENGL_ES_API
);
221 ctx
= eglCreateContext(egl_dpy
, config
, EGL_NO_CONTEXT
, NULL
);
223 printf("Error: eglCreateContext failed\n");
227 *surfRet
= eglCreateWindowSurface(egl_dpy
, config
, win
, NULL
);
230 printf("Error: eglCreateWindowSurface failed\n");
242 event_loop(Display
*dpy
, Window win
,
243 EGLDisplay egl_dpy
, EGLSurface egl_surf
)
250 if (!anim
|| XPending(dpy
)) {
252 XNextEvent(dpy
, &event
);
254 switch (event
.type
) {
258 case ConfigureNotify
:
259 reshape(event
.xconfigure
.width
, event
.xconfigure
.height
);
265 code
= XLookupKeysym(&event
.xkey
, 0);
266 if (code
== XK_Left
) {
269 else if (code
== XK_Right
) {
272 else if (code
== XK_Up
) {
275 else if (code
== XK_Down
) {
279 r
= XLookupString(&event
.xkey
, buffer
, sizeof(buffer
),
281 if (buffer
[0] == ' ') {
284 else if (buffer
[0] == 'w') {
287 else if (buffer
[0] == 'W') {
290 else if (buffer
[0] == 'h') {
293 else if (buffer
[0] == 'H') {
296 else if (buffer
[0] == 27) {
317 eglSwapBuffers(egl_dpy
, egl_surf
);
327 printf(" -display <displayname> set the display to run on\n");
328 printf(" -info display OpenGL renderer info\n");
333 main(int argc
, char *argv
[])
335 const int winWidth
= 400, winHeight
= 300;
341 char *dpyName
= NULL
;
342 GLboolean printInfo
= GL_FALSE
;
343 EGLint egl_major
, egl_minor
;
347 for (i
= 1; i
< argc
; i
++) {
348 if (strcmp(argv
[i
], "-display") == 0) {
352 else if (strcmp(argv
[i
], "-info") == 0) {
361 x_dpy
= XOpenDisplay(dpyName
);
363 printf("Error: couldn't open display %s\n",
364 dpyName
? dpyName
: getenv("DISPLAY"));
368 egl_dpy
= eglGetDisplay(x_dpy
);
370 printf("Error: eglGetDisplay() failed\n");
374 if (!eglInitialize(egl_dpy
, &egl_major
, &egl_minor
)) {
375 printf("Error: eglInitialize() failed\n");
379 s
= eglQueryString(egl_dpy
, EGL_VERSION
);
380 printf("EGL_VERSION = %s\n", s
);
382 s
= eglQueryString(egl_dpy
, EGL_VENDOR
);
383 printf("EGL_VENDOR = %s\n", s
);
385 s
= eglQueryString(egl_dpy
, EGL_EXTENSIONS
);
386 printf("EGL_EXTENSIONS = %s\n", s
);
388 s
= eglQueryString(egl_dpy
, EGL_CLIENT_APIS
);
389 printf("EGL_CLIENT_APIS = %s\n", s
);
391 make_x_window(x_dpy
, egl_dpy
,
392 "drawtex", 0, 0, winWidth
, winHeight
,
393 &win
, &egl_ctx
, &egl_surf
);
395 XMapWindow(x_dpy
, win
);
396 if (!eglMakeCurrent(egl_dpy
, egl_surf
, egl_surf
, egl_ctx
)) {
397 printf("Error: eglMakeCurrent() failed\n");
402 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
403 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
404 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
405 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
410 /* Set initial projection/viewing transformation.
411 * We can't be sure we'll get a ConfigureNotify event when the window
414 reshape(winWidth
, winHeight
);
416 event_loop(x_dpy
, win
, egl_dpy
, egl_surf
);
418 eglDestroyContext(egl_dpy
, egl_ctx
);
419 eglDestroySurface(egl_dpy
, egl_surf
);
420 eglTerminate(egl_dpy
);
423 XDestroyWindow(x_dpy
, win
);
424 XCloseDisplay(x_dpy
);