2014-07-02 3 views
0

J'ai essayé d'écrire un simple programme OpenGL android.OpenGL ne produit pas le résultat attendu

Ceci est mon renderer

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); 
} 

et c'est ma classe 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; 
} 

Vertex Shader

attribute vec4 a_Position; 

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

fragment shaders

precision mediump float; 

uniform vec4 u_color; 

void main() { 
    gl_FragColor = u_color; 
} 

le résultat est un écran vert avec un intérieur rectangulaire noir. mais je veux que le rectangle soit bleu à la place. Y at-il un problème avec mon code?

+0

Esprit partageant le code du shader aussi? – harism

+0

vertex_shader attribut vec4 a_Position; void principal() { \t gl_Position = a_Position; \t gl_PointSize = 10,0; } fragment_shader précision médium float; uniforme vec4 u_color; void principal() { \t gl_FragColor = u_color; } –

+0

S'il vous plaît ne pas poster de code dans les commentaires. C'est illisible et la limite de texte n'est pas assez longue. –

Répondre

0

Dans votre code renderer vous utilisez

private static final String U_COLOR = "u_Color"; 

Et dans le fragment shaders l'uniforme est nommé

uniform vec4 u_color; 

En utilisant le même nom uniforme fixe, espérons la couleur noire à celui que vous essayez de définir pour elle.

+0

Oh, c'est vrai. Merci de votre aide –

Questions connexes