㈠ 如何在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的應用,用習慣之後就很順手的。