OpenGL不会产生预期的结果

时间:2014-07-02 15:46:12

标签: android opengl-es

我试图编写一个简单的Android OpenGL程序。

这是我的渲染器

private final int BYTE_PER_FLOAT = 4;
private final int POSITION_COMPONENT_COUNT = 2;
private FloatBuffer vertexbuffer;

private static final String U_COLOR = "u_Color";
private int uColorLocation;

private static final String A_POSITION = "a_Position";
private int aPositionLocation;

private int program;

private Context context;

public MyRenderer(Context context) {
    this.context = context;

    float[] vertices = {
            // Triangle 1
            -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f,

            // Triangle 2
            -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f };

    vertexbuffer = ByteBuffer
            .allocateDirect(vertices.length * BYTE_PER_FLOAT)
            .order(ByteOrder.nativeOrder()).asFloatBuffer();
    vertexbuffer.put(vertices);
    vertexbuffer.position(0);
};

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {

    glClearColor(0.0f, 1.0f, 0.0f, 0.0f);

    String vertexShaderCode = TextResourceReader.readTextFileFromResource(
            context, R.raw.simple_vertex_shader);
    String fragmentShaderCode = TextResourceReader
            .readTextFileFromResource(context, R.raw.simple_fragment_shader);

    int vertexShader = ShaderHelper.compileVertexShader(vertexShaderCode);
    int fragmentShader = ShaderHelper
            .compileFragmentShader(fragmentShaderCode);

    program = ShaderHelper.linkProgram(vertexShader, fragmentShader);

    if (LoggerConfig.ON) {
        ShaderHelper.validateProgram(program);
    }

    glUseProgram(program);

    uColorLocation = glGetUniformLocation(program, U_COLOR);
    aPositionLocation = glGetAttribLocation(program, A_POSITION);

    glVertexAttribPointer(aPositionLocation, POSITION_COMPONENT_COUNT,
            GL_FLOAT, false, 0, vertexbuffer);

    glEnableVertexAttribArray(aPositionLocation);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {

    glViewport(0, 0, width, height);

}

@Override
public void onDrawFrame(GL10 gl) {

    glClear(GL_COLOR_BUFFER_BIT);

    glUniform4f(uColorLocation, 0.0f, 0.0f, 1.0f, 1.0f);
    glDrawArrays(GL_TRIANGLES, 0, 6);
}

这是我的ShaderHelper类

private static final String TAG = "ShaderHelper";

/**
 * Loads and compiles a vertex shader, returning the OpenGL object ID.
 */
public static int compileVertexShader(String shaderCode) {
    return compileShader(GL_VERTEX_SHADER, shaderCode);
}

/**
 * Loads and compiles a fragment shader, returning the OpenGL object ID.
 */
public static int compileFragmentShader(String shaderCode) {
    return compileShader(GL_FRAGMENT_SHADER, shaderCode);
}

/**
 * Compiles a shader, returning the OpenGL object ID.
 */
private static int compileShader(int type, String shaderCode) {

    // Create a new shader object.
    final int shaderObjectId = glCreateShader(type);

    if (shaderObjectId == 0) {
        if (LoggerConfig.ON) {
            Log.w(TAG, "Could not create new shader.");
        }

        return 0;
    }

    // Pass in the shader source.
    glShaderSource(shaderObjectId, shaderCode);

    // Compile the shader.
    glCompileShader(shaderObjectId);

    // Get the compilation status.
    final int[] compileStatus = new int[1];
    glGetShaderiv(shaderObjectId, GL_COMPILE_STATUS, compileStatus, 0);

    if (LoggerConfig.ON) {
        // Print the shader info log to the Android log output.
        Log.v(TAG, "Results of compiling source:" + "\n" + shaderCode
                + "\n:" + glGetShaderInfoLog(shaderObjectId));
    }

    // Verify the compile status.
    if (compileStatus[0] == 0) {
        // If it failed, delete the shader object.
        glDeleteShader(shaderObjectId);

        if (LoggerConfig.ON) {
            Log.w(TAG, "Compilation of shader failed.");
        }

        return 0;
    }

    // Return the shader object ID.
    return shaderObjectId;
}

/**
 * Links a vertex shader and a fragment shader together into an OpenGL
 * program. Returns the OpenGL program object ID, or 0 if linking failed.
 */
public static int linkProgram(int vertexShaderId, int fragmentShaderId) {

    // Create a new program object.
    final int programObjectId = glCreateProgram();

    if (programObjectId == 0) {
        if (LoggerConfig.ON) {
            Log.w(TAG, "Could not create new program");
        }

        return 0;
    }

    // Attach the vertex shader to the program.
    glAttachShader(programObjectId, vertexShaderId);
    // Attach the fragment shader to the program.
    glAttachShader(programObjectId, fragmentShaderId);

    // Link the two shaders together into a program.
    glLinkProgram(programObjectId);

    // Get the link status.
    final int[] linkStatus = new int[1];
    glGetProgramiv(programObjectId, GL_LINK_STATUS, linkStatus, 0);

    if (LoggerConfig.ON) {
        // Print the program info log to the Android log output.
        Log.v(TAG, "Results of linking program:\n"
                + glGetProgramInfoLog(programObjectId));
    }

    // Verify the link status.
    if (linkStatus[0] == 0) {
        // If it failed, delete the program object.
        glDeleteProgram(programObjectId);
        if (LoggerConfig.ON) {
            Log.w(TAG, "Linking of program failed.");
        }
        return 0;
    }

    // Return the program object ID.
    return programObjectId;
}

/**
 * Validates an OpenGL program. Should only be called when developing the
 * application.
 */
public static boolean validateProgram(int programObjectId) {
    glValidateProgram(programObjectId);

    final int[] validateStatus = new int[1];
    glGetProgramiv(programObjectId, GL_VALIDATE_STATUS, validateStatus, 0);
    Log.v(TAG, "Results of validating program: " + validateStatus[0]
            + "\nLog:" + glGetProgramInfoLog(programObjectId));

    return validateStatus[0] != 0;
}

顶点着色器

attribute vec4 a_Position;

void main() {
    gl_Position = a_Position;
    gl_PointSize = 10.0;
}

片段着色器

precision mediump float;

uniform vec4 u_color;

void main() {
    gl_FragColor = u_color;
}

结果是一个内部带有黑色矩形的绿色屏幕。但我希望矩形是蓝色的。我的代码有问题吗?

1 个答案:

答案 0 :(得分:0)

在您的渲染器代码中使用

private static final String U_COLOR = "u_Color";

在片段着色器中,制服命名为

uniform vec4 u_color;

使用相同的统一名称希望将黑色修复为您尝试为其设置的颜色。