2012-07-28 96 views
0

我檢查了一個LibGDX高度圖加載器and found one,但我不太確定buildIndices()代碼的工作原理。我也不知道爲什麼它只是產生一個高度貼圖塊是128 * X * 128,任何超過128(例如1024)更大,使裝載機弄亂這樣的:OpenGL高度圖加載器

1024塊 1024 chunk

正如你可以看到,山上有洞,塊形狀呈現爲128 * 1024網

128塊 128 chunk

與128 * 128塊

這裏是我的代碼完全確定:

package com.amzoft.game.utils; 

import java.util.Arrays; 

import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.files.FileHandle; 
import com.badlogic.gdx.graphics.Color; 
import com.badlogic.gdx.graphics.Pixmap; 

public class HeightmapConverter { 

    public int mapWidth, mapHeight; 
    private float[] heightMap; 
    public float[] vertices; 
    public short[] indices; 
    private int strength; 
    private String heightmapFile; 
    private float textureWidth; 

    public HeightmapConverter(int mapWidth, int mapHeight, int strength, String heightmapFile) 
    { 
     this.heightMap = new float[(mapWidth+1) * (mapHeight+1)]; 
     this.mapWidth = mapWidth; 
     this.mapHeight = mapHeight; 
     this.vertices = new float[heightMap.length*5]; 
     this.indices = new short[mapWidth * mapHeight * 6]; 
     this.strength = strength; 
     this.heightmapFile = heightmapFile; 

     loadHeightmap(); 
     createIndices(); 
     createVertices(); 
    } 

    public void loadHeightmap() 
    { 
     try{ 
      FileHandle handle = Gdx.files.internal(heightmapFile); 
      Pixmap heightmapImage = new Pixmap(handle); 
      textureWidth = (float)heightmapImage.getWidth(); 
      Color color = new Color(); 
      int indexToIterate = 0; 
      for(int y = 0; y < mapHeight + 1; y++) 
      { 
       for(int x = 0; x < mapWidth + 1; x++) 
       { 
        Color.rgba8888ToColor(color, heightmapImage.getPixel(x, y)); 
        heightMap[indexToIterate++] = color.r; 
       } 
      } 
      handle = null; 
      heightmapImage.dispose(); 
      heightmapImage = null; 
      color = null; 
      indexToIterate = 0; 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 

    public void createVertices() 
    { 
     int heightPitch = mapHeight + 1; 
     int widthPitch = mapWidth + 1; 

     int idx = 0; 
     int hIdx = 0; 

     for(int z = 0; z < heightPitch; z++) 
     { 
      for(int x = 0; x < widthPitch; x++) 
      { 
       vertices[idx+0] = x; 
       vertices[idx+1] = heightMap[hIdx++] * strength; 
       vertices[idx+2] = z; 
       vertices[idx+3] = x/textureWidth; 
       vertices[idx+4] = z/textureWidth; 
       idx += 5; 
      } 
     } 
    } 

    public void createIndices() 
    { 
     int idx = 0; 
     short pitch = (short)(mapWidth + 1); 
     short i1 = 0; 
     short i2 = 1; 
     short i3 = (short)(1 + pitch); 
     short i4 = pitch; 

     short row = 0; 

     for(int z = 0; z < mapHeight; z++) 
     { 
      for(int x = 0; x < mapWidth; x++) 
      { 
       indices[idx++] = (short)(i1); 
       indices[idx++] = (short)(i2); 
       indices[idx++] = (short)(i3); 

       indices[idx++] = (short)(i3); 
       indices[idx++] = (short)(i4); 
       indices[idx++] = (short)(i1); 

       i1++; 
       i2++; 
       i3++; 
       i4++; 
      } 

      row += pitch; 
      i1 = row; 
      i2 = (short)(row + 1); 
      i3 = (short)(i2 + pitch); 
      i4 = (short)(row + pitch); 
     } 
    } 

    public String getHeightmapFile() 
    { 
     return heightmapFile; 
    } 

} 

爲什麼不更大的塊工作嗎? createIndices()(在LibGDX代碼中稱爲buildIndices())如何工作?

+0

它看起來像你缺少光。你有沒有啓用並添加一些光源?嘗試不啓用光第一個... – epatel 2012-07-28 14:36:45

+0

是的,光線和天空盒將接下來,首先雖然我需要得到這個高度圖加載器工作,雖然...也我想我需要提供紋理座標正確應用的紋理, 對? – Nik 2012-07-28 15:21:59

+0

是的,紋理座標是必需的... – epatel 2012-07-28 15:27:09

回答