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");
 }
Example #2
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;
  }
  @Override
  public void onSurfaceCreated(GL10 unused, EGLConfig config) {
    int program = GLES20.glCreateProgram();
    addShaderTo(GLES20.GL_VERTEX_SHADER, VERTEX_SHADER_STRING, program);
    addShaderTo(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER_STRING, program);

    GLES20.glLinkProgram(program);
    int[] result = new int[] {GLES20.GL_FALSE};
    result[0] = GLES20.GL_FALSE;
    GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, result, 0);
    abortUnless(result[0] == GLES20.GL_TRUE, GLES20.glGetProgramInfoLog(program));
    GLES20.glUseProgram(program);

    GLES20.glUniform1i(GLES20.glGetUniformLocation(program, "y_tex"), 0);
    GLES20.glUniform1i(GLES20.glGetUniformLocation(program, "u_tex"), 1);
    GLES20.glUniform1i(GLES20.glGetUniformLocation(program, "v_tex"), 2);

    // Actually set in drawRectangle(), but queried only once here.
    posLocation = GLES20.glGetAttribLocation(program, "in_pos");

    int tcLocation = GLES20.glGetAttribLocation(program, "in_tc");
    GLES20.glEnableVertexAttribArray(tcLocation);
    GLES20.glVertexAttribPointer(tcLocation, 2, GLES20.GL_FLOAT, false, 0, textureCoords);

    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    checkNoGLES2Error();
  }
Example #4
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;
  }
 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;
 }
Example #6
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;
  }
  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;
  }
Example #8
0
 @Override
 public void glGetProgramInfoLog(
     int program, int bufsize, int[] length, int lengthOffset, byte[] infolog, int infologOffset) {
   String log = GLES20.glGetProgramInfoLog(program);
   byte[] byteArray = log.getBytes();
   for (int i = 0; i < bufsize && i < byteArray.length; i++) {
     infolog[i + infologOffset] = byteArray[i];
   }
   length[lengthOffset] = log.length();
 }
Example #9
0
 @Override
 public void printProgramInfoLog(final int program) {
   checkOpenGLThread();
   final int[] linkStatus = new int[1];
   GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
   if (linkStatus[0] == GLES20.GL_TRUE) {
     Log.e("GL2Shaders", "Could not link program: ");
     Log.e("GL2Shaders", GLES20.glGetProgramInfoLog(program));
   }
 }
  public static boolean validateProgram(int prog) {
    if (prog == 0) {
      return false;
    }
    GLES20.glValidateProgram(prog);

    int[] status = new int[1];
    GLES20.glGetProgramiv(prog, GLES20.GL_VALIDATE_STATUS, status, 0);
    Log.e(TAG, "validate progra " + GLES20.glGetProgramInfoLog(prog));
    return status[0] != 0;
  }
  /** Validates an OpenGL program. Should only be called when developing the application. */
  public static boolean validateProgram(int programObjectId) {
    GLES20.glValidateProgram(programObjectId);
    final int[] validateStatus = new int[1];
    glGetProgramiv(programObjectId, GLES20.GL_VALIDATE_STATUS, validateStatus, 0);
    Log.v(
        TAG,
        "Results of validating program: "
            + validateStatus[0]
            + "\nLog:"
            + GLES20.glGetProgramInfoLog(programObjectId));

    return validateStatus[0] != 0;
  }
Example #12
0
  public int loadShader(int type, String shaderCode) {
    // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
    // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
    int shader = GLES20.glCreateShader(type);

    // add the source code to the shader and compile it
    GLES20.glShaderSource(shader, shaderCode);
    GLES20.glCompileShader(shader);

    String logmsg = GLES20.glGetProgramInfoLog(shader);
    Log.d("ShaderCompile", logmsg);

    return shader;
  }
  /**
   * 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;
 }
 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;
 }
  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);
  }
Example #17
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;
  }
Example #18
0
 @Override
 public String glGetProgramInfoLog(int program) {
   return GLES20.glGetProgramInfoLog(program);
 }
Example #19
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;
    }
  }