找不到适用于不使用GL11Ext进行绘图的android 2d opengl sprite类的示例 [英] Can't find suitable example for android 2d opengl sprite class which does not use GL11Ext for drawing

查看:98
本文介绍了找不到适用于不使用GL11Ext进行绘图的android 2d opengl sprite类的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如SpriteMethodTest所说的,有很多绘制精灵的方法. 首先,我尝试使用canvas并遇到一些性能问题.接下来,我决定学习opengl.我使用GL11Ext扩展程序取得了我的第一个成就.但是,默认情况下您知道,当绘制纹理时,它们会翻转并且x和y轴在设备屏幕的左下角(在横向模式下)为零,这与画布不同,但是您不能旋转精灵./p>

然后,我尝试使用GLU外观影响此轴视图,但没有效果. 接下来,我想旋转精灵,但正如他们所说,由于GL11Ext,我没有任何作用.

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

  1. 使用哪种方法对子画面实现缩放,旋转和弹跳效果,以及在旧的甜味方式中查找X和Y轴[[0,0]在景观模式中居于左上]?**

  2. 在宇宙中是否有某些SPRITE类示例,仅使用一种良好的SPRITE渲染方式? (SpriteMethodTest让我很困惑)

解决方案

EVRIKA !!!

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

Web上到处都是OpenGL教程,里面满是垃圾,其中许多未完成,并且许多导致错误的2D OpenGL游戏引擎实现方法.最大的错误是使用G11Ext制作游戏.当它们不旋转时:D

然后我从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/

仅仅15分钟前,我就发现了一种方法,可以用其精灵旋转,移动和调整形状! ! !哈哈

因此,许多读者在阅读了本伟大的教程后都在问,如何移动,调整大小和旋转精灵.因此,我从这些混乱的示例和教程中得出了一些代码:

此类用于一些顶点操作

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像素,以及如何加载纹理

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/围裙/教程/gl/gl_matrix.php

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

在1周内为+1 android simpliest 2d opengl游戏引擎...

开心的心情...

:P

一年之后,我有了一个不错的框架 https://github.com/hazardland/game.android 使用此处介绍的概念以及此处带有任何可能的框架用法示例的示例游戏 https://play.google.com/store/apps/details?id=hazardland.borani )

As SpriteMethodTest says there are many ways for drawing sprites. Firstly, I tried canvas, and had some performance problems. Next, I decided to learn opengl. I made my first achievements using GL11Ext extension. However, you know by default, that when you draw textures they become flipped and the x and y axis have zero on the bottom left corner of the device screen (in landscape mode), not like the canvas, but you cannot rotate sprites.

Then, I tried to affect this axis view using GLU look at, but it had no effect. Next, I wanted to rotate my sprites, but I had no effect due to the GL11Ext, as they say.

So I have major stackoverflow condition now and the basic question is:

  1. Which method to use to achieve ZOOMING, ROTATING and BOUNCING effects on sprites and for LOOKING AT X AND Y AXIS IN OLD SWEET WAY [(0,0) IS ON TOP LEFT IN LANDSCAPE MODE] ?**

  2. AND IS THERE SOME SPRITE CLASS EXAMPLE IN THE UNIVERSE WHICH USES ONLY 1 GOOD WAY OF SPRITE RENDERING ? (SpriteMethodTest confuses me much)

解决方案

EVRIKA !!!

I was allmost killing myself ! after 3 days of leaving Canvas and learning OpenGL methods to implement game engine.

The web is full of OpenGL tutorials full of trash and many of them are unfinished and many of them lead to wrong way for 2D OpenGL game engine implementation methotds. The big wrong point is using G11Ext for making games. AS THEY DONT ROTATE : D

Annd annd then i found this tutorial from other tutorial which i found from youtube game sample video link lol:

not to confuse viewers here is

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

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

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

Annd just 15 minutes ago I discovered the way I can ROTATE, MOVE AND RESIZE shapes with its sprites ! ! ! hahah

So as many of readers are asking after reading this GREAT tutorial how to move and resize and rotate sprites. So i worked out some code from this mess of examples and tutorials:

This class is used for some vertex manipulations

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

and this class is used for drawing shape with texture able to move rotate and position

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

    }
}

and the main thing how to set camera so that 1 opengl unit == 1 pixel annd how to load textures

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

}

and some usage

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

}

the main thing here i wanted to implement and implemented is that the X and Y axis are like in canvas:

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

I ll write some full tutorial after this and I like to announce that i achieved all goals i wanted to achieve i.e: X axis on top, Y axis on left, opengl unit = pixel, set size of object in pixels, rotate object, move object everything in pixels. now i ll handle animating sprites and make them in finer classes and thats the new 2d opengl game framework basis...

discovering this functions helped me tutorial http://www.morrowland.com/apron/tutorials/gl/gl_matrix.php

So many thanks to this blog for pointing me out the only true way...

+1 android simpliest 2d opengl game engine in 1 week...

happy mind blowing...

:P

Edit: After year I have a nice framework https://github.com/hazardland/game.android using concepts described here and sample game with any possible framework usage examples here https://github.com/hazardland/ferry.android (view screens on market https://play.google.com/store/apps/details?id=hazardland.borani)

这篇关于找不到适用于不使用GL11Ext进行绘图的android 2d opengl sprite类的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆