1 /* 2 * Copyright 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <stdio.h> 18 #include <string.h> 19 #include <stdlib.h> 20 #include <unistd.h> 21 22 //#define GL_API 23 //#define GL_APIENTRY 24 25 #undef ANDROID 26 #include <EGL/egl.h> 27 #include <GLES/gl.h> 28 29 #ifdef __APPLE__ 30 extern "C" void * createGLView(void *nsWindowPtr, int x, int y, int width, int height); 31 #endif 32 33 #undef HAVE_MALLOC_H 34 #include <SDL.h> 35 #include <SDL_syswm.h> 36 37 38 #define WINDOW_WIDTH 500 39 #define WINDOW_HEIGHT 500 40 41 #define TEX_WIDTH 256 42 #define TEX_HEIGHT 256 43 44 45 #define F_to_X(d) ((d) > 32767.65535 ? 32767 * 65536 + 65535 : \ 46 (d) < -32768.65535 ? -32768 * 65536 + 65535 : \ 47 ((GLfixed) ((d) * 65536))) 48 #define X_to_F(x) ((float)(x))/65536.0f 49 50 static EGLint const attribute_list[] = { 51 EGL_RED_SIZE, 1, 52 EGL_GREEN_SIZE, 1, 53 EGL_BLUE_SIZE, 1, 54 EGL_NONE 55 }; 56 57 unsigned char *genTexture(int width, int height, int comp) 58 { 59 unsigned char *img = new unsigned char[width * height * comp]; 60 unsigned char *ptr = img; 61 for (int i = 0; i < height; i++) { 62 for (int j = 0; j < width; j++) { 63 unsigned char col = ((i / 8 + j / 8) % 2) * 255 ; 64 if (j>(width/2)) col/=2; 65 for (int c = 0; c < comp; c++) { 66 *ptr = col; ptr++; 67 } 68 } 69 } 70 return img; 71 } 72 73 unsigned char *genRedTexture(int width, int height, int comp) 74 { 75 unsigned char *img = new unsigned char[width * height * comp]; 76 memset(img,0,width*height*comp); 77 unsigned char *ptr = img; 78 for (int i = 0; i < height; i++) { 79 for (int j = 0; j < width; j++) { 80 unsigned char col = ((i / 8 + j / 8) % 2) * 255 ; 81 *ptr = col; 82 ptr+=comp; 83 } 84 } 85 return img; 86 } 87 88 //mip 0; 89 unsigned char *genPalette4_rgb8(int width, int height,int color) 90 { 91 int size = width*height/2 + 16*3/*palette size*/; 92 unsigned char *img = new unsigned char[size]; 93 94 memset(img,0,size); 95 img[0] = 255; img[1] = 0; img[2] = 0; // red 96 img[3] = 0; img[4] = 0; img[5] = 255; //blue 97 img[7] = 128; img[8] = 0; img[9] = 128; //fucsia 98 //rest of the palette is empty 99 100 unsigned char *ptr = img+(16*3); 101 for (int i = 0; i < (height*width/2); i++) { 102 ptr[i] = (i%2)?0x0|color:0x11|color; 103 } 104 return img; 105 } 106 107 void usage(const char *progname) 108 { 109 fprintf(stderr, "usage: %s [-n <nframes> -i -h]\n", progname); 110 fprintf(stderr, "\t-h: this message\n"); 111 fprintf(stderr, "\t-i: immidate mode\n"); 112 fprintf(stderr, "\t-n nframes: generate nframes\n"); 113 fprintf(stderr, "\t-e: use index arrays\n"); 114 fprintf(stderr, "\t-t: use texture\n"); 115 fprintf(stderr, "\t-f: use fixed points\n"); 116 fprintf(stderr, "\t-p: use point size OES extention\n"); 117 } 118 119 #define SWITCH_SOURCE(add)\ 120 if(useConvertedType){ \ 121 if(useFixed){ \ 122 data = (GLvoid*)(fixedVertices+(add)); \ 123 } else { \ 124 data = (GLvoid*)(byteVertices +(add)); \ 125 } \ 126 } else { \ 127 data = (GLvoid*)(afVertices+(add)); \ 128 } \ 129 130 #ifdef _WIN32 131 int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) 132 #else 133 int main(int argc, char **argv) 134 #endif 135 { 136 GLuint ui32Vbo = 0; // Vertex buffer object handle 137 GLuint ui32IndexVbo; 138 GLuint ui32Texture; 139 140 int nframes = 100; 141 bool immidateMode = false; 142 bool useIndices = true; 143 bool useTexture = false; 144 bool useCompTexture = false; 145 bool useConvertedType = true; 146 bool useFixed = false; 147 bool usePoints = false; 148 bool useCopy = false; 149 bool useSubCopy = false; 150 151 #ifdef _WIN32 152 HWND windowId = NULL; 153 #elif __linux__ 154 Window windowId = 0; 155 #elif __APPLE__ 156 void* windowId = NULL; 157 #endif 158 159 // // Inialize SDL window 160 // 161 if (SDL_Init(SDL_INIT_NOPARACHUTE | SDL_INIT_VIDEO)) { 162 fprintf(stderr,"SDL init failed: %s\n", SDL_GetError()); 163 return -1; 164 } 165 166 SDL_Surface *surface = SDL_SetVideoMode(WINDOW_WIDTH,WINDOW_HEIGHT, 32, SDL_HWSURFACE); 167 if (surface == NULL) { 168 fprintf(stderr,"Failed to set video mode: %s\n", SDL_GetError()); 169 return -1; 170 } 171 172 SDL_SysWMinfo wminfo; 173 memset(&wminfo, 0, sizeof(wminfo)); 174 SDL_GetWMInfo(&wminfo); 175 #ifdef _WIN32 176 windowId = wminfo.window; 177 #elif __linux__ 178 windowId = wminfo.info.x11.window; 179 #elif __APPLE__ 180 windowId = createGLView(wminfo.nsWindowPtr,0,0,WINDOW_WIDTH,WINDOW_HEIGHT); 181 182 #endif 183 184 int major,minor,num_config; 185 EGLConfig configs[150]; 186 EGLSurface egl_surface; 187 EGLContext ctx; 188 EGLDisplay d = eglGetDisplay(EGL_DEFAULT_DISPLAY); 189 eglInitialize(d,&major,&minor); 190 printf("DISPLAY == %p major =%d minor = %d\n",d,major,minor); 191 eglChooseConfig(d, attribute_list, configs, 150, &num_config); 192 printf("config returned %d\n",num_config); 193 egl_surface = eglCreateWindowSurface(d,configs[0],windowId,NULL); 194 printf("before creating context..\n"); 195 ctx = eglCreateContext(d,configs[0],EGL_NO_CONTEXT,NULL); 196 printf("SURFACE == %p CONTEXT == %p\n",egl_surface,ctx); 197 if(eglMakeCurrent(d,egl_surface,egl_surface,ctx)!= EGL_TRUE){ 198 printf("make current failed\n"); 199 return false; 200 } 201 printf("after make current\n"); 202 203 GLenum err = glGetError(); 204 if(err != GL_NO_ERROR) { 205 printf("error before drawing ->>> %d \n",err); 206 } else { 207 printf("no error before drawing\n"); 208 } 209 210 if (useTexture) { 211 212 glEnable(GL_TEXTURE_2D); 213 ui32Texture = 1; 214 glBindTexture(GL_TEXTURE_2D, ui32Texture); 215 GLenum err = glGetError(); 216 217 unsigned char *pixels = NULL; 218 if(useCompTexture) { 219 pixels = genPalette4_rgb8(TEX_WIDTH,TEX_HEIGHT,3); 220 glCompressedTexImage2D(GL_TEXTURE_2D,0,GL_PALETTE4_RGB8_OES,TEX_WIDTH,TEX_HEIGHT,0,3*16+TEX_WIDTH*TEX_HEIGHT/2,pixels); 221 222 } else { 223 pixels = genTexture(TEX_WIDTH, TEX_HEIGHT, 4); 224 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_WIDTH, TEX_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); 225 } 226 227 delete pixels; 228 229 err = glGetError(); 230 if(err != GL_NO_ERROR) 231 printf("error %d after image \n",err); 232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 233 err = glGetError(); 234 if(err != GL_NO_ERROR) 235 printf("error after min filter \n"); 236 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 237 err = glGetError(); 238 if(err != GL_NO_ERROR) 239 printf("error after mag filter \n"); 240 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 241 err = glGetError(); 242 if(err != GL_NO_ERROR) 243 printf("error after env mode \n"); 244 245 if(useCompTexture) { 246 pixels = genPalette4_rgb8(TEX_WIDTH,TEX_HEIGHT,1); 247 glCompressedTexSubImage2D(GL_TEXTURE_2D,0,TEX_WIDTH/4,TEX_HEIGHT/4,TEX_WIDTH/8,TEX_HEIGHT/8,GL_PALETTE4_RGB8_OES,3*16+(TEX_WIDTH*TEX_HEIGHT/128),pixels); 248 } else { 249 pixels = genRedTexture(TEX_WIDTH/8, TEX_HEIGHT/8, 4); 250 glTexSubImage2D(GL_TEXTURE_2D,0,TEX_WIDTH/4,TEX_HEIGHT/4,TEX_WIDTH/8,TEX_HEIGHT/8,GL_RGBA,GL_UNSIGNED_BYTE,pixels); 251 } 252 err = glGetError(); 253 if(err != GL_NO_ERROR) 254 printf("error %d after subimage \n",err); 255 delete pixels; 256 257 } 258 259 glClearColor(0.6f, 0.8f, 1.0f, 1.0f); // clear blue 260 261 float afVertices[] = { -0.4f,-0.4f,0.0f, // Position 262 1.0f,0.0f,0.0f,1.0f, // Color 263 0.0f, 0.0f, // texture 264 12.f, //point size 265 266 0.4f,-0.4f,0.0f, 267 0.0f,1.0f,0.0f,1.0f, 268 1.0f, 0.0f, 269 47.0f, 270 271 0.0f,0.4f,0.0f, 272 0.0f,0.0f,1.0f,1.0f, 273 0.5f, 1.0f, 274 14.0f 275 }; 276 277 #define MAX_T 1 278 #define MID_T 0 279 #define MIN_T 0 280 281 GLbyte byteVertices[] = { -1,-1,0, // Position 282 (GLbyte)255,0,0,(GLbyte)255, // Color 283 MIN_T, MIN_T, // texture 284 12, //point size 285 286 1,-1,0, 287 0,(GLbyte)255,0,(GLbyte)255, 288 MAX_T,MIN_T, 289 47, 290 291 0,1,0, 292 0,0,(GLbyte)255,(GLbyte)255, 293 MID_T, MAX_T, 294 14 295 }; 296 297 GLfixed fixedVertices[] = { F_to_X(-0.4f),F_to_X(-0.4f),F_to_X(0.0f), // Position 298 F_to_X(1.0f),F_to_X(0.0f),F_to_X(0.0f),F_to_X(1.0f), // Color 299 F_to_X(0.0f),F_to_X(0.0f), // texture 300 F_to_X(12.0f),//points size 301 302 F_to_X(0.4f),F_to_X(-0.4f),F_to_X(0.0f), 303 F_to_X(0.0f),F_to_X(1.0f),F_to_X(0.0f),F_to_X(1.0f), 304 F_to_X(1.0f),F_to_X( 0.0f), 305 F_to_X(30.0f), 306 307 F_to_X(0.0f),F_to_X(0.4f),F_to_X(0.0f), 308 F_to_X(0.0f),F_to_X(0.0f),F_to_X(1.0f),F_to_X(1.0f), 309 F_to_X(0.5f), F_to_X(1.0f), 310 F_to_X(30.0) 311 }; 312 313 unsigned short indices[] = { 2, 1, 0 }; 314 315 if (!immidateMode) { 316 glGenBuffers(1, &ui32Vbo); 317 ui32Vbo = 1; 318 printf("ui32Vbo = %d\n", ui32Vbo); 319 320 glBindBuffer(GL_ARRAY_BUFFER, ui32Vbo); 321 void* data = (void*)afVertices; 322 unsigned int uiSize = 3*(sizeof(float)*10); 323 if(useConvertedType){ 324 if(useFixed){ 325 data = (void*)fixedVertices; 326 } else { 327 data = (void*)byteVertices; 328 uiSize = 3*(sizeof(GLbyte)*10); 329 } 330 } 331 glBufferData(GL_ARRAY_BUFFER, uiSize,data, GL_STATIC_DRAW); 332 333 ui32IndexVbo = 2; 334 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ui32IndexVbo); 335 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); 336 } 337 338 // Draws a triangle for 800 frames 339 float angle = 0.0; 340 glMatrixMode(GL_PROJECTION); 341 glLoadIdentity(); 342 glMatrixMode(GL_MODELVIEW); 343 glLoadIdentity(); 344 345 GLvoid* arr = NULL; 346 GLenum type; 347 GLenum drawType; 348 GLenum colorType; 349 int size_of; 350 351 if(useConvertedType){ 352 if(useFixed) 353 { 354 arr = fixedVertices; 355 colorType = type = GL_FIXED; 356 size_of = sizeof(GLfixed); 357 } else { 358 arr = byteVertices; 359 colorType = GL_UNSIGNED_BYTE; 360 type = GL_BYTE; 361 size_of = sizeof(GLbyte); 362 } 363 }else { 364 arr = afVertices; 365 colorType = type = GL_FLOAT; 366 size_of = sizeof(float); 367 } 368 369 if(usePoints) 370 { 371 drawType = GL_POINTS; 372 } 373 else 374 drawType = GL_TRIANGLES; 375 376 GLvoid* data = NULL; 377 for (int i = 0; i < 100; i++) { 378 379 glClear(GL_COLOR_BUFFER_BIT); 380 glPushMatrix(); 381 glRotatef(angle, 0.0, 0.0, 1.0); 382 angle += 360.0 / nframes; 383 // Enable vertex arrays 384 glEnableClientState(GL_VERTEX_ARRAY); 385 if (immidateMode) { 386 glVertexPointer(3,type, size_of * 10, arr); 387 } else { 388 glVertexPointer(3,type, size_of * 10, 0); 389 } 390 391 // Set color data in the same way 392 glEnableClientState(GL_COLOR_ARRAY); 393 if (immidateMode) { 394 SWITCH_SOURCE(3) 395 glColorPointer(4, colorType, size_of * 10, data); 396 } else { 397 glColorPointer(4,colorType,size_of * 10, (GLvoid*) (size_of * 3) ); 398 } 399 if (useTexture) { 400 glEnableClientState(GL_TEXTURE_COORD_ARRAY); 401 if (immidateMode) { 402 SWITCH_SOURCE(7) 403 glTexCoordPointer(2, type, size_of * 10,data); 404 } else { 405 glTexCoordPointer(2, type, size_of * 10, (GLvoid*)(size_of * 7)); 406 } 407 } 408 if(usePoints) 409 { 410 glEnableClientState(GL_POINT_SIZE_ARRAY_OES); 411 if (immidateMode) { 412 SWITCH_SOURCE(9) 413 glPointSizePointerOES(type,size_of * 10,data); 414 } else { 415 glPointSizePointerOES(type,size_of * 10,(GLvoid*)(size_of * 9)); 416 } 417 } 418 419 if (useIndices) { 420 if (immidateMode) { 421 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 422 glDrawElements(drawType, 3, GL_UNSIGNED_SHORT, indices); 423 } else { 424 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ui32IndexVbo); 425 glDrawElements(drawType, 3, GL_UNSIGNED_SHORT, 0); 426 } 427 } else { 428 glDrawArrays(drawType, 0, 3); 429 } 430 431 GLenum err = glGetError(); 432 if(err != GL_NO_ERROR) 433 printf(" error %d has occured while drawing\n",err); 434 435 436 glPopMatrix(); 437 eglSwapBuffers(d,egl_surface); 438 439 if(useTexture && useCopy) 440 glCopyTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,0,0,256,256,0); 441 else if(useTexture && useSubCopy) 442 glCopyTexSubImage2D(GL_TEXTURE_2D,0,100,100,WINDOW_WIDTH/2,WINDOW_HEIGHT/2,50,50); 443 } 444 err = glGetError(); 445 if(err != GL_NO_ERROR) 446 printf("error ->>> %d \n",err); 447 eglDestroySurface(d,egl_surface); 448 eglDestroyContext(d,ctx); 449 450 // Just wait until the window is closed 451 SDL_Event ev; 452 while( SDL_WaitEvent(&ev) ) { 453 if (ev.type == SDL_QUIT) { 454 break; 455 } 456 } 457 return 0; 458 } 459 460 461