private int createProgram(String vertexSource, String fragmentSource) {
   int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
   if (vertexShader == 0) {
     return TRIANGLE_VERTICES_DATA_POS_OFFSET;
   }
   int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
   if (pixelShader == 0) {
     return TRIANGLE_VERTICES_DATA_POS_OFFSET;
   }
   int program = GLES20.glCreateProgram();
   checkGlError("glCreateProgram");
   if (program == 0) {
     Log.e(TAG, "Could not create program");
   }
   GLES20.glAttachShader(program, vertexShader);
   checkGlError("glAttachShader");
   GLES20.glAttachShader(program, pixelShader);
   checkGlError("glAttachShader");
   GLES20.glLinkProgram(program);
   int[] linkStatus = new int[FLIP_TYPE_HORIZONTAL];
   GLES20.glGetProgramiv(
       program, GLES20.GL_LINK_STATUS, linkStatus, TRIANGLE_VERTICES_DATA_POS_OFFSET);
   if (linkStatus[TRIANGLE_VERTICES_DATA_POS_OFFSET] == FLIP_TYPE_HORIZONTAL) {
     return program;
   }
   Log.e(TAG, "Could not link program: ");
   Log.e(TAG, GLES20.glGetProgramInfoLog(program));
   GLES20.glDeleteProgram(program);
   throw new IllegalStateException("Could not link program");
 }
 private int createProgram(final String vertexSource, final String fragmentSource) {
   final int vertexShader = this.loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
   if (vertexShader == 0) {
     return 0;
   }
   final int pixelShader = this.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
   if (pixelShader == 0) {
     return 0;
   }
   int program = GLES20.glCreateProgram();
   if (program != 0) {
     GLES20.glAttachShader(program, vertexShader);
     this.checkGlError("glAttachShader");
     GLES20.glAttachShader(program, pixelShader);
     this.checkGlError("glAttachShader");
     GLES20.glLinkProgram(program);
     final int[] linkStatus = {0};
     GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
     if (linkStatus[0] != 1) {
       Log.e("DistortionRenderer", "Could not link program: ");
       Log.e("DistortionRenderer", GLES20.glGetProgramInfoLog(program));
       GLES20.glDeleteProgram(program);
       program = 0;
     }
   }
   return program;
 }
Ejemplo n.º 3
0
  public static int createProgram(String vertexShaderSource, String fragmentShaderSource) {
    if (TextUtils.isEmpty(vertexShaderSource) || TextUtils.isEmpty(fragmentShaderSource)) {
      throw new IllegalArgumentException("传入的顶点着色器源码为空或者传入的片元着色器源码为空");
    }

    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderSource);
    if (vertexShader == 0) {
      return 0;
    }

    int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderSource);
    if (fragmentShader == 0) {
      return 0;
    }

    int program = GLES20.glCreateProgram();
    if (program != 0) {
      GLES20.glAttachShader(program, vertexShader);
      checkGLError("AttachVertexShader");
      GLES20.glAttachShader(program, vertexShader);
      checkGLError("AttachFragmentShader");

      GLES20.glLinkProgram(program);
      int[] result = new int[1];
      GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, result, 0);
      if (result[0] == 0) {
        Log.w(TAG, "加载程序失败");
        Log.w(TAG, GLES20.glGetProgramInfoLog(program));
        GLES20.glDeleteProgram(program);
        program = 0;
      }
    }

    return program;
  }
Ejemplo n.º 4
0
  public static int createProgram(int vertexShaderHandle, int fragmentShaderHandle) {
    int mProgram = GLES20.glCreateProgram();

    if (mProgram != 0) {
      GLES20.glAttachShader(mProgram, vertexShaderHandle);
      GLES20.glAttachShader(mProgram, fragmentShaderHandle);

      GLES20.glLinkProgram(mProgram);

      final int[] linkStatus = new int[1];
      GLES20.glGetProgramiv(mProgram, GLES20.GL_LINK_STATUS, linkStatus, 0);

      if (linkStatus[0] == 0) {
        Log.v(OpenGLUtil.class.getCanonicalName(), GLES20.glGetProgramInfoLog(mProgram));
        GLES20.glDeleteProgram(mProgram);
        mProgram = 0;
      }
    }

    if (mProgram == 0) {
      throw new RuntimeException("Error creating program.");
    }

    return mProgram;
  }
Ejemplo n.º 5
0
 /** Replaces the fragment shader. */
 public void changeFragmentShader(String fragmentShader) {
   GLES20.glDeleteProgram(mProgram);
   mProgram = createProgram(VERTEX_SHADER, fragmentShader);
   if (mProgram == 0) {
     throw new RuntimeException("failed creating program");
   }
 }
Ejemplo n.º 6
0
  /**
   * Creates a new program from the supplied vertex and fragment shaders.
   *
   * @return A handle to the program, or 0 on failure.
   */
  public static int createProgram(String vertexSource, String fragmentSource) {
    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
    if (vertexShader == 0) {
      return 0;
    }
    int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
    if (pixelShader == 0) {
      return 0;
    }

    int program = GLES20.glCreateProgram();
    checkGlError("glCreateProgram");
    if (program == 0) {
      Log.e(TAG, "Could not create program");
    }
    GLES20.glAttachShader(program, vertexShader);
    checkGlError("glAttachShader");
    GLES20.glAttachShader(program, pixelShader);
    checkGlError("glAttachShader");
    GLES20.glLinkProgram(program);
    int[] linkStatus = new int[1];
    GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
    if (linkStatus[0] != GLES20.GL_TRUE) {
      Log.e(TAG, "Could not link program: ");
      Log.e(TAG, GLES20.glGetProgramInfoLog(program));
      GLES20.glDeleteProgram(program);
      program = 0;
    }
    return program;
  }
Ejemplo n.º 7
0
  /**
   * Creates a shader program by compiling the vertex and fragment shaders from a string.
   *
   * @param vertexSource
   * @param fragmentSource
   * @return
   */
  private int createProgram(String vertexSource, String fragmentSource) {
    mVShaderHandle = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
    if (mVShaderHandle == 0) {
      return 0;
    }

    mFShaderHandle = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
    if (mFShaderHandle == 0) {
      return 0;
    }

    int program = GLES20.glCreateProgram();
    if (program != 0) {
      GLES20.glAttachShader(program, mVShaderHandle);
      GLES20.glAttachShader(program, mFShaderHandle);
      GLES20.glLinkProgram(program);

      int[] linkStatus = new int[1];
      GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
      if (linkStatus[0] != GLES20.GL_TRUE) {
        RajLog.e("Could not link program in " + getClass().getCanonicalName() + ": ");
        RajLog.e(GLES20.glGetProgramInfoLog(program));
        GLES20.glDeleteProgram(program);
        program = 0;
      }
    }
    return program;
  }
Ejemplo n.º 8
0
  public static int getProgram(String vsSrc, String fsSrc) {
    // vertexShaderのコンパイル
    int vs = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
    GLES20.glShaderSource(vs, vsSrc);
    GLES20.glCompileShader(vs);

    // コンパイルチェック
    final int[] vsCompileStatus = new int[1];
    GLES20.glGetShaderiv(vs, GLES20.GL_COMPILE_STATUS, vsCompileStatus, 0);

    if (vsCompileStatus[0] == 0) {
      // コンパイル失敗
      GLES20.glDeleteShader(vs);
      vs = 0;
      // ログにエラーを出力
      Log.d("debug", "VERTEXSHADER_COMPILE_ERROR");
    }

    // fragmentShaderのコンパイル
    int fs = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
    GLES20.glShaderSource(fs, fsSrc);
    GLES20.glCompileShader(fs);

    // コンパイルチェック
    final int[] fsCompileStatus = new int[1];
    GLES20.glGetShaderiv(fs, GLES20.GL_COMPILE_STATUS, fsCompileStatus, 0);

    if (fsCompileStatus[0] == 0) {
      // コンパイル失敗
      GLES20.glDeleteShader(fs);
      fs = 0;
      Log.d("debug", "FRAGMENTSHADER_COMPILE_ERROR");
    }

    // shaderをリンクしてProgramを作る
    int shaderProgram;
    shaderProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(shaderProgram, vs);
    GLES20.glAttachShader(shaderProgram, fs);
    GLES20.glLinkProgram(shaderProgram);

    // リンク結果をチェック
    final int[] linkStatus = new int[1];
    GLES20.glGetProgramiv(shaderProgram, GLES20.GL_LINK_STATUS, linkStatus, 0);
    if (linkStatus[0] == 0) {
      // リンク失敗
      GLES20.glDeleteProgram(shaderProgram);
      shaderProgram = 0;
      Log.d("debug", "LINK_ERROR");
    }
    return shaderProgram;
  }
Ejemplo n.º 9
0
  /** {@inheritDoc} */
  void remove() {
    mModelMatrix = null;
    mModelViewMatrix = null;

    if (mLights != null) mLights.clear();
    if (mTextureList != null) mTextureList.clear();

    if (RajawaliRenderer.hasGLContext()) {
      GLES20.glDeleteShader(mVShaderHandle);
      GLES20.glDeleteShader(mFShaderHandle);
      GLES20.glDeleteProgram(mProgramHandle);
    }
  }
Ejemplo n.º 10
0
  /**
   * 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 = GLES20.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, GLES20.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" + GLES20.glGetProgramInfoLog(programObjectId));
    }

    // Verify the link status.
    if (linkStatus[0] == 0) {
      // If it failed, delete the program object.
      GLES20.glDeleteProgram(programObjectId);

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

      return 0;
    }

    // Return the program object ID.
    return programObjectId;
  }
 public static int linkProgram(int vsh, int fsh) {
   if (vsh == 0 || fsh == 0) {
     Log.e(TAG, "Invalid vertex shader and fragment shader");
     return 0;
   }
   int prog = GLES20.glCreateProgram();
   GLES20.glAttachShader(prog, vsh);
   GLES20.glAttachShader(prog, fsh);
   GLES20.glLinkProgram(prog);
   int[] status = new int[1];
   GLES20.glGetProgramiv(prog, GLES20.GL_LINK_STATUS, status, 0);
   Log.e(TAG, "link program " + GLES20.glGetProgramInfoLog(prog));
   if (status[0] == 0) {
     GLES20.glDeleteProgram(prog);
     return 0;
   }
   return prog;
 }
Ejemplo n.º 12
0
 public static int createProgram(String vertexSource, String fragmentSource) {
   int vs = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
   int fs = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
   int program = GLES20.glCreateProgram();
   GLES20.glAttachShader(program, vs);
   GLES20.glAttachShader(program, fs);
   GLES20.glLinkProgram(program);
   //
   int[] linkStatus = new int[1];
   GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
   if (linkStatus[0] != GLES20.GL_TRUE) {
     Log.e(TAG, "Could not link program:");
     Log.e(TAG, GLES20.glGetProgramInfoLog(program));
     GLES20.glDeleteProgram(program);
     program = 0;
   }
   //
   return program;
 }
Ejemplo n.º 13
0
  public Cube() {
    // Prepare shaders and OpenGL program
    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADER_CODE);
    if (vertexShader == 0) {
      Log.e(TAG, "Vertex shader failed");
      return;
    }
    int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER_CODE);
    if (fragmentShader == 0) {
      Log.e(TAG, "Fragment shader failed");
      return;
    }

    mProgram = GLES20.glCreateProgram(); // create empty OpenGL Program
    GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program
    GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
    GLES20.glLinkProgram(mProgram); // create OpenGL program executables

    int[] linkStatus = new int[1];
    GLES20.glGetProgramiv(mProgram, GLES20.GL_LINK_STATUS, linkStatus, 0);
    if (linkStatus[0] != GLES20.GL_TRUE) {
      Log.e(TAG, "Could not link program: ");
      Log.e(TAG, GLES20.glGetProgramInfoLog(mProgram));
      GLES20.glDeleteProgram(mProgram);
      mProgram = 0;
      return;
    }
    GLES20.glDeleteShader(vertexShader);
    GLES20.glDeleteShader(fragmentShader);

    // Initialize vertex byte buffer for shape coordinates
    mVertexBuffer = allocateFloatBuffer(VERTICES);

    // Initialize byte buffer for the colors
    mColor1Buffer = allocateFloatBuffer(COLORS1);
    mColor2Buffer = allocateFloatBuffer(COLORS2);

    // Initialize byte buffer for the draw list
    mIndexBuffer = allocateShortBuffer(INDICES);
  }
Ejemplo n.º 14
0
  /**
   * Helper function to compile and link a program.
   *
   * @param vertexShaderHandle An OpenGL handle to an already-compiled vertex shader.
   * @param fragmentShaderHandle An OpenGL handle to an already-compiled fragment shader.
   * @param attributes Attributes that need to be bound to the program.
   * @return An OpenGL handle to the program.
   */
  private int createAndLinkProgram(
      final int vertexShaderHandle, final int fragmentShaderHandle, final String[] attributes) {
    int programHandle = GLES20.glCreateProgram();

    if (programHandle != 0) {
      // Bind the vertex shader to the program.
      GLES20.glAttachShader(programHandle, vertexShaderHandle);

      // Bind the fragment shader to the program.
      GLES20.glAttachShader(programHandle, fragmentShaderHandle);

      // Bind attributes
      if (attributes != null) {
        final int size = attributes.length;
        for (int i = 0; i < size; i++) {
          GLES20.glBindAttribLocation(programHandle, i, attributes[i]);
        }
      }

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

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

      // If the link failed, delete the program.
      if (linkStatus[0] == 0) {
        GLES20.glDeleteProgram(programHandle);
        programHandle = 0;
      }
    }

    if (programHandle == 0) {
      throw new RuntimeException("Error creating program.");
    }

    return programHandle;
  }
Ejemplo n.º 15
0
  // 创建shader程序的方法
  public static int createProgram(String vertexSource, String fragmentSource) {
    // 加载顶点着色器
    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
    if (vertexShader == 0) {
      return 0;
    }

    // 加载片元着色器
    int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
    if (pixelShader == 0) {
      return 0;
    }

    // 创建程序
    int program = GLES20.glCreateProgram();
    // 若程序创建成功则向程序中加入顶点着色器与片元着色器
    if (program != 0) {
      // 向程序中加入顶点着色器
      GLES20.glAttachShader(program, vertexShader);
      checkGlError("glAttachShader");
      // 向程序中加入片元着色器
      GLES20.glAttachShader(program, pixelShader);
      checkGlError("glAttachShader");
      // 链接程序
      GLES20.glLinkProgram(program);
      // 存放链接成功program数量的数组
      int[] linkStatus = new int[1];
      // 获取program的链接情况
      GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
      // 若链接失败则报错并删除程序
      if (linkStatus[0] != GLES20.GL_TRUE) {
        Log.e("ES20_ERROR", "Could not link program: ");
        Log.e("ES20_ERROR", GLES20.glGetProgramInfoLog(program));
        GLES20.glDeleteProgram(program);
        program = 0;
      }
    }
    return program;
  }
Ejemplo n.º 16
0
  @Override
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {

    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    // Use culling to remove back faces.
    //		GLES20.glEnable(GLES20.GL_CULL_FACE);
    GLES20.glEnable(GLES20.GL_DEPTH_TEST);

    //		// Position the eye behind the origin.
    // Position the eye behind the origin.
    final float eyeX = 0.0f;
    final float eyeY = 0.0f;
    final float eyeZ = 2f; // 1.5f;

    // We are looking toward the distance
    final float lookX = 0.0f;
    final float lookY = 0.0f;
    final float lookZ = -5.0f;

    // Set our up vector. This is where our head would be pointing were we holding the camera.
    final float upX = 0.0f;
    final float upY = 1.0f;
    final float upZ = 0.0f;

    // Set the view matrix. This matrix can be said to represent the camera position.
    // NOTE: In OpenGL 1, a ModelView matrix is used, which is a combination of a model and
    // view matrix. In OpenGL 2, we can keep track of these matrices separately if we choose.
    Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);

    // please note i am making view matrix as identity matrix intentionally here to avoid the
    // effects of view matrix. if you want explore the effect of view matrix you can uncomment this
    // line
    // Matrix.setIdentityM(mViewMatrix, 0);

    final String vertexShader =
        "uniform mat4 u_MVPMatrix;      \n" // A constant representing the combined
            // model/view/projection matrix.
            + "attribute vec4 a_Position;     \n" // Per-vertex position information we will pass
            // in.
            + "attribute vec4 a_Color;        \n" // Per-vertex color information we will pass in.
            + "attribute vec2 a_TexCoordinate;\n"
            + "varying vec2 v_TexCoordinate;  \n"
            + "varying vec4 v_Color;          \n" // This will be passed into the fragment shader.
            + "void main()                    \n" // The entry point for our vertex shader.
            + "{                              \n"
            + "   v_Color = a_Color;          \n" // Pass the color through to the fragment shader.
            + "v_TexCoordinate = a_TexCoordinate;\n" // It will be interpolated across the triangle.
            + "   gl_Position = u_MVPMatrix   \n" // gl_Position is a special variable used to store
            // the final position.
            + "               * a_Position;   \n" // Multiply the vertex by the matrix to get the
            // final point in
            + "}                              \n"; // normalized screen coordinates.

    final String fragmentShader =
        "precision mediump float;       \n" // Set the default precision to medium. We don't need as
            // high of a
            // precision in the fragment shader.
            + "varying vec4 v_Color;          \n" // This is the color from the vertex shader
            // interpolated across the
            + "uniform sampler2D u_Texture;   \n"
            + "varying vec2 v_TexCoordinate;   \n" // triangle per fragment.
            + "void main()                    \n" // The entry point for our fragment shader.
            + "{                              \n"
            + " gl_FragColor = (v_Color * texture2D(u_Texture, v_TexCoordinate));     \n" // Pass
            // the
            // color
            // directly through the pipeline.
            + "}                              \n";

    // Load in the vertex shader.
    int vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);

    if (vertexShaderHandle != 0) {
      // Pass in the shader source.
      GLES20.glShaderSource(vertexShaderHandle, vertexShader);

      // Compile the shader.
      GLES20.glCompileShader(vertexShaderHandle);

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

      // If the compilation failed, delete the shader.
      if (compileStatus[0] == 0) {
        GLES20.glDeleteShader(vertexShaderHandle);
        vertexShaderHandle = 0;
      }
    }

    if (vertexShaderHandle == 0) {
      throw new RuntimeException("Error creating vertex shader.");
    }

    // Load in the fragment shader shader.
    int fragmentShaderHandle = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);

    if (fragmentShaderHandle != 0) {
      // Pass in the shader source.
      GLES20.glShaderSource(fragmentShaderHandle, fragmentShader);

      // Compile the shader.
      GLES20.glCompileShader(fragmentShaderHandle);

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

      // If the compilation failed, delete the shader.
      if (compileStatus[0] == 0) {
        GLES20.glDeleteShader(fragmentShaderHandle);
        fragmentShaderHandle = 0;
      }
    }

    if (fragmentShaderHandle == 0) {
      throw new RuntimeException("Error creating fragment shader.");
    }

    // Create a program object and store the handle to it.
    mProgramHandle = GLES20.glCreateProgram();

    if (mProgramHandle != 0) {
      // Bind the vertex shader to the program.
      GLES20.glAttachShader(mProgramHandle, vertexShaderHandle);

      // Bind the fragment shader to the program.
      GLES20.glAttachShader(mProgramHandle, fragmentShaderHandle);

      // Bind attributes
      GLES20.glBindAttribLocation(mProgramHandle, 0, "a_Position");
      GLES20.glBindAttribLocation(mProgramHandle, 1, "a_Color");
      GLES20.glBindAttribLocation(mProgramHandle, 2, "a_TexCoordinate");

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

      mTextureDataHandle0 = loadTexture(mActivityContext, R.drawable.nature1);
      mTextureDataHandle1 = loadTexture(mActivityContext, R.drawable.nature2);
      mTextureDataHandle2 = loadTexture(mActivityContext, R.drawable.nature3);
      mTextureDataHandle3 = loadTexture(mActivityContext, R.drawable.nature4);
      mTextureDataHandle4 = loadTexture(mActivityContext, R.drawable.nature5);
      mTextureDataHandle5 = loadTexture(mActivityContext, R.drawable.nature6);

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

      // If the link failed, delete the program.
      if (linkStatus[0] == 0) {
        GLES20.glDeleteProgram(mProgramHandle);
        mProgramHandle = 0;
      }
    }

    if (mProgramHandle == 0) {
      throw new RuntimeException("Error creating program.");
    }
  }
Ejemplo n.º 17
0
 public final void destroy() {
   mIsInitialized = false;
   GLES20.glDeleteProgram(mGLProgId);
   onDestroy();
 }
Ejemplo n.º 18
0
 @Override
 public void glDeleteProgram(int program) {
   GLES20.glDeleteProgram(program);
 }
Ejemplo n.º 19
0
 @Override
 public void deleteProgram(final int program) {
   checkOpenGLThread();
   GLES20.glDeleteProgram(program);
 }
Ejemplo n.º 20
0
  @Override
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {

    SurfaceCreated(glUnused, config);
    // Set the background clear color to gray.
    GLES20.glClearColor(0.5f, 0.5f, 0.5f, 0.5f);

    // Position the eye behind the origin.
    final float eyeX = 0.0f;
    final float eyeY = 0.0f;
    final float eyeZ = 1.5f;

    // We are looking toward the distance
    final float lookX = 0.0f;
    final float lookY = 0.0f;
    final float lookZ = -5.0f;

    // Set our up vector. This is where our head would be pointing were we holding the camera.
    final float upX = 0.0f;
    final float upY = 1.0f;
    final float upZ = 0.0f;

    // Set the view matrix. This matrix can be said to represent the camera position.
    // NOTE: In OpenGL 1, a ModelView matrix is used, which is a combination of a model and
    // view matrix. In OpenGL 2, we can keep track of these matrices separately if we choose.
    Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);

    final String vertexShader =
        "uniform mat4 u_MVPMatrix;      \n" // A constant representing the combined
            // model/view/projection matrix.
            + "attribute vec4 a_Position;     \n" // Per-vertex position information we will pass
            // in.
            + "attribute vec4 a_Color;        \n" // Per-vertex color information we will pass in.
            + "varying vec4 v_Color;          \n" // This will be passed into the fragment shader.
            + "void main()                    \n" // The entry point for our vertex shader.
            + "{                              \n"
            + "   v_Color = a_Color;          \n" // Pass the color through to the fragment shader.
            // It will be interpolated across the triangle.
            + "   gl_Position = u_MVPMatrix   \n" // gl_Position is a special variable used to store
            // the final position.
            + "               * a_Position;   \n" // Multiply the vertex by the matrix to get the
            // final point in
            + "}                              \n"; // normalized screen coordinates.

    final String fragmentShader =
        "precision mediump float;       \n" // Set the default precision to medium. We don't need as
            // high of a
            // precision in the fragment shader.
            + "varying vec4 v_Color;          \n" // This is the color from the vertex shader
            // interpolated across the
            // triangle per fragment.
            + "void main()                    \n" // The entry point for our fragment shader.
            + "{                              \n"
            + "   gl_FragColor = v_Color;     \n" // Pass the color directly through the pipeline.
            + "}                              \n";

    // Load in the vertex shader.
    int vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);

    if (vertexShaderHandle != 0) {
      // Pass in the shader source.
      GLES20.glShaderSource(vertexShaderHandle, vertexShader);

      // Compile the shader.
      GLES20.glCompileShader(vertexShaderHandle);

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

      // If the compilation failed, delete the shader.
      if (compileStatus[0] == 0) {
        GLES20.glDeleteShader(vertexShaderHandle);
        vertexShaderHandle = 0;
      }
    }

    if (vertexShaderHandle == 0) {
      throw new RuntimeException("Error creating vertex shader.");
    }

    // Load in the fragment shader shader.
    int fragmentShaderHandle = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);

    if (fragmentShaderHandle != 0) {
      // Pass in the shader source.
      GLES20.glShaderSource(fragmentShaderHandle, fragmentShader);

      // Compile the shader.
      GLES20.glCompileShader(fragmentShaderHandle);

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

      // If the compilation failed, delete the shader.
      if (compileStatus[0] == 0) {
        GLES20.glDeleteShader(fragmentShaderHandle);
        fragmentShaderHandle = 0;
      }
    }

    if (fragmentShaderHandle == 0) {
      throw new RuntimeException("Error creating fragment shader.");
    }

    // Create a program object and store the handle to it.
    int programHandle = GLES20.glCreateProgram();

    if (programHandle != 0) {
      // Bind the vertex shader to the program.
      GLES20.glAttachShader(programHandle, vertexShaderHandle);

      // Bind the fragment shader to the program.
      GLES20.glAttachShader(programHandle, fragmentShaderHandle);

      // Bind attributes
      GLES20.glBindAttribLocation(programHandle, 0, "a_Position");
      GLES20.glBindAttribLocation(programHandle, 1, "a_Color");

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

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

      // If the link failed, delete the program.
      if (linkStatus[0] == 0) {
        GLES20.glDeleteProgram(programHandle);
        programHandle = 0;
      }
    }

    if (programHandle == 0) {
      throw new RuntimeException("Error creating program.");
    }

    // Set program handles. These will later be used to pass in values to the program.
    mMVPMatrixHandle = GLES20.glGetUniformLocation(programHandle, "u_MVPMatrix");
    mPositionHandle = GLES20.glGetAttribLocation(programHandle, "a_Position");
    mColorHandle = GLES20.glGetAttribLocation(programHandle, "a_Color");

    // Tell OpenGL to use this program when rendering.
    GLES20.glUseProgram(programHandle);
  }
Ejemplo n.º 21
0
  @Override
  public boolean initObject() {
    if (imageResId != 0) {
      vertexCount = coordinates.length / COORDS_PER_VERTEX;
      ByteBuffer buff = ByteBuffer.allocateDirect(coordinates.length * 4);
      buff.order(ByteOrder.nativeOrder());
      vBuffer = buff.asFloatBuffer();
      vBuffer.put(coordinates);
      vBuffer.position(0);

      ByteBuffer drawBuff = ByteBuffer.allocateDirect(drawOrder.length * 2);
      drawBuff.order(ByteOrder.nativeOrder());
      drawBuffer = drawBuff.asShortBuffer();
      drawBuffer.put(drawOrder);
      drawBuffer.position(0);

      ByteBuffer textBuff = ByteBuffer.allocateDirect(textureCoordinates.length * 4);
      textBuff.order(ByteOrder.nativeOrder());
      textureBuffer = textBuff.asFloatBuffer();
      textureBuffer.put(textureCoordinates);
      textureBuffer.position(0);

      // Prepare shaders and OpenGL program
      vertexShaderHandle = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
      fragShaderHandle = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

      programHandle = GLES20.glCreateProgram(); // create empty OpenGL Program
      GLES20.glAttachShader(programHandle, vertexShaderHandle); // add the vertex shader to program
      GLES20.glAttachShader(programHandle, fragShaderHandle); // add the fragment shader to program

      GLES20.glBindAttribLocation(programHandle, 0, "aPosition");
      GLES20.glBindAttribLocation(programHandle, 1, "aTexCoordinate");

      GLES20.glLinkProgram(programHandle); // create OpenGL program executables

      final int[] status = new int[1];
      GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, status, 0);

      if (status[0] == 0) {
        Log.e(
            "GLSprite",
            "Error compiling OpenGL program: " + GLES20.glGetProgramInfoLog(programHandle));
        GLES20.glDeleteProgram(programHandle);
        programHandle = 0;
        throw new RuntimeException("Error creating OpenGL program.");
      }

      textureDataHandle = GLUtil.loadTexture(mCanvas.getView().getContext(), imageResId);

      // get handle to vertex shader's aPosition member
      positionHandle = GLES20.glGetAttribLocation(programHandle, "aPosition");
      textureCoordinateHandle = GLES20.glGetAttribLocation(programHandle, "aTexCoordinate");
      mModelMatrixHandle = GLES20.glGetUniformLocation(programHandle, "uMVMatrix");
      GLES20Renderer.checkGLError("glUniformLocation uMVMatrix");
      mMVPMatrixHandle = GLES20.glGetUniformLocation(programHandle, "uMVPMatrix");
      GLES20Renderer.checkGLError("glUniformLocation uMVPMatrix");
      textureUniformHandle = GLES20.glGetUniformLocation(programHandle, "u_Texture");
      GLES20Renderer.checkGLError("glUniformLocation u_Texture");

      GLES20.glEnable(GLES20.GL_BLEND);

      GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);

      initialized = true;
      return true;
    } else {
      return false;
    }
  }
  /** Method for creating the most basic of shaders. */
  private void createShaders() {
    final String vertexShader =
        "uniform mat4 u_MVPMatrix;      \n"
            + "attribute vec4 a_Position;     \n"
            + "attribute vec4 a_Color;        \n"
            + "varying vec4 v_Color;          \n"
            + "void main()                    \n"
            + "{                              \n"
            + "   v_Color = a_Color;          \n"
            + "   gl_Position = u_MVPMatrix   \n"
            + "               * a_Position;   \n"
            + "}                              \n";

    final String fragmentShader =
        "precision mediump float;       \n"
            + "varying vec4 v_Color;          \n"
            + "void main()                    \n"
            + "{                              \n"
            + "   gl_FragColor = v_Color;     \n"
            + "}                              \n";

    // Load in the vertex shader.
    int vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);

    if (vertexShaderHandle != 0) {
      // Pass in the shader source.
      GLES20.glShaderSource(vertexShaderHandle, vertexShader);

      // Compile the shader.
      GLES20.glCompileShader(vertexShaderHandle);

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

      // If the compilation failed, delete the shader.
      if (compileStatus[0] == 0) {
        GLES20.glDeleteShader(vertexShaderHandle);
        vertexShaderHandle = 0;
      }
    }

    if (vertexShaderHandle == 0) {
      throw new RuntimeException("Error creating vertex shader.");
    }

    // Load in the fragment shader shader.
    int fragmentShaderHandle = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);

    if (fragmentShaderHandle != 0) {
      // Pass in the shader source.
      GLES20.glShaderSource(fragmentShaderHandle, fragmentShader);

      // Compile the shader.
      GLES20.glCompileShader(fragmentShaderHandle);

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

      // If the compilation failed, delete the shader.
      if (compileStatus[0] == 0) {
        GLES20.glDeleteShader(fragmentShaderHandle);
        fragmentShaderHandle = 0;
      }
    }

    if (fragmentShaderHandle == 0) {
      throw new RuntimeException("Error creating fragment shader.");
    }

    // Create a program object and store the handle to it.
    int programHandle = GLES20.glCreateProgram();

    if (programHandle != 0) {
      // Bind the vertex shader to the program.
      GLES20.glAttachShader(programHandle, vertexShaderHandle);

      // Bind the fragment shader to the program.
      GLES20.glAttachShader(programHandle, fragmentShaderHandle);

      // Bind attributes
      GLES20.glBindAttribLocation(programHandle, 0, "a_Position");
      GLES20.glBindAttribLocation(programHandle, 1, "a_Color");

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

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

      // If the link failed, delete the program.
      if (linkStatus[0] == 0) {
        GLES20.glDeleteProgram(programHandle);
        programHandle = 0;
      }
    }

    if (programHandle == 0) {
      throw new RuntimeException("Error creating program.");
    }

    // Set program handles. These will later be used to pass in values to the program.
    mMVPMatrixHandle = GLES20.glGetUniformLocation(programHandle, "u_MVPMatrix");
    mPositionHandle = GLES20.glGetAttribLocation(programHandle, "a_Position");
    mColorHandle = GLES20.glGetAttribLocation(programHandle, "a_Color");

    // Tell OpenGL to use this program when rendering.
    GLES20.glUseProgram(programHandle);
  }
Ejemplo n.º 23
0
 /** terminatinng, this should be called in GL context */
 public void release() {
   if (hProgram >= 0) GLES20.glDeleteProgram(hProgram);
   hProgram = -1;
 }