Home | History | Annotate | Download | only in kube
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.apis.graphics.kube;
     18 
     19 import java.nio.ByteBuffer;
     20 import java.nio.ByteOrder;
     21 import java.nio.IntBuffer;
     22 import java.nio.ShortBuffer;
     23 import java.util.Iterator;
     24 import java.util.ArrayList;
     25 
     26 import javax.microedition.khronos.opengles.GL10;
     27 
     28 public class GLWorld {
     29 
     30 	public void addShape(GLShape shape) {
     31 		mShapeList.add(shape);
     32 		mIndexCount += shape.getIndexCount();
     33 	}
     34 
     35 	public void generate() {
     36 	    ByteBuffer bb = ByteBuffer.allocateDirect(mVertexList.size()*4*4);
     37 	    bb.order(ByteOrder.nativeOrder());
     38 		mColorBuffer = bb.asIntBuffer();
     39 
     40 	    bb = ByteBuffer.allocateDirect(mVertexList.size()*4*3);
     41 	    bb.order(ByteOrder.nativeOrder());
     42 	    mVertexBuffer = bb.asIntBuffer();
     43 
     44 	    bb = ByteBuffer.allocateDirect(mIndexCount*2);
     45 	    bb.order(ByteOrder.nativeOrder());
     46 	    mIndexBuffer = bb.asShortBuffer();
     47 
     48 		Iterator<GLVertex> iter2 = mVertexList.iterator();
     49 		while (iter2.hasNext()) {
     50 			GLVertex vertex = iter2.next();
     51 			vertex.put(mVertexBuffer, mColorBuffer);
     52 		}
     53 
     54 		Iterator<GLShape> iter3 = mShapeList.iterator();
     55 		while (iter3.hasNext()) {
     56 			GLShape shape = iter3.next();
     57 			shape.putIndices(mIndexBuffer);
     58 		}
     59 	}
     60 
     61 	public GLVertex addVertex(float x, float y, float z) {
     62 		GLVertex vertex = new GLVertex(x, y, z, mVertexList.size());
     63 		mVertexList.add(vertex);
     64 		return vertex;
     65 	}
     66 
     67 	public void transformVertex(GLVertex vertex, M4 transform) {
     68 		vertex.update(mVertexBuffer, transform);
     69 	}
     70 
     71 	int count = 0;
     72     public void draw(GL10 gl)
     73     {
     74 		mColorBuffer.position(0);
     75 		mVertexBuffer.position(0);
     76 		mIndexBuffer.position(0);
     77 
     78 		gl.glFrontFace(GL10.GL_CW);
     79         gl.glShadeModel(GL10.GL_FLAT);
     80         gl.glVertexPointer(3, GL10.GL_FIXED, 0, mVertexBuffer);
     81         gl.glColorPointer(4, GL10.GL_FIXED, 0, mColorBuffer);
     82         gl.glDrawElements(GL10.GL_TRIANGLES, mIndexCount, GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
     83         count++;
     84     }
     85 
     86     static public float toFloat(int x) {
     87     	return x/65536.0f;
     88     }
     89 
     90 	private ArrayList<GLShape>	mShapeList = new ArrayList<GLShape>();
     91 	private ArrayList<GLVertex>	mVertexList = new ArrayList<GLVertex>();
     92 
     93 	private int mIndexCount = 0;
     94 
     95     private IntBuffer   mVertexBuffer;
     96     private IntBuffer   mColorBuffer;
     97     private ShortBuffer mIndexBuffer;
     98 }
     99