2014-06-10 197 views
0

我正在使用VBO爲我的遊戲渲染所有我的多維數據集(因爲會有很多它們),我希望它們都具有紋理。當我運行我的代碼時,立方體沒有紋理(相反,它看起來有點紅,因爲我的紋理是磚牆),只有當我非常接近立方體時,紋理才顯示出來(並且位置錯誤,比立方體小得多本身),是的,紋理是2(64x64)的力量,所以這不是問題。無法正確渲染的紋理LWJGL

渲染

package engine; 

import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT; 
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT; 
import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST; 
import static org.lwjgl.opengl.GL11.glClear; 
import static org.lwjgl.opengl.GL11.glEnable; 
import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; 
import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; 
import static org.lwjgl.opengl.GL15.glBindBuffer; 
import static org.lwjgl.opengl.GL15.glBufferData; 
import static org.lwjgl.opengl.GL15.glGenBuffers; 

import java.io.File; 
import java.io.FileInputStream; 
import java.nio.FloatBuffer; 
import java.util.ArrayList; 

import org.lwjgl.BufferUtils; 
import org.lwjgl.opengl.GL11; 
import org.lwjgl.util.vector.Vector3f; 
import org.newdawn.slick.opengl.Texture; 
import org.newdawn.slick.opengl.TextureLoader; 

public class Render { 

    private int amountOfVerts; 
    private int vertexSize = 3; 
    private int colorSize = 3; 
    private int textureSize = 2; 
    private FloatBuffer vertData, colorData, textureData; 
    private int handle, colorHandle, textureHandle; 
    private ArrayList<Cube> cubes = new ArrayList<Cube>(); 
    private Texture brick; 

    public Render() { 
     try{ 
      brick = TextureLoader.getTexture("BMP", new FileInputStream(new File("brick.bmp"))); 
     } 
     catch (Exception e){ 
      e.printStackTrace(); 
     } 
     glEnable(GL11.GL_TEXTURE_2D); 
     glEnable(GL_DEPTH_TEST); 
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

     addCube(new Cube(new Vector3f(0, 0, 0))); 

     amountOfVerts = cubes.size() * 24; 
     vertData = BufferUtils.createFloatBuffer(amountOfVerts * vertexSize); 
     createCubeArray(); 
     vertData.flip(); 

     /* 
     * colorData = BufferUtils.createFloatBuffer(amountOfVerts * 
     * colorSize); float[] color = new float[amountOfVerts * 
     * colorSize]; Arrays.fill(color, 1f); colorData.put(color); 
     * colorData.flip(); 
     */ 

     textureData = BufferUtils.createFloatBuffer(amountOfVerts * textureSize); 
     createTextureArray();; 
     textureData.flip(); 

     handle = glGenBuffers(); 
     glBindBuffer(GL_ARRAY_BUFFER, handle); // sets the current 
     glBufferData(GL_ARRAY_BUFFER, vertData, GL_STATIC_DRAW); // fills 
     glBindBuffer(GL_ARRAY_BUFFER, 0); // unbinds 

     /* 
     colorHandle = glGenBuffers(); 
     glBindBuffer(GL_ARRAY_BUFFER, colorHandle); // sets the current 
     glBufferData(GL_ARRAY_BUFFER, colorData, GL_STATIC_DRAW); // fills 
     glBindBuffer(GL_ARRAY_BUFFER, 0); // unbinds 
     */ 

     textureHandle = glGenBuffers(); 
     glBindBuffer(GL_ARRAY_BUFFER, textureHandle); 
     glBufferData(GL_ARRAY_BUFFER, textureData, GL_STATIC_DRAW); 
     glBindBuffer(GL_ARRAY_BUFFER, 0); 

     GL11.glBindTexture(GL11.GL_TEXTURE_2D, brick.getTextureID()); 
    } 

    public void createCubeArray() { 
     for (int i = 0; i < cubes.size(); i++) { 
      Cube c = cubes.get(i); 
      storeVertexData(c.getData()); 
     } 
    } 

    public void createTextureArray(){ 
     for (int i = 0; i < cubes.size(); i++) { 
      Cube c = cubes.get(i); 
      storeTextureData(c.getTextureData()); 
     } 
    } 

    public void storeVertexData(float[] data) { 
     vertData.put(data); 
    } 

    public void storeTextureData(float[] data) { 
     textureData.put(data); 
    } 

public void render() { 
     glEnable(GL_DEPTH_TEST); 
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

     glBindBuffer(GL_ARRAY_BUFFER, handle); 
     GL11.glVertexPointer(vertexSize, GL11.GL_FLOAT, 0, 0L); 

     /* 
     glBindBuffer(GL_ARRAY_BUFFER, colorHandle); 
     GL11.glColorPointer(colorSize, GL11.GL_FLOAT, 0, 0L); 
     */ 

     glBindBuffer(GL_ARRAY_BUFFER, textureHandle); 
     GL11.glTexCoordPointer(textureSize, GL11.GL_FLOAT, 0, 0L); 

     GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY); 
     GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY); 
     GL11.glDrawArrays(GL11.GL_QUADS, 0, amountOfVerts); 
     GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY); 
     GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY); 



} 

立方類

package engine; 

import org.lwjgl.util.vector.Vector3f; 

public class Cube { 

    private Vector3f pos = null; 
    private float cubeSize = 100f; 

    public Cube(Vector3f pos) { 
     this.pos = pos; 
    } 

    public float[] getData(){ 
     return new float[] { pos.x,pos.y,pos.z, 
        pos.x + cubeSize,pos.y,pos.z, 
        pos.x + cubeSize,pos.y + cubeSize,pos.z, 
        pos.x,pos.y + cubeSize,pos.z, 

        pos.x,pos.y,pos.z + cubeSize, 
        pos.x + cubeSize,pos.y,pos.z + cubeSize, 
        pos.x + cubeSize,pos.y + cubeSize,pos.z + cubeSize, 
        pos.x,pos.y + cubeSize,pos.z + cubeSize, 

        pos.x,pos.y,pos.z, 
        pos.x,pos.y,pos.z + cubeSize, 
        pos.x,pos.y + cubeSize,pos.z + cubeSize, 
        pos.x,pos.y + cubeSize,pos.z, 

        pos.x + cubeSize,pos.y,pos.z, 
        pos.x + cubeSize,pos.y,pos.z + cubeSize, 
        pos.x + cubeSize,pos.y + cubeSize,pos.z + cubeSize, 
        pos.x + cubeSize,pos.y + cubeSize,pos.z, 

        pos.x,pos.y,pos.z, 
        pos.x,pos.y,pos.z + cubeSize, 
        pos.x + cubeSize,pos.y,pos.z + cubeSize, 
        pos.x + cubeSize,pos.y,pos.z, 

        pos.x,pos.y + cubeSize,pos.z, 
        pos.x,pos.y + cubeSize,pos.z + cubeSize, 
        pos.x + cubeSize,pos.y + cubeSize,pos.z + cubeSize, 
        pos.x + cubeSize,pos.y + cubeSize,pos.z, 
       }; 
    } 

    public float[] getTextureData(){ 
     return new float[]{0,0, 1,0, 1,1, 0,1}; 
    } 

} 

回答

2

您沒有遠遠不夠紋理座標。由於每個立方體使用24個頂點,因此還需要24組紋理座標。每組紋理座標有2個浮點數,getTextureData()需要返回一個由48個浮點數組成的數組。在你發佈的代碼中,它返回一個只有8個浮點數的數組。

當使用頂點數組/緩衝區來渲染幾何時,對於所有屬性總是需要相同的計數。在這種情況下,24個位置,24組紋理座標,24種顏色(如果你想使用顏色)等等。