vbos着色器从OGLES 1.0转到2.0

时间:2012-05-23 02:47:03

标签: opengl-es-2.0 shader vbo

我花了大约一周时间学习和使用OGLES 1.0。我意识到我需要使用2.0来获得一些我觉得很吸引人的功能。我一直在努力将1.0转换为2.0几天。我终于达到了一个点,我没有得到错误(我可以找到)并且有一个屏幕渲染。我所看到的只是清晰的色彩。我试过改变周围的东西并阅读ALOT来弄清楚我的问题是什么,但我找不到答案:(所以我在这里问你为什么我所看到的都是clearcolor。假设我的解析器是正确的(它正在工作完美无缺的1.0)我的BufferObjects /数组/索引是正确的(它们是1.0)我的问题出现在我发布的代码中。请非常感谢任何我需要开始寻找的方向。提前致谢。< / p>

(请原谅未使用的变量和方法等) 渲染器

public class TestRenderer implements Renderer {
private static final String TAG = TestRenderer.class.getSimpleName();
Cube tester;
Parser parser;
Context context;

private int mProgram;
private int muMVPMatrixHandle;
private float[] mMVPMatrix = new float[16];
private float[] mMMatrix = new float[16];
private float[] mVMatrix = new float[16];
private float[] mProjMatrix = new float[16];
private String vertexShaderCode = "attribute vec4 a_Position; "
        + "attribute vec3 a_Normal; " + "attribute vec2 a_Textcoords; "
        + "varying vec2 v_Textcoords;" + "uniform mat4 uMVPMatrix;  "

        + "attribute vec4 vPosition;  " + "void main(){ "
        + "v_Textcoords = a_Textcoords;"
        + " gl_Position = uMVPMatrix * vPosition; " +

        "}  ";

private String fragmentShaderCode = "precision mediump float; "
        + "varying vec2 v_Textcoords;" + "uniform sampler2D u_Texture; "
        + "void main(){ "
        + " gl_FragColor = texture2D(u_Texture, v_Textcoords); " + "} ";

TestRenderer(Context context) {
    this.context = context;
    parser = new Parser(context);
    parser.parse("Turret2.obj");
    tester = new Cube(parser.v, parser.f, parser.vt, parser.vtPointer,
            parser.vn, parser.vnPointer, 1);
}

public static void checkGLError(String msg) {
    int e = GLES20.glGetError();
    if (e != GLES20.GL_NO_ERROR) {
        Log.d(TAG, "GLES20 ERROR: " + msg + " " + e);
        Log.d(TAG, errString(e));
    }
}

public static String errString(int ec) {
    switch (ec) {
    case GLES20.GL_NO_ERROR:
        return "No error has been recorded.";
    case GLES20.GL_INVALID_ENUM:
        return "An unacceptable value is specified for an enumerated argument.";
    case GLES20.GL_INVALID_VALUE:
        return "A numeric argument is out of range.";
    case GLES20.GL_INVALID_OPERATION:
        return "The specified operation is not allowed in the current state.";
    case GLES20.GL_INVALID_FRAMEBUFFER_OPERATION:
        return "The command is trying to render to or read from the framebuffer"
                + " while the currently bound framebuffer is not framebuffer complete (i.e."
                + " the return value from glCheckFramebufferStatus is not"
                + " GL_FRAMEBUFFER_COMPLETE).";
    case GLES20.GL_OUT_OF_MEMORY:
        return "There is not enough memory left to execute the command."
                + " The state of the GL is undefined, except for the state"
                + " of the error flags, after this error is recorded.";
    default:
        return "UNKNOW ERROR";
    }
}

public void onDrawFrame(GL10 unused) {
    // TODO Auto-generated method stub
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
    GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
    tester.draw(mProgram);
    checkGLError("onDrawFrame 0");

}

public void onSurfaceChanged(GL10 unused, int width, int height) {
    GLES20.glViewport(0, 0, width, height);

    float ratio = (float) width / height;

    Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, .001f, 100);
    muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    Matrix.setLookAtM(mVMatrix, 0, 0, 0, 0, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

}

public void onSurfaceCreated(GL10 unused, EGLConfig config) {
    GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);

    mProgram = GLES20.glCreateProgram();
    checkGLError("onSurfaceCreated 3");

    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    checkGLError("onSurfaceCreated 1");

    int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER,
            fragmentShaderCode);

    GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader
                                                    // to program

    checkGLError("onSurfaceCreated 5");

    GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment
                                                        // shader to program
    checkGLError("onSurfaceCreated 2");

    GLES20.glLinkProgram(mProgram);
    checkGLError("onSurfaceCreated 7");
    Log.d(TAG,
            "link program true/false 1 = "
                    + GLES20.glGetProgramInfoLog(mProgram));

    GLES20.glUseProgram(mProgram);
    checkGLError("onDrawFrame 2");
    checkGLError("onSurfaceCreated 8");
    Bitmap bmp = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.turretbottom);
    tester.loadTextures(context, bmp);
}

private int loadShader(int type, String shaderCode) {

    int shader = GLES20.glCreateShader(type);
    GLES20.glShaderSource(shader, shaderCode);
    GLES20.glCompileShader(shader);

    Log.d(TAG, "Shader info log = " + GLES20.glGetShaderInfoLog(shader));

    return shader;
}

}

public void draw(int program) {

    GLES20.glGenBuffers(4, vboIds, 0);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboIds[0]);
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, verts.length, vertBuff,
            GLES20.GL_STATIC_DRAW);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboIds[1]);
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, textVerts.length, textBuff,
            GLES20.GL_STATIC_DRAW);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboIds[2]);
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, normPoints.length,
            normBuff, GLES20.GL_STATIC_DRAW);

    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, vboIds[3]);
    GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, 2 * indexa.length,
            faceBuff, GLES20.GL_STATIC_DRAW);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboIds[0]);
    GLES20.glEnableVertexAttribArray(0);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboIds[1]);
    GLES20.glEnableVertexAttribArray(1);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboIds[2]);
    GLES20.glEnableVertexAttribArray(2);

    GLES20.glVertexAttribPointer(0, 3, GLES20.GL_FLOAT, false, 0, 0);

    GLES20.glVertexAttribPointer(1, 2, GLES20.GL_FLOAT, false, 0, 0);

    GLES20.glVertexAttribPointer(2, 3, GLES20.GL_FLOAT, false, 0, 0);

    GLES20.glBindAttribLocation(program, 0, "a_Position");
    GLES20.glBindAttribLocation(program, 1, "a_Textcoords");
    GLES20.glBindAttribLocation(program, 2, "a_Normal");

    GLES20.glDrawElements(GLES20.GL_TRIANGLES, indexa.length,
            GLES20.GL_UNSIGNED_SHORT, 0);
    GLES20.glDeleteBuffers(4, vboIds, 0);

}

1 个答案:

答案 0 :(得分:1)

你会立刻咬掉很多东西,如果你没有得到任何东西(没有纹理,没有颜色,只有单个三角形),我可能会从更简单的东西开始,当你开始工作时从那里积累起来。

那就是说,我确实看到一个错误,因为glBindAttribLocation只在下一次调用glLinkProgram后生效,所以你对glBindAttribLocation的调用没有做任何事情(你需要在链接之前绑定位置)着色器)。

当您编译/链接着色器时,您应该使用glGetShaderiv(GL_COMPILE_STATUS)/ glGetProgramiv(GL_LINK_STATUS)检查结果。这比检查信息日志更好地检查成功/失败。