作者:whycloud 文章来源:http://www.j2medev.com/Article/ShowArticle.asp?ArticleID=663
在这里我们将向您展示在您的JSR184 MIDlet中怎样使用3D帖图
这是一个可以获得非常漂亮的效果和意想不到的性能的方法;窍门就是渲染你的3D world到一个Image2D对象,并且将这个Image2D对象作为帖图。这样一来帖图本身就是一个包含动画的3D场景。<wrap type="square"><img onmousewheel="return bbimg(this)" height="309" src="http://www.j2medev.com/Article/UploadFiles/200508/20050830125823685.gif" width="215" onload="javascript:resizepic(this)" border="0" alt=""></wrap>
<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_s1026" style="MARGIN-TOP: 0px; Z-INDEX: 1; LEFT: 0px; MARGIN-LEFT: -9pt; WIDTH: 161.25pt; POSITION: absolute; HEIGHT: 231.75pt; TEXT-ALIGN: left" type="#_x0000_t75"><p></p></shape>
在这个例子中,我们需要创建两个world对象。
第一个是用来渲染一个四棱锥到Image2D对象中;第二个用来渲染一个用刚刚建立的Image2D对象作为帖图的多边形
渲染四棱锥到Image2D
texg3d.bindTarget(sceneTexture); // 绑定一个Image2D图形对象
texg3d.render(texWorld); // 渲染四棱锥到这个Image2D
g3d.bindTarget(g); // 绑定Graphics 到g3d
g3d.render(world); // 渲染该 world
帖图的长宽必须是2的指数 (2, 4, 8, 16 ? 256).
sceneTexture = new Image2D(Image2D.RGB, 64, 64);
使用这个Iamge2D作为帖图并且赋给多边形
现在使用我们刚刚生成的Image2D创建一个帖图;并且设置该帖图到平面的 appearance。
Texture2D texture1 = new Texture2D(sceneTexture);
texture1.setBlending(Texture2D.FUNC_REPLACE);
texture1.setWrapping(Texture2D.WRAP_CLAMP, Texture2D.WRAP_CLAMP);
texture1.setFiltering(Texture2D.FILTER_NEAREST, Texture2D.FILTER_NEAREST);
appearance.setTexture(0, texture1);
mesh.setAppearance(0, appearance);
这样一来我们就把sceneTexture设置在了帖图上了,我们可以通过及时的改变texWorld来改变这个帖图,也就是说我们获得了一个动画帖图。
译者心得:
这篇文章介绍了一种非常有意思的特效果,作者很巧妙的通过绑定一个Image2D对象到Graphics3D上,并且将一个World对象渲染到了该Graphics3D上,从而获得了一个及时的动画帖图,这是一个很高明的做法。换一个简单的说法,就是作者在这里渲染了两个World一个渲染到帖图上一个渲染到屏幕,似乎这样解释更容易明白了。
这里既然是介绍帖图的那么,我就来个大家补充一些东西。
对于初学Mobile3D的人来说,及时生成模型的过程中有一个设置是比较难理解的
// The texture coordinates is scaled down to 0-1 values in the setTextCoords method
short []TEXTURES = new short[] {0, 255, 255, 255, 127, 0,
0, 255, 255, 255, 127, 0,
0, 255, 255, 255, 127, 0,
0, 255, 255, 255, 127, 0,
0, 0, 255, 0, 255, 255,
0, 0, 255, 255, 0, 255};
TEXTURE_ARRAY = new VertexArray(TEXTURES.length / 2, 2, 2);
TEXTURE_ARRAY.set(0, TEXTURES.length / 2, TEXTURES);
vertexBuffer.setTexCoords(0, TEXTURE_ARRAY, (<chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="1" unitname="F" w:st="on">1.0f</chmetcnv>/<chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="255" unitname="F" w:st="on">255.0f</chmetcnv>), null);
vertexBuffer.setTexCoords(1, TEXTURE_ARRAY, (<chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="1" unitname="F" w:st="on">1.0f</chmetcnv>/<chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="255" unitname="F" w:st="on">255.0f</chmetcnv>), null);
这里TEXTURES其实设置的是每个顶点对应在帖图中的位置,也许有人在这里要问,帖图的尺寸是1为单位的,而这里这个数组只可以设置成short类型的,那么我如何获得帖图中的某个位置呢?在这个例子中就给出解决的方法。在设置顶点对应帖图坐标的时候在这个例子中给出的是大于1的正整数。而在设置多边形的时候vertexBuffer.setTexCoords(0, TEXTURE_ARRAY, (<chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="1" unitname="F" w:st="on">1.0f</chmetcnv>/<chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="255" unitname="F" w:st="on">255.0f</chmetcnv>), null);这个方法中的第三个参数恰恰就是设置帖图的比例尺,这样一来就需要用数组中的值乘以该比例,获得是该顶点的帖图坐标。
这里调用两次vertexBuffer.setTexCoords(0, TEXTURE_ARRAY, (<chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="1" unitname="F" w:st="on">1.0f</chmetcnv>/<chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="255" unitname="F" w:st="on">255.0f</chmetcnv>), null);其实是设置两个帖图信息,为什么要设置两次?这和Graphics3D有关。具体细节这里不介绍了,但要告诉你的是,这里你把第二此调用去掉也可以运行该例程。