LWJGL 3着色器无法渲染

时间:2015-08-22 09:18:34

标签: java opengl shader lwjgl

我正在渲染一个简单的四边形并且一直在尝试使用着色器。它工作得很好,但我按照纹理教程添加纹理,我必须改变一些东西。因为现在,即使所有纹理代码都被注释掉了,当我打开着色器时四边形也不会渲染。我怀疑它与数据绑定有关,因为我仍在努力学习它。我不能为我的生活发现什么是错的!

着色器(顶点和片段):

//vertex
#version 400 core

in vec3 position;
out vec3 colour;

void main (void) {

colour = vec3(position.x + 0.5, 1.0, position.y + 0.5);

}

//fragment
#version 400 core

in vec3 colour;
out vec4 out_Colour;

void main (void) {

out_Colour = vec4(colour, 1.0);

}

着色器程序:

public abstract class ShaderProgram {

private int programId;
private int vertexId;
private int fragmentId;

public ShaderProgram (String vertexFile, String fragmentFile) {

    vertexId = loadShader(vertexFile, GL20.GL_VERTEX_SHADER);
    fragmentId = loadShader(fragmentFile, GL20.GL_FRAGMENT_SHADER);
    programId = GL20.glCreateProgram();
    GL20.glAttachShader(programId, vertexId);
    GL20.glAttachShader(programId, fragmentId);
    bindAttributes();
    GL20.glLinkProgram(programId);
    GL20.glValidateProgram(programId);


}

public void start () {
    GL20.glUseProgram(programId);
}

public void stop () {
    GL20.glUseProgram(0);
}

public void cleanUp () {
    stop();
    GL20.glDetachShader(programId, vertexId);
    GL20.glDetachShader(programId, fragmentId);
    GL20.glDeleteShader(vertexId);
    GL20.glDeleteShader(fragmentId);
    GL20.glDeleteProgram(programId);
}

protected abstract void bindAttributes ();

protected void bindAttribute (int attribute, String variableName) {
    GL20.glBindAttribLocation(programId, attribute, variableName);
}

private static int loadShader (String file, int type) {

    StringBuilder shaderSource = new StringBuilder();
    try {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line;
        while ((line = reader.readLine()) != null) {
            shaderSource.append(line).append("\n");
        }
        reader.close();
    } catch (IOException e) {
        System.err.println("Could not read shader file!");
        e.printStackTrace();
        System.exit(-1);
    }
    int shaderId = GL20.glCreateShader(type);
    GL20.glShaderSource(shaderId, shaderSource);
    GL20.glCompileShader(shaderId);
    if (GL20.glGetShaderi(shaderId, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
        System.err.println(GL20.glGetShaderInfoLog(shaderId));
        System.err.println("Could not compile shader!");
        System.exit(-1);
    }
    return shaderId;
}

}

主着色器(扩展着色器程序):

public class MainShader extends ShaderProgram {

private static final String VERTEX_FILE = "src/shaders/vertexShader.txt";
private static final String FRAGMENT_FILE = "src/shaders/fragmentShader.txt";

public MainShader() {
    super(VERTEX_FILE, FRAGMENT_FILE);
}

@Override
protected void bindAttributes() {
    super.bindAttribute(ModelLoader.VERTEX_INDICE, "position");
    //super.bindAttribute(1, "uv");
}

}

模型加载器:

public class ModelLoader {

private static List<Integer> vaos = new ArrayList<Integer>();
private static List<Integer> vbos = new ArrayList<Integer>();
private static List<Integer> textures = new ArrayList<Integer>();
public static final int VERTEX_INDICE = 0;
public static final int UV_INDICE = 1;

public static RawModel loadToVAO (float[] positions, int[] indices) {

    int vaoId = createVAO();
    bindIndicesBuffer(indices);
    storeDataInAttributeList(VERTEX_INDICE, 3, positions);
    //storeDataInAttributeList(UV_INDICE, 2, uvCoords);
    unbindVAO();
    return new RawModel(vaoId, indices.length);

}

/*public static int loadTexture (String file, int textureUnit) {

    try {
        return TextureLoader.loadTexture("rsrc/" + file + ".png", GL11.GL_TEXTURE_2D);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return 0;
}*/

public static int loadTexture(String file, int textureUnit) {
    String filename = "rsrc/" + file + ".png";
    ByteBuffer buf = null;
    int tWidth = 0;
    int tHeight = 0;

    try {
        // Open the PNG file as an InputStream
        InputStream in = new FileInputStream(filename);
        // Link the PNG decoder to this stream
        PNGDecoder decoder = new PNGDecoder(in);

        // Get the width and height of the texture
        tWidth = decoder.getWidth();
        tHeight = decoder.getHeight();


        // Decode the PNG file in a ByteBuffer
        buf = ByteBuffer.allocateDirect(
                4 * decoder.getWidth() * decoder.getHeight());
        decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA);
        buf.flip();

        in.close();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(-1);
    }

    // Create a new texture object in memory and bind it
    int texId = GL11.glGenTextures();
    GL13.glActiveTexture(textureUnit);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);

    // All RGB bytes are aligned to each other and each component is 1 byte
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);

    // Upload the texture data and generate mip maps (for scaling)
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tWidth, tHeight, 0, 
            GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
    GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);

    // Setup the ST coordinate system
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);

    // Setup what to do when the texture has to be scaled
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, 
            GL11.GL_NEAREST);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, 
            GL11.GL_LINEAR_MIPMAP_LINEAR);

    return texId;
}

private static int createVAO () {

    int vaoId = GL30.glGenVertexArrays();
    vaos.add(vaoId);
    GL30.glBindVertexArray(vaoId);
    return vaoId;

}

private static void bindIndicesBuffer (int[] indices) {

    int vbo = GL15.glGenBuffers();
    vbos.add(vbo);
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vbo);
    IntBuffer buffer = storeDataInIntBuffer(indices);
    GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);

}

public static void cleanUp () {

    for (int vao : vaos) {
        GL30.glDeleteVertexArrays(vao);
    }
    for (int vbo : vbos) {
        GL15.glDeleteBuffers(vbo);
    }
    for (int texture : textures) {
        GL11.glDeleteTextures(texture);
    }

}

private static void storeDataInAttributeList (int attributeNumber, int size, float[] data) {

    int vboId = GL15.glGenBuffers();
    vbos.add(vboId);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
    FloatBuffer buffer = storeDataInFloatBuffer(data);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
    GL20.glVertexAttribPointer(attributeNumber, size, GL11.GL_FLOAT, false, 0, 0);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

}

private static void unbindVAO () {

    GL30.glBindVertexArray(0);

}

private static IntBuffer storeDataInIntBuffer (int[] data) {

    IntBuffer buffer = BufferUtils.createIntBuffer(data.length);
    buffer.put(data);
    buffer.flip();
    return buffer;

}

private static FloatBuffer storeDataInFloatBuffer (float[] data) {

    FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
    buffer.put(data);
    buffer.flip();
    return buffer;

}

}

1 个答案:

答案 0 :(得分:0)

我忘了在顶点着色器中指定gl_Position。