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

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

(0)

相关推荐

  • JAVA获得域名IP地址的方法

    本文实例讲述了JAVA获得域名IP地址的方法.分享给大家供大家参考.具体如下: import java.net.InetAddress; import java.net.UnknownHostException; public class TestInetAddress { InetAddress myIpAddress = null; InetAddress[] myServer = null; public static void main(String args[]) { TestInet

  • java利用mybatis拦截器统计sql执行时间示例

    可以根据执行时间打印sql语句,打印的sql语句是带参数的,可以拷贝到查询分析器什么的直接运行 复制代码 代码如下: package mybatis; import java.text.DateFormat;import java.util.Date;import java.util.List;import java.util.Locale;import java.util.Properties; import org.apache.ibatis.executor.Executor;import

  • JAVA实现FTP断点上传的方法

    本文实例讲述了JAVA实现FTP断点上传的方法.分享给大家供大家参考.具体分析如下: 这里主要使用apache中的net包来实现.网址http://commons.apache.org/net/.具体包的下载和API文档请看官网. 断点上传就是在上传的过程中设置传输的起始位置.并设置二进制传输. import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.

  • java自定义拦截器用法实例

    本文实例讲述了java自定义拦截器及其用法.分享给大家供大家参考.具体如下: LoginInterceptor.java文件如下: package com.tq365.util; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwo

  • java使用POI读取properties文件并写到Excel的方法

    本文实例讲述了java使用POI读取properties文件并写到Excel的方法.分享给大家供大家参考.具体实现方法如下: package com.hubberspot.code; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import

  • java Struts2 在拦截器里的跳转问题

    复制代码 代码如下: java.lang.IllegalStateException at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:405) at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:725) at org.apache.struts2.dispatcher.Dispatcher.servi

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

    本文实例讲述了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实现OpenGL ES纹理映射的方法

    本文实例讲述了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.GL

  • OpenGL ES正交投影实现方法(三)

    本文实例为大家分享了OpenGL ES正交投影展示的具体代码,供大家参考,具体内容如下 绘制正方形 在最开始绘制的六边形里面好像看起来挺容易的,也没有出现什么问题,接下来不妨忘记前面绘制六边形的代码,让我们按照自己的理解来绘制一个简单的正方形. 按照我的理解,要想在屏幕中间显示一个正方形,效果如下图所示 应该创建的数据如下图所示 即传给渲染管线的顶点数据如下图: float[] vertexArray = new float[] { (float) -0.5, (float) -0.5, 0,

  • OpenGL ES透视投影实现方法(四)

    在之前的学习中,我们知道了一个顶点要想显示到屏幕上,它的x.y.z分量都要在[-1,1]之间,我们回顾一下渲染管线的图元装配阶段,它实际上做了以下几件事:剪裁坐标.透视分割.视口变换.图元装配的输入是顶点着色器的输出,抓哟是物体坐标gl_Position,之后到光栅化阶段. 图元装配 剪裁坐标 当顶点着色器写入一个值到gl_Position时,这个点要求必须在剪裁空间中,即它的x.y.z坐标必须在[-w,w]之间,任何这个范围之外的点都是不可见的. 这里需要注意以下,对于attribute类型的

  • 一文详解 OpenGL ES 纹理颜色混合的方法

    目录 一.混合API 二.参数含义 2.1 举个栗子 2.2 参数含义 三. 几种常用混合方式效果 3.1 混合(GL_ONE, GL_ZERO) 3.2 混合(GL_ONE, GL_ONE) 3.3 混合(GL_ONE, GL_ONE_MINUS_DST_ALPHA) 3.4 混合 (GL_SRC_ALPHA, GL_ONE) 3.5 混合 (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) 在OpenGL中绘制的时候,有时候想使新画的颜色和已经有的颜色按照一定的方式

  • 在iOS中使用OpenGL ES实现绘画板的方法

    今天我们使用 OpenGL ES 来实现一个绘画板,主要介绍在 OpenGL ES 中绘制平滑曲线的实现方案. 首先看一下最终效果: 在 iOS 中,有很多种方式可以实现一个绘画板,比如我的另外一个项目 MFPaintView 就是基于 CoreGraphics 实现的. 然而,使用 OpenGL ES 来实现可以获得更多的灵活性,比如我们可以自定义笔触的形状,这是其他实现方式做不到的. 我们知道,OpenGL ES 中只有 点.直线.三角形 这三种图元.因此, 怎么在 OpenGL ES 中绘

  • 通过OpenGL ES混合模式缩放视频缓冲区来适应显示尺寸

    当开发基于软件模式的游戏时,通过缩放视频缓冲区来适应显示尺寸是最棘手的问题之一.当面对众多不同的分辨率时(比如开放环境下的Android),该问题会变得更加麻烦,作为开发人员,我们必须尝试在性能与显示质量之间找到最佳平衡点.正如我们在第2章中看到的,缩放视频缓冲区从最慢到最快共有3种类型. 软件模拟:3中类型中最慢,但最容易实现,是没有GPU的老款设备上的最佳选择.但是现在大部分智能手机都支持硬件加速. 混合模式:这种方式混合使用软件模拟(创建图像缓冲区)和硬件渲染(向显示屏绘制)两种模式.这种

  • OpenGL ES纹理详解

    使用前面学过的技术已经可以利用OpenGL ES构建立体图形,并通过顶点着色器和片元着色器对其进行各种变化呢和光照等效果使得三维效果更加真实,实际上我看看到很多的3D游戏漂亮多了,那是因为有各种各样的漂亮的图像带给人很多视觉盛宴,这篇文章在前面的基础上,增加物体的表面贴图,使得物体更加好看. 纹理概念 纹理用来表示图像照片或者说一系列的数据,使用纹理可以使物体用用更多的细节.OpenGL ES 2.0 中有两种贴图:二维纹理和立方体纹理. 每个二维纹理都由许多小的纹理元素组成,类似与片元和像素,

  • android使用OPENGL ES绘制圆柱体

    本文实例为大家分享了android使用OPENGL ES绘制圆柱体的具体代码,供大家参考,具体内容如下 效果图: 编写jiem.java *指定屏幕所要显示的假面,并对见.界面进行相关设置     *为Activity设置恢复处理,当Acitvity恢复设置时显示界面同样应该恢复     *当Activity暂停设置时,显示界面同样应该暂停 package com.scout.eeeeeee; import android.app.Activity; import android.os.Bund

  • OpenGL ES着色器使用详解(二)

    本文介绍了OpenGL ES着色器使用的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 1.着色器语言 着色器语言是一种高级图形编程语言,和C/C++语言很类似,但存在很大差别,比如,不支持double,byte ,short,不支持unin,enum,unsigned以及位运算等,但其加入了很多原生的数据类型,如向量,矩阵等. 数据类型可分为标量.向量.矩阵.采样器.结构体.数组等 向量 向量传递参数,如果只提供一个标量,这个值用于设置所有向量的值:如果输入是多个标量或者是矢量,从左到

随机推荐