使用lwjgl的Java高度图

使用lwjgl的Java高度图,java,lwjgl,frame-rate,heightmap,Java,Lwjgl,Frame Rate,Heightmap,我已经编写了高度图,但它似乎滞后于客户。我只是不知道如何提高fps。根据高度图,我可以获得3-6fps的速度。我使用一个相当大的bmp作为高度图,我想它是1024x1024。当我使用一个较小的它的罚款,也许我只是没有有效地使用代码。有没有更好的方法来编码这个高度图,还是我只是编码错了。这是我第一次绘制高度图。谢谢 public class HeightMap { private final float xScale, yScale, zScale; private float[]

我已经编写了高度图,但它似乎滞后于客户。我只是不知道如何提高fps。根据高度图,我可以获得3-6fps的速度。我使用一个相当大的bmp作为高度图,我想它是1024x1024。当我使用一个较小的它的罚款,也许我只是没有有效地使用代码。有没有更好的方法来编码这个高度图,还是我只是编码错了。这是我第一次绘制高度图。谢谢

public class HeightMap {
    private final float xScale, yScale, zScale;
    private float[][] heightMap;

    private FloatBuffer vertices, normals, texCoords;
    private IntBuffer indices;

    private Vector3f[] verticesArray, normalsArray;
    private int[] indicesArray;
    private int width;
    private int height;

    public float getHeight(int x, int y) {
            return heightMap[x][y] * yScale;
    }

    public HeightMap(String path, int resolution) {
            heightMap = loadHeightmap("heightmap.bmp");

            xScale = 1000f / resolution;
            yScale = 8;
            zScale = 1000f / resolution;

            verticesArray = new Vector3f[width * height];
            vertices = BufferUtils.createFloatBuffer(3 * width * height);
            texCoords = BufferUtils.createFloatBuffer(2 * width * height);
            for (int x = 0; x < width; x++) {
                    for (int y = 0; y < height; y++) {
                            final int pos = height * x + y;
                            final Vector3f vertex = new Vector3f(xScale * x, yScale * heightMap[x][y], zScale * y);

                            verticesArray[pos] = vertex;
                            vertex.store(vertices);

                            texCoords.put(x / (float) width);
                            texCoords.put(y / (float) height);
                    }
            }
            vertices.flip();
            texCoords.flip();

            normalsArray = new Vector3f[height * width];
            normals = BufferUtils.createFloatBuffer(3 * width * height);
            final float xzScale = xScale;
            for (int x = 0; x < width; ++x) {
                    for (int y = 0; y < height; ++y) {
                            final int nextX = x < width - 1 ? x + 1 : x;
                            final int prevX = x > 0 ? x - 1 : x;
                            float sx = heightMap[nextX][y] - heightMap[prevX][y];
                            if (x == 0 || x == width - 1) {
                                    sx *= 2;
                            }

                            final int nextY = y < height - 1 ? y + 1 : y;
                            final int prevY = y > 0 ? y - 1 : y;
                            float sy = heightMap[x][nextY] - heightMap[x][prevY];
                            if (y == 0 || y == height - 1) {
                                    sy *= 2;
                            }

                            final Vector3f normal = new Vector3f(-sx * yScale, 2 * xzScale, sy * yScale).normalise(null);
                            normalsArray[height * x + y] = normal;
                            normal.store(normals);
                    }
            }
            normals.flip();

            indicesArray = new int[6 * (height - 1) * (width - 1)];
            indices = BufferUtils.createIntBuffer(6 * (width - 1) * (height - 1));
            for (int i = 0; i < width - 1; i++) {
                    for (int j = 0; j < height - 1; j++) {
                            int pos = (height - 1) * i + j;

                            indices.put(height * i + j);
                            indices.put(height * (i + 1) + j);
                            indices.put(height * (i + 1) + (j + 1));

                            indicesArray[6 * pos] = height * i + j;
                            indicesArray[6 * pos + 1] = height * (i + 1) + j;
                            indicesArray[6 * pos + 2] = height * (i + 1) + (j + 1);

                            indices.put(height * i + j);
                            indices.put(height * i + (j + 1));
                            indices.put(height * (i + 1) + (j + 1));

                            indicesArray[6 * pos + 3] = height * i + j;
                            indicesArray[6 * pos + 4] = height * i + (j + 1);
                            indicesArray[6 * pos + 5] = height * (i + 1) + (j + 1);

                    }
            }
            indices.flip();
    }

    private float[][] loadHeightmap(String fileName) {
            try {
                    BufferedImage img = ImageIO.read(ResourceLoader.getResourceAsStream(fileName));
                    width = img.getWidth();
                    height = img.getHeight();
                    float[][] heightMap = new float[width][height];
                    for (int x = 0; x < width; x++) {
                            for (int y = 0; y < height; y++) {
                                    heightMap[x][y] = 0xFF & img.getRGB(x, y);
                            }
                    }
                    return heightMap;
            } catch (IOException e) {
                    System.out.println("Nincs meg a heightmap!");
                    return null;
            }
    }

    public void render() {

            glEnableClientState(GL_NORMAL_ARRAY);
            glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_TEXTURE_COORD_ARRAY);
            glNormalPointer(0, normals);
            glVertexPointer(3, 0, vertices);
            glTexCoordPointer(2, 0, texCoords);
            glDrawElements(GL_TRIANGLE_STRIP, indices);
            glDisableClientState(GL_NORMAL_ARRAY);
            glDisableClientState(GL_TEXTURE_COORD_ARRAY);
            glDisableClientState(GL_VERTEX_ARRAY);

    }

}
公共类高度图{
私人最终浮动xScale、yScale、zScale;
私人浮动[][]高度图;
私有浮动缓冲区顶点、法线、纹理坐标;
私有缓冲区指数;
私有向量3f[]垂直数组,normalsArray;
私有int[]指示阵列;
私有整数宽度;
私人内部高度;
公共浮动高度(整数x,整数y){
返回高度图[x][y]*yScale;
}
公共高度图(字符串路径,整数分辨率){
heightMap=loadHeightmap(“heightMap.bmp”);
xScale=1000f/分辨率;
yScale=8;
zScale=1000f/分辨率;
verticesArray=新矢量3f[宽度*高度];
顶点=BufferUtils.createFloatBuffer(3*宽度*高度);
texCoords=BufferUtils.createFloatBuffer(2*宽度*高度);
对于(int x=0;x0?x-1:x;
float sx=高度图[nextX][y]-高度图[prevX][y];
如果(x==0 | | x==width-1){
sx*=2;
}
最终整数nextY=y<高度-1?y+1:y;
最终int prevY=y>0?y-1:y;
float sy=heightMap[x][nextY]-heightMap[x][prevY];
如果(y==0 | | y==height-1){
sy*=2;
}
最终矢量3f法线=新矢量3f(-sx*yScale,2*xzScale,sy*yScale)。归一化(null);
法线光线[高度*x+y]=法线;
正常。存储(正常);
}
}
法线翻转();
指标数组=新整数[6*(高度-1)*(宽度-1)];
index=BufferUtils.createIntBuffer(6*(宽度-1)*(高度-1));
对于(int i=0;i
很抱歉提出一个老话题,但是我看到很多人问这个问题:

使用显示列表,而不是每次都重新制作高度贴图。
codinguniverse提供了一个很好的教程,介绍了如何实现这一点。

您可以在哪里创建VBO