PageRenderTime 37ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/android/jni/src/stars.cpp

http://forget3d.googlecode.com/
C++ | 289 lines | 137 code | 52 blank | 100 comment | 9 complexity | 829c44e679298264d1b8b788b1415e59 MD5 | raw file
  1. /*****************************************************************************
  2. * Copyright (C) 2009 The Forget3D Project by Martin Foo (i25ffz@hotmail.com)
  3. * ALL RIGHTS RESERVED
  4. *
  5. * License I
  6. * Permission to use, copy, modify, and distribute this software for
  7. * any purpose and WITHOUT a fee is granted under following requirements:
  8. * - You make no money using this software.
  9. * - The authors and/or this software is credited in your software or any
  10. * work based on this software.
  11. *
  12. * Licence II
  13. * Permission to use, copy, modify, and distribute this software for
  14. * any purpose and WITH a fee is granted under following requirements:
  15. * - As soon as you make money using this software, you have to pay a
  16. * licence fee. Until this point of time, you can use this software
  17. * without a fee.
  18. * Please contact Martin Foo (i25ffz@hotmail.com) for further details.
  19. * - The authors and/or this software is credited in your software or any
  20. * work based on this software.
  21. *
  22. * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  23. * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  24. * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  25. * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHORS
  26. * BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT, SPECIAL, INCIDENTAL,
  27. * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER,
  28. * INCLUDING WITHOUT LIMITATION, LOSS OF PROFIT, LOSS OF USE, SAVINGS OR
  29. * REVENUE, OR THE CLAIMS OF THIRD PARTIES, WHETHER OR NOT THE AUTHORS HAVE
  30. * BEEN ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  31. * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  32. * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  33. *****************************************************************************/
  34. #include "f3d_jni.h"
  35. /* Define the star structure */
  36. typedef struct
  37. {
  38. int r, g, b; /* Stars Color */
  39. GLfloat dist; /* Stars Distance From Center */
  40. GLfloat angle; /* Stars Current Angle */
  41. } star;
  42. /* Number of stars */
  43. #define NUM 50
  44. star stars[NUM]; /* Make an array of size 'NUM' of stars */
  45. // draw all stars
  46. static void drawScene() {
  47. GLfloat spin = 0;
  48. GLfloat texcoords[4][2];
  49. GLfloat vertices[4][3];
  50. GLubyte indices[4] = {1, 0, 2, 3}; /* QUAD to TRIANGLE_STRIP conversion; */
  51. glEnable(GL_TEXTURE_2D);
  52. glDisable(GL_DEPTH_TEST);
  53. glEnable(GL_BLEND);
  54. glBlendFunc( GL_SRC_ALPHA, GL_ONE ); //GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA, GL_ONE
  55. /* Select Our Texture */
  56. glBindTexture(GL_TEXTURE_2D, texture->textureId);
  57. glEnableClientState(GL_VERTEX_ARRAY);
  58. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  59. /* Set pointers to vertices and texcoords */
  60. glVertexPointer(3, GL_FLOAT, 0, vertices);
  61. glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
  62. /* Loop Through All The Stars */
  63. for (int loop = 0; loop < NUM; loop++) {
  64. /* Reset The View Before We Draw Each Star */
  65. glLoadIdentity();
  66. /* Zoom Into The Screen (Using The Value In 'zoom') */
  67. glTranslatef(0.0f, 0.0f, zoom);
  68. /* Tilt The View (Using The Value In 'tilt') */
  69. glRotatef(tilt, 1.0f, 0.0f, 0.0f);
  70. /* Rotate To The Current Stars Angle */
  71. glRotatef(stars[loop].angle, 0.0f, 1.0f, 0.0f);
  72. /* Move Forward On The X Plane */
  73. glTranslatef(stars[loop].dist, 0.0f, 0.0f);
  74. /* Cancel The Current Stars Angle */
  75. glRotatef(-stars[loop].angle, 0.0f, 1.0f, 0.0f);
  76. /* Cancel The Screen Tilt */
  77. glRotatef(-tilt, 1.0f, 0.0f, 0.0f);
  78. /* Twinkling Stars Enabled */
  79. if (is_twinkled) {
  80. /* Assign A Color Using Bytes */
  81. glColor4f((GLfloat)stars[(NUM-loop)-1].r/255.0f,
  82. (GLfloat)stars[(NUM-loop)-1].g/255.0f,
  83. (GLfloat)stars[(NUM-loop)-1].b/255.0f, 0.8f);
  84. /* Fill texture and vertex arrays */
  85. texcoords[0][0]=0.0f; texcoords[0][1]=0.0f;
  86. vertices[0][0]=-1.0f; vertices[0][1]=-1.0f; vertices[0][2]=0.0f;
  87. texcoords[1][0]=1.0f; texcoords[1][1]=0.0f;
  88. vertices[1][0]=1.0f; vertices[1][1]=-1.0f; vertices[1][2]=0.0f;
  89. texcoords[2][0]=1.0f; texcoords[2][1]=1.0f;
  90. vertices[2][0]=1.0f; vertices[2][1]=1.0f; vertices[2][2]=0.0f;
  91. texcoords[3][0]=0.0f; texcoords[3][1]=1.0f;
  92. vertices[3][0]=-1.0f; vertices[3][1]=1.0f; vertices[3][2]=0.0f;
  93. /* Draw one textured plane using two stripped triangles */
  94. glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, indices);
  95. }
  96. /* Rotate The Star On The Z Axis */
  97. glRotatef(spin, 0.0f, 0.0f, 1.0f);
  98. /* Assign A Color Using Bytes */
  99. glColor4f((GLfloat)stars[loop].r/255.0f,
  100. (GLfloat)stars[loop].g/255.0f,
  101. (GLfloat)stars[loop].b/255.0f, 0.8f);
  102. /* Fill texture and vertex arrays */
  103. texcoords[0][0]=0.0f; texcoords[0][1]=0.0f;
  104. vertices[0][0]=-1.0f; vertices[0][1]=-1.0f; vertices[0][2]=0.0f;
  105. texcoords[1][0]=1.0f; texcoords[1][1]=0.0f;
  106. vertices[1][0]=1.0f; vertices[1][1]=-1.0f; vertices[1][2]=0.0f;
  107. texcoords[2][0]=1.0f; texcoords[2][1]=1.0f;
  108. vertices[2][0]=1.0f; vertices[2][1]=1.0f; vertices[2][2]=0.0f;
  109. texcoords[3][0]=0.0f; texcoords[3][1]=1.0f;
  110. vertices[3][0]=-1.0f; vertices[3][1]=1.0f; vertices[3][2]=0.0f;
  111. /* Draw one textured plane using two stripped triangles */
  112. glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, indices);
  113. /* Used To Spin The Stars */
  114. spin += 0.01f;
  115. /* Changes The Angle Of A Star */
  116. stars[loop].angle+=(float)loop/NUM;
  117. /* Changes The Distance Of A Star */
  118. stars[loop].dist-=0.01f;
  119. /* Is The Star In The Middle Yet */
  120. if (stars[loop].dist<0.0f) {
  121. /* Move The Star 5 Units From The Center */
  122. stars[loop].dist+=5.0f;
  123. /* Give It A New Red Value */
  124. stars[loop].r=rand()%256;
  125. /* Give It A New Green Value */
  126. stars[loop].g=rand()%256;
  127. /* Give It A New Blue Value */
  128. stars[loop].b=rand()%256;
  129. }
  130. }
  131. glDisable(GL_BLEND);
  132. glEnable(GL_DEPTH_TEST);
  133. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  134. glDisableClientState(GL_VERTEX_ARRAY);
  135. }
  136. /*
  137. * Class: com_forget3d_demo_F3DStarsRenderer
  138. * Method: f3dStarsInit
  139. * Signature: ()V
  140. */
  141. void JNICALL Java_com_forget3d_demo_F3DStarsRenderer_f3dStarsInit(JNIEnv* env, jclass cls) {
  142. __android_log_print(ANDROID_LOG_INFO, "Forget3D", "world->init()...");
  143. World::release();
  144. world = World::getInstance();
  145. world->init();
  146. texture = Image::loadTexture("/sdcard/star.bmp");
  147. __android_log_print(ANDROID_LOG_INFO, "Forget3D", "load texture: %s ...", "/sdcard/star.bmp");
  148. camera = world->getActiveCamera();
  149. camera->setEye(0.0f, 0.0f, 15.0f);
  150. /* Create A Loop That Goes Through All The Stars */
  151. for (int loop=0; loop < NUM; loop++) {
  152. /* Start All The Stars At Angle Zero */
  153. stars[loop].angle=0.0f;
  154. /* Calculate Distance From The Center */
  155. stars[loop].dist=((float)loop/NUM)*5.0f;
  156. /* Give star[loop] A Random Red Intensity */
  157. stars[loop].r=rand() % 256;
  158. /* Give star[loop] A Random Green Intensity */
  159. stars[loop].g=rand() % 256;
  160. /* Give star[loop] A Random Blue Intensity */
  161. stars[loop].b=rand() % 256;
  162. }
  163. DELETEANDNULL(font, false);
  164. font = new Font(16, 16, 24, 36, "/sdcard/font.bmp"); // /sdcard/game/forget3d/font.bmp, /dev/sample/font.bmp
  165. __android_log_print(ANDROID_LOG_INFO, "Forget3D", "create Font: %s ...", "/sdcard/font.bmp");
  166. is_initialized = true;
  167. is_done = 1;
  168. __android_log_print(ANDROID_LOG_INFO, "Forget3D", "start loop...");
  169. is_done = 1;
  170. gettimeofday(&timeNow, NULL);
  171. i_time = CLOCK(timeNow);
  172. sprintf(strFps, "Fps:%.2f", 0.0f);
  173. printf("strFps: %s\n", strFps);
  174. }
  175. /*
  176. * Class: com_forget3d_demo_F3DStarsRenderer
  177. * Method: f3dStarsResize
  178. * Signature: (II)V
  179. */
  180. void JNICALL Java_com_forget3d_demo_F3DStarsRenderer_f3dStarsResize(JNIEnv* env, jclass cls, jint w, jint h) {
  181. __android_log_print(ANDROID_LOG_INFO, "Forget3D", "call f3dStarsResize...");
  182. width = w;
  183. height = h;
  184. if (is_initialized) {
  185. world->resize(width, height);
  186. }
  187. }
  188. /*
  189. * Class: com_forget3d_demo_F3DStarsRenderer
  190. * Method: f3dStarsRender
  191. * Signature: ()V
  192. */
  193. void JNICALL Java_com_forget3d_demo_F3DStarsRenderer_f3dStarsRender(JNIEnv* env, jclass cls) {
  194. __android_log_print(ANDROID_LOG_INFO, "Forget3D", "call f3dStarsRender...");
  195. if (!is_done)
  196. return;
  197. world->prepareRender();
  198. drawScene();
  199. //printf("strFps: %s\n", strFps);
  200. font->drawString(4, 4, strFps);
  201. // world->finishRender();
  202. fps++;
  203. gettimeofday(&timeNow, NULL);
  204. interval = CLOCK(timeNow) - i_time;
  205. if (((CLOCK(timeNow) - i_time) / 1000) % 2 == 0 && interval > 0) {
  206. sprintf(strFps, "Fps:%.2f", fps * 1000.0f / interval);
  207. __android_log_print(ANDROID_LOG_INFO, "Forget3D", "%s", strFps);
  208. }
  209. }
  210. /*
  211. * Class: com_forget3d_demo_F3DStarsRenderer
  212. * Method: f3dStarsDone
  213. * Signature: ()V
  214. */
  215. void JNICALL Java_com_forget3d_demo_F3DStarsRenderer_f3dStarsDone(JNIEnv* env, jclass cls) {
  216. __android_log_print(ANDROID_LOG_INFO, "Forget3D", "call f3dStarsDone...");
  217. DELETEANDNULL(font, false);
  218. World::release();
  219. }
  220. /*
  221. * Class: com_forget3d_demo_F3DStarsRenderer
  222. * Method: f3dStarsPause
  223. * Signature: ()V
  224. */
  225. void JNICALL Java_com_forget3d_demo_F3DStarsRenderer_f3dStarsPause(JNIEnv* env, jclass cls) {
  226. __android_log_print(ANDROID_LOG_INFO, "Forget3D", "call f3dStarsPause...");
  227. is_done = !is_done;
  228. }
  229. /*
  230. * Class: com_forget3d_demo_F3DStarsRenderer
  231. * Method: f3dStarsTwinkled
  232. * Signature: ()V
  233. */
  234. void JNICALL Java_com_forget3d_demo_F3DStarsRenderer_f3dStarsTwinkled(JNIEnv* env, jclass cls) {
  235. __android_log_print(ANDROID_LOG_INFO, "Forget3D", "call f3dStarsTwinkled...");
  236. is_twinkled = !is_twinkled;
  237. }