2014-03-12 25 views
1

我對我的遊戲的fps有點失望。我仍然處於遊戲開發的開始階段。當我首先開始我的比賽時,我的速度達到了350 fps。在我爲程序添加了一個高度圖和更多代碼之後,fps下降是否合乎邏輯。現在我得到了39 fps。 我還處於起步階段,fps已經很低。我想知道當我完成這個項目時會發生什麼,我認爲fps會很低,以至於令人不快。 我知道我非常需要這個程序,高度圖是個大問題。 地圖有200 * 200頂點的面積,每個頂點都有一個高度。 200 * 200 = 40000個頂點,每個幀。 我在想簡化地圖。我的想法是創建一個簡化整個高度圖的方法。每4個頂點屬於一個四元組。當每個頂點有兩個或更多相鄰高度相同的四邊形時,它們可以合併爲一個四邊形。 重點是應該有更少的頂點。 (我認爲)Lwjgl如何簡化高度圖以獲得更高的fps?

我將顯示我的高度圖的一些示例代碼。

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)) { 


       } 
      } 
     } 
    } 

} 

有沒有人有這方面的經驗? 是否有任何想法或改進,並且是我正確地做它的方式? 在此先感謝。

+0

也許你可以得到一些更多的幫助[這裏](http://gamedev.stackexchange.com/) – Leviathan

+2

那麼。這是一個高度圖。重要的是:你怎麼樣渲染它?現代圖形卡上的40k頂點應該幾乎不可顯示。但是,如果像glVertex3f(x,y,map.getHeight(x,y))那樣涉及某些東西,那麼我可以想象這太慢了。 (順便說一句:通過用一個簡單的'float []'數組替代'List '就可以實現一個明顯的改進,但爲了獲得真正的高性能,你應該考慮頂點緩衝對象(VBO)和其他「現代」OpenGL東西) – Marco13

+1

使用VBO。這將有所幫助。 –

回答

1

首先你應該給我們一個固定的數組而不是一個列表。這會給你一個小小的提升。我沒有看到調整高度圖的功能,所以它可能是固定大小。你可以初始化的靜態數組方式:

float[][] map; 
public HeightMap(int width, int height) { 
    this.map = new float[width][height]; 
    ... 
} 

使用二維數組也可以簡化您的getHeight(X,Y)和setHeihgt(X,Y,高度)的方法。除此之外,它高度依賴於你的渲染方法。我建議使用GL_TRIANGLE_STRIP的頂點緩衝對象作爲heightmaps。
有關頂點緩衝區對象的更多信息,請檢查http://lwjgl.org/wiki/index.php?title=Using_Vertex_Buffer_Objects_%28VBO%29
除此之外,請嘗試使用cull facing。這可能會進一步提升您的表現。
另外,您可能需要設置更大級別的渲染距離以提高性能。

我知道,這可能不是你正在尋找的答案,但我希望它會幫助。

編輯:
哦,看來你正在使用四邊形。我爲heightmaps所做的一件事是,我爲每個像素分配了一個頂點,並使用GL_TRIANGLE_STRIP連接它們(如上所示)。除此之外,你仍然可以使用QUADS,我認爲它根本沒有任何區別。如果您遵循上述建議,則可能會獲得以200 FPS運行的1000 * 1000高度圖。 (這就是我目前在一個項目上所做的)。