㈠ 如何在android上使用OpenGL ES 2.0绘制点
FrameBuffer对象的概念可以参见前面文章AndroidOpenGLES开发教程(23):FrameBuffer。简单的和2D图像类比,FrameBuffer如果对应到二维图形环境中,就是一个2D的内存数组空间,缺省情况为屏幕的显存,也可以创建Offscreen内存空间,此时FrameBuffer可以是一个二维数组,数组每个元素代表一个像素颜色。对于三维图形来说,除了需要代表颜色的二维数组(ColorBuffer),还需要深度二维数组(DepthBuffer)或遮罩数组(StencilBuffer),因此在OpenGL中的FrameBuffer为上述ColorBuffer,DepthBuffer,StencilBuffer的集合。如果手机具有GPU,其缺省的FrameBuffer也是3D屏幕显示区域。通过OpenglES扩展支持,应用程序也可以创建内存中的FrameBuffer对象(不用于屏幕显示)。通过这种应用程序创建的FrameBuffer对象,OpenGL应用可以将图像显示输出重新定向到这个非屏幕显示用FrameBuffer对象中,类似于二维图形绘制中常用的Offscreen技术。和缺省的屏幕显示FrameBuffer一样,由应用程序创建的FrameBuffer对象也是由ColorBuffer,DepthBuffer和StencilBuffer(可选)的集合组成。这些Buffer在FrameBuffer对象中可以称为FrameBuffer-attachable图像,FrameBuffer定义了一些接入点(AttachmentPoint)可以用于连接(Attach)这些Buffer数组。OpenGLES定义了两种FrameBuffer-attachable图像,Texture和renderbuffer,简单的可以将Texture理解为Colorbuffer或是2D图像,renderbuffer对应于depthbuffer。
㈡ opengl如何画动态的曲线
定义一个static变量,然后动态的更新它来实现连续的显示
㈢ 为什么使用 android opengl
准备 为了开始本次的教程,你必须具备: 1.一款支持Android开发的IDE,如果你没有的话,可以在Android Developer website下载最新版本的Android studio。 2.一款运行Android4.0之上Android手机,并且GPU支持OpenGL ES2.0 3.对OpenGL的基本知识了解 设置OpenGL ES环境 创建GLSurfaceView 为了显示OpenGL的图形,你需要使用GLSurfaceView类,就像其他任何的View子类意义,你可以将它添加到你的Activity或Fragment之上,通过在布局xml文件中定义或者在代码中创建实例。 在本次的教程中,我们使用GLSurfaceView作为唯一的View在我们的Activity中,因此,为了简便,我们在代码中创建 GLSurfaceView的实例并将其传入setContentView中,这样它将会填充你的整个手机屏幕。Activity中的onCreate方 法如下: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GLSurfaceView view = new GLSurfaceView(this); setContentView(view); }123456123456 因为媒体效果的框架仅仅支持OpenGL ES2.0及以上的版本,所以在setEGLContextClientVersion 方法中传入2; view.setEGLContextClientVersion(2);11 为了确保GLSurfaceView仅仅在必要的时候进行渲染,我们在setRenderMode 方法中进行设置: view.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);11 创建Renderer Renderer负责渲染GLSurfaceView中的内容。 创建类实现接口GLSurfaceView.Renderer,在这里我们打算将这个类命名为EffectsRenderer,添加构造函数并覆写接口中的抽象方法,如下: public class EffectsRenderer implements GLSurfaceView.Renderer { public EffectsRenderer(Context context){ super(); } @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { } @Override public void onDrawFrame(GL10 gl) { } } 回到Activity中调用setRenderer方法,让GLSurfaceView使用我们创建的Renderer: view.setRenderer(new EffectsRenderer(this));11 编写Manifest文件 如果你想要发布你的App到谷歌商店,在AndroidManifest.xml文件中添加如下语句: <uses-feature android:glEsVersion="0x00020000" android:required="true" />11 这会确保你的app只能被安装在支持OpenGL ES2.0的设备之上。现在OpenGL环境准备完毕。 创建一个OpenGL平面 定义顶点 GLSurfaceView是不能直接显示一张照片的,照片首先应该被转化为纹理,应用在OpenGL square之上。在本次教程中,我将创建一个2D平面,并且具有4个顶点。为了简单,我将使用一个长方形,现在,创建一个新的类Square,用它来代表形状。 public class Square { }123123 默认的OpenGL系统的坐标系中的原点是在中心,因此4个角的坐标可以表示为: 左下角: (-1, -1) 右下角:(1, -1) 右上角:(1, 1) 左上角:(-1, 1) 我们使用OpenGL绘制的所有的物体都应该是由三角形决定的,为了画一个方形,我们需要两个具有一条公共边的三角形,那意味着这些三角形的坐标应该是: triangle 1: (-1, -1), (1, -1), 和 (-1, 1) triangle 2: (1, -1), (-1, 1), 和 (1, 1) 创建一个float数组来代表这些顶点: private float vertices[] = { -1f, -1f, 1f, -1f, -1f, 1f, 1f, 1f, };123456123456
㈣ 如何动态地画一条虚线在Android中使用OpenGL ES 2.0编程
动态绘制虚线,可以参考如下内容:
、被称为点画的影响。不幸的是,点画已经从OpenGL的删除,但幸运的是,有几种方法仍然得到的效果。我们必须充分利用的OpenGL的着色语言的这项任务。 顶点着色器:uniform mat4 u_modelViewProjectionMatrix;
uniform mat4 mv;
attribute vec4 a_position;
attribute vec4 a_color;
varying vec4 v_color;
varying vec4 position;
void main() {
gl_Position = u_modelViewProjectionMatrix * a_position;
position = mv * a_position;
v_color = a_color;
}
着色器:precision mediump float;
uniform vec2 sourcePoint;
varying vec4 v_color;
varying vec4 position;
void main() {
if (cos(0.1*abs(distance(sourcePoint.xy, position.xy))) + 0.5 > 0.0) {
gl_FragColor = vec4(0,0,0,0);
} else {
gl_FragColor = v_color;
}
}
我没有在这里找到本教程中,我测试了,这里是我的结果:
正如罗斯托夫在这个线程解释,这里最大的部分是sourcePoint。
的关键 CodeGo.net,整个事情是sourcePoint被传递的
该行的起源。
如果你不喜欢这种方式也存在textures图案的效果。有与α虚线效果textures,并将其应用到您的线路。
2.
我找到了一个更好的解决方案。它的水平和垂直线。
#define DOT_VERTEX_CODE \
"attribute vec4 a_Position;" \
"uniform mat4 projectionMatrix;" \
"varying vec2 v_xy;" \
"void main() {gl_PointSize = 1.0; gl_Position = a_Position*projectionMatrix; v_xy = a_Position.xy;}"
#define DOT_FRAGMENT_CODE \
"precision mediump float;" \
"varying vec2 v_xy;" \
"uniform float isVert;" \
"uniform vec4 color1;" \
"uniform vec4 color2;" \
"void main() {gl_FragColor = mod(isVert > 0.0 ? v_xy.y : v_xy.x, 2.0) >= 1.0 ? color1 : color2;}"
㈤ qt android 怎样使用opengl
qt 可以通过QGLWidget运行opengl。QGLWidget继承QWidget,能够直接在里面调用opengl的接口。这个在qt文档里有具体说明,也有相关例子,所以不赘述了。但是无法在正式软件里面执行,为什么?因为正式软件是用QGraphicsScene这个场景类操作和操作一切item,而用QGraphicsView将其显示出来,而每一个item都是QGraphicsItem的子类。QGLWidget并不是QGraphicsItem类,我曾经尝试用普通的QWidget类那样,通过proxy来加进QGraphicsItem,但是没有成功。或许有方法,但是没有找到。
于是我放弃了用QGLWidget来操作opengl的打算,寻找直接在QGraphicsItem中操作opengl的方法。通过查看文档和示例代码,找到了这个方法:
1 往qt工程文件里添加opengl以及对应的lib。
2 对QGraphicsView进行一个三维对话框的指定,代码如下:
QGLWidget *widget = new QGLWidget(QGLFormat(QGL::SampleBuffers));
widget->makeCurrent();
QGraphicsView view;
view.setViewport(widget);
上述代码告诉了 QGraphicsView 类当前绘制的对象是支持opengl的。于是所有的场景中的item都将绘制到widget 上。
3 写一个QGraphicsItem的继承类,特别要重写paint函数。代码如下:
void XXX::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->beginNativePainting();
glColor3f(0.5,1.0,0.2);
glBegin(GL_TRIANGLES);
glVertex3f(100.0,100.0,-100.0);
glVertex3f(150.0, 100.0, -100.0);
glVertex3f(100.0, 150.0, -100.0);
glEnd();
painter->endNativePainting();
}
上面这个函数主要是用opengl接口绘制了一个三角形。记住,在opengl绘制之前一定要执行painter->beginNativePainting()以及painter->endNativePainting()这两个语句。
QGraphicsScene、 QGraphicsView和QGraphicsItem的关系可以查阅相关文档,也不赘述了。
不过我按照这个方式画的三角形,怎么也在窗口上显示不出来,找了半天才发现问题在这个函数上QGraphicsItem::boundingRect()。这个函数是 干什么用的呢?主要用来返回该item的初始化大小,这个大小不会轻易改变,后续的改变都可以通过矩阵来完成,但是初始大小是不变的。QGraphicsView通过这个矩形来判断当前item是不是需要重绘,如果在重绘区外,则不调用重绘函数了。同时碰撞检测之类,也可以用这个矩形来判断。原来,item本身的矩阵外包框不对,所以才导致了重回不出来,改过来就正确了。
上面说的很潦草,具体怎么改的步骤就不说了。要想正确的绘制,必须得弄清楚坐标系的关系,QGraphicsScene、QGraphicsView以及QGraphicsItem这三个坐标系到底是什么关系。我看了文档,也自己进行了测试,但是感觉文档和测试的结果有些出入。具体出入不说了。说一下自己得心的吧。
先说明:涉及到一切大小和长度,都是像素大小,至少我测试的结果是这样的。
在建立QGraphicsScene对象的时候,有一个构造函数是矩形,这个矩形是什么含义呢?经过测试,发现这个矩形并没有指定弹出窗口的位置,比如,我把矩形的左上角点指定为-1000,-1000,显示的位置和1000,1000是一样的,而长度则正确指定了(当然,可能会有滚动条)。所以,这个矩形的左上角点并不是显示的窗口的位置,而是它在逻辑上的左上角点。我们显示一切item,都是以这个逻辑上的坐标系为准来绘制的。比如,左上角点是-1000,-1000,而item的位置在-500,-500,则这个-500,-500相当于在显示窗口的左上角往下各加500个像素的坐标的位置。
那么 QGraphicsItem的boundingRect是什么意思呢?返回的是什么大小?是以什么坐标系显示的大小?首先,这个大小肯定是以像素为单位的,其次,这个矩形的坐标是以QGraphicsScene的逻辑坐标为准的。当然这个大小是没有任何矩阵叠加的大小。有了矩阵叠加后,实际的矩形可能会发生变化。假如在boundingRect中指定矩形的左上角为100,100,那么最终体现的位置则是QGraphicsScene逻辑坐标100,100的位置,如果QGraphicsScene的左上角点已经指定为-1000,-1000,那么这个位置实际上就是离窗口左上角点1100,1100的位置(由于有滚动条,所以也不一定是这个长度。)
那么在QGraphicsItem的paint函数中进行了opengl绘制用的是什么坐标呢?其实用的也是QGraphicsScene的逻辑坐标。如上面的例子,绘制的直角三角形直角顶点是0,0,那么显示的位置就是距离显示窗口左上角点1000,1000的位置。不过opengl的所有绘制都是没有矩阵叠加的基础上,如果用矩阵叠加,则显示的位置肯定和指定的有区别了。比如,我用setPos强制指定一个位置,这个位置将和opengl绘图坐标相叠加,最后显示到窗口上。我推测setPos其实是改变了矩阵,是一个平移矩阵。
㈥ 怎么通过opengl实现一个二维图形的平移和曲线移动呢
应该是你translate使用错误,translate使用时应该在绘制矩形之前。
实现物体曲线移动:做一个随时间刷新的命令响应,隔一个刷新时间更新一下物体的坐标。例如做正弦函数移动:
OnTimer
{
xpos=sin(t);
ypos=cos(t);
zpos=tan(t);
t++
gltranslatef(xpos,ypos,zpos);
gldraw*******();//你要绘制的物体
}
㈦ android opengl怎么画虚线
在OpenGL中画线是可以完全的控制,比如画虚线,用函数glLineStipple就可以控制画线的模式:
函数glLineStipple有两个参数,第一个是重复的次数,第二个是用一个16-bit的数来控制,0表示不画,1表示画;比如0000111100001111=0x0F0F表示“ — —”这个模式。
glLineWidth (1.0);
glLineStipple (1, 0x0F0F);
glBegin(GL_LINES);
glVertex2f (0.0,0.0); glVertex2f (100.0,100.0);
glEnd();
画出来的就是虚线。
㈧ Android三维坐标图
Android进行三维图案绘制需要使用opengl,低版本不支持.简单来说图形也是由点,线,面组成,简单来说就是规定三位坐标系原点,根据曲线函数进行点计算绘制出来.
㈨ android下的openGL开发
这个建议买本教材看看,现在这样的教材很多。
OpenGL ES 2.0
In this document
Create an Activity with GLSurfaceView
Draw a Shape on GLSurfaceViewDefine a Triangle
Draw the Triangle
Apply Projection and Camera Views
Add Motion
Respond to Touch Events
Related Samples
API Demos - graphics
OpenGL ES 2.0 Sample
TouchRotateActivity
See also
3D with OpenGL
OpenGL ES 1.0
This tutorial shows you how to create a simple Android application that uses the OpenGL ES 2.0 API to perform some basic graphics operations. You'll learn how to:
•Create an activity using GLSurfaceView and GLSurfaceView.Renderer
•Create and draw a graphic object
•Define a projection to correct for screen geometry
•Define a camera view
•Rotate a graphic object
•Make graphics touch interactive
The Android framework supports both the OpenGL ES 1.0/1.1 and OpenGL ES 2.0 APIs. You should carefully consider which version of the OpenGL ES API (1.0/1.1 or 2.0) is most appropriate for your needs. For more information, see Choosing an OpenGL API Version. If you would prefer to use OpenGL ES 1.0, see the OpenGL ES 1.0 tutorial.
Before you start, you should understand how to create a basic Android application. If you do not know how to create an app, follow the Hello World Tutorial to familiarize yourself with the process.
Caution: OpenGL ES 2.0 is currently not supported by the Android Emulator. You must have a physical test device running Android 2.2 (API Level 8) or higher in order to run and test the example code in this tutorial.
Create an Activity with GLSurfaceView
To get started using OpenGL, you must implement both a GLSurfaceView and a GLSurfaceView.Renderer. The GLSurfaceView is the main view type for applications that use OpenGL and the GLSurfaceView.Renderer controls what is drawn within that view. (For more information about these classes, see the 3D with OpenGL document.)
To create an activity using GLSurfaceView:
1.Start a new Android project that targets Android 2.2 (API Level 8) or higher.
2.Name the project HelloOpenGLES20 and make sure it includes an activity called HelloOpenGLES20.
3.Modify the HelloOpenGLES20 class as follows: package com.example.android.apis.graphics;import android.app.Activity;import android.content.Context;import android.opengl.GLSurfaceView;import android.os.Bundle;public class HelloOpenGLES20 extends Activity { private GLSurfaceView mGLView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create a GLSurfaceView instance and set it // as the ContentView for this Activity mGLView = new HelloOpenGLES20SurfaceView(this); setContentView(mGLView); } @Override protected void onPause() { super.onPause(); // The following call pauses the rendering thread. // If your OpenGL application is memory intensive, // you should consider de-allocating objects that // consume significant memory here. mGLView.onPause(); } @Override protected void onResume() { super.onResume(); // The following call resumes a paused rendering thread. // If you de-allocated graphic objects for onPause() // this is a good place to re-allocate them. mGLView.onResume(); }} class HelloOpenGLES20SurfaceView extends GLSurfaceView { public HelloOpenGLES20SurfaceView(Context context){ super(context); // Create an OpenGL ES 2.0 context. setEGLContextClientVersion(2); // Set the Renderer for drawing on the GLSurfaceView setRenderer(new HelloOpenGLES20Renderer()); }}
Note: You will get a compile error for the HelloOpenGLES20Renderer class reference. That's expected; you will fix this error in the next step.
As shown above, this activity uses a single GLSurfaceView for its view. Notice that this activity implements crucial lifecycle callbacks for pausing and resuming its work.
The HelloOpenGLES20SurfaceView class in this example code above is just a thin wrapper for an instance of GLSurfaceView and is not strictly necessary for this example. However, if you want your application to monitor and respond to touch screen events—and we are guessing you do—you must extend GLSurfaceView to add touch event listeners, which you will learn how to do in the Reponding to Touch Events section.
In order to draw graphics in the GLSurfaceView, you must define an implementation of GLSurfaceView.Renderer. In the next step, you create a renderer class to complete this OpenGL application.
4.Create a new file for the following class HelloOpenGLES20Renderer, which implements the GLSurfaceView.Renderer interface: package com.example.android.apis.graphics;import javax.microedition.khronos.egl.EGLConfig;import javax.microedition.khronos.opengles.GL10;import android.opengl.GLES20;import android.opengl.GLSurfaceView;public class HelloOpenGLES20Renderer implements GLSurfaceView.Renderer { public void onSurfaceCreated(GL10 unused, EGLConfig config) { // Set the background frame color GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f); } public void onDrawFrame(GL10 unused) { // Redraw background color GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); } public void onSurfaceChanged(GL10 unused, int width, int height) { GLES20.glViewport(0, 0, width, height); } }
This minimal implementation of GLSurfaceView.Renderer provides the code structure needed to use OpenGL drawing methods:
◦onSurfaceCreated() is called once to set up the GLSurfaceView environment.
◦onDrawFrame() is called for each redraw of the GLSurfaceView.
◦onSurfaceChanged() is called if the geometry of the GLSurfaceView changes, for example when the device's screen orientation changes.
For more information about these methods, see the 3D with OpenGL document.
The code example above creates a simple Android application that displays a grey screen using OpenGL ES 2.0 calls. While this application does not do anything very interesting, by creating these classes, you have layed the foundation needed to start drawing graphic elements with OpenGL ES 2.0.
If you are familiar with the OpenGL ES APIs, these classes should give you enough information to use the OpenGL ES 2.0 API and create graphics. However, if you need a bit more help getting started with OpenGL, head on to the next sections for a few more hints.
Note: If your application requires OpenGL 2.0, make sure you declare this in your manifest:
<!-- Tell the system this app requires OpenGL ES 2.0. --> <uses-feature android:glEsVersion="0x00020000" android:required="true" />
For more information, see OpenGL manifest declarations in the 3D with OpenGL document.
㈩ android opengl
1:需要哪些包?
这个很难说,要看你的游戏用到什么功能。
2:有必要使用opengl吗?
最好用上,怎么说opengl也是比java更底层的,图像的运算处理效率会更高,而且学好opengl,就算你以后转去做苹果了,或是你的游戏要开发苹果版的,也能快速转型和移植(苹果也是有opengl的)
3:使用opengl做的2d游戏比使用j2me的game包有哪些优势及不足??
opengl的优势上面说了,不足就是会更难入手,首先用opengl的话,你就不能用那些又方便又好用的java接口了,然后要搭建NDK平台,再然后,opengl es(手机上的opengl 叫opengl es)的学习资料很少,很难入门。我个人就用opengl es写过一个2d的应用,用习惯之后就很顺手的。