错误C5145:必须写入gl_Position

时间:2016-01-20 22:11:57

标签: java glsl jogl opengl-3

我在1小时内制作了这个简单的"""" 2d基于纹理的游戏引擎""""。我在GLSL程序中发现了一个错误(C5145:"必须写入gl_Position"): 这是我的调试输出:

顶点信息

(0):错误C5145:必须写入gl_Position

这里是glsl代码:

    private static final String[] VERTEX_SOURCE = {
        "#version 150 core\n",
        // vertices sent by vbo
        "layout(location=0) in vec2 position;", // 0a
        "layout(location=1) in vec2 texCoord;", // 1a
        // output data to use in the fragment
        "out vec4 Color;",
        "out vec2 TexCoord;",
        // uniforms values
        "layout(location=0) uniform vec4 color = vec3(0.0f, 0.0f, 0.0f, 0.0f);", // 0u
        "layout(location=1) uniform mat4 model;", // 1u
        // -----------
        "void main()",
        "{",
        "   Color = color;",
        "   TexCoord = texCoord;",
        //                              (v) default z
        "   gl_Position = vec4(position, 1.0f, 1.0f) * model;", // the error is here
        "}"
};

最后我如何调试它......

    public static String printProgramInfoLog(GL3 gl, int obj) {
    // get the GL info log
    final int logLen = getProgramParameter(gl, obj, GL3.GL_INFO_LOG_LENGTH);
    if (logLen <= 0)
        return "";

    // Get the log
    final int[] retLength = new int[1];
    final byte[] bytes = new byte[logLen + 1];
    gl.glGetProgramInfoLog(obj, logLen, retLength, 0, bytes, 0);
    final String logMessage = new String(bytes);

    return logMessage;
}

public static int getProgramParameter(GL3 gl, int obj, int paramName) {
    final int params[] = new int[1];
    gl.glGetProgramiv(obj, paramName, params, 0);
    return params[0];
}

这是我初始化缓冲区和编译着色器的地方:

    @Override
public void init(GLAutoDrawable glad) {
    final GL3 gl = glad.getGL().getGL3();
    // init OpenGL functions (buffers, shader)
    gl.glEnable(GL_TEXTURE_2D);
    System.out.println("Enabled useful things...");
    // first generates vao and store next buffers in
    final IntBuffer genVAO = IntBuffer.allocate(4);
    gl.glGenVertexArrays(1, genVAO);
    vao = genVAO.get();
    gl.glBindVertexArray(vao);
    System.out.println("VAO bound.");
    // generates vbo buffer array and saves default array on
    final IntBuffer genVBO = IntBuffer.allocate(4);
    gl.glGenBuffers(1, genVBO);
    gl.glBindBuffer(GL_ARRAY_BUFFER, genVBO.get());
    gl.glBufferData(GL_ARRAY_BUFFER, DEF_VBO_ARRAY.array().length, DEF_VBO_ARRAY, GL_STATIC_DRAW);
    System.out.println("VBO stored in the VAO bound before.");
    // generates ebo buffer array and saves default array on
    final IntBuffer genEBO = IntBuffer.allocate(4);
    gl.glGenBuffers(1, genEBO);
    gl.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, genEBO.get());
    gl.glBufferData(GL_ELEMENT_ARRAY_BUFFER, DEF_EBO_ARRAY.array().length, DEF_EBO_ARRAY, GL_STATIC_DRAW);
    System.out.println("EBO stored in the VBO.");

    // compile vertex shader
    final int vs = gl.glCreateShader(GL_VERTEX_SHADER);
    gl.glShaderSource(vs, 1, VERTEX_SOURCE, IntBuffer.allocate(0)); // IntBuffer.allocate(0) = NULL (?)
    gl.glCompileShader(vs);
    if (isShaderCompiled(gl, vs))
        System.out.println("Vertex shader compiled.");
    // compile fragment shader
    final int fs = gl.glCreateShader(GL_FRAGMENT_SHADER);
    gl.glShaderSource(fs, 1, FRAGMENT_SOURCE, IntBuffer.allocate(0));
    gl.glCompileShader(fs);
    if (isShaderCompiled(gl, fs))
        System.out.println("Fragment shader compiled.");
    // create program attach shader
    final int program = gl.glCreateProgram();
    gl.glAttachShader(program, vs);
    gl.glAttachShader(program, fs);
    gl.glLinkProgram(program);
    gl.glUseProgram(program);
    System.out.println("Program created and shader has been attached to.");
    System.out.println(printProgramInfoLog(gl, program));

    // links vertices to the shader program
    gl.glEnableVertexAttribArray(0); // position-attribute id
    gl.glVertexAttribPointer(0, 2, GL_FLOAT, false, 4 * Float.BYTES, 0); // 2 par. -> first 2 floats, last par. -> from 0
    gl.glEnableVertexAttribArray(1); // texCoords-attribute id
    gl.glVertexAttribPointer(1, 2, GL_FLOAT, false, 4 * Float.BYTES, 3);
    System.out.println("Attributes linked.");
}

这里是显示事件,其中包括&#34; draw&#34;功能:

    @Override
public void display(GLAutoDrawable glad) {
    final GL3 gl = glad.getGL().getGL3();
    gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear buffers
    gl.glClearColor(0, 0, 0, 0);
    for (Drawable d : drawables) // draw each drawable
        d.draw(gl, this);
}

这里是绘制功能:

    @Override
public void draw(GL3 gl, DrawingProcess dp) {
    // move and scale the rectangle of the given value
    final Matrix4 m = new Matrix4();
    m.translate(1.0f, 1.0f, 1.0f); // temp values
    m.scale(1.0f, 1.0f, 1.0f); // temp values
    // set shader program uniforms
    gl.glUniform4f(0, color.getR(), color.getG(), color.getB(), color.getA()); // set color
    gl.glUniformMatrix4fv(1, 1, false, FloatBuffer.wrap(m.getMatrix())); // set created matrix
    texHand.ifPresent(TextureHandler::use); // bind texture if present
    // bind vao and draw
    gl.glBindVertexArray(dp.getVAO());
    gl.glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}

0 个答案:

没有答案