游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2189|回复: 3

恳求名词解释及使用:网格、节点、模型、选择器、Animat

[复制链接]

12

主题

21

帖子

32

积分

注册会员

Rank: 2

积分
32
发表于 2006-2-19 10:08:00 | 显示全部楼层 |阅读模式
网格(mesh)、节点(node)、模型(model)、选择器(selector)、Animator

俺是游戏编程初学者,正在看irrlicht(德国鬼火)引擎的示例代码,可是好几天了还没搞懂这个例子,我想是一些基本的东西还没理解吧。

附:冲突检测的示例代码



  1. Tutorial 7. Collision detection and response
  2. In this tutorial, I will show how to collision detection with the Irrlicht Engine. I will describe 3 methods: Automatic collision detection for moving through 3d worlds with stair climbing and sliding, manual triangle picking and manual scene node picking.

  3. The program which is described here will look like this:





  4. Lets start!
  5. To start, we take the program from tutorial 2, which loaded and displayed a quake 3 level. We will use the level to walk in it and to pick triangles from it. In addition we'll place 3 animated models into it for scene node picking. The following code starts up the engine and loads a quake 3 level. I will not explain it, because it should already be known from tutorial 2.

  6. #include <irrlicht.h>
  7. #include <iostream>
  8. using namespace irr;

  9. #pragma comment(lib, "Irrlicht.lib")

  10. int main()
  11. {
  12.   // let user select driver type
  13.   video::E_DRIVER_TYPE driverType;  printf("Please select the driver you want for this example:\n"\    " (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n"\    " (d) Software Renderer\n (e) Apfelbaum Software Renderer\n"\    " (f) NullDevice\n (otherKey) exit\n\n");  char i;  std::cin >> i;  switch(i)  {  case 'a': driverType = video::EDT_DIRECT3D9;break;  case 'b': driverType = video::EDT_DIRECT3D8;break;  case 'c': driverType = video::EDT_OPENGL;   break;  case 'd': driverType = video::EDT_SOFTWARE; break;  case 'e': driverType = video::EDT_SOFTWARE2;break;    case 'f': driverType = video::EDT_NULL;     break;  default: return 0;  }       
  14.   // create device
  15.   IrrlichtDevice *device = createDevice(driverType,
  16.      core::dimension2d<s32>(640, 480), 16, false);
  17.   if (device == 0)    return 1; // could not create selected driver.  video::IVideoDriver* driver = device->getVideoDriver();  scene::ISceneManager* smgr = device->getSceneManager();  device->getFileSystem()->addZipFileArchive      ("../../media/map-20kdm2.pk3");

  18.        
  19.         scene::IAnimatedMesh* q3levelmesh = smgr->getMesh("20kdm2.bsp");
  20.         scene::ISceneNode* q3node = 0;
  21.        
  22.         if (q3levelmesh)
  23.                 q3node = smgr->addOctTreeSceneNode(q3levelmesh->getMesh(0));



  24. So far so good, we've loaded the quake 3 level like in tutorial 2. Now, here comes something different: We create a triangle selector. A triangle selector is a class which can fetch the triangles from scene nodes for doing different things with them, for example collision detection. There are different triangle selectors, and all can be created with the ISceneManager. In this example, we create an OctTreeTriangleSelector, which optimizes the triangle output a little bit by reducing it like an octree. This is very useful for huge meshes like quake 3 levels.
  25. Afte we created the triangle selector, we attach it to the q3node. This is not necessary, but in this way, we do not need to care for the selector, for example dropping it after we do not need it anymore.

  26. scene::ITriangleSelector* selector = 0;
  27.        
  28.         if (q3node)
  29.         {               
  30.                 q3node->setPosition(core::vector3df(-1370,-130,-1400));

  31.                 selector = smgr->createOctTreeTriangleSelector(
  32.             q3levelmesh->getMesh(0), q3node, 128);
  33.                 q3node->setTriangleSelector(selector);
  34.                 selector->drop();
  35.         }


  36. We add a first person shooter camera to the scene for being able to move in the quake 3 level like in tutorial 2. But this, time, we add a special animator to the camera: A Collision Response animator. This thing modifies the scene node to which it is attached to in that way, that it may no more move through walls and is affected by gravity. The only thing we have to tell the animator is how the world looks like, how big the scene node is, how gravity and so on. After the collision response animator is attached to the camera, we do not have to do anything more for collision detection, anything is done automaticly, all other collision detection code below is for picking. And please note another cool feature: The collsion response animator can be attached also to all other scene nodes, not only to cameras. And it can be mixed with other scene node animators. In this way, collision detection and response in the Irrlicht
  37. engine is really, really easy.
  38. Now we'll take a closer look on the parameters of createCollisionResponseAnimator(). The first parameter is the TriangleSelector, which specifies how the world, against collision detection is done looks like. The second parameter is the scene node, which is the object, which is affected by collision detection, in our case it is the camera. The third defines how big the object is, it is the radius of an ellipsoid. Try it out and change the radius to smaller values, the camera will be able to move closer to walls after this. The next parameter is the direction and speed of gravity. You could set it to (0,0,0) to disable gravity. And the last value is just a translation: Without this, the ellipsoid with which collision detection is done would be around the camera, and the camera would be in the middle of the ellipsoid. But as human beings, we are used to have our eyes on top of the body, with which we collide with our world, not in the middle of it. So we place the scene node 50 units over the center of the ellipsoid with this parameter. And that's it, collision detection works now.


  39.         scene::ICameraSceneNode* camera =                         camera = smgr->addCameraSceneNodeFPS(0,100.0f,300.0f);
  40.         camera->setPosition(core::vector3df(-100,50,-150));

  41.         scene::ISceneNodeAnimator* anim =    smgr->createCollisionResponseAnimator(
  42.                 selector, camera, core::vector3df(30,50,30),
  43.                 core::vector3df(0,-3,0),
  44.                 core::vector3df(0,50,0));
  45.         camera->addAnimator(anim);
  46.         anim->drop();


  47. Because collision detection is no big deal in irrlicht, I'll describe how to do two different types of picking in the next section. But before this, I'll prepare the scene a little. I need three animated characters which we
  48. could pick later, a dynamic light for lighting them, a billboard for drawing where we found an intersection, and, yes, I need to get rid of this mouse cursor. :)

  49.         // disable mouse cursor

  50.         device->getCursorControl()->setVisible(false);

  51.         // add billboard

  52.         scene::IBillboardSceneNode * bill = smgr->addBillboardSceneNode();
  53.         bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR );
  54.         bill->setMaterialTexture(0, driver->getTexture(         "../../media/particle.bmp"));
  55.         bill->setMaterialFlag(video::EMF_LIGHTING, false);
  56.         bill->setSize(core::dimension2d<f32>(20.0f, 20.0f));

  57.         // add 3 animated faeries.

  58.         video::SMaterial material;
  59.         material.Texture1 = driver->getTexture(         "../../media/faerie2.bmp");
  60.         material.Lighting = true;

  61.         scene::IAnimatedMeshSceneNode* node = 0;
  62.         scene::IAnimatedMesh* faerie = smgr->getMesh(        "../../media/faerie.md2");

  63.         if (faerie)
  64.         {
  65.                 node = smgr->addAnimatedMeshSceneNode(faerie);
  66.                 node->setPosition(core::vector3df(-70,0,-90));
  67.                 node->setMD2Animation(scene::EMAT_RUN);
  68.                 node->getMaterial(0) = material;

  69.                 node = smgr->addAnimatedMeshSceneNode(faerie);
  70.                 node->setPosition(core::vector3df(-70,0,-30));
  71.                 node->setMD2Animation(scene::EMAT_SALUTE);
  72.                 node->getMaterial(0) = material;

  73.                 node = smgr->addAnimatedMeshSceneNode(faerie);
  74.                 node->setPosition(core::vector3df(-70,0,-60));
  75.                 node->setMD2Animation(scene::EMAT_JUMP);
  76.                 node->getMaterial(0) = material;
  77.         }

  78.         material.Texture1 = 0;
  79.         material.Lighting = false;

  80.         // Add a light

  81.         smgr->addLightSceneNode(0, core::vector3df(-60,100,400),
  82.                 video::SColorf(1.0f,1.0f,1.0f,1.0f),
  83.                 600.0f);


  84. For not making it to complicated, I'm doing picking inside the drawing loop. We take two pointers for storing the current and the last selected scene node and start the loop.

  85.         scene::ISceneNode* selectedSceneNode = 0;
  86.         scene::ISceneNode* lastSelectedSceneNode = 0;

  87.        
  88.         int lastFPS = -1;

  89.         while(device->run())  if (device->isWindowActive())
  90.         {
  91.                 driver->beginScene(true, true, 0);

  92.                 smgr->drawAll();


  93. After we've drawn the whole scene whit smgr->drawAll(), we'll do the first picking: We want to know which triangle of the world we are looking at. In addition, we want the exact point of the quake 3 level we are looking at. For this, we create a 3d line starting at the position of the camera and going through the lookAt-target of it. Then we ask the collision manager if this line collides with a triangle of the world stored in the triangle selector. If yes, we draw the 3d triangle and set the position of the billboard to the intersection point.

  94.                 core::line3d<f32> line;
  95.                 line.start = camera->getPosition();
  96.                 line.end = line.start +
  97.          (camera->getTarget() - line.start).normalize() * 1000.0f;

  98.                 core::vector3df intersection;
  99.                 core::triangle3df tri;

  100.                 if (smgr->getSceneCollisionManager()->getCollisionPoint(
  101.                         line, selector, intersection, tri))
  102.                 {
  103.                         bill->setPosition(intersection);
  104.                                
  105.                         driver->setTransform(video::ETS_WORLD, core::matrix4());
  106.                         driver->setMaterial(material);
  107.                         driver->draw3DTriangle(tri, video::SColor(0,255,0,0));
  108.                 }


  109. Another type of picking supported by the Irrlicht Engine is scene node picking based on bouding boxes. Every scene node has got a bounding box, and because of that, it's very fast for example to get the scene node which the camera looks
  110. at. Again, we ask the collision manager for this, and if we've got a scene node, we highlight it by disabling Lighting in its material, if it is not the billboard or the quake 3 level.

  111.                 selectedSceneNode = smgr->getSceneCollisionManager()->
  112.           getSceneNodeFromCameraBB(camera);

  113.                 if (lastSelectedSceneNode)
  114.                         lastSelectedSceneNode->setMaterialFlag(
  115.                 video::EMF_LIGHTING, true);

  116.                 if (selectedSceneNode == q3node ||
  117.            selectedSceneNode == bill)
  118.                         selectedSceneNode = 0;

  119.                 if (selectedSceneNode)
  120.                         selectedSceneNode->setMaterialFlag(
  121.                video::EMF_LIGHTING, false);

  122.                 lastSelectedSceneNode = selectedSceneNode;


  123. That's it, we just have to finish drawing.

  124.                 driver->endScene();

  125.                 int fps = driver->getFPS();

  126.                 if (lastFPS != fps)
  127.                 {
  128.                   core::stringw str = L"Collision detection example - Irrlicht Engine [";                  str += driver->getName();                  str += "] FPS:";                  str += fps;                  device->setWindowCaption(str.c_str());                  lastFPS = fps;                }
  129.         }

  130.         device->drop();
  131.        
  132.         return 0;
  133. }
复制代码

14

主题

245

帖子

256

积分

中级会员

Rank: 3Rank: 3

积分
256
QQ
发表于 2006-2-19 23:47:00 | 显示全部楼层

Re:恳求名词解释及使用:网格、节点、模型、选择器、Ani

你都知道他们的中文名了还有什么好解释的。

6

主题

42

帖子

42

积分

注册会员

Rank: 2

积分
42
QQ
发表于 2006-2-20 10:18:00 | 显示全部楼层

Re:恳求名词解释及使用:网格、节点、模型、选择器、Ani

网格(mesh)
   模型的顶点、骨骼信息,表示模型资源

节点(node)
   是Irr中场景管理采用的SceneNode结构,这种结构将场景中的大小部件或者功能性模块均抽象为节点,甚至包括Camera、粒子系统等。这样可以相当容易在BSP或者空分查找的应用方面使用,效率也不错,是一种比较成功的抽象,可以说它是Irr的精髓。

模型(model)
    支持3D模型显示的一种SceneNode,分几种类型,比如静态模型、骨架动画模型……,以一种mesh作为资源

选择器(selector)
     Irr实现的一种三角形选择器,应该是采用eye ray trace 方式,具体你自己看一下。功能就是做场景多边形检测、应该也可以用于碰撞检测

Animator
     用作SceneNode的动画,可以有多种方式的Animator,这也是Irr设计比较好的地方

12

主题

21

帖子

32

积分

注册会员

Rank: 2

积分
32
 楼主| 发表于 2006-2-22 23:14:00 | 显示全部楼层

Re:恳求名词解释及使用:网格、节点、模型、选择器、Ani

谢谢!!!!!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-23 17:40

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表