Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (591)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Terrain code  (Read 1582 times)
0 Members and 1 Guest are viewing this topic.
Offline Amos Wenger

Senior Devvie




Everything's possible, but not everything's fun...


« Posted 2006-09-16 14:14:05 »

It has been implemented by David Yazel first then by William Denniss (see TerrainTest1 and TerrainTest2). What more do we need ?

(Personnally I do use my own terrain code).

"Once you start working on something, don't be afraid of failure and don't abandon it. People who work sincerely are the happiest"
Offline Marvin Fröhlich

Senior Devvie




May the 4th, be with you...


« Reply #1 - Posted 2006-09-16 15:09:01 »

It has been implemented by David Yazel first then by William Denniss (see TerrainTest1 and TerrainTest2). What more do we need ?

(Personnally I do use my own terrain code).

I haven't use any terrain code so far. But over the time again and again people have demanded to have an understandable terrain code implementation in Xith, which is reusable. I cannot say, if we already have it. But if we do, it's just fine.

So you use your own terrain implementation. Whould you share your code and commit it to xith?
Offline cylab

JGO Ninja


Medals: 55



« Reply #2 - Posted 2006-09-16 21:02:37 »

I will take a look at the terrain code tomorrow. Blue, why did you make an own terrain implementation? Does it have any advantages over the existing one or where you just interested in making an own?

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Amos Wenger

Senior Devvie




Everything's possible, but not everything's fun...


« Reply #3 - Posted 2006-09-18 16:52:48 »

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  
[code][quote author=cylab link=topic=14897.msg118049#msg118049 date=1158440557]
I will take a look at the terrain code tomorrow. Blue, why did you make an own terrain implementation? Does it have any advantages over the existing one or where you just interested in making an own?
[/quote]
Well I repeat I don't know exactly what you mean by *terrain code*. I didn't need *terrain generation* and I figured out it would take me more time to understand David Yazel implementation than to roll my own.
Here are the two interesting classes (which are completely pre-alpha so don't rant please) :

[i]TerrainAttributes.java[/i] : serialized/deserialized to XML with XStream (http://xstream.codehaus.org/)
[code]package org.stratagem.flavorable;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashMap;

import javax.vecmath.Color3f;
import javax.vecmath.Point2f;
import javax.vecmath.Point3f;
import javax.vecmath.Tuple3f;
import javax.vecmath.Vector2f;

import org.stratagem.options.Options;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.XppDriver;

/**
 * TerrainAttributes are all the things which differentiates
 * types of projectiles. These are loaded from XML files.
 * To create an XML skeleton file, see the
 * ClassCreator utility.
 */
public class TerrainAttributes {
   
   private static final float BORDER = 5f;
   
   private static HashMap<String, TerrainAttributes> cache = new HashMap<String, TerrainAttributes> ();
   
   public static TerrainAttributes get(String fileName) {
     
      if(!cache.containsKey(fileName)) {
         try {
            cache.put(fileName, (TerrainAttributes)(new XStream(new XppDriver()).fromXML(new FileReader("flavors/"+
                  Options.get().flavor+"/terrains/"+fileName))));
         } catch (FileNotFoundException e) {
            e.printStackTrace();
         }
      }
     
      return cache.get(fileName);
     
   }
   
   public String name = "Terrain"; // The name of the terrain
   
   public Vector2f cellSize = new Vector2f(1f, 1f); // The size of one cell
   public float[][] height = new float[5][5]; // Height map (array of lines)
   
   // Texture files, which can be found in "flavor/textures"
   public String ground = "terrain.png";
   public String background = "background.png";
   public int groundRepeatCount = 1; // The count the ground texture should be repeated
   
   public Point3f sunPosition = new Point3f(5f, 5f, 20f); // The position of the sun
   
   public Color3f sunColor = new Color3f(1f, 1f, 1f); // The color of the sun
   
   public float fogDepth = 50f; // The depth of the fog (the distance of the camera at which you can see)
   
   // List of sound zones (files can be found in "flavor/sounds")
   public SoundZone[] soundZones = new SoundZone[]{new SoundZone(new Point2f(2f, 2f), new Point2f(8f, 3f), "mySound.ogg")};
   
   // List of obstacles (files can be found in "flavor/models")
   public Obstacle[] obstacles = new Obstacle[]{new Obstacle(new Point2f(5f, 5f), "tree.cfg")};
   
   
   public float getSizeX() {
       
      return height[0].length * cellSize.x;
     
   }
   
   public float getSizeY() {
     
      return height.length * cellSize.y;
     
   }

   public float lowerLimitX() {
     
      return BORDER;
     
   }
   
   public float upperLimitX() {
   
      return getSizeX() - BORDER;
     
   }
   
   public float lowerLimitY() {
     
      return BORDER;
     
   }
   
   public float upperLimitY() {
     
      return getSizeY() - BORDER;
     
   }

   public void evaluateZ(Tuple3f pos) {

     
      pos.z = getZ(pos.x, pos.y);
     
   }
   
   public float getZ(float x, float y) {
      /*
      System.out.println(x+"\t"+y + "\t : original pos");
      System.out.println((x / cellSize.x)+"\t"+(y / cellSize.y)+"\t : scaled pos");
      System.out.println((int)(x / cellSize.x)+"\t"+(int)(y / cellSize.y)+"\t : inted pos");
      */
      return getZ((int)(x / cellSize.x), (int)(y / cellSize.y));
     
   }

   public int getDimX() {
     
      return height[0].length;
     
   }
   
   public int getDimY() {
     
      return height.length;
     
   }

   public float getZ(int x, int y) {
     
      float z = 0f;
     
      try {
         //FIXME test value
         z = height[x][y] * 3f;
      } catch (ArrayIndexOutOfBoundsException e) {
         
      }
     
      return z;
     
   }

}


TexturedTerrainDisplayer
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  
package org.stratagem.gfx;

import javax.vecmath.Point3f;
import javax.vecmath.TexCoord2f;

import org.stratagem.flavorable.TerrainAttributes;
import org.stratagem.options.Options;
import org.xith3d.geometry.Sphere;
import org.xith3d.scenegraph.Transform;

import com.xith3d.loaders.texture.TextureLoader;
import com.xith3d.scenegraph.Appearance;
import com.xith3d.scenegraph.GeometryArray;
import com.xith3d.scenegraph.Group;
import com.xith3d.scenegraph.PolygonAttributes;
import com.xith3d.scenegraph.Shape3D;
import com.xith3d.scenegraph.TriangleArray;

public class TexturedTerrainDisplayer extends TerrainDisplayer {
   
   private int repeatCount;
   private Shape3D terrainShape;
   private Group debugGroup;

   public TexturedTerrainDisplayer(TerrainAttributes terrain, int repeatCount) {
     
      super(terrain);
     
      this.repeatCount = repeatCount;
     
      terrainShape = new Shape3D();
      this.addChild(terrainShape);
     
      update();
     
   }
   
   @Override
   public void update() {
     
      // Convenience variables
      int dimX = terrain.getDimX();
      int dimY = terrain.getDimY();
      float cSizeX = terrain.cellSize.x;
      float cSizeY = terrain.cellSize.y;

      // 6 because two triangles per cell, an dimX * dimY cell
      int vertexCount = dimX * dimY * 6;
      Point3f[] coords = new Point3f[vertexCount];
      TexCoord2f[] texCoords = new TexCoord2f[vertexCount];
     
      int coordIndex = 0;
      int texIndex = 0;
     
      float sizeX = dimX * cSizeX / repeatCount;
      float sizeY = dimY * cSizeY / repeatCount;
     
      for(int x = 0; x < dimX; x++) {
         
         for(int y = 0; y < dimY; y++) {
           
            coords[coordIndex++] = new Point3f(x * cSizeX,    y * cSizeY,    getZ(x, y));
            coords[coordIndex++] = new Point3f((x+1) * cSizeX, y * cSizeY,    getZ(x+1, y));
            coords[coordIndex++] = new Point3f(x * cSizeX,    (y+1) * cSizeY,    getZ(x, y+1));
           
            coords[coordIndex++] = new Point3f(x * cSizeX,    (y+1) * cSizeY,    getZ(x, y+1));
            coords[coordIndex++] = new Point3f((x+1) * cSizeX,(y+1) * cSizeY,    getZ(x+1, y+1));
            coords[coordIndex++] = new Point3f((x+1) * cSizeX, y * cSizeY,    getZ(x+1, y));
           
            texCoords[texIndex++] = new TexCoord2f((x / sizeX    ) * repeatCount, (y / sizeY) * repeatCount);
            texCoords[texIndex++] = new TexCoord2f((x / sizeX + 1) * repeatCount, (y / sizeY) * repeatCount);
            texCoords[texIndex++] = new TexCoord2f((x / sizeX    ) * repeatCount, (y / sizeY + 1) * repeatCount);
           
            texCoords[texIndex++] = new TexCoord2f((x / sizeX    ) * repeatCount, (y / sizeY + 1) * repeatCount);
            texCoords[texIndex++] = new TexCoord2f((x / sizeX + 1) * repeatCount, (y / sizeY + 1) * repeatCount);
            texCoords[texIndex++] = new TexCoord2f((x / sizeX + 1) * repeatCount, (y / sizeY) * repeatCount);
           
         }
         
      }
     
      TriangleArray geom = new TriangleArray(vertexCount, GeometryArray.COORDINATES | GeometryArray.TEXTURE_COORDINATE_2);
     
      geom.setCoordinates(0, coords);
      geom.setTextureCoordinates(0, 0, texCoords);
     
      terrainShape.setGeometry(geom);
     
      Appearance app = new Appearance();
      app.setTexture(TextureLoader.getInstance().getTexture("flavors/"+Options.get().flavor+"/textures/"+terrain.ground));
     
      terrainShape.setAppearance(app);
     
      app.setPolygonAttributes(new PolygonAttributes(PolygonAttributes.POLYGON_LINE, PolygonAttributes.CULL_NONE, 0f));
     
      if(Options.DEBUG_TERRAIN) {
         
         if(debugGroup == null) {
            debugGroup = new Group();
            this.addChild(debugGroup);
         } else {
            debugGroup.removeAllChildren();
         }
         
         for(int x = 0; x < dimX; x++) {
           
            for(int y = 0; y < dimY; y++) {
               
               debugGroup.addChild(new Transform().add(new Sphere(5, 5, GeometryArray.COLOR_3, 1f))
                     .setTranslation(new Point3f(x * cSizeX, y * cSizeY, terrain.getZ(x * cSizeX, y * cSizeY))));
               
            }
           
         }
         
      } else {
         
         if(debugGroup != null) {
            debugGroup.detach();
            debugGroup.removeAllChildren();
            debugGroup = null;
         }
         
      }
     
   }

}


What really suck hard is height approximation. But I'm gonna fix it right now.[/code][/code]

"Once you start working on something, don't be afraid of failure and don't abandon it. People who work sincerely are the happiest"
Offline cylab

JGO Ninja


Medals: 55



« Reply #4 - Posted 2006-09-18 17:02:33 »

I took a quick look at the code and it seems, that David already had a lot good stuff in there, like height data abstraction, chunked LOD and (as it seems) file paging support.

So I think the best option would be to big fix and clean his version, and document it along with a getting started guide.

Mathias - I Know What [you] Did Last Summer!
Offline Amos Wenger

Senior Devvie




Everything's possible, but not everything's fun...


« Reply #5 - Posted 2006-09-18 17:21:37 »

I took a quick look at the code and it seems, that David already had a lot good stuff in there, like height data abstraction, chunked LOD and (as it seems) file paging support.

So I think the best option would be to big fix and clean his version, and document it along with a getting started guide.
Yes, but i'm a bit short on CPU time (I have to handle 1000 units simultaneously) so LOD is maybe too expensive.

"Once you start working on something, don't be afraid of failure and don't abandon it. People who work sincerely are the happiest"
Offline cylab

JGO Ninja


Medals: 55



« Reply #6 - Posted 2006-09-18 17:52:14 »

but i'm a bit short on CPU time
I am aiming for a LOD that is easy on CPU. Let's see how good Davids code is in this respect. I wouldn't want to write a terrain renderer from scratch, if I don't have to.

I have to handle 1000 units simultaneously
That's not _so_ much... have you benchmarked your code yet?

Mathias - I Know What [you] Did Last Summer!
Offline Amos Wenger

Senior Devvie




Everything's possible, but not everything's fun...


« Reply #7 - Posted 2006-09-19 17:18:53 »

I have to handle 1000 units simultaneously
That's not _so_ much... have you benchmarked your code yet?
[/quote]
It's that much when each unit has 300 polygons and is fully animated... and you want to make your game run smooth on a low-end computer (Pentium 500Mhz, 128 RAM, Geforce 2 MX).

Bench :
Athlon XP 2200+, 512 Mo RAM PC3200, Geforce 4MX 440SE 64 Mo
The last time I ran it with 1000+ units I had a low 1-2 FPS.

BUT it's not optimized at all :
- Naive O(n^2) collision detection algorithm (which is to be replaced by a quadtree)
- No display lists for now (will be implemented soon)
- No LOD (each model is ~300 polys)

"Once you start working on something, don't be afraid of failure and don't abandon it. People who work sincerely are the happiest"
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (34 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (66 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04

CopyableCougar4 (82 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!