opengl problem works on droid but not droid eris and others - android
This GlRenderer works fine on the moto droid, but does not work well at all on droid eris or other android phones does anyone know why?
package com.ntu.way2fungames.spacehockeybase;
import java.io.DataInputStream;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import com.ntu.way2fungames.LoadFloatArray;
import com.ntu.way2fungames.OGLTriReader;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Handler;
import android.os.Message;
public class GlRenderer extends Thread implements Renderer {
private float drawArray[];
private float yoff;
private float yoff2;
private long lastRenderTime;
private float[] yoffs= new float[10];
int Width;
int Height;
private float[] pixelVerts = new float[] {
+.0f,+.0f,2,
+.5f,+.5f,0,
+.5f,-.5f,0,
+.0f,+.0f,2,
+.5f,-.5f,0,
-.5f,-.5f,0,
+.0f,+.0f,2,
-.5f,-.5f,0,
-.5f,+.5f,0,
+.0f,+.0f,2,
-.5f,+.5f,0,
+.5f,+.5f,0,
};
#Override
public void run() {
}
private float[] arenaWalls = new float[] {
8.00f,2.00f,1f,2f,2f,1f,2.00f,8.00f,1f,8.00f,2.00f,1f,2.00f,8.00f,1f,8.00f,8.00f,1f,
2.00f,8.00f,1f,2f,2f,1f,0.00f,0.00f,0f,2.00f,8.00f,1f,0.00f,0.00f,0f,0.00f,10.00f,0f,
8.00f,8.00f,1f,2.00f,8.00f,1f,0.00f,10.00f,0f,8.00f,8.00f,1f,0.00f,10.00f,0f,10.00f,10.00f,0f,
2f,2f,1f,8.00f,2.00f,1f,10.00f,0.00f,0f,2f,2f,1f,10.00f,0.00f,0f,0.00f,0.00f,0f,
8.00f,2.00f,1f,8.00f,8.00f,1f,10.00f,10.00f,0f,8.00f,2.00f,1f,10.00f,10.00f,0f,10.00f,0.00f,0f,
10.00f,10.00f,0f,0.00f,10.00f,0f,0.00f,0.00f,0f,10.00f,10.00f,0f,0.00f,0.00f,0f,10.00f,0.00f,0f,
8.00f,6.00f,1f,8.00f,4.00f,1f,122f,4.00f,1f,8.00f,6.00f,1f,122f,4.00f,1f,122f,6.00f,1f,
8.00f,6.00f,1f,122f,6.00f,1f,120f,7.00f,0f,8.00f,6.00f,1f,120f,7.00f,0f,10.00f,7.00f,0f,
122f,4.00f,1f,8.00f,4.00f,1f,10.00f,3.00f,0f,122f,4.00f,1f,10.00f,3.00f,0f,120f,3.00f,0f,
480f,10.00f,0f,470f,10.00f,0f,470f,0.00f,0f,480f,10.00f,0f,470f,0.00f,0f,480f,0.00f,0f,
478f,2.00f,1f,478f,8.00f,1f,480f,10.00f,0f,478f,2.00f,1f,480f,10.00f,0f,480f,0.00f,0f,
472f,2f,1f,478f,2.00f,1f,480f,0.00f,0f,472f,2f,1f,480f,0.00f,0f,470f,0.00f,0f,
478f,8.00f,1f,472f,8.00f,1f,470f,10.00f,0f,478f,8.00f,1f,470f,10.00f,0f,480f,10.00f,0f,
472f,8.00f,1f,472f,2f,1f,470f,0.00f,0f,472f,8.00f,1f,470f,0.00f,0f,470f,10.00f,0f,
478f,2.00f,1f,472f,2f,1f,472f,8.00f,1f,478f,2.00f,1f,472f,8.00f,1f,478f,8.00f,1f,
478f,846f,1f,472f,846f,1f,472f,852f,1f,478f,846f,1f,472f,852f,1f,478f,852f,1f,
472f,852f,1f,472f,846f,1f,470f,844f,0f,472f,852f,1f,470f,844f,0f,470f,854f,0f,
478f,852f,1f,472f,852f,1f,470f,854f,0f,478f,852f,1f,470f,854f,0f,480f,854f,0f,
472f,846f,1f,478f,846f,1f,480f,844f,0f,472f,846f,1f,480f,844f,0f,470f,844f,0f,
478f,846f,1f,478f,852f,1f,480f,854f,0f,478f,846f,1f,480f,854f,0f,480f,844f,0f,
480f,854f,0f,470f,854f,0f,470f,844f,0f,480f,854f,0f,470f,844f,0f,480f,844f,0f,
10.00f,854f,0f,0.00f,854f,0f,0.00f,844f,0f,10.00f,854f,0f,0.00f,844f,0f,10.00f,844f,0f,
8.00f,846f,1f,8.00f,852f,1f,10.00f,854f,0f,8.00f,846f,1f,10.00f,854f,0f,10.00f,844f,0f,
2f,846f,1f,8.00f,846f,1f,10.00f,844f,0f,2f,846f,1f,10.00f,844f,0f,0.00f,844f,0f,
8.00f,852f,1f,2.00f,852f,1f,0.00f,854f,0f,8.00f,852f,1f,0.00f,854f,0f,10.00f,854f,0f,
2.00f,852f,1f,2f,846f,1f,0.00f,844f,0f,2.00f,852f,1f,0.00f,844f,0f,0.00f,854f,0f,
8.00f,846f,1f,2f,846f,1f,2.00f,852f,1f,8.00f,846f,1f,2.00f,852f,1f,8.00f,852f,1f,
6f,846f,1f,4f,846f,1f,4f,8f,1f,6f,846f,1f,4f,8f,1f,6f,8f,1f,
6f,846f,1f,6f,8f,1f,7f,10f,0f,6f,846f,1f,7f,10f,0f,7f,844f,0f,
4f,8f,1f,4f,846f,1f,3f,844f,0f,4f,8f,1f,3f,844f,0f,3f,10f,0f,
474f,8f,1f,474f,846f,1f,473f,844f,0f,474f,8f,1f,473f,844f,0f,473f,10f,0f,
476f,846f,1f,476f,8f,1f,477f,10f,0f,476f,846f,1f,477f,10f,0f,477f,844f,0f,
476f,846f,1f,474f,846f,1f,474f,8f,1f,476f,846f,1f,474f,8f,1f,476f,8f,1f,
130f,10.00f,0f,120f,10.00f,0f,120f,0.00f,0f,130f,10.00f,0f,120f,0.00f,0f,130f,0.00f,0f,
128f,2.00f,1f,128f,8.00f,1f,130f,10.00f,0f,128f,2.00f,1f,130f,10.00f,0f,130f,0.00f,0f,
122f,2f,1f,128f,2.00f,1f,130f,0.00f,0f,122f,2f,1f,130f,0.00f,0f,120f,0.00f,0f,
128f,8.00f,1f,122f,8.00f,1f,120f,10.00f,0f,128f,8.00f,1f,120f,10.00f,0f,130f,10.00f,0f,
122f,8.00f,1f,122f,2f,1f,120f,0.00f,0f,122f,8.00f,1f,120f,0.00f,0f,120f,10.00f,0f,
128f,2.00f,1f,122f,2f,1f,122f,8.00f,1f,128f,2.00f,1f,122f,8.00f,1f,128f,8.00f,1f,
352f,8.00f,1f,358f,8.00f,1f,358f,2.00f,1f,352f,8.00f,1f,358f,2.00f,1f,352f,2.00f,1f,
358f,2.00f,1f,358f,8.00f,1f,360f,10.00f,0f,358f,2.00f,1f,360f,10.00f,0f,360f,0.00f,0f,
352f,2.00f,1f,358f,2.00f,1f,360f,0.00f,0f,352f,2.00f,1f,360f,0.00f,0f,350f,0.00f,0f,
358f,8.00f,1f,352f,8.00f,1f,350f,10.00f,0f,358f,8.00f,1f,350f,10.00f,0f,360f,10.00f,0f,
352f,8.00f,1f,352f,2.00f,1f,350f,0.00f,0f,352f,8.00f,1f,350f,0.00f,0f,350f,10.00f,0f,
350f,0.00f,0f,360f,0.00f,0f,360f,10.00f,0f,350f,0.00f,0f,360f,10.00f,0f,350f,10.00f,0f,
358f,6.00f,1f,472f,6.00f,1f,470f,7.00f,0f,358f,6.00f,1f,470f,7.00f,0f,360f,7.00f,0f,
472f,4.00f,1f,358f,4.00f,1f,360f,3.00f,0f,472f,4.00f,1f,360f,3.00f,0f,470f,3.00f,0f,
472f,4.00f,1f,472f,6.00f,1f,358f,6.00f,1f,472f,4.00f,1f,358f,6.00f,1f,358f,4.00f,1f,
472f,848f,1f,472f,850f,1f,358f,850f,1f,472f,848f,1f,358f,850f,1f,358f,848f,1f,
472f,848f,1f,358f,848f,1f,360f,847f,0f,472f,848f,1f,360f,847f,0f,470f,847f,0f,
358f,850f,1f,472f,850f,1f,470f,851f,0f,358f,850f,1f,470f,851f,0f,360f,851f,0f,
350f,844f,0f,360f,844f,0f,360f,854f,0f,350f,844f,0f,360f,854f,0f,350f,854f,0f,
352f,852f,1f,352f,846f,1f,350f,844f,0f,352f,852f,1f,350f,844f,0f,350f,854f,0f,
358f,852f,1f,352f,852f,1f,350f,854f,0f,358f,852f,1f,350f,854f,0f,360f,854f,0f,
352f,846f,1f,358f,846f,1f,360f,844f,0f,352f,846f,1f,360f,844f,0f,350f,844f,0f,
358f,846f,1f,358f,852f,1f,360f,854f,0f,358f,846f,1f,360f,854f,0f,360f,844f,0f,
352f,852f,1f,358f,852f,1f,358f,846f,1f,352f,852f,1f,358f,846f,1f,352f,846f,1f,
128f,846f,1f,122f,846f,1f,122f,852f,1f,128f,846f,1f,122f,852f,1f,128f,852f,1f,
122f,852f,1f,122f,846f,1f,120f,844f,0f,122f,852f,1f,120f,844f,0f,120f,854f,0f,
128f,852f,1f,122f,852f,1f,120f,854f,0f,128f,852f,1f,120f,854f,0f,130f,854f,0f,
122f,846f,1f,128f,846f,1f,130f,844f,0f,122f,846f,1f,130f,844f,0f,120f,844f,0f,
128f,846f,1f,128f,852f,1f,130f,854f,0f,128f,846f,1f,130f,854f,0f,130f,844f,0f,
130f,854f,0f,120f,854f,0f,120f,844f,0f,130f,854f,0f,120f,844f,0f,130f,844f,0f,
122f,848f,1f,8f,848f,1f,10f,847f,0f,122f,848f,1f,10f,847f,0f,120f,847f,0f,
8f,850f,1f,122f,850f,1f,120f,851f,0f,8f,850f,1f,120f,851f,0f,10f,851f,0f,
8f,850f,1f,8f,848f,1f,122f,848f,1f,8f,850f,1f,122f,848f,1f,122f,850f,1f,
10f,847f,0f,120f,847f,0f,124.96f,829.63f,-0.50f,10f,847f,0f,124.96f,829.63f,-0.50f,19.51f,829.63f,-0.50f,
130f,844f,0f,130f,854f,0f,134.55f,836.34f,-0.50f,130f,844f,0f,134.55f,836.34f,-0.50f,134.55f,826.76f,-0.50f,
350f,844f,0f,350f,854f,0f,345.45f,836.34f,-0.50f,350f,844f,0f,345.45f,836.34f,-0.50f,345.45f,826.76f,-0.50f,
360f,847f,0f,470f,847f,0f,460.49f,829.63f,-0.50f,360f,847f,0f,460.49f,829.63f,-0.50f,355.04f,829.63f,-0.50f,
470f,7.00f,0f,360f,7.00f,0f,355.04f,24.37f,-0.50f,470f,7.00f,0f,355.04f,24.37f,-0.50f,460.49f,24.37f,-0.50f,
350f,10.00f,0f,350f,0.00f,0f,345.45f,17.66f,-0.50f,350f,10.00f,0f,345.45f,17.66f,-0.50f,345.45f,27.24f,-0.50f,
130f,10.00f,0f,130f,0.00f,0f,134.55f,17.66f,-0.50f,130f,10.00f,0f,134.55f,17.66f,-0.50f,134.55f,27.24f,-0.50f,
473f,844f,0f,473f,10f,0f,463.36f,27.24f,-0.50f,473f,844f,0f,463.36f,27.24f,-0.50f,463.36f,826.76f,-0.50f,
7f,10f,0f,7f,844f,0f,16.64f,826.76f,-0.50f,7f,10f,0f,16.64f,826.76f,-0.50f,16.64f,27.24f,-0.50f,
120f,7.00f,0f,10.00f,7.00f,0f,19.51f,24.37f,-0.50f,120f,7.00f,0f,19.51f,24.37f,-0.50f,124.96f,24.37f,-0.50f,
120f,7.00f,0f,130f,10.00f,0f,134.55f,27.24f,-0.50f,120f,7.00f,0f,134.55f,27.24f,-0.50f,124.96f,24.37f,-0.50f,
10.00f,7.00f,0f,7f,10f,0f,16.64f,27.24f,-0.50f,10.00f,7.00f,0f,16.64f,27.24f,-0.50f,19.51f,24.37f,-0.50f,
350f,10.00f,0f,360f,7.00f,0f,355.04f,24.37f,-0.50f,350f,10.00f,0f,355.04f,24.37f,-0.50f,345.45f,27.24f,-0.50f,
473f,10f,0f,470f,7.00f,0f,460.49f,24.37f,-0.50f,473f,10f,0f,460.49f,24.37f,-0.50f,463.36f,27.24f,-0.50f,
473f,844f,0f,470f,847f,0f,460.49f,829.63f,-0.50f,473f,844f,0f,460.49f,829.63f,-0.50f,463.36f,826.76f,-0.50f,
360f,847f,0f,350f,844f,0f,345.45f,826.76f,-0.50f,360f,847f,0f,345.45f,826.76f,-0.50f,355.04f,829.63f,-0.50f,
130f,844f,0f,120f,847f,0f,124.96f,829.63f,-0.50f,130f,844f,0f,124.96f,829.63f,-0.50f,134.55f,826.76f,-0.50f,
7f,844f,0f,10f,847f,0f,19.51f,829.63f,-0.50f,7f,844f,0f,19.51f,829.63f,-0.50f,16.64f,826.76f,-0.50f,
19.51f,829.63f,-0.50f,124.96f,829.63f,-0.50f,136.47f,789.37f,-2f,19.51f,829.63f,-0.50f,136.47f,789.37f,-2f,41.56f,789.37f,-2f,
134.55f,826.76f,-0.50f,134.55f,836.34f,-0.50f,145.09f,795.41f,-2f,134.55f,826.76f,-0.50f,145.09f,795.41f,-2f,145.09f,786.78f,-2f,
345.45f,826.76f,-0.50f,345.45f,836.34f,-0.50f,334.91f,795.41f,-2f,345.45f,826.76f,-0.50f,334.91f,795.41f,-2f,334.91f,786.78f,-2f,
355.04f,829.63f,-0.50f,460.49f,829.63f,-0.50f,438.44f,789.37f,-2f,355.04f,829.63f,-0.50f,438.44f,789.37f,-2f,343.53f,789.37f,-2f,
460.49f,24.37f,-0.50f,355.04f,24.37f,-0.50f,343.53f,64.63f,-2f,460.49f,24.37f,-0.50f,343.53f,64.63f,-2f,438.44f,64.63f,-2f,
345.45f,27.24f,-0.50f,345.45f,17.66f,-0.50f,334.91f,58.59f,-2f,345.45f,27.24f,-0.50f,334.91f,58.59f,-2f,334.91f,67.22f,-2f,
134.55f,27.24f,-0.50f,134.55f,17.66f,-0.50f,145.09f,58.59f,-2f,134.55f,27.24f,-0.50f,145.09f,58.59f,-2f,145.09f,67.22f,-2f,
463.36f,826.76f,-0.50f,463.36f,27.24f,-0.50f,441.03f,67.22f,-2f,463.36f,826.76f,-0.50f,441.03f,67.22f,-2f,441.03f,786.78f,-2f,
16.64f,27.24f,-0.50f,16.64f,826.76f,-0.50f,38.97f,786.78f,-2f,16.64f,27.24f,-0.50f,38.97f,786.78f,-2f,38.97f,67.22f,-2f,
124.96f,24.37f,-0.50f,19.51f,24.37f,-0.50f,41.56f,64.63f,-2f,124.96f,24.37f,-0.50f,41.56f,64.63f,-2f,136.47f,64.63f,-2f,
124.96f,24.37f,-0.50f,134.55f,27.24f,-0.50f,145.09f,67.22f,-2f,124.96f,24.37f,-0.50f,145.09f,67.22f,-2f,136.47f,64.63f,-2f,
19.51f,24.37f,-0.50f,16.64f,27.24f,-0.50f,38.97f,67.22f,-2f,19.51f,24.37f,-0.50f,38.97f,67.22f,-2f,41.56f,64.63f,-2f,
345.45f,27.24f,-0.50f,355.04f,24.37f,-0.50f,343.53f,64.63f,-2f,345.45f,27.24f,-0.50f,343.53f,64.63f,-2f,334.91f,67.22f,-2f,
463.36f,27.24f,-0.50f,460.49f,24.37f,-0.50f,438.44f,64.63f,-2f,463.36f,27.24f,-0.50f,438.44f,64.63f,-2f,441.03f,67.22f,-2f,
463.36f,826.76f,-0.50f,460.49f,829.63f,-0.50f,438.44f,789.37f,-2f,463.36f,826.76f,-0.50f,438.44f,789.37f,-2f,441.03f,786.78f,-2f,
355.04f,829.63f,-0.50f,345.45f,826.76f,-0.50f,334.91f,786.78f,-2f,355.04f,829.63f,-0.50f,334.91f,786.78f,-2f,343.53f,789.37f,-2f,
134.55f,826.76f,-0.50f,124.96f,829.63f,-0.50f,136.47f,789.37f,-2f,134.55f,826.76f,-0.50f,136.47f,789.37f,-2f,145.09f,786.78f,-2f,
16.64f,826.76f,-0.50f,19.51f,829.63f,-0.50f,41.56f,789.37f,-2f,16.64f,826.76f,-0.50f,41.56f,789.37f,-2f,38.97f,786.78f,-2f,
};
private float[] backgroundData = new float[] {
// # ,Scale, Speed,
300 , 1.05f, .001f,
150 , 1.07f, .002f,
075 , 1.10f, .003f,
040 , 1.12f, .006f,
20 , 1.15f, .012f,
10 , 1.25f, .025f,
05 , 1.50f, .050f,
3 , 2.00f, .100f,
2 , 3.00f, .200f,
};
private float[] triangleCoords = new float[] {
0, -25, 0,
-.75f, -1, 0,
+.75f, -1, 0,
0, +2, 0,
-.99f, -1, 0,
.99f, -1, 0,
};
private float[] triangleColors = new float[] {
1.0f, 1.0f, 1.0f, 0.05f,
1.0f, 1.0f, 1.0f, 0.5f,
1.0f, 1.0f, 1.0f, 0.5f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 0.5f,
1.0f, 1.0f, 1.0f, 0.5f,
};
private float[] drawArray2;
private FloatBuffer drawBuffer2;
private float[] colorArray2;
private static FloatBuffer colorBuffer;
private static FloatBuffer triangleBuffer;
private static FloatBuffer quadBuffer;
private static FloatBuffer drawBuffer;
private float[] backgroundVerts;
private FloatBuffer backgroundVertsWrapped;
private float[] backgroundColors;
private Buffer backgroundColorsWraped;
private FloatBuffer backgroundColorsWrapped;
private FloatBuffer arenaWallsWrapped;
private FloatBuffer arenaColorsWrapped;
private FloatBuffer arena2VertsWrapped;
private FloatBuffer arena2ColorsWrapped;
private long wallHitStartTime;
private int wallHitDrawTime;
private FloatBuffer pixelVertsWrapped;
private float[] wallHit;
private FloatBuffer pixelColorsWrapped;
//private float[] pitVerts;
private Resources lResources;
private FloatBuffer pitVertsWrapped;
private FloatBuffer pitColorsWrapped;
private boolean arena2;
private long lastStartTime;
private long startTime;
private int state=1;
private long introEndTime;
protected long introTotalTime =8000;
protected long introStartTime;
private boolean initDone= false;
private static int stateIntro = 0;
private static int stateGame = 1;
public GlRenderer(spacehockey nspacehockey) {
lResources = nspacehockey.getResources();
nspacehockey.SetHandlerToGLRenderer(new Handler() {
#Override
public void handleMessage(Message m) {
if (m.what ==0){
wallHit = m.getData().getFloatArray("wall hit");
wallHitStartTime =System.currentTimeMillis();
wallHitDrawTime = 1000;
}else if (m.what ==1){
//state = stateIntro;
introEndTime= System.currentTimeMillis()+introTotalTime ;
introStartTime = System.currentTimeMillis();
}
}});
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearColor(.01f, .01f, .01f, .1f);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}
private float SumOfStrideI(float[] data, int offset, int stride) {
int sum= 0;
for (int i=offset;i<data.length-1;i=i+stride){
sum = (int) (data[i]+sum);
}
return sum;
}
public void onDrawFrame(GL10 gl) {
if (state== stateIntro){DrawIntro(gl);}
if (state== stateGame){DrawGame(gl);}
}
private void DrawIntro(GL10 gl) {
startTime = System.currentTimeMillis();
if (startTime< introEndTime){
float ptd = (float)(startTime- introStartTime)/(float)introTotalTime;
float ptl = 1-ptd;
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);//dont move
gl.glMatrixMode(GL10.GL_MODELVIEW);
int setVertOff = 0;
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, backgroundColorsWrapped);
for (int i = 0; i < backgroundData.length / 3; i = i + 1) {
int setoff = i * 3;
int setVertLen = (int) backgroundData[setoff];
yoffs[i] = (backgroundData[setoff + 2]*(90+(ptl*250))) + yoffs[i];
if (yoffs[i] > Height) {yoffs[i] = 0;}
gl.glPushMatrix();
//gl.glTranslatef(0, -(Height/2), 0);
//gl.glScalef(1f, 1f+(ptl*2), 1f);
//gl.glTranslatef(0, +(Height/2), 0);
gl.glTranslatef(0, yoffs[i], i+60);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, backgroundVertsWrapped);
gl.glDrawArrays(GL10.GL_TRIANGLES, (setVertOff * 2 * 3) - 0, (setVertLen * 2 * 3) - 1);
gl.glTranslatef(0, -Height, 0);
gl.glDrawArrays(GL10.GL_TRIANGLES, (setVertOff * 2 * 3) - 0, (setVertLen * 2 * 3) - 1);
setVertOff = (int) (setVertOff + setVertLen);
gl.glPopMatrix();
}
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}else{state = stateGame;}
}
private void DrawGame(GL10 gl) {
lastStartTime = startTime;
startTime = System.currentTimeMillis();
long moveTime = startTime-lastStartTime;
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);//dont move
gl.glMatrixMode(GL10.GL_MODELVIEW);
int setVertOff = 0;
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, backgroundColorsWrapped);
for (int i = 0; i < backgroundData.length / 3; i = i + 1) {
int setoff = i * 3;
int setVertLen = (int) backgroundData[setoff];
yoffs[i] = (backgroundData[setoff + 2]*moveTime) + yoffs[i];
if (yoffs[i] > Height) {yoffs[i] = 0;}
gl.glPushMatrix();
gl.glTranslatef(0, yoffs[i], i+60);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, backgroundVertsWrapped);
gl.glDrawArrays(GL10.GL_TRIANGLES, (setVertOff * 6) - 0, (setVertLen *6) - 1);
gl.glTranslatef(0, -Height, 0);
gl.glDrawArrays(GL10.GL_TRIANGLES, (setVertOff * 6) - 0, (setVertLen *6) - 1);
setVertOff = (int) (setVertOff + setVertLen);
gl.glPopMatrix();
}
//arena frame
gl.glPushMatrix();
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, arenaWallsWrapped);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, arenaColorsWrapped);
gl.glColor4f(.1f, .5f, 1f, 1f);
gl.glTranslatef(0, 0, 50);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, (int)(arenaWalls.length / 3));
gl.glPopMatrix();
//arena2 frame
if (arena2 == true){
gl.glLoadIdentity();
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, pitVertsWrapped);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, pitColorsWrapped);
gl.glTranslatef(0, -Height, 40);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, (int)(pitVertsWrapped.capacity() / 3));
}
if (wallHitStartTime != 0) {
float timeRemaining = (wallHitStartTime + wallHitDrawTime)-System.currentTimeMillis();
if (timeRemaining>0) {
gl.glPushMatrix();
float percentDone = 1-(timeRemaining/wallHitDrawTime);
gl.glLoadIdentity();
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, pixelVertsWrapped);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, pixelColorsWrapped);
gl.glTranslatef(wallHit[0], wallHit[1], 0);
gl.glScalef(8, Height*percentDone, 0);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 12);
gl.glPopMatrix();
} else {
wallHitStartTime = 0;
}
}
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
public void init(GL10 gl) {
if (arena2 == true) {
AssetManager assetManager = lResources.getAssets();
try {
// byte[] ba = {111,111};
DataInputStream Dis = new DataInputStream(assetManager
.open("arena2.ogl"));
pitVertsWrapped = LoadFloatArray.FromDataInputStream(Dis);
pitColorsWrapped = MakeFakeLighting(pitVertsWrapped.array(),
.25f, .50f, 1f, 200, .5f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if ((Height != 854) || (Width != 480)) {
arenaWalls = ScaleFloats(arenaWalls, Width / 480f, Height / 854f);
}
arenaWallsWrapped = FloatBuffer.wrap(arenaWalls);
arenaColorsWrapped = MakeFakeLighting(arenaWalls, .03f, .16f, .33f,
.33f, 3);
pixelVertsWrapped = FloatBuffer.wrap(pixelVerts);
pixelColorsWrapped = MakeFakeLighting(pixelVerts, .03f, .16f, .33f,
.10f, 20);
initDone=true;
}
public void onSurfaceChanged(GL10 gl, int nwidth, int nheight) {
Width= nwidth;
Height = nheight;
// avoid division by zero
if (Height == 0)
Height = 1;
// draw on the entire screen
gl.glViewport(0, 0, Width, Height);
// setup projection matrix
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0, Width, Height, 0, 100, -100);
// gl.glOrthof(-nwidth*2, nwidth*2, nheight*2,-nheight*2, 100, -100);
// GLU.gluPerspective(gl, 180.0f, (float)nwidth / (float)nheight,
// 1000.0f, -1000.0f);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
System.gc();
if (initDone == false){
SetupStars();
init(gl);
}
}
public void SetupStars(){
backgroundVerts = new float[(int) SumOfStrideI(backgroundData,0,3)*triangleCoords.length];
backgroundColors = new float[(int) SumOfStrideI(backgroundData,0,3)*triangleColors.length];
int iii=0;
int vc=0;
float ascale=1;
for (int i=0;i<backgroundColors.length-1;i=i+1){
if (iii==0){ascale = (float) Math.random();}
if (vc==3){
backgroundColors[i]= (float) (triangleColors[iii]*(ascale));
}else if(vc==2){
backgroundColors[i]= (float) (triangleColors[iii]-(Math.random()*.2));
}else{
backgroundColors[i]= (float) (triangleColors[iii]-(Math.random()*.3));
}
iii=iii+1;if (iii> triangleColors.length-1){iii=0;}
vc=vc+1; if (vc>3){vc=0;}
}
int ii=0;
int i =0;
int set =0;
while(ii<backgroundVerts.length-1){
float scale = (float) backgroundData[(set*3)+1];
int length= (int) backgroundData[(set*3)];
for (i=0;i<length;i=i+1){
if (set ==0){
AddVertsToArray(ScaleFloats(triangleCoords, scale,scale*.25f), backgroundVerts, (float)(Math.random()*Width),(float) (Math.random()*Height), ii);
}else{
AddVertsToArray(ScaleFloats(triangleCoords, scale), backgroundVerts, (float)(Math.random()*Width),(float) (Math.random()*Height), ii);}
ii=ii+triangleCoords.length;
}
set=set+1;
}
backgroundVertsWrapped = FloatBuffer.wrap(backgroundVerts);
backgroundColorsWrapped = FloatBuffer.wrap(backgroundColors);
}
public void AddVertsToArray(float[] sva,float[]dva,float ox,float oy,int start){
//x
for (int i=0;i<sva.length;i=i+3){
if((start+i)<dva.length){dva[start+i]= sva[i]+ox;}
}
//y
for (int i=1;i<sva.length;i=i+3){
if((start+i)<dva.length){dva[start+i]= sva[i]+oy;}
}
//z
for (int i=2;i<sva.length;i=i+3){
if((start+i)<dva.length){dva[start+i]= sva[i];}
}
}
public FloatBuffer MakeFakeLighting(float[] sa,float r, float g,float b,float a,float multby){
float[] da = new float[((sa.length/3)*4)];
int vertex=0;
for (int i=0;i<sa.length;i=i+3){
if (sa[i+2]>=1){
da[(vertex*4)+0]= r*multby*sa[i+2];
da[(vertex*4)+1]= g*multby*sa[i+2];
da[(vertex*4)+2]= b*multby*sa[i+2];
da[(vertex*4)+3]= a*multby*sa[i+2];
}else if (sa[i+2]<=-1){
float divisor = (multby*(-sa[i+2]));
da[(vertex*4)+0]= r / divisor;
da[(vertex*4)+1]= g / divisor;
da[(vertex*4)+2]= b / divisor;
da[(vertex*4)+3]= a / divisor;
}else{
da[(vertex*4)+0]= r;
da[(vertex*4)+1]= g;
da[(vertex*4)+2]= b;
da[(vertex*4)+3]= a;
}
vertex = vertex+1;
}
return FloatBuffer.wrap(da);
}
public float[] ScaleFloats(float[] va,float s){
float[] reta= new float[va.length];
for (int i=0;i<va.length;i=i+1){
reta[i]=va[i]*s;
}
return reta;
}
public float[] ScaleFloats(float[] va,float sx,float sy){
float[] reta= new float[va.length];
int cnt = 0;
for (int i=0;i<va.length;i=i+1){
if (cnt==0){reta[i]=va[i]*sx;}
else if (cnt==1){reta[i]=va[i]*sy;}
else if (cnt==2){reta[i]=va[i];}
cnt = cnt +1;if (cnt>2){cnt=0;}
}
return reta;
}
}
Related
Cast remote display android Nullpointer
When we click on change color button nullpointer exception in CubeRender class. java.lang.NullPointerException: Attempt to invoke virtual method 'void CubeRenderer.changeColor()' on a null object reference java class public class CubeRenderer implements GLSurfaceView.Renderer { private static final String TAG = "CubeRenderer"; private static final float ANGLE_INCREMENT = 1.2f; private static final boolean CALCULATE_FPS = false; private Cube mCube; private float mAngle; private boolean mChangeColor; private long mLastTime; private long mFpsCounter; protected final float[] mMMatrix = new float[16]; protected final float[] mMVMatrix = new float[16]; protected final float[] mMVPMatrix = new float[16]; protected final float[] mProjectionMatrix = new float[16]; protected final float[] mViewMatrix = new float[16]; protected final float[] mRotationMatrix = new float[16]; public void onDrawFrame(GL10 unused) { if (CALCULATE_FPS) { long currentTime = SystemClock.uptimeMillis(); if (mLastTime == 0) { mLastTime = currentTime; } else { mFpsCounter++; long diffTime = currentTime - mLastTime; if (diffTime >= 1000) { Log.d(TAG, "fps=" + mFpsCounter); mFpsCounter = 0; mLastTime = currentTime; } } } GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); // Set the camera position Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -10, 0f, 0f, 0f, 0f, 1.0f, 0.0f); // Configure matrices for first cube Matrix.setIdentityM(mMMatrix, 0); Matrix.translateM(mMMatrix, 0, 0.0f, -0.5f, -1.5f); Matrix.setRotateM(mRotationMatrix, 0, 2 * mAngle, 0.0f, 1.0f, 1.0f); Matrix.multiplyMM(mMMatrix, 0, mRotationMatrix, 0, mMMatrix, 0); Matrix.multiplyMM(mMVMatrix, 0, mViewMatrix, 0, mMMatrix, 0); Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVMatrix, 0); mCube.draw(mMVPMatrix, mChangeColor); // Configure matrices for second cube Matrix.setIdentityM(mMMatrix, 0); Matrix.translateM(mMMatrix, 0, 0.0f, 2.0f, 0.0f); Matrix.setRotateM(mRotationMatrix, 0, -mAngle, 0.0f, 1.0f, 1.0f); Matrix.multiplyMM(mMMatrix, 0, mRotationMatrix, 0, mMMatrix, 0); Matrix.multiplyMM(mMVMatrix, 0, mViewMatrix, 0, mMMatrix, 0); Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVMatrix, 0); mCube.draw(mMVPMatrix, mChangeColor); mAngle += ANGLE_INCREMENT; } public void onSurfaceChanged(GL10 unused, int width, int height) { float ratio = (float) width / height; GLES20.glViewport(0, 0, width, height); // Configure perspective with field of view float fov = 30.0f; float near = 1.0f; float far = 100.0f; float top = (float) Math.tan(fov * Math.PI / 360.0f) * near; float bottom = -top; float left = ratio * bottom; float right = ratio * top; Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far); } public void onSurfaceCreated(GL10 unused, EGLConfig config) { // Set background color GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Depth handling GLES20.glEnable(GLES20.GL_DEPTH_TEST); GLES20.glDepthFunc(GLES20.GL_LEQUAL); // Set anti-aliasing GLES20.glEnable(GLES20.GL_BLEND); GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); // Important to initialize the graphics on the GL thread mCube = new Cube(); } /** * Utility method to allow the user to change the cube color. */ public void changeColor() { mChangeColor = !mChangeColor; } } Accessing CubeRenderer class method mCubeRenderer.changeColor() exact place where null pointer exception need to handle public class PresentationService extends CastRemoteDisplayLocalService { private static final String TAG = "PresentationService"; // First screen private CastPresentation mPresentation; private MediaPlayer mMediaPlayer; private CubeRenderer mCubeRenderer; #Override public void onCreate() { super.onCreate(); // Audio mMediaPlayer = MediaPlayer.create(this, R.raw.sound); mMediaPlayer.setVolume((float) 0.1, (float) 0.1); mMediaPlayer.setLooping(true); } #Override public void onCreatePresentation(Display display) { createPresentation(display); } #Override public void onDismissPresentation() { dismissPresentation(); } private void dismissPresentation() { if (mPresentation != null) { mMediaPlayer.stop(); mPresentation.dismiss(); mPresentation = null; } } private void createPresentation(Display display) { dismissPresentation(); mPresentation = new TVPresentation(this, display); try { mPresentation.show(); mMediaPlayer.start(); } catch (WindowManager.InvalidDisplayException ex) { Log.e(TAG, "Unable to show presentation, display was removed.", ex); dismissPresentation(); } } /** * Utility method to allow the user to change the cube color. */ public void changeColor() { mCubeRenderer.changeColor(); } }
Problem solved mCubeRenderer= new CubeRenderer(); need to add this line in PresentationService class
Why is my list of objects all the same?
I've been making lists of objects for a while now to make multiple objects in the games I program. I haven't had an issue until now. I use a for loop to create 3 objects, give each object its own values, and then add them to the array list. Unfortunately, when I tried this each object had the same values, which I figured out through logs were the values of the last item in the list. I'm not sure what I'm doing wrong. Here is the code I'm using(I apologize. It's very sloppy, I'm just trying to program the core of this project currently. There is a lot of useless code/poorly programmed code/incorrectly programmed code, I know) GLCircle.java(the object I want to make multiple of): package com.background.gl.objects; import static android.opengl.GLES20.GL_TRIANGLE_FAN; import static android.opengl.GLES20.glDrawArrays; import static android.opengl.Matrix.multiplyMM; import static android.opengl.Matrix.setIdentityM; import static android.opengl.Matrix.translateM; import static com.background.gl.glcirclebackgroundanimation.Constants.BYTES_PER_FLOAT; import java.util.Random; import android.opengl.Matrix; import com.background.gl.data.VertexArray; import com.background.gl.glcirclebackgroundanimation.CircleHandler; import com.background.gl.helper.TextureShaderProgram; public class GLCircle { private static final int POSITION_COMPONENT_COUNT = 2; private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; private static final int STRIDE = (POSITION_COMPONENT_COUNT + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT; public static float x; public static float y; protected static float[] wallBounds; protected static boolean positiveX, positiveY; public static boolean nullify; protected static float xCounter = 0f; protected static float yCounter = 0f; public static float[] bounds; protected Random ran; private static final float[] VERTEX_DATA = { // Order of coordinates: X, Y, S, T // Triangle Fan 0f, 0f, 0.5f, 0.5f, -0.25f, -0.25f, 0f, 0.9f, 0.25f, -0.25f, 1f, 0.9f, 0.25f, 0.25f, 1f, 0.1f, -0.25f, 0.25f, 0f, 0.1f, -0.25f, -0.25f, 0f, 0.9f }; private final VertexArray vertexArray; public GLCircle(float x, float y) { vertexArray = new VertexArray(VERTEX_DATA); ran = new Random(); wallBounds = new float[4]; nullify = false; this.x = x; this.y = y; } public void bindData(TextureShaderProgram textureProgram) { //Bind the position data to the shader attribute vertexArray.setVertexAttribPointer( 0, textureProgram.getPositionAttributeLocation(), POSITION_COMPONENT_COUNT, STRIDE); //Bind the texture coordinate data to the shader attribute vertexArray.setVertexAttribPointer( POSITION_COMPONENT_COUNT, textureProgram.getTextureCoordinatesAttributeLocation(), TEXTURE_COORDINATES_COMPONENT_COUNT, STRIDE); } public void drawCircle() { glDrawArrays(GL_TRIANGLE_FAN, 0, 6); } public float getX() { return this.x; } public float getY() { return this.y; } public static boolean isPositiveX() { return positiveX; } public static boolean isPositiveY() { return positiveY; } public float[] getBounds(float ranX, float ranY) { if(!positiveX) { /*if(ranX >= 0f) { wallBounds[0] = 1.05f + ranX; } else {*/ this.wallBounds[0] = 1.05f + ranX; //} } else { /* if(ranX >= 0f) { wallBounds[0] = 1.05f - ranX; } else {*/ this.wallBounds[1] = 1.05f - ranX; //} } if(!positiveY) { this.wallBounds[2] = 1.75f + ranY; } else { this.wallBounds[3] = 1.75f - ranY; } return this.wallBounds; } public void setPos(float[] modelMatrix, float[] projectionMatrix, TextureShaderProgram textureProgram, int texture, float x, float y) { setIdentityM(modelMatrix, 0); if(!nullify) translateM(modelMatrix, 0, 0f, 0.01f, 0f); else translateM(modelMatrix, 0, 0f, 0f, 0f); final float[] temp = new float[16]; multiplyMM(temp, 0, projectionMatrix, 0, modelMatrix, 0); System.arraycopy(temp, 0, projectionMatrix, 0, temp.length); textureProgram.useProgram(); textureProgram.setUniforms(projectionMatrix, texture); bindData(textureProgram); drawCircle(); } public void draw(float velocity, float[] modelMatrix, float[] projectionMatrix, TextureShaderProgram textureProgram, int texture) { xCounter+=0.001f; setIdentityM(modelMatrix, 0); if(-x < -1.75f) { translateM(modelMatrix, 0, 0f, 0.001f, 0f); } else { translateM(modelMatrix, 0, 0f, -0.001f, 0f); } final float[] temp = new float[16]; multiplyMM(temp, 0, projectionMatrix, 0, modelMatrix, 0); System.arraycopy(temp, 0, projectionMatrix, 0, temp.length); textureProgram.useProgram(); textureProgram.setUniforms(projectionMatrix, texture); bindData(textureProgram); drawCircle(); } public void scaleCircle(float[] modelMatrix, float x, float y, float z) { Matrix.scaleM(modelMatrix, 0, x, y, z); } public void storeResults(float[] results) { this.x = results[0]; this.y = results[1]; } } This is how I create the multiple objects: for(int i = 0; i < 3; i++) { GLCircle circle = new GLCircle(generateRanFloats()[0], generateRanFloats()[1]); circles.add(circle); /*circle[i].x = circle[i].getX(); circle[i].y = circle[i].getY(); circle[i].bounds = circle[i].getBounds();*/ } And this is the generateranFloats() method: public float[] generateRanFloats() { ranSignX = ran.nextFloat(); ranSignY = ran.nextFloat(); ranSignX = ranSignX > 0.5f? -1:1; ranSignY = ranSignY > 0.5f? -1:1; ranSignVeloX = ran.nextFloat(); ranSignVeloY = ran.nextFloat(); ranX = ran.nextFloat() * 1.05f; ranY = ran.nextFloat() * 1.75f; ranX = ranSignX > 0.5? -ranX:ranX; ranY = ranSignY > 0.5? -ranY:ranY; Log.d("Generated", Float.toString(ranX)); return new float[] {ranX, ranY}; } Why do all of the objects contain the same values(such as x and y)?
The answer is simple Your x, y variables are declared as static, meaning all instances of your object share the same variables. By the way, this line GLCircle circle = new GLCircle(generateRanFloats()[0], generateRanFloats()[1]); is doing double work, you're calling generateRanFloats twice, and you use half of the generated information every time.
3D Bar graph using OpenGLES in android
I am creating a bar graph in android using opengl from an activity I'm calling the renderer and from the renderer I'm calling a cube(im using cubes for showing the bars) Here is my renderer code: public class BarRenderer implements Renderer { int[] vals; private float translatex, translatez, scaly; public BarRenderer(boolean useTranslucentBackground, int[] vals) { mTranslucentBackground = useTranslucentBackground; this.vals = vals; for (int i = 0; i < this.vals.length; i++) { mcube[i] = new Cube(); } } public void onDrawFrame(GL10 gl) { gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glClearColor(0.0f, 0.5f, 0.5f, 1.0f); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); translatex = 0.5f; scaly = 0.8f; for (int i = 0; i < vals.length; i++) { gl.glTranslatef((-1.0f + (translatex * i)), 0.0f, translatez); gl.glRotatef(mAngle, 0.0f, 1.0f, 0.0f); gl.glScalef(0.4f, scaly * vals[i], 0.6f); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_COLOR_ARRAY); mcube[i].draw(gl); } mTransY = .075f; mAngle = -60.0f; translatez = -7.0f; Log.i("Draw", "called"); } public void onSurfaceChanged(GL10 gl, int width, int height) { gl.glViewport(0, 0, width, height); float aspectRatio; float zNear = .1f; float zFar = 1000; float fieldOfView = 80.0f / 57.3f; float size; gl.glEnable(GL10.GL_NORMALIZE); aspectRatio = (float) width / (float) height; gl.glMatrixMode(GL10.GL_PROJECTION); size = zNear * (float) (Math.tan((double) (fieldOfView / 2.0f))); gl.glFrustumf(-size, size, -size / aspectRatio, size / aspectRatio, zNear, zFar); gl.glMatrixMode(GL10.GL_MODELVIEW); } public void onSurfaceCreated(GL10 gl, EGLConfig config) { gl.glDisable(GL10.GL_DITHER); gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); if (mTranslucentBackground) { gl.glClearColor(0, 0, 0, 0); } else { gl.glClearColor(1, 1, 1, 1); } gl.glEnable(GL10.GL_CULL_FACE); gl.glShadeModel(GL10.GL_SMOOTH); gl.glEnable(GL10.GL_POINT_SMOOTH); gl.glEnable(GL10.GL_DEPTH_TEST); } private boolean mTranslucentBackground; private Cube[] mcube = new Cube[4]; private float mTransY; private float mAngle; } but unfortunately it is giving the bar graph like this: anyone will understand this is not exactly like a bar graph but please point out my faults here, where am i doing wrong: 1>the bar heights are 4 1 2 1 but they r not accurately of their sizes 2>the space between the bars are supposed to be .5f apart but they start with a big gap but reduce repeatedly after every bar 3>how to start them from one base plane EDIT: 4> can I animate the growth of this bars?how to do that My cube code: public class Cube { private ByteBuffer mTfan1; private ByteBuffer mTfan2; private int bar; public Cube(int i) { this.bar = i; float vertices[] = { -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f,1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f }; byte maxColor = (byte) 255; byte colors[][] = { {maxColor,maxColor, 0,maxColor}, {0, maxColor,maxColor,maxColor}, {0, 0, 0,maxColor}, {maxColor, 0,maxColor,maxColor}, {maxColor, 0, 0,maxColor}, {0, maxColor, 0,maxColor}, {0, 0,maxColor,maxColor}, {0, 0, 0,maxColor} }; byte tfan1[] = { 1,0,3, 1,3,2, 1,2,6, 1,6,5, 1,5,4, 1,4,0 }; byte tfan2[] = { 7,4,5, 7,5,6, 7,6,2, 7,2,3, 7,3,0, 7,0,4 }; byte indices[] = { 0, 3, 1, 0, 2, 3 }; ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4); vbb.order(ByteOrder.nativeOrder()); mFVertexBuffer = vbb.asFloatBuffer(); mFVertexBuffer.put(vertices); mFVertexBuffer.position(0); mColorBuffer = ByteBuffer.allocateDirect(colors.length); mColorBuffer.put(colors[bar]); mColorBuffer.position(0); mTfan1 = ByteBuffer.allocateDirect(tfan1.length); mTfan1.put(tfan1); mTfan1.position(0); mTfan2 = ByteBuffer.allocateDirect(tfan2.length); mTfan2.put(tfan2); mTfan2.position(0); } public void draw(GL10 gl) { gl.glVertexPointer(3, GL11.GL_FLOAT, 0, mFVertexBuffer); gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer); gl.glDrawElements( gl.GL_TRIANGLE_FAN, 6 * 3, gl.GL_UNSIGNED_BYTE, mTfan1); gl.glDrawElements( gl.GL_TRIANGLE_FAN, 6 * 3, gl.GL_UNSIGNED_BYTE, mTfan2); } private FloatBuffer mFVertexBuffer; private ByteBuffer mColorBuffer; private ByteBuffer mIndexBuffer; } i think something has to be done in the draw method but what??how to animate here? please help thanks
2 Before glTranslate and rotate calls you should use glPushMatrix and after that glPopMatrix functions, or call glTranslate without increment. Because glTranslate multiply existing projection matrix on new matrix from glTranslate parameters 3 gl.glPushMatrix() ; gl.glTranslate(0,y,0); mcube[i].draw(gl); gl.glPopMatrix(); with y= - Barheight/2 in your case or change vertex coordinates of your bars
I have done creating the 3d bar graph though I have some other issues right now but I believe some one might be helpful with this code. Myrenderer class: public class BarRenderer implements GLSurfaceView.Renderer { int[] vals; private float translatex, scaly; // For controlling cube's z-position, x and y angles and speeds (NEW) float angleX = 0; // (NEW) float angleY = 0; // (NEW) float speedX = 0; // (NEW) float speedY = 0; // (NEW) float translatez; public BarRenderer(Context context, boolean useTranslucentBackground, int[] vals) { mTranslucentBackground = useTranslucentBackground; mfloor = new Cube(0); mwall = new Cube(0); this.vals = vals; for (int i = 0; i < this.vals.length; i++) { mcube[i] = new Cube(i); } } public void onDrawFrame(GL10 gl) { gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glClearColor(0.0f, 0.5f, 0.5f, 1.0f); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); translatex = 2.0f; scaly = 0.8f; for (int i = 0; i < vals.length; i++) { gl.glPushMatrix(); mTransY = -5 + (scaly * vals[i]); gl.glTranslatef((-3.0f + (translatex * i)), mTransY, translatez); gl.glRotatef(mAngleX, 1.0f, 0.0f, 0.0f); gl.glRotatef(mAngleY, 0.0f, 1.0f, 0.0f); gl.glScalef(0.4f, scaly * vals[i], 0.4f); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_COLOR_ARRAY); mcube[i].draw(gl); gl.glPopMatrix(); } mTransY = .075f; // mAngleX = -90.0f; // mAngleY = -0.01f; mAngleX += speedX; mAngleY += speedY; translatez = -6.0f; } public void onSurfaceChanged(GL10 gl, int width, int height) { gl.glViewport(0, 0, width, height); float aspectRatio; float zNear = .1f; float zFar = 1000; float fieldOfView = 80.0f / 57.3f; float size; gl.glEnable(GL10.GL_NORMALIZE); aspectRatio = (float) width / (float) height; gl.glMatrixMode(GL10.GL_PROJECTION); size = zNear * (float) (Math.tan((double) (fieldOfView / 2.0f))); gl.glFrustumf(-size, size, -size / aspectRatio, size / aspectRatio, zNear, zFar); gl.glMatrixMode(GL10.GL_MODELVIEW); } public void onSurfaceCreated(GL10 gl, EGLConfig config) { gl.glDisable(GL10.GL_DITHER); gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); if (mTranslucentBackground) { gl.glClearColor(0, 0, 0, 0); } else { gl.glClearColor(1, 1, 1, 1); } gl.glEnable(GL10.GL_CULL_FACE); gl.glShadeModel(GL10.GL_SMOOTH); gl.glEnable(GL10.GL_POINT_SMOOTH); gl.glEnable(GL10.GL_DEPTH_TEST); } private boolean mTranslucentBackground; private Cube[] mcube = new Cube[4]; private Cube mfloor, mwall; private float mTransY; private float mAngleX,mAngleY; } and myGLsurfaceview class: public class MyBarGLSurfaceView extends GLSurfaceView { BarRenderer renderer; // Custom GL Renderer // For touch event private final float TOUCH_SCALE_FACTOR = 180.0f / 320.0f; private float previousX; private float previousY; public MyBarGLSurfaceView(Context context, boolean useTranslucentBackground, int[] vals) { super(context); renderer = new BarRenderer(context, useTranslucentBackground, vals); this.setRenderer(renderer); // Request focus, otherwise key/button won't react this.requestFocus(); this.setFocusableInTouchMode(true); } // Handler for key event #Override public boolean onKeyUp(int keyCode, KeyEvent evt) { switch (keyCode) { case KeyEvent.KEYCODE_DPAD_LEFT: // Decrease Y-rotational speed renderer.speedY -= 01.1f; break; case KeyEvent.KEYCODE_DPAD_RIGHT: // Increase Y-rotational speed renderer.speedY += 01.1f; break; case KeyEvent.KEYCODE_DPAD_UP: // Decrease X-rotational speed renderer.speedX -= 01.1f; break; case KeyEvent.KEYCODE_DPAD_DOWN: // Increase X-rotational speed renderer.speedX += 01.1f; break; case KeyEvent.KEYCODE_A: // Zoom out (decrease z) renderer.translatez -= 0.2f; break; case KeyEvent.KEYCODE_Z: // Zoom in (increase z) renderer.translatez += 0.2f; break; } return true; // Event handled } // Handler for touch event #Override public boolean onTouchEvent(final MotionEvent evt) { float currentX = evt.getX(); float currentY = evt.getY(); float deltaX, deltaY; switch (evt.getAction()) { case MotionEvent.ACTION_MOVE: // Modify rotational angles according to movement deltaX = currentX - previousX; deltaY = currentY - previousY; renderer.angleX += deltaY * TOUCH_SCALE_FACTOR; renderer.angleY += deltaX * TOUCH_SCALE_FACTOR; } // Save current x, y previousX = currentX; previousY = currentY; return true; // Event handled } } though it is done still I would like if anyone can please answer me 1> how to make this bar graphs to grow animatedly, right now when this bar graph activity starts the bar graph appears but i want the bar graph to grow animatedly not just show it 2> how to create a base and a background wall to the bar graph like this link http://www.fusioncharts.com/demos/gallery/#column-and-bar 3> and I was trying to rotate or move the bar graph as per user touch movements i tried the code like another example, you will see that code too and that is why i created this glsurfaceview class of my own, but for some reason it is not working, don't know where and what i have missed, if anyone have any idea please point it out to me thanks
only one triangle of my vertices is showing?
This is my code for 2 triangles for a tree with indices and only the bottom half of the square is showing, i cant figure out where im messing up, i been up a while, thanks for your time public class sword extends GLGame { #Override public Screen getStartScreen() { return new LightScreen(this); } class LightScreen extends GLScreen { float angle; Vertices3 cube,trees; PointLight light; EulerCamera camera; Texture tree; Texture buttonTexture; SpriteBatcher batcher; Camera2D guiCamera; TextureRegion buttonRegion; Vector2 touchPos; float lastX = -1; float lastY = -1; public Context mContext; protected float[] mVMatrix = new float[16]; protected float[] mPMatrix = new float[16]; public float swordtouchx,swordtouchy,swordtouchz; SkyBox skybox; public LightScreen(Game game) { super(game); mContext = getApplicationContext(); tree = new Texture(glGame,"tree.png"); cube = ObjLoader.load(glGame, "swordal.obj"); light = new PointLight(); light.setPosition(3, 3, -3); camera = new EulerCamera(80, 480 / (float) 320, 1, 100); camera.getPosition().set(0, 0, 0); batcher = new SpriteBatcher(glGraphics, 400); guiCamera = new Camera2D(glGraphics, 480, 320); touchPos = new Vector2(); GL10 gl = glGraphics.getGL(); skybox = new SkyBox(mContext); skybox.loadTexture(gl); trees = new Vertices3(glGraphics,4, 6, false, true,false); float[] vertices = { -0.5f, -0.5f, 0.5f, 0, 1, 0.5f, -0.5f, 0.5f, 1, 1, 0.5f, 0.5f, 0.5f, 1, 0, -0.5f, 0.5f, 0.5f, 0, 0}; short[] indices = { 0, 1, 2, 3, 2, 0}; trees.setVertices(vertices, 0, 20); trees.setIndices(indices, 0, indices.length); } #Override public void resume() { } #Override public void update(float deltaTime) { angle += deltaTime * 5; game.getInput().getTouchEvents(); float x = game.getInput().getTouchX(0); float y = game.getInput().getTouchY(0); guiCamera.touchToWorld(touchPos.set(x, y)); swordtouchx = x; swordtouchy = y; if(game.getInput().isTouchDown(0)) { swordtouchy = -1f; if(touchPos.x < 64 && touchPos.y < 64) { camera.rotate(5, 0); } else if(touchPos.x < 128 && touchPos.x > 64 && touchPos.y <64){ camera.rotate(-5, 0); } } else { lastX = -1; lastY = -1; } List<TouchEvent> events = game.getInput().getTouchEvents(); int len = events.size(); for (int i = 0; i < len; i++) { TouchEvent event = events.get(i); if (event.type == TouchEvent.TOUCH_UP){ swordtouchy = 600; } } if(camera.getDirection().z < 0){ swordtouchz = camera.getDirection().z-1.5f; }else{ swordtouchz = camera.getDirection().z +1.5f; } if(camera.getDirection().x < 0){ swordtouchx = camera.getDirection().x -0.5f; }else{ swordtouchx = camera.getDirection().x +0.5f; } } #Override public void present(float deltaTime) { GL10 gl = glGraphics.getGL(); gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glViewport(0, 0,glGraphics.getWidth(), glGraphics.getHeight()); camera.setMatrices(gl); //skybox.draw(gl); gl.glEnable(GL10.GL_DEPTH_TEST); gl.glEnable(GL10.GL_TEXTURE_2D); gl.glEnable(GL10.GL_BLEND); gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); //gl.glEnable(GL10.GL_LIGHTING); trees.bind(); tree.bind(); float o = 0; for(int i = 0;i<50;i++){ gl.glPushMatrix(); gl.glTranslatef(o, -2.5f, 5); trees.draw(GL10.GL_TRIANGLES, 0, trees.getNumVertices()); gl.glPopMatrix(); o+=1f;} trees.unbind(); gl.glDisable(GL10.GL_BLEND); //gl.glDisable(GL10.GL_TEXTURE_2D); cube.bind(); //light.enable(gl, GL10.GL_LIGHT0); gl.glTranslatef( swordtouchx,camera.getPosition().y + swordtouchy, swordtouchz); //gl.glRotatef(90, 0, 0, 1); gl.glRotatef(90, 1, 0, 0); cube.draw(GL10.GL_TRIANGLES, 0, cube.getNumVertices()); cube.unbind(); guiCamera.setViewportAndMatrices(); gl.glDisable(GL10.GL_DEPTH_TEST); } #Override public void pause() { } #Override public void dispose() { } protected void setSkybox(int resourceId) { } } }
Taking a look at the way you are giving indices it seems you mixed up winding orders: short[] indices = { 0, 1, 2, 3, 2, 0}; While your first triangle is given in counter clockwise order as it should you are adding the second one in clock wise order. Most probably your second triangle is just culled away. Try specifying your coordinates like this instead: short[] indices = { 0, 1, 2, 3, 0, 2}; Unless been told to so glDisable(GL_CULL_FACE) to speed up drawing opengl will discard any triangles with normals pointing away from the viewer as they are suggested to be invisible from that point of view. As your coordinates are given like that: 3 2 +------+ | | | | +------+ 0 1 The sequence 3, 2, 0 will wind up like this: 3 2 ----> ^ / | / | / v 0 Instead it is this winding order you're looking for: 3 2 <---- | ^ | / | / v 0
meshobjects are glitchy, but only on Tegra 3 devices
Hello my rendering is very glitchy on Tegra 3 devices.. any Idea how this is possible? Here are two screens. the first one is the glitchy one from an Asus A510 tablet and the second is the correct rendering on my galaxy nexus And here is my code of the render class: public class ShowCaseRenderer { GLGraphics glGraphics; LookAtCamera camera; AmbientLight ambientLight; DirectionalLight directionalLight; public float rotation = 0; public static float rotationCamera = 0; private Vector2 camDirection; public static final int SWIPE_LEFT = -1; public static final int SWIPE_RIGHT = 1; public ShowCaseRenderer(GLGraphics glGraphics) { camDirection = new Vector2(); this.glGraphics = glGraphics; camera = new LookAtCamera(67, glGraphics.getWidth() / (float) glGraphics.getHeight(), 0.1f, 100); ambientLight = new AmbientLight(); ambientLight.setColor(0.2f, 0.2f, 0.2f, 1.0f); directionalLight = new DirectionalLight(); directionalLight.setDirection(-1, -0.5f, 0); GL10 gl = glGraphics.getGL(); gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight()); gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); GLU.gluPerspective(gl, 67, glGraphics.getWidth() / (float) glGraphics.getHeight(), 0.1f, 10.0f); } public void render(int swipe, float deltaTime) { GL10 gl = glGraphics.getGL(); camDirection.set((float) Math.cos(rotationCamera * Vector2.TO_RADIANS), (float) Math.sin(rotationCamera * Vector2.TO_RADIANS)).nor(); camera.getPosition().set(0, 0, 0) .sub(camDirection.x * 2.55f, -0.4f, camDirection.y * 2.5f); camera.getLookAt().set(0, 0, 0); camera.setMatrices(gl); gl.glEnable(GL10.GL_DEPTH_TEST); gl.glEnable(GL10.GL_TEXTURE_2D); gl.glEnable(GL10.GL_LIGHTING); gl.glEnable(GL10.GL_COLOR_MATERIAL); gl.glEnable(GL10.GL_BLEND); ambientLight.enable(gl); directionalLight.enable(gl, GL10.GL_LIGHT0); if (swipe == SWIPE_LEFT && rotationCamera % 90 != 0) { rotationCamera = rotationCamera -= 1.5f; } if (swipe == SWIPE_RIGHT && rotationCamera % 90 != 0) { rotationCamera = rotationCamera += 1.5f; } if (rotationCamera == 360) rotationCamera = 0; if (rotationCamera < 0) rotationCamera = rotationCamera + 360; //eigenRotation der Autos rotation = rotation += deltaTime * 25; if (rotation > 360) { rotation = rotation - 360; } renderShowCase(gl); gl.glDisable(GL10.GL_TEXTURE_2D); gl.glDisable(GL10.GL_COLOR_MATERIAL); gl.glDisable(GL10.GL_LIGHTING); gl.glDisable(GL10.GL_DEPTH_TEST); gl.glDisable(GL10.GL_BLEND); } private void renderShowCase(GL10 gl) { Assets.ectoMobileTexture.bind(); Assets.ectoMobileModel.bind(); gl.glPushMatrix(); gl.glTranslatef(0, 0.01f, -1.5f); gl.glRotatef(-rotation, 0, 1, 0); Assets.ectoMobileModel.draw(GL10.GL_TRIANGLES, 0, Assets.ectoMobileModel.getNumVertices()); gl.glPopMatrix(); Assets.ectoMobileModel.unbind(); Assets.batMobileTexture.bind(); Assets.batMobileModel.bind(); gl.glPushMatrix(); gl.glTranslatef(0, 0.01f, 1.5f); gl.glRotatef(-rotation, 0, 1, 0); Assets.batMobileModel.draw(GL10.GL_TRIANGLES, 0, Assets.batMobileModel.getNumVertices()); gl.glPopMatrix(); Assets.batMobileModel.unbind(); Assets.mysteryMachineTexture.bind(); Assets.mysteryMachineModel.bind(); gl.glPushMatrix(); gl.glTranslatef(-1.5f, 0.01f, 0); gl.glRotatef(-rotation, 0, 1, 0); Assets.mysteryMachineModel.draw(GL10.GL_TRIANGLES, 0, Assets.mysteryMachineModel.getNumVertices()); gl.glPopMatrix(); Assets.mysteryMachineModel.unbind(); Assets.podRacerTexture.bind(); Assets.podRacerModel.bind(); gl.glPushMatrix(); gl.glTranslatef(1.5f, 0.01f, 0); gl.glRotatef(-rotation, 0, 1, 0); Assets.podRacerModel.draw(GL10.GL_TRIANGLES, 0, Assets.podRacerModel.getNumVertices()); gl.glPopMatrix(); Assets.podRacerModel.unbind(); } } thank you...
ok solved the Problem. The glitch caused a guiElement, that had a strange blend config...