Welcome![Sign In][Sign Up]
Location:
Search - mesh matrix

Search list

[DirextXXModel

Description: 利用矩阵变换实现在埸景中加载多个网格模型-matrix transform the scene to load more than King Mesh
Platform: | Size: 1438367 | Author: sdfkl | Hits:

[Internet-Network用D3D模拟地月系

Description:

 

 

 

  一、建立空窗体

  新建一个工程,添加引用,并导入名称空间。

  加入一个设备对象变量:

private Microsoft.DirectX.Direct3D.Device device = null;

  添加初始化图形函数,并在这里面对设备对象进行实例化:

public void InitializeGraphics()
{
 PresentParameters presentParams = new PresentParameters();
 presentParams.Windowed = true;
 presentParams.SwapEffect = SwapEffect.Flip;
 presentParams.AutoDepthStencilFormat = DepthFormat.D16;
 presentParams.EnableAutoDepthStencil = true;
 device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this,  CreateFlags.HardwareVertexProcessing, presentParams);
}

  当程序执行时,需要绘制场景,代码在这个函数里:

public void Render()

{
 // 清空设备,并准备显示下一帧。
 device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black , 1.0f, 0);
 // 设置照相机的位置
 SetupCamera();
 //开始场景
 device.BeginScene();
 if(meshLoaded)
 {
  mesh.Render(meshLoc);
 }
 device.EndScene();
 //显示设备内容。
 device.Present();
}

  设置照相机的位置:

private void SetupCamera()
{
 device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 1000.00f);
 device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f ,0.0f, 20.0f), new Vector3(0.0f,0.0f, 0.0f), new Vector3(0,1,0));
}

  现在改变主函数,调用我们写的初始化函数,并显示场景:

[STAThread]

static void Main()
{
 using (Form1 EarthForm = new Form1())
 {
  EarthForm.InitializeGraphics();
  EarthForm.Show();

  while(EarthForm.Created)
  {
   EarthForm.Render();
   Application.DoEvents();
  }
  EarthForm.Dispose();
}

  运行程序,会显示一个空的窗体。

  二、加入地球:

  在这一步里需创建一个3D网格对象,来作为要显示的地球,为此,在工程中新加入一个类Earth,此类可以包含所创建的网格对象的信息。

  加入一些相关变量,含义见注释:

public class Earth : BaseEarth
{
 private Material[] mMaterials; //保存材质
 private Texture[] mTextures; //保存纹理
 private Matrix locationOffset; //用来保存网格对象的相对位置
 private Mesh mMesh = null; //三角形网格对象
 private Device meshDevice; //需要显示在哪个设备上。
}

  在构造函数中,把Device设备拷贝到私有成员变量,这样就可以在这个类的其它方法内使用它,另外就是把位置变量进行赋值:

public Earth(ref Device device, Matrix location): base(ref device)
{
 meshDevice = device;
 locationOffset = location;
}

  下面这个函数是装入.X文件。

public bool LoadMesh(string meshfile)
{
 ExtendedMaterial[] mtrl;
 try
 {
  // 装载文件
  mMesh = Mesh.FromFile(meshfile, MeshFlags.Managed, meshDevice, out mtrl);
  // 如果有材质的话,装入它们
  if ((mtrl != null) && (mtrl.Length > 0))
  {
   mMaterials = new Material[mtrl.Length];
   mTextures = new Texture[mtrl.Length];

   // 得到材质和纹理

   for (int i = 0; i < mtrl.Length; i++)
   {
    mMaterials[i] = mtrl[i].Material3D;
    if ((mtrl[i].TextureFilename != null) && (mtrl[i].TextureFilename != string.Empty))

 

    {
     //前面得到的纹理的路径是相对路径,需要保存的是绝对路径,通过应用程序路径可以获得
     mTextures[i] = TextureLoader.FromFile(meshDevice, @"..\..\" + mtrl[i].TextureFilename);
    }
   }
  }
  return true;
 }
 catch
 {
  return false;
 }
}

  在这个方法内,使用Mesh.FromFile()这个方法,从给定的文件名中找到.X文件,并装入相关数据,一旦数据格式设置完成,可以从此文件中找到材质和贴图信息,并把它存放在数组中,并通过文件路径,得到纹理文件文件的路径,最后返回真值,如果整个过程出现错误,返回假值。

  下面这个Render()方法,是把此对象,即地球显示在设备对象上,此方法较简单,通过变形操作来得到网格对象的X,Y,Z坐标,接着设置网格对象的材质和纹理,最后,将每个材质和纹理应用到每个网格。

public void Render(Matrix worldTransform)
{
 /把位置变为世界坐标
 meshDevice.Transform.World = Matrix.Multiply(locationOffset, worldTransform);
 //绘制网格
 for (int i = 0; i < mMaterials.Length; i++)
 {
  meshDevice.Material = mMaterials[i];
  meshDevice.SetTexture(0, mTextures[i]);
  mMesh.DrawSubset(i);
 }
}

  现在回到窗体代码中,添加引用网格对象的相关变量:

private Earth mesh = null;
private Matrix meshLoc;
private bool meshLoaded = false;

  在图形初始化函数中,需要对网格对象进行初始化。加入下面的代码:

meshLoc = Matrix.Identity;
meshLoc.M41 = 2.0f;
mesh = new Earth(ref device, meshLoc);
if (mesh.LoadMesh(@"..\..\earth.x"))
{
 meshLoaded = true;
}

  代码第一句把网格对象的位置定为原点,接着偏移X轴2个单位,接下来从文件中得到此.X文件。如果成功设置,meshLoaded置为真。注意,这里有一个.X文件,在源代码中有此文件。

 


  在设置相机的函数中,加入一盏灯光:

device.Lights[0].Type = LightType.Directional;
device.Lights[0].Diffuse = Color.White;
device.Lights[0].Direction = new Vector3(0, -1, -1);
device.Lights[0].Update();
device.Lights[0].Enabled = true;


  此灯光较简单,仅为一个直射型白光灯。

最后,在Render()方法中,调用网格对象的Render()方法,以显示地球。

 

  三、使地球旋转

  前面用一个网格对象来建立地球,但此类没有平移,旋转及缩放等方法,下面就加入这些方法,因为这些方法具有通用性,因此可以新建一个类,把这些方法写在这些类中,使地球对象成为它的派生类。

  在工程中新添加一个类:BaseEarth;

  加入进行平移、旋转、缩放的变量:

 

private float xloc = 0.0f;
private float yloc = 0.0f;
private float zloc = 0.0f;
private float xrot = 0.0f;
private float yrot = 0.0f;
private float zrot = 0.0f;
private float xscale = 1.0f;
private float yscale = 1.0f;
private float zscale = 1.0f;


  加入相应的属性代码:

 

public float XLoc
{
 get
 {
  return xloc;
 }
 set
 {
  xloc = value;
 }
}
…………

 

  在Render()虚函数中,应用平移、旋转及缩放。
 

public virtual void Render()
{
 objdevice.MultiplyTransform(TransformType.World,Matrix.Translation(xloc, yloc, zloc));
 objdevice.MultiplyTransform(TransformType.World,Matrix.RotationAxis(new Vector3(1.0f, 0.0f, 0.0f), xrot));
 objdevice.MultiplyTransform(TransformType.World,Matrix.RotationAxis(new Vector3(0.0f, 1.0f, 0.0f), yrot));
 objdevice.MultiplyTransform(TransformType.World,Matrix.RotationAxis(new Vector3(0.0f, 0.0f, 1.0f), zrot));
 objdevice.MultiplyTransform(TransformType.World,Matrix.Scaling(xscale, yscale, zscale));
 return;
}

 

  现在回到地球类,需要将其改为新类的派生类,同时更改构造函数,另外,在Render()方法中,应先调用基类的Render()方法:


public override void Render()
{
 base.Render();
 //把位置变为世界坐标
 // meshDevice.Transform.World = Matrix.Multiply(locationOffset, worldTransform);
 //绘制网格
 。。。。。。
}

 


  现在,由于在基类中可以设置对象位置,因此,可以把与locationOffset相关,即与设置位置的变量及语句注释掉。

  四、加入月球

  在这一步加入月球,实际上是再创建一个网格对象新实例,只是把纹理进行更改即可,为了代码模块性更好,把两个对象放在一个新类CModel中,在工程中新添加一个类CModel,并声明对象实例。


public class cModel
{
 private cMeshObject mesh1 = null;
 private cMeshObject mesh2 = null;
 private bool modelloaded;
}


  把窗口代码中的Load()事件,放在CModel中,这次不仅生成了地球,而且生成了月球。

 


public void Load(ref Device device)
{
 mesh1 = new Earth(ref device);
 mesh2 = new Earth(ref device);
 if (mesh1.LoadMesh(@"..\..\earth2.x"))
 {
  modelloaded = true;
 }
 else
 {
  modelloaded = false;
 }
 if (mesh2.LoadMesh(@"..\..\moon.x"))
 {
  mesh2.XLoc += 20.0f;
  modelloaded = true;
 }
 else
 {
  modelloaded = false;
 }
}

 

  下面的Update()方法中,参数dir 用来判断是顺时针旋转还是逆时针旋转,另外,地球和月球绕Y轴增加的角度大小不同,也就决定了二者旋转的速度不同。


public void Update(int dir)
{
 if(dir > 0)
 {
  mesh1.YRot += 0.02f;
  mesh2.YRot += 0.05f;
 }
 else if(dir < 0)
 {
  mesh1.YRot -= 0.02f;
  mesh2.YRot -= 0.05f;
 }
}


  在下面的render()方法中,生成显示月球和地球:

 


public void Render(ref Device device)
{
 device.Transform.World = Matrix.Identity;
 if(modelloaded)
 {
  mesh1.Render();
  mesh2.Render();
 }
}


  把窗口代码中的加入灯光的方法,也放在此类中:


public void LoadLights(ref Device device)
{
 device.Lights[0].Type = LightType.Directional;
 device.Lights[0].Diffuse = Color.White;
 device.Lights[0].Position = new Vector3(0.0f, 0.0f, 25.0f);
 device.Lights[0].Direction = new Vector3(0, 0, -1);
}
public void Light(ref Device device)
{
 device.Lights[0].Update();
 device.Lights[0].Enabled = true;
}


  五、与鼠标交互操作

  为了实现与键盘、鼠标交互,新添加一个类:CMouse,添加引用Microsoft.DirectX.DirectInput,并添加命名空间。加入相关变量:


private Microsoft.DirectX.DirectInput.Device mouse = null;
public System.Threading.AutoResetEvent MouseUpdated;
private float x, y, z = 0.0f;
private byte[] buttons;

 

  在下面的构造函数代码中,首先创建鼠标设备,并初始化回调事件:


public CMouse(System.Windows.Forms.Control control)
{
 mouse = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Mouse);
 mouse.SetCooperativeLevel(control, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
 mouse.Properties.AxisModeAbsolute = false;
 MouseUpdated = new System.Threading.AutoResetEvent(false);
 mouse.SetEventNotification(MouseUpdated);
 mouse.Acquire();
 Update();

 


  下面的Update()方法中获得鼠标的坐标值,并赋给私有成员变量:

public void Update()
{
 MouseState state = mouse.CurrentMouseState;
 x = state.X;
 y = state.Y;
 z = state.Z;
 buttons = state.GetMouseButtons();
}


  还需要有一个函数来检测鼠标左键是否按下:

 


public bool LeftButtonDown
{
 get
 {
  bool a;
  return a = (buttons[0] != 0);
 }
}


  六、大结局

  现在已经做完了准备工作,返回到窗口代码中,需要对这里的代码重新进行一些调整:

  在图形初始化函数中创建一个CModel类及CMouse类:

 

private CModel model = null;
private CMouse mouse = null;
private bool leftbuttondown = false;
private float mousexloc;

 

  添加对鼠标初始化的方法:

 

网管联盟bitsCN@com


public void InitializeInput()
{
 mouse = new CMouse(this);
}


  添加UpdateInputState()方法,当按下鼠标左键时,将leftbuttondown值设置为真,当鼠标抬起时,将mousexloc置0:


private void UpdateInputState()
{
 mouse.Update();
 if (mouse.LeftButtonDown)
 {
  if(leftbuttondown == false)
  {
   mousexloc = 0.0f;
   leftbuttondown = true;
  }
  else
  {
   mousexloc = -mouse.X;
  }
 }
 else
 {
  leftbuttondown = false;
  mousexloc = 0.0f;
 }
}


  在此程序中,只对X值进行了操作,即只能左右转。

  Render()方法更新如下:

public void Render()
{
 UpdateInputState();
 device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkGray, 1.0f, 0);
 SetupCamera();
 device.BeginScene();
 model.Update((int)mousexloc);
 model.Light(ref device);
 model.Render(ref device);
 device.EndScene();
 device.Present();
}

 

  最后更改Main()主函数:


static void Main()
{
 using (Form1 EarthForm = new Form1())
 {
  EarthForm.InitializeGraphics();
  EarthForm.InitializeInput();
  EarthForm.Show();
  while(EarthForm.Created)
  {
   EarthForm.Render();
   Application.DoEvents();
  }
  EarthForm.Dispose();
 }
 


Platform: | Size: 11817 | Author: mantoutou | Hits:

[DirextXXModel

Description: 利用矩阵变换实现在埸景中加载多个网格模型-matrix transform the scene to load more than King Mesh
Platform: | Size: 1437696 | Author: sdfkl | Hits:

[Special EffectsprogramIDL

Description: 基于图形工作站的IDL程序代码,彩色应用,矩阵操作,插值,图像处理,信号处理,小波变换,mesh对象操作,打印对象,对象图形输出,对象移动旋转,图像对象,光源设置,影像纹理动画: dxf对象显示,对象彩色 自定义类并应用,数据库应用。-Graphics workstations based on the IDL code, color applications, matrix operations, interpolation, image processing, signal processing, wavelet transform, mesh object operations, printing object, the object graphics output, the target mobile rotation, image object, light settings, image texture animation : dxf object showed that the object type and color custom applications, database applications.
Platform: | Size: 98304 | Author: liwei | Hits:

[DirextXterrain

Description: Direct3D 游戏开发技术详解 配套第12章代码 绘制并加载显示地形 1.用D3DX库接口创建一个空Mesh模型 2.创建Mesh所需的顶点矩阵,填写顶点缓冲区的索引信息 3.创建纹理,添加光照并渲染-Direct3D game development technology supporting Detailed Chapter 12 code display topographic mapping and load 1. D3DX library interface used to create an empty Mesh model 2. To create the necessary Mesh vertex matrix, fill vertex buffer indexing information 3. To create texture, Add lighting and rendering
Platform: | Size: 897024 | Author: kongxiangan | Hits:

[3D GraphicMesh45

Description: 三维科学图形包括三维网线图形;三维线状图形;三维坐标设置;填充网状图形;填充颜色的层数;颜色分层的数值条;向顶部和底部投影;边框设置;背景颜色设置;实体图形;等高线图形。给定zij=f(xi,yj)离散数据点,三维图形控件可以绘制出各种精美的图形。三维 图形控件包含的文件有:mesh45.ocx,olch3d32.dll,meshxyz.dll。-Three-dimensional graphics, including three-dimensional network of scientific line graphics linear three-dimensional graphics three-dimensional coordinates set filled mesh graphics fill color layers color layering Numerical Article to the top and bottom of the projector border settings background color settings graphical entities contour graphics. Given zij = f (xi, yj) of discrete data points, three-dimensional graphics can control a variety of beautifully drawn graphics. Three-dimensional graphical controls contained in the documents: mesh45.ocx, olch3d32.dll, meshxyz.dll.
Platform: | Size: 270336 | Author: 栾天观 | Hits:

[matlabTopology_Optimization_a_99_line

Description: Ole Sigmund编写的99行Matlab程序是拓扑优化方面的经典案例。该程序有5个可修改的输入参数:竖直方向网格划分数nely,水平方向网格划分数nelx,保留的材料的体积分数volfrac,惩罚因子penal和滤波器大小rmin。加载方式矩阵F和支承方式fixeddofs也可以分别选取不同的方式或组合。-Ole Sigmund prepared by the 99-line Matlab program is a classic example of topology optimization. The program has five can modify the input parameters: the number of vertical mesh nely, the number of horizontal mesh nelx, keep the volume fraction of material volfrac, penalty factor penal and filter size rmin. Loading mode and the supporting means fixeddofs matrix F can also be selected separately or combined in different ways.
Platform: | Size: 12288 | Author: jiang | Hits:

[matlabsurf2dxf

Description: The X, Y and Z matrix arguments can be used exactly as in the surf and mesh functions. But attention to the colors, because DXF fiels do not uses RGB palete. Instead they use a scaled color palete between 0 and 255.
Platform: | Size: 2048 | Author: Kriubha | Hits:

[GDI-BitmapGraphic

Description: 基于图模型的图像分割并行算法研究与实现,该方案通过网格划分来实现相似度矩阵的并行计算同时考虑到相似度矩阵的稀疏性和矩阵向量乘运算的内在并行性, 在该方案中本文设计并行算法来求解特征值问题在环境下的实验结果表明, 该并行方案是提高图模型分割方法实时性的有效途径.-Image segmentation based on graph model of parallel algorithm and implementation of the program through the mesh to achieve similarity matrix of parallel computing the similarity matrix, taking into account the sparsity of matrix vector multiplication He' s inherent parallelism in the program in this article Design of parallel algorithms to solve the eigenvalue problem in the environment under the experimental results show that, 该 parallel programs is Tigao graph model segmentation in real time to Xing' s Youxiao way.
Platform: | Size: 575488 | Author: 郭事业 | Hits:

[Internet-Networkmesh

Description: 本程序可以生成随机节点数mesh结构,并将节点的链路和链路权值存储为矩阵-The program can generate a random number of nodes in mesh structure, and node-link and link weights store the matrix
Platform: | Size: 293888 | Author: 张新华 | Hits:

[Othermatlab_FEM_dismesh

Description: DistMesh giving a singular FEM matrix?-Hi Anyone here with some experience of using DistMesh in finite element code? I m solving the scalar Helmoltz equation in an annular region in 2D: rho_1 < rho < rho_2 where rho_1 is the radius of a Perfect Electric Conducting cylinder and rho_2 is where the mesh is truncated using an Absorbing Boundary condition. Now, I ve written code to mesh the region myself (by dividing the region into annular rings and picking a fixed number of points on each ring) and I ve written another program which uses DistMesh to mesh the region. I get a nice solution using my own meshing code but the FEM matrix becomes singular when I use DistMesh. This is the DistMesh Code I used: Circle with hole rABC=1.5 rCyl=0.5 fdstring=sprintf( ddiff(dcircle(p,0,0, f),dcircle(p,0,0, f)) ,rABC,rCyl) fd=inline(fdstring, p ) box=[-2,-2 2,2] [p,N]=distmesh2d(fd,@huniform,0.04,box,[]) I tried changing box and the 0.04 value (initial edge length). But every time, the matrix becomes sin
Platform: | Size: 37888 | Author: skypigr | Hits:

[DirextXDirect3DCameraSample

Description: 一个实现了Direct3D的动态摄影机的例子 在这个例子中我先创建了一个Mesh模型 然后通过动态的给view矩阵赋值实现了像CS中的摄影机移动的效果 用户可以使用WASD平移摄影机 使用鼠标调整摄影机的焦点-An implementation of the Direct3D example of the dynamic camera in this example I first created a Mesh model and then to the view through the dynamic assignment matrix implemented in the camera as the CS effects of mobile users can use WASD camera panning the camera to adjust the focus using the mouse
Platform: | Size: 121856 | Author: 陈昊 | Hits:

[Algorithmgetopt1

Description: 三角形单元的网格划分以及单元刚度矩阵的求解程序-Triangular mesh elements and the element stiffness matrix solver
Platform: | Size: 2048 | Author: 王增丽 | Hits:

[Algorithmjacobizz

Description: 雅克比矩阵转正程序,用于常规有限元网格生成后的初始网格检查,评价网格生成的结果。-Jacobian matrix regularization procedure for the conventional finite element mesh generation of the initial grid inspection, evaluation of the results of mesh generation.
Platform: | Size: 118784 | Author: yanjun | Hits:

[OtherFEP2

Description: FEP2程序是一个具有通用性的教学程序,可用于计算一般的线性静力学问题。目前它已设计了平面梁单元与平面3-9个节点的两种单元,但留下接口,可以接入所设计的其他类型的单元。该程序可以处理多种工况、多种类型单元组合结构问题。同时,还可以处理固定约束和指定位移的情况。其主控程序的功能主要有以下几点:(1)内存分配;(2)网格生成;(3)变带宽存储设置;(4)单刚的形成与组集;(5)刚度矩阵的分解;(6)形成节点载荷;(7)形成与组集单元载荷;(8)求解位移;(9)求单元应力;(10)求解结构反力。在FEP2中,每种单元模块都有其相对独立性,自成体系,读者可以自行接入任何一个其他的单元类型。总体来看,该教学程序目的明确,思路清晰,各个模块之间具有独立性且留有接口,非常适合初学者进行学习。-FEP2 program is a universal teaching program, can be used to calculate the general linear statics problem. At present, it has been designed both units 3-9 nodes unit and the plane of plane beam, but leave the interface, and can access the design of other types of unit. The program can handle a variety of conditions, many types of modular structures problem. At the same time, can also handle fixed constraints and the specified displacement. The function of the master program are the following: (1) memory allocation (2) mesh generation (3) Variable bandwidth storage settings (4) single-gang formation and group set (5) stiffness matrix decomposition (6) to form a node load (7) formed the group set unit load (8) to solve the displacement (9) Find the unit stress (10) to solve the structure of the reaction force. In FEP2, each unit module has its relative independence, self-contained, can tap into any other cell type. Overall, the teaching program clarity of purpose, clear thinking
Platform: | Size: 10240 | Author: juege | Hits:

[Algorithmscotch_6.0.0.tar

Description: 一个很强大的图形计算处理软件,包括图像聚类,矩阵计算等-Scotch is a software package for graph and mesh/hypergraph partitioning, graph clustering, and sparse matrix ordering
Platform: | Size: 4519936 | Author: rick | Hits:

[matlabmesh

Description: 空间曲面图 mesh(x,y,z)其中x,y,z为同阶矩阵,每一组对应元素代表一个网格点。 -FIG curved surface mesh (x, y, z) where x, y, z for the same order matrix, each element represents a group corresponding grid point.
Platform: | Size: 2048 | Author: mengxi | Hits:

[Internet-Networkmesh

Description: 空间曲面图 mesh(x,y,z)其中x,y,z为同阶矩阵,每一组对应元素代表一个网格点。 -FIG curved surface mesh (x, y, z) where x, y, z for the same order matrix, each element represents a group corresponding grid point.
Platform: | Size: 2048 | Author: otcall | Hits:

[3D Graphicplywrite

Description: 将三维三角网格数据存为ply格式文件,输入参数为三维数据x,y,z坐标,色彩信息,三角网格下标矩阵-The data is saved in the three-dimensional triangular mesh ply format, input parameters for the three-dimensional data x, y, z coordinates, color information, the subscript matrix triangular mesh
Platform: | Size: 1024 | Author: | Hits:

[3D GraphicMESH--SIMPLE

Description: 针对ply文件格式的网格简化代码实现,简化方法主要是利用二次误差矩阵的方法减少面片数。在vs上执行oglpmesh.dsw文件在视图中添加文件中的自带的ply模型可进行模型简化实现。-The simplification method is to reduce the number of patches by using the quadratic error matrix method. Execute the oglpmesh.dsw file on vs Add the ply model that comes with the file in the view to make the model simplified.
Platform: | Size: 30123008 | Author: china | Hits:
« 12 »

CodeBus www.codebus.net