我正在使用VBO为我的游戏渲染我的所有立方体(因为它们会有很多)并且我希望它们都被纹理化。当我运行我的代码时,立方体没有纹理(相反,它看起来有点红,因为我的纹理是砖墙),只有当我非常接近立方体时,纹理才会出现(并且位置错误且比立方体小得多) (是),纹理是2(64x64)的幂,所以不是问题。
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};
}
}
答案 0 :(得分:2)
您没有足够的纹理坐标。由于每个立方体使用24个顶点,因此还需要24组纹理坐标。每组纹理坐标有2个浮点数,getTextureData()
需要返回48个浮点数组。在您发布的代码中,它返回一个只有8个浮点数的数组。
使用顶点数组/缓冲区渲染几何体时,所有属性始终需要相同的计数。在这种情况下,24个位置,24组纹理坐标,24种颜色(如果你想使用颜色)等。