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); }
void t_Tick(object sender, EventArgs e) { selected_sphere = null; foreach (sphere_info spi in listSphere) { Vector3 x0 = spi.center; Vector3 x1 = Engine.g_MoveCamera.LookAt; Vector3 x2 = Engine.g_MoveCamera.Eye; Vector3 sub1 = Vector3.Subtract(x0, x1); Vector3 sub2 = Vector3.Subtract(x0, x2); Vector3 sub3 = Vector3.Subtract(x2, x1); Vector3 d_arith = Vector3.Cross(sub1, sub2); float dis = (d_arith.Length() / sub3.Length()); if (dis < spi.r) { spi.sh.SetVariables(new Vector3(1.0f, 0.5f, 0.5f), ShaderViariables.Ambient); selected_sphere = spi; } else { spi.sh.SetVariables(new Vector3(1, 1, 1), ShaderViariables.Ambient); } } }