Java OpenGL高度图加载器
我检查了LibGDX heightmap加载程序,但我不太确定buildIndexes()代码是如何工作的。我也不知道为什么它只生成一个128*x*128的heightmap块,任何大于128(例如1024)的东西都会让加载程序像这样一团糟: 1024块 如您所见,山脉有洞,块形状渲染为128*1024网格 128块 128*128块完全可以 这是我的密码:Java OpenGL高度图加载器,java,opengl,heightmap,Java,Opengl,Heightmap,我检查了LibGDX heightmap加载程序,但我不太确定buildIndexes()代码是如何工作的。我也不知道为什么它只生成一个128*x*128的heightmap块,任何大于128(例如1024)的东西都会让加载程序像这样一团糟: 1024块 如您所见,山脉有洞,块形状渲染为128*1024网格 128块 128*128块完全可以 这是我的密码: package com.amzoft.game.utils; import java.util.Arrays; import co
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;
}
}
package com.amzoft.game.utils;
导入java.util.array;
导入com.badlogic.gdx.gdx;
导入com.badlogic.gdx.files.FileHandle;
导入com.badlogic.gdx.graphics.Color;
导入com.badlogic.gdx.graphics.Pixmap;
公共类高度映射转换器{
公共int地图宽度、地图高度;
私人浮动[]高度图;
公共浮点数;
公众短期指数;;
私人综合实力;
私有字符串高度映射文件;
私有浮动宽度;
公共HeightmapConverter(int-mapWidth、int-mapHeight、int-strength、字符串heightmapFile)
{
this.heightMap=newfloat[(贴图宽度+1)*(贴图高度+1)];
this.mapWidth=mapWidth;
this.mapHeight=mapHeight;
this.vertices=newfloat[heightMap.length*5];
this.indexs=新短[mapWidth*mapHeight*6];
力量=力量;
this.heightmapFile=heightmapFile;
loadHeightmap();
createindex();
创建顶点();
}
公共void loadHeightmap()
{
试一试{
FileHandle handle=Gdx.files.internal(heightmapFile);
Pixmap heightmapImage=新的Pixmap(句柄);
textureWidth=(float)heightmapImage.getWidth();
颜色=新颜色();
int indexToIterate=0;
对于(int y=0;y
为什么大块不起作用?CreateIndexes()(在LibGDX代码中称为buildIndexes())是如何工作的?解决方案如下:
Java short只能保存该地形网格的值[-32768..32767]
使用更多的索引。256*256太多了(大约65k),所以
只看到高度图的一小部分
多亏了kalle_h提供的解决方案。看起来你错过了光线。您是否启用了该功能并添加了一些光源?尝试先不启用灯光…是的,灯光和天空盒将在下一个,但首先我需要让这个高度图加载器工作,虽然。。。此外,我认为我需要提供纹理坐标,以便正确应用纹理,对吗?是的,纹理坐标是必需的…好吧,我现在添加了这些坐标,但我想,也许一种方法有很多块,然后将它们粘在一起可能更好?我将屏幕截图改为一些带有纹理的截图,任何超过200*200的东西对我来说都是小故障。我认为这可能与指数有关:?。