2012-07-05 7 views
10

Come dice SpriteMethodTest ci sono molti modi per disegnare sprite. Per prima cosa ho provato la tela e ho avuto alcuni problemi di prestazioni. Poi ho deciso di imparare opengl. Ho realizzato i miei primi risultati utilizzando l'estensione GL11Ext. ma sai per impostazione predefinita quando disegni trame che sono capovolte e l'asse x e y hanno zero nell'angolo in basso a sinistra dello schermo del dispositivo (in modalità orizzontale) non come la tela e non puoi ruotare sprite.Non riesci a trovare un esempio adatto per Android 2d opengl sprite class che non usa GL11Ext per disegnare

Rispetto a quanto ho provato ad influire su questa vista asse utilizzando l'aspetto GLU. Ma non ha avuto effetto.

Di quanto volevo ruotare i miei sprite e non aveva effetto causa di GL11Ext come si suol dire.

Così ho la principale condizione di StackOverflow ora e la domanda di fondo è:

1. il metodo da utilizzare per raggiungere lo zoom, rotazione e rimbalzando effetti sulla sprite e per guardare assi X e Y IN VECCHIA DOLCE WAY [(0,0) È IN ALTO A SINISTRA IN MODALITÀ PAESAGGIO]?

2. E c'è qualche SPRITE CLASSE ESEMPIO NELL'UNIVERSO WICH usa solo 1 buon modo di Sprite RENDERING? (SpriteMethodTest mi confonde molto)

risposta

10

EVRIKA !!!

Mi stavo quasi uccidendo! dopo 3 giorni di partenza dalla tela e imparando i metodi OpenGL per implementare il motore di gioco.

Il Web è pieno di tutorial OpenGL pieni di spazzatura e molti di essi sono incompleti e molti di essi portano a un modo errato per methotds di implementazione del motore di gioco 2D OpenGL. Il grosso punto sbagliato è usare G11Ext per creare giochi. Dato che non RUOTA: D

annd gustato poi ho trovato questo tutorial da altri tutorial che ho trovato da youtube video link campione gioco lol:

a non confondere gli spettatori qui è

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

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

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

Annd, solo 15 minuti fa, ho scoperto il modo in cui posso RUOTARE, MUOVERE E RIDIMIZZARE le forme con i suoi sprite! ! ! hahah

Così come molti lettori stanno chiedendo dopo aver letto questo GRANDE tutorial su come spostare e ridimensionare e ruotare sprite.Così ho lavorato fuori qualche codice da questo pasticcio di esempi e tutorial:

Questa classe viene utilizzata per alcune manipolazioni di vertice

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);   
    } 
} 

e questa classe viene utilizzata per disegnare la forma con struttura in grado di muoversi rotazione e della posizione

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 !!! 

    } 
} 

e la cosa principale come impostare telecamera in modo che 1 opengl unità == 1 pixel gustato come caricare texture

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; 
    } 

} 

e alcuni utilizzo

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); 
    } 

} 

la cosa principale qui ho voluto implementare e implementato è che l'asse X e Y sono come in tela:

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

I ll scrivere alcuni tutorial completo dopo questo e mi piace annunciare che ho raggiunto tutti gli obiettivi che volevo raggiungere, vale a dire: asse X in alto, asse Y a sinistra, unità opengl = pixel, imposta dimensioni dell'oggetto in pixel, ruota oggetto, sposta oggetto tutto in pixel. ora i ll gestire l'animazione sprite e li facciamo in classi più fini e thats la nuova base quadro OpenGL 2D gioco ...

scoprire questa funzione mi ha aiutato a tutorial http://www.morrowland.com/apron/tutorials/gl/gl_matrix.php

Tante grazie a questo blog per me sottolineare la unico vero modo ...

+1 Android più semplice motore di opengl gioco 2d in 1 settimana ...

felice mind blowing ...

: P

Edit: Dopo anni ho un bel quadro https://github.com/hazardland/game.android utilizzando concetti descritti qui e gioco campione con eventuali esempi di utilizzo quadro qui https://github.com/hazardland/ferry.android (vista schermi sul mercato https://play.google.com/store/apps/details?id=hazardland.borani)

+3

sento il tuo fratello dolore. – torger

+0

passate molte volte da quando ho creato framework opengl dopo https: //github.com/hazardland/hazardland ... : P – BIOHAZARD

+0

Un gioco 2D semplice, ma completo, con OpenGL ES 2.0: http://code.google .com/p/android-breakout /. Questo fa alcune scelte differenti con il resto assi e unità pixel, ma indica dove sono prese le decisioni e perché. – fadden

Problemi correlati