lwjgl中的GL_TEXTURE_2D_ARRAY无法工作

3
我在使用lwjgl中的GL_TEXTURE_2D_ARRAY替换GL_TEXTURE_2D时遇到了问题。所有的纹理都只显示为纯白色。我错过了什么吗?
还有,有没有人知道一个简单的示例程序,它在lwjgl中使用GL_TEXTURE_2D_ARRAY,或者至少是另一种基于C的编程语言的示例程序,其中包含从头到尾的编译说明?
我想要的: Image showing textures 我得到的: Image showing nontextured white cube 我已经尝试将项目逻辑最小化,以减少代码。
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:

texture for covering cube

1个回答

1
似乎您正在尝试在固定管线中使用数组纹理。这在GL中是不可能的,如果要访问数组纹理,则必须使用着色器。 GL11.glEnable(GL12.GL_TEXTURE_3D); 启用了一个3D纹理,但您没有绑定它,而GL_TEXTURE_2D_ARRAY不是3D纹理,只是由于输入数据布局的相似性而重用TexImage3D()函数。
如果您真的想使用固定管线,您可能可以使用真正的3D纹理 - 并且必须接受其缺点(层间过滤和mipmapping、较低的纹理尺寸限制等)。
我的建议是放弃已弃用的固定管线并切换到着色器。

作为一个初学者,我总是觉得着色器非常令人困惑。我不会建议初学者立即转向着色器,因为这是一个全新的世界,我个人发现需要先熟悉OpenGL代码,然后逐渐进入着色器。是的,我确实使用着色器,但即使现在,有时我仍然使用一些固定功能管道的东西。 - Joehot200
@Joehot200:嗯,人们都有个人经历和观点。我真的很努力将我的回答中的事实与个人观点(在一定程度上)分开,这就是为什么我提到了3D纹理作为一个可能的替代方案。然而,在_我_的观点中,现在开始使用GL的人根本不应该再考虑使用固定功能管线了。根据我的经验,当一个人习惯于固定功能的东西时,使用着色器会变得更加混乱,而不是从零开始学习。你的情况可能会有所不同。 - derhass
很不幸,三年前我只能从TheCodingUniverse的教程中学习,这些教程都是使用固定功能或复制的着色器。现在有像ThinMatrix这样的人出现了,他们迫使我学习着色器,感觉就像另一个世界。我个人不会强迫初学者去学习它。然而,我绝对同意从早期阶段开始学习着色器比晚期更好。 - Joehot200

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接