Android Open Source - agldemo Lesson06






From Project

Back to project page agldemo.

License

The source code is released under:

Copyright (c) 2013 Wenhao Li Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software...

If you think the Android project agldemo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Copyright (C) 2010 Wenhao Li. All Rights Reserved.
 * //from w  ww  .j  a  v  a2 s  .c  om
 */
package com.agldemo.lessons;

import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import com.agldemo.Lesson;
import com.agldemo.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLU;

/**
 * Lesson 06
 */
public class Lesson06 extends Lesson {

  FloatBuffer quad = makeFloatBuffer(new float[] {
      // FRONT
      -1.0f, -1.0f, 1.0f,
      //
      1.0f, -1.0f, 1.0f,
      //
      1.0f, 1.0f, 1.0f,
      //
      -1.0f, 1.0f, 1.0f,

      // BACK
      -1.0f, -1.0f, -1.0f,
      //
      1.0f, -1.0f, -1.0f,
      //
      1.0f, 1.0f, -1.0f,
      //
      -1.0f, 1.0f, -1.0f,

      // LEFT
      -1.0f, -1.0f, -1.0f,
      //
      -1.0f, 1.0f, -1.0f,
      //
      -1.0f, 1.0f, 1.0f,
      //
      -1.0f, -1.0f, 1.0f,

      // RIGHT
      1.0f, -1.0f, -1.0f,
      //
      1.0f, 1.0f, -1.0f,
      //
      1.0f, 1.0f, 1.0f,
      //
      1.0f, -1.0f, 1.0f,

      // TOP
      -1.0f, 1.0f, -1.0f,
      //
      1.0f, 1.0f, -1.0f,
      //
      1.0f, 1.0f, 1.0f,
      //
      -1.0f, 1.0f, 1.0f,

      // BOTTOM
      -1.0f, -1.0f, -1.0f,
      //
      1.0f, -1.0f, -1.0f,
      //
      1.0f, -1.0f, 1.0f,
      //
      -1.0f, -1.0f, 1.0f, });

  FloatBuffer qtex = makeFloatBuffer(new float[] {
      // FRONT
      0.0f, 1.0f,
      //
      1.0f, 1.0f,
      //
      1.0f, 0.0f,
      //
      0.0f, 0.0f,

      // BACK
      0.0f, 1.0f,
      //
      1.0f, 1.0f,
      //
      1.0f, 0.0f,
      //
      0.0f, 0.0f,

      // LEFT
      0.0f, 1.0f,
      //
      1.0f, 1.0f,
      //
      1.0f, 0.0f,
      //
      0.0f, 0.0f,

      // RIGHT
      0.0f, 1.0f,
      //
      1.0f, 1.0f,
      //
      1.0f, 0.0f,
      //
      0.0f, 0.0f,

      // TOP
      0.0f, 0.0f,
      //
      1.0f, 0.0f,
      //
      1.0f, 1.0f,
      //
      0.0f, 1.0f,

      // BOTTOM
      0.0f, 0.0f,
      //
      1.0f, 0.0f,
      //
      1.0f, 1.0f,
      //
      0.0f, 1.0f, });

  float xrot = 0.0f;

  float yrot = 0.0f;

  float zrot = 0.0f;

  private Bitmap mBitmap;

  int texture = -1;

  public Lesson06(Context c) {
    super(c, "Lesson 06", "Texture Mapping");
    mBitmap = BitmapFactory.decodeResource(c.getResources(), R.drawable.robot);
  }

  public void onDrawFrame(GL10 gl, int width, int height) {
    float ratio = (float) width / height;
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    GLU.gluPerspective(gl, 45.0f, ratio, 0.1f, 100.0f);

    // texture
    if (texture == -1) {
      texture = loadTexture(gl, mBitmap);
    }
    gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
    gl.glEnable(GL10.GL_TEXTURE_2D);

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

    gl.glTranslatef(0.0f, 0.0f, -5.0f);

    gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
    gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
    gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f);

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, quad);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, qtex);

    gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4);
    gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 4, 4);
    gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 8, 4);
    gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 12, 4);
    gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 16, 4);
    gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 20, 4);

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    // clean up
    gl.glDisable(GL10.GL_TEXTURE_2D);

    xrot += 0.6f;
    yrot += 0.4f;
    zrot += 0.8f;
  }

}




Java Source Code List

com.agldemo.BaseRenderer.java
com.agldemo.GLDemoActivity.java
com.agldemo.GLViewStage.java
com.agldemo.GLView.java
com.agldemo.Lesson.java
com.agldemo.lessons.Lesson01.java
com.agldemo.lessons.Lesson02.java
com.agldemo.lessons.Lesson03.java
com.agldemo.lessons.Lesson04.java
com.agldemo.lessons.Lesson05.java
com.agldemo.lessons.Lesson06.java
com.agldemo.lessons.Lesson07.java
com.agldemo.lessons.Lesson08.java
com.agldemo.lessons.Lesson09.java