summaryrefslogtreecommitdiff
path: root/src/Terrain.java
blob: 403baca29939ea627b389ea63e7982779c2713f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336

import java.awt.Color;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Random;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import static javax.media.opengl.GL2.*;
import robotrace.Vector;

/**
 * Implementation of the terrain.
 */
class Terrain extends BetterBase {

    /**
     * The display list containing the terrain.
     */
    private int terrainDisplayList;
    /**
     * The array containing the height map of the terrain.
     */
    private float[][] heightMap;
    /**
     * The array containing all vertex normals of the terrain.
     */
    private Vector[][] normalMap;
    /**
     * The value determining the alpha value of the water plane.
     */
    private float waterAlpha;
    private final RobotRace race;
    /*
     * The array containing all trees of the terrain.
     */
    private Tree[] terrainTrees;

    /**
     * Can be used to set up a display list.
     */
    public Terrain(RobotRace race) {
        // Setup terrain variables
        this.race = race;
        this.terrainDisplayList = 0;
        this.heightMap = new float[41][41];
        this.normalMap = new Vector[41][41];
        this.waterAlpha = 0.30f;
        this.terrainTrees = new Tree[10];

        // Fill the height map array
        for (int y = 0; y < 41; y++) {
            for (int x = 0; x < 41; x++) {
                heightMap[x][y] = heightAt(x, y);
            }
        }

        // Fill the array containing the normal vectors for drawing triangles
        for (int y = 0; y < 41; y++) {
            for (int x = 0; x < 41; x++) {
                normalMap[x][y] = calculateNormal(x, y);
            }
        }
    }

    /**
     * Used to generate a new tree position.
     *
     * @return Vector containing random (x,y) position
     */
    private Vector getRandomPosition() {
        Random r = new Random();
        float x = r.nextFloat() * 36 - 18;
        float y = r.nextFloat() * 36 - 18;
        return new Vector(x, y, heightAt((float) x + 20, (float) y + 20));
    }

    /**
     * Checks whether the given position including the specified marge is free,
     * so a tree can placed.
     *
     * @param position Tree position
     * @param marge Marge to check
     * @return
     */
    private boolean checkPositionFree(Vector position, float marge) {
        for (Tree tree : terrainTrees) {
            float x = (float)position.x();
            float y = (float)position.y();
            float z = (float)position.z();
            
            if (tree != null) {
                if (Math.abs(tree.getPosition().x() - x) < marge) {
                    return false;
                }
                if (Math.abs(tree.getPosition().y() - y) < marge) {
                    return false;
                }
                
                // No trees allowed in water
                if (z <= 0) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * Create the terrain and store all calls in a display list
     * terrainDisplayList, so drawing the terrain is more efficient.
     */
    public void createTerrain() {
        // Create display list
        this.terrainDisplayList = gl.glGenLists(1);

        // Initialize list
        gl.glNewList(terrainDisplayList, GL2.GL_COMPILE);

        // Set up texture mode
        gl.glDisable(GL_TEXTURE_2D);
        gl.glEnable(GL_TEXTURE_1D);
        Color[] textureColors = new Color[3];

        // Set the colors for the texture (blue at the bottom, grass at top)
        textureColors[0] = new Color(28, 107, 220);   // Water
        textureColors[2] = new Color(1, 166, 17);     // Grass
        textureColors[1] = new Color(235, 220, 75);   // Sand

        // Create the 1D texture
        int colorTextureID = create1DTexture(gl, textureColors);

        // Setting up texture 
        gl.glBindTexture(GL_TEXTURE_1D, colorTextureID);

        // Create all points of the terrain based on the heightAt function
        for (int y = -20; y < 20; y++) {
            gl.glBegin(GL_TRIANGLE_STRIP);

            for (int x = -20; x < 20; x++) {
                // Normalize values for use with the arrays
                int arrayX = x + 20;
                int arrayY = y + 20;

                // Create all vertices               
                Vector pointA = new Vector(x, y, heightMap[arrayX][arrayY]);
                Vector pointB = new Vector(x + 1, y, heightMap[arrayX + 1][arrayY]);
                Vector pointC = new Vector(x, y + 1, heightMap[arrayX][arrayY + 1]);
                Vector pointD = new Vector(x + 1, y + 1, heightMap[arrayX + 1][arrayY + 1]);

                /**
                 * First set the texture coordinate based on the height of the
                 * vertex, then set the normal based on the vertex (per-vertex)
                 * and finally draw the vertex itself.
                 */
                gl.glTexCoord1f(getTextureCoordinate((float) pointA.z()));
                glNormal(normalMap[arrayX][arrayY]);
                glVertex(pointA);
                gl.glTexCoord1f(getTextureCoordinate((float) pointB.z()));
                glNormal(normalMap[arrayX + 1][arrayY]);
                glVertex(pointB);
                gl.glTexCoord1f(getTextureCoordinate((float) pointC.z()));
                glNormal(normalMap[arrayX][arrayY + 1]);
                glVertex(pointC);
                gl.glTexCoord1f(getTextureCoordinate((float) pointD.z()));
                glNormal(normalMap[arrayX + 1][arrayY + 1]);
                glVertex(pointD);
            }

            gl.glEnd();
        }

        // Add water to the terrain at z = 0
        gl.glBegin(GL_QUADS);

        // Create water plane vectors
        Vector pointA = new Vector(-20, -20, 0);
        Vector pointB = new Vector(-20, 20, 0);
        Vector pointC = new Vector(20, 20, 0);
        Vector pointD = new Vector(20, -20, 0);

        // Set grey color and transparant alpha value
        gl.glColor4f(100f, 100f, 100f, waterAlpha);

        // Draw vertices
        glVertex(pointA);
        glVertex(pointB);
        glVertex(pointC);
        glVertex(pointD);

        gl.glEnd();

        // Set texture modes
        gl.glDisable(GL_TEXTURE_1D);
        gl.glEnable(GL_TEXTURE_2D);

        /**
         * End the list, everything in the list can be easily drawed by using
         * this display list. This is more efficient then just drawing
         * everything every time.
         */
        gl.glEndList();

        // Initialize all trees
        fillTreeArray();
        createTrees();

        System.out.println("Terrain created");
    }
    
    /**
     * Fills the array with new valid trees.
     */
    private void fillTreeArray() {
        // Fill the tree array with new trees for the terrain
        for (int i = 0; i < 10; i++) {
            Vector treePosition;

            // Generate a new position until one is found
            do {
                treePosition = getRandomPosition();
            } while (!checkPositionFree(treePosition, 1f));


            // The location is free, create scale and variation variable
            Random r = new Random();
            int variation = r.nextInt(2); // 0 (inclusive) and 2 (exclusive)
            float scale = r.nextFloat() * 1.5f; // 0 - 1.5
            
            terrainTrees[i] = new Tree(this, treePosition,
                    scale, variation);
            System.out.println(i);
        }
    }

    /**
     * Creates (initializes) all trees stored in the terrainTrees arrays.
     */
    private void createTrees() {
        for (Tree tree : terrainTrees) {
            tree.createTree();
        }
    }

    /**
     * Determines the texture coordinate based on the height of a vertice so the
     * 1D textures can be displayed correctly.
     *
     * Add 1 to the height and divide by two (now we have [0-1] interval). Now
     * multiply by 0.8 so we have [0-0.76] and add 0.10 so we end up with [0.10
     * - 0.86] for the correct texture display.
     *
     * @param height
     * @return 1D texture coordinate
     */
    private float getTextureCoordinate(float height) {
        return (((height + 1) / 2) * 0.76f) + 0.10f;
    }

    /**
     * Creates a normal vector based on two input vectors.
     *
     * @param x Vector 1
     * @param y Vector 2
     * @return
     */
    private Vector calculateNormal(int x, int y) {
        // Create three vectors (points) based on initial x and y (neighbors)
        Vector v0 = new Vector(x, y, heightMap[x][y]);
        Vector v1 = new Vector(x + 1, y, heightMap[x < 40 ? x + 1 : x][y]);
        Vector v2 = new Vector(x, y + 1, heightMap[x][y < 40 ? y + 1 : y]);

        // Create two vectors pointing from (x,y) to the other two 'points'
        Vector a = v2.subtract(v0);
        Vector b = v1.subtract(v0);

        // Cross product of these two vectors is the normal
        Vector normal = a.cross(b);

        // Invert the direction of the normal so it is pointing outwards and normalize it
        return normal.scale(-1).normalized();
    }

    /**
     * Draws the terrain.
     */
    public void draw() {
        gl.glCallList(terrainDisplayList);
        drawTrees();
    }
    
    /**
     * Draw all the trees stored in the terrainTrees array on the terrain.
     */
    private void drawTrees() {
        for(Tree tree : terrainTrees) {
            tree.drawTree();
        }
    }

    /**
     * Computes the elevation of the terrain at ({@code x}, {@code y}).
     */
    public final float heightAt(float x, float y) {
        float height = (float) (0.6f * Math.cos(0.3f * x + 0.2f * y) + 0.4f
                * Math.cos(x - 0.5f * y));

        return height;
    }

    /**
     * Creates a new 1D - texture.
     *
     * @param gl
     * @param colors
     * @return the texture ID for the generated texture.
     */
    public int create1DTexture(GL2 gl, Color[] colors) {
        int[] texid = new int[]{-1};
        gl.glGenTextures(1, texid, 0);
        ByteBuffer bb = ByteBuffer.allocateDirect(colors.length * 4).order(ByteOrder.nativeOrder());
        for (Color color : colors) {
            int pixel = color.getRGB();
            bb.put((byte) ((pixel >> 16) & 0xFF)); // Red component
            bb.put((byte) ((pixel >> 8) & 0xFF));  // Green component
            bb.put((byte) (pixel & 0xFF));         // Blue component
            bb.put((byte) ((pixel >> 24) & 0xFF)); // Alpha component
        }
        bb.flip();
        gl.glBindTexture(GL_TEXTURE_1D, texid[0]);
        gl.glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, colors.length, 0, GL_RGBA, GL_UNSIGNED_BYTE, bb);
        gl.glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        gl.glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        gl.glBindTexture(GL_TEXTURE_1D, 0);
        return texid[0];
    }
}