meshobjects are glitchy, but only on Tegra 3 devices - android
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...
Related
Strange opengl-es picture on nexus 4
I'm implementing opengl-es activity for android. And I've met strange picture on nexus 4. While on nexus-s everything looks as I expect. Here is my Renderer code: public void onSurfaceChanged(GL10 gl, int width, int height) { Log.d(TAG, "onSurfaceChanged"); if (height == 0) { height = 1; } this.width = width; this.height = height; cellsh = height / cellSize; cellsw = width / cellSize; gl.glViewport(0, 0, width, height); // Reset The Current Viewport gl.glMatrixMode(GL10.GL_PROJECTION); // Select The Projection Matrix gl.glLoadIdentity(); // Reset The Projection Matrix gl.glDisable(GL10.GL_DEPTH_TEST);// disable Z coordinate gl.glOrthof(0f, width, height, 0, 0, 1); gl.glMatrixMode(GL10.GL_MODELVIEW); // Select The Modelview Matrix gl.glLoadIdentity(); } #Override public void onSurfaceCreated(GL10 gl, javax.microedition.khronos.egl.EGLConfig config) { Log.d(TAG, "onSurfaceCreated"); gl.glShadeModel(GL10.GL_SMOOTH); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); 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); } public void onDrawFrame(GL10 gl) { gl.glLoadIdentity(); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); gl.glTranslatef(cellSize / 2, cellSize / 2, 0f); for (int i = 0; i < cellsw; i++) { for (int j = 0; j < cellsh; j++) { if (Cells.firstGen()) { if (Cells.getCell(j, i)) { Coloriry(gl, 2); } else { Coloriry(gl, 1); } gl.glScalef(squareSize / 2f, squareSize / 2f, 1f); quad.draw(gl); // Draw triangle ( NEW ) gl.glScalef(1 / (squareSize / 2f), 1 / (squareSize / 2f), 1); gl.glTranslatef(0.0f, cellSize, 0.0f); continue; } if (Cells.getOldCell(j, i) != Cells.getCell(j, i)) { if (Cells.getCell(j, i)) { Coloriry(gl, 2); } else { Coloriry(gl, 1); } gl.glScalef(squareSize / 2f, squareSize / 2f, 1f); quad.draw(gl); gl.glScalef(1 / (squareSize / 2f), 1 / (squareSize / 2f), 1); } gl.glTranslatef(0.0f, cellSize, 0.0f); } gl.glTranslatef(cellSize, -cellsh * cellSize, 0.0f); } Cells.nextGen(); frames += 1; } And here is screenshots from Nexus S and Nexus 4.
I think trouble is in your coordinate system. Are width, height, cellsh, cellsw, cellSize float? How you calculate them? What target api is?
ugly Nexus 4 picture fixed by adding gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); to beginning of onDrawFrame. But now I don't know how to draw only changed regions, not whole screen.
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
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; } }