/TheElements/jni/gl.c

http://thelements.googlecode.com/ · C · 103 lines · 55 code · 24 blank · 24 comment · 2 complexity · e6b46b95df178c2f7f1cd4897565e606 MD5 · raw file

  1. /*
  2. * gl.c
  3. * --------------------------
  4. * Defines the gl rendering and initialization
  5. * functions appInit, appDeinit, and appRender.
  6. */
  7. #include "gl.h"
  8. static unsigned int textureID;
  9. // Called from the app framework. is onSurfaceCreated
  10. void appInit()
  11. {
  12. glShadeModel(GL_SMOOTH);
  13. glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
  14. glClearDepthf(1.0f);
  15. glEnable(GL_DEPTH_TEST);
  16. glDepthFunc(GL_LEQUAL);
  17. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  18. //Generate the new texture
  19. glGenTextures(1, &textureID);
  20. //Bind the texture
  21. glBindTexture(GL_TEXTURE_2D, textureID);
  22. glEnable(GL_TEXTURE_2D);
  23. //Set tex params
  24. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  25. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  26. //Different possible texture parameters, e.g
  27. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  28. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  29. //Generate the tex image
  30. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 512, 1024, 0, GL_RGB, GL_UNSIGNED_BYTE, colors);
  31. //Disable tex (clean up)
  32. glDisable(GL_TEXTURE_2D);
  33. }
  34. // Called from the app framework.
  35. void appDeinit()
  36. {
  37. }
  38. // Called from the app framework.
  39. /* The tick is current time in milliseconds, width and height
  40. * are the image dimensions to be rendered.
  41. */
  42. void appRender()
  43. {
  44. float vertices[] =
  45. { 0.0f, 0.0f, 512.0f, 0.0f, 0.0f, 1024.0f, 512.0f, 1024.0f };
  46. float texture[] =
  47. { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f };
  48. unsigned char indices[] =
  49. { 0, 1, 3, 0, 3, 2 };
  50. UpdateView();
  51. glMatrixMode(GL_PROJECTION);
  52. glLoadIdentity();
  53. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  54. if (flipped == 0)
  55. {
  56. glOrthof(0.0f, (float) maxx, (float) maxy, 0.0f, 1.0f, -1.0f); //--Device
  57. }
  58. else
  59. {
  60. glOrthof(0.0f, (float) maxx, 0.0f, (float) -maxy, 1.0f, -1.0f); //--Emulator
  61. }
  62. //Draw the texture stuff --------------------------------------------------------------
  63. glEnable(GL_TEXTURE_2D);
  64. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 512, maxy, GL_RGB,
  65. GL_UNSIGNED_BYTE, colors);
  66. //Enable the vertex and coord arrays
  67. glEnableClientState(GL_VERTEX_ARRAY);
  68. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  69. //Set the pointers
  70. glVertexPointer(2, GL_FLOAT, 0, vertices);
  71. glTexCoordPointer(2, GL_FLOAT, 0, texture);
  72. //Actually draw the rectangle with the text on it
  73. glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);
  74. //Disable the vertex and coord arrays (clean up)
  75. glDisableClientState(GL_VERTEX_ARRAY);
  76. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  77. //Disable tex (clean up)
  78. glDisable(GL_TEXTURE_2D);
  79. }