我在使用lwjgl中的GL_TEXTURE_2D_ARRAY替换GL_TEXTURE_2D时遇到了问题。所有的纹理都只显示为纯白色。我错过了什么吗?
还有,有没有人知道一个简单的示例程序,它在lwjgl中使用GL_TEXTURE_2D_ARRAY,或者至少是另一种基于C的编程语言的示例程序,其中包含从头到尾的编译说明?
我想要的:
我得到的:
我已经尝试将项目逻辑最小化,以减少代码。
Test.java:
还有,有没有人知道一个简单的示例程序,它在lwjgl中使用GL_TEXTURE_2D_ARRAY,或者至少是另一种基于C的编程语言的示例程序,其中包含从头到尾的编译说明?
我想要的:
![Image showing textures](https://istack.dev59.com/RWds7.webp)
![Image showing nontextured white cube](https://istack.dev59.com/64GPT.webp)
Test.java:
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import org.lwjgl.util.glu.GLU;
public class Test {
private Texture3D texture;
private VertexBuffer mainCube;
public static void main(String[] args) throws Exception {
new Test().run();
}
public void run() throws Exception {
this.setup();
while (!Display.isCloseRequested()) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
this.render();
Display.sync(60);
Display.update();
}
this.destroy();
}
private void setup() throws Exception {
Display.setTitle("Game");
Display.setDisplayMode(new DisplayMode(600, 400));
Display.create();
Test.checkForGLError("setupDisplay");
this.texture = new Texture3D("res/images/textures.png");
this.texture.buildBuffer();
this.texture.bufferData();
Test.checkForGLError("setupTexture");
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glCullFace(GL11.GL_BACK);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(90.0f, 1.5f, 0.1f, 1000.0F);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glTranslatef(0, 0, -4);
GL11.glEnable(GL12.GL_TEXTURE_3D);
Test.checkForGLError("setupCamera");
this.mainCube = new VertexBuffer();
int vi0, vi1, vi2, vi3;
// Top
vi0 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(1f, 1f, 1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(0f, 0f, 0f));
vi1 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(1f, 1f, -1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(0f, 1f, 0f));
vi2 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(-1f, 1f, -1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(1f, 1f, 0f));
vi3 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(-1f, 1f, 1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(1f, 0f, 0f));
this.mainCube.addIndices(vi0, vi1, vi2);
this.mainCube.addIndices(vi2, vi3, vi0);
// Bottom
vi0 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(-1f, -1f, 1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(0f, 0f, 1f));
vi1 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(-1f, -1f, -1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(0f, 1f, 1f));
vi2 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(1f, -1f, -1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(1f, 1f, 1f));
vi3 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(1f, -1f, 1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(1f, 0f, 1f));
this.mainCube.addIndices(vi0, vi1, vi2);
this.mainCube.addIndices(vi2, vi3, vi0);
// North
vi0 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(1f, 1f, -1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(0f, 0f, 2f));
vi1 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(1f, -1f, -1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(0f, 1f, 2f));
vi2 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(-1f, -1f, -1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(1f, 1f, 2f));
vi3 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(-1f, 1f, -1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(1f, 0f, 2f));
this.mainCube.addIndices(vi0, vi1, vi2);
this.mainCube.addIndices(vi2, vi3, vi0);
// South
vi0 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(-1f, 1f, 1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(0f, 0f, 3f));
vi1 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(-1f, -1f, 1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(0f, 1f, 3f));
vi2 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(1f, -1f, 1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(1f, 1f, 3f));
vi3 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(1f, 1f, 1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(1f, 0f, 3f));
this.mainCube.addIndices(vi0, vi1, vi2);
this.mainCube.addIndices(vi2, vi3, vi0);
// East
vi0 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(1f, 1f, 1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(0f, 0f, 4f));
vi1 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(1f, -1f, 1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(0f, 1f, 4f));
vi2 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(1f, -1f, -1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(1f, 1f, 4f));
vi3 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(1f, 1f, -1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(1f, 0f, 4f));
this.mainCube.addIndices(vi0, vi1, vi2);
this.mainCube.addIndices(vi2, vi3, vi0);
// West
vi0 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(-1f, 1f, -1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(0f, 0f, 5f));
vi1 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(-1f, -1f, -1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(0f, 1f, 5f));
vi2 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(-1f, -1f, 1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(1f, 1f, 5f));
vi3 = this.mainCube.addVertex(new VertexBuffer.Vertex().setVertexCoords(-1f, 1f, 1f, 1f).setColors(1f, 1f, 1f, 1f).setTextureCoords(1f, 0f, 5f));
this.mainCube.addIndices(vi0, vi1, vi2);
this.mainCube.addIndices(vi2, vi3, vi0);
this.mainCube.buildBuffers();
this.mainCube.bufferData();
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
Test.checkForGLError("setupMainCube");
}
private void render() {
GL11.glPushMatrix();
{
long currnet_timestamp = System.currentTimeMillis();
GL11.glRotated((currnet_timestamp / 30.0) % 360.0, 1.0, 0.0, 0.0);
GL11.glRotated((currnet_timestamp / 20.0) % 360.0, 0.0, 1.0, 0.0);
GL11.glRotated((currnet_timestamp / 10.0) % 360.0, 0.0, 0.0, 1.0);
this.texture.bind();
{
this.mainCube.render();
}
this.texture.unbind();
}
GL11.glPopMatrix();
Test.checkForGLError("render");
}
private void destroy() {
this.texture.destroyBuffer();
this.mainCube.destroyBuffers();
Display.destroy();
}
public static void checkForGLError(String string) {
int errorFlag = GL11.glGetError();
if (errorFlag != GL11.GL_NO_ERROR) {
System.out.println(string + ": " + GLU.gluErrorString(errorFlag));
}
}
}
Texture3D.java:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import javax.imageio.ImageIO;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GL30;
public class Texture3D {
private static final int BYTES_PER_PIXEL = 4;
public static BufferedImage loadImage(String loc) {
try {
File file = new File(loc);
return ImageIO.read(file);
} catch (IOException e) {
// Error Handling Here
}
return null;
}
private int textureId;
private int height = 1;
private int width = 1;
private int depth = 6;
private int[] pixels;
public Texture3D(String loc) {
this.loadTexture(Texture3D.loadImage(loc));
}
void loadTexture(BufferedImage image) {
this.width = image.getWidth();
this.height = image.getHeight() / this.depth;
this.pixels = new int[this.width * this.height * 6];
image.getRGB(0, 0, this.width, this.height, this.pixels, 0, this.width);
}
public ByteBuffer buildPixelBuffer() {
ByteBuffer pixelBuffer = BufferUtils.createByteBuffer(this.width * this.height * this.depth * Texture3D.BYTES_PER_PIXEL);
for (int y = 0; y < (this.height * 6); y++) {
for (int x = 0; x < this.width; x++) {
int pixel = this.pixels[(y * this.width) + x];
pixelBuffer.put((byte) ((pixel >> 16) & 0xFF)); // Red component
pixelBuffer.put((byte) ((pixel >> 8) & 0xFF)); // Green
// component
pixelBuffer.put((byte) (pixel & 0xFF)); // Blue component
pixelBuffer.put((byte) ((pixel >> 24) & 0xFF)); // Alpha
// component
}
}
pixelBuffer.flip();
return pixelBuffer;
}
public void buildBuffer() {
this.textureId = GL11.glGenTextures();
}
public void bufferData() {
GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, this.textureId);
GL12.glTexImage3D(GL30.GL_TEXTURE_2D_ARRAY, 0, GL11.GL_RGBA8, this.width, this.height, this.depth, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, this.buildPixelBuffer());
GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, 0);
}
public void bind() {
GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, this.textureId);
}
public void unbind() {
GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, 0);
}
public void destroyBuffer() {
GL11.glDeleteTextures(this.textureId);
}
}
VertexBuffer.java:
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.LinkedList;
import java.util.List;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
public class VertexBuffer {
public static class Vertex {
public float[] pos;
public float[] color;
public float[] tex;
Vertex setVertexCoords(float x, float y, float z, float w) {
this.pos = new float[] { x, y, z, w };
return this;
}
Vertex setColors(float r, float g, float b, float a) {
this.color = new float[] { r, g, b, a };
return this;
}
Vertex setTextureCoords(float s, float t, float r) {
this.tex = new float[] { s, t, r };
return this;
}
}
public static final int stride = 11 * 4;
public static final int vertexOffset = 0 * 4;
public static final int colorOffset = 4 * 4;
public static final int texCoordOffset = 8 * 4;
List<Vertex> rawVertexs = new LinkedList<Vertex>();
List<short[]> rawIndicesSets = new LinkedList<short[]>();
private int vertexBufferHandle;
private int indexBufferHandle;
private int indexBufferLength;
private boolean dirty = false;
private boolean hasBuffers = false;
public int addVertex(Vertex vertex) {
this.rawVertexs.add(vertex);
this.dirty = true;
return this.rawVertexs.size() - 1;
}
public void addIndices(int index0, int index1, int index2) {
this.rawIndicesSets.add(new short[] { (short) index0, (short) index1, (short) index2 });
this.dirty = true;
}
public FloatBuffer getVertexBuffer() {
FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(this.rawVertexs.size() * 11);
for (Vertex vertex : this.rawVertexs) {
verticesBuffer.put(vertex.pos);
verticesBuffer.put(vertex.color);
verticesBuffer.put(vertex.tex);
}
verticesBuffer.flip();
return verticesBuffer;
}
public ShortBuffer getIndicesBuffer() {
ShortBuffer indicesBuffer = BufferUtils.createShortBuffer(this.rawIndicesSets.size() * 3);
for (short[] rawVertexIndexSet : this.rawIndicesSets) {
indicesBuffer.put(rawVertexIndexSet);
}
indicesBuffer.flip();
return indicesBuffer;
}
public int getIndicesCount() {
return this.rawIndicesSets.size() * 3;
}
public void buildBuffers() {
if (this.hasBuffers) {
return;
}
this.vertexBufferHandle = GL15.glGenBuffers();
this.indexBufferHandle = GL15.glGenBuffers();
}
public void bufferData() {
if (!this.hasBuffers) {
this.buildBuffers();
}
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.vertexBufferHandle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, this.getVertexBuffer(), GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, this.indexBufferHandle);
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, this.getIndicesBuffer(), GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
this.indexBufferLength = this.getIndicesCount();
this.dirty = false;
}
public void render() {
if (this.dirty) {
this.bufferData();
}
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.vertexBufferHandle);
{
GL11.glVertexPointer(4, GL11.GL_FLOAT, VertexBuffer.stride, VertexBuffer.vertexOffset);
GL11.glColorPointer(4, GL11.GL_FLOAT, VertexBuffer.stride, VertexBuffer.colorOffset);
GL11.glTexCoordPointer(2, GL11.GL_FLOAT, VertexBuffer.stride, VertexBuffer.texCoordOffset);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, this.indexBufferHandle);
{
GL11.glDrawElements(GL11.GL_TRIANGLES, this.indexBufferLength, GL11.GL_UNSIGNED_SHORT, 0);
}
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
}
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}
void destroyBuffers() {
if (!this.hasBuffers) {
return;
}
GL15.glDeleteBuffers(this.vertexBufferHandle);
GL15.glDeleteBuffers(this.indexBufferHandle);
}
}
texture.png: