Welcome![Sign In][Sign Up]
Location:
Search - buttons for device control

Search list

[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:

[SCMFluiddripdetector

Description: 本设计以msp430为液体点滴速度监控装置,实现了对液体点滴速度的检测与控制和储液瓶中液面高度的检测报警,并且动态显示点滴速度,可以通过按键设置液体点滴速度并使用步进电机进行速度控制;为了达到较好的调整稳定度,通过PID控制滴速;利用软件屏蔽检测中的异常信号。-The design of the speed monitoring device for the liquid drip, drip of liquid to achieve the speed of detection and control and liquid storage bottle detection of liquid level alarm, and dynamic display of bit rate, you can set the liquid drip rate buttons and use the stepper motor speed control In order to achieve a better adjustment of stability, PID control by dropping speed the use of software screening detection of abnormalities.
Platform: | Size: 454656 | Author: pipi | Hits:

[Documentstemperature

Description: 温度是一个很重要的物理量,在现代工农业生产中,对它的测量与控制有十分重要的意义,特别在冶金、机械、食品、化工等工业中,对工件的处理温度都要求严格控制,对于温度的精确度和稳定性均有较高的要求。此系统采用AT89S51作为控制内核,还有按键、报警装置,实现了完全自动控制。-Temperature is a very important physical quantities, in modern industrial and agricultural production, its measurement and control are of great significance, especially in metallurgy, machinery, food, chemical industry, processing of the workpiece temperature require strict control The temperature accuracy and stability have higher requirements. This system uses AT89S51 as the control core, as well as buttons, alarm device, to achieve a fully automatic control.
Platform: | Size: 4096 | Author: 冷卓燕 | Hits:

[JSP/JavaAirConditioningCode

Description: 分布式温控系统基本要求 1. 中央空调是冷暖两用,但一次只能使用一种温控装置。当设置为供暖时,供暖温度控制在25°C~30°C之间,当设置为制冷时,制冷温度控制在18°C~25°C之间。 2. 中央空调具备开关按钮,只可人工开启和关闭,中央空调开启后处于待机状态。当关闭后,不响应来自房间的任何温控请求。当有来自从控机的温控要求时,中央空调开始工作。当所有房间都没有温控要求时,中央空调的状态回到待机状态。 3. 房间内有独立的从控空调机,但没有冷暖控制设备。从控机具有一个传感器,实时监测房间的温度,并与从控机的目标设置温度进行对比,决定是否需要制冷或制热,并向中央空调机发出请求。如果从控机发出的请求和中央空调设置的冷暖控制状态发生矛盾时,以中央空调机的状态优先,即中央空调机不予响应。 4. 从控机只能人工方式开闭,并通过控制面板设置目标温度,目标温度有上下限制。所有房间的初始目标温度由中央空调机设置,每个房间的空调机开启时要读取中央空调机预置的房间温度。温度升降范围应该在目标温度的上下1 °C。房间不考虑大小和管道的分布及大小问题,在达到目标温度后,房间的温度每分钟上下变化0.1°C。 -Basic requirements of a distributed temperature control system 1. Central air conditioning is the well-being of dual-use, but can only use a thermostat device. When set to heating, the heating temperature at 25 ° C ~ 30 ° C, and when set to cooling, the cooling temperature at 18 ° C ~ 25 ° C between. 2. Central air conditioning with switch buttons can only be manually opened and closed, central air-conditioning turned on in standby mode. When closed, do not respond to a request from the room of any thermostat. When the machine from temperature control from the control requirements, central air-conditioning work. When all rooms are no thermostat demand, central air condition back to standby. 3. Room independent from the control air conditioning, but no climate-control equipment. From the control machine with a sensor, real-time monitoring the temperature of the room, and with the goal set from the control unit compares the temperature to determine the need for cooling or he
Platform: | Size: 43008 | Author: parid | Hits:

[FlashMXwebcamsnap

Description: Capture JPEG webcam images on your website and submit to your server. Flash + Javascript library which allows you to display a variable-sized Flash movie in your page that captures Webcam snapshots (still frames), and submits them to your server in JPEG format. Sample PHP 5 code included for receiving submissions and saving them to disk. The Flash movie first activates the webcam and allows the user to make adjustments before submitting. All controls for displaying the Flash device configuration panel and taking snapshots is handled from Javascript. This way you can control the user interface, using your own buttons and layout. The Flash movie consists of nothing except a full-canvas camera control with NO flash user interface elements.
Platform: | Size: 92160 | Author: Muhammad Fareed | Hits:

[SCMqiangdaqi

Description: 本程序为一抢答器;其中主持人操控S12与S13两个按键。选手共六位,分别操控S6--S11中的一个按键。 当主持人按下抢答开始按键S13后,倒计时开始,计时5s。此后最先按下按键的选手号码将显示与数码管上。后来按下的将无显示。若五秒计时结束后,再按下按键也不会显示。若主持人没有按开始键,就有选手抢答,则视为犯规。此时犯规的选手号码将被显示于数码管上(最多显示五位犯规选手)同时,蜂鸣器发出长笛声报警,数码管全亮。而当主持人按下清零键S12后,一切状态均恢复,可以开始新一轮的抢答。 -The procedures for a vies to answer first device Among them the host control S12 and S13 two buttons. Players were six, respectively-in the S11 control S6 a button. When host press vies to answer first start button, the countdown begins, S13 time 5 s. Since then the first press buttons players will show with digital number on the tube. Then press will show no. If five seconds after timing is over, then press the button will not display. If the host did not press start, there are the key players vies to answer first, then as a foul. At this time the player foul will be displayed in the number of digital tube (up to show five fouls player) at the same time, a long flute version for the police, digital tube light all. And when the host press the reset button S12, all states are restored, can start a new round of the contest.
Platform: | Size: 2048 | Author: 耙斗星 | Hits:

[VHDL-FPGA-Verilogmain

Description: 采用现场可编程逻辑器件(FPGA)制作,利用EDA软件中的verilog HDL硬件描述语言控制进行控制,然后烧写实现.按键7~1分别用于七个音符的发音(DO,RE,MI,FA,SO,LA,SI),同时LED灯点亮。按键8和9用于控制乐曲的播放,可以选择三个曲子的播放。-Using field-programmable logic device (FPGA) production, the use of EDA software verilog HDL hardware description language control to control, and programming implementation. Buttons 7 to 1, respectively, for the seven notes pronunciation (DO, RE, MI, FA , SO, LA, SI), while LED lights up. 8 and 9 buttons for controlling music playback, you can choose three song playback.
Platform: | Size: 2048 | Author: | Hits:

[ARM-PowerPC-ColdFire-MIPSek-lm4f120xl--qs-rgb

Description: ek-lm4f120xl TI Stellaris LaunchPad 例程之出厂自带源码 闪烁变换RGBLED 有串口命令控制 按键控制等综合程序-A demonstration of the Stellaris LaunchPad (EK-LM4F120XL) capabilities. Press and/or hold the left button traverse toward the red end of the ROYGBIV color spectrum. Press and/or hold the right button to traverse toward the violet end of the ROYGBIV color spectrum. Leave idle for 5 seconds to see a automatically changing color display Press and hold both left and right buttons for 3 seconds to enter hibernation. During hibernation last color on screen will blink on the LED for 0.5 seconds every 3 seconds. Command line UART protocol can also control the system. Command help to generate list of commands and helpful information. Command hib will place the device into hibernation mode. Command rand will initiate the pseudo-random sequence. Command intensity followed by a number between 0.0 and 1.0 will scale the brightness of the LED by that factor. Command rgb followed by a six character hex value will set the color. For example rgb FF0000 will produc
Platform: | Size: 556032 | Author: 123 | Hits:

[matlabvirtual-Mouse

Description: In computing, a mouse is a pointing device that functions by detecting two-dimensional motion relative to its supporting surface. Physically, a mouse consists of an object held under one of the user s hands, with one or more buttons.The mouse sometimes features other elements, such as wheels , which allow the user to perform various system-dependent operations, or extra buttons or features that can add more control or dimensional input. The mouse s motion typically translates into the motion of a pointer on a display, which allows for fine control of a graphical user interface.- In computing, a mouse is a pointing device that functions by detecting two-dimensional motion relative to its supporting surface. Physically, a mouse consists of an object held under one of the user s hands, with one or more buttons.The mouse sometimes features other elements, such as wheels , which allow the user to perform various system-dependent operations, or extra buttons or features that can add more control or dimensional input. The mouse s motion typically translates into the motion of a pointer on a display, which allows for fine control of a graphical user interface.
Platform: | Size: 6144 | Author: Lee Kurian | Hits:

CodeBus www.codebus.net