/** TexturedSurface.java*/ package com.dafer45.virtualreality; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import javax.microedition.khronos.opengles.GL10; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.opengl.GLUtils; import com.dafer45.virtualreality.renderable.Renderable; import com.dafer45.utilities.MathVector; public class TexturedSurface implements Renderable{ MathVector position; private FloatBuffer vertexBuffer; private FloatBuffer textureBuffer; private ByteBuffer indexBuffer; private static int[] textures = new int[1]; private float vertices[] = {-5f, 0.0f, -5f, 5f, 0.0f, -5f, -5f, 0.0f, 5f, 5f, 0.0f, 5f}; private float texture[] = { 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f }; private byte indices[] = { 0, 1, 3, 0, 3, 2 }; public TexturedSurface(){ position = new MathVector(0, 0, 0); ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length*4); byteBuf.order(ByteOrder.nativeOrder()); vertexBuffer = byteBuf.asFloatBuffer(); vertexBuffer.put(vertices); vertexBuffer.position(0); byteBuf = ByteBuffer.allocateDirect(texture.length*4); byteBuf.order(ByteOrder.nativeOrder()); textureBuffer = byteBuf.asFloatBuffer(); textureBuffer.put(texture); textureBuffer.position(0); indexBuffer = ByteBuffer.allocateDirect(indices.length); indexBuffer.put(indices); indexBuffer.position(0); } public static void loadTexture(GL10 gl, Context context){ InputStream is = context.getResources().openRawResource(R.drawable.texture); Bitmap bitmap = null; try{ bitmap = BitmapFactory.decodeStream(is); } finally{ try{ is.close(); is = null; } catch(IOException e){} } gl.glGenTextures(1, textures, 0); gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); bitmap.recycle(); } public void render(GL10 gl){ gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); gl.glFrontFace(GL10.GL_CCW); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer); gl.glTranslatef(position.x, position.y, position.z); gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer); gl.glTranslatef(-position.x, -position.y, -position.z); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); } public void setPosition(MathVector position){ this.position = position; } } /** VRFirst.java*/ package com.dafer45.virtualreality; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; import com.dafer45.virtualreality.VirtualRealityView; import com.dafer45.utilities.MathVector; import com.dafer45.virtualreality.renderable.predefined.Block; import com.dafer45.virtualreality.BasicScene; import com.dafer45.virtualreality.LocalOrientationHandler; import com.dafer45.virtualreality.GPSLocationHandler; import android.widget.Button; import android.view.View.OnClickListener; import android.view.View; import javax.microedition.khronos.opengles.GL10; import javax.microedition.khronos.egl.EGLConfig; public class VRFirst extends Activity { private VirtualRealityView virtualRealityView; private LocalOrientationHandler localOrientationHandler; private GPSLocationHandler gpsLocationHandler; private VirtualRealityView.VRInitCallback vrInitCallback = new VirtualRealityView.VRInitCallback(){ public void init(GL10 gl, EGLConfig config){ gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); TexturedSurface.loadTexture(gl, VRFirst.this); gl.glEnable(GL10.GL_TEXTURE_2D); gl.glEnable(GL10.GL_DEPTH_TEST); gl.glDepthFunc(GL10.GL_LEQUAL); } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Window win = getWindow(); win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); virtualRealityView = (VirtualRealityView)findViewById(R.id.virtualRealityView); BasicScene basicScene = new BasicScene(); float coordinates[][] = {{0, 0, -10}, {0, 0, 10}, {0, -10, 0}, {0, 10, 0}, {-10, 0, 0}, {10, 0, 0}, {0, 10, 10}, {10, 5, 0}, {5, 5, 5}, {-10, -10, -10}}; for(int c = 0; c < coordinates.length; c++){ Block block = new Block(new MathVector(1, 1, 1)); block.setPosition(new MathVector(coordinates[c][0], coordinates[c][1], coordinates[c][2])); basicScene.addRenderable(block); } virtualRealityView.setScene(basicScene); localOrientationHandler = new LocalOrientationHandler(this); virtualRealityView.setOrientationHandler(localOrientationHandler); gpsLocationHandler = new GPSLocationHandler(this, GPSLocationHandler.MODE_LOCAL_ORIGO); virtualRealityView.setLocationHandler(gpsLocationHandler); Button resetOrigoButton = (Button)findViewById(R.id.fixOrigoButton); resetOrigoButton.setOnClickListener(new OnClickListener(){ public void onClick(View v){ gpsLocationHandler.fixOrigo(); } }); virtualRealityView.setVRInitCallback(vrInitCallback); TexturedSurface texturedSurface = new TexturedSurface(); texturedSurface.setPosition(new MathVector(0, 20, 0)); basicScene.addRenderable(texturedSurface); } @Override public void onDestroy(){ localOrientationHandler.destroy(); gpsLocationHandler.destroy(); super.onDestroy(); } }