Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  Texture Mapping for Terrain  (Read 2717 times)
0 Members and 1 Guest are viewing this topic.
Offline frdfsnlght

Senior Newbie




Trying to understand


« Posted 2004-05-17 02:46:49 »

I've read a number of previous posts on texturing terrain and I still can't get my head around certain aspects. Here's what I want to do:

I have terrain constructed of triangle meshes, based on a height map. For any given triangle in the mesh, I want to assign a texture based on a combination of the elevations of each vertex and the "slope" of the triangle. That is, if the triangle has a shallow or flat "slope", I'd assign either a grass texture for a low elevation triangle, or a snow texture for a high altitude triangle. If a triangle's slope is very steep, I'd assign a rock texture. In between slopes might get dirt, sand, or other interesting textires. Get the idea?

I can certainly do what I propose, but I expect what I'll get won't make me happy. I expect I'll get hard edges on the triangles when the textures assigned to two adjacent triangles are different. I'd like a smooth transition between the textures. How is that done?

And when people refer to "detail" textures on base textures, and splatting, what do they mean?

If it makes any difference, I'm using Java3D.

-Tab
Offline Herkules

Senior Devvie




Friendly fire isn't friendly!


« Reply #1 - Posted 2004-05-17 04:16:00 »

Not easy ... common problem for games like Settlers,....

First, having textures for each triangle means to have each vertex at least twice bc. you cannot share a vertex with different texture coordinates.

But if you have that and determined the texture you need, typically there is another texture set that contains 'transition' textures for every pair of ground material (water-grass, grass-rock, water-rock, rock-desert, desert-grass...) that is used as an additional texture stage.

Another option might be to have 'overlap' triangles where different ground materials are blended.

Detail maps are used to simulate a higher texture resolution. Typically a base texture gives only a couple of texels to a triangle and the detailmap, which might be grayscale texture, modulates these texels to show a more fine-grained texture. (see FlyingGuns as an example). If you have a texture per triangle, it is unlikely that you will need a detailmap.

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline frdfsnlght

Senior Newbie




Trying to understand


« Reply #2 - Posted 2004-05-17 14:43:42 »

Something you said in the last paragraph seems to have jarred some realization in my brain. Just to be sure, when you refer to a texel, you're referring to a pixel in a texture bitmap, correct?

That would explain some of the other posts I've read. Some people refer to having a single terrain texture some 2 or 4 times the width and depth of their height map, meaning 4 or 16 texels map to a single square in the height map and those texels get "spread out" over the rendered quad (or 2 triangles). The texture map used is built using Java2D techniques and whatever algorthm I want as the scene is setup. It's static during rendering.

Does that make sense? That's my distillation of a few posts I've read that didn't seem to spell the details out. It makes more sense now.

On to the detail map... I'm not clear how that works in practice. I understand the theory, but how does it get implemented? It sounds like a texture to each triangle problem again. Is this a form of "multi-texturing"? And when you say the detail map "modulates" the underlying texture, are you taliking about the rendering properties like BLEND and MODULATE? Can a detail map be used to modulate a simple color shaded triangle (i.e., a color assigned to each vertex and the engine interpolates the colors accross the face of the shape)?

I once took a look at FG, but I wasn't looking for texturing techniques at the time. I'll have another look.

-Tab
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline frdfsnlght

Senior Newbie




Trying to understand


« Reply #3 - Posted 2004-05-18 21:51:28 »

Ok, I looked at the terrain stuff if FG, but I admit I don't readily understand what's going on (reading someone else's code is always tough). I think I need a higher level explanation of what's happening with the detail map and then maybe the code will make more sense. Is Herkules listening?

-Tab
Offline Herkules

Senior Devvie




Friendly fire isn't friendly!


« Reply #4 - Posted 2004-05-19 05:48:54 »

Yes he is Smiley

Sorry, the terrain code is a bit messy....

These are the interesting files:

TerrainTexture.java
TiledTerrain.java

Basically there is a heightmap and a base texture of approx. the same size (maybe double or 4). This is texture is used a first texture stage, that is modulated with the material. But this texture only gives 1-4 texel/triangle. Texture coordinates at the north-west end of the terrain (or one tile of it) is (0,0) increasing slowly to the south-east corner there being (1,1). (increasing to higher values makes the texture repeat)

The detail texture is set as a second texture stage (also
Texture.MODULATE). Texture coordinates count 0,1,2,3... with every triangle so is gets repeated fully with each triangle.

On top of that, there is some tiling of the terrain but concering texturing this only complicates the code Sad


HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline frdfsnlght

Senior Newbie




Trying to understand


« Reply #5 - Posted 2004-05-19 15:14:13 »

I think I understand now! The high level description is just what I needed. As I expected, it's not too difficult, I just couldn't get the pieces to line up in my head. I appreciate the help.

Just for a minor point of clarification, I assume the tiling you're speaking of relates to breaking up the terrain into individual Shape3D's to allow Java3D to better optimize the rendering? That's what I'm doing in my own code, but I haven't been using the term 'tile', so I just want to make sure I'm not missing some other essential point.

-Tab
Offline Herkules

Senior Devvie




Friendly fire isn't friendly!


« Reply #6 - Posted 2004-05-19 17:02:57 »

Quote
I assume the tiling you're speaking of relates to breaking up the terrain into individual Shape3D's to allow Java3D to better optimize the rendering?


Exactly, allows Java3D to cull away unvisible parts of the terrain (and elements connected to that part, e.g. the woods in FG) quickly.

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline frdfsnlght

Senior Newbie




Trying to understand


« Reply #7 - Posted 2004-05-19 21:21:38 »

I think I have a handle on this now. The trick, in my case, will be generating the base texture since my terrain is randomly generated at run time. I noticed in FG that the textures and height map are all static, which won't work for me.

I'm working on some code to test some ideas for generating the base texture now. It will probably take a few days to get it working the way I want.

Herkules, concerning the detail map you use in FG, how do the dimensions of that image relate to world scale? In other words, how many linear texels to a unit length in 3-space are you using? I'm just wondering how detailed the detail map should be.

Thanks for the help!

-Tab
Offline frdfsnlght

Senior Newbie




Trying to understand


« Reply #8 - Posted 2004-05-21 15:40:59 »

Eureka!

I've got my terrain textured the way I want. And I actually understand how it's done. I'd like to thank all those that helped.

Herkules, I answered my last question myself after I reread one of your previous posts and took a look at the detail map you're using in FG. For all those reading along also looking for the answer, the detail map is totally mapped (0..1) to each 3D "quad", which is usually composed of 2 triangles. So in my case, each quad is 4 Java3D units on a side and the detail map is 256x256. So for the base level mipmap, that's 64 texels per Java3D unit (each texel is about 2cm square resolution), which seems to be plenty of detail for my project.

Thanks all!

-Tab
Offline pepe

Junior Devvie




Nothing unreal exists


« Reply #9 - Posted 2004-05-24 09:51:47 »

could you please post a screenshot of your terrain rendered? i'm very curious about the effect it gives.
Thanks a lot.

Home page: http://frederic.barachant.com
------------------------------------------------------
GoSub: java2D gamechmark http://frederic.barachant.com/GoSub/GoSub.jnlp
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline frdfsnlght

Senior Newbie




Trying to understand


« Reply #10 - Posted 2004-05-24 17:29:10 »

As requested:



I tried to position the player to get a decent panoramic view showing a few different texture types. The player is sitting on a mix of snow and rock and you can see dirt, grass, and sand below. The transitions between the areas and the close up detail were the effects I were going for. The wall in the background is also textured using the same base texture. You can see some artifacts there, but they go away when I turn on fog (which is off for testing purposes).

The terrain is randomly generated and the textures are built based on elevations and slopes of the resulting height map. I'd like to eventually tweak the generation algorthm to get more dramatic elevation differences rather than the "rolling hills" I have now.

To give a sense of scale, the player is about 800 meters from the far corner of the wall (near the center of the image) as can be seen in the HUD map, and elevation difference between where the player is and bottom of the sand pit is around 60 meters. The entire terrain is 1km on a side, with a maximum elevation difference of 100 meters.

The frame rate displayed in the upper right isn't that great because a) I haven't fully optimized the code and it's lags once in a while, and b) I'm only running on an nForce board with the onboard gfx, which is a GF2MX with 32 MB, and a 1.4MHz Athlon. Normal framerate for me (when it's not laggy) is about 29 FPS. The texturing didn't effect the FPS at all when I turned it on. I had been using simple material colors with shading previously.

-Tab
Offline Herkules

Senior Devvie




Friendly fire isn't friendly!


« Reply #11 - Posted 2004-05-24 19:48:26 »

For just having such a problem: how is the HUD made?


HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline frdfsnlght

Senior Newbie




Trying to understand


« Reply #12 - Posted 2004-05-24 20:37:51 »

The HUD is simply Java2D stuff on top of the Canvas3D. Specifically, I subclassed Canvas3D and implemented the postRender method:

1  
2  
3  
4  
5  
6  
public void postRender() {
    J3DGraphics2D g2 = getGraphics2D();
    paintMap(g2);
    paintText(g2);
    g2.flush(true);
}


The two paint methods are completely my own and look a lot like a standard AWT/Swing paint method. The J3DGraphics class is a subclass of Graphics2D, which of course is a subclass of Graphics. It was really quite simple to do.

Previous to this, I had used the J3D overlay system, but it has problems. Most notibly for me, the lack of precise pixel control (since it uses textures on 3D surfaces), and the performance hit (it drops the framerate by 30%). I looked into the newdawn HUD stuff to, but I never tried it, believing that there had to be a better way. Then I actually looked at the Java3D API docs and found the magical postRender and getGraphics2D methods. The Canvas3D technique works quite well for me.

-Tab
Offline Herkules

Senior Devvie




Friendly fire isn't friendly!


« Reply #13 - Posted 2004-05-25 05:51:46 »

yeah, tried it and it WORKS .... but seems to be NOT cheap!
Painting to J3DGraphics2D each frame is expensive, painting to a BufferedImage and than drawImage() to the J3DGraphics is better, but still expensive.

Using VolatileImage instead of BufferedImage lacks features and does not give any performance advantage.

My framerate drops from ~110 to ~90.


HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline frdfsnlght

Senior Newbie




Trying to understand


« Reply #14 - Posted 2004-05-25 14:14:56 »

Nothing is ever cheap these days  Smiley

I didn't notice that kind of hit for my app, but it probably has to do with the fact my frame rate is already low (~10% of 30 FPS is only 3 FPS). I noticed a slightly bigger hit using the J3D overlay, which everyone seems to say works the best. Maybe it works better for higher FPS apps. I'll stick with the simpler, standard paint interface for now. Maybe it will get accelerated in 1.5b2? One can only hope.

But now that you bring it up, I'm curious why people care so much about frame rate when it's above 30 FPS? I can understand using it for a performance indicator, but high frame rates (above movie or video frame rates of 24/30 FPS) don't do anything for the visual experience IMHO. I'm not trying to start a war here, I'm genually curious why people get so worked up about it?

-Tab
Offline Herkules

Senior Devvie




Friendly fire isn't friendly!


« Reply #15 - Posted 2004-05-25 15:26:52 »

uh uh ... hot topic .....

It depends on the game. Pong, adventures, hack&slay can work with 10fps, a flightsim is fine with 30fps, but FPS definately need >60fps for the very fast-paced action.

And yes, its mainly used as a performance indicator (although its a bit unsharp about what it measures).

For FlyingGuns, I run unthrottled for performance measurements only. Other wise I'll throttle it to ~35FPS, just to keep CPU-load low (I prefer to run on the desktop) and to keep is smooth. On full-power-thread is a bad thing in the MT-Java3D-framework Smiley

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline Usul

Senior Newbie




I hunt bugs


« Reply #16 - Posted 2004-09-21 19:42:35 »

Quote
The HUD is simply Java2D stuff on top of the Canvas3D. Specifically, I subclassed Canvas3D and implemented the postRender method:

1  
2  
3  
4  
5  
6  
public void postRender() {
    J3DGraphics2D g2 = getGraphics2D();
    paintMap(g2);
    paintText(g2);
    g2.flush(true);
}


The two paint methods are completely my own and look a lot like a standard AWT/Swing paint method. The J3DGraphics class is a subclass of Graphics2D, which of course is a subclass of Graphics. It was really quite simple to do.

Previous to this, I had used the J3D overlay system, but it has problems. Most notibly for me, the lack of precise pixel control (since it uses textures on 3D surfaces), and the performance hit (it drops the framerate by 30%). I looked into the newdawn HUD stuff to, but I never tried it, believing that there had to be a better way. Then I actually looked at the Java3D API docs and found the magical postRender and getGraphics2D methods. The Canvas3D technique works quite well for me.

-Tab


Can you please show me paintMap(g2); ?  I tried this myself, but the colors are mixed up.

This is what I tried:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
  public void postRender()
  {
    J3DGraphics2D g = getGraphics2D();

      g.setPaintMode();
      g.setColor(Color.red);
      g.drawString("red",20,320);
      g.setColor(Color.green);
      g.drawString("green",20,340);
      g.setColor(Color.orange);
      g.drawString("orange",20,360);
      g.setColor(Color.yellow);
      g.drawString("yellow",20,380);

      g.flush(false);
  }


Well, it draws the text, but as I said, the colors are wrong.  Orange is for example purple.  And if I draw images, its the same:  They are drawn, but the colors are mixed up.  What am I doin wrong?

It's times like this that I wish I'd listened to what my dad used to tell me.
Yeah? What was that?
I don't know. I never listened. 
- Dr. Venkman and Mr. Zeddemore
Offline frdfsnlght

Senior Newbie




Trying to understand


« Reply #17 - Posted 2004-09-21 20:12:18 »

I haven't been working on this for months; I got busy doing something else, totally unrelated.

I don't know why the colors are messed up. Like I said, I didn't do anything wierd, just straight painting, much like you posted in your code. Maybe a color depth problem (to few bits, so colors are getting mapped to other colors)? But I wouldn't expect that for the primary colors you're using. I honestly don't know. Sorry.

-Tab
Offline Usul

Senior Newbie




I hunt bugs


« Reply #18 - Posted 2004-09-22 09:29:46 »

Well, if anyone could take a quick look at these short two classes (they compile and show the problem), I really would appreciate that.

Test2d.class:
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  
import java.awt.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.swing.*;

public class Test2d extends JFrame
{
  public Test2d ()
  {
    getContentPane().setLayout(new BorderLayout());
    Canvas3D c = new Visual_Canvas3D(SimpleUniverse.getPreferredConfiguration());
    getContentPane().add("Center", c);
    SimpleUniverse simpleU = new SimpleUniverse(c);
    BranchGroup scene = new BranchGroup();
    simpleU.getViewingPlatform().setNominalViewingTransform();
    scene.compile();
    simpleU.addBranchGraph(scene);
  }

   public static void main(String[] args)
   {
     Test2d mainApp = new Test2d();
     mainApp.setSize(800,600);
     mainApp.show();
   }

}

Visual_Canvas3D.class:
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  
import java.awt.*;
import javax.media.j3d.*;

public class Visual_Canvas3D
extends Canvas3D
{
  public Visual_Canvas3D(GraphicsConfiguration gc)
  {
    super(gc);
  }

  public void postRender()
  {
      J3DGraphics2D g = getGraphics2D();

      g.setColor(Color.red);
      g.drawString("red",20,320);
      g.setColor(Color.green);
      g.drawString("green",20,340);
      g.setColor(Color.orange);
      g.drawString("orange",20,360);
      g.setColor(Color.yellow);
      g.drawString("yellow",20,100);

      g.flush(false);
  }

}

Where could be the problem?

It's times like this that I wish I'd listened to what my dad used to tell me.
Yeah? What was that?
I don't know. I never listened. 
- Dr. Venkman and Mr. Zeddemore
Offline cweila2s

Senior Newbie




Java games rock!


« Reply #19 - Posted 2004-09-22 12:20:26 »

Wrote an answer to this problem in its own thread...

Has anybody come to the idea just to hang a Text2D just in front of your ViewPlatform (maybe this is considered very basic, but it was my first approach then and it works Cheesy )? Of course, you have to set FrontClippingDistance to aminimum, but it works fine. The question is, has anybody ever tried it out and can compare both regarding performance? I don't have an application complex enough to test it...
Offline Usul

Senior Newbie




I hunt bugs


« Reply #20 - Posted 2004-09-22 12:44:05 »

But then you would be limited to text, right?  I guess many people (like me) want pictures also to be displayed.

It's times like this that I wish I'd listened to what my dad used to tell me.
Yeah? What was that?
I don't know. I never listened. 
- Dr. Venkman and Mr. Zeddemore
Offline cweila2s

Senior Newbie




Java games rock!


« Reply #21 - Posted 2004-09-23 06:07:21 »

Shouldn't think so! You can hang everything you want in front of your view, text was just the example in my case. I didn't try any other, but that could even open the opportunity to create a 3d surrounding for your view (with rotating icons / displays that morph in real 3d and so on). Hmm, I wonder if picking works fine on these distances...?
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.

rwatson462 (37 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (138 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (38 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!