Home | History | Annotate | Download | only in globaltime
      1 /*
      2  * Copyright (C) 2007 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.globaltime;
     18 
     19 import javax.microedition.khronos.opengles.GL10;
     20 
     21 /**
     22  * A class that draws a ring with a given center and inner and outer radii.
     23  * The inner and outer rings each have a color and the remaining pixels are
     24  * colored by interpolation.  GlobalTime uses this class to simulate an
     25  * "atmosphere" around the earth.
     26  */
     27 public class Annulus extends Shape {
     28 
     29     /**
     30      * Constructs an annulus.
     31      *
     32      * @param centerX the X coordinate of the center point
     33      * @param centerY the Y coordinate of the center point
     34      * @param Z the fixed Z for the entire ring
     35      * @param innerRadius the inner radius
     36      * @param outerRadius the outer radius
     37      * @param rInner the red channel of the color of the inner ring
     38      * @param gInner the green channel of the color of the inner ring
     39      * @param bInner the blue channel of the color of the inner ring
     40      * @param aInner the alpha channel of the color of the inner ring
     41      * @param rOuter the red channel of the color of the outer ring
     42      * @param gOuter the green channel of the color of the outer ring
     43      * @param bOuter the blue channel of the color of the outer ring
     44      * @param aOuter the alpha channel of the color of the outer ring
     45      * @param sectors the number of sectors used to approximate curvature
     46      */
     47     public Annulus(float centerX, float centerY, float Z,
     48         float innerRadius, float outerRadius,
     49         float rInner, float gInner, float bInner, float aInner,
     50         float rOuter, float gOuter, float bOuter, float aOuter,
     51         int sectors) {
     52         super(GL10.GL_TRIANGLES, GL10.GL_UNSIGNED_SHORT,
     53               false, false, true);
     54 
     55         int radii = sectors + 1;
     56 
     57         int[] vertices = new int[2 * 3 * radii];
     58         int[] colors = new int[2 * 4 * radii];
     59         short[] indices = new short[2 * 3 * radii];
     60 
     61         int vidx = 0;
     62         int cidx = 0;
     63         int iidx = 0;
     64 
     65         for (int i = 0; i < radii; i++) {
     66             float theta = (i * TWO_PI) / (radii - 1);
     67             float cosTheta = (float) Math.cos(theta);
     68             float sinTheta = (float) Math.sin(theta);
     69 
     70             vertices[vidx++] = toFixed(centerX + innerRadius * cosTheta);
     71             vertices[vidx++] = toFixed(centerY + innerRadius * sinTheta);
     72             vertices[vidx++] = toFixed(Z);
     73 
     74             vertices[vidx++] = toFixed(centerX + outerRadius * cosTheta);
     75             vertices[vidx++] = toFixed(centerY + outerRadius * sinTheta);
     76             vertices[vidx++] = toFixed(Z);
     77 
     78             colors[cidx++] = toFixed(rInner);
     79             colors[cidx++] = toFixed(gInner);
     80             colors[cidx++] = toFixed(bInner);
     81             colors[cidx++] = toFixed(aInner);
     82 
     83             colors[cidx++] = toFixed(rOuter);
     84             colors[cidx++] = toFixed(gOuter);
     85             colors[cidx++] = toFixed(bOuter);
     86             colors[cidx++] = toFixed(aOuter);
     87         }
     88 
     89         for (int i = 0; i < sectors; i++) {
     90             indices[iidx++] = (short) (2 * i);
     91             indices[iidx++] = (short) (2 * i + 1);
     92             indices[iidx++] = (short) (2 * i + 2);
     93 
     94             indices[iidx++] = (short) (2 * i + 1);
     95             indices[iidx++] = (short) (2 * i + 3);
     96             indices[iidx++] = (short) (2 * i + 2);
     97         }
     98 
     99         allocateBuffers(vertices, null, null, colors, indices);
    100     }
    101 }
    102