Rotating pyramid!
authorPaul Hunkin
Thu, 17 Jun 2010 23:04:16 +1200
changeset 4705190f043af37d
parent 4704 611d39792201
child 4706 12c9d4532b49
Rotating pyramid!
android/testproject/jni/app-android.c
     1.1 --- a/android/testproject/jni/app-android.c	Thu Jun 17 22:19:27 2010 +1200
     1.2 +++ b/android/testproject/jni/app-android.c	Thu Jun 17 23:04:16 2010 +1200
     1.3 @@ -7,6 +7,12 @@
     1.4  #include <android/log.h>
     1.5  #include <stdint.h>
     1.6  
     1.7 +#include <stdio.h>
     1.8 +#include <stdlib.h>
     1.9 +#include <math.h>
    1.10 +
    1.11 +#include "importgl.h"
    1.12 +
    1.13  /*******************************************************************************
    1.14                                 Globals
    1.15  *******************************************************************************/
    1.16 @@ -22,6 +28,106 @@
    1.17  	return (long)(now.tv_sec*1000 + now.tv_usec/1000);
    1.18  }
    1.19  
    1.20 +/**************************************
    1.21 +	gluperspective implementation
    1.22 +**************************************/
    1.23 +void gluPerspective(double fovy, double aspect, double zNear, double zFar){
    1.24 +	glMatrixMode(GL_PROJECTION);
    1.25 +	glLoadIdentity();
    1.26 +	double xmin, xmax, ymin, ymax;
    1.27 +	ymax = zNear * tan(fovy * M_PI / 360.0);
    1.28 +	ymin = -ymax;
    1.29 +	xmin = ymin * aspect;
    1.30 +	xmax = ymax * aspect;
    1.31 +	glFrustumf(xmin, xmax, ymin, ymax, zNear, zFar);
    1.32 +}
    1.33 +
    1.34 +
    1.35 +/**************************************
    1.36 +	  glulookat implementation
    1.37 +**************************************/
    1.38 +void gluLookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez,
    1.39 +          GLfloat centerx, GLfloat centery, GLfloat centerz,
    1.40 +          GLfloat upx, GLfloat upy, GLfloat upz)
    1.41 +{
    1.42 +    GLfloat m[16];
    1.43 +    GLfloat x[3], y[3], z[3];
    1.44 +    GLfloat mag;
    1.45 +    
    1.46 +    /* Make rotation matrix */
    1.47 +    
    1.48 +    /* Z vector */
    1.49 +    z[0] = eyex - centerx;
    1.50 +    z[1] = eyey - centery;
    1.51 +    z[2] = eyez - centerz;
    1.52 +    mag = sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
    1.53 +    if (mag) {          /* mpichler, 19950515 */
    1.54 +        z[0] /= mag;
    1.55 +        z[1] /= mag;
    1.56 +        z[2] /= mag;
    1.57 +    }
    1.58 +    
    1.59 +    /* Y vector */
    1.60 +    y[0] = upx;
    1.61 +    y[1] = upy;
    1.62 +    y[2] = upz;
    1.63 +    
    1.64 +    /* X vector = Y cross Z */
    1.65 +    x[0] = y[1] * z[2] - y[2] * z[1];
    1.66 +    x[1] = -y[0] * z[2] + y[2] * z[0];
    1.67 +    x[2] = y[0] * z[1] - y[1] * z[0];
    1.68 +    
    1.69 +    /* Recompute Y = Z cross X */
    1.70 +    y[0] = z[1] * x[2] - z[2] * x[1];
    1.71 +    y[1] = -z[0] * x[2] + z[2] * x[0];
    1.72 +    y[2] = z[0] * x[1] - z[1] * x[0];
    1.73 +    
    1.74 +    /* mpichler, 19950515 */
    1.75 +    /* cross product gives area of parallelogram, which is < 1.0 for
    1.76 +     * non-perpendicular unit-length vectors; so normalize x, y here
    1.77 +     */
    1.78 +    
    1.79 +    mag = sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
    1.80 +    if (mag) {
    1.81 +        x[0] /= mag;
    1.82 +        x[1] /= mag;
    1.83 +        x[2] /= mag;
    1.84 +    }
    1.85 +    
    1.86 +    mag = sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
    1.87 +    if (mag) {
    1.88 +        y[0] /= mag;
    1.89 +        y[1] /= mag;
    1.90 +        y[2] /= mag;
    1.91 +    }
    1.92 +    
    1.93 +#define M(row,col)  m[col*4+row]
    1.94 +    M(0, 0) = x[0];
    1.95 +    M(0, 1) = x[1];
    1.96 +    M(0, 2) = x[2];
    1.97 +    M(0, 3) = 0.0;
    1.98 +    M(1, 0) = y[0];
    1.99 +    M(1, 1) = y[1];
   1.100 +    M(1, 2) = y[2];
   1.101 +    M(1, 3) = 0.0;
   1.102 +    M(2, 0) = z[0];
   1.103 +    M(2, 1) = z[1];
   1.104 +    M(2, 2) = z[2];
   1.105 +    M(2, 3) = 0.0;
   1.106 +    M(3, 0) = 0.0;
   1.107 +    M(3, 1) = 0.0;
   1.108 +    M(3, 2) = 0.0;
   1.109 +    M(3, 3) = 1.0;
   1.110 +#undef M
   1.111 +    glMultMatrixf(m);
   1.112 +    
   1.113 +    /* Translate Eye to Origin */
   1.114 +    glTranslatef(-eyex, -eyey, -eyez);
   1.115 +    
   1.116 +}
   1.117 +
   1.118 +
   1.119 +
   1.120  /*******************************************************************************
   1.121                        Initialize the graphics state
   1.122  *******************************************************************************/
   1.123 @@ -31,6 +137,29 @@
   1.124  
   1.125  	gAppAlive    = 1;
   1.126  	sDemoStopped = 0;
   1.127 +
   1.128 +	__android_log_print(ANDROID_LOG_INFO, "SDL", "Entry point");
   1.129 +
   1.130 +
   1.131 +	 /* Enable smooth shading */
   1.132 +    glShadeModel( GL_SMOOTH );
   1.133 +
   1.134 +    /* Set the background black */
   1.135 +    glClearColor( 1.0f, 0.0f, 0.0f, 0.0f );
   1.136 +
   1.137 +    /* Depth buffer setup */
   1.138 +    //glClearDepth( 1.0f );
   1.139 +
   1.140 +    /* Enables Depth Testing */
   1.141 +    glEnable( GL_DEPTH_TEST );
   1.142 +
   1.143 +    /* The Type Of Depth Test To Do */
   1.144 +    glDepthFunc( GL_LEQUAL );
   1.145 +
   1.146 +    /* Really Nice Perspective Calculations */
   1.147 +    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
   1.148 +
   1.149 +
   1.150  }
   1.151  
   1.152  /*******************************************************************************
   1.153 @@ -44,6 +173,35 @@
   1.154  	sWindowWidth  = w;
   1.155  	sWindowHeight = h;
   1.156  	__android_log_print(ANDROID_LOG_INFO, "SDL", "resize w=%d h=%d", w, h);
   1.157 +
   1.158 +
   1.159 +
   1.160 +	 /* Height / width ration */
   1.161 +    GLfloat ratio;
   1.162 + 
   1.163 +    /* Protect against a divide by zero */
   1.164 +   if ( h == 0 )
   1.165 +	h = 1;
   1.166 +
   1.167 +    ratio = ( GLfloat )w / ( GLfloat )h;
   1.168 +
   1.169 +    /* Setup our viewport. */
   1.170 +    glViewport( 0, 0, ( GLsizei )w, ( GLsizei )h );
   1.171 +
   1.172 +    /* change to the projection matrix and set our viewing volume. */
   1.173 +    glMatrixMode( GL_PROJECTION );
   1.174 +    glLoadIdentity( );
   1.175 +
   1.176 +    /* Set our perspective */
   1.177 +    gluPerspective( 45.0f, ratio, 0.1f, 100.0f );
   1.178 +
   1.179 +    /* Make sure we're chaning the model view and not the projection */
   1.180 +    glMatrixMode( GL_MODELVIEW );
   1.181 +
   1.182 +    /* Reset The View */
   1.183 +    glLoadIdentity( );
   1.184 +
   1.185 +
   1.186  }
   1.187  
   1.188  /*******************************************************************************
   1.189 @@ -55,6 +213,8 @@
   1.190  	//shut down the app
   1.191  
   1.192  	importGLDeinit();
   1.193 +
   1.194 +	__android_log_print(ANDROID_LOG_INFO, "SDL", "Finalize");
   1.195  }
   1.196  
   1.197  /*******************************************************************************
   1.198 @@ -65,15 +225,129 @@
   1.199  	sDemoStopped = !sDemoStopped;
   1.200  	if (sDemoStopped) {
   1.201  		//we paused
   1.202 +		__android_log_print(ANDROID_LOG_INFO, "SDL", "Pause");
   1.203  	} else {
   1.204  		//we resumed
   1.205 +		__android_log_print(ANDROID_LOG_INFO, "SDL", "Resume");
   1.206  	}
   1.207  }
   1.208  
   1.209  /*******************************************************************************
   1.210                       Render the next frame
   1.211  *******************************************************************************/
   1.212 +
   1.213 +const GLbyte vertex []=
   1.214 +{
   1.215 +	0,1,0,
   1.216 +	-1,0,0,
   1.217 +	1,0,0
   1.218 +};
   1.219 +
   1.220 +const GLubyte color []=
   1.221 +{
   1.222 +	255,0,0,
   1.223 +	0,255,0,
   1.224 +	0,0,255
   1.225 +};
   1.226 +
   1.227 +int iRot = 0;
   1.228 +int Frames = 0;
   1.229 +
   1.230 +
   1.231 +static void prepareFrame(int width, int height)
   1.232 +{
   1.233 +    glViewport(0, 0, width, height);
   1.234 +
   1.235 +    glClearColorx(0,0,0,255);
   1.236 +    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
   1.237 +
   1.238 +    glMatrixMode(GL_PROJECTION);
   1.239 +    glLoadIdentity();
   1.240 +    gluPerspective(45, (float)width / height, 0.5f, 150);
   1.241 +
   1.242 +    glMatrixMode(GL_MODELVIEW);
   1.243 +
   1.244 +    glLoadIdentity();
   1.245 +}
   1.246 +
   1.247  void Java_org_libsdl_android_TestRenderer_nativeRender( JNIEnv*  env )
   1.248  {    
   1.249  	//TODO: Render here
   1.250 +
   1.251 +	prepareFrame(sWindowWidth, sWindowHeight);
   1.252 +	
   1.253 +	//Camera
   1.254 +	gluLookAt(0,0,5, 0,0,0, 0,1,0);
   1.255 +			
   1.256 +	//Draw a triangle
   1.257 +	//glRotatef(iRot, 0, 1, 0);
   1.258 +
   1.259 +	glRotatef( Frames % 360, 0.0f, 1.0f, 0.0f );
   1.260 +
   1.261 +
   1.262 +	glEnableClientState (GL_VERTEX_ARRAY);
   1.263 +	glEnableClientState (GL_COLOR_ARRAY);
   1.264 +	
   1.265 +	/* Rotate The Triangle On The Y axis ( NEW ) */
   1.266 +    glRotatef( Frames % 360, 0.0f, 1.0f, 0.0f );
   1.267 +
   1.268 +    /* GLES variant of drawing a triangle */
   1.269 +    const GLfloat triVertices[][9] = {
   1.270 +      {     /* Front Triangle */
   1.271 +	 0.0f,  1.0f,  0.0f,               /* Top Of Triangle               */
   1.272 +	-1.0f, -1.0f,  1.0f,               /* Left Of Triangle              */
   1.273 +	 1.0f, -1.0f,  1.0f                /* Right Of Triangle             */
   1.274 +      }, {  /* Right Triangle */
   1.275 +	 0.0f,  1.0f,  0.0f,               /* Top Of Triangle               */
   1.276 +	 1.0f, -1.0f,  1.0f,               /* Left Of Triangle              */
   1.277 +	 1.0f, -1.0f, -1.0f                /* Right Of Triangle             */
   1.278 +      }, {  /* Back Triangle */
   1.279 +	 0.0f,  1.0f,  0.0f,               /* Top Of Triangle               */
   1.280 +	 1.0f, -1.0f, -1.0f,               /* Left Of Triangle              */
   1.281 +	-1.0f, -1.0f, -1.0f                /* Right Of Triangle             */
   1.282 +      }, {  /* Left Triangle */
   1.283 +	 0.0f,  1.0f,  0.0f,               /* Top Of Triangle               */
   1.284 +	-1.0f, -1.0f, -1.0f,               /* Left Of Triangle              */
   1.285 +	-1.0f, -1.0f,  1.0f                /* Right Of Triangle             */
   1.286 +      }
   1.287 +    };
   1.288 +
   1.289 +    /* unlike GL, GLES does not support RGB. We have to use RGBA instead */
   1.290 +    const GLfloat triColors[][12] = {
   1.291 +      {     /* Front triangle */
   1.292 +        1.0f, 0.0f, 0.0f, 1.0f,            /* Red                           */
   1.293 +	0.0f, 1.0f, 0.0f, 1.0f,            /* Green                         */
   1.294 +	0.0f, 0.0f, 1.0f, 1.0f             /* Blue                          */
   1.295 +      }, {  /* Right triangle */
   1.296 +        1.0f, 0.0f, 0.0f, 1.0f,            /* Red                           */
   1.297 +	0.0f, 0.0f, 1.0f, 1.0f,            /* Blue                          */
   1.298 +	0.0f, 1.0f, 0.0f, 1.0f             /* Green                         */
   1.299 +      }, {  /* Back triangle */
   1.300 +        1.0f, 0.0f, 0.0f, 1.0f,            /* Red                           */
   1.301 +	0.0f, 1.0f, 0.0f, 1.0f,            /* Green                         */
   1.302 +	0.0f, 0.0f, 1.0f, 1.0f             /* Blue                          */
   1.303 +      }, {  /* Left triangle */
   1.304 +        1.0f, 0.0f, 0.0f, 1.0f,            /* Red                           */
   1.305 +	0.0f, 0.0f, 1.0f, 1.0f,            /* Blue                          */
   1.306 +	0.0f, 1.0f, 0.0f, 1.0f             /* Green                         */
   1.307 +      }
   1.308 +    };
   1.309 +
   1.310 +    glEnableClientState(GL_COLOR_ARRAY);
   1.311 +
   1.312 +    int tri=0;
   1.313 +
   1.314 +    /* Loop through all Triangles */
   1.315 +    for(tri=0;tri<sizeof(triVertices)/(9*sizeof(GLfloat));tri++) 
   1.316 +    {
   1.317 +      glVertexPointer(3, GL_FLOAT, 0, triVertices[tri]);
   1.318 +      glColorPointer(4, GL_FLOAT, 0, triColors[tri]);
   1.319 +      
   1.320 +      glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
   1.321 +    }
   1.322 +		
   1.323 +    //__android_log_print(ANDROID_LOG_INFO, "SDL", "render %d", Frames++);
   1.324 +
   1.325 +    Frames++;
   1.326 +
   1.327  }