2012-07-05 10 views
10

由於SpriteMethodTest說繪製sprite的方法很多,首先,我嘗試了畫布,並遇到一些性能問題。比我決定學習opengl。我使用GL11Ext擴展名取得了第一個成就。但默認情況下,當您繪製紋理時,它們將被翻轉,並且設備屏幕左下角(橫向模式)中的x和y軸爲零,而不像畫布,並且不能旋轉精靈。不能找到適用於android 2d opengl sprite類的例子,它不使用GL11Ext進行繪製

比我試圖影響使用GLU看這個軸視圖。但沒有效果。

比我想旋轉我的精靈,並沒有影響GL11Ext的原因,因爲他們說。

所以我現在主要的計算器條件和基本的問題是:

1.哪種方法來實現變焦功能,旋轉和彈跳精靈影響,爲尋找AT X和Y軸OLD SWEET方式[(0,0)在景觀模式左上角]?

2.是否有某些SPIRITE CLASS示例在宇宙中WICH只使用1種好的方式進行SPRITE渲染? (SpriteMethodTest讓我很困惑)

回答

10

EVRIKA !!!

我幾乎是在自殺!在離開Canvas 3天后,學習OpenGL方法來實現遊戲引擎。

網絡充滿了垃圾的OpenGL教程,其中許多未完成,其中許多未完成,導致2D OpenGL遊戲引擎實施methotds錯誤的方式。最大的錯誤是使用G11Ext製作遊戲。 AS他們不ROTATE:d

Annd annd後來我發現從其他教程本教程中,我從YouTube遊戲樣本視頻鏈接找到笑:

不要混淆觀衆這裏是

第1章:http://obviam.net/index.php/opengl-es-with-android-switching-from-canvas-to-opengl/

第2章:http://obviam.net/index.php/opengl-es-android-displaying-graphical-elements-primitives/

第3章:http://obviam.net/index.php/texture-mapping-opengl-android-displaying-images-using-opengl-and-squares/

Annd只是15分鐘前我發現了我可以旋轉,移動和調整其精靈形狀的方式! ! ! hahah

因此,很多讀者在閱讀完這篇GREAT教程之後都會問如何移動和調整大小和旋轉精靈。所以,我從例子和教程這個爛攤子摸索出一些代碼:

這個類是用於一些頂點操作

public class Vertex 
{ 
    public FloatBuffer buffer; // buffer holding the vertices 
    public float vertex[]; 
    public Vertex (float[] vertex) 
    { 
     this.vertex = vertex; 
     this.prepare(); 
    } 
    private void prepare() 
    { 
     // a float has 4 bytes so we allocate for each coordinate 4 bytes 
     ByteBuffer factory = ByteBuffer.allocateDirect (vertex.length * 4); 
     factory.order (ByteOrder.nativeOrder()); 
     // allocates the memory from the byte buffer 
     buffer = factory.asFloatBuffer(); 
     // fill the vertexBuffer with the vertices 
     buffer.put (vertex); 
     // set the cursor position to the beginning of the buffer 
     buffer.position (0);   
    } 
} 

這個類是用於繪製形狀,質地能夠移動旋轉和位置

public class Square 
{ 
    Vertex shape,texture; 
    int corner=0; 
    float x=0; 

    public Square() 
    { 
     shape = new Vertex (new float[] 
       { 
       1f,1f,0f, 
       0f,1f,0f, 
       1f,0f,0f, 
       0f,0f,0f, 
       }); 

     texture = new Vertex (new float[] 
       { 
       1.0f, 0.0f, 
       0.0f, 0.0f, 
       1.0f, 1.0f, 
       0.0f, 1.0f, 
       });  
    } 

    /** The draw method for the square with the GL context */ 
    public void draw (GL10 gl, int image, float x, float y, float width, float height, float corner) 
    { 
     if (corner>=0) 
     { 
      corner += 1;  
     } 
     if (corner>360) 
     { 
      corner = -1; 
     } 
     gl.glPushMatrix(); 

     x += 1f; 
     if (x>800) 
     { 
      x = 0; 
     } 

     position (gl, 0, 0, width, height, corner); 

     // bind the previously generated texture 
     gl.glBindTexture(GL10.GL_TEXTURE_2D, image); 

     // Point to our buffers 
     gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
     gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 

     // set the colour for the square 
     gl.glColor4f (0.0f, 1.0f, 0.0f, 0.5f); 

     // Set the face rotation 
     gl.glFrontFace(GL10.GL_CW);  

     // Point to our vertex buffer 
     gl.glVertexPointer (3, GL10.GL_FLOAT, 0, shape.buffer); 
     gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texture.buffer); 

     // Draw the vertices as triangle strip 
     gl.glDrawArrays (GL10.GL_TRIANGLE_STRIP, 0, shape.vertex.length/3); 

     // Disable the client state before leaving 
     gl.glDisableClientState (GL10.GL_VERTEX_ARRAY); 
     gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 

     gl.glPopMatrix();  
    } 

    public void position (GL10 gl, float x, float y, float width, float height, float corner) 
    { 
     gl.glTranslatef (x, y, 0f); //MOVE !!! 1f is size of figure if called after scaling, 1f is pixel if called before scaling 

     if (corner>0) 
     { 
      gl.glTranslatef (width/2, height/2, 0f); 
      gl.glRotatef (corner, 0f, 0f, 1f); // ROTATE !!! 
      gl.glTranslatef (-width/2, -height/2, 0f);   

     } 

     gl.glScalef (width, height, 0f); // ADJUST SIZE !!! 

    } 
} 

和主要的如何設置相機以使1級的OpenGL單元== 1個像素annd如何加載紋理

public class Scene implements Renderer 
{ 
    public Context context; 
    public Resources resources; 
    public SparseIntArray images = new SparseIntArray(); 
    public float width; 
    public float height; 

    public Scene (Context context) 
    { 
     this.context = context; 
     this.resources = context.getResources(); 
    } 

    @Override 
    public void onDrawFrame (GL10 gl) 
    { 
//  // clear Screen and Depth Buffer 
     gl.glClear (GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 
     gl.glMatrixMode(GL10.GL_MODELVIEW); 
//  // Reset the Modelview Matrix 
     gl.glLoadIdentity(); 
     draw (gl); 

    } 

    @Override 
    public void onSurfaceChanged (GL10 gl, int width, int height) 
    { 
     this.width = width; 
     this.height = height; 

     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 

     gl.glOrthof (0, width, 0, height, -1f, 1f); 
     //gl.glTranslatef (0f, -height/2, 0.0f); // move the camera !! 


     gl.glMatrixMode (GL10.GL_MODELVIEW); // Select The Modelview Matrix 
     gl.glLoadIdentity(); // Reset The Modelview Matrix 

     load (gl); 
    } 

    public void onSurfaceCreated(GL10 gl, EGLConfig config) 
    { 
     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 

     gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); 
     gl.glEnable(GL10.GL_BLEND); 


     //Really Nice Perspective Calculations 
     gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); 

     init (gl); 
    } 


    public void init (GL10 gl) 
    { 

    } 

    public void load (GL10 gl) 
    { 

    } 

    public void draw (GL10 gl) 
    { 

    } 

    private static int next (GL10 gl) 
    { 
     int[] temp = new int[1]; 
     gl.glGenTextures (1, temp, 0); 
     return temp[0]; 
    } 

    public int image (GL10 gl, int resource) 
    { 
     int id = next (gl); 
     images.put (resource, id); 

     gl.glBindTexture (GL10.GL_TEXTURE_2D, id); 

     gl.glTexParameterf (GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); 
     gl.glTexParameterf (GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); 

     gl.glTexParameterf (GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE); 
     gl.glTexParameterf (GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE); 

     gl.glTexEnvf (GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE); 

     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inScaled = false; 

     InputStream input = resources.openRawResource (resource); 
     Bitmap bitmap; 
     try 
     { 
      bitmap = BitmapFactory.decodeStream (input, null, options); 
     } 
     finally 
     { 
      try 
      { 
       input.close(); 
      } 
      catch (IOException e) 
      { 
       // Ignore. 
      } 
     } 

//  Matrix flip = new Matrix(); 
//  flip.postScale (1f, -1f); 
//  bitmap = Bitmap.createBitmap (bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), flip, true); 

     GLUtils.texImage2D (GL10.GL_TEXTURE_2D, 0, bitmap, 0);  
     return id; 
    } 

} 

和一些使用

public class Scene2 extends Scene 
{ 
    Square square1, square2; 

    public Scene2(Context context) 
    { 
     super (context); 
     // TODO Auto-generated constructor stub 
    } 

    public void init (GL10 gl) 
    { 
     square1 = new Square(); 
     square2 = new Square(); 
    } 

    public void load (GL10 gl) 
    { 
     image (gl, R.drawable.s1_clouds); 
     image (gl, R.drawable.s1_ground); 
    } 

    public void draw (GL10 gl) 
    { 
     square1.draw (gl, images.get(R.drawable.s1_clouds), 0, 0, width, height, 0); 
     square1.draw (gl, images.get(R.drawable.s1_ground), 0, 0, width, height, 0); 
    } 

} 

這裏最主要的事情,我想實現和執行是將X軸和Y軸都像在畫布上:

(0,0) 
--------------------------------- X axis 
| 
| 
| 
| 
| 
| 
| 
| 
Y axis 

我會在這之後寫一些完全教程並且我想宣佈我實現了我想實現的所有目標,即:頂部X軸,左側Y軸,opengl單位=像素,以像素爲單位設置對象大小,旋轉對象,以像素爲單位移動對象。現在我會處理動畫精靈,讓他們在更細的類別和多數民衆贊成在新的2D OpenGL的遊戲框架的基礎...

發現這個功能幫我補習http://www.morrowland.com/apron/tutorials/gl/gl_matrix.php

所以非常感謝這個博客指着我了唯一正確的方法...

+1的Android simpliest OpenGL的2D遊戲在1周內引擎...

幸福令人興奮......

:P

編輯:一年後,我一直在使用這裏描述的概念和樣品遊戲,這裏的任何可能的框架使用例子https://github.com/hazardland/ferry.android(市場https://play.google.com/store/apps/details?id=hazardland.borani視屏)

+3

我覺得你的痛苦哥哥一個很好的框架https://github.com/hazardland/game.android。 – torger 2012-12-04 05:11:01

+0

自從我在https://github.com/hazardland/hazardland之後製作了opengl框架後,經過了很多次... ::P – BIOHAZARD 2012-12-05 15:10:33

+0

一個簡單但完整的示例2D遊戲,帶有OpenGL ES 2.0:http://code.google .com/p/android-breakout /。這使得一些不同的選擇w.r.t.座標軸和像素單位,但它決定了決策的地點和原因。 – fadden 2013-01-15 20:43:19

相關問題