textures.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. **
  3. ** Copyright 2006, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <EGL/egl.h>
  20. #include <GLES/gl.h>
  21. #include <GLES/glext.h>
  22. #include <WindowSurface.h>
  23. #include <EGLUtils.h>
  24. using namespace android;
  25. int main(int argc, char** argv)
  26. {
  27. EGLint configAttribs[] = {
  28. EGL_DEPTH_SIZE, 0,
  29. EGL_NONE
  30. };
  31. EGLint majorVersion;
  32. EGLint minorVersion;
  33. EGLContext context;
  34. EGLConfig config;
  35. EGLSurface surface;
  36. EGLint w, h;
  37. EGLDisplay dpy;
  38. WindowSurface windowSurface;
  39. EGLNativeWindowType window = windowSurface.getSurface();
  40. dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  41. eglInitialize(dpy, &majorVersion, &minorVersion);
  42. status_t err = EGLUtils::selectConfigForNativeWindow(
  43. dpy, configAttribs, window, &config);
  44. if (err) {
  45. fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n");
  46. return 0;
  47. }
  48. surface = eglCreateWindowSurface(dpy, config, window, NULL);
  49. context = eglCreateContext(dpy, config, NULL, NULL);
  50. eglMakeCurrent(dpy, surface, surface, context);
  51. eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
  52. eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
  53. GLint dim = w<h ? w : h;
  54. GLint crop[4] = { 0, 4, 4, -4 };
  55. glBindTexture(GL_TEXTURE_2D, 0);
  56. glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
  57. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  58. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  59. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  60. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  61. glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  62. glEnable(GL_TEXTURE_2D);
  63. glColor4f(1,1,1,1);
  64. // packing is always 4
  65. uint8_t t8[] = {
  66. 0x00, 0x55, 0x00, 0x55,
  67. 0xAA, 0xFF, 0xAA, 0xFF,
  68. 0x00, 0x55, 0x00, 0x55,
  69. 0xAA, 0xFF, 0xAA, 0xFF };
  70. uint16_t t16[] = {
  71. 0x0000, 0x5555, 0x0000, 0x5555,
  72. 0xAAAA, 0xFFFF, 0xAAAA, 0xFFFF,
  73. 0x0000, 0x5555, 0x0000, 0x5555,
  74. 0xAAAA, 0xFFFF, 0xAAAA, 0xFFFF };
  75. uint16_t t5551[] = {
  76. 0x0000, 0xFFFF, 0x0000, 0xFFFF,
  77. 0xFFFF, 0x0000, 0xFFFF, 0x0000,
  78. 0x0000, 0xFFFF, 0x0000, 0xFFFF,
  79. 0xFFFF, 0x0000, 0xFFFF, 0x0000 };
  80. uint32_t t32[] = {
  81. 0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFFFF0000,
  82. 0xFF00FF00, 0xFFFF0000, 0xFF000000, 0xFF0000FF,
  83. 0xFF00FFFF, 0xFF00FF00, 0x00FF00FF, 0xFFFFFF00,
  84. 0xFF000000, 0xFFFF00FF, 0xFF00FFFF, 0xFFFFFFFF
  85. };
  86. glClear(GL_COLOR_BUFFER_BIT);
  87. glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 4, 4, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, t8);
  88. glDrawTexiOES(0, 0, 0, dim/2, dim/2);
  89. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, t16);
  90. glDrawTexiOES(dim/2, 0, 0, dim/2, dim/2);
  91. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, t16);
  92. glDrawTexiOES(0, dim/2, 0, dim/2, dim/2);
  93. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE, t32);
  94. glDrawTexiOES(dim/2, dim/2, 0, dim/2, dim/2);
  95. eglSwapBuffers(dpy, surface);
  96. sleep(2); // so you have a chance to admire it
  97. return 0;
  98. }