Ejemplo n.º 1
0
    public void draw(float[] viewProjMatrix) {
        glUseProgram(mProgram);

        multiplyMM(mMVPMatrix, 0, viewProjMatrix, 0, mModelMatrix, 0);
        int handle = glGetUniformLocation(mProgram, Constants.U_MVP_MATRIX);
        glUniformMatrix4fv(handle, 1, false, mMVPMatrix, 0);

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, mTextureA);
        handle = glGetUniformLocation(mProgram, Constants.U_TEX_UNIT_0);
        glUniform1i(handle, 0);

        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, mTextureB);
        handle = glGetUniformLocation(mProgram, Constants.U_TEX_UNIT_1);
        glUniform1i(handle, 1);

        handle = glGetUniformLocation(mProgram, Constants.U_MIX_FACTOR);
        glUniform1f(handle, calcMixFactor());

        handle = glGetUniformLocation(mProgram, Constants.U_BACK_COLOR);
        glUniform3fv(handle, 1, mBackColor, 0);

        mMesh.draw(mProgram);
    }
Ejemplo n.º 2
0
  public static void bindPillowTexture(int color, int state, int deathTimeCounter, int direction) {
    if (state == ALIVE || state == BORNING) {

      if (direction == DIR_LEFT)
        GLES20.glVertexAttribPointer(
            Render.maTexCoordsHandle, 2, GLES20.GL_FLOAT, false, 8, Render.rect1TexCoordsBuf);
      else if (direction == DIR_UP)
        GLES20.glVertexAttribPointer(
            Render.maTexCoordsHandle, 2, GLES20.GL_FLOAT, false, 8, Render.rect2TexCoordsBuf);
      else if (direction == DIR_RIGHT)
        GLES20.glVertexAttribPointer(
            Render.maTexCoordsHandle, 2, GLES20.GL_FLOAT, false, 8, Render.rect3TexCoordsBuf);
      else if (direction == DIR_DOWN)
        GLES20.glVertexAttribPointer(
            Render.maTexCoordsHandle, 2, GLES20.GL_FLOAT, false, 8, Render.rect4TexCoordsBuf);
      //

      if (color == COLOR_GREY) {
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, Render.charGreyTexID);
      } else if (color == COLOR_RED) {
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, Render.charRedTexID);
      }
    } else {
      findDyingTexture(direction, color, deathTimeCounter);
    }

    Render.quadTCBinded = false;
  }
  public void onDraw(
      final int textureId, final FloatBuffer cubeBuffer, final FloatBuffer textureBuffer) {
    GLES20.glUseProgram(mGLProgId);
    runPendingOnDrawTasks();
    if (!mIsInitialized) {
      return;
    }

    cubeBuffer.position(0);
    GLES20.glVertexAttribPointer(mGLAttribPosition, 2, GLES20.GL_FLOAT, false, 0, cubeBuffer);
    GLES20.glEnableVertexAttribArray(mGLAttribPosition);
    textureBuffer.position(0);
    GLES20.glVertexAttribPointer(
        mGLAttribTextureCoordinate, 2, GLES20.GL_FLOAT, false, 0, textureBuffer);
    GLES20.glEnableVertexAttribArray(mGLAttribTextureCoordinate);
    if (textureId != OpenGlUtils.NO_TEXTURE) {
      GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
      GLES20.glUniform1i(mGLUniformTexture, 0);
    }
    onDrawArraysPre();
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
    GLES20.glDisableVertexAttribArray(mGLAttribPosition);
    GLES20.glDisableVertexAttribArray(mGLAttribTextureCoordinate);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
  }
Ejemplo n.º 4
0
  /**
   * draw specific texture with specific texture matrix
   *
   * @param tex_id texture ID
   * @param tex_matrix texture matrix、if this is null, the last one use(we don't check size of this
   *     array and needs at least 16 of float)
   */
  public void draw(final int tex_id, final float[] tex_matrix) {
    GLES20.glUseProgram(hProgram);
    if (tex_matrix != null) GLES20.glUniformMatrix4fv(muTexMatrixLoc, 1, false, tex_matrix, 0);

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, tex_id);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, VERTEX_NUM);
    GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0);
    GLES20.glUseProgram(0);
  }
    @Override
    public void onDrawFrame(GL10 glUnused) {
      GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
      GLES20.glUseProgram(mProgram);
      checkGlError("glUseProgram");

      if (mBuffer != null) {
        synchronized (this) {
          GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
          GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureY[0]);
          GLES20.glTexImage2D(
              GLES20.GL_TEXTURE_2D,
              0,
              GLES20.GL_LUMINANCE,
              mBufferWidthY,
              mBufferHeightY,
              0,
              GLES20.GL_LUMINANCE,
              GLES20.GL_UNSIGNED_BYTE,
              mBuffer.position(mBufferPositionY));
          GLES20.glUniform1i(muSamplerYHandle, 0);

          GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
          GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureU[0]);
          GLES20.glTexImage2D(
              GLES20.GL_TEXTURE_2D,
              0,
              GLES20.GL_LUMINANCE,
              mBufferWidthUV,
              mBufferHeightUV,
              0,
              GLES20.GL_LUMINANCE,
              GLES20.GL_UNSIGNED_BYTE,
              mBuffer.position(mBufferPositionU));
          GLES20.glUniform1i(muSamplerUHandle, 1);

          GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
          GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureV[0]);
          GLES20.glTexImage2D(
              GLES20.GL_TEXTURE_2D,
              0,
              GLES20.GL_LUMINANCE,
              mBufferWidthUV,
              mBufferHeightUV,
              0,
              GLES20.GL_LUMINANCE,
              GLES20.GL_UNSIGNED_BYTE,
              mBuffer.position(mBufferPositionV));
          GLES20.glUniform1i(muSamplerVHandle, 2);
        }
      }

      GLES20.glDrawElements(
          GLES20.GL_TRIANGLES, INDICES_DATA.length, GLES20.GL_UNSIGNED_SHORT, mIndices);
    }
Ejemplo n.º 6
0
 public static void bindPillowGunTexture(int gunType) {
   if (gunType == GUN_PISTOL) GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, Render.pistolTexID);
   else if (gunType == GUN_BAZOOKA)
     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, Render.bazookaPillowTexID);
   else if (gunType == GUN_MACHINGUN)
     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, Render.machinGunPillowTexID);
   else if (gunType == GUN_SHOTGUN)
     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, Render.shotGunPillowTexID);
   else if (gunType == GUN_MINE)
     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, Render.minePillowTexID);
 }
Ejemplo n.º 7
0
  /** The Surface is created/init() */
  @Override
  public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    Log.i("Almalence", "GLLayer.onSurfaceCreated()");

    PluginManager.getInstance().onGLSurfaceCreated(gl, config);

    if (PluginManager.getInstance().shouldPreviewToGPU()) {
      final int[] tex = new int[1];
      GLES20.glGenTextures(1, tex, 0);
      this.texture_preview = tex[0];

      GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, this.texture_preview);
      GLES20.glTexParameteri(
          GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
      GLES20.glTexParameteri(
          GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
      GLES20.glTexParameteri(
          GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
      GLES20.glTexParameteri(
          GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
      GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0);

      this.surfaceTexture = new SurfaceTexture(this.texture_preview);
      this.surfaceTexture.setOnFrameAvailableListener(
          new OnFrameAvailableListener() {
            @Override
            public void onFrameAvailable(final SurfaceTexture surfaceTexture) {
              PluginManager.getInstance().onFrameAvailable();
            }
          });

      final Camera camera = CameraController.getCamera();
      if (camera == null) {
        return;
      }

      try {
        camera.setDisplayOrientation(90);
      } catch (RuntimeException e) {
        e.printStackTrace();
      }

      try {
        camera.setPreviewTexture(this.surfaceTexture);
      } catch (final IOException e) {
        e.printStackTrace();
      }

      camera.startPreview();
    }
  }
Ejemplo n.º 8
0
 private void load(Bitmap bitmap, boolean mipmap) {
   int[] textureIds = new int[1];
   GLES20.glGenTextures(1, textureIds, 0);
   textureId = textureIds[0];
   this.width = bitmap.getWidth();
   this.height = bitmap.getHeight();
   GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
   GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
   if (mipmap) {
     GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
     setFilter(GLES20.GL_LINEAR_MIPMAP_NEAREST, GLES20.GL_LINEAR_MIPMAP_LINEAR);
   } else setFilter(GLES20.GL_NEAREST, GLES20.GL_LINEAR);
   bitmap.recycle();
   GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
 }
Ejemplo n.º 9
0
  public boolean Draw(Matrix4 projectionMatrix, Matrix4 viewMatrix) {
    GLES20.glUseProgram(programID);

    Matrix4 mvp =
        Matrix4.Multiply(
            projectionMatrix, Matrix4.Multiply(viewMatrix, parent.transform.transformMatrix));

    float[] mvpArray = mvp.GetOneDimensionalArray();

    GLES20.glUniformMatrix4fv(mvpLocation, 1, false, mvpArray, 0);

    GLES20.glUniform1f(opacityLocation, opacity);

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureID);

    GLES20.glUniform1i(textureSamplerLocation, 0);

    // Log.i(DEBUG_TAG,"Enabling " + vertexArrayList.size() + " attribs");

    for (GLVertexArrayObject element : vertexArrayList) {
      GLES20.glEnableVertexAttribArray(element.attributeNumber);
    }

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

    for (GLVertexArrayObject element : vertexArrayList) {
      GLES20.glDisableVertexAttribArray(element.attributeNumber);
    }
    return true;
  }
Ejemplo n.º 10
0
  boolean InitTexture(int ResourceId) {
    /*
     * Android
     * public static void glGenTextures (int n, int[] textures, int offset)
     * Returns n currently unused names for texture objects in the array textures.
     */
    int[] textures = new int[1];
    GLES20.glGenTextures(1, textures, 0);

    /*
     * Android
     * public static void glBindTexture (int target, int texture)
     */
    m_TextureId = textures[0];
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, m_TextureId);

    // Loads in Texture from Resource File
    LoadTexture(ResourceId);

    /*
     * Android
     * static void	 texImage2D(int target, int level, Bitmap bitmap, int border)
     * A version of texImage2D that determines the internalFormat and type automatically.
     */
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, m_Bitmap, 0);

    return true;
  }
Ejemplo n.º 11
0
  protected SurfaceTexture createSurfaceTexture() {
    int tex[] = new int[1];
    GLES20.glGenTextures(1, tex, 0);
    //		GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
    GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex[0]);
    GLES20.glTexParameteri(
        GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(
        GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    textureName = tex[0];
    Log.d("OESTexture", "createSurfaceTexture textureName:" + textureName);
    final SurfaceTexture t = new SurfaceTexture(tex[0]);
    t.setOnFrameAvailableListener(
        new SurfaceTexture.OnFrameAvailableListener() {

          @Override
          public void onFrameAvailable(SurfaceTexture surfaceTexture) {
            textureUpdated = true;
            // Log.d("OESTexture", "onFrameAvailable textureName:" + textureName);
          }
        });
    return t;
  }
Ejemplo n.º 12
0
  public void drawSelf(int texId) {
    MatrixState.rotate(xAngle, 1, 0, 0);
    MatrixState.rotate(yAngle, 0, 1, 0);
    MatrixState.rotate(zAngle, 0, 0, 1);

    // 制定使用某套shader程序
    GLES20.glUseProgram(mProgram);

    // 将最终变换矩阵传入shader程序
    GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);

    // 传送顶点位置数据
    GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, mVertexBuffer);
    // 传送顶点纹理坐标数据
    GLES20.glVertexAttribPointer(maTexCoorHandle, 2, GLES20.GL_FLOAT, false, 2 * 4, mTexCoorBuffer);

    // 启用顶点位置数据
    GLES20.glEnableVertexAttribArray(maPositionHandle);
    // 启用顶点纹理数据
    GLES20.glEnableVertexAttribArray(maTexCoorHandle);

    // 绑定纹理
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);

    // 绘制纹理矩形
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
  }
Ejemplo n.º 13
0
  public static int loadTexture(final Context context, final int resourceId) {
    final int[] textureHandle = new int[1];

    GLES20.glGenTextures(1, textureHandle, 0);

    if (textureHandle[0] != 0) {
      final BitmapFactory.Options options = new BitmapFactory.Options();
      options.inScaled = false;

      final Bitmap bitmap =
          BitmapFactory.decodeResource(context.getResources(), resourceId, options);

      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);

      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

      GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

      bitmap.recycle();
    }

    if (textureHandle[0] == 0) {
      throw new RuntimeException("Error loading texture.");
    }

    return textureHandle[0];
  }
Ejemplo n.º 14
0
 /** Inform this View of the dimensions of frames coming from |stream|. */
 public void setSize(Endpoint stream, int width, int height) {
   // Generate 3 texture ids for Y/U/V and place them into |textures|,
   // allocating enough storage for |width|x|height| pixels.
   int[] textures = yuvTextures[stream == Endpoint.LOCAL ? 0 : 1];
   GLES20.glGenTextures(3, textures, 0);
   for (int i = 0; i < 3; ++i) {
     int w = i == 0 ? width : width / 2;
     int h = i == 0 ? height : height / 2;
     GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i);
     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[i]);
     GLES20.glTexImage2D(
         GLES20.GL_TEXTURE_2D,
         0,
         GLES20.GL_LUMINANCE,
         w,
         h,
         0,
         GLES20.GL_LUMINANCE,
         GLES20.GL_UNSIGNED_BYTE,
         null);
     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.glTexParameterf(
         GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
     GLES20.glTexParameterf(
         GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
   }
   checkNoGLES2Error();
 }
Ejemplo n.º 15
0
  public static int loadTexture2(final Bitmap bitmap) {
    final int[] textureHandle = new int[1];

    glGenTextures(1, textureHandle, 0);

    if (textureHandle[0] != 0) {
      // Bind to the texture in OpenGL
      glBindTexture(GL_TEXTURE_2D, textureHandle[0]);

      // Set filtering
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

      // Load the bitmap into the bound texture.
      GLUtils.texSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap);

      //			Buffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
      //			bitmap.copyPixelsToBuffer(byteBuffer);
      //			glTexImage2D(GL_TEXTURE_2D, 0, GLES20.GL_RGB, bitmap.getWidth(), bitmap.getHeight(),
      //					0, GLES20.GL_RGB, GLES20.GL_UNSIGNED_BYTE, byteBuffer);

      // Recycle the bitmap, since its data has been loaded into OpenGL.
      bitmap.recycle();
    } else {
      throw new RuntimeException("Error loading texture.");
    }

    return textureHandle[0];
  }
Ejemplo n.º 16
0
 protected void onDrawArraysPre() {
   if (mToneCurveTexture[0] != -1) {
     GLES20.glActiveTexture(GLES20.GL_TEXTURE3);
     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mToneCurveTexture[0]);
     GLES20.glUniform1i(mToneCurveTextureUniformLocation, 3);
   }
 }
Ejemplo n.º 17
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);
  }
Ejemplo n.º 18
0
 protected void onDrawArraysAfter() {
   if (mToneCurveTexture[0] != -1) {
     GLES20.glActiveTexture(GLES20.GL_TEXTURE3);
     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
     GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
   }
 }
Ejemplo n.º 19
0
  public static int loadTexture(final Context context, final int resourceId) {
    final int[] textureHandle = new int[1];

    GLES20.glGenTextures(1, textureHandle, 0);

    if (textureHandle[0] != 0) {
      final BitmapFactory.Options options = new BitmapFactory.Options();
      options.inScaled = false; // No pre-scaling

      // Read in the resource
      final Bitmap bitmap =
          BitmapFactory.decodeResource(context.getResources(), resourceId, options);

      // Bind to the texture in OpenGL
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);

      // Set filtering
      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
      // Load the bitmap into the bound texture.
      GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

      // Recycle the bitmap, since its data has been loaded into OpenGL.
      bitmap.recycle();
    }

    if (textureHandle[0] == 0) {
      throw new RuntimeException("Error loading texture.");
    }

    return textureHandle[0];
  }
Ejemplo n.º 20
0
  /**
   * Creates a texture from raw data.
   *
   * @param data Image data, in a "direct" ByteBuffer.
   * @param width Texture width, in pixels (not bytes).
   * @param height Texture height, in pixels.
   * @param format Image data format (use constant appropriate for glTexImage2D(), e.g. GL_RGBA).
   * @return Handle to texture.
   */
  public static int createImageTexture(ByteBuffer data, int width, int height, int format) {
    int[] textureHandles = new int[1];
    int textureHandle;

    GLES20.glGenTextures(1, textureHandles, 0);
    textureHandle = textureHandles[0];
    GlUtil.checkGlError("glGenTextures");

    // Bind the texture handle to the 2D texture target.
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle);

    // Configure min/mag filtering, i.e. what scaling method do we use if what we're rendering
    // is smaller or larger than the source image.
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GlUtil.checkGlError("loadImageTexture");

    // Load the data from the buffer into the texture handle.
    GLES20.glTexImage2D(
        GLES20.GL_TEXTURE_2D, /*level*/
        0,
        format,
        width,
        height, /*border*/
        0,
        format,
        GLES20.GL_UNSIGNED_BYTE,
        data);
    GlUtil.checkGlError("loadImageTexture");

    return textureHandle;
  }
Ejemplo n.º 21
0
  public void drawSelf(int texId) {
    // 制定使用某套shader程序
    GLES20.glUseProgram(mProgram);
    // 将最终变换矩阵传入shader程序
    GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
    // 将位置、旋转变换矩阵传入shader程序
    GLES20.glUniformMatrix4fv(muMMatrixHandle, 1, false, MatrixState.getMMatrix(), 0);
    // 将摄像机位置传入shader程序
    GLES20.glUniform3fv(maCameraHandle, 1, MatrixState.cameraFB);
    // 将光源位置传入shader程序
    GLES20.glUniform3fv(maLightLocationHandle, 1, MatrixState.lightPositionFB);

    // 传送顶点位置数据
    GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, mVertexBuffer);
    // 传送顶点纹理坐标数据
    GLES20.glVertexAttribPointer(maTexCoorHandle, 2, GLES20.GL_FLOAT, false, 2 * 4, mTexCoorBuffer);
    // 传送顶点法向量数据
    GLES20.glVertexAttribPointer(maNormalHandle, 4, GLES20.GL_FLOAT, false, 3 * 4, mNormalBuffer);

    // 启用顶点位置数据
    GLES20.glEnableVertexAttribArray(maPositionHandle);
    // 启用顶点纹理数据
    GLES20.glEnableVertexAttribArray(maTexCoorHandle);
    // 启用顶点法向量数据
    GLES20.glEnableVertexAttribArray(maNormalHandle);
    // 绑定纹理
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);

    // 绘制纹理矩形
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
  }
  @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();
  }
Ejemplo n.º 23
0
  @Override
  public void draw(
      float[] mMVPMatrix, float[] mRotationMatrix, float[] mvMatrix, float[] mProjMatrix) {
    if (visible && canvasSurfaceCreated()) {

      if (!initialized) {
        if (!initObject()) {
          return;
        }
      }

      // Add program to OpenGL environment
      GLES20.glUseProgram(programHandle);

      GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureDataHandle);

      GLES20.glUniform1i(textureUniformHandle, 0);

      // Enable a handle to the triangle vertices
      // Prepare the triangle coordinate data
      vBuffer.position(0);
      GLES20.glVertexAttribPointer(
          positionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vBuffer);
      GLES20.glEnableVertexAttribArray(positionHandle);

      // Pass in the texture coordinate information
      textureBuffer.position(0);
      GLES20.glVertexAttribPointer(
          textureCoordinateHandle,
          COORDS_PER_TEXTURE,
          GLES20.GL_FLOAT,
          false,
          (COORDS_PER_TEXTURE * 4),
          textureBuffer);
      GLES20.glEnableVertexAttribArray(textureCoordinateHandle);

      Matrix.multiplyMM(mMVPMatrix, 0, mvMatrix, 0, mModelMatrix, 0);

      // Apply the projection and view transformation
      GLES20.glUniformMatrix4fv(mModelMatrixHandle, 1, false, mMVPMatrix, 0);
      GLES20Renderer.checkGLError("glUniformMatrix4fv");

      Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);

      GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
      GLES20Renderer.checkGLError("glUniformMatrix4fv");

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

      // Disable vertex array
      // GLES20.glDisableVertexAttribArray(positionHandle);
    }
  }
Ejemplo n.º 24
0
 void ActivateTexture() {
   // Activate Texture
   if (m_TextureId != 0) {
     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, m_TextureId);
     // Log.d("DEBUG - Texture", "Texture Activated! ");
   } else {
     Log.e("ERROR - Texture ERROR- m_TextureId = 0", "Error in ActivateTexture()! ");
   }
 }
Ejemplo n.º 25
0
 protected void onDrawArraysPre() {
   for (int i = 0;
       i < inputTextureHandles.length && inputTextureHandles[i] != OpenGLUtils.NO_TEXTURE;
       i++) {
     GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i);
     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, inputTextureHandles[i]);
     GLES20.glUniform1i(inputTextureUniformLocations[i], i + 1);
   }
 }
Ejemplo n.º 26
0
 protected void onDrawArraysAfter() {
   for (int i = 0;
       i < inputTextureHandles.length && inputTextureHandles[i] != OpenGLUtils.NO_TEXTURE;
       i++) {
     GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i);
     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
     GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
   }
 }
Ejemplo n.º 27
0
  static void initTexture(int id) {
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, id);

    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.glTexParameterf(
        GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); // Set U Wrapping
    GLES20.glTexParameterf(
        GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); // Set V Wrapping
  }
Ejemplo n.º 28
0
 private void makeTexture() {
   int[] handleArr = new int[1];
   glGenTextures(1, handleArr, 0);
   handle = handleArr[0];
   glBindTexture(GL_TEXTURE_2D, handle);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 }
Ejemplo n.º 29
0
    public void onDrawFrame(GL10 glUnused) {

      synchronized (this) {
        if (updateSurface) {
          mSurface.updateTexImage();
          mSurface.getTransformMatrix(mSTMatrix);
          updateSurface = false;
        } else {
          return;
        }
      }

      GLES20.glClearColor(255.0f, 255.0f, 255.0f, 1.0f);
      GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);

      GLES20.glUseProgram(mProgram);
      checkGlError("glUseProgram");

      GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
      GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureID);

      mTriangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
      GLES20.glVertexAttribPointer(
          maPositionHandle,
          3,
          GLES20.GL_FLOAT,
          false,
          TRIANGLE_VERTICES_DATA_STRIDE_BYTES,
          mTriangleVertices);
      checkGlError("glVertexAttribPointer maPosition");
      GLES20.glEnableVertexAttribArray(maPositionHandle);
      checkGlError("glEnableVertexAttribArray maPositionHandle");

      mTextureVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
      GLES20.glVertexAttribPointer(
          maTextureHandle,
          2,
          GLES20.GL_FLOAT,
          false,
          TEXTURE_VERTICES_DATA_STRIDE_BYTES,
          mTextureVertices);

      checkGlError("glVertexAttribPointer maTextureHandle");
      GLES20.glEnableVertexAttribArray(maTextureHandle);
      checkGlError("glEnableVertexAttribArray maTextureHandle");

      Matrix.setIdentityM(mMVPMatrix, 0);

      GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
      GLES20.glUniformMatrix4fv(muSTMatrixHandle, 1, false, mSTMatrix, 0);

      GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
      checkGlError("glDrawArrays");
      GLES20.glFinish();
    }
Ejemplo n.º 30
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);
  }