Working with textures in android's OpenGL/ES.

本文介绍了一个用于Android应用程序的GLTextures类,该类简化了纹理加载过程,并提供了使用多个纹理的方法。通过实例展示了如何创建和使用纹理,以及如何优化加载过程。

http://xxw8393.blog.163.com/blog/static/37256834200992714036910/ 

 

As you may recall, I use textures to draw the moon backdrop for my android application, monolithandroid. Originally, I only used one texture and I used some code from Ed Burnette's forthcoming book "Hello, Android". However, I decided to add more textures in order to improve the game's graphics. So I created a class named GLTextures, that can be used to make working with multiple textures easier.
Here's the code:

package org.teacake.monolith.apk;
import javax.microedition.khronos.opengles.*;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.lang.Integer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
public class GLTextures {
public GLTextures(GL10 gl,Context context)
{
this.gl = gl;
this.context = context;
this.textureMap = new java.util.HashMap ();
}

public void loadTextures()
{
int[] tmp_tex = new int[textureFiles.length];
gl.glGenTextures(textureFiles.length, tmp_tex, 0);
textures = tmp_tex;
for(int i=0;i
{
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), textureFiles[i]);
ByteBuffer bb = extract(bmp);
// Get a new texture name
// Load it up
this.textureMap.put(new Integer(textureFiles[i]),new Integer(i));
int tex = tmp_tex[i];
int width = bmp.getWidth();
int height = bmp.getHeight();
gl.glBindTexture(GL10.GL_TEXTURE_2D, tex);
gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA,width, height, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

}
}
public void setTexture(int id)
{
try
{
int textureid = this.textureMap.get(new Integer(id)).intValue();
gl.glBindTexture(GL10.GL_TEXTURE_2D, this.textures[textureid]);

}
catch(Exception e)
{
return;
}
}
private static ByteBuffer extract(Bitmap bmp)
{
ByteBuffer bb = ByteBuffer.allocateDirect(bmp.height() * bmp.width() * 4);
bb.order(ByteOrder.BIG_ENDIAN);
IntBuffer ib = bb.asIntBuffer();
// Convert ARGB -> RGBA
for (int y = bmp.height() - 1; y > -1; y--)
{

for (int x = 0; x < bmp.width(); x++)
{
int pix = bmp.getPixel(x, bmp.getHeight() - y - 1);
int alpha = ((pix >> 24) & 0xFF);
int red = ((pix >> 16) & 0xFF);
int green = ((pix >> 8) & 0xFF);
int blue = ((pix) & 0xFF);

// Make up alpha for interesting effect

//ib.put(red << 24 | green << 16 | blue << 8 | ((red + blue + green) / 3));
ib.put(red << 24 | green << 16 | blue << 8 | alpha);
}
}
bb.position(0);
return bb;
}



public void add(int resource)
{
if(textureFiles==null)
{
textureFiles = new int[1];
textureFiles[0]=resource;
}
else
{
int[] newarray = new int[textureFiles.length+1];
for(int i=0;i
{
newarray[i]=textureFiles[i];
}
newarray[textureFiles.length]=resource;
textureFiles = newarray;
}
}
private java.util.HashMap textureMap;
private int[] textureFiles;
private GL10 gl;
private Context context;
private int[] textures;
}


So, what can you use this code for?
This code enables you to load resources as textures.
At first you create a GLTextures object like this:

GLTextures textures = new GLTextures(gl, context);

Then you add the resource images you want to use as textures:

this.textures.add(R.drawable.moon);
this.textures.add(R.drawable.earth);

And then you loadup the textures:

textures.loadTextures();

When you want to use the texture in your OpenGL code, you can use:

textures.setTexture(R.drawable.moon);
.
.
//OpenGL drawing code
.
.

Actually the setTexture() method calls glBindTexture() which sets the current texture to the correct one.
Of course, you have to enable textures in your OpenGL code in order to do that! If you want more details and examples study the classes GLThread, Square and GLTextures found at
http://code.google.com/p/monolithandroid
As you may find, loading pictures and converting them to textures can take a lot of time, so one improvement that you can make is to create a caching scheme. You can, for example, store the textures in a file, after converting them from a picture for the first time, so the next time the application is run, it will load the textures without having to do a conversion. Happy hacking!

Posted by Tasos Kleisas at 6:24 AM

 

### 解决 @babel/runtime-corejs3 和 core-js-stable 的依赖未找到问题 在使用 Babylon.js 时,如果遇到 `@babel/runtime-corejs3` 和 `core-js/stable` 的依赖缺失问题,可以通过以下方法解决。 #### 配置 Babel 使用 `@babel/runtime-corejs3` 确保在项目中安装了正确的依赖项。需要安装以下包: ```bash npm install --save-dev @babel/core @babel/preset-env npm install --save @babel/runtime-corejs3 core-js ``` 接下来,在 `babel.config.js` 中正确配置 `preset-env`,以支持按需加载 Polyfill 并指定核心库版本为第三代[^3]。 ```javascript module.exports = { presets: [ [ '@babel/preset-env', { useBuiltIns: 'usage', // 按需加载 Polyfill corejs: 3, // 指定使用第三代 core-js }, ], ], }; ``` #### 处理 Babylon.js 的兼容性问题 Babylon.js 可能依赖某些现代 JavaScript 特性,而这些特性在旧浏览器中可能不受支持。通过设置 `useBuiltIns: 'usage'`,可以确保仅在需要时引入相应的 Polyfill。如果 Babylon.js 或其依赖已经自带了 Polyfill,则应避免重复引入,以免导致冲突或错误[^1]。 如果仍然出现依赖缺失问题,可以在入口文件中显式引入必要的 Polyfill: ```javascript import 'core-js/stable'; import 'regenerator-runtime/runtime'; ``` 此方法适用于 `useBuiltIns: 'entry'` 的场景,但需要注意这会增加打包体积[^1]。 #### Webpack 配置示例 以下是完整的 Webpack 配置示例,确保 `babel-loader` 正确处理 Babylon.js 相关代码: ```javascript const path = require('path'); module.exports = { entry: './src/index.js', output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist'), }, module: { rules: [ { test: /\.js$/, include: [ path.resolve(__dirname, 'src'), /node_modules\/libgeo3dviewer\/node_modules\/@babylonjs/, ], use: { loader: 'babel-loader', options: { presets: [ [ '@babel/preset-env', { useBuiltIns: 'usage', corejs: 3, }, ], ], cacheDirectory: true, }, }, }, ], }, }; ``` #### 常见问题及解决方案 - **问题:为什么使用 `@babel/runtime-corejs3` 后仍然报错?** - 确保已安装所有必要依赖,包括 `core-js`。此外,检查是否正确配置了 `babel.config.js`,特别是 `useBuiltIns` 和 `corejs` 参数[^3]。 - **问题:如何减少打包体积?** - 使用 `useBuiltIns: 'usage'` 而非 `entry`,可以按需加载 Polyfill,从而减少不必要的代码引入。 - **问题:Babylon.js 是否自带 Polyfill?** - Babylon.js 本身并不包含 Polyfill。如果目标环境不支持某些特性,则需要通过 Babel 引入相应的 Polyfill[^2]。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值