java实现OpenGL ES纹理映射的方法_java

本文实例讲述了java实现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程序设计有所帮助。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索java
, opengl
, 纹理映射
es
opengl 纹理映射、opengl 球面纹理映射、opengl 纹理映射 球体、opengl 环境纹理映射、opengl纹理映射原理,以便于您获取更多的相关知识。

时间: 2024-11-10 11:38:52

java实现OpenGL ES纹理映射的方法_java的相关文章

java基于OpenGL ES实现渲染实例_java

本文实例讲述了java基于OpenGL ES实现渲染的方法.分享给大家供大家参考.具体如下: 1. 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

在Java下利用log4j记录日志的方法_java

1.前言 log4j是一个用Java编写的可靠,快速和灵活的日志框架(API),它在Apache软件许可下发布. Log4j已经被移植到了C,C++,C#,Perl,Python和Ruby等语言中. Log4j是高度可配置的,并可通过在运行时的外部文件配置.它根据记录的优先级别,并提供机制,以指示记录信息到许多的目的地,例如:数据库,文件,控制台,UNIX系统日志等. Log4j中有三个主要组成部分:     loggers: 负责捕获记录信息.     appenders : 负责发布日志信息

Java实现洗牌发牌的方法_java

本文实例讲述了Java实现洗牌发牌的方法.分享给大家供大家参考.具体如下: import java.util.*; public class Main { /** * @param args the command line arguments */ static int numbersOfPlayers = 4; static int numbersOfHandCard = 13; public static void main(String[] args) { // TODO code ap

java获取日期之间天数的方法_java

本文实例讲述了java获取日期之间天数的方法.分享给大家供大家参考.具体实现方法如下: private int daysBetween(Date now, Date returnDate) { Calendar cNow = Calendar.getInstance(); Calendar cReturnDate = Calendar.getInstance(); cNow.setTime(now); cReturnDate.setTime(returnDate); setTimeToMidni

Java实现Html转Pdf的方法_java

本文实例讲述了Java实现Html转Pdf的方法.分享给大家供大家参考.具体如下: package test; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import org.xhtmlrenderer.pdf.ITextFontResolver; import org.xhtmlrenderer.pdf.ITextRenderer; import com.lowagie.

JAVA+Struts2获取服务器地址的方法_java

本文实例讲述了JAVA+Struts2获取服务器地址的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: HttpServletRequest request=ServletActionContext.getRequest();  String path=request.getRequestURI();  String actionPath=".."+path.substring(9);  //访问服务器所带有的参数信息  String queryInfo=reque

JAVA随机打乱数组顺序的方法_java

本文实例讲述了JAVA随机打乱数组顺序的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: import java.util.Random;        public class RandomSort {        private Random random = new Random();        //数组大小        private static final int SIZE = 10;        //要重排序的数组        private int

Java中对话框的弹出方法_java

最近在做学校的课程设计,java编程需要用到对话框弹出,第一反应是js中的alert和confirm,java的话瞬间懵,查阅学习总结如下,用以以后的学习 1.显示一个错误对话框,该对话框显示的 message 为 'alert': JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE); 2.显示一个内部信息对话框,其 message 为 'informati

java实现上传图片进行切割的方法_java

本文实例讲述了java实现上传图片进行切割的方法.分享给大家供大家参考.具体分析如下: 为什么我要进行上传的图片进行切割呢,我这个项目的图片是部门logo,每个部门都可以选择不同的logo,但是要应对浏览器的兼容以及拉伸,我选择了把一张图片切成左.中.右和剩下的部分,因为左边和中变可能会有图案或者字所以不能拉伸,拉伸的只是右边的部分,剩下的部分自适应就可以了.所以用了javax的ImageReader来操作.最后以blob类型保存数据库中. 首先要在form表单里面写上enctype="mult