⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 heightmap.java

📁 J2ME3D手机赛车游戏 开发环境JBuilder
💻 JAVA
字号:
package game.terrain;

import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;

public class HeightMap
{

    private short[] heightMap;
    private int[] data;
    private int imgw, imgh;
    private int average = 0;
    
    private int mapWidth;
    private int mapHeight;
    
    private Mesh[][] map;
    
    private final int waterAppID = 0;
    private final int grassAppID = 1;
    private final int roadAppID  = 2;
    private final int sandAppID  = 3;
    
    private Appearance app[] ;
    
    private Group group = new Group();
    
    int factor ;
    int xTrans ;
    int zTrans ;
    public HeightMap(String imageName, float resolution, int waterLevel) throws IOException
    {    
        if(resolution <= 0.0001f || resolution > 1.0f)
            throw new IllegalArgumentException("Resolution too small or too large");
                
        app = new Appearance[4];
        app[waterAppID] = MeshFactory.makeAppearance("/res/water.png");
        app[grassAppID] = MeshFactory.makeAppearance("/res/grass.png");
        app[roadAppID] = MeshFactory.makeAppearance("/res/road.png");
        app[sandAppID] = MeshFactory.makeAppearance("/res/sand.png");
        
        loadImage(imageName, resolution);
        
        setTerrainParameter(resolution);
        
        createQuads();        
    }
    
    private void setTerrainParameter(float resolution)
    {
        factor = (int) (1 / resolution);
        
        xTrans = ( mapWidth * factor ) / 2;
        zTrans = ( mapHeight * factor ) / 2;
    }
    
    private void createQuads()
    {
        map = new Mesh[mapWidth][mapHeight];
        short[] heights = new short[4];
        
        for(int x = 0; x < (mapWidth - 1); x++)
        {
            for(int y = 0; y < (mapHeight - 1); y++)
            {
                setQuadHeights(heights, x, y, mapWidth);
                
                //根据平均高度的不同决定使用不同的纹理
                if( average == 0 )
                {
                	map[x][y] = MeshFactory.createQuad(heights, PolygonMode.CULL_NONE, factor * x, factor * y, factor, xTrans,zTrans,app[waterAppID] );
                }
                else if( 1 <= average && average <= 4 )
                {
                	map[x][y] = MeshFactory.createQuad(heights, PolygonMode.CULL_NONE, factor * x, factor * y, factor, xTrans,zTrans,app[grassAppID] );
                }else if( 4 < average && average <= 10)
                {
                   	map[x][y] = MeshFactory.createQuad(heights, PolygonMode.CULL_NONE, factor * x, factor * y, factor, xTrans,zTrans,app[sandAppID] );
                }
                group.addChild(map[x][y]);                
            }
        }
    }

    
    private void setQuadHeights(short[] heights, int x, int y, int scanline)
    {
    	
        heights[0] = heightMap[x + y * scanline];
        heights[1] = heightMap[x + y * scanline + 1];
        heights[3] = heightMap[x + (y + 1) * scanline];
        heights[2] = heightMap[x + (y + 1) * scanline + 1];
        average = 0;
        average = (heights[0] + heights[1] + heights[2] + heights[3]) / 4;
        
    }

    private void loadImage(String path, float res) throws IOException
    {        
        Image img = Image.createImage(path);
        
        data = new int[img.getWidth() * img.getHeight()];
        
        img.getRGB(data, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight());
        
        imgw = img.getWidth();
        imgh = img.getHeight();
        
        img = null;
        System.gc();
        
        mapWidth = (int)(res * imgw);
        mapHeight = (int)(res * imgh);
        
        heightMap = new short[mapWidth * mapHeight];
        
        int xoff = imgw / mapWidth;
        int yoff = imgh / mapHeight;
        
        for(int y = 0; y < mapHeight; y++)
        {
            for(int x = 0; x < mapWidth; x++)
            {   
            	heightMap[x + y * mapWidth] = (short)((data[x * xoff + y * yoff * imgw] & 0x000000ff) / 30);
            }
        }
     
        data = null;
        img = null;
        System.gc();
    }
    
    public Group getTerrainGroup()
    {
    	return group;    	
    }    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -