示例#1
0
 public boolean AssignShaderProgram(String materialName) {
   programID = MainGame.singleton.sharedResources.GetShaderProgram(materialName);
   mvpLocation = GLES20.glGetUniformLocation(programID, UNIFORM_MATRIX_MVP);
   opacityLocation = GLES20.glGetUniformLocation(programID, "opacity");
   textureSamplerLocation = GLES20.glGetUniformLocation(programID, UNIFORM_TEX_SAMPLER);
   return (programID != 0);
 }
示例#2
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;
  }
示例#3
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);
   }
 }
示例#4
0
  private int compileShader(final int shaderType, final String shaderSource) {
    int shaderHandle = GLES20.glCreateShader(shaderType);

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

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

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

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

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

    return shaderHandle;
  }
示例#5
0
 /**
  * Applies the Rajawali default GL state to the driver. Developers who wish to change this default
  * behavior can override this method.
  */
 public void resetGLState() {
   GLES20.glEnable(GLES20.GL_CULL_FACE);
   GLES20.glCullFace(GLES20.GL_BACK);
   GLES20.glFrontFace(GLES20.GL_CCW);
   GLES20.glDisable(GLES20.GL_BLEND);
   GLES20.glEnable(GLES20.GL_DEPTH_TEST);
 }
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
      // 设置屏幕背景色RGBA
      GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
      // 打开深度检测
      GLES20.glEnable(GLES20.GL_DEPTH_TEST);
      MatrixState.setInitStack();

      treeTextureId = initTexture(R.drawable.tree);

      desert =
          new Desert(
              MySurfaceView.this,
              new float[] {
                0, 0, 0, 6, 6, 6,
                6, 6, 6, 0, 0, 0
              },
              30,
              20);
      desertId = initTexture(R.drawable.desert);

      group = new TreeGroup(MySurfaceView.this, treeTextureId);

      Collections.sort(mRender.group.trees);
    }
  @Override
  protected void onDrawScene(final GLState pGLState, final Camera pFirstCamera) {
    if (super.mScene != null) {
      final Camera secondCamera = this.getSecondCamera();

      final int surfaceWidth = this.mSurfaceWidth;
      final int surfaceWidthHalf = surfaceWidth >> 1;

      final int surfaceHeight = this.mSurfaceHeight;

      pGLState.enableScissorTest();

      /* First Screen. With first camera, on the left half of the screens width. */
      {
        GLES20.glScissor(0, 0, surfaceWidthHalf, surfaceHeight);
        GLES20.glViewport(0, 0, surfaceWidthHalf, surfaceHeight);

        super.mScene.onDraw(pGLState, pFirstCamera);
        pFirstCamera.onDrawHUD(pGLState);
      }

      /* Second Screen. With second camera, on the right half of the screens width. */
      {
        GLES20.glScissor(surfaceWidthHalf, 0, surfaceWidthHalf, surfaceHeight);
        GLES20.glViewport(surfaceWidthHalf, 0, surfaceWidthHalf, surfaceHeight);

        super.mScene.onDraw(pGLState, secondCamera);
        secondCamera.onDrawHUD(pGLState);
      }

      pGLState.disableScissorTest();
    }
  }
  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);
  }
 public void drawObject(float[] paramArrayOfFloat)
   throws OpenGLException
 {
   if (!this.mInitialized);
   do
   {
     return;
     if (this.mTextures.size() == 0)
     {
       LG.d("No textures defined!");
       return;
     }
     this.mBackgroundSprite.setShader(this.mLineShader);
     this.mBackgroundSprite.setTexture((GLTexture)this.mTextures.get(0));
     this.mBackgroundSprite.draw(paramArrayOfFloat);
     this.mPreviewSprite.setTexture((GLTexture)this.mTextures.get(0));
     this.mPreviewSprite.setShader(this.mShader);
     this.mPreviewSprite.draw(paramArrayOfFloat);
   }
   while (!this.mValidEstimate);
   GLES20.glDisable(2929);
   updateCurrentFrameOutline();
   this.mLineShader.bind();
   this.mLineShader.setVertices(this.mOutlineVertices);
   this.mLineShader.setTransform(paramArrayOfFloat);
   this.mOutlineIndices.position(0);
   this.mOutlineVertices.position(0);
   GLES20.glLineWidth(2.0F);
   GLES20.glDrawElements(2, 20, 5123, this.mOutlineIndices);
   GLES20.glEnable(2929);
 }
  @Override
  protected void onRender(final long ellapsedTime, final double deltaTime) {
    mUserScene = getCurrentScene();

    setRenderTarget(mLeftRenderTarget);
    getCurrentScene().switchCamera(mCameraLeft);
    GLES20.glViewport(0, 0, mViewportWidthHalf, mDefaultViewportHeight);
    mCameraLeft.setProjectionMatrix(mViewportWidthHalf, mDefaultViewportHeight);
    mCameraLeft.setOrientation(mCameraOrientation);

    render(ellapsedTime, deltaTime);

    setRenderTarget(mRightRenderTarget);

    getCurrentScene().switchCamera(mCameraRight);
    mCameraRight.setProjectionMatrix(mViewportWidthHalf, mDefaultViewportHeight);
    mCameraRight.setOrientation(mCameraOrientation);

    render(ellapsedTime, deltaTime);

    switchSceneDirect(mSideBySideScene);
    GLES20.glViewport(0, 0, mDefaultViewportWidth, mDefaultViewportHeight);

    setRenderTarget(null);

    render(ellapsedTime, deltaTime);

    switchSceneDirect(mUserScene);
  }
  public Triangle() {
    // initialize vertex byte buffer for shape coordinates
    ByteBuffer bb =
        ByteBuffer.allocateDirect(
            // (number of coordinate values * 4 bytes per float)
            triangleCoords.length * 4);
    // use the device hardware's native byte order
    bb.order(ByteOrder.nativeOrder());

    // create a floating point buffer from the ByteBuffer
    vertexBuffer = bb.asFloatBuffer();
    // add the coordinates to the FloatBuffer
    vertexBuffer.put(triangleCoords);
    // set the buffer to read the first coordinate
    vertexBuffer.position(0);

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

    // create empty OpenGL ES Program
    mProgram = GLES20.glCreateProgram();

    // add the vertex shader to program
    GLES20.glAttachShader(mProgram, vertexShader);

    // add the fragment shader to program
    GLES20.glAttachShader(mProgram, fragmentShader);

    // creates OpenGL ES program executables
    GLES20.glLinkProgram(mProgram);
  }
示例#12
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];
  }
示例#13
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);
 }
示例#14
0
  public void GenerateVertexArray(
      float[] arrayBuffer, int bufferSize, int valuesPerElement, int attributeNumber) {
    FloatBuffer.allocate(arrayBuffer.length);
    FloatBuffer bufferData =
        ByteBuffer.allocateDirect(bufferSize).order(ByteOrder.nativeOrder()).asFloatBuffer();
    bufferData.put(arrayBuffer).position(0);

    IntBuffer bufferID = (IntBuffer.allocate(1));
    bufferID.position(0);

    GLES20.glGenBuffers(1, bufferID);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, bufferID.get(0));
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, bufferSize, bufferData, GLES20.GL_STATIC_DRAW);

    GLES20.glVertexAttribPointer(
        attributeNumber, // attribute
        valuesPerElement, // size
        GLES20.GL_FLOAT, // type
        false, // normalized?
        valuesPerElement * FLOAT_SIZE_BYTES, // stride
        0 // array buffer offset
        );

    vertexArrayList.add(new GLVertexArrayObject(bufferID.get(), attributeNumber, valuesPerElement));
  }
示例#15
0
    public axizLines(float size) {
      triangleCoords =
          new float[] {
            0f, 0f, 0f, size, 0f, 0f, 0f, 0f, 0f, 0f, 0, size, 0f, 0f, 0f, 0f, size, 0f,
            //				0f,0f,0f,
            //				size,size,size
          };
      vertexCount = triangleCoords.length / COORDS_PER_VERTEX;
      vertexStride = COORDS_PER_VERTEX * 4;
      // initialize vertex byte buffer for shape coordinates
      ByteBuffer bb =
          ByteBuffer.allocateDirect(
              // (number of coordinate values * 4 bytes per float)
              triangleCoords.length * 4);
      // use the device hardware's native byte order
      bb.order(ByteOrder.nativeOrder());

      // create a floating point buffer from the ByteBuffer
      vertexBuffer = bb.asFloatBuffer();
      // add the coordinates to the FloatBuffer
      vertexBuffer.put(triangleCoords);
      // set the buffer to read the first coordinate
      vertexBuffer.position(0);

      // prepare shaders and OpenGL program
      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); // create OpenGL program executables
    }
示例#16
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;
  }
示例#17
0
    public circle(float r) {
      transMatrix = new float[16];
      int sides = 24;
      vertexCoords = new float[sides * 3];
      double angle = 2 * Math.PI / sides;

      for (int i = 0; i < sides; i++) {
        vertexCoords[3 * i] = (float) (Math.cos(i * angle) * r);
        vertexCoords[3 * i + 1] = (float) (Math.sin(i * angle) * r);
        vertexCoords[3 * i + 2] = 0f;
      }
      vertexCount = vertexCoords.length / COORDS_PER_VERTEX;
      vertexStride = COORDS_PER_VERTEX * 4;
      ByteBuffer bb = ByteBuffer.allocateDirect(vertexCoords.length * 4);
      bb.order(ByteOrder.nativeOrder());
      vertexBuffer = bb.asFloatBuffer();
      vertexBuffer.put(vertexCoords);
      vertexBuffer.position(0);
      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); // create OpenGL program executables
    }
  public void onSurfaceChanged(GL10 unused, int width, int height) {
    GLES20.glViewport(0, 0, width, height);

    ratio = (float) width / height;

    // this projection matrix is applied to object coodinates
    // in the onDrawFrame() method
    Matrix.orthoM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);

    muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    Matrix.setLookAtM(mVMatrix, 0, 0, 0, 3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    engine.initGL();

    GLES20.glUseProgram(mProgram);

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

    GLES20.glUniform1i(mSampleHandle, 0);
    GLES20.glUniform1f(mOpacityHandle, 1f);

    GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 12, quadCoordsBuf);
    GLES20.glEnableVertexAttribArray(maPositionHandle);
    GLES20.glVertexAttribPointer(maTexCoordsHandle, 2, GLES20.GL_FLOAT, false, 8, quadTexCoordsBuf);
    GLES20.glEnableVertexAttribArray(maTexCoordsHandle);

    initTextures();
  }
示例#19
0
文件: Scene.java 项目: Karm/galgs
  private void newVertexBufferToDraw() {
    // (number of points) * (number of coordinate values) * 4 bytes per float)
    ByteBuffer bb = ByteBuffer.allocateDirect(verticesCoords.size() * COORDS_PER_VERTEX * 4);
    // use the device hardware's native byte order
    bb.order(ByteOrder.nativeOrder());
    // create a floating point buffer from the ByteBuffer
    vertexBuffer = bb.asFloatBuffer();
    // add the coordinates to the FloatBuffer
    vertexBuffer.put(Utils.pointVectorToArray(verticesCoords));
    // set the buffer to read the first coordinate
    vertexBuffer.position(0);
    vertexCount = verticesCoords.size();
    // Log.d(GAlg.DEBUG_TAG, "Scene coords to draw: " + verticesCoords.toString());
    GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, 0, vertexCount, vertexBuffer);

    if (drawLines) {
      bb = ByteBuffer.allocateDirect(linesCoords.size() * COORDS_PER_VERTEX * 4);
      bb.order(ByteOrder.nativeOrder());
      linesVertexBuffer = bb.asFloatBuffer();
      linesVertexBuffer.put(Utils.pointVectorToArray(linesCoords));
      linesVertexBuffer.position(0);
      linesVertexCount = linesCoords.size();
      Log.d(GAlg.DEBUG_TAG, "Drawing lines between: " + linesCoords.toString());
      GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, 0, linesVertexCount, linesVertexBuffer);
    }
  }
  /**
   * Test disconnecting the SurfaceTextureHelper, but keep trying to produce more texture frames. No
   * frames should be delivered to the listener.
   */
  @MediumTest
  public static void testDisconnect() throws InterruptedException {
    // Create SurfaceTextureHelper and listener.
    final SurfaceTextureHelper surfaceTextureHelper = SurfaceTextureHelper.create(null);
    final MockTextureListener listener = new MockTextureListener();
    surfaceTextureHelper.setListener(listener);
    // Create EglBase with the SurfaceTexture as target EGLSurface.
    final EglBase eglBase = EglBase.create(null, EglBase.ConfigType.PLAIN);
    eglBase.createSurface(surfaceTextureHelper.getSurfaceTexture());
    eglBase.makeCurrent();
    // Assert no frame has been received yet.
    assertFalse(listener.waitForNewFrame(1));
    // Draw and wait for one frame.
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    // swapBuffers() will ultimately trigger onTextureFrameAvailable().
    eglBase.swapBuffers();
    listener.waitForNewFrame();
    surfaceTextureHelper.returnTextureFrame();

    // Disconnect - we should not receive any textures after this.
    surfaceTextureHelper.disconnect();

    // Draw one frame.
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    eglBase.swapBuffers();
    // swapBuffers() should not trigger onTextureFrameAvailable() because we are disconnected.
    // Assert that no OES texture was delivered.
    assertFalse(listener.waitForNewFrame(500));

    eglBase.release();
  }
  @Override
  public void onDrawFrame(GL10 unused) {

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

    if (mShaderCompilerSupport[0] == false) {
      return;
    }

    GLES20.glDisable(GLES20.GL_DEPTH_TEST);
    GLES20.glEnable(GLES20.GL_BLEND);
    GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);

    float angle = (SystemClock.uptimeMillis() % 5000) / 5000f * 360;
    Matrix.setRotateM(mMatrixModel, 0, angle, 1, 2, 0);

    Matrix.multiplyMM(mMatrixModelViewProjection, 0, mMatrixView, 0, mMatrixModel, 0);
    Matrix.multiplyMM(
        mMatrixModelViewProjection, 0, mMatrixProjection, 0, mMatrixModelViewProjection, 0);

    mShaderSpline.useProgram();

    GLES20.glUniformMatrix4fv(
        mShaderSpline.getHandle("uModelViewProjectionM"), 1, false, mMatrixModelViewProjection, 0);

    GLES20.glVertexAttribPointer(
        mShaderSpline.getHandle("aPosition"), 2, GLES20.GL_FLOAT, false, 0, mBufferSpline);
    GLES20.glEnableVertexAttribArray(mShaderSpline.getHandle("aPosition"));

    for (float[] ctrl : mSplines) {
      GLES20.glUniform3fv(mShaderSpline.getHandle("uCtrl"), 4, ctrl, 0);
      GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, COUNT_VERTICES);
    }
  }
 /** Draws a red box in the corner. */
 private void drawBox() {
   GLES20.glEnable(GLES20.GL_SCISSOR_TEST);
   GLES20.glScissor(0, 0, 100, 100);
   GLES20.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
   GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
   GLES20.glDisable(GLES20.GL_SCISSOR_TEST);
 }
  @Override
  public void onSurfaceChanged(GL10 glUnused, int width, int height) {
    GLES20.glViewport(0, 0, width, height);
    ratio = (float) width / height;

    Matrix.perspectiveM(
        mProjectionMatrix,
        0,
        FOV / (float) Math.PI * 180.0f,
        ratio,
        len / LEN_RANGE,
        len * LEN_RANGE);
    Matrix.setLookAtM(
        mViewMatrix,
        0,
        matCam[0] * len,
        matCam[1] * len,
        matCam[2] * len,
        0.0f,
        0.0f,
        0.0f,
        0.0f,
        0.0f,
        1.0f);

    float[] lightPos = {len, len, 0};
    GLES20.glUniform3fv(mLightPosHandle, 1, lightPos, 0);
  }
示例#24
0
  public Square() {
    // initialize vertex byte buffer for shape coordinates
    ByteBuffer bb =
        ByteBuffer.allocateDirect(
            // (# of coordinate values * 4 bytes per float)
            squareCoords.length * 4);
    bb.order(ByteOrder.nativeOrder());
    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(squareCoords);
    vertexBuffer.position(0);

    // initialize byte buffer for the draw list
    ByteBuffer dlb =
        ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
            drawOrder.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);

    // prepare shaders and OpenGL program
    int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = MyGLRenderer.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); // create OpenGL program executables
  }
  public static 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);

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

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

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

    return shader;
  }
示例#26
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);
  }
 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;
 }
 protected void onInit() {
   super.onInit();
   mToneCurveTextureUniformLocation = GLES20.glGetUniformLocation(mGLProgId, "curve");
   mMaskGrey1UniformLocation = GLES20.glGetUniformLocation(getProgram(), "grey1Frame");
   mMaskGrey2UniformLocation = GLES20.glGetUniformLocation(getProgram(), "grey2Frame");
   mMaskGrey3UniformLocation = GLES20.glGetUniformLocation(getProgram(), "grey3Frame");
 }
示例#29
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);
   }
 }
示例#30
0
  @SuppressLint("NewApi")
  @Override
  public void onSurfaceCreated(GL10 unused, EGLConfig config) {
    Logging.d(TAG, "VideoRendererGui.onSurfaceCreated");
    // Store render EGL context.
    if (CURRENT_SDK_VERSION >= EGL14_SDK_VERSION) {
      synchronized (VideoRendererGui.class) {
        eglContext = EGL14.eglGetCurrentContext();
        Logging.d(TAG, "VideoRendererGui EGL Context: " + eglContext);
      }
    }

    synchronized (yuvImageRenderers) {
      // Create drawer for YUV/OES frames.
      drawer = new GlRectDrawer();
      // Create textures for all images.
      for (YuvImageRenderer yuvImageRenderer : yuvImageRenderers) {
        yuvImageRenderer.createTextures();
      }
      onSurfaceCreatedCalled = true;
    }
    GlUtil.checkNoGLES2Error("onSurfaceCreated done");
    GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
    GLES20.glClearColor(0.15f, 0.15f, 0.15f, 1.0f);

    // Fire EGL context ready event.
    synchronized (VideoRendererGui.class) {
      if (eglContextReady != null) {
        eglContextReady.run();
      }
    }
  }