无法找到适合Android 2d opengl sprite类的例子,它不使用GL11Ext进行绘图

BIO*_*ARD 10 android 2d opengl-es sprite

正如SpriteMethodTest所说,绘制精灵的方法有很多种.首先,我尝试了帆布,并有一些性能问题.比我决定学习opengl.我使用GL11Ext扩展我的第一个成就.但是你知道默认情况下,当你绘制纹理时,它们被翻转,而x和y轴在设备屏幕的左下角(横向模式)为零,而不像画布,你不能旋转精灵.

比我试图用GLU看看影响这个轴视图.但没效果.

比我想旋转我的精灵并且没有像他们说的那样影响GL11Ext的原因.

所以我现在有主要的stackoverflow条件,基本问题是:

1.使用哪种方法来实现对精灵的变焦,旋转和弹奏效果以及在旧的SWEET方式中查看X和Y轴[(0,0)在风景模式中的左上方]?

2.在宇宙中,有哪些类似于SPRING类的例子只使用了一种良好的SPRING RENDERING方式?(SpriteMethodTest让我很困惑)

BIO*_*ARD 10

EVRIKA !!!

我几乎要杀了自己!离开Canvas 3天后学习OpenGL方法来实现游戏引擎.

网络上充满了垃圾的OpenGL教程,其中很多都未完成,其中许多导致2D OpenGL游戏引擎实现的错误方式.最重要的一点是使用G11Ext制作游戏.因为他们不能旋转:D

然后我从其他教程中找到了这个教程,我从youtube游戏示例视频链接中找到lol:

这里不要混淆观众

第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/

仅仅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);        
    }
}
Run Code Online (Sandbox Code Playgroud)

此类用于绘制具有能够移动旋转和位置的纹理的形状

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

    }
}
Run Code Online (Sandbox Code Playgroud)

而主要的事情是如何设置相机使1个opengl单位== 1像素和如何加载纹理

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

}
Run Code Online (Sandbox Code Playgroud)

和一些用法

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

}
Run Code Online (Sandbox Code Playgroud)

我想要实现和实现的主要内容是X和Y轴就像在canvas中:

(0,0)
 --------------------------------- X axis
|
|
|
|
|
|
|
|
Y axis
Run Code Online (Sandbox Code Playgroud)

之后我会写一些完整的教程,我想宣布我实现了我想达到的所有目标,即:X轴在顶部,Y轴在左边,opengl单位=像素,设置对象的大小,以像素为单位,旋转对象,移动对象以像素为单位.现在我将处理动画精灵并使它们更精细的类,这就是新的2d opengl游戏框架基础......

发现这些功能帮助我教程http://www.morrowland.com/apron/tutorials/gl/gl_matrix.php

非常感谢这个博客指出我唯一真实的方式......

1周内+1机器人最简单的2d opengl游戏引擎......

快乐的心灵吹......

:P

编辑:年后我有一个很好的框架https://github.com/hazardland/game.android使用这里描述的概念和示例游戏与任何可能的框架用法示例https://github.com/hazardland/ferry.android (在市场上查看屏幕https://play.google.com/store/apps/details?id=hazardland.borani)

  • 我觉得你的痛苦兄弟. (3认同)