我对我的游戏中的fps有点失望。我还处于游戏开发的初级阶段。当我第一次开始我的游戏时,我得到了大约350帧/秒。在我向程序中添加了一个高度图和一些更多的代码之后,fps下降是否合乎逻辑。现在我得到了39帧/秒。我还处于初级阶段,fps已经很低了。我想知道当我完成这个项目后会发生什么,我认为fps会如此之低,以至于它会令人恼火。我知道我对程序的要求很高,高度图是个大问题。贴图的面积为200 * 200个顶点,每个顶点都有一个高度。200 * 200 = 40000个顶点,每帧。我在想简化一下地图。我的想法是创建一个简化整个高度图的方法。每4个顶点属于一个四边形。当每个顶点上有两个或多个高度相同的相邻四边形时,可以将它们合并为一个四边形。重点是应该有更少的顶点。(我想)
我将展示我的高度图的一些示例代码。
package rgc.area;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
public class HeightMap {
public int width; // Vertices (width)
public int height; // Vertices (height)
public List<Float> map = new ArrayList<Float>();
/**
*
* @param width The width of the map (x-axis)
* @param height The height of the map (z-axiz, NOT y-axis);
*/
public HeightMap(int width, int height) {
this.width = width;
this.height = height;
for(int i = 0; i < width * height; i++) {
map.add(1.0f);
}
}
public Dimension getSize() {
return new Dimension(this.width, this.height);
}
public int getWidth() {
return this.width;
}
public int getHeight() {
return this.height;
}
/**
* Set the height of a vertex of the map
*/
public void setHeight(int x, int y, float h) {
int index = x;
if(y > 0) {
index += (y - 1) * width;
}
map.set(index - 1, h);
/* DEBUG
for(int i = 0; i < map.size(); i++) {
System.out.println(i + " height: " + map.get(i));
}
*/
}
public float getHeight(int x, int y) {
int index = x;
if(y > 0) {
index += (y - 1) * width;
}
return map.get(index);
}
public float getHeight(float x, float y) {
return this.getHeight((int)x, (int)y);
}
/**
* This method simplifies the heightmap.
* It will merge seperate quads with the same vertex heights.
* This is to save memory and render faster.
*
* This method should only be called when the heightmap is changed.
* So this method should NOT be called every frame.
*/
public void simplify() {
// Don't really know how to do this.
for(int i = 0; i < width * height; i++) {
for(int w = 1; w < width - 1; w++) {
if(map.get(i) == map.get(i + w)) {
}
}
}
}
}有人有这方面的经验吗?有什么想法或改进吗?我做的方式是否正确?提前谢谢。
发布于 2014-04-18 20:25:02
首先,你应该使用一个固定的数组而不是列表。这已经会给你一个小小的提振。我没有看到一个调整高度图大小的函数,所以它可能是固定大小的。你可以这样初始化一个静态数组:
float[][] map;
public HeightMap(int width, int height) {
this.map = new float[width][height];
...
}使用二维数组还可以简化getHeight(x,y)和setHeihgt(x,y,height)方法。除此之外,它在很大程度上取决于你的渲染方法。我建议将顶点缓冲区对象与GL_TRIANGLE_STRIP一起用于高度贴图。
有关折点缓冲区对象的详细信息,请查看http://lwjgl.org/wiki/index.php?title=Using_Vertex_Buffer_Objects_%28VBO%29
除此之外,尝试使用cull facing。这可能会进一步提升你的表现。
此外,您可能希望为较大的级别设置渲染距离,以提高性能。
我知道,这可能不是你想要的答案,但我希望它能有所帮助。
编辑:
哦,看起来你用的是四元组。我对heightmaps做的一件事是,我为每个像素分配了一个顶点,并使用GL_TRIANGLE_STRIP连接它们(如上所述)。除此之外,你仍然可以使用QUADS,我认为这不会有任何不同。如果你遵循上面的建议,你可能会得到一个以200FPS运行的1000*1000高度图。(这就是我目前正在做的一个项目)。
https://stackoverflow.com/questions/22364360
复制相似问题