`
melord
  • 浏览: 144059 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

OpenGL ES教程I之创建OpenGL视图(原文对照)

阅读更多

OpenGL ES Tutorial for Android – Part I – Setting up the view

 

I'm going to write a couple of tutorials on using OpenGL ES on Android phones. The theory of OpenGL ES is the same on different devices so it should be quite easy to convert them to another platform.

我将写一些关于在Andoird 手机上使用OpenGL Es的教程。理论上OpenGL ES适用于不同的设备(平台),因此很容易被移植到其它平台。

I can't always remember where I found particular info so I might not always be able to give you the right reference. If you feel that I have borrowed stuff from you but have forgotten to add you as a reference, please e-mail me.

我常常忘了我所搜集的资料的来源与出处,所以(在某些资料方面)我可能不能给出正确的引用。如果您觉得我使用了您的资料而没有标明引用(位置),请发电子邮件给我。

In the code examples I will have two different links for each function. The actual function will be linked to the android documentation and after that I will also link the OpenGL documentations. Like this:

在示例代码中,我对每个函数将使用两种不同的链接。点击函数将链接到Android docAndroid doc太简单了,建议还是链接到OpenGL doc吧),在函数后面,我会添加一个到OpenGL docs的链接,如下:

gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);  // OpenGL docs.

So, let's start.

好了,(言归正传)让我们这就开始OpenGL学习之旅吧。

In this tutorial I will show you how to set up your OpenGL ES view that’s always a good place to start.

在本教程中,我将(先)展示怎样来建立OpenGL ES界面,通常这是一个好的开始。

Setting up an OpenGL ES View

Setting up a OpenGL view has never been hard and on Android it is still easy. There really are only two things you need to get started.

Android中建立一个OpenGL视图非常简单,您需要做的只有两件事。

GLSurfaceView

GLSurfaceView is a API class in Android 1.5 that helps you write OpenGL ES applications.

GLSurfaceViewAndroid1.5中帮助您编写OpenGL ES应用程序的API类(,其具有以下功能)

·         Providing the glue code to connect OpenGL ES to the View system.

·         Providing the glue code to make OpenGL ES work with the Activity life-cycle.

·         Making it easy to choose an appropriate frame buffer pixel format.

·         Creating and managing a separate rendering thread to enable smooth animation.

·         Providing easy-to-use debugging tools for tracing OpenGL ES API calls and checking for errors.

·         提供了OpenGL ES连接View系统的粘合代码。

·         提供了OpenGL ESActivity生命周期协作的粘合代码。

·         易于选择合适的帧缓冲像素格式。

·         创建和管理独立的渲染线程来启用平滑动画(效果)

·         提供易于使用的调试工具来跟踪OpenGL ES API调用与检查错误。

If you want to get going fast with your OpenGL ES application this is where you should start.

The only function you need to call on is:

如果您想快速着手OpenGL ES应用程序,请从这里开始

您需要调用的唯一函数是:

public void  setRenderer(GLSurfaceView.Renderer renderer)

Read more at: GLSurfaceView

GLSurfaceView.Renderer

GLSurfaceView.Renderer is a generic render interface. In your implementation of this renderer you should put all your calls to render a frame.

GLSurfaceView.Renderer是一个通用的渲染接口,您必须实现此类的抽象方法来画(动画中的)帧
There are three functions to implement:

您需要实现的三个方法如下:

// Called when the surface is created or recreated.

// 当平面创建或或重新创建的时候调用

 

public void onSurfaceCreated(GL10 gl, EGLConfig config)

 

// Called to draw the current frame.

// 画当前帧

public void onDrawFrame(GL10 gl)

 

// Called when the surface changed size.

// 当视图大小变更的时候调用,如屏幕横纵切换

public void onSurfaceChanged(GL10 gl, int width, int height)

onSurfaceCreated

Here it's a good thing to setup things that you don't change so often in the rendering cycle. Stuff like what color to clear the screen with, enabling z-buffer and so on.

推荐将一些在渲染周期中不会变更的代码放到此处,如设置屏幕初始颜色,开始Z轴缓冲等。

onDrawFrame

Here is where the actual drawing take place.

在此函数中进行绘画(帧)

onSurfaceChanged

If your device supports flipping between landscape and portrait you will get a call to this function when it happens. What you do here is setting upp the new ratio.
Read more at:
GLSurfaceView.Renderer

如果您的设备支持横竖屏切换,那么在切换发生时,您就需要在此方法中重新设置新的横纵比例了。

Putting it together

First we create our activity, we keep it clean and simple.

首先,创建一个尽可能的简洁 Activity

package se.jayway.opengl.tutorial;

 

import android.app.Activity;

import android.opengl.GLSurfaceView;

import android.os.Bundle;

 

public class TutorialPartI extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

               GLSurfaceView view = new GLSurfaceView(this);

             view.setRenderer(new OpenGLRenderer());

             setContentView(view);

    }

}

Our renderer takes little bit more work to setup, look at it and I will explain the code a bit more.

更多的render设置,请见我扩展的renderer

package se.jayway.opengl.tutorial;

 

import javax.microedition.khronos.egl.EGLConfig;

import javax.microedition.khronos.opengles.GL10;

 

import android.opengl.GLU;

import android.opengl.GLSurfaceView.Renderer;

 

public class OpenGLRenderer implements Renderer {

        /*

         * (non-Javadoc)

         *

         * @see

         * android.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax.

         * microedition.khronos.opengles.GL10, javax.microedition.khronos.

         * egl.EGLConfig)

         */

        public void onSurfaceCreated(GL10 gl, EGLConfig config) {

                // Set the background color to black ( rgba ).

                // 设置背景颜色为黑色 ( rgba ).

                gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);  // OpenGL docs.

                // Enable Smooth Shading, default not really needed.

                // 开启平滑阴影,实际上不需要(默认的shadeModel就是GL_SMOOTH

                gl.glShadeModel(GL10.GL_SMOOTH);// OpenGL docs.

                // Depth buffer setup.

                // 设置深度缓冲

                gl.glClearDepthf(1.0f);// OpenGL docs.

                // Enables depth testing.

                // 启用深度测试

                gl.glEnable(GL10.GL_DEPTH_TEST);// OpenGL docs.

                // The type of depth testing to do.

                // 设置深度测试类型

                gl.glDepthFunc(GL10.GL_LEQUAL);// OpenGL docs.

                // Really nice perspective calculations.

                // 真实透视计算

                gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, // OpenGL docs.

                          GL10.GL_NICEST);

        }

 

        /*

         * (non-Javadoc)

         *

         * @see

         * android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.

         * microedition.khronos.opengles.GL10)

         */

        public void onDrawFrame(GL10 gl) {

                // Clears the screen and depth buffer.

                // 清除屏幕和深度(Z轴)缓冲

                gl.glClear(GL10.GL_COLOR_BUFFER_BIT | // OpenGL docs.

                           GL10.GL_DEPTH_BUFFER_BIT);

        }

 

        /*

         * (non-Javadoc)

         *

         * @see

         * android.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax.

         * microedition.khronos.opengles.GL10, int, int)

         */

        public void onSurfaceChanged(GL10 gl, int width, int height) {

                // Sets the current view port to the new size.

                //设置当前视窗新大小

                gl.glViewport(0, 0, width, height);// OpenGL docs.

                // Select the projection matrix

                // 选择投影方式为透视投影

                gl.glMatrixMode(GL10.GL_PROJECTION);// OpenGL docs.

                // Reset the projection matrix

                // 重置投影矩阵

                gl.glLoadIdentity();// OpenGL docs.

                // Calculate the aspect ratio of the window

                // 计算窗口视角比例

                GLU.gluPerspective(gl, 45.0f,

                                   (float) width / (float) height,

                                   0.1f, 100.0f);

                // Select the modelview matrix

                // 选择变换方式为modelview

                gl.glMatrixMode(GL10.GL_MODELVIEW);// OpenGL docs.

                // Reset the modelview matrix

                // 重置变换矩阵

                gl.glLoadIdentity();// OpenGL docs.

        }

}

Fullscreen

Just add this lines in the OpenGLDemo class and you will get fullscreen.

只需要在OpenGLDemo类中添加如下代码,就可以让Activity全屏。建议还是在Manifest.xml中配置全屏

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE); // (NEW)

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

            WindowManager.LayoutParams.FLAG_FULLSCREEN); // (NEW)

        ... // Previous code.

    }

This is pretty much all you need to get your view up and running. If you compile and run it you will see a nice black screen.

如果想要运行创建的OpenGL视图,以上就已经足够了,编译运行此应用,你可以看到一个纯黑色的屏幕。不过GLSurfaceView运行于独立的线程,所以需要将此线程同步到Android主线程中,做法是在Activity pause时,GLSurfaceViewpauseActivity resume时,GLSurfaceView线程也resume

References

The info used in this tutorial is collected from:
Android Developers
OpenGL ES 1.1 Reference Pages

You can download the source for this tutorial here: Tutorial_Part_I.zip
You can also checkout the code from: code.google.com

Next tutorial: OpenGL ES Tutorial for Android – Part II – Building a polygon

Per-Erik Bergman
Consultant at
Jayway

 

分享到:
评论

相关推荐

    OpenGL ES之加载图片.zip

    OpenGL ES之加载图片:详细地配置OpenGL ES的初始化,配置视图创建的渲染缓存区,加载顶点/纹理坐标数据,加载纹理数据的逻辑

    OPENGL ES ios教程源码

    OPENGL ES ios教程源码,包括渲染管线、着色器、矩阵变换、模型视图投影、VBO、光照、深度缓存等内容。

    OpenGL ES 教程3

    每个面设置成不同颜色旋转的正方体,能够通过点击屏幕设置不同的视图模式。 如果想自己建立工程,选着win32只能设备应用,删除跟工程MyProject相同的两个文件MyProject.h,MyProject.cpp,将我上传的文件添加到项目...

    《OpenGL ES 2.0 开发向导》源码

    本教程向您展示了如何创建一个简单的Android应用程序,该应用程序使用OpenGL ES 2.0 API来执行一些基本的图形操作。你将学习如何: - 使用GLSurfaceView和GLSurfaceView. renderer创建一个活动 - 创建并绘制图形对象 ...

    OpenGL ES之渲染旋转的立方体.zip

    OpenGL ES之渲染了一个旋转的立方体,并在立方体上面展示图片,形成一个动态的相册展示集锦,运用了GIKit设定状态机、着色器渲染、纹理绘制、动画等多种技术。

    OpenGLES之CubeImage.zip

    OpenGL ES之渲染了一个旋转的立方体,并在立方体上面展示图片,形成一个动态的相册展示集锦,运用了GIKit设定状态机、着色器渲染、纹理绘制、动画等多种技术。

    OpenGL 对硬件曲面细分的支持,通过3 个管线阶段提供: (1)曲面细分控制着色器; (2)曲面细分器; (3)曲面细分评估

    创建一个摄像机和相关的MVP 矩阵,视图(v)和投影(p)矩阵确定摄像机朝向,模 型(m)矩阵可用于修改网格的位置和方向。 (2)顶点着色器: 在这个例子中基本上什么都不做,顶点将在曲面细分器中生成。 (3)曲面...

    GLES_Engine_Part1:OpenGL ES 引擎第 1 部分 - 空的 GLES 窗口

    在活动的 create 方法中,我们创建了 GLSurfaceView 的新子类并将其设置为内容视图(这样我们将看到的只是具有 OpenGL ES 上下文的表面。 我的GLRenderView.java 该类派生自 GLRenderView 类,这里所需要做的就是...

    glSatellite Demo:OpenGL ES 2 Android NDK技术演示-开源

    适用于Android平台的OpenGL ES 2技术演示。 它读取公共NORAD卫星数据库,并以光束的形式... -将Java视图与OpenGL视图混合(Java用于设置活动,信息和广告面板); -使用Android自定义消息队列将异步消息从Java发送到JNI

    ViewTransition

    在iphone中,普通视图和opengl es的视图之间的跳转在iphone中,普通视图和opengl es的视图之间的跳转在iphone中,普通视图和opengl es的视图之间的跳转在iphone中,普通视图和opengl es的视图之间的跳转

    android_opengl_example:Android 版本的 OpenGL 编程挑战

    第1部分: 您可以选择使用 OpenGL ES 2.0 或 3.0 构建全屏 OpenGL ES 视图。 为了节省时间,您可以使用像 Apple 的 GLKView 这样的平台级便利来构建视图,但如果您直接使用 OpenGL ES API 调用自己构建它,您将获得...

    Android基于OpenGL的GLSurfaceView创建一个Activity实现方法

    Android提供了两个基本的类让我们使用OpenGL ES API来创建和操纵图形:GLSurfaceView和 GLSurfaceView.Renderer。因此我们首先需要了解这两个类。 1. GLSurfaceView: 这是一个视图类,你可以调用OpenGL API在上面...

    iPhone应用程序开发指南.中文.pdf

    用OpenGL ES进行描画 101 应用Core Animation的效果 101 关于层 102 关于动画 102 文本和Web 103 关于文本和Web的支持 103 文本视图 103 Web视图 104 键盘和输入法 106 管理键盘 107 接收键盘通告 107 显示键盘 109 ...

    Android基于OpenGL在GLSurfaceView上绘制三角形及使用投影和相机视图方法示例

    本文实例讲述了Android基于OpenGL在GLSurfaceView上绘制三角形及使用投影和相机视图方法。分享给大家供大家参考,具体如下: 定义三角形 OpenGL 允许我们使用三维坐标来定义物体。在绘制三角形前,我们需要定义它...

    iphone3开发基础教程

    12.5 一些OpenGL ES基础知识 325 12.5.1 构建GLFun应用程序 326 12.5.2 设计Nib、添加框架、运行应用程序 335 12.6 小结 335 第13章 轻击、触摸和手势 336 13.1 多触摸术语 336 13.2 响应者链 337 13.3 多触摸体系...

    AndroidMazeGame:基于Android OpenGLES的3D迷宫游戏

    Android迷宫游戏 基于Android OpenGLES的3D迷宫游戏,用户可以使用手势来旋转视图并使用操纵杆来移动。 游戏示范

    gl-button:使用 OpenGL ES2 快速测试视觉反馈按钮

    使用 OpenGL ES2 对视觉反馈按钮进行快速 Android 自定义视图测试 所有渲染都是通过片段着色器完成的,这提供了很大的灵活性和可能性。 演示: : precision highp float ; uniform vec2 u_Size; // View size ...

    RenderTools:基于OpenGL的跨平台轻量级场景图库-开源

    RenderTools是C ++中基于OpenGL的场景图形库,适用于Windows,OSX和IOS,支持OpenGL | ES1.1,ES2.0,OpenGL1.5和最新版本(当前为4.2)。 它允许开发人员轻松地将场景图与xml进行反序列化,创建渲染缓冲区,帧缓冲...

    UnityPhotosphericView:使用倒法线球的等矩形光球视图

    使用程序性倒置法线球和自定义未照明的等角矩形着色器的光球视图,可避免大的纹理失真 入门 最初的想法 将InvertedSphere.cs放入/ Assets / Editor文件夹 生成具有自定义半径(例如20)的程序球体(法向倒置)...

Global site tag (gtag.js) - Google Analytics