Home | History | Annotate | Download | only in opengl
      1 /*
      2  * Copyright (C) 2011 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 package com.example.android.opengl;
     17 
     18 import java.nio.ByteBuffer;
     19 import java.nio.ByteOrder;
     20 import java.nio.FloatBuffer;
     21 import java.nio.ShortBuffer;
     22 
     23 import javax.microedition.khronos.opengles.GL10;
     24 
     25 /**
     26  * A two-dimensional square for use as a drawn object in OpenGL ES 1.0/1.1.
     27  */
     28 public class Square {
     29 
     30     private final FloatBuffer vertexBuffer;
     31     private final ShortBuffer drawListBuffer;
     32 
     33     // number of coordinates per vertex in this array
     34     static final int COORDS_PER_VERTEX = 3;
     35     static float squareCoords[] = {
     36             -0.5f,  0.5f, 0.0f,   // top left
     37             -0.5f, -0.5f, 0.0f,   // bottom left
     38              0.5f, -0.5f, 0.0f,   // bottom right
     39              0.5f,  0.5f, 0.0f }; // top right
     40 
     41     private final short drawOrder[] = { 0, 1, 2, 0, 2, 3 }; // order to draw vertices
     42 
     43     float color[] = { 0.2f, 0.709803922f, 0.898039216f, 1.0f };
     44 
     45     /**
     46      * Sets up the drawing object data for use in an OpenGL ES context.
     47      */
     48     public Square() {
     49         // initialize vertex byte buffer for shape coordinates
     50         ByteBuffer bb = ByteBuffer.allocateDirect(
     51         // (# of coordinate values * 4 bytes per float)
     52                 squareCoords.length * 4);
     53         bb.order(ByteOrder.nativeOrder());
     54         vertexBuffer = bb.asFloatBuffer();
     55         vertexBuffer.put(squareCoords);
     56         vertexBuffer.position(0);
     57 
     58         // initialize byte buffer for the draw list
     59         ByteBuffer dlb = ByteBuffer.allocateDirect(
     60                 // (# of coordinate values * 2 bytes per short)
     61                 drawOrder.length * 2);
     62         dlb.order(ByteOrder.nativeOrder());
     63         drawListBuffer = dlb.asShortBuffer();
     64         drawListBuffer.put(drawOrder);
     65         drawListBuffer.position(0);
     66     }
     67 
     68     /**
     69      * Encapsulates the OpenGL ES instructions for drawing this shape.
     70      *
     71      * @param gl - The OpenGL ES context in which to draw this shape.
     72      */
     73     public void draw(GL10 gl) {
     74         // Since this shape uses vertex arrays, enable them
     75         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
     76 
     77         // draw the shape
     78         gl.glColor4f(       // set color
     79                 color[0], color[1],
     80                 color[2], color[3]);
     81         gl.glVertexPointer( // point to vertex data:
     82                 COORDS_PER_VERTEX,
     83                 GL10.GL_FLOAT, 0, vertexBuffer);
     84         gl.glDrawElements(  // draw shape:
     85                 GL10.GL_TRIANGLES,
     86                 drawOrder.length, GL10.GL_UNSIGNED_SHORT,
     87                 drawListBuffer);
     88 
     89         // Disable vertex array drawing to avoid
     90         // conflicts with shapes that don't use it
     91         gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
     92     }
     93 }