PageRenderTime 24ms CodeModel.GetById 2ms app.highlight 17ms RepoModel.GetById 2ms 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
 35#include "f3d_jni.h"

 36
 37/* Define the star structure */
 38typedef struct
 39{
 40    int r, g, b;   /* Stars Color */
 41    GLfloat dist;  /* Stars Distance From Center */
 42    GLfloat angle; /* Stars Current Angle */
 43} star;
 44
 45/* Number of stars */
 46#define NUM 50

 47star stars[NUM];       /* Make an array of size 'NUM' of stars */
 48
 49// draw all stars
 50static void drawScene() {
 51    GLfloat spin = 0;
 52
 53    GLfloat texcoords[4][2];
 54    GLfloat vertices[4][3];
 55    GLubyte indices[4] = {1, 0, 2, 3}; /* QUAD to TRIANGLE_STRIP conversion; */
 56
 57    glEnable(GL_TEXTURE_2D);
 58
 59	glDisable(GL_DEPTH_TEST);
 60
 61    glEnable(GL_BLEND);
 62    glBlendFunc( GL_SRC_ALPHA, GL_ONE ); //GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA, GL_ONE
 63
 64    /* Select Our Texture */
 65    glBindTexture(GL_TEXTURE_2D, texture->textureId);
 66
 67	glEnableClientState(GL_VERTEX_ARRAY);
 68    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 69
 70	/* Set pointers to vertices and texcoords */
 71    glVertexPointer(3, GL_FLOAT, 0, vertices);
 72    glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
 73
 74    /* Loop Through All The Stars */
 75    for (int loop = 0; loop < NUM; loop++) {
 76        /* Reset The View Before We Draw Each Star */
 77        glLoadIdentity();
 78        /* Zoom Into The Screen (Using The Value In 'zoom') */
 79        glTranslatef(0.0f, 0.0f, zoom);
 80
 81        /* Tilt The View (Using The Value In 'tilt') */
 82        glRotatef(tilt, 1.0f, 0.0f, 0.0f);
 83        /* Rotate To The Current Stars Angle */
 84        glRotatef(stars[loop].angle, 0.0f, 1.0f, 0.0f);
 85
 86        /* Move Forward On The X Plane */
 87        glTranslatef(stars[loop].dist, 0.0f, 0.0f);
 88
 89        /* Cancel The Current Stars Angle */
 90        glRotatef(-stars[loop].angle, 0.0f, 1.0f, 0.0f);
 91        /* Cancel The Screen Tilt */
 92        glRotatef(-tilt, 1.0f, 0.0f, 0.0f);
 93
 94        /* Twinkling Stars Enabled */
 95        if (is_twinkled) {
 96            /* Assign A Color Using Bytes */
 97            glColor4f((GLfloat)stars[(NUM-loop)-1].r/255.0f,
 98                      (GLfloat)stars[(NUM-loop)-1].g/255.0f,
 99                      (GLfloat)stars[(NUM-loop)-1].b/255.0f, 0.8f);
100
101            /* Fill texture and vertex arrays */
102            texcoords[0][0]=0.0f; texcoords[0][1]=0.0f;
103            vertices[0][0]=-1.0f; vertices[0][1]=-1.0f; vertices[0][2]=0.0f;
104            texcoords[1][0]=1.0f; texcoords[1][1]=0.0f;
105            vertices[1][0]=1.0f; vertices[1][1]=-1.0f; vertices[1][2]=0.0f;
106            texcoords[2][0]=1.0f; texcoords[2][1]=1.0f;
107            vertices[2][0]=1.0f; vertices[2][1]=1.0f; vertices[2][2]=0.0f;
108            texcoords[3][0]=0.0f; texcoords[3][1]=1.0f;
109            vertices[3][0]=-1.0f; vertices[3][1]=1.0f; vertices[3][2]=0.0f;
110
111            /* Draw one textured plane using two stripped triangles */
112            glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, indices);
113        }
114
115        /* Rotate The Star On The Z Axis */
116        glRotatef(spin, 0.0f, 0.0f, 1.0f);
117
118        /* Assign A Color Using Bytes */
119        glColor4f((GLfloat)stars[loop].r/255.0f,
120                  (GLfloat)stars[loop].g/255.0f,
121                  (GLfloat)stars[loop].b/255.0f, 0.8f);
122
123        /* Fill texture and vertex arrays */
124        texcoords[0][0]=0.0f; texcoords[0][1]=0.0f;
125        vertices[0][0]=-1.0f; vertices[0][1]=-1.0f; vertices[0][2]=0.0f;
126        texcoords[1][0]=1.0f; texcoords[1][1]=0.0f;
127        vertices[1][0]=1.0f; vertices[1][1]=-1.0f; vertices[1][2]=0.0f;
128        texcoords[2][0]=1.0f; texcoords[2][1]=1.0f;
129        vertices[2][0]=1.0f; vertices[2][1]=1.0f; vertices[2][2]=0.0f;
130        texcoords[3][0]=0.0f; texcoords[3][1]=1.0f;
131        vertices[3][0]=-1.0f; vertices[3][1]=1.0f; vertices[3][2]=0.0f;
132
133        /* Draw one textured plane using two stripped triangles */
134        glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, indices);
135
136        /* Used To Spin The Stars */
137        spin += 0.01f;
138        /* Changes The Angle Of A Star */
139        stars[loop].angle+=(float)loop/NUM;
140        /* Changes The Distance Of A Star */
141        stars[loop].dist-=0.01f;
142
143        /* Is The Star In The Middle Yet */
144        if (stars[loop].dist<0.0f) {
145            /* Move The Star 5 Units From The Center */
146            stars[loop].dist+=5.0f;
147            /* Give It A New Red Value */
148            stars[loop].r=rand()%256;
149            /* Give It A New Green Value */
150            stars[loop].g=rand()%256;
151            /* Give It A New Blue Value */
152            stars[loop].b=rand()%256;
153        }
154    }
155
156    glDisable(GL_BLEND);
157    glEnable(GL_DEPTH_TEST);
158
159    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
160    glDisableClientState(GL_VERTEX_ARRAY);
161}
162
163/*
164 * Class:     com_forget3d_demo_F3DStarsRenderer
165 * Method:    f3dStarsInit
166 * Signature: ()V
167 */
168void JNICALL Java_com_forget3d_demo_F3DStarsRenderer_f3dStarsInit(JNIEnv* env, jclass cls) {
169	__android_log_print(ANDROID_LOG_INFO, "Forget3D", "world->init()...");
170
171	World::release();
172    world = World::getInstance();
173    world->init();
174
175    texture = Image::loadTexture("/sdcard/star.bmp");
176	__android_log_print(ANDROID_LOG_INFO, "Forget3D", "load texture: %s ...", "/sdcard/star.bmp");
177
178	camera = world->getActiveCamera();
179    camera->setEye(0.0f, 0.0f, 15.0f);
180
181	/* Create A Loop That Goes Through All The Stars */
182    for (int loop=0; loop < NUM; loop++) {
183        /* Start All The Stars At Angle Zero */
184        stars[loop].angle=0.0f;
185
186        /* Calculate Distance From The Center */
187        stars[loop].dist=((float)loop/NUM)*5.0f;
188        /* Give star[loop] A Random Red Intensity */
189        stars[loop].r=rand() % 256;
190        /* Give star[loop] A Random Green Intensity */
191        stars[loop].g=rand() % 256;
192        /* Give star[loop] A Random Blue Intensity */
193        stars[loop].b=rand() % 256;
194    }
195
196	DELETEANDNULL(font, false);
197    font = new Font(16, 16, 24, 36, "/sdcard/font.bmp"); // /sdcard/game/forget3d/font.bmp, /dev/sample/font.bmp
198	__android_log_print(ANDROID_LOG_INFO, "Forget3D", "create Font: %s ...", "/sdcard/font.bmp");
199
200	is_initialized = true;
201	is_done = 1;
202
203	__android_log_print(ANDROID_LOG_INFO, "Forget3D", "start loop...");
204    is_done = 1;
205
206    gettimeofday(&timeNow, NULL);
207    i_time = CLOCK(timeNow);
208    sprintf(strFps, "Fps:%.2f", 0.0f);
209    printf("strFps: %s\n", strFps);
210}
211
212/*
213 * Class:     com_forget3d_demo_F3DStarsRenderer
214 * Method:    f3dStarsResize
215 * Signature: (II)V
216 */
217void JNICALL Java_com_forget3d_demo_F3DStarsRenderer_f3dStarsResize(JNIEnv* env, jclass cls, jint w, jint h) {
218    __android_log_print(ANDROID_LOG_INFO, "Forget3D", "call f3dStarsResize...");
219	width = w;
220	height = h;
221
222	if (is_initialized) {
223		world->resize(width, height);
224	}
225}
226
227/*
228 * Class:     com_forget3d_demo_F3DStarsRenderer
229 * Method:    f3dStarsRender
230 * Signature: ()V
231 */
232void JNICALL Java_com_forget3d_demo_F3DStarsRenderer_f3dStarsRender(JNIEnv* env, jclass cls) {
233    __android_log_print(ANDROID_LOG_INFO, "Forget3D", "call f3dStarsRender...");
234	if (!is_done)
235		return;
236
237	world->prepareRender();
238
239	drawScene();
240
241	//printf("strFps: %s\n", strFps);
242	font->drawString(4, 4, strFps);
243
244//	world->finishRender();
245
246	fps++;
247
248	gettimeofday(&timeNow, NULL);
249	interval = CLOCK(timeNow) - i_time;
250
251	if (((CLOCK(timeNow) - i_time) / 1000) % 2 == 0 && interval > 0) {
252		sprintf(strFps, "Fps:%.2f", fps * 1000.0f / interval);
253		__android_log_print(ANDROID_LOG_INFO, "Forget3D", "%s", strFps);
254	}
255}
256
257/*
258 * Class:     com_forget3d_demo_F3DStarsRenderer
259 * Method:    f3dStarsDone
260 * Signature: ()V
261 */
262void JNICALL Java_com_forget3d_demo_F3DStarsRenderer_f3dStarsDone(JNIEnv* env, jclass cls) {
263    __android_log_print(ANDROID_LOG_INFO, "Forget3D", "call f3dStarsDone...");
264
265    DELETEANDNULL(font, false);
266    World::release();
267}
268
269/*
270 * Class:     com_forget3d_demo_F3DStarsRenderer
271 * Method:    f3dStarsPause
272 * Signature: ()V
273 */
274void JNICALL Java_com_forget3d_demo_F3DStarsRenderer_f3dStarsPause(JNIEnv* env, jclass cls) {
275    __android_log_print(ANDROID_LOG_INFO, "Forget3D", "call f3dStarsPause...");
276
277	is_done = !is_done;
278}
279
280/*
281 * Class:     com_forget3d_demo_F3DStarsRenderer
282 * Method:    f3dStarsTwinkled
283 * Signature: ()V
284 */
285void JNICALL Java_com_forget3d_demo_F3DStarsRenderer_f3dStarsTwinkled(JNIEnv* env, jclass cls) {
286    __android_log_print(ANDROID_LOG_INFO, "Forget3D", "call f3dStarsTwinkled...");
287
288	is_twinkled = !is_twinkled;
289}