public void onSurfaceCreated(GL10 unused, EGLConfig config) {

    // Set the background frame color
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    GLES20.glDisable(GLES20.GL_DITHER);
    GLES20.glDisable(GL10.GL_LIGHTING);
    GLES20.glDisable(GLES20.GL_DEPTH_TEST);
    GLES20.glEnable(GLES20.GL_TEXTURE_2D);
    GLES20.glEnable(GL10.GL_BLEND);
    GLES20.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    inirBuffers();

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

    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); // creates OpenGL program executables

    // get handle to the vertex shader's vPosition member

    maPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
    maTexCoordsHandle = GLES20.glGetAttribLocation(mProgram, "aTexCoords");
    mSampleHandle = GLES20.glGetUniformLocation(mProgram, "u_Texture");
    //        mSampleHandle2 = GLES20.glGetUniformLocation(mProgram, "u_Texture2");
    mOpacityHandle = GLES20.glGetUniformLocation(mProgram, "u_Opacity");
  }
示例#2
0
  void renderFrame(float fDeltaTime) {
    GLES20.glUseProgram(m_DefaultProgram);

    int posHandle = GLES20.glGetAttribLocation(m_DefaultProgram, "vPosition");
    int uvHandle = GLES20.glGetAttribLocation(m_DefaultProgram, "vUV");
    int textureHandle = GLES20.glGetUniformLocation(m_DefaultProgram, "texture");
    int laserColorHandle = GLES20.glGetUniformLocation(m_DefaultProgram, "colLaser");

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glUniform1i(textureHandle, 0);

    for (GameObject go : gameActivity.gameObjects) {
      go.render(fDeltaTime, posHandle, uvHandle, laserColorHandle);
    }

    GLES20.glDisableVertexAttribArray(posHandle);
    GLES20.glDisableVertexAttribArray(uvHandle);

    ArrayList<PointF> laserSegments = new ArrayList<PointF>();
    ArrayList<Float> laserLengths = new ArrayList<>();
    ArrayList<ColorF> laserColors = new ArrayList<ColorF>();

    gameActivity.computeLasers(laserSegments, laserLengths, laserColors);

    laserRenderer.setLasers(laserSegments, laserLengths, laserColors);
    laserRenderer.render(fDeltaTime);
  }
  @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();
  }
 public void onInit() {
   mGLProgId = OpenGlUtils.loadProgram(mVertexShader, mFragmentShader);
   mGLAttribPosition = GLES20.glGetAttribLocation(mGLProgId, "position");
   mGLUniformTexture = GLES20.glGetUniformLocation(mGLProgId, "inputImageTexture");
   mGLAttribTextureCoordinate = GLES20.glGetAttribLocation(mGLProgId, "inputTextureCoordinate");
   mIsInitialized = true;
 }
示例#5
0
 public void LoadShape() {
   int aPositionHandle = GLES20.glGetAttribLocation(programID, "aPosition");
   GenerateVertexArray(
       shape.verticeData, shape.verticeData.length * FLOAT_SIZE_BYTES, 3, aPositionHandle);
   int aUVHandle = GLES20.glGetAttribLocation(programID, "aTextureCoord");
   GenerateVertexArray(shape.UVData, shape.UVData.length * FLOAT_SIZE_BYTES, 2, aUVHandle);
 }
 // 初始化着色器的intShader方法
 public void initShader(int mProgram) {
   this.mProgram = mProgram;
   // 获取程序中顶点位置属性引用id
   maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
   // 获取程序中顶点纹理坐标属性引用id
   maTexCoorHandle = GLES20.glGetAttribLocation(mProgram, "aTexCoor");
   // 获取程序中总变换矩阵引用id
   muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
   // 获取位置、旋转变换矩阵引用id
   muMMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMMatrix");
   // 获取程序中顶点法向量属性引用id
   maNormalHandle = GLES20.glGetAttribLocation(mProgram, "aNormal");
   // 获取程序中光源位置引用id
   maLightLocationHandle = GLES20.glGetUniformLocation(mProgram, "uLightLocation");
   // 获取程序中摄像机位置引用id
   maCameraHandle = GLES20.glGetUniformLocation(mProgram, "uCamera");
   // 获取程序中是否绘制阴影属性引用id
   muIsShadow = GLES20.glGetUniformLocation(mProgram, "uisShadow");
   muIsShadowFrag = GLES20.glGetUniformLocation(mProgram, "uisShadowFrag");
   // 获取是否绘制篮板上阴影属性应用ID
   muIsLanBanShdow = GLES20.glGetUniformLocation(mProgram, "uisLanbanFrag");
   // 获取程序中摄像机矩阵引用id
   muCameraMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMCameraMatrix");
   // 获取程序中投影矩阵引用id
   muProjMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMProjMatrix");
   // 获取桌球纹理属性引用id
   muBallTexHandle = GLES20.glGetUniformLocation(mProgram, "usTextureBall");
   // 获取程序中平面法向量引用id;
   muPlaneN = GLES20.glGetUniformLocation(mProgram, "uplaneN");
   // 获取程序中平面上的点的引用的Id
   muPlaneV = GLES20.glGetUniformLocation(mProgram, "uplaneA");
 }
示例#7
0
  /**
   * Draws a frame for an eye. The transformation for that eye (from the camera) is passed in as a
   * parameter.
   *
   * @param transform The transformations to apply to render this eye.
   */
  @Override
  public void onDrawEye(EyeTransform transform) {
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

    GLES20.glUseProgram(mProgram);

    GLES20.glActiveTexture(GL_TEXTURE_EXTERNAL_OES);
    GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture);

    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "position");
    GLES20.glEnableVertexAttribArray(mPositionHandle);
    GLES20.glVertexAttribPointer(
        mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);

    mTextureCoordHandle = GLES20.glGetAttribLocation(mProgram, "inputTextureCoordinate");
    GLES20.glEnableVertexAttribArray(mTextureCoordHandle);
    GLES20.glVertexAttribPointer(
        mTextureCoordHandle,
        COORDS_PER_VERTEX,
        GLES20.GL_FLOAT,
        false,
        vertexStride,
        textureVerticesBuffer);

    GLES20.glDrawElements(
        GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

    // Disable vertex array
    GLES20.glDisableVertexAttribArray(mPositionHandle);
    GLES20.glDisableVertexAttribArray(mTextureCoordHandle);

    Matrix.multiplyMM(mView, 0, transform.getEyeView(), 0, mCamera, 0);
  }
示例#8
0
  /** Constructor this should be called in GL context */
  public GLDrawer2D() {
    pVertex =
        ByteBuffer.allocateDirect(VERTEX_SZ * FLOAT_SZ)
            .order(ByteOrder.nativeOrder())
            .asFloatBuffer();
    pVertex.put(VERTICES);
    pVertex.flip();
    pTexCoord =
        ByteBuffer.allocateDirect(VERTEX_SZ * FLOAT_SZ)
            .order(ByteOrder.nativeOrder())
            .asFloatBuffer();
    pTexCoord.put(TEXCOORD);
    pTexCoord.flip();

    hProgram = loadShader(vss, fss);
    GLES20.glUseProgram(hProgram);
    maPositionLoc = GLES20.glGetAttribLocation(hProgram, "aPosition");
    maTextureCoordLoc = GLES20.glGetAttribLocation(hProgram, "aTextureCoord");
    muMVPMatrixLoc = GLES20.glGetUniformLocation(hProgram, "uMVPMatrix");
    muTexMatrixLoc = GLES20.glGetUniformLocation(hProgram, "uTexMatrix");

    Matrix.setIdentityM(mMvpMatrix, 0);
    GLES20.glUniformMatrix4fv(muMVPMatrixLoc, 1, false, mMvpMatrix, 0);
    GLES20.glUniformMatrix4fv(muTexMatrixLoc, 1, false, mMvpMatrix, 0);
    GLES20.glVertexAttribPointer(maPositionLoc, 2, GLES20.GL_FLOAT, false, VERTEX_SZ, pVertex);
    GLES20.glVertexAttribPointer(
        maTextureCoordLoc, 2, GLES20.GL_FLOAT, false, VERTEX_SZ, pTexCoord);
    GLES20.glEnableVertexAttribArray(maPositionLoc);
    GLES20.glEnableVertexAttribArray(maTextureCoordLoc);
  }
示例#9
0
  public void draw(final int mShader) {

    // Bind Texture VBO
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, getTexVBO());
    // Pass in texture map position
    mTexVertBuffer.position(0);
    final int mTextureCoordinateHandle = GLES20.glGetAttribLocation(mShader, "a_TexCoord");
    GLES20.glVertexAttribPointer(
        mTextureCoordinateHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 0, 0);
    GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);

    // Bind VBO
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, getPlaneVBO());
    // Pass in the position information
    mPlaneVertBuffer.position(0);
    final int mPositionHandle = GLES20.glGetAttribLocation(mShader, "a_Position");
    GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 0, 0);
    GLES20.glEnableVertexAttribArray(mPositionHandle);

    //	Pass in the color information
    final int mColorHandle = GLES20.glGetUniformLocation(mShader, "u_Color");
    GLES20.glUniform4f(mColorHandle, 0, 1, 1, 1);

    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, indexCount);

    // Unbind buffers
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
  }
  @Override
  public void onDrawFrame(GL10 gl) {
    super.onDrawFrame(gl);
    // GL Draw code onwards
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

    // Do a complete rotation every 10 seconds.
    long time = SystemClock.uptimeMillis() % 10000L;
    float angleInDegrees = (360.0f / 10000.0f) * ((int) time);

    // Set our per-vertex lighting program.
    GLES20.glUseProgram(mProgramHandle);

    // Set program handles for cube drawing.
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVPMatrix");
    mMVMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVMatrix");
    mLightPosHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_LightPos");
    mTextureUniformHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_Texture");
    mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position");
    mColorHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Color");
    mNormalHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Normal");
    mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_TexCoordinate");

    GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, getGLSurfaceTexture());
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glUniform1i(mTextureUniformHandle, 0);

    //        GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0);
    //        GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, glSurfaceTex);
    //        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

    //        // Set the active texture unit to texture unit 0.
    //        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    //
    //        // Bind the texture to this unit.
    //        GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, glSurfaceTex);

    // Tell the texture uniform sampler to use this texture in the shader by binding to texture unit
    // 0.
    //        GLES20.glUniform1i(mTextureUniformHandle, 0);

    // Calculate position of the light. Rotate and then push into the distance.
    Matrix.setIdentityM(mLightModelMatrix, 0);
    Matrix.translateM(mLightModelMatrix, 0, 0.0f, 0.0f, -4.0f);
    Matrix.rotateM(mLightModelMatrix, 0, angleInDegrees, 0.0f, 1.0f, 0.0f);
    Matrix.translateM(mLightModelMatrix, 0, 0.0f, 0.0f, 2.0f);

    Matrix.multiplyMV(mLightPosInWorldSpace, 0, mLightModelMatrix, 0, mLightPosInModelSpace, 0);
    Matrix.multiplyMV(mLightPosInEyeSpace, 0, mViewMatrix, 0, mLightPosInWorldSpace, 0);

    Matrix.setIdentityM(mModelMatrix, 0);
    Matrix.translateM(mModelMatrix, 0, 0.0f, 0.0f, -3.3f);
    Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 1.0f, 1.0f, 0.0f);
    drawCube();

    // Draw a point to indicate the light.
    GLES20.glUseProgram(mPointProgramHandle);
    drawLight();
  }
示例#11
0
 // 初始化着色器程序的initShader方法
 public void initShader(int programId) {
   mProgram = programId;
   // 获得顶点坐标数据的引用
   maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
   // 顶点纹理坐标的引用id
   maTexCoorHandle = GLES20.glGetAttribLocation(mProgram, "aTexCoor");
   muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
 }
示例#12
0
文件: Scene.java 项目: Karm/galgs
  public void draw() {
    // Add program to OpenGL environment
    GLES20.glUseProgram(mProgram);

    // get handle to vertex shader's vPosition member
    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

    // Enable a handle to the scene vertices
    GLES20.glEnableVertexAttribArray(mPositionHandle);

    // Prepare the scene coordinate data
    GLES20.glVertexAttribPointer(
        mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, VERTEX_STRIDE, vertexBuffer);

    // get handle to fragment shader's vColor member
    mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
    // Set colorVertices for drawing the scene
    GLES20.glUniform4fv(mColorHandle, 1, colorVertices, 0);

    // Get handle to shape's transformation matrix
    int mtrxhandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");

    // Apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mtrxhandle, 1, false, pointsRenderer.mtrxProjectionAndView, 0);

    // Draw the scene
    GLES20.glDrawArrays(GLES20.GL_POINTS, 0, vertexCount);

    // Disable vertex array
    GLES20.glDisableVertexAttribArray(mPositionHandle);

    if (drawLines) {
      GLES20.glUseProgram(mLinesProgram);
      mLinesPositionHandle = GLES20.glGetAttribLocation(mLinesProgram, "vPosition");
      GLES20.glEnableVertexAttribArray(mLinesPositionHandle);
      // Prevent null pointer race condition
      if (linesVertexBuffer != null) {
        GLES20.glVertexAttribPointer(
            mLinesPositionHandle,
            COORDS_PER_VERTEX,
            GLES20.GL_FLOAT,
            false,
            VERTEX_STRIDE,
            linesVertexBuffer);
        mLinesColorHandle = GLES20.glGetUniformLocation(mLinesProgram, "vColor");
        GLES20.glUniform4fv(mLinesColorHandle, 1, colorLines, 0);
        int mtrxLineshandle = GLES20.glGetUniformLocation(mLinesProgram, "uMVPMatrix");
        GLES20.glUniformMatrix4fv(
            mtrxLineshandle, 1, false, pointsRenderer.mtrxProjectionAndView, 0);
        // GLES20.glDrawArrays(GLES20.GL_LINE_STRIP, 0, linesVertexCount);
        // GLES20.glDrawArrays(GLES20.GL_LINES, 0, linesVertexCount);
        // GLES20.glDrawArrays(GLES20.GL_LINE_LOOP, 0, linesVertexCount);
        // GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, linesVertexCount);
        GLES20.glDrawArrays(renderMethod, 0, linesVertexCount);
        GLES20.glDisableVertexAttribArray(mLinesPositionHandle);
      }
    }
  }
示例#13
0
  @Override
  public void onDrawEye(Eye eyeTransform) {
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

    checkGLError("colorParam");

    GLES20.glUseProgram(mProgram);
    GLES20.glDrawElements(
        GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

    checkGLError("befor activeTex");
    GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
    checkGLError("after activeTex");
    GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
    checkGLError("bind Text");

    checkGLError("mPostionHandle");
    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "position");
    GLES20.glEnableVertexAttribArray(mPositionHandle);
    GLES20.glVertexAttribPointer(
        mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);

    checkGLError("mTexture Handle");
    mTextureCoordHandle = GLES20.glGetAttribLocation(mProgram, "inputTextureCoordinate");
    GLES20.glEnableVertexAttribArray(mTextureCoordHandle);
    GLES20.glVertexAttribPointer(
        mTextureCoordHandle,
        COORDS_PER_VERTEX,
        GLES20.GL_FLOAT,
        false,
        vertexStride,
        textureVerticesBuffer);
    checkGLError("after mTexture Handle");

    mColorHandle = GLES20.glGetAttribLocation(mProgram, "s_texture");
    checkGLError("mColor Handel");

    GLES20.glDrawElements(
        GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

    checkGLError("Disable");
    GLES20.glDisableVertexAttribArray(mPositionHandle);
    GLES20.glDisableVertexAttribArray(mTextureCoordHandle);

    checkGLError("before Cube 2");
    Matrix.multiplyMM(mView, 0, eyeTransform.getEyeView(), 0, mCamera, 0);
    Matrix.multiplyMV(lightPosInEyeSpace, 0, mView, 0, LIGHT_POS_IN_WORLD_SPACE, 0);

    checkGLError("before Cube");
    float[] perspective = eyeTransform.getPerspective(Z_NEAR, Z_FAR);
    Matrix.multiplyMM(modelView, 0, mView, 0, mModelCube, 0);
    Matrix.multiplyMM(modelViewProjection, 0, perspective, 0, modelView, 0);
    drawCube();
  }
  SimpleShader() {
    program = loadProgram(vertexShader, fragmentShader);
    uMVMatrixHandle = GLES20.glGetUniformLocation(program, "mvMatrix");
    if (uMVMatrixHandle == 0) Log.d("WEEE", "" + uMVMatrixHandle);
    uPMatrixHandle = GLES20.glGetUniformLocation(program, "pMatrix");
    if (uPMatrixHandle == 0) Log.d("WEEE", "" + uPMatrixHandle);

    aVertexHandle = GLES20.glGetAttribLocation(program, "vertex");
    if (aVertexHandle == 0) Log.d("WEEE", "" + aVertexHandle);
    aNormalHandle = GLES20.glGetAttribLocation(program, "normal");
    if (aNormalHandle == 0) Log.d("WEEE", "" + aNormalHandle);
  }
 public void initShader(MySurfaceView mv) {
   // 加载顶点着色器的脚本内容
   mVertexShader = ShaderUtil.loadFromAssetsFile("vertex_tex.sh", mv.getResources());
   // 加载片元着色器的脚本内容
   mFragmentShader = ShaderUtil.loadFromAssetsFile("frag_tex.sh", mv.getResources());
   // 基于顶点着色器与片元着色器创建程序
   mProgram = ShaderUtil.createProgram(mVertexShader, mFragmentShader);
   // 获取程序中顶点位置属性引用
   maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
   // 获取程序中顶点纹理坐标属性引用
   maTexCoorHandle = GLES20.glGetAttribLocation(mProgram, "aTexCoor");
   // 获取程序中总变换矩阵id
   muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
 }
  /**
   * Encapsulates the OpenGL ES instructions for drawing this shape.
   *
   * @param mvpMatrix - The Model View Project matrix in which to draw this shape.
   */
  public void draw(float[] mvpMatrix, int eye) {
    // Add program to OpenGL environment
    GLES20.glUseProgram(mProgram);

    // get handle to vertex shader's vPosition member
    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
    mUVCoordsHandle = GLES20.glGetAttribLocation(mProgram, "aTexCoord");
    mSamplerUniformHandle = GLES20.glGetUniformLocation(mProgram, "sTexture");

    // Enable a handle to the triangle vertices
    GLES20.glEnableVertexAttribArray(mPositionHandle);
    GLES20.glEnableVertexAttribArray(mUVCoordsHandle);

    // Prepare the triangle coordinate data
    GLES20.glVertexAttribPointer(
        mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);

    GLES20.glVertexAttribPointer(mUVCoordsHandle, 2, GLES20.GL_FLOAT, true, 2 * 4, uvBuffer);

    int index = 0;
    int textureUnit = 0;
    if (eye == Eye.Type.LEFT) {
      index = 0;
    } else {
      index = 1;
    }

    textureUnit = GLES20.GL_TEXTURE0 + index;

    GLES20.glActiveTexture(textureUnit);
    GLES20.glUniform1i(mSamplerUniformHandle, index);

    // get handle to shape's transformation matrix
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    MyGLRenderer.checkGlError("glGetUniformLocation");

    // Apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
    MyGLRenderer.checkGlError("glUniformMatrix4fv");

    // Draw the square
    GLES20.glDrawElements(
        GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

    // Disable vertex array
    GLES20.glDisableVertexAttribArray(mPositionHandle);
    GLES20.glDisableVertexAttribArray(mUVCoordsHandle);
  }
  /**
   * Encapsulates the OpenGL ES instructions for drawing this shape.
   *
   * @param mvpMatrix - The Model View Project matrix in which to draw this shape.
   */
  public void draw(float[] mvpMatrix, boolean changeColor) {
    if (mProgram != 0) {
      // Add program to OpenGL environment
      GLES20.glUseProgram(mProgram);

      // Get handle to vertex shader's vPosition member
      mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

      // Enable a handle to the triangle vertices
      GLES20.glEnableVertexAttribArray(mPositionHandle);

      // Get handle to fragment shader's vColor member
      mColor = GLES20.glGetAttribLocation(mProgram, "vColor");

      // Enable a handle to the color vertices
      GLES20.glEnableVertexAttribArray(mColor);

      // Get handle to shape's transformation matrix
      mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
      checkGlError("glGetUniformLocation");

      // Apply the projection and view transformation
      GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
      checkGlError("glUniformMatrix4fv");

      // Prepare the coordinate data
      GLES20.glVertexAttribPointer(
          mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, VERTEX_STRIDE, mVertexBuffer);

      // Prepare the color data
      if (changeColor) {
        GLES20.glVertexAttribPointer(
            mColor, COORDS_PER_COLORS, GLES20.GL_FLOAT, false, COLORS_STRIDE, mColor2Buffer);
      } else {
        GLES20.glVertexAttribPointer(
            mColor, COORDS_PER_COLORS, GLES20.GL_FLOAT, false, COLORS_STRIDE, mColor1Buffer);
      }

      // Draw the shape
      GLES20.glDrawElements(
          GLES20.GL_TRIANGLES, INDICES.length, GLES20.GL_UNSIGNED_SHORT, mIndexBuffer);

      // Disable vertex array
      GLES20.glDisableVertexAttribArray(mPositionHandle);
      // Disable color array
      GLES20.glDisableVertexAttribArray(mColor);
    }
  }
示例#18
0
  public void draw(float[] mvpMatrix) {
    // Add program to OpenGL environment
    GLES20.glUseProgram(mProgram);

    // get handle to vertex shader's vPosition member
    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

    // Enable a handle to the triangle vertices
    GLES20.glEnableVertexAttribArray(mPositionHandle);

    // Prepare the triangle coordinate data
    GLES20.glVertexAttribPointer(
        mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);

    // get handle to fragment shader's vColor member
    mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

    // Set color for drawing the triangle
    GLES20.glUniform4fv(mColorHandle, 1, color, 0);

    // get handle to shape's transformation matrix
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    MyGLRenderer.checkGlError("glGetUniformLocation");

    // Apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
    MyGLRenderer.checkGlError("glUniformMatrix4fv");

    // Draw the square
    GLES20.glDrawElements(
        GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

    // Disable vertex array
    GLES20.glDisableVertexAttribArray(mPositionHandle);
  }
示例#19
0
  public void draw() {
    GLES20.glUseProgram(shaderProgram);

    int muMVPMat = GLES20.glGetUniformLocation(shaderProgram, "uMVPMatrix");
    GLESUtils.checkGlError("glGetUniformLocation");
    GLES20.glUniformMatrix4fv(muMVPMat, 1, false, scene.modelViewProjectionMatrix(), 0);
    GLESUtils.checkGlError("glUniformMatrix4fv");

    int mPositionH = vertexAttribArrayf("vPosition", CpV, cStride, posBuffer);
    GLESUtils.checkGlError("vPosition");

    int normalH = vertexAttribArrayf("vNormal", CpV, cStride, normalBuffer);
    GLESUtils.checkGlError("vNormal");

    int mColorH = GLES20.glGetAttribLocation(shaderProgram, "vColor");
    GLES20.glVertexAttrib4f(mColorH, c[0], c[1], c[2], c[3]);
    GLESUtils.checkGlError("vColor");

    int phaseH = vertexAttribArrayf("vPhase", 1, NumVerts, phaseBuffer);
    GLESUtils.checkGlError("vPhase");

    int gTime = GLES20.glGetUniformLocation(shaderProgram, "iGlobalTime");
    GLES20.glUniform1f(gTime, (float) (SystemClock.uptimeMillis() / 1000.0));
    GLESUtils.checkGlError("iGlobalTime");

    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, oBufferLength);

    GLES20.glDisableVertexAttribArray(mPositionH);
    GLES20.glDisableVertexAttribArray(normalH);
    GLES20.glDisableVertexAttribArray(mColorH);
    GLES20.glDisableVertexAttribArray(phaseH);
  }
示例#20
0
  public void draw() {

    // Add program to OpenGLES environment
    GLES20.glUseProgram(mProgram);

    // get handle to vertex shader's vPosition member
    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

    // Enable a handle to the triangle vertices
    GLES20.glEnableVertexAttribArray(mPositionHandle);

    // Prepare the triangle coordinate data
    GLES20.glVertexAttribPointer(
        mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, mVertexStride, mVertexBuffer);

    // get handle to fragment shader's vColor member
    mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

    // set color for drawing the triangle
    GLES20.glUniform4fv(mColorHandle, 1, mColor, 0);

    // draw the triangle
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, mVertexCount);

    // disable vertex array
    GLES20.glDisableVertexAttribArray(mPositionHandle);
  }
  public void draw(float[] mvpMatrix) {
    // Add program to OpenGL ES environment
    GLES20.glUseProgram(mProgram);

    // get handle to vertex shader's vPosition member
    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

    // Enable a handle to the triangle vertices
    GLES20.glEnableVertexAttribArray(mPositionHandle);

    // Prepare the triangle coordinate data
    GLES20.glVertexAttribPointer(
        mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);

    // get handle to fragment shader's vColor member
    mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

    // Set color for drawing the triangle
    GLES20.glUniform4fv(mColorHandle, 1, color, 0);

    // get handle to shape's transformation matrix
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");

    // Pass the projection and view transformation to the shader
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);

    // Draw the triangle
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);

    // Disable vertex array
    GLES20.glDisableVertexAttribArray(mPositionHandle);
  }
示例#22
0
 public Desktop(Display display) {
   mDisplay = display;
   mVertexShaderHandle = ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADER);
   mFragmentShaderHandle = ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER);
   mProgramHandle =
       ShaderHelper.createAndLinkProgram(
           mVertexShaderHandle,
           mFragmentShaderHandle,
           new String[] {"a_Position", "a_TexCoordinate", "u_CombinedMatrix", "u_Texture"});
   mCombinedMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_CombinedMatrix");
   mTextureUniformHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_Texture");
   mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position");
   mTransparentHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_transparency");
   mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_TexCoordinate");
   mTextureDataHandle = TextureHelper.createTextureHandle();
 }
示例#23
0
  public void draw() {
    // 将program加入OpenGL ES环境中
    GLES20.glUseProgram(mProgram);

    // 获取指向vertex shader的成员vPosition的 handle
    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

    // 启用一个指向三角形的顶点数组的handle
    GLES20.glEnableVertexAttribArray(mPositionHandle);

    // 准备三角形的坐标数据
    GLES20.glVertexAttribPointer(
        mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);

    // 获取指向fragment shader的成员vColor的handle
    mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

    // 设置三角形的颜色
    GLES20.glUniform4fv(mColorHandle, 1, color, 0);

    // 画三角形
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);

    // 禁用指向三角形的顶点数组
    GLES20.glDisableVertexAttribArray(mPositionHandle);
  }
示例#24
0
  public static void initGl() {
    // Initialize shaders and create/link program
    int vertexShaderHandle = GLUtil.loadShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADER_CODE);
    int fragShaderHandle = GLUtil.loadShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER_CODE);

    sProgramHandle = GLUtil.createAndLinkProgram(vertexShaderHandle, fragShaderHandle, null);
    sAttribPositionHandle = GLES20.glGetAttribLocation(sProgramHandle, "aPosition");
    sAttribTextureCoordsHandle = GLES20.glGetAttribLocation(sProgramHandle, "aTexCoords");
    sUniformMVPMatrixHandle = GLES20.glGetUniformLocation(sProgramHandle, "uMVPMatrix");
    sUniformTextureHandle = GLES20.glGetUniformLocation(sProgramHandle, "uTexture");
    sUniformAlphaHandle = GLES20.glGetUniformLocation(sProgramHandle, "uAlpha");

    // Compute max texture size
    int[] maxTextureSize = new int[1];
    GLES20.glGetIntegerv(GLES20.GL_MAX_TEXTURE_SIZE, maxTextureSize, 0);
    sMaxTextureSize = maxTextureSize[0];
  }
  // Function for initializing the renderer.
  private void initRendering() {
    mTeapot = new Teapot();
    mTextPlane = new TextPlane();

    mRenderer = Renderer.getInstance();

    GLES20.glClearColor(0.0f, 0.0f, 0.0f, Vuforia.requiresAlpha() ? 0.0f : 1.0f);

    for (Texture t : mTextures) {
      GLES20.glGenTextures(1, t.mTextureID, 0);
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, t.mTextureID[0]);
      GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
      GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
      GLES20.glTexImage2D(
          GLES20.GL_TEXTURE_2D,
          0,
          GLES20.GL_RGBA,
          t.mWidth,
          t.mHeight,
          0,
          GLES20.GL_RGBA,
          GLES20.GL_UNSIGNED_BYTE,
          t.mData);
    }

    shaderProgramID =
        SampleUtils.createProgramFromShaderSrc(
            CubeShaders.CUBE_MESH_VERTEX_SHADER, CubeShaders.CUBE_MESH_FRAGMENT_SHADER);

    vertexHandle = GLES20.glGetAttribLocation(shaderProgramID, "vertexPosition");
    normalHandle = GLES20.glGetAttribLocation(shaderProgramID, "vertexNormal");
    textureCoordHandle = GLES20.glGetAttribLocation(shaderProgramID, "vertexTexCoord");
    mvpMatrixHandle = GLES20.glGetUniformLocation(shaderProgramID, "modelViewProjectionMatrix");
    texSampler2DHandle = GLES20.glGetUniformLocation(shaderProgramID, "texSampler2D");

    try {
      mBuildingsModel = new SampleApplication3DModel();
      mBuildingsModel.loadModel(mActivity.getResources().getAssets(), "ImageTargets/Buildings.txt");
    } catch (IOException e) {
      Log.e(LOGTAG, "Unable to load buildings");
    }

    // Hide the Loading Dialog
    mActivity.loadingDialogHandler.sendEmptyMessage(LoadingDialogHandler.HIDE_LOADING_DIALOG);
  }
示例#26
0
  public void Draw(float[] viewProjectionMatrix, int textureName) {
    GLES20.glUseProgram(shaderProgram);
    // set position
    int aPosition = GLES20.glGetAttribLocation(shaderProgram, "aPosition");
    GLES20.glEnableVertexAttribArray(aPosition);
    GLES20.glVertexAttribPointer(aPosition, 3, GLES20.GL_FLOAT, false, 0, vertexBuffer);

    // set textcoord
    int aTexcoord = GLES20.glGetAttribLocation(shaderProgram, "aTexcoord");
    GLES20.glEnableVertexAttribArray(aTexcoord);
    GLES20.glVertexAttribPointer(aTexcoord, 2, GLES20.GL_FLOAT, false, 0, textureCoordinatesBuffer);

    // set matrix Projection * View * Rotation * world
    Matrix.multiplyMM(worldViewProjectionMatrix, 0, viewProjectionMatrix, 0, worldMatrix, 0);
    int mat4x4 = GLES20.glGetUniformLocation(shaderProgram, "matrix");
    GLES20.glUniformMatrix4fv(mat4x4, 1, false, worldViewProjectionMatrix, 0);

    int uTemp = GLES20.glGetUniformLocation(shaderProgram, "uTemp");

    if (textureName != 0) {
      GLES20.glUniform1f(uTemp, 1.0f);

      int uTexture = GLES20.glGetUniformLocation(shaderProgram, "uTexture");
      GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureName);
      GLES20.glUniform1i(uTexture, 0);

      int uTexture2 = GLES20.glGetUniformLocation(shaderProgram, "uTexture2");
      GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture.getName());
      GLES20.glUniform1i(uTexture2, 1);

    } else GLES20.glUniform1f(uTemp, 0.0f);

    // Draw Cube
    GLES20.glDrawElements(
        GLES20.GL_TRIANGLE_STRIP, index.length, GLES20.GL_UNSIGNED_SHORT, indexBuffer);

    // Disable attribute
    GLES20.glDisableVertexAttribArray(aPosition);
    GLES20.glDisableVertexAttribArray(aTexcoord);
  }
示例#27
0
 // 初始化shader
 public void intShader(MySurfaceView mv) {
   // 加载顶点着色器的脚本内容
   mVertexShader = ShaderUtil.loadFromAssetsFile("vertex_color.sh", mv.getResources());
   ShaderUtil.checkGlError("==ss==");
   // 加载片元着色器的脚本内容
   mFragmentShader = ShaderUtil.loadFromAssetsFile("frag_color.sh", mv.getResources());
   // 基于顶点着色器与片元着色器创建程序
   ShaderUtil.checkGlError("==ss==");
   mProgram = ShaderUtil.createProgram(mVertexShader, mFragmentShader);
   // 获取程序中顶点位置属性引用id
   maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
   // 获取程序中总变换矩阵引用id
   muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
   muMMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMMatrix");
   // 获取程序中顶点法向量属性引用
   maNormalHandle = GLES20.glGetAttribLocation(mProgram, "aNormal");
   // 获取程序中光源位置引用
   maLightLocationHandle = GLES20.glGetUniformLocation(mProgram, "uLightLocation");
   // 获取程序中摄像机位置引用
   maCameraHandle = GLES20.glGetUniformLocation(mProgram, "uCamera");
 }
示例#28
0
 /** Initializes GL state. Call this after the EGL surface has been created and made current. */
 public void surfaceCreated() {
   mProgram = createProgram(VERTEX_SHADER, FRAGMENT_SHADER);
   if (mProgram == 0) {
     throw new RuntimeException("failed creating program");
   }
   maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
   checkGlError("glGetAttribLocation aPosition");
   if (maPositionHandle == -1) {
     throw new RuntimeException("Could not get attrib location for aPosition");
   }
   maTextureHandle = GLES20.glGetAttribLocation(mProgram, "aTextureCoord");
   checkGlError("glGetAttribLocation aTextureCoord");
   if (maTextureHandle == -1) {
     throw new RuntimeException("Could not get attrib location for aTextureCoord");
   }
   muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
   checkGlError("glGetUniformLocation uMVPMatrix");
   if (muMVPMatrixHandle == -1) {
     throw new RuntimeException("Could not get attrib location for uMVPMatrix");
   }
   muSTMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uSTMatrix");
   checkGlError("glGetUniformLocation uSTMatrix");
   if (muSTMatrixHandle == -1) {
     throw new RuntimeException("Could not get attrib location for uSTMatrix");
   }
   int[] textures = new int[1];
   GLES20.glGenTextures(1, textures, 0);
   mTextureID = textures[0];
   GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID);
   checkGlError("glBindTexture mTextureID");
   GLES20.glTexParameterf(
       GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
   GLES20.glTexParameterf(
       GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
   GLES20.glTexParameteri(
       GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
   GLES20.glTexParameteri(
       GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
   checkGlError("glTexParameter");
 }
示例#29
0
  public Points() {
    mRGB[0] = 0.8f;
    mRGB[1] = 0.2f;
    mRGB[2] = 0.2f;
    // prepare shaders and OpenGL program
    int vertexShader = ShaderHelper.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = ShaderHelper.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

    mProgram = ShaderHelper.createAndLinkProgram(vertexShader, fragmentShader);

    mPosition = GLES20.glGetAttribLocation(mProgram, "vPosition");
    mColor = GLES20.glGetAttribLocation(mProgram, "vColor");
    mMVPMatrix = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");

    initSomePoints();
    loadVBO();

    mMediumPointThread = new MediumPoint();
    mMediumPointThread.start();

    enable();
    Log.d(TAG, "Rendered points = " + totalPoints);
  }
  // 自定义初始化着色器的initShader方法
  public void initShader(MySurfaceView mv) {
    // 加载顶点着色器的脚本内容
    mVertexShader = ShaderUtil.loadFromAssetsFile("vertex_tex_light.sh", mv.getResources());
    // 加载片元着色器的脚本内容
    mFragmentShader = ShaderUtil.loadFromAssetsFile("frag_tex_light.sh", mv.getResources());
    // 基于顶点着色器与片元着色器创建程序
    mProgram = createProgram(mVertexShader, mFragmentShader);
    // 获取程序中顶点位置属性引用id
    maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
    // 获取程序中顶点纹理坐标属性引用id
    maTexCoorHandle = GLES20.glGetAttribLocation(mProgram, "aTexCoor");
    // 获取程序中总变换矩阵引用id
    muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");

    // 获取程序中顶点法向量属性引用id
    maNormalHandle = GLES20.glGetAttribLocation(mProgram, "aNormal");
    // 获取程序中摄像机位置引用id
    maCameraHandle = GLES20.glGetUniformLocation(mProgram, "uCamera");
    // 获取程序中光源位置引用id
    maLightLocationHandle = GLES20.glGetUniformLocation(mProgram, "uLightLocation");
    // 获取位置、旋转变换矩阵引用id
    muMMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMMatrix");
  }