Cast remote display android Nullpointer - android
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
Related
How to rotate a camera view in OpenGL 2.0 ES
I want to make a 360-world using OpenGL 2.0 ES on Smart phone. And I want to make camera view follow the Smart phone Sensor using a quaternion, like an youtube 360 video or an Oculus. I get a Quarternion value using public void setquaternion(...) Function. I change the Quarternion values to 4x4 rotate matrix using makeRotateMatrix() function. How can I control the camera view using this matrixes....?(moving camera's sight) I'm getting hard time because of this... please help me..... ;( And My Rendering source code is.. (This source just shows the Blue Square box) package com.lookie.tom.superlookie; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLES20; import android.opengl.GLSurfaceView; import android.opengl.Matrix; import android.util.Log; public class MyGLRenderer implements GLSurfaceView.Renderer { private static final String TAG = "MyGLRenderer"; private Square mSquare; // mMVPMatrix is an abbreviation for "Model View Projection Matrix" private final float[] mMVPMatrix = new float[16]; private final float[] mProjectionMatrix = new float[16]; private final float[] mViewMatrix = new float[16]; private float mQw = 0; private float mQx = 0; private float mQy = 0; private float mQz = 0; #Override public void onSurfaceCreated(GL10 unused, EGLConfig config) { // Set the background frame color GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); mSquare = new Square(); } #Override public void onDrawFrame(GL10 unused) { float[] scratch = new float[16]; float[] temp = new float[16]; // Draw background color GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -0.2f, 0f, 0f, 0f, 0.0f, 1.0f, 0.0f ); // Calculate the projection and view transformation Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0); mSquare.draw(mMVPMatrix); // Draw square } #Override public void onSurfaceChanged(GL10 unused, int width, int height) { // Adjust the viewport based on geometry changes, // such as screen rotation GLES20.glViewport(0, 0, width, height); float ratio = (float) width / height; // this projection matrix is applied to object coordinates // in the onDrawFrame() method Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 0.2f, 7 ); } public static int loadShader(int type, String shaderCode){ // create a vertex shader type (GLES20.GL_VERTEX_SHADER) // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER) int shader = GLES20.glCreateShader(type); // add the source code to the shader and compile it GLES20.glShaderSource(shader, shaderCode); GLES20.glCompileShader(shader); return shader; } private float[] makeRotateMatrix(){ float [] tempMatrix = { 1.0f - 2.0f*mQy*mQy - 2.0f*mQz*mQz, 2.0f*mQx*mQy - 2.0f*mQz*mQw, 2.0f*mQx*mQz + 2.0f*mQy*mQw, 0.0f, 2.0f*mQx*mQy + 2.0f*mQz*mQw, 1.0f - 2.0f*mQx*mQx - 2.0f*mQz*mQz, 2.0f*mQy*mQz - 2.0f*mQx*mQw, 0.0f, 2.0f*mQx*mQz - 2.0f*mQy*mQw, 2.0f*mQy*mQz + 2.0f*mQx*mQw, 1.0f - 2.0f*mQx*mQx - 2.0f*mQy*mQy, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; return tempMatrix; } public void setQuaternion(float x, float y, float z, float w){ mQx=x; mQy=y; mQz=z; mQw=w; } public static void checkGlError(String glOperation) { int error; while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) { Log.e(TAG, glOperation + ": glError " + error); throw new RuntimeException(glOperation + ": glError " + error); } } }
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
android OpenGL-Es gluProject results not accurate?
I want to overlay an icon on top of 3D models in my game. I'm using gluProject to get the screen coordinates of the centre point of the models, and then using that data to draw icons on a custom view: In my renderer class: private void projectScreenCoords(GLSurfaceView view, GraphicsEntity ge, GL10 gl){ MatrixGrabber matrixGrabber = new MatrixGrabber(); matrixGrabber.getCurrentModelView(gl); float[] modelMat = matrixGrabber.mModelView; matrixGrabber.getCurrentProjection(gl); float[] projMat = matrixGrabber.mProjection; gl.glMatrixMode(GL10.GL_MODELVIEW); // view port int[] viewport = new int[]{view.getTop(),view.getLeft(),view.getWidth(),view.getHeight()}; float[] vector = new float[4]; GLU.gluProject(ge.getTransform().tx, ge.getTransform().ty, ge.getTransform().tz, modelMat, 0, projMat, 0, viewport, 0, vector, 0); ge.setScreenCoords(vector[0], viewport[3] - vector[1]); } and my custom view: protected void onDraw (Canvas canvas){ Vector<GraphicsEntity> scene = renderer.getForegroundScene(); for(int i = 0;i<scene.size();i++){ GraphicsEntity ge = scene.get(i); float[] xy = ge.getScreenCoords(); if(xy[0]>-1 && xy[1]>-1){ canvas.drawBitmap(bitmap, xy[0]-bitmapWidth/2, xy[1]-bitmapHeight/2, null); } invalidate(); } } and where I set my projection matrix: protected void setProjectionMatrix(GL10 gl){ float ratio = (float) viewportWidth / viewportHeight; gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); gl.glFrustumf(-ratio*zoom, ratio*zoom, -1*zoom, 1*zoom, nearPlane, farPlane); } However, the further from the centre of the screen, the further off the centre of the model the icon is drawn: Clearly, if it was just the viewport size that was incorrect (due to the bars at the top/bottom) then the icons would be off just in the y-axis, but they are off in the x-axis as well. What am I missing / doing wrong? Also I haven't used the 3rd value returned by gluProject, but this always has a value of 0.7 so I'm not quite sure how it would be used if at all? Using SDK version 7, I've tested this on multiple devices (ZTE Blade running android 2.1 and Kindle Fire running 2.3.4) with the same results. viewportWidth/Height and view.getWidth/Height() are the same, and view.getTop/Left() returns 0. The MatrixGrabber code works for gluUnProject, so I'm reasonably confident that isn't the problem EDIT: here is the rest of my GL-related drawing code: In renderer: // camera variables protected float FOV = 60.0f; protected float nearPlane = 3; protected float farPlane = 7; protected float eyeX = 0; protected float eyeY = 0; protected float eyeZ = 0; protected float centreX = 0; protected float centreY = 0; protected float centreZ = ((farPlane - nearPlane) / 2) + nearPlane; protected float upX = 0; protected float upY = 1; protected float upZ = 0; protected float viewportWidth; protected float viewportHeight; // user camera control variables protected float zoom = 1; protected float rotatedX = 0; protected float rotatedY = 0; protected boolean zoomed = true; protected TrackingTransform tracking; protected void setWorldTransform(GL10 gl){ gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); // transforms the centre position to 0,0,0 gl.glTranslatef(-centreX, -centreY, -centreZ); } protected void setModelRotation(GL10 gl, GraphicsEntity ge){ if(ge.isTrackerball()){ gl.glRotatef(rotatedX, 1.0f, 0, 0); gl.glRotatef(rotatedY, 0, -1.0f, 0); if(tracking!=null){ synchronized(tracking){ tracking.transform(gl); } } } else if(ge.isBackground()){ gl.glTranslatef(centreX, centreY, centreZ); gl.glRotatef(rotatedX, 1.0f, 0, 0); gl.glRotatef(rotatedY, 0, -1.0f, 0); if(ge.isSkybox()==true){ ge.getTransform().sx = nearPlane + 1.0f; ge.getTransform().sy = nearPlane + 1.0f; ge.getTransform().sz = nearPlane + 1.0f; ge.getTransform().tx = 0; ge.getTransform().ty = 0; ge.getTransform().tz = 0; } } } protected void setModelViewMatrix(GL10 gl){ gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); // not used: //GLU.gluLookAt(gl, eyeX, eyeY, eyeZ, centreX, centreY, centreZ, upX, upY, upZ); } #Override public void onDrawFrame(GL10 gl) { // Set up various things before drawing gl.glFrontFace(GL10.GL_CW); gl.glEnable(GL10.GL_CULL_FACE); gl.glCullFace(GL10.GL_FRONT); gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glEnable(GL10.GL_DEPTH_TEST); // change projection matrix float oldzoom = zoom; zoom = 1.0f; setProjectionMatrix(gl); zoom = oldzoom; // set global world transform (also changes to modelview) setWorldTransform(gl); // loop through and draw background models for(int i = 0;i<backgroundGraphicsEntities.size();i++){ GraphicsEntity ge = backgroundGraphicsEntities.get(i); SimpleTransform t = ge.getTransform(); int modelIndex = ge.getModelIndex(); if(modelIndex>=0){ gl.glPushMatrix(); setModelRotation(gl, ge); t.transform(gl); if(ge.isSprite() && gl11flag){ Sprite s = sprites.get(modelIndex); s.draw((GL11) gl, ge); } else { Model m = models.get(modelIndex); if(m!=null){ m.draw(gl); } } gl.glPopMatrix(); } if(i==0 && ge.isSkybox()){ // if skybox, reset depth bit gl.glClear(GL10.GL_DEPTH_BUFFER_BIT); } } gl.glClear(GL10.GL_DEPTH_BUFFER_BIT); // change projection matrix (if zoomed) setProjectionMatrix(gl); // change back to modelview gl.glMatrixMode(GL10.GL_MODELVIEW); // loop through and draw models for(int i = 0;i<graphicsEntities.size();i++){ GraphicsEntity ge = graphicsEntities.get(i); SimpleTransform t = ge.getTransform(); int modelIndex = ge.getModelIndex(); if(modelIndex>=0){ gl.glPushMatrix(); setModelRotation(gl, ge); t.transform(gl); if(ge.isSprite() && gl11flag){ Sprite s = sprites.get(modelIndex); s.draw((GL11) gl, ge); } else { Model m = models.get(modelIndex); if(m!=null){ m.draw(gl); } if(projectScreenCoords){ projectScreenCoords(glSurfaceView, ge, gl); } } gl.glPopMatrix(); } } #Override public void onSurfaceChanged(GL10 gl, int width, int height) { viewportWidth = width; viewportHeight = height; gl.glViewport(0, 0, width, height); setProjectionMatrix(gl); } #Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { if(gl instanceof GL11){ gl11flag = true; } gl.glClearColor(0, 0, 0, 1); gl.glShadeModel(GL10.GL_SMOOTH); gl.glEnable(GL10.GL_DEPTH_TEST); gl.glDepthMask(true); //gl.glClearDepthf(1.0f); gl.glDepthFunc(GL10.GL_LEQUAL); //gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); setProjectionMatrix(gl); } and then in SimpleTransform (i.e. what gets called when ge.getTransform().transform(gl) is called): public void transform(GL10 gl) { gl.glTranslatef(tx, ty, tz); gl.glRotatef(rz, 0, 0, 1); gl.glRotatef(ry, 0, 1, 0); gl.glRotatef(rx, 1, 0, 0); gl.glScalef(sx, sy, sz); } and for TrackingTransform: #Override public void transform(GL10 gl) { gl.glTranslatef(-tx, -ty, -tz); } and finally in model.draw(): public void draw(GL10 gl){ gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); // Pass the vertex buffer in gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices); int textureID = material.getTexture().getTextureID(); if(textureID>=0){ // Enable Textures gl.glEnable(GL10.GL_TEXTURE_2D); // Get specific texture. gl.glBindTexture(GL10.GL_TEXTURE_2D, textureID); // Use UV coordinates. gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // Pass in texture coordinates gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureCoordinates); } // Pass in vertex normals gl.glNormalPointer(GL10.GL_FLOAT, 0, normals); gl.glEnableClientState(GL10.GL_NORMAL_ARRAY); gl.glDrawElements(GL10.GL_TRIANGLES, numindices,GL10.GL_UNSIGNED_SHORT, indices); gl.glDisableClientState(GL10.GL_NORMAL_ARRAY); if(textureID>=0){ // Disable buffers gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); } gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); }
The problem wasn't in the gluProject code at all. In fact, it was to do with the translations done before calling gluProject: In onDrawFrame: Model m = models.get(modelIndex); if(m!=null){ m.draw(gl); gl.glTranslatef(-tracking.getTransform().tx, -tracking.getTransform().ty, -tracking.getTransform().tz); if(tickcount % projectFrequency == 0 ){ projectScreenCoords(glSurfaceView, ge, gl); } }
opengl problem works on droid but not droid eris and others
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; } }