OpenGL 3.2纹理不显示

时间:2013-12-23 16:04:42

标签: java shader lwjgl slick2d opengl-3

我正在尝试创建一个类来加速我的程序中纹理多边形的生成,但纹理没有显示。

我正在使用带有Slick2D的LWJGL进行纹理加载。

这是我的TexturedPolygon课程:

package zeus.core.geom;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.util.vector.Vector2f;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

import zeus.core.Disposable;

public class TexturedPolygon extends Disposable {

    private final List<Float> vertices = new ArrayList<Float>();
    private final List<Float> texCoords = new ArrayList<Float>();
    private final int VAO, VBO;
    private final FloatBuffer vertBuffer;
    private final ByteBuffer texBuffer;
    private final int GL_DRAWING_MODE;
    private final String IMG_FILE;
    private final Texture TEXTURE;

    public TexturedPolygon(final LinkedHashMap<Vector2f, Vector2f> info, final String IMG_FILE, final int GL_DRAWING_MODE) throws IOException {

        this.IMG_FILE = IMG_FILE;
        this.GL_DRAWING_MODE = GL_DRAWING_MODE;
        this.TEXTURE = TextureLoader.getTexture(".png", new FileInputStream(new File(System.getProperty("user.dir") + "/res/img/", IMG_FILE)));

        for(Map.Entry<Vector2f, Vector2f> entry : info.entrySet()) {
            vertices.add(entry.getKey().x);
            vertices.add(entry.getKey().y);

            texCoords.add(entry.getValue().x);
            texCoords.add(entry.getValue().y);
        }

        vertBuffer = BufferUtils.createFloatBuffer(vertices.size());

        final float[] vertArray = new float[vertices.size()];
        int i = 0;
        for(final float f : vertices) {
            vertArray[i++] = f;
        }

        vertBuffer.put(vertArray);
        vertBuffer.flip();

        VAO = GL30.glGenVertexArrays();
        GL30.glBindVertexArray(VAO);

        VBO = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBO);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertBuffer, GL15.GL_STATIC_DRAW);
        GL20.glEnableVertexAttribArray(0);

        GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 0, 0);
        GL30.glBindVertexArray(VAO);

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, TEXTURE.getTextureID());

        texBuffer = BufferUtils.createByteBuffer(TEXTURE.getTextureData().length);
        texBuffer.put(TEXTURE.getTextureData());
        texBuffer.flip();

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, TEXTURE.getTextureID());
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, texBuffer, GL15.GL_STATIC_DRAW);
        GL20.glVertexAttribPointer(1, 4, GL11.GL_FLOAT, false, 0, 0);
        GL20.glEnableVertexAttribArray(1);

    }

    public void draw() {
        GL30.glBindVertexArray(VAO);
        GL11.glDrawArrays(GL_DRAWING_MODE, 0, vertices.size());
        GL30.glBindVertexArray(0);
    }

    public void update(final int delta) {

    }

    @Override
    public void dispose() {
        GL20.glDisableVertexAttribArray(0);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
        GL15.glDeleteBuffers(VBO);

        GL30.glBindVertexArray(0);
        GL30.glDeleteVertexArrays(VAO);

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

    public int getVAO() {
        return VAO;
    }

    public int getVBO() {
        return VBO;
    }

    public int getDrawingMode() {
        return GL_DRAWING_MODE;
    }

    public FloatBuffer getVertBuffer() {
        return vertBuffer;
    }

    public ByteBuffer getTextureBuffer() {
        return texBuffer;
    }

    public List<Float> getVertices() {
        return vertices;
    }

    public List<Float> getTexCoords() {
        return texCoords;
    }

    public String getImageFilename() {
        return IMG_FILE;
    }

}

这是我的主要课程:

package tests;

import java.util.LinkedHashMap;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import org.lwjgl.util.vector.Vector2f;

import zeus.core.Window;
import zeus.core.geom.TexturedPolygon;
import zeus.core.opengl.Shader;
import zeus.core.opengl.ShaderProgram;

public class WindowTest {

    public static void main(String[] args) throws Exception {

        final Window win = new Window(800, 600, "Window Test");

        win.create();

        Shader vertexShader = new Shader("drawing_test_vertex", GL20.GL_VERTEX_SHADER);
        Shader fragmentShader = new Shader("drawing_test_fragment", GL20.GL_FRAGMENT_SHADER);
        ShaderProgram program = new ShaderProgram();

        vertexShader.compile();
        fragmentShader.compile();

        LinkedHashMap<Vector2f, Vector2f> info = new LinkedHashMap<Vector2f, Vector2f>();

        info.put(new Vector2f(0f, 0f), new Vector2f(0f, 0f));
        info.put(new Vector2f(1f, 0f), new Vector2f(1f, 0f));
        info.put(new Vector2f(1f, 1f), new Vector2f(1f, 1f));

        info.put(new Vector2f(1f, 1f), new Vector2f(1f, 1f));
        info.put(new Vector2f(0f, 1f), new Vector2f(0f, 1f));
        info.put(new Vector2f(0f, 0f), new Vector2f(0f, 0f));

        TexturedPolygon p = new TexturedPolygon(info, "test.png", GL11.GL_TRIANGLES);

        program.addShader(vertexShader);
        program.addShader(fragmentShader);

        program.link();
        program.use();

        while(!win.isCloseRequested()) {
            win.clear();

            p.update(1);
            p.draw();

            win.update(120);
        }

        p.dispose();

        vertexShader.dispose();
        fragmentShader.dispose();
        program.dispose();

        win.dispose();

    }

}

这些是我的着色器文件:

顶点:

#version 410

layout(location=0) vec3 pos;
layout(location=1) vec2 tex;

out smooth vec2 Tex;

void main() {
    gl_Position = vec4(pos, 1.0f);
    Tex = tex;
}

片段:

#version 410

layout(location=0) vec2 Tex;

uniform sampler2D textureDiffuse;

out vec4 color;

void main() {
    color = texture(textureDiffuse, Tex);
}

我不知道造成这个问题的原因。

这是目前正在显示的内容: Current Result

当我在此行上更改1到4之间的'size'参数时,此处的颜色会发生变化:

GL20.glVertexAttribPointer(1, 4, GL11.GL_FLOAT, false, 0, 0);

1 个答案:

答案 0 :(得分:0)

我没有检查过你的所有代码,但你的着色器坏了。您没有声明正确的in变量,因此您基本上使用未初始化的数据。那些着色器甚至可以使用layout限定符来编译全局变量吗?

顶点着色器应更改为:

layout(location=0) in vec3 pos;
layout(location=1) in vec2 tex;

类似于片段着色器:

in vec2 Tex;

请注意,位置仅适用于某些主机&lt; - &gt;的情况。 GL通信(如制服,顶点属性和片段着色器输出),但是GL管道内部没有着色器输入和输出的位置,因此您无法为片段着色器输入声明位置。这些变量与名称相匹配。