private void AddSphete(float r, Vector3 center, Vector3 scale, String imagePath) { ShaderSimple sh = new ShaderSimple(); // add to list the sphere sphere_info spi = new sphere_info(); spi.sh = sh; spi.r = r * scale.X*1.5f; spi.center = center; listSphere.Add(spi); // add the textures for the shader sh.SetTexture(imagePath, TextureType.Diffuse); sh.SetTexture(imagePath, TextureType.Lightmap); sh.SetTexture("Resources/height.jpg", TextureType.Heightmap); ShaderManager.AddShader("Shader" + r.ToString(), sh); List<Polygon> polygonList = new List<Polygon>(); float numSteps = 100; float thita_step = (float)(Math.PI / numSteps); float phi_step = (float)(2 * Math.PI / numSteps); for (float thita = 0; thita < Math.PI; thita += thita_step) { for (float phi = 0; phi < 2 * Math.PI; phi += phi_step) { float x = (float)(r * Math.Sin(thita) * Math.Cos(phi)); float y = (float)(r * Math.Sin(thita) * Math.Sin(phi)); float z = (float)(r * Math.Cos(thita)); float u = (float)(thita / (Math.PI)); float v = (float)(phi / (2 * Math.PI)); Vertex ver1A = new Vertex(x, z, y, 0, 0, 0, v, u); x = (float)(r * Math.Sin(thita) * Math.Cos(phi + phi_step)); y = (float)(r * Math.Sin(thita) * Math.Sin(phi + phi_step)); z = (float)(r * Math.Cos(thita)); u = (float)(thita / (Math.PI)); v = (float)((phi + phi_step) / (2 * Math.PI)); Vertex ver2A = new Vertex(x, z, y, 0, 0, 0, v, u); x = (float)(r * Math.Sin(thita + thita_step) * Math.Cos(phi + phi_step)); y = (float)(r * Math.Sin(thita + thita_step) * Math.Sin(phi + phi_step)); z = (float)(r * Math.Cos(thita + thita_step)); u = (float)((thita + thita_step) / (Math.PI)); v = (float)((phi + phi_step) / (2 * Math.PI)); Vertex ver3A = new Vertex(x, z, y, 0, 0, 0, v, u); x = (float)(r * Math.Sin(thita + thita_step) * Math.Cos(phi)); y = (float)(r * Math.Sin(thita + thita_step) * Math.Sin(phi)); z = (float)(r * Math.Cos(thita + thita_step)); u = (float)((thita + thita_step) / (Math.PI)); v = (float)((phi) / (2 * Math.PI)); Vertex ver4A = new Vertex(x, z, y, 0, 0, 0, v, u); polygonList.Add(new Polygon(ver1A, ver2A, ver3A)); polygonList.Add(new Polygon(ver1A, ver3A, ver4A)); } } Mesh mesh = new Mesh(); /// set to the new mesh the shader mesh.m_shader = sh; // set the position mesh.SetPosition(center); // scale it mesh.SetScale(scale); // add the polygons on mesh foreach (Polygon poly in polygonList) { // add the polygons to the mesh mesh.AddPolygon(poly, false); } /// create the mesh and download it to the card mesh.CreateMesh(); /// add the mesh to the engine mesh list Engine.g_MeshManager.AddMesh(mesh); sh.SetVariables(new Vector3(1, 1, 1), ShaderViariables.Ambient); }
private void button_create_mesh_Click(object sender, EventArgs e) { if (firstTime) { firstTime = false; AddSphete(100, new Vector3(600, 80, 600), new Vector3(2, 2, 2), "Resources/lady2.jpg"); AddSphete(100, new Vector3(1100, 80, 2000), new Vector3(3, 3, 3), "Resources/lady.jpg"); ///////////////////////////////////////////////////////////// Init the Shader // create the shader ShaderSimple sh = new ShaderSimple(); /// add the shader to the list ShaderManager.AddShader("Shader12", sh); // add the textures for the shader sh.SetTexture("Resources/tmima.jpg", TextureType.Diffuse); sh.SetTexture("Resources/tmima.jpg", TextureType.Lightmap); sh.SetTexture("Resources/height.jpg", TextureType.Heightmap); sh.SetVariables(new Vector3(1, 1, 1), ShaderViariables.Ambient); List<Polygon> polygonList = new List<Polygon>(); FxVector2f p1 = new FxVector2f(0, 0); FxVector2f p2 = new FxVector2f(0, 100); FxVector2f p3 = new FxVector2f(100, 100); FxVector2f p4 = new FxVector2f(100, 0); float u1 = 0; float v1 = 0; Vertex ver1 = new Vertex(p1.X, -1, p1.Y, 0, 0, 0, u1, v1); u1 = 0; v1 = 1; Vertex ver2 = new Vertex(p2.X, -1, p2.Y, 0, 0, 0, u1, v1); u1 = 1; v1 = 1; Vertex ver3 = new Vertex(p3.X, -1, p3.Y, 0, 0, 0, u1, v1); u1 = 1; v1 = 0; Vertex ver4 = new Vertex(p4.X, -1, p4.Y, 0, 0, 0, u1, v1); polygonList.Add(new Polygon(ver1, ver2, ver3)); polygonList.Add(new Polygon(ver1, ver3, ver4)); ///////////////////////////////////////////////////////////// Init the Mesh /// make a new mesh Mesh mesh = new Mesh(); /// set to the new mesh the shader mesh.m_shader = ShaderManager.GetExistShader("Shader12"); // set the position mesh.SetPosition(new Vector3(0, 0, 0)); // scale it mesh.SetScale(new Vector3(40, 40, 40)); // add the polygons on mesh foreach (Polygon poly in polygonList) { // add the polygons to the mesh mesh.AddPolygon(poly, false); } /// create the mesh and download it to the card mesh.CreateMesh(); /// add the mesh to the engine mesh list Engine.g_MeshManager.AddMesh(mesh); sh.SetVariables(new Vector3(1, 1, 1), ShaderViariables.Ambient); ///////////////////////////////////////////////////////////// Change Camera position // Engine.g_MoveCamera. Engine.g_MoveCamera.SetViewParams(new Vector3(4500, 3500, 2000), new Vector3(200, 0, 200)); //////////////////////////////////////////////////// t.Interval = 200; t.Tick += t_Tick; t.Enabled = true; } else { if (selected_sphere != null) { Engine.g_MoveCamera.SetViewParams(selected_sphere.center, new Vector3(200, 0, 200)); selected_sphere.sh.SetVariables(new Vector3(1, 1, 1), ShaderViariables.Ambient); t.Enabled = false; } } }
private void addVideoSphereToolStripMenuItem_Click(object sender, EventArgs e) { ///////////////////////////////////////////////////////////// Start Video mp.Initialize(Engine.g_device); mp.SetFile("sample.avi"); difTex = mp.CreateTexture(); Engine.AppHandleKeys = (SharpDX.DirectInput.Key key) => { /// handle the keys that we want switch (key) { case SharpDX.DirectInput.Key.Up: mp.OnRender(difTex.texture2D, true); break; case SharpDX.DirectInput.Key.Down: mp.OnRender(difTex.texture2D, false); break; } return false; }; ///////////////////////////////////////////////////////////// Add the Spheres AddSphete(1000, new Vector3(0, 0, 0), new Vector3(20, 20, 20)); //AddSphete(100, new Vector3(1100, 80, 2000), new Vector3(3, 3, 3)); ///////////////////////////////////////////////////////////// Init the Shader #if false // create the shader ShaderSimple sh = new ShaderSimple(); /// add the shader to the list ShaderManager.AddShader("Shader12", sh); // add the textures for the shader sh.SetTexture("Resources/diffuse.jpg", TextureType.Diffuse); sh.SetTexture("Resources/lightmap.jpg", TextureType.Lightmap); sh.SetTexture("Resources/height.jpg", TextureType.Heightmap); sh.SetVariables(new Vector3(1, 1, 1), ShaderViariables.Ambient); List<Polygon> polygonList = new List<Polygon>(); FxVector2f p1 = new FxVector2f(0, 0); FxVector2f p2 = new FxVector2f(0, 100); FxVector2f p3 = new FxVector2f(100, 100); FxVector2f p4 = new FxVector2f(100, 0); float u1 = 0; float v1 = 0; Vertex ver1 = new Vertex(p1.X, -1, p1.Y, 0, 0, 0, u1, v1); u1 = 0; v1 = 1; Vertex ver2 = new Vertex(p2.X, -1, p2.Y, 0, 0, 0, u1, v1); u1 = 1; v1 = 1; Vertex ver3 = new Vertex(p3.X, -1, p3.Y, 0, 0, 0, u1, v1); u1 = 1; v1 = 0; Vertex ver4 = new Vertex(p4.X, -1, p4.Y, 0, 0, 0, u1, v1); polygonList.Add(new Polygon(ver1, ver2, ver3)); polygonList.Add(new Polygon(ver1, ver3, ver4)); ///////////////////////////////////////////////////////////// Init the Mesh /// make a new mesh Mesh mesh = new Mesh(); /// set to the new mesh the shader mesh.m_shader = ShaderManager.GetExistShader("Shader12"); // set the position mesh.SetPosition(new Vector3(0, 0, 0)); // scale it mesh.SetScale(new Vector3(40, 40, 40)); // add the polygons on mesh foreach (Polygon poly in polygonList) { // add the polygons to the mesh mesh.AddPolygon(poly, false); } /// create the mesh and download it to the card mesh.CreateMesh(); /// add the mesh to the engine mesh list Engine.g_MeshManager.AddMesh(mesh); sh.SetVariables(new Vector3(1, 1, 1), ShaderViariables.Ambient); #endif ///////////////////////////////////////////////////////////// Change Camera position // Engine.g_MoveCamera. Engine.g_MoveCamera.SetViewParams(new Vector3(2000, 2000, 200), new Vector3(0, 0, 0)); //////////////////////////////////////////////////// }
private void addSamplePlaneToolStripMenuItem_Click(object sender, EventArgs e) { ///////////////////////////////////////////////////////////// Init the Shader // create the shader ShaderSimple sh = new ShaderSimple(); /// add the shader to the list ShaderManager.AddShader("Shader10", sh); // add the textures for the shader sh.SetTexture("Resources/diffuse.jpg", TextureType.Diffuse); sh.SetTexture("Resources/lightmap.jpg", TextureType.Lightmap); sh.SetTexture("Resources/height.jpg", TextureType.Heightmap); sh.SetVariables(new Vector3(1, 1, 1), ShaderViariables.Ambient); ///////////////////////////////////////////////////////////// Init the polygons of mesh List<Polygon> polygonList = new List<Polygon>(); FxVector2f p1 = new FxVector2f(0, 0); FxVector2f p2 = new FxVector2f(0, 100); FxVector2f p3 = new FxVector2f(100, 100); FxVector2f p4 = new FxVector2f(100, 0); float u = 0; float v = 0; Vertex ver1 = new Vertex(p1.X, 1, p1.Y, 0, 0, 0, u, v); u = 0; v = 1; Vertex ver2 = new Vertex(p2.X, 1, p2.Y, 0, 0, 0, u, v); u = 1; v = 1; Vertex ver3 = new Vertex(p3.X, 1, p3.Y, 0, 0, 0, u, v); u = 1; v = 0; Vertex ver4 = new Vertex(p4.X, 1, p4.Y, 0, 0, 0, u, v); polygonList.Add(new Polygon(ver1, ver2, ver3)); polygonList.Add(new Polygon(ver1, ver3, ver4)); ///////////////////////////////////////////////////////////// Init the Mesh /// make a new mesh Mesh mesh = new Mesh(); /// set to the new mesh the shader mesh.m_shader = ShaderManager.GetExistShader("Shader10"); // set the position mesh.SetPosition(new Vector3()); // scale it mesh.SetScale(new Vector3(10, 10, 5)); // add the polygons on mesh foreach (Polygon poly in polygonList) { // add the polygons to the mesh mesh.AddPolygon(poly, false); } /// create the mesh and download it to the card mesh.CreateMesh(); /// add the mesh to the engine mesh list Engine.g_MeshManager.AddMesh(mesh); ///////////////////////////////////////////////////////////// Change Camera position Engine.g_MoveCamera.SetViewParams(new Vector3(2000, 2000, 200), new Vector3(0, 0, 0)); }
private void addCEIDToolStripMenuItem_Click(object sender, EventArgs e) { ///////////////////////////////////////////////////////////// Start Video mp.Initialize(Engine.g_device); mp.SetFile("sample.avi"); difTex = mp.CreateTexture(); Engine.AppHandleKeys = (SharpDX.DirectInput.Key key) => { /// handle the keys that we want switch (key) { case SharpDX.DirectInput.Key.Up: mp.OnRender(difTex.texture2D, true); break; case SharpDX.DirectInput.Key.Down: mp.OnRender(difTex.texture2D, false); break; case SharpDX.DirectInput.Key.Space: if (selected_sphere != null) { Engine.g_MoveCamera.SetViewParams(selected_sphere.center, new Vector3(2000, 0, 2000)); selected_sphere.sh.SetVariables(new Vector3(1, 1, 1), ShaderViariables.Ambient); selected_sphere = null; weAreIn = true; } break; case SharpDX.DirectInput.Key.LeftShift: Engine.g_MoveCamera.SetViewParams(new Vector3(6500, 4500, 2000), new Vector3(200, 0, 200)); weAreIn = false; break; } return false; }; ///////////////////////////////////////////////////////////// Add the Spheres // AddSphete(1000, new Vector3(0, 0, 0), new Vector3(2, 2, 2),"sample.avi"); AddSphere(100, new Vector3(1500, 110, 900), new Vector3(2.5f, 2.5f, 2.5f), "Resources/lady2.jpg"); AddSphere(100, new Vector3(2300, 80, 3000), new Vector3(2.5f, 2.5f, 2.5f), "Resources/lady.jpg"); AddSphere(100, new Vector3(2300, 110, 2300), new Vector3(2.5f, 2.5f, 2.5f), "Resources/lady3.jpg"); AddSphete(100, new Vector3(2300, 110, 1700), new Vector3(2.5f, 2.5f, 2.5f)); // AddSphete(100, new Vector3(1100, 80, 2000), new Vector3(3, 3, 3)); // create the shader ShaderSimple sh1 = new ShaderSimple(); /// add the shader to the list ShaderManager.AddShader("Shader11", sh1); // add the textures for the shader sh1.SetTexture("Resources/tmima2.png", TextureType.Diffuse); sh1.SetTexture("Resources/tmima2.png", TextureType.Lightmap); sh1.SetTexture("Resources/height.jpg", TextureType.Heightmap); List<Polygon> polygonList1 = new List<Polygon>(); FxVector2f p1 = new FxVector2f(0, 0); FxVector2f p2 = new FxVector2f(0, 100); FxVector2f p3 = new FxVector2f(100, 100); FxVector2f p4 = new FxVector2f(100, 0); float u1 = 0; float v1 = 0; Vertex ver1 = new Vertex(p1.X, -1, p1.Y, 0, 0, 0, u1, v1); u1 = 0; v1 = 1; Vertex ver2 = new Vertex(p2.X, -1, p2.Y, 0, 0, 0, u1, v1); u1 = 1; v1 = 1; Vertex ver3 = new Vertex(p3.X, -1, p3.Y, 0, 0, 0, u1, v1); u1 = 1; v1 = 0; Vertex ver4 = new Vertex(p4.X, -1, p4.Y, 0, 0, 0, u1, v1); polygonList1.Add(new Polygon(ver1, ver2, ver3)); polygonList1.Add(new Polygon(ver1, ver3, ver4)); /// make a new mesh Mesh mesh1 = new Mesh(); /// set to the new mesh the shader mesh1.m_shader = ShaderManager.GetExistShader("Shader11"); // set the position mesh1.SetPosition(new Vector3(0, 0, 0)); // scale it mesh1.SetScale(new Vector3(60, 60, 60)); // add the polygons on mesh foreach (Polygon poly in polygonList1) { // add the polygons to the mesh mesh1.AddPolygon(poly, false); } /// create the mesh and download it to the card mesh1.CreateMesh(); /// add the mesh to the engine mesh list Engine.g_MeshManager.AddMesh(mesh1); sh1.SetVariables(new Vector3(1, 1, 1), ShaderViariables.Ambient); }
/// <summary> /// Load FBX Files from max /// It loads all the meshes in the FBX files /// including their textures /// </summary> /// <param name="FileName">The file name of the FBX file to load</param> public static void LoadFBX(String FileName) { String localFile = Environment.CurrentDirectory; Environment.CurrentDirectory = Path.GetDirectoryName(FileName); StringBuilder strB = new StringBuilder(FileName); /// start FBX dll file FBX.Initialize(strB); /// get the number of mesh that FBX have int meshNum = FBX.GetNumberMesh(); /// load all the mesh for (int mesh_ind = 0; mesh_ind < meshNum; mesh_ind++) { /// make a new mesh Mesh mesh = new Mesh(); /// add the mesh to the engine mesh list Engine.g_MeshManager.AddMesh(mesh); Vector3_t v3; /// select the mesh to the FBX /// after that we can get anything /// that we want from the mesh FBX.SelectMesh(mesh_ind); /// get the number of polygons int PoligonsNum = FBX.GetPolygons_Count(); /// get the polygons and add them to the mesh for (int i = 0; i < PoligonsNum; i++) { Polygon poly = FBX.GetPolygon(i); mesh.AddPolygon(poly, false); } /// get the position of the mesh v3 = FBX.GetPosition(); Vector3 pos = new Vector3(v3.x, v3.y, -v3.z); mesh.SetPosition(pos); /// get the Scale of the mesh v3 = FBX.GetScale(); Vector3 scale = new Vector3(v3.x, v3.y, v3.z); mesh.SetScale(scale); /// get the rotation of the mesh v3 = FBX.GetRotation(); Vector3 rot = new Vector3(-v3.x, -v3.z, -v3.y); rot *= (float)Math.PI; rot /= 180.0f; mesh.SetRotation(rot); mesh.SetRotation(new Vector3 (-(float)Math.PI/2f,0,0)); /// get the material that the mesh have int MaterialCount = FBX.GetMaterialCount(); /// Material name will be the same with the shader StringBuilder MaterialName = new StringBuilder(124); /// get the material name FBX.GetMaterialName(0, MaterialName); if (!ShaderManager.IsExist(MaterialName.ToString())) { // create the shader ShaderSimple sh = new ShaderSimple(); /// set to the new mesh the shader mesh.m_shader = sh; /// add the shader to the list ShaderManager.AddShader(MaterialName.ToString(), sh); if (MaterialCount > 0) { /// Get the number of the texture that the mesh has /// Run through all textures found and add them /// to the mesh according to their attributes for (int i = 0; i < FBX.GetTextureCount(0); i++) { StringBuilder TextureBld = new StringBuilder(124); StringBuilder PropertyBld = new StringBuilder(124); FBX.GetTexture(0, i, PropertyBld, TextureBld); Console.WriteLine( "Texture:" + TextureBld.ToString() + " Property:" + PropertyBld.ToString()); String PropertyStr = PropertyBld.ToString(); if (!TextureBld.ToString().Replace(" ", "").Equals("")) { /// pass the textures if (PropertyStr.Equals("DiffuseColor")) sh.SetTexture(TextureBld.ToString(), TextureType.Diffuse); else if (PropertyStr.Equals("Bump")) sh.SetTexture(TextureBld.ToString(), TextureType.Bump); else if (PropertyStr.Equals("ReflectionColor")) sh.SetTexture(TextureBld.ToString(), TextureType.Normal); } else { sh.SetVariables(new Vector3(0, 0, 1), ShaderViariables.Diffuse); } } } } else { mesh.m_shader = ShaderManager.GetExistShader(MaterialName.ToString()); } /// create the mesh and download it to the card mesh.CreateMesh(); /// get the mesh Name from the FBX StringBuilder MeshName = new StringBuilder(124); FBX.GetMeshName(MeshName); /// set the mesh name mesh.Name = MeshName.ToString(); } Environment.CurrentDirectory = localFile; }