void buildShadowVolume(List <Vector3Df> shadowVertices, MeshBuffer meshbuffer, Matrix matrix, Vector3Df light) { ushort[] indices = meshbuffer.Indices as ushort[]; if (indices == null) { throw new ArgumentException(); } Triangle3Df t123 = new Triangle3Df(); for (int i = 0; i < indices.Length; i += 3) { Vector3Df v1 = meshbuffer.GetPosition(indices[i]); Vector3Df v2 = meshbuffer.GetPosition(indices[i + 1]); Vector3Df v3 = meshbuffer.GetPosition(indices[i + 2]); matrix.TransformVector(ref v1); matrix.TransformVector(ref v2); matrix.TransformVector(ref v3); t123.Set(v1, v2, v3); Vector3Df v1Dir = v1 - light; if (!t123.IsFrontFacing(v1Dir)) { continue; } Vector3Df v2Dir = v2 - light; Vector3Df v3Dir = v3 - light; // calc near points Vector3Df v1near = v1 + v1Dir * shadowNearMultiplier; Vector3Df v2near = v2 + v2Dir * shadowNearMultiplier; Vector3Df v3near = v3 + v3Dir * shadowNearMultiplier; // calc infinity points Vector3Df v1inf = v1 + v1Dir.Normalize() * shadowInfinityRange; Vector3Df v2inf = v2 + v2Dir.Normalize() * shadowInfinityRange; Vector3Df v3inf = v3 + v3Dir.Normalize() * shadowInfinityRange; // top shadowVertices.Add(v1near); shadowVertices.Add(v2near); shadowVertices.Add(v3near); // bottom shadowVertices.Add(v3inf); shadowVertices.Add(v2inf); shadowVertices.Add(v1inf); // side1 shadowVertices.Add(v1inf); shadowVertices.Add(v2near); shadowVertices.Add(v1near); shadowVertices.Add(v1inf); shadowVertices.Add(v2inf); shadowVertices.Add(v2near); // side2 shadowVertices.Add(v2inf); shadowVertices.Add(v3near); shadowVertices.Add(v2near); shadowVertices.Add(v2inf); shadowVertices.Add(v3inf); shadowVertices.Add(v3near); // side3 shadowVertices.Add(v1near); shadowVertices.Add(v3near); shadowVertices.Add(v1inf); shadowVertices.Add(v3near); shadowVertices.Add(v3inf); shadowVertices.Add(v1inf); } }
public void deformMesh(MeshSceneNode t, Vector3Df position, Vector3Df direction, Triangle3Df triangle) { //get the closest vector to this point //USE BRUTE FORCE FOR RIGHT NOW Vertex3D[] v = (Vertex3D[])t.Mesh.MeshBuffers[0].Vertices; TriangleSelector tri = device.SceneManager.CreateTriangleSelector(t.Mesh, t); t.TriangleSelector = tri; tri.Drop(); int size = t.Mesh.MeshBuffers[0].VertexCount; int min = 0; float minDist = v[0].Position.GetDistanceFromSQ(position); for (int i = 1; i < size; i++) { //nsole.WriteLine(v[i].Position.SphericalCoordinateAngles.X); //query verts float currDist = v[i].Position.GetDistanceFromSQ(position); // float currDist = v[i].TCoords - position; if (currDist < minDist) { min = i; minDist = currDist; } } //int radius = 2; //for (; radius > 0; radius--) //{// this should give a staircase like effect //v[min - radius].Position = new Vector3Df(v[min - radius].Position + v[min - radius].Normal * direction); if (v[min].Position.GetDistanceFromSQ(new Vector3Df(0, 0, 0)) > 100 || direction.X > 0) { v[min].Position = new Vector3Df(v[min].Position + v[min].Normal * 2 * direction); //} t.Mesh.MeshBuffers[0].UpdateVertices(v, 0); t.Mesh.MeshBuffers[0].SetDirty(HardwareBufferType.VertexAndIndex); } //device.SceneManager.MeshManipulator.RecalculateNormals(t.Mesh); }
void buildShadowVolume(List<Vector3Df> shadowVertices, MeshBuffer meshbuffer, Matrix matrix, Vector3Df light) { ushort[] indices = meshbuffer.Indices as ushort[]; if (indices == null) throw new ArgumentException(); Triangle3Df t123 = new Triangle3Df(); for (int i = 0; i < indices.Length; i += 3) { Vector3Df v1 = meshbuffer.GetPosition(indices[i]); Vector3Df v2 = meshbuffer.GetPosition(indices[i + 1]); Vector3Df v3 = meshbuffer.GetPosition(indices[i + 2]); matrix.TransformVector(ref v1); matrix.TransformVector(ref v2); matrix.TransformVector(ref v3); t123.Set(v1, v2, v3); Vector3Df v1Dir = v1 - light; if (!t123.IsFrontFacing(v1Dir)) continue; Vector3Df v2Dir = v2 - light; Vector3Df v3Dir = v3 - light; // calc near points Vector3Df v1near = v1 + v1Dir * shadowNearMultiplier; Vector3Df v2near = v2 + v2Dir * shadowNearMultiplier; Vector3Df v3near = v3 + v3Dir * shadowNearMultiplier; // calc infinity points Vector3Df v1inf = v1 + v1Dir.Normalize() * shadowInfinityRange; Vector3Df v2inf = v2 + v2Dir.Normalize() * shadowInfinityRange; Vector3Df v3inf = v3 + v3Dir.Normalize() * shadowInfinityRange; // top shadowVertices.Add(v1near); shadowVertices.Add(v2near); shadowVertices.Add(v3near); // bottom shadowVertices.Add(v3inf); shadowVertices.Add(v2inf); shadowVertices.Add(v1inf); // side1 shadowVertices.Add(v1inf); shadowVertices.Add(v2near); shadowVertices.Add(v1near); shadowVertices.Add(v1inf); shadowVertices.Add(v2inf); shadowVertices.Add(v2near); // side2 shadowVertices.Add(v2inf); shadowVertices.Add(v3near); shadowVertices.Add(v2near); shadowVertices.Add(v2inf); shadowVertices.Add(v3inf); shadowVertices.Add(v3near); // side3 shadowVertices.Add(v1near); shadowVertices.Add(v3near); shadowVertices.Add(v1inf); shadowVertices.Add(v3near); shadowVertices.Add(v3inf); shadowVertices.Add(v1inf); } }
public Application() { _01.HelloWorld.Kinect kinect = new _01.HelloWorld.Kinect(); mfX = mouseX = 840; mfY = mouseY = 525; mouseL = false; mouseR = false; //device = IrrlichtDevice.CreateDevice( // DriverType.Direct3D9, new Dimension2Di(800, 600), 16, false, true, false); // | device = IrrlichtDevice.CreateDevice( // \|/ Fullscreen DriverType.Direct3D9, new Dimension2Di(1680, 1050), 32, false, true, false); device.SetWindowCaption("BENder3D"); VideoDriver driver = device.VideoDriver; SceneManager smgr = device.SceneManager; GUIEnvironment gui = device.GUIEnvironment; device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent); smgr.AmbientLight = new Colorf(128, 128, 128, 128); //smgr.AddLightSceneNode(null, new Vector3Df(0, 70, 0), new Colorf(122,0,122,0), (float)10); MeshSceneNode box = smgr.AddCubeSceneNode(100, null, 9001, new Vector3Df(0.0f, -ballRadius * 3 / 2, 0.0f)); box.Scale = new Vector3Df(100.0f, 0.1f, 100.0f); //Mesh cyl = smgr.GeometryCreator.CreateCylinderMesh(ballRadius, 50, 256); //Mesh sphere = smgr.GeometryCreator.CreateSphereMesh(ballRadius, 16,16); //MeshSceneNode t = smgr.AddSphereSceneNode(ballRadius, 32); //MeshSceneNode t = smgr.AddOctreeSceneNode(sphere); MeshSceneNode t = smgr.AddMeshSceneNode(smgr.GetMesh("pill.obj")); //MeshSceneNode t = smgr.AddMeshSceneNode(cyl); TriangleSelector triselect = smgr.CreateTriangleSelector(t.Mesh, t); t.TriangleSelector = triselect; triselect.Drop(); //t = smgr.AddMeshSceneNode(smgr.GetMesh("../../media/sphere.x")); //smgr t.SetMaterialTexture(0, driver.GetTexture("rockwall.jpg")); //t.SetMaterialFlag(MaterialFlag.Lighting, true); t.GetMaterial(0).SpecularColor.Set(0, 0, 0); //t.GetMaterial(0).Lighting = true; t.GetMaterial(0).NormalizeNormals = false; /*Texture citrus = driver.AddTexture(new Dimension2Di(200, 200), "citrus.png"); * gui.AddImage(citrus, new Vector2Di(824, 0), true);*/ gui.AddStaticText("Hey, Listen! Press C to switch the mesh to a cylinder!\n Press S to change to a sphere, and enter to send yourself the obj file!", new Recti(0, 0, 400, 60)); //t.AddShadowVolumeSceneNode(); // driver.GPUProgrammingServices.OnSetConstants += new GPUProgrammingServices.SetConstantsHandler(gpu_OnSetConstants); /* * MaterialType shaderMat = MaterialType.Solid; * shaderMat = driver.GPUProgrammingServices.AddHighLevelShaderMaterialFromFiles("C:/IrrlichtLime-1.4/examples/01.HelloWorld/bumpmap.hlsl", "VertexShaderFunction", VertexShaderType.VS_3_0, * "C:/IrrlichtLime-1.4/examples/01.HelloWorld/bumpmap.hlsl", "PixelShaderFunction", PixelShaderType.PS_3_0, MaterialType.Solid); * * t.SetMaterialType(shaderMat); * t.SetMaterialTexture(1, driver.GetTexture("../../media/rockwall_height.bmp"));*/ GPUProgrammingServices gpu = driver.GPUProgrammingServices; MaterialType newMaterialType1 = MaterialType.Solid; MaterialType newMaterialType2 = MaterialType.TransparentAddColor; gpu.OnSetConstants += new GPUProgrammingServices.SetConstantsHandler(gpu_OnSetConstants); // create the shaders depending on if the user wanted high level or low level shaders newMaterialType1 = gpu.AddHighLevelShaderMaterialFromFiles( "d3d9.hlsl", "vertexMain", VertexShaderType.VS_1_1, "d3d9.hlsl", "pixelMain", PixelShaderType.PS_1_1, MaterialType.Solid, 0, GPUShadingLanguage.Default); t.SetMaterialType(newMaterialType1); //t.GetMaterial(0).Wireframe = true; //t.DebugDataVisible = DebugSceneType.Full; //t.AddShadowVolumeSceneNode(null, -1, false, 1000.0f); smgr.AddLightSceneNode(null, new Vector3Df(40, 150, -50), new Colorf(255, 255, 255, 255), 250.0f); //CSampleSceneNode sceneNode = new CSampleSceneNode(smgr.RootNode, smgr, 667); camera = smgr.AddCameraSceneNode(null, new Vector3Df(0, 50, -140), new Vector3Df(0, 5, 0)); //camera.Target = new Vector3Df(-70, 30, -60); //smgr.AddCameraSceneNodeFPS(null, (float)50.0); Material m = new Material(); m.Lighting = false; double PI = 3.1415926f; float distance = 200.0f; double angle = 180.0f; double angleY = 20.0f; int oldMouseX = mouseX; int oldMouseY = mouseY; uint then = device.Timer.Time; uint currentAutism = device.Timer.Time; bool autism = false; while (device.Run()) { uint now = device.Timer.Time; float frameDeltaTime = (float)(now - then) / 1000.0f; then = now; if (kinect.isTranslating && (kinect.translation.X < 30 && kinect.translation.X > -30)) { mfX -= (int)(kinect.translation.X); mfY -= (int)(kinect.translation.Y); Console.WriteLine(mouseX + ", " + mouseY + " ----------------- " + (int)(kinect.translation.X) + ", " + (int)(kinect.translation.Y)); } kinect.resetTranslation(); /* * if (getDistance((int)mfX, (int)mfY, 512, 384) > 150) * { * mfX = 512; mfY= 384; * }*/ mouseX = Math.Abs((int)mfX) % 1024; mouseY = Math.Abs((int)mfY) % 768; //mouseX = kinect.position.X; device.CursorControl.Position = new Vector2Di(mouseX, mouseY); if (!potterWheelDown && IsKeyDown(KeyCode.Up)) { potterWheelDown = true; deltaAngle = 1.0d; potterWheelActivate = !potterWheelActivate; } else if (!IsKeyDown(KeyCode.Up)) { potterWheelDown = false; } if (!leftKeyPressed && IsKeyDown(KeyCode.Left)) { leftKeyPressed = true; deltaAngle /= 2; } else if (!IsKeyDown(KeyCode.Left)) { leftKeyPressed = false; } if (!rightKeyPressed && IsKeyDown(KeyCode.Right)) { rightKeyPressed = true; deltaAngle *= 2; } else if (!IsKeyDown(KeyCode.Right)) { rightKeyPressed = false; } if (potterWheelActivate) { angle -= 700.0f * deltaAngle * frameDeltaTime; } if (angle > 360) { angle -= 360; } else if (angle < 0) { angle += 360; } if (angleY > 360) { angle -= 360; } else if (angleY < 0) { angleY += 360; } driver.BeginScene(true, true, new Color(100, 101, 140)); camera.Target = new Vector3Df(0, 0, 0); double temp = Math.Cos(angleY * PI / 180.0) * distance; double X = Math.Sin(angle * PI / 180.0) * temp; double Y = Math.Sin(angleY * PI / 180.0) * distance; double Z = Math.Cos(angle * PI / 180.0) * temp; camera.Position = new Vector3Df((float)X, (float)Y, (float)Z); smgr.DrawAll(); gui.DrawAll(); driver.SetMaterial(m); Triangle3Df triangle = interpolateFrom2D(new Vector2Di(mouseX, mouseY)); if (kinect.isMorphing && kinect.morphDist > 0) //if (IsKeyDown(KeyCode.KeyW)) { //Console.WriteLine("PRESSED KEY"); triangle.A *= new Vector3Df(0.5f); triangle.B *= new Vector3Df(0.5f); triangle.C *= new Vector3Df(0.5f); if (isCyl) { deformCyl(t, triangle.A, new Vector3Df(.5f / (potterWheelActivate ? (float)(1 / deltaAngle) : 60f)), triangle); } else { deformMesh(t, triangle.A, new Vector3Df(.5f / (potterWheelActivate ? (float)(1 / deltaAngle) : 60f)), triangle); } } else if (kinect.isMorphing && kinect.morphDist < 0) { //Console.WriteLine("PRESSED KEY"); triangle.A *= new Vector3Df(1.5f); triangle.B *= new Vector3Df(1.5f); triangle.C *= new Vector3Df(1.5f); if (isCyl) { deformCyl(t, triangle.A, new Vector3Df(-.5f / (potterWheelActivate ? (float)(1 / deltaAngle) : 60f)), triangle); } else { deformMesh(t, triangle.A, new Vector3Df(-.5f / (potterWheelActivate ? (float)(1 / deltaAngle) : 60f)), triangle); } } if (kinect.isZoom && kinect.zoomDist < 0) { if (distance < 300.0f) { distance += .0625f; } } if (kinect.isZoom && kinect.zoomDist > 0) { if (distance > 150) { distance -= .0625f; } } if (kinect.isRotating && kinect.rotation > 0) { angle += 200 * frameDeltaTime; } if (kinect.isRotating && kinect.rotation < 0) { angle -= 200 * frameDeltaTime; } //Change shape if (IsKeyDown(KeyCode.KeyA)) { IrrlichtLime.IO.WriteFile file = device.FileSystem.CreateWriteFile("./Saved.obj"); writeMesh(file, t.Mesh, 1); mail(); } if (IsKeyDown(KeyCode.KeyC)) { isCyl = true; t.Remove(); t = smgr.AddMeshSceneNode(smgr.GetMesh("pill.obj")); //MeshSceneNode t = smgr.AddMeshSceneNode(cyl); triselect = smgr.CreateTriangleSelector(t.Mesh, t); t.TriangleSelector = triselect; triselect.Drop(); //t = smgr.AddMeshSceneNode(smgr.GetMesh("../../media/sphere.x")); //smgr t.SetMaterialTexture(0, driver.GetTexture("rockwall.jpg")); //t.SetMaterialFlag(MaterialFlag.Lighting, true); t.GetMaterial(0).SpecularColor.Set(0, 0, 0); //t.GetMaterial(0).Lighting = true; t.GetMaterial(0).NormalizeNormals = false; t.SetMaterialType(newMaterialType1); } else if (IsKeyDown(KeyCode.KeyS)) { isCyl = false; t.Remove(); t = smgr.AddSphereSceneNode(ballRadius, 32); triselect = smgr.CreateTriangleSelector(t.Mesh, t); t.TriangleSelector = triselect; triselect.Drop(); //t = smgr.AddMeshSceneNode(smgr.GetMesh("../../media/sphere.x")); //smgr t.SetMaterialTexture(0, driver.GetTexture("rockwall.jpg")); //t.SetMaterialFlag(MaterialFlag.Lighting, true); t.GetMaterial(0).SpecularColor.Set(0, 0, 0); //t.GetMaterial(0).Lighting = true; t.GetMaterial(0).NormalizeNormals = false; t.SetMaterialType(newMaterialType1); } driver.EndScene(); } device.Drop(); }