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
17package com.example.android.apis.graphics.spritetext;
18
19import java.nio.ByteBuffer;
20import java.nio.ByteOrder;
21import java.nio.CharBuffer;
22import java.nio.FloatBuffer;
23
24import javax.microedition.khronos.opengles.GL10;
25
26/**
27 * A 2D rectangular mesh. Can be drawn textured or untextured.
28 *
29 */
30class Grid {
31
32    public Grid(int w, int h) {
33        if (w < 0 || w >= 65536) {
34            throw new IllegalArgumentException("w");
35        }
36        if (h < 0 || h >= 65536) {
37            throw new IllegalArgumentException("h");
38        }
39        if (w * h >= 65536) {
40            throw new IllegalArgumentException("w * h >= 65536");
41        }
42
43        mW = w;
44        mH = h;
45        int size = w * h;
46        final int FLOAT_SIZE = 4;
47        final int CHAR_SIZE = 2;
48        mVertexBuffer = ByteBuffer.allocateDirect(FLOAT_SIZE * size * 3)
49            .order(ByteOrder.nativeOrder()).asFloatBuffer();
50        mTexCoordBuffer = ByteBuffer.allocateDirect(FLOAT_SIZE * size * 2)
51            .order(ByteOrder.nativeOrder()).asFloatBuffer();
52
53        int quadW = mW - 1;
54        int quadH = mH - 1;
55        int quadCount = quadW * quadH;
56        int indexCount = quadCount * 6;
57        mIndexCount = indexCount;
58        mIndexBuffer = ByteBuffer.allocateDirect(CHAR_SIZE * indexCount)
59            .order(ByteOrder.nativeOrder()).asCharBuffer();
60
61        /*
62         * Initialize triangle list mesh.
63         *
64         *     [0]-----[  1] ...
65         *      |    /   |
66         *      |   /    |
67         *      |  /     |
68         *     [w]-----[w+1] ...
69         *      |       |
70         *
71         */
72
73        {
74            int i = 0;
75            for (int y = 0; y < quadH; y++) {
76                for (int x = 0; x < quadW; x++) {
77                    char a = (char) (y * mW + x);
78                    char b = (char) (y * mW + x + 1);
79                    char c = (char) ((y + 1) * mW + x);
80                    char d = (char) ((y + 1) * mW + x + 1);
81
82                    mIndexBuffer.put(i++, a);
83                    mIndexBuffer.put(i++, b);
84                    mIndexBuffer.put(i++, c);
85
86                    mIndexBuffer.put(i++, b);
87                    mIndexBuffer.put(i++, c);
88                    mIndexBuffer.put(i++, d);
89                }
90            }
91        }
92
93    }
94
95    void set(int i, int j, float x, float y, float z, float u, float v) {
96        if (i < 0 || i >= mW) {
97            throw new IllegalArgumentException("i");
98        }
99        if (j < 0 || j >= mH) {
100            throw new IllegalArgumentException("j");
101        }
102
103        int index = mW * j + i;
104
105        int posIndex = index * 3;
106        mVertexBuffer.put(posIndex, x);
107        mVertexBuffer.put(posIndex + 1, y);
108        mVertexBuffer.put(posIndex + 2, z);
109
110        int texIndex = index * 2;
111        mTexCoordBuffer.put(texIndex, u);
112        mTexCoordBuffer.put(texIndex + 1, v);
113    }
114
115    public void draw(GL10 gl, boolean useTexture) {
116        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
117        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
118
119        if (useTexture) {
120            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
121            gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexCoordBuffer);
122            gl.glEnable(GL10.GL_TEXTURE_2D);
123        } else {
124            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
125            gl.glDisable(GL10.GL_TEXTURE_2D);
126        }
127
128        gl.glDrawElements(GL10.GL_TRIANGLES, mIndexCount,
129                GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
130        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
131    }
132
133    private FloatBuffer mVertexBuffer;
134    private FloatBuffer mTexCoordBuffer;
135    private CharBuffer mIndexBuffer;
136
137    private int mW;
138    private int mH;
139    private int mIndexCount;
140}
141