Ejemplo n.º 1
0
  public void drawNinePatch(NinePatchTexture tex, int x, int y, int width, int height) {

    NinePatchChunk chunk = tex.getNinePatchChunk();

    // The code should be easily extended to handle the general cases by
    // allocating more space for buffers. But let's just handle the only
    // use case.
    if (chunk.mDivX.length != 2 || chunk.mDivY.length != 2) {
      throw new RuntimeException("unsupported nine patch");
    }
    if (!tex.bind(this, mGL)) {
      throw new RuntimeException("cannot bind" + tex.toString());
    }
    if (width <= 0 || height <= 0) return;

    int divX[] = mNinePatchX;
    int divY[] = mNinePatchY;
    float divU[] = mNinePatchU;
    float divV[] = mNinePatchV;

    int nx = stretch(divX, divU, chunk.mDivX, tex.getWidth(), width);
    int ny = stretch(divY, divV, chunk.mDivY, tex.getHeight(), height);

    setAlphaValue(mTransformation.getAlpha());
    Matrix matrix = mTransformation.getMatrix();
    matrix.getValues(mMatrixValues);
    GL11 gl = mGL;
    gl.glPushMatrix();
    gl.glMultMatrixf(toGLMatrix(mMatrixValues), 0);
    gl.glTranslatef(x, y, 0);
    drawMesh(divX, divY, divU, divV, nx, ny);
    gl.glPopMatrix();
  }
  @Override
  protected void onDraw(Canvas canvas) {
    GL11 gl = (GL11) context.getGL();
    int w = getWidth();
    int h = getHeight();

    context.waitNative(canvas, this);

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glViewport(0, 0, w, h);
    GLU.gluPerspective(gl, 45.0f, ((float) w) / h, 1f, 100f);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    GLU.gluLookAt(gl, 0, 0, 5, 0, 0, 0, 0, 1, 0);
    gl.glTranslatef(0, 0, -10);
    gl.glRotatef(30.0f, 1, 0, 0);
    // gl.glRotatef(40.0f, 0, 1, 0);

    gl.glVertexPointer(3, GL10.GL_FIXED, 0, vertices[frame_ix]);
    gl.glNormalPointer(GL10.GL_FIXED, 0, normals);
    gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texCoords);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    // gl.glColor4f(1,0,0,1);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, tex);
    // gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
    gl.glDrawElements(GL10.GL_TRIANGLES, verts, GL10.GL_UNSIGNED_SHORT, indices);

    frame_ix = (frame_ix + 1) % m.getFrameCount();
    context.waitGL();
  }
    @Override
    public void onDrawFrame(GL10 gl) {
      gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      GL11 gl11 = (GL11) gl;

      gl.glMatrixMode(GL10.GL_PROJECTION);
      gl.glLoadIdentity();

      // perspective
      GLU.gluPerspective(gl11, 45, aspect, 0.01f, 100f);
      GLU.gluLookAt(
          gl,
          (float) (Math.cos(angle) * eyeDistance),
          0,
          (float) (Math.sin(angle) * eyeDistance),
          0,
          0,
          0.0f,
          0.0f,
          1.0f,
          0.0f);

      gl.glMatrixMode(GL10.GL_MODELVIEW);
      gl.glLoadIdentity();

      // set render status
      gl.glEnable(GL10.GL_TEXTURE_2D);
      gl.glEnable(GL10.GL_ALPHA_TEST);
      gl.glAlphaFunc(GL10.GL_EQUAL, 1.0f);
      // gl.glColor4f(1, 1, 1, 1);

      // bind
      gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
      vbo.bind(gl11);

      // left
      gl11.glPushMatrix();
      {
        gl11.glTranslatef(-1, 0, 0);
        ibo.draw(gl11);
      }
      gl11.glPopMatrix();

      // right
      gl11.glPushMatrix();
      {
        gl11.glTranslatef(1, 0, 0);
        ibo.draw(gl11);
      }
      gl11.glPopMatrix();

      // up
      gl11.glPushMatrix();
      {
        gl11.glTranslatef(0, -1, 0);
        ibo.draw(gl11);
      }
      gl11.glPopMatrix();

      // down
      gl11.glPushMatrix();
      {
        gl11.glTranslatef(0, 1, 0);
        ibo.draw(gl11);
      }
      gl11.glPopMatrix();

      // unbind
      vbo.unbind(gl11);
      gl.glDisable(GL10.GL_TEXTURE_2D);

      angle += (float) (1f / 180f * Math.PI);
    }