最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實現(xiàn)OpenGL ES紋理映射的方法

 更新時間:2015年06月16日 16:19:44   作者:紅薯  
這篇文章主要介紹了java實現(xiàn)OpenGL ES紋理映射的方法,以實例形式較為詳細(xì)的分析了紋理映射的實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了java實現(xiàn)OpenGL ES紋理映射的方法。分享給大家供大家參考。具體如下:

1. GlRenderer.java文件:

package net.obviam.opengl;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
public class GlRenderer implements Renderer {
  private Square   square;   // the square
  private Context   context;
  /** Constructor to set the handed over context */
  public GlRenderer(Context context) {
    this.context = context;
    // initialise the square
    this.square = new Square();
  }
  @Override
  public void onDrawFrame(GL10 gl) {
    // clear Screen and Depth Buffer
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    // Reset the Modelview Matrix
    gl.glLoadIdentity();
    // Drawing
    gl.glTranslatef(0.0f, 0.0f, -5.0f);
    // move 5 units INTO the screen
    // is the same as moving the camera 5 units away
//   gl.glScalef(0.5f, 0.5f, 0.5f);
// scale the square to 50% 
// otherwise it will be too large
    square.draw(gl); // Draw the triangle
  }
  @Override
  public void onSurfaceChanged(GL10 gl, int width, int height) {
    if(height == 0) { //Prevent A Divide By Zero By
      height = 1; //Making Height Equal One
    }
    gl.glViewport(0, 0, width, height); //Reset The Current Viewport
    gl.glMatrixMode(GL10.GL_PROJECTION); //Select The Projection Matrix
    gl.glLoadIdentity(); //Reset The Projection Matrix
    //Calculate The Aspect Ratio Of The Window
    GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    //Select The Modelview Matrix
    gl.glLoadIdentity(); //Reset The Modelview Matrix
  }
  @Override
  public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    // Load the texture for the square
    square.loadGLTexture(gl, this.context);
    gl.glEnable(GL10.GL_TEXTURE_2D); //Enable Texture Mapping ( NEW )
    gl.glShadeModel(GL10.GL_SMOOTH); //Enable Smooth Shading
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); //Black Background
    gl.glClearDepthf(1.0f); //Depth Buffer Setup
    gl.glEnable(GL10.GL_DEPTH_TEST); //Enables Depth Testing
    gl.glDepthFunc(GL10.GL_LEQUAL); //The Type Of Depth Testing To Do
    //Really Nice Perspective Calculations
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); 
  }
}

2. Square.java文件:

package net.obviam.opengl;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
public class Square {
  private FloatBuffer vertexBuffer;  // buffer holding the vertices
  private float vertices[] = {
      -1.0f, -1.0f, 0.0f,    // V1 - bottom left
      -1.0f, 1.0f, 0.0f,    // V2 - top left
       1.0f, -1.0f, 0.0f,    // V3 - bottom right
       1.0f, 1.0f, 0.0f     // V4 - top right
  };
  private FloatBuffer textureBuffer; // buffer holding the texture coordinates
  private float texture[] = {     
      // Mapping coordinates for the vertices
      0.0f, 1.0f,   // top left   (V2)
      0.0f, 0.0f,   // bottom left (V1)
      1.0f, 1.0f,   // top right  (V4)
      1.0f, 0.0f   // bottom right (V3)
  };
  /** The texture pointer */
  private int[] textures = new int[1];
  public Square() {
    // a float has 4 bytes so we allocate for each coordinate 4 bytes
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    // allocates the memory from the byte buffer
    vertexBuffer = byteBuffer.asFloatBuffer();
    // fill the vertexBuffer with the vertices
    vertexBuffer.put(vertices);
    // set the cursor position to the beginning of the buffer
    vertexBuffer.position(0);
    byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    textureBuffer = byteBuffer.asFloatBuffer();
    textureBuffer.put(texture);
    textureBuffer.position(0);
  }
  /**
   * Load the texture for the square
   * @param gl
   * @param context
   */
  public void loadGLTexture(GL10 gl, Context context) {
    // loading texture
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.android);
    // generate one texture pointer
    gl.glGenTextures(1, textures, 0);
    // ...and bind it to our array
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
    // create nearest filtered texture
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    //Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
//   gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
//   gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
    // Use Android GLUtils to specify a two-dimensional texture image from our bitmap 
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
    // Clean up
    bitmap.recycle();
  }
  /** The draw method for the square with the GL context */
  public void draw(GL10 gl) {
    // bind the previously generated texture
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
    // Point to our buffers
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    // Set the face rotation
    gl.glFrontFace(GL10.GL_CW);
    // Point to our vertex buffer
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
    // Draw the vertices as triangle strip
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
    //Disable the client state before leaving
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
  }
}

3. Triangle.java文件:

package net.obviam.opengl;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
public class Triangle {
  private FloatBuffer vertexBuffer;  // buffer holding the vertices
  private float vertices[] = {
      -0.5f, -0.5f, 0.0f,    // V1 - first vertex (x,y,z)
       0.5f, -0.5f, 0.0f,    // V2 - second vertex
       0.0f, 0.5f, 0.0f     // V3 - third vertex
//      1.0f, 0.5f, 0.0f     // V3 - third vertex
  };
  public Triangle() {
    // a float has 4 bytes so we allocate for each coordinate 4 bytes
    ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
    vertexByteBuffer.order(ByteOrder.nativeOrder());
    // allocates the memory from the byte buffer
    vertexBuffer = vertexByteBuffer.asFloatBuffer();
    // fill the vertexBuffer with the vertices
    vertexBuffer.put(vertices);
    // set the cursor position to the beginning of the buffer
    vertexBuffer.position(0);
  }
  /** The draw method for the triangle with the GL context */
  public void draw(GL10 gl) {
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    // set the colour for the background
//   gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    // to show the color (paint the screen) we need to clear the color buffer
//   gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    // set the colour for the triangle
    gl.glColor4f(0.0f, 1.0f, 0.0f, 0.5f);
    // Point to our vertex buffer
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    // Draw the vertices as triangle strip
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
    //Disable the client state before leaving
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
  }
}

4. Run.java文件:

package net.obviam.opengl;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class Run extends Activity {
  /** The OpenGL view */
  private GLSurfaceView glSurfaceView;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // requesting to turn the title OFF
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    // making it full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    // Initiate the Open GL view and
    // create an instance with this activity
    glSurfaceView = new GLSurfaceView(this);
    // set our renderer to be the main renderer with
    // the current activity context
    glSurfaceView.setRenderer(new GlRenderer(this));
    setContentView(glSurfaceView);
  }
  /**
   * Remember to resume the glSurface
   */
  @Override
  protected void onResume() {
    super.onResume();
    glSurfaceView.onResume();
  }
  /**
   * Also pause the glSurface
   */
  @Override
  protected void onPause() {
    super.onPause();
    glSurfaceView.onPause();
  }
}

希望本文所述對大家的java程序設(shè)計有所幫助。

相關(guān)文章

  • 使用Maven創(chuàng)建和管理多模塊項目的詳細(xì)步驟

    使用Maven創(chuàng)建和管理多模塊項目的詳細(xì)步驟

    使用Maven進(jìn)行多模塊項目管理是一種常見的做法,它可以幫助你組織大型項目,使其結(jié)構(gòu)更加清晰,便于維護(hù)和構(gòu)建,以下是使用Maven創(chuàng)建和管理多模塊項目的詳細(xì)步驟,需要的朋友可以參考下
    2024-10-10
  • Spring boot進(jìn)行參數(shù)校驗的方法實例詳解

    Spring boot進(jìn)行參數(shù)校驗的方法實例詳解

    這篇文章主要介紹了Spring boot進(jìn)行參數(shù)校驗的方法實例詳解,非 常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2018-05-05
  • 劍指Offer之Java算法習(xí)題精講鏈表專項訓(xùn)練

    劍指Offer之Java算法習(xí)題精講鏈表專項訓(xùn)練

    跟著思路走,之后從簡單題入手,反復(fù)去看,做過之后可能會忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會發(fā)現(xiàn)質(zhì)的變化
    2022-03-03
  • java實現(xiàn)簡單網(wǎng)絡(luò)象棋游戲

    java實現(xiàn)簡單網(wǎng)絡(luò)象棋游戲

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)簡單網(wǎng)絡(luò)象棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • IDEA設(shè)置JVM運(yùn)行參數(shù)的方法步驟

    IDEA設(shè)置JVM運(yùn)行參數(shù)的方法步驟

    這篇文章主要介紹了IDEA設(shè)置JVM運(yùn)行參數(shù)的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Spring中的事務(wù)隔離級別的介紹

    Spring中的事務(wù)隔離級別的介紹

    今天小編就為大家分享一篇關(guān)于Spring中的事務(wù)隔離級別的介紹,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Spring Boot啟動流程分析

    Spring Boot啟動流程分析

    本文給大家介紹spring boot是怎樣啟動和啟動做了哪些事情。具體內(nèi)容詳情大家通過本文詳細(xì)學(xué)習(xí)吧
    2017-09-09
  • Java字符編碼簡介_動力節(jié)點(diǎn)Java學(xué)院整理

    Java字符編碼簡介_動力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要介紹了Java字符編碼簡介,本文主要包括以下幾個方面:編碼基本知識,Java,系統(tǒng)軟件,url,工具軟件等,感興趣的朋友一起看看吧
    2017-08-08
  • java跳出多重循環(huán)的三種實現(xiàn)方式

    java跳出多重循環(huán)的三種實現(xiàn)方式

    文章主要介紹了Java中跳出多重循環(huán)的三種方式:使用`break`配合標(biāo)簽、在布爾表達(dá)式中添加判斷變量、以及使用`try-catch`制造異常,每種方式都有具體的代碼示例,并輸出了相應(yīng)的執(zhí)行結(jié)果
    2025-01-01
  • spring boot自定義404錯誤信息的方法示例

    spring boot自定義404錯誤信息的方法示例

    這篇文章主要介紹了spring boot自定義404錯誤信息的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-09-09

最新評論

渑池县| 浙江省| 延边| 东乡族自治县| 阿拉尔市| 五华县| 皋兰县| 德惠市| 南溪县| 海丰县| 邹城市| 鹤壁市| 兰溪市| 阿拉善右旗| 林州市| 交口县| 柯坪县| 来宾市| 林口县| 桦甸市| 闸北区| 开原市| 玉田县| 古蔺县| 太和县| 岱山县| 山阳县| 建始县| 阿拉善右旗| 灵武市| 寿光市| 吐鲁番市| 信丰县| 溧水县| 宝鸡市| 昌邑市| 阿瓦提县| 河源市| 襄汾县| 阆中市| 盐城市|