public void onDrawFrame(GL10 gl) {
    // Redraw background color
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    Log.i("MyGLRenderer", "MyGLRenderer : onDrawFrame() - Starting to set camera position");

    float[] mViewMatrix = new float[16];
    // Set the camera position (View matrix)
    Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    float[] mMVPMatrix = new float[16];
    // Calculate the projection and view transformation
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);

    Log.i("MyGLRenderer", "MyGLRenderer : onDrawFrame() - Drawing ClientSide");

    float[] scratch = new float[16];

    // Create a rotation transformation for the triangle
    long time = SystemClock.uptimeMillis() % 4000L;
    float angle = 0.090f * ((int) time);
    Matrix.setRotateM(mRotationMatrix, 0, angle, 0, 0, -1.0f);

    // Combine the rotation matrix with the projection and camera view
    // Note that the mMVPMatrix factor *must be first* in order
    // for the matrix multiplication product to be correct.
    Matrix.multiplyMM(scratch, 0, mMVPMatrix, 0, mRotationMatrix, 0);

    // Draw triangle
    mTriangle.draw_ClientSide(scratch);
  }
  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();
  }
  @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);
  }
  @Override
  public void onDrawFrame(GL10 unused) {

    // Draw background color
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    // Set the camera position (View matrix)
    Matrix.setLookAtM(mVMatrix, 0, 0, 0, -5, 0f, 0f, 0f, 0f, 5.0f, 0.0f);

    // Calculate the projection and view transformation
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);

    // Draw square
    // mSquare.draw(mMVPMatrix);

    // Create a rotation for the triangle
    //        long time = SystemClock.uptimeMillis() % 4000L;
    //        float angle = 0.090f * ((int) time);
    // Matrix.setRotateM(mRotationMatrix, 0, mAngle, 1, 0, -1.0f);
    Matrix.setRotateM(xRotationMatrix, 0, -mAngle2, 1, 0, 0f);
    Matrix.setRotateM(yRotationMatrix, 0, -mAngle, 0, 1, 0f);
    Matrix.multiplyMM(mRotationMatrix, 0, xRotationMatrix, 0, yRotationMatrix, 0);

    // Combine the rotation matrix with the projection and camera view
    Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0);

    // Draw triangle
    // mTriangle.draw(mMVPMatrix);
    if (mMesh != null) {
      mMesh.draw(mMVPMatrix);
    }
  }
 @Override
 public void onSurfaceChanged(GL10 unused, int width, int height) {
   float aspect = (float) width / height;
   Matrix.perspectiveM(mMatrixProjection, 0, 60f, aspect, .1f, 10f);
   Matrix.setLookAtM(mMatrixView, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0);
   Matrix.multiplyMM(mMatrixModelViewProjection, 0, mMatrixProjection, 0, mMatrixView, 0);
   mLastRenderTime = -1;
 }
 @Override
 public void onSurfaceChanged(GL10 gl, int width, int height) {
   glViewport(0, 0, width, height);
   // perspectiveM(projectionMatrix,0 , 45, (float) width / (float) height,1f, 10f);
   float aspectRatio = (float) width / (float) height;
   frustumM(projectionMatrix, 0, -aspectRatio, aspectRatio, -1, 1, 3, 7);
   setLookAtM(viewMatrix, 0, 0f, 0f, -3f, 0f, 0f, 0f, 0f, 1f, 0f);
 }
  @Override
  public void onDrawFrame(GL10 unused) {
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    float[] parScratch = new float[16];
    float[] bogieScratch = new float[16];
    float[] birdieScratch = new float[16];
    float[] shadowScratch1 = new float[16];
    float[] shadowScratch2 = new float[16];

    // Set the camera position (View matrix)
    Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    // Calculate the projection and view transformation
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);

    float parAngle = -1 * (360 * parPercent);
    float birdieAngle = -1 * (360 * birdiePercent);
    float startingAngle = -90;

    // System.out.println("par angle = " + Float.toString(parAngle));
    // System.out.println("birdie angle = " + Float.toString(birdieAngle));
    // System.out.println("bogie angle = " + Float.toString(-1 * (360 * bogiePercent)));

    // Create a rotation transformation;
    if (angle > -450) {
      // rotates birdie sector to starting angle from angle 0
      if (angle > startingAngle) {
        Matrix.setRotateM(mRotationMatrixBirdie, 0, angle, 0, 0, -1.0f);
      }
      if (angle > startingAngle + birdieAngle) {
        Matrix.setRotateM(mRotationMatrixPar, 0, angle, 0, 0, -1.0f);
      }
      if (angle > startingAngle + birdieAngle + parAngle) {
        Matrix.setRotateM(mRotationMatrixBogie, 0, angle, 0, 0, -1.0f);
      }
      angle -= 2;
    }

    // Combine the rotation matrix with the projection and camera view
    // Note that the mMVPMatrix factor *must be first* in order
    // for the matrix multiplication product to be correct.
    Matrix.multiplyMM(shadowScratch1, 0, mMVPMatrix, 0, mRotationMatrixShadow, 0);
    Matrix.multiplyMM(bogieScratch, 0, mMVPMatrix, 0, mRotationMatrixBogie, 0);
    Matrix.multiplyMM(parScratch, 0, mMVPMatrix, 0, mRotationMatrixPar, 0);
    Matrix.multiplyMM(birdieScratch, 0, mMVPMatrix, 0, mRotationMatrixBirdie, 0);

    shadowPieWheel.draw(mMVPMatrix);
    wheelBG.draw(mMVPMatrix);
    bogiePieWheel.draw(bogieScratch);
    parPieWheel.draw(parScratch);
    birdiePieWheel.draw(birdieScratch);
    wheelHole.draw(mMVPMatrix);
  }
  @Override
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {

    // Set the background clear color to sky blue
    GLES20.glClearColor(135.0f / 255.0f, 206.0f / 255.0f, 255.0f / 255f, 1.0f);
    GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    // GLES20.glEnable(GLES20.GL_CULL_FACE);
    GLES20.glEnable(GLES20.GL_TEXTURE_2D);

    // Position the eye behind the origin
    this.mCamera.eye = new Vector3d(0.0f, 0.0f, 0.0f);

    // We are looking toward the distance
    this.mCamera.look = new Vector3d(0f, 0f, 0f);

    // Set our up vector. This is where our head would be pointing were we holding the camera
    this.mCamera.up = new Vector3d(0.0f, 0.0f, 1.0f);

    // Set the view matrix. This matrix can be said to represent the camera position.
    // NOTE: In OpenGL 1, a ModelView matrix is used, which is a combination of a model and
    // view matrix. In OpenGL 2, we can keep track of these matrices separately if we choose.
    Matrix.setLookAtM(
        this.mViewMatrix,
        0,
        this.mCamera.eye.x,
        this.mCamera.eye.y,
        this.mCamera.eye.z,
        this.mCamera.look.x,
        this.mCamera.look.y,
        this.mCamera.look.z,
        this.mCamera.up.x,
        this.mCamera.up.y,
        this.mCamera.up.z);

    Matrix.setIdentityM(mModelMatrix, 0);
    final String vertexShader = getVertexShader();
    final String fragmentShader = getFragmentShader();

    final int vertexShaderHandle = compileShader(GLES20.GL_VERTEX_SHADER, vertexShader);
    final int fragmentShaderHandle = compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader);

    mPerVertexProgramHandle =
        createAndLinkProgram(
            vertexShaderHandle,
            fragmentShaderHandle,
            new String[] {"a_Position", "a_Color", "a_Normal", "a_TexCoordinate"});

    addTexture(R.drawable.grass_texture_1);
    addTexture(R.drawable.azure_waters);
    addTexture(R.drawable.wood_texture_1);
    addTexture(R.drawable.sky_texture_1);
  }
  protected BaseRenderer(Context context) {
    this.context = context;

    /* *** set default ViewProjection Matrix*** */
    Matrix.setIdentityM(modelMatrix, 0);
    Matrix.translateM(modelMatrix, 0, xP, yP, zP);
    float[] viewMatrix = new float[16];
    float[] projectionMatrix = new float[16];
    MatrixHelper.perspectiveM(projectionMatrix, 45, 1f, 1f, 10f);
    Matrix.setLookAtM(viewMatrix, 0, 0, 0, 3.0f, 0.0f, 0.0f, 0.0f, 0, 1.0f, 0);
    Matrix.multiplyMM(viewProjectionMatrix, 0, projectionMatrix, 0, viewMatrix, 0);
    setModelViewProjectionMatrix();
  }
 // @Override
 public void updateMatrix() {
   Matrix.setLookAtM(
       viewMatrix,
       0,
       position[0],
       position[1],
       position[2],
       position[0],
       position[1],
       position[2],
       0,
       1,
       0);
 }
 private void updateLookAt() {
   Matrix.setLookAtM(
       this.mViewMatrix,
       0,
       this.mCamera.eye.x,
       this.mCamera.eye.y,
       this.mCamera.eye.z,
       this.mCamera.look.x,
       this.mCamera.look.y,
       this.mCamera.look.z,
       this.mCamera.up.x,
       this.mCamera.up.y,
       this.mCamera.up.z);
 }
  @Override
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    // Use culling to remove back faces.
    GLES20.glEnable(GLES20.GL_CULL_FACE);

    // Enable depth testing
    GLES20.glEnable(GLES20.GL_DEPTH_TEST);

    // Position the eye in front of the origin.
    final float eyeX = 0.0f;
    final float eyeY = 8.0f;
    final float eyeZ = 3.5f;

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

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

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

    String vertexShaderSource =
        TextResourceReader.readTextFileFromResource(context, R.raw.simple_vertex_shader);
    String fragmentShaderSource =
        TextResourceReader.readTextFileFromResource(context, R.raw.simple_fragment_shader);

    int vertexShader = ShaderHelper.compileVertexShader(vertexShaderSource);
    int fragmentShader = ShaderHelper.compileFragmentShader(fragmentShaderSource);

    program = ShaderHelper.linkProgram(vertexShader, fragmentShader);

    if (LoggerConfig.ON) {
      ShaderHelper.validateProgram(program);
    }

    glUseProgram(program);

    uMatrixLocation = glGetUniformLocation(program, U_MATRIX);
  }
  private void setUpCamera() {
    final float eyeX = 0.0f;
    final float eyeY = 0.0f;
    final float eyeZ = 7.0f;

    mCameraPosition[0] = eyeX;
    mCameraPosition[1] = eyeY;
    mCameraPosition[2] = eyeZ;

    final float lookX = 0.0f;
    final float lookY = 0.0f;
    final float lookZ = -1.0f;

    final float upX = 0.0f;
    final float upY = 1.0f;
    final float upZ = 0.0f;

    Matrix.setLookAtM(mCameraMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
  }
  @Override
  public void onNewFrame(HeadTransform headTransform) {

    Matrix.rotateM(mModelCube, 0, TIME_DELTA, 0.5f, 0.5f, 1.0f);

    // Build the camera matrix and apply it to the ModelView.
    Matrix.setLookAtM(mCamera, 0, 0.0f, 0.0f, CAMERA_Z, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

    headTransform.getHeadView(headView, 0);

    checkGLError("onReadyToDraw");

    float[] mtx = new float[16];
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
    surface.updateTexImage();
    surface.getTransformMatrix(mtx);
    mHeadTransform = headTransform;
    headTransform.getHeadView(headView, 0);
  }
  public void onDrawFrame(GL10 gl) {
    // TODO Auto-generated method stub
    loopStart = System.currentTimeMillis();
    try {
      if (loopRunTime < ZxCEngine.GAME_THREAD_FPS_SLEEP) {
        Thread.sleep(ZxCEngine.GAME_THREAD_FPS_SLEEP - loopRunTime);
      }
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
    // gl.glLoadIdentity();
    loopEnd = System.currentTimeMillis();
    loopRunTime = ((loopEnd - loopStart));
    // Draw background color

    // GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    // Set the camera position (View matrix)
    Matrix.setLookAtM(mVMatrix, 0, 0, 0, -10, 0f, 0f, 0f, 0f, 10.0f, 10.0f);

    // Calculate the projection and view transformation
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);

    // Draw square
    mSquare.draw(mMVPMatrix);
    // personaje.draw(mMVPMatrix);
    // Create a rotation for the triangle
    //        long time = SystemClock.uptimeMillis() % 4000L;
    //        float angle = 0.090f * ((int) time);
    Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);

    // Combine the rotation matrix with the projection and camera view
    Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0);

    // Draw triangle
    // mTriangle.draw(mMVPMatrix);
  }
  /**
   * Called when a surface is created.
   *
   * @param gl The unused context.
   * @param config Unused configuration options.
   */
  @Override
  public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    GLES20.glClearColor(0.0f, 0f, 0f, 0.0f);
    // GLES20.glEnable(GLES20.GL_CULL_FACE);
    GLES20.glEnable(GLES20.GL_DEPTH_TEST);

    final float eyeX = 0.0f;
    final float eyeY = 0.0f;
    final float eyeZ = 1.5f;
    final float lookX = 0.0f;
    final float lookY = 0.0f;
    final float lookZ = -5.0f;
    // Set our up vector.
    final float upX = 0.0f;
    final float upY = 1.0f;
    final float upZ = 0.0f;

    // Set the view matrix => camera position
    Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);

    // Create shaders (voodoo!):
    createShaders();
  }
 public void camMove(float ay, float az) {
   float[] matCamTemp = new float[16];
   float[] matZ = new float[16];
   Matrix.setRotateM(matZ, 0, az, 0.0f, 0.0f, 1.0f);
   Matrix.multiplyMM(matCamTemp, 0, matZ, 0, matCam, 0);
   matCopy(matCamTemp, matCam);
   Matrix.rotateM(matCam, 0, ay, 0.0f, 1.0f, 0.0f);
   if (Math.abs(Math.atan2(matCam[2], matCam[10])) > ANGLE_Y_LIMIT) {
     matCopy(matCamTemp, matCam);
   }
   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);
 }
  public static void setCamera(
      float cx, // 摄像机位置x
      float cy, // 摄像机位置y
      float cz, // 摄像机位置z
      float tx, // 摄像机目标点x
      float ty, // 摄像机目标点y
      float tz, // 摄像机目标点z
      float upx, // 摄像机UP向量X分量
      float upy, // 摄像机UP向量Y分量
      float upz // 摄像机UP向量Z分量	
      ) {
    Matrix.setLookAtM(mVMatrix, 0, cx, cy, cz, tx, ty, tz, upx, upy, upz);

    cameraLocation[0] = cx;
    cameraLocation[1] = cy;
    cameraLocation[2] = cz;

    llbb.clear();
    llbb.order(ByteOrder.nativeOrder()); // 设置字节顺序
    cameraFB = llbb.asFloatBuffer();
    cameraFB.put(cameraLocation);
    cameraFB.position(0);
  }
Exemple #19
0
  public void onSurfaceCreated(GL10 unused, EGLConfig config) {
    mSurfaceCreated = false;
    GLES20.glEnable(GLES20.GL_BLEND);
    //        GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
    GLES20.glBlendFuncSeparate(
        GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA, GLES20.GL_ONE, GLES20.GL_ONE);
    GLES20.glClearColor(0, 0, 0, 0);

    // Set the camera position (View matrix)
    Matrix.setLookAtM(mVMatrix, 0, 0, 0, 1, 0, 0, -1, 0, 1, 0);

    GLColorOverlay.initGl();
    GLPicture.initGl();

    mColorOverlay = new GLColorOverlay(0);

    mSurfaceCreated = true;
    if (mQueuedNextBitmapRegionLoader != null) {
      BitmapRegionLoader loader = mQueuedNextBitmapRegionLoader;
      mQueuedNextBitmapRegionLoader = null;
      setAndConsumeBitmapRegionLoader(loader);
    }
  }
  @Override
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
    Log.d(TAG, "onSurfaceCreated");
    GLUtils.init();
    GLUtils.printSysConfig();

    // set the background color
    GLES20.glClearColor(0.0f, 1.0f, 0.0f, 1.0f);

    // set up the "camera"
    Matrix.setLookAtM(
        mViewMatrix,
        0,
        0.0f,
        0.0f,
        1.0f, // eye x,y,z
        0.0f,
        0.0f,
        0.0f, // look x,y,z
        0.0f,
        1.0f,
        0.0f); // up x,y,z

    mExternalSurfaceTexture = new ExternalSurfaceTexture();
    mReadExternalTextureShaderProgram = new ReadExternalTextureShaderProgram();

    mDefaultShaderProgram = new TextureShaderProgram();

    mTextureToScreenShaderProgram = new TextureShaderProgram();

    if (mOnExternalSurfaceTextureCreatedListener != null) {
      mOnExternalSurfaceTextureCreatedListener.onExternalSurfaceTextureCreated(
          mExternalSurfaceTexture);
    }

    mFrameRateCalculator = new FrameRateCalculator(30);
  }
Exemple #21
0
  @Override
  public void onSurfaceCreated(GL10 unused, EGLConfig config) {
    ShaderManager.getSingletonObject().unloadAll();
    ShaderManager.getSingletonObject().cleanUp();

    TextureManager.getSingletonObject().unloadAll();
    TextureManager.getSingletonObject().cleanUp();

    loadResources();

    if (phenixLineProgram != null) {
      phenixLineProgram.load();
    }

    ShaderManager.getSingletonObject().unloadAllShaders();

    renderTextures = new RenderTexture[2];
    frameBuffers = new FrameBuffer[2];

    setupFrameBuffer(unused);

    Matrix.setLookAtM(V_matrix, 0, 0, 0, 1.0f, 0f, 0f, 0f, 0f, -1.0f, 0.0f);
    Matrix.orthoM(Q_matrix, 0, 0, 1, 0, 1, -1, 1);
  }
  @Override
  public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    super.onSurfaceCreated(gl, config);
    // Set the background clear color to black.
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    // Use culling to remove back faces.
    GLES20.glEnable(GLES20.GL_CULL_FACE);

    // Enable depth testing
    GLES20.glEnable(GLES20.GL_DEPTH_TEST);

    // The below glEnable() call is a holdover from OpenGL ES 1, and is not needed in OpenGL ES 2.
    // Enable texture mapping
    // GLES20.glEnable(GLES20.GL_TEXTURE_2D);

    // Position the eye in front of the origin.
    final float eyeX = 0.0f;
    final float eyeY = 0.0f;
    final float eyeZ = -0.5f;

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

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

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

    final String vertexShader = getVertexShader();
    final String fragmentShader = getFragmentShader();

    final int vertexShaderHandle =
        ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, vertexShader);
    final int fragmentShaderHandle =
        ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader);

    mProgramHandle =
        ShaderHelper.createAndLinkProgram(
            vertexShaderHandle,
            fragmentShaderHandle,
            new String[] {"a_Position", "a_Color", "a_Normal", "a_TexCoordinate"});

    // Define a simple shader program for our point.
    final String pointVertexShader =
        RawResourceReader.readTextFileFromRawResource(mContext, R.raw.point_vertex_shader);
    final String pointFragmentShader =
        RawResourceReader.readTextFileFromRawResource(mContext, R.raw.point_fragment_shader);

    final int pointVertexShaderHandle =
        ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, pointVertexShader);
    final int pointFragmentShaderHandle =
        ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, pointFragmentShader);
    mPointProgramHandle =
        ShaderHelper.createAndLinkProgram(
            pointVertexShaderHandle, pointFragmentShaderHandle, new String[] {"a_Position"});
  }
  private void drawCar(int program, float[] startPos) {
    Matrix.setIdentityM(mMMatrix, 0);
    Matrix.setIdentityM(mTransMatrix, 0);

    // Képernyõ Y tengely
    if (Math.abs(mDY) < 2) mDY = 0;

    if (Math.abs(mDY) <= 250) {
      accelY = -1 * mDY * 2;
    } else {
      if (mDY < 0) mDY = -250f;
      if (mDY > 0) mDY = 250f;
    }
    distanceY += accelY;
    Log.d("mDY:", String.valueOf(mDY));

    // Képernyõ X tengely
    if (Math.abs(mDX) < 2) mDX = 0;
    float[] forward = {(float) Math.cos(mDX / 50), 0.0f, (float) Math.sin(mDX / 50)};

    // lookAt
    lookAt[0] = eyePos[0] + 25 * forward[0];
    lookAt[1] = eyePos[1] + 25 * forward[1];
    lookAt[2] = eyePos[2] + 25 * forward[2];

    // eyePos
    eyePos[0] = 0.0f + distanceY / 1000 * forward[0];
    eyePos[2] = 25.0f + distanceY / 1000 * forward[2];

    Matrix.setLookAtM(
        mVMatrix, 0, eyePos[0], eyePos[1], eyePos[2], lookAt[0], lookAt[1], lookAt[2], 0.0f, 1.0f,
        0.0f);

    // Kormányzás
    startPos[0] = eyePos[0] + 5 * forward[0];
    startPos[2] = eyePos[2] + 5 * forward[2];

    Matrix.translateM(mTransMatrix, 0, startPos[0], startPos[1], startPos[2]);

    Matrix.multiplyMM(mMMatrix, 0, mMMatrix, 0, mTransMatrix, 0); // Translate
    Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0); // View
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0); // Proj

    // send to the shader
    GLES20.glUniformMatrix4fv(
        GLES20.glGetUniformLocation(program, "uMVPMatrix"), 1, false, mMVPMatrix, 0);

    // Create the normal modelview matrix
    // Invert + transpose of mvpmatrix
    Matrix.invertM(normalMatrix, 0, mMVPMatrix, 0);
    Matrix.transposeM(normalMatrix, 0, normalMatrix, 0);

    // send to the shader
    GLES20.glUniformMatrix4fv(
        GLES20.glGetUniformLocation(program, "normalMatrix"), 1, false, mMVPMatrix, 0);

    /** * DRAWING OBJECT * */
    // Get buffers from mesh
    Object3D ob = this._objects[this.CUBE];
    Mesh mesh = ob.getMesh();
    FloatBuffer _vb = mesh.get_vb();
    ShortBuffer _ib = mesh.get_ib();

    short[] _indices = mesh.get_indices();

    // Vertex buffer

    // the vertex coordinates
    _vb.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
    GLES20.glVertexAttribPointer(
        GLES20.glGetAttribLocation(program, "aPosition"),
        3,
        GLES20.GL_FLOAT,
        false,
        TRIANGLE_VERTICES_DATA_STRIDE_BYTES,
        _vb);
    GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(program, "aPosition"));

    // the normal info
    _vb.position(TRIANGLE_VERTICES_DATA_NOR_OFFSET);
    GLES20.glVertexAttribPointer(
        GLES20.glGetAttribLocation(program, "aNormal"),
        3,
        GLES20.GL_FLOAT,
        false,
        TRIANGLE_VERTICES_DATA_STRIDE_BYTES,
        _vb);
    GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(program, "aNormal"));

    // Texture info

    // bind textures
    if (ob.hasTexture()) { // && enableTexture) {
      // number of textures
      int[] texIDs = ob.get_texID();

      for (int i = 0; i < _texIDs.length; i++) {
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i);
        // Log.d("TEXTURE BIND: ", i + " " + texIDs[i]);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIDs[i]);
        GLES20.glUniform1i(GLES20.glGetUniformLocation(program, "texture" + (i + 1)), i);
      }
    }

    // enable texturing? [fix - sending float is waste]
    GLES20.glUniform1f(
        GLES20.glGetUniformLocation(program, "hasTexture") /*shader.hasTextureHandle*/,
        ob.hasTexture() && enableTexture ? 2.0f : 0.0f);

    // texture coordinates
    _vb.position(TRIANGLE_VERTICES_DATA_TEX_OFFSET);
    GLES20.glVertexAttribPointer(
        GLES20.glGetAttribLocation(program, "textureCoord") /*shader.maTextureHandle*/,
        2,
        GLES20.GL_FLOAT,
        false,
        TRIANGLE_VERTICES_DATA_STRIDE_BYTES,
        _vb);
    GLES20.glEnableVertexAttribArray(
        GLES20.glGetAttribLocation(
            program, "textureCoord")); // GLES20.glEnableVertexAttribArray(shader.maTextureHandle);

    // Draw with indices
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, _indices.length, GLES20.GL_UNSIGNED_SHORT, _ib);
    checkGlError("glDrawElements");

    /** END DRAWING OBJECT ** */
  }
 @Override
 public void onSurfaceChanged(GL10 unused, int width, int height) {
   float aspect = (float) width / height;
   Matrix.perspectiveM(mMatrixProjection, 0, 60f, aspect, .1f, 10f);
   Matrix.setLookAtM(mMatrixView, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0);
 }
Exemple #25
0
 public static void updateCameraPos(float[] viewMatrix) {
   if (focus == null) return;
   VPoint focusPoint = focus.getAnchor();
   setPos((float) focusPoint.position.x, (float) focusPoint.position.y, 0.0f);
   Matrix.setLookAtM(viewMatrix, 0, pos[0], pos[1], -3, pos[0], pos[1], 0f, 0f, 1.0f, 0.0f);
 }
  /* (non-Javadoc)
   * @see fr.kesk.gl.shader.GlShader#render(fr.kesk.gl.GlAssets.Node)
   */
  @Override
  public void render(Node nodeInstance) {
    // android.util.Log.d(TAG,"render("+nodeInstance.id+")");
    if (this.lightType == Light.DIRECTIONAL) {
      this.lightModel[4] = this.lightModel[5] = this.lightModel[6] = 0f;
      this.lightModel[7] = 1f;
      MatrixUtils.multiplyMV(this.lightModel, 4, nodeInstance.model, 0, this.lightModel, 4);
      this.lightModel[0] = this.lightModel[4] - this.lightModel[8];
      this.lightModel[1] = this.lightModel[5] - this.lightModel[9];
      this.lightModel[2] = this.lightModel[6] - this.lightModel[10];
      this.lightModel[3] = 1f;
      Matrix.setLookAtM(
          this.lightMvpCache,
          0,
          this.lightModel[0],
          this.lightModel[1],
          this.lightModel[2],
          this.lightModel[4],
          this.lightModel[5],
          this.lightModel[6],
          0f,
          1f,
          0f);
      MatrixUtils.multiplyMM(this.lightMvpCache, 32, this.lightMvpCache, 16, this.lightMvpCache, 0);
      MatrixUtils.multiplyMM(
          this.currentShadowMap.shadowMatrix,
          0,
          GlFastShadowMapShader.BIAS_MATRIX,
          0,
          this.lightMvpCache,
          32);
    } else if (this.lightType == Light.POINT) {
      this.lightModel[4] = this.lightModel[5] = this.lightModel[6] = 0f;
      this.lightModel[7] = 1f;
      MatrixUtils.multiplyMV(this.lightModel, 4, nodeInstance.model, 0, this.lightModel, 4);
      Matrix.setLookAtM(
          this.lightMvpCache,
          0,
          this.lightModel[0],
          this.lightModel[1],
          this.lightModel[2],
          this.lightModel[4],
          this.lightModel[5],
          this.lightModel[6],
          0f,
          1f,
          0f);
      MatrixUtils.multiplyMM(this.lightMvpCache, 32, this.lightMvpCache, 16, this.lightMvpCache, 0);
      MatrixUtils.multiplyMM(
          this.currentShadowMap.shadowMatrix,
          0,
          GlFastShadowMapShader.BIAS_MATRIX,
          0,
          this.lightMvpCache,
          32);
    }

    // Render the scene in the FBO
    this.fbo.bind();
    this.program.start();
    this.cullfaceMode = GlContext.glGetState(GLES20.GL_CULL_FACE_MODE);
    this.viewPort = GlContext.glGetState(GLES20.GL_VIEWPORT);
    GLES20.glCullFace(GLES20.GL_FRONT);
    GLES20.glViewport(0, 0, this.quality, this.quality);
    GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
    GLES20.glEnableVertexAttribArray(this.a_PositionVec4Handle);
    this.drawNode(nodeInstance);
    GLES20.glDisableVertexAttribArray(this.a_PositionVec4Handle);
    this.fbo.unbind();
    GLES20.glCullFace((int) this.cullfaceMode[0]);
    GLES20.glViewport(
        (int) this.viewPort[0],
        (int) this.viewPort[1],
        (int) this.viewPort[2],
        (int) this.viewPort[3]);
  }
Exemple #27
0
 public CameraGL() {
   viewMatrix = new float[16];
   projectionMatrix = new float[16];
   Matrix.setLookAtM(viewMatrix, 0, 0f, 0f, -100.0f, 0f, 0f, 1f, 0f, 1.0f, 0.0f);
 }
  @Override
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    // Tell OpenGL to use this program when rendering.
    GLES20.glUseProgram(programHandle);
  }
  /** Initialization function */
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
    // initialize shaders
    try {
      _shaders[GOURAUD_SHADER] =
          new Shader(
              vShaders[GOURAUD_SHADER], fShaders[GOURAUD_SHADER], mContext, false, 0); // gouraud
      _shaders[PHONG_SHADER] =
          new Shader(vShaders[PHONG_SHADER], fShaders[PHONG_SHADER], mContext, false, 0); // phong
      _shaders[NORMALMAP_SHADER] =
          new Shader(
              vShaders[NORMALMAP_SHADER],
              fShaders[NORMALMAP_SHADER],
              mContext,
              false,
              0); // normal map
    } catch (Exception e) {
      Log.d("SHADER 0 SETUP", e.getLocalizedMessage());
    }

    GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    GLES20.glClearDepthf(1.0f);
    GLES20.glDepthFunc(GLES20.GL_LEQUAL);
    GLES20.glDepthMask(true);

    // cull backface
    GLES20.glEnable(GLES20.GL_CULL_FACE);
    GLES20.glCullFace(GLES20.GL_BACK);

    // light variables
    float[] lightP = {30.0f, 0.0f, 10.0f, 1};
    this.lightPos = lightP;

    float[] lightC = {0.5f, 0.5f, 0.5f};
    this.lightColor = lightC;

    // material properties
    float[] mA = {0.45f, 0.45f, 0.45f, 1.0f};
    matAmbient = mA;

    float[] mD = {0.6f, 0.6f, 0.6f, 1.0f};
    matDiffuse = mD;

    float[] mS = {0.9f, 0.9f, 0.9f, 1.0f};
    matSpecular = mS;

    matShininess = 5.0f;

    // setup textures for all objects
    for (int i = 0; i < _objects.length; i++) setupTextures(_objects[i]);

    // set the view matrix

    Matrix.setLookAtM(
        mVMatrix, 0, eyePos[0], eyePos[1], eyePos[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
    /*Matrix.setLookAtM(
    rm,
    rmOffset,
    eyeX, eyeY, eyeZ,
    centerX, centerY, centerZ,
    upX, upY, upZ)*/
  }
  public void camZoom(float exp) {
    len = LEN_DEFAULT * (float) Math.pow(LEN_RANGE, exp);

    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);
  }