找不到不使用 GL11Ext 进行绘图的 android 2d opengl sprite 类的合适示例

2024-04-28

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

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

所以我现在有严重的 stackoverflow 情况,基本问题是:

  1. 使用哪种方法来实现精灵上的缩放、旋转和弹跳效果以及以旧式甜蜜方式查看 X 轴和 Y 轴 [(0,0) 在横向模式下位于左上角]?**

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


埃里卡!!!

我差点自杀了!离开 Canvas 并学习 OpenGL 方法来实现游戏引擎 3 天后。

网络上充斥着充满垃圾的OpenGL教程,其中许多都未完成,其中许多导致2D OpenGL游戏引擎实现方法的错误。最大的错误点是使用G11Ext来制作游戏。因为它们不旋转:D

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

这里不要让观众感到困惑

第1章:http://obviam.net/index.php/opengl-es-with-android-switching-from-canvas-to-opengl/ 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/ 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/ 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/apron/tutorials/gl/gl_matrix.php http://www.morrowland.com/apron/tutorials/gl/gl_matrix.php

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

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

开心得让人心潮澎湃...

:P

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

找不到不使用 GL11Ext 进行绘图的 android 2d opengl sprite 类的合适示例 的相关文章

随机推荐

  • 如何使用 PHP 制作 pdf 文件

    如何用 PHP 制作 PDF 文件 我想要制作的是学生名单 所以我想查询数据库 获取信息 并在用户单击 生成学生列表 时将其以 PDF 形式提供给用户 你有两个不错的选择 首先是用于操作 pdf 的标准 php 库 https www ph
  • C#等待串口数据

    我试图通过 C 应用程序从指纹扫描仪获取数据 但在指纹发送之前 我的整个代码都会执行 我尝试使用延迟功能System Threading Thread Sleep 1000 因此它可以在下一步执行之前获取数据 但这一切似乎都是徒劳的 任何人
  • CSS 中的边框图像 svg(颜色)样式?

    是否可以从CSS样式填充自定义边框图像 我应该使用哪个属性 E g border image source url assets images dots new svg border color red doesn t work fill
  • 动态创建多个上传文件

    我想知道是否有人知道动态创建上传表单的最佳方法 这就是我想要实现的目标 下面显示的代码允许一次上传 我想要一个按钮 按下该按钮后 应添加另一种形式用于文件上传 因此 如果我想上传 假设有 7 个文件 我想按按钮 7 次来创建这些上传表单 每
  • 我无法在 docker 中安装 opencv-contrib-python

    我尝试安装opencv contrib python但我无法让它在 docker 上工作 它说找不到满足 opencv contrib python 要求的版本 I tried pip install opencv contrib pyth
  • C# 从视频文件的一部分中提取帧

    使用 AForge ffmpeg 包装器 您可以使用 VideoFileReader 类从视频中提取帧并将其保存为位图 请参阅以下示例 提取 avi 文件的帧 https stackoverflow com questions 178256
  • 为什么sql表名中通常使用下划线而不是驼峰式大小写[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 接口实现中的非接口方法

    我有一个定义方法的接口 我有一个结构实施这个界面 在其中 我实现了该接口中的方法 并且还定义了其他方法 例如 package main import fmt type Animal interface MakeNoise type Dog
  • Azure 函数 - 如何读取表单数据

    如何阅读表单数据 in Azure 函数 我尝试了多种方法 但总是出现错误 例如 using System Net public static async Task
  • HttpCookie 和 Cookie 的区别?

    所以我很困惑 因为 msdn 和其他教程告诉我使用 HttpCookies 通过 Response Cookies Add cookie 添加 cookie 但这就是问题所在 Response Cookies Add 只接受 Cookie
  • 没有名为“_pywrap_tensorflow_internal”的模块

    在尝试验证tensorflow gpu的安装时 在尝试执行 import tensorflow as tf 时出现ImportError 我在 Windows 7 上使用 Quadro K620 Tensorflow 是使用 pip 安装的
  • 如何在 Xcode 7 Beta 5 中安装 iOS 7 及更高版本的模拟器?

    我想在 iOS 7 及更高版本上测试我的应用程序 在这些操作系统中测试我的应用程序的唯一方法是使用模拟器 但是将 Xcode 升级到 7 Beta 后 我看到了 iOS 8 1 8 2 8 3 但收到一条错误消息 指出未找到它们 iOS7
  • 使用 PHP 将数据添加到 json

    我使用 json decode 创建一个 json 对象 在浏览了一些元素之后 我想向其中添加子元素 我该怎么做呢 取决于您传递给的选项json decode http php net manual en function json dec
  • 在 Swift 4 IOS 中将 XML 数据获取到 String 中

    我有一个 XML 字符串格式 我想使用 Swift 4 从它获取单个值到字符串变量 我的数据如下 let myString Adasdnajinasdshabjdbaiusd Encrypted Text Sample let MyResu
  • 分段表单上传和 NSURLSession.uploadTaskWithRequest 之间的区别

    来自网络编程领域 我非常喜欢使用多部分表单请求来上传文件 然而 在iOS中 我们有一个东西叫做NSURLSession用方法uploadTaskWithRequest 这似乎是调用进行图像上传等操作的方法 您能解释一下这两种方法之间的区别吗
  • XMLHttpRequest 无法加载 ZScaler 的问题

    我在 EC2 实例中托管了一个网站 并使用以下命令访问该页面http ec2 网址 该页面向同一实例上托管的另一个 Web 应用程序发出 ajax 请求 如果我访问通过 ZScaler 代理的页面 我会得到XMLHttpRequest ca
  • 后视模式无效

    为什么这个正则表达式在 Python 中有效 但在 Ruby 中无效
  • 如何以编程方式设置(文本)视图属性?

    我拼命尝试以编程方式设置表中单元格的 TextView 属性 但无法使其工作 每当我设置布局属性时 该字段将不会出现 但不会给出任何错误或异常 我将其归结为这个简单的例子 package mmo application listpro im
  • 如何包装 UnityEngine.Debug.Log 但在单击时保留代码行

    我有这种代码 目的是包装UnityEngine Debug Log所以我也可以在生产中禁用它们 以便我可以稍后查找 过滤 using System public enum LogType DEBUG CRITICAL public clas
  • 找不到不使用 GL11Ext 进行绘图的 android 2d opengl sprite 类的合适示例

    正如 SpriteMethodTest 所说 绘制精灵的方法有很多种 首先 我尝试了canvas 并遇到了一些性能问题 接下来 我决定学习opengl 我使用 GL11Ext 扩展取得了第一个成就 但是 默认情况下您知道 当您绘制纹理时 它