Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2009 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.android.sampleplugin.graphics;
     18 
     19 import java.nio.ByteBuffer;
     20 import java.nio.ByteOrder;
     21 import java.nio.IntBuffer;
     22 
     23 import javax.microedition.khronos.opengles.GL10;
     24 
     25 /**
     26  * A vertex shaded cube.
     27  */
     28 class Cube
     29 {
     30     public Cube()
     31     {
     32         int one = 0x10000;
     33         int vertices[] = {
     34                 -one, -one, -one,
     35                 one, -one, -one,
     36                 one,  one, -one,
     37                 -one,  one, -one,
     38                 -one, -one,  one,
     39                 one, -one,  one,
     40                 one,  one,  one,
     41                 -one,  one,  one,
     42         };
     43 
     44         int colors[] = {
     45                 0,    0,    0,  one,
     46                 one,    0,    0,  one,
     47                 one,  one,    0,  one,
     48                 0,  one,    0,  one,
     49                 0,    0,  one,  one,
     50                 one,    0,  one,  one,
     51                 one,  one,  one,  one,
     52                 0,  one,  one,  one,
     53         };
     54 
     55         byte indices[] = {
     56                 0, 4, 5,    0, 5, 1,
     57                 1, 5, 6,    1, 6, 2,
     58                 2, 6, 7,    2, 7, 3,
     59                 3, 7, 4,    3, 4, 0,
     60                 4, 7, 6,    4, 6, 5,
     61                 3, 0, 1,    3, 1, 2
     62         };
     63 
     64         // Buffers to be passed to gl*Pointer() functions
     65         // must be direct, i.e., they must be placed on the
     66         // native heap where the garbage collector cannot
     67         // move them.
     68         //
     69         // Buffers with multi-byte datatypes (e.g., short, int, float)
     70         // must have their byte order set to native order
     71 
     72         ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
     73         vbb.order(ByteOrder.nativeOrder());
     74         mVertexBuffer = vbb.asIntBuffer();
     75         mVertexBuffer.put(vertices);
     76         mVertexBuffer.position(0);
     77 
     78         ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);
     79         cbb.order(ByteOrder.nativeOrder());
     80         mColorBuffer = cbb.asIntBuffer();
     81         mColorBuffer.put(colors);
     82         mColorBuffer.position(0);
     83 
     84         mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
     85         mIndexBuffer.put(indices);
     86         mIndexBuffer.position(0);
     87     }
     88 
     89     public void draw(GL10 gl)
     90     {
     91         gl.glFrontFace(gl.GL_CW);
     92         gl.glVertexPointer(3, gl.GL_FIXED, 0, mVertexBuffer);
     93         gl.glColorPointer(4, gl.GL_FIXED, 0, mColorBuffer);
     94         gl.glDrawElements(gl.GL_TRIANGLES, 36, gl.GL_UNSIGNED_BYTE, mIndexBuffer);
     95     }
     96 
     97     private IntBuffer   mVertexBuffer;
     98     private IntBuffer   mColorBuffer;
     99     private ByteBuffer  mIndexBuffer;
    100 }
    101