package com.example.cubedemo; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.ShortBuffer; import javax.microedition.khronos.opengles.GL10; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.opengl.GLUtils; //filename: SimpleTriangleRenderer.java public class CubeTextureRenderer extends AbstractTextureCubeRenderer { private FloatBuffer mFVertexBuffer; private FloatBuffer mFColorBuffer; private FloatBuffer mFNormalBuffer; private FloatBuffer mFTextureBuffer; private ByteBuffer mIndexBuffer; private int mTextureID; private Context appContext; public CubeTextureRenderer(Context context) { // center our unit cube around the origin so translations make sense float vertices[] = { // front -0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f,-0.5f, 0.5f, -0.5f,-0.5f, 0.5f, // back 0.5f, 0.5f,-0.5f, -0.5f, 0.5f,-0.5f, -0.5f,-0.5f,-0.5f, 0.5f,-0.5f,-0.5f, // top -0.5f, 0.5f,-0.5f, 0.5f, 0.5f,-0.5f, 0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, // bottom -0.5f,-0.5f, 0.5f, 0.5f,-0.5f, 0.5f, 0.5f,-0.5f,-0.5f, -0.5f,-0.5f,-0.5f, // right 0.5f, 0.5f, 0.5f, 0.5f, 0.5f,-0.5f, 0.5f,-0.5f,-0.5f, 0.5f,-0.5f, 0.5f, // left -0.5f, 0.5f,-0.5f, -0.5f, 0.5f, 0.5f, -0.5f,-0.5f, 0.5f, -0.5f,-0.5f,-0.5f, }; byte indices[] = { //front 0,1,2, 2,3,0, //back 4,5,6, 6,7,4, // top 8,9,10, 10,11,8, // bottom 12,13,14, 14,15,12, //right 16,17,18, 18,19,16, //left 20,21,22, 22,23,20, }; float normals[] = { // front 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // back 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, // top 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // bottom 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, // right 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // left -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, }; float texture[] = { 1,0, 1,1, 0,1, 0,0, 1,0, 1,1, 0,1, 0,0, 1,0, 1,1, 0,1, 0,0, 1,0, 1,1, 0,1, 0,0, 1,0, 1,1, 0,1, 0,0, 1,0, 1,1, 0,1, 0,0, }; ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4); //1 float= 4 octets vbb.order(ByteOrder.nativeOrder()); mFVertexBuffer = vbb.asFloatBuffer(); mFVertexBuffer.put(vertices); mFVertexBuffer.position(0); ByteBuffer nbb = ByteBuffer.allocateDirect(normals.length * 4); //1 float= 4 octets nbb.order(ByteOrder.nativeOrder()); mFNormalBuffer = nbb.asFloatBuffer(); mFNormalBuffer.put(normals); mFNormalBuffer.position(0); ByteBuffer tbb = ByteBuffer.allocateDirect(texture.length * 4); //1 float= 4 octets tbb.order(ByteOrder.nativeOrder()); mFTextureBuffer = tbb.asFloatBuffer(); mFTextureBuffer.put(texture); mFTextureBuffer.position(0); // ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4); //1 float= 4 octets // cbb.order(ByteOrder.nativeOrder()); // mFColorBuffer = cbb.asFloatBuffer(); // mFColorBuffer.put(colors); // mFColorBuffer.position(0); mIndexBuffer = ByteBuffer.allocateDirect(indices.length); //ibb.order(ByteOrder.nativeOrder()); //mIndexBuffer = ibb.asShortBuffer(); mIndexBuffer.put(indices); mIndexBuffer.position(0); appContext=context; } private void setTexture (GL10 gl, Context c, int textureID, int drawableID) { mTextureID = textureID; gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID); Bitmap bitmap = BitmapFactory.decodeResource(c.getResources(), drawableID); Bitmap bitmap256 = Bitmap.createScaledBitmap(bitmap, 256, 256, false); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap256, 0); bitmap.recycle(); bitmap256.recycle(); 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); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { // SPECIALIZE onSurfaceChanged inherited // create room for two textures gl.glEnable(GL10.GL_TEXTURE_2D); // use this texture unit gl.glActiveTexture(GL10.GL_TEXTURE0); int[] textures = new int[1]; gl.glGenTextures(1, textures, 0); setTexture(gl, appContext, textures[0], R.drawable.ic_launcher); // CALLING superbase super.onSurfaceChanged(gl, width, height); } //overriden method public void draw(GL10 gl) { gl.glEnable(GL10.GL_CULL_FACE); //différentiation active des triangle face avant ou arrier gl.glCullFace(GL10.GL_BACK); // triangles en back face non tracés => gain calcul gl.glFrontFace(GL10.GL_CW); // triangle antihoraire en front face // gl.glColor4f(1f, 0f, 0f, 1f); //couleur unie gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID); gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mFTextureBuffer); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mFVertexBuffer); // set our normals for correct lighting gl.glNormalPointer(GL10.GL_FLOAT, 0, mFNormalBuffer); // gl.glColorPointer(4, GL10.GL_FLOAT, 0, mFColorBuffer); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_NORMAL_ARRAY); gl.glEnable(GL10.GL_TEXTURE_2D); gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // gl.glEnableClientState(GL10.GL_COLOR_ARRAY); gl.glDrawElements(GL10.GL_TRIANGLES, 36, GL10.GL_UNSIGNED_BYTE, mIndexBuffer);// 36 indices= 12 triangles *3 vertex gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); gl.glDisableClientState(GL10.GL_NORMAL_ARRAY); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glDisableClientState(GL10.GL_TEXTURE_2D); // gl.glDisableClientState(GL10.GL_COLOR_ARRAY); } }