1

所以我一直在创建一个游戏,我想支持带有着色器的 opengl 2.1 版。我根据在线教程实现了我所能做的,当我运行游戏时没有任何显示,注意:如果着色器更改为最新版本,我支持一切正常..(我使用 VBO)

以下是着色器:

片段着色器:

uniform sampler2D texture1;

varying vec4 pass_Color;
varying vec2 pass_TextureCoord;

void main(void) {

    gl_FragColor = pass_Color;

    vec2 texcoord = vec2(pass_TextureCoord.xy);
    vec4 color = texture2D(texture1, texcoord) * pass_Color  ;


    gl_FragColor = color;
}

*顶点着色器:*

attribute vec4 in_Position;
attribute vec4 in_Color;
attribute vec2 in_TextureCoord;

varying vec4 pass_Color;
varying vec2 pass_TextureCoord;

uniform vec4 cameraPos;
uniform mat4 projection;

void main(void) {

    gl_Position = ( (vec4(cameraPos.x*projection[0][0],cameraPos.y*projection[1][1],cameraPos.z*projection[0][0],cameraPos.w*projection[0][0])) + (in_Position * projection)) ;

    pass_Color = in_Color;
    pass_TextureCoord = in_TextureCoord;
}

注意:在顶点着色器中我计算了位置,这肯定是正确的,因为我使用这条精确的线来计算新着色器中的位置并且效果很好。

我如何创建 VBO:

        vboId = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vboData, GL15.GL_STATIC_DRAW);

            // Put the position coordinates in attribute list 0
            GL20.glVertexAttribPointer(GL20.glGetAttribLocation(game.getResourceManager().getShaderProgramID(), "in_Position"), TexturedVertex.positionElementCount, GL11.GL_FLOAT,
                    false, TexturedVertex.stride, TexturedVertex.positionByteOffset);
            // Put the color components in attribute list 1
            GL20.glVertexAttribPointer(GL20.glGetAttribLocation(game.getResourceManager().getShaderProgramID(), "in_Color"), TexturedVertex.colorElementCount, GL11.GL_FLOAT,
                    false, TexturedVertex.stride, TexturedVertex.colorByteOffset);
            // Put the texture coordinates in attribute list 2
            GL20.glVertexAttribPointer(GL20.glGetAttribLocation(game.getResourceManager().getShaderProgramID(), "in_TextureCoord"), TexturedVertex.textureElementCount, GL11.GL_FLOAT,
                    false, TexturedVertex.stride, TexturedVertex.textureByteOffset);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

这就是我渲染的方式:(省略无用的东西)

GL20.glUseProgram(game.getResourceManager().getShaderProgramID());

        //Send the camera location and the projection matrix
        int loc3 = GL20.glGetUniformLocation(game.getResourceManager().getShaderProgramID(), "projection");
        FloatBuffer buf2 =  BufferUtils.createFloatBuffer(16);
        GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX,buf2);
        GL20.glUniformMatrix4(loc3,false,buf2);

        int loc4 = GL20.glGetUniformLocation(game.getResourceManager().getShaderProgramID(), "cameraPos");
        GL20.glUniform4f(loc4, game.getGameCamera().getCameraLocation().x*-1*Constants.PHYS_PIXEL_TO_METER_RATIO, game.getGameCamera().getCameraLocation().y*-1*Constants.PHYS_PIXEL_TO_METER_RATIO, 0,1);

       ////////////////////////////RENDERING\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

       GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);

       GL20.glEnableVertexAttribArray(0);
       GL20.glEnableVertexAttribArray(1);
       GL20.glEnableVertexAttribArray(2);

       // Bind to the index VBO that has all the information about the order of the vertices
       GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, VBOIndeciesID);

       // Draw the vertices
       GL11.glDrawElements(GL11.GL_TRIANGLES,6 , GL11.GL_UNSIGNED_INT, 0);  

       // Put everything back to default (deselect)
       GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);

       GL20.glDisableVertexAttribArray(0);
       GL20.glDisableVertexAttribArray(1);
       GL20.glDisableVertexAttribArray(2);


GL20.glUseProgram(0);

编辑:

我检查了我遗漏的几乎所有内容是否有错误,因为它没有给我任何输出。这是我检查错误的方法:

private void exitOnGLError(String errorMessage) { //Method to check if any opengl errors occured
    int errorValue = GL11.glGetError();

    if (errorValue != GL11.GL_NO_ERROR) {
        String errorString = GLU.gluErrorString(errorValue);
        System.err.println("ERROR - " + errorMessage + ": " + errorString);

        if (Display.isCreated()) Display.destroy();
        System.exit(-1);
    }
}

链接着色器:

        loadShader("res/shaders/vert21.glsl",GL20.GL_VERTEX_SHADER);
        loadShader("res/shaders/frag21.glsl",GL20.GL_FRAGMENT_SHADER);

    shaderProgramID = GL20.glCreateProgram(); //Create a new shader program
    for (Integer id : shaders) {
        GL20.glAttachShader(shaderProgramID, id);  //attach all the custom shaders to the program
    }

    // Position information will be attribute 0
    GL20.glBindAttribLocation(shaderProgramID, 0, "in_Position");
    // Color information will be attribute 1
    GL20.glBindAttribLocation(shaderProgramID, 1, "in_Color");
    // Textute information will be attribute 2
    GL20.glBindAttribLocation(shaderProgramID, 2, "in_TextureCoord");

    GL20.glLinkProgram(shaderProgramID);        //Link the program to lwjgl
    GL20.glValidateProgram(shaderProgramID);    //Compile and make sure program was setup correctly

    GL20.glUseProgram(shaderProgramID);   //Use the program so we can pick the texture unit before continuing

    setTextureUnit0(shaderProgramID); //pick the unit and set it in the shader to use

加载和编译:

StringBuilder shaderSource = new StringBuilder();
    int shaderID = 0;

    try {  //Read shader
        BufferedReader reader = new BufferedReader(new InputStreamReader(ClassLoader.getSystemResourceAsStream(filename)));
        String line;
        while ((line = reader.readLine()) != null) {
            shaderSource.append(line).append("\n");
        }
        reader.close();
    } catch (IOException e) {
        System.err.println("Could not read file.");
        Logger.getGlobal().log(Level.WARNING, e.getMessage(), e);
        System.exit(-1);
    } catch (NullPointerException e) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader( new FileInputStream(filename)));
            String line;
            while ((line = reader.readLine()) != null) {
                shaderSource.append(line).append("\n");
            }
            reader.close();
        } catch (IOException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    shaderID = GL20.glCreateShader(type); //Create shader
    GL20.glShaderSource(shaderID, shaderSource); //Link source
    GL20.glCompileShader(shaderID);   //Compile

    //Check if compiled correctly
    if (GL20.glGetShaderi(shaderID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
        System.err.println(shaderID+" | Shader wasn't able to be compiled correctly.");
        System.out.println(GL20.glGetShaderInfoLog(shaderID,GL20.glGetShaderi(shaderID,GL20.GL_INFO_LOG_LENGTH)));
    }

    this.exitOnGLError("loadShader");
4

0 回答 0