public MeshObj(MeshObj m) { this.meshMaterials = m.meshMaterials; this.meshTextures = m.meshTextures; this.mesh = m.mesh; this.radius = m.radius; this.distanceZ = m.distanceZ; this.distanceX = m.distanceX; this.distanceY = m.distanceY; }
private void LoadMesh(string file, MeshObj obj) { ExtendedMaterial[] mtrl; Material[] meshMaterials; Texture[] meshTextures; mesh = Mesh.FromFile(file, MeshFlags.Managed, device, out mtrl); if ((mtrl != null) && (mtrl.Length > 0)) { meshMaterials = new Material[mtrl.Length]; meshTextures = new Texture[mtrl.Length]; for (int i = 0; i < mtrl.Length; i++) { meshMaterials[i] = mtrl[i].Material3D; if ((mtrl[i].TextureFilename != null) && (mtrl[i].TextureFilename != string.Empty)) meshTextures[i] = TextureLoader.FromFile(device, @"..\..\Mesh\" + mtrl[i].TextureFilename); } VertexBuffer vb = mesh.VertexBuffer; float radius; try { GraphicsStream stm = vb.Lock(0, 0, LockFlags.None); Vector3 center; radius = Geometry.ComputeBoundingSphere(stm, mesh.NumberVertices, mesh.VertexFormat, out center); obj.radius = radius; } finally { vb.Unlock(); vb.Dispose(); } obj.mesh = mesh; obj.meshMaterials = meshMaterials; obj.meshTextures = meshTextures; } }
private void DrawSimpleMesh(float yaw, float pitch, float roll, float x, float y, float z, MeshObj obj) { device.Transform.World = Matrix.RotationYawPitchRoll(yaw, pitch, roll) * Matrix.Translation(x, y, z); obj.mesh.DrawSubset(0); }
private void DrawMesh(float yaw, float pitch, float roll, float x, float y, float z, MeshObj obj) { device.Transform.World = Matrix.RotationYawPitchRoll(yaw, pitch, roll) * Matrix.Translation(x, y, z) * Matrix.Scaling(50.0f, 50.0f, 50.0f); for (int i = 0; i < obj.meshMaterials.Length; i++) { device.Material = obj.meshMaterials[i]; device.SetTexture(0, obj.meshTextures[i]); obj.mesh.DrawSubset(i); } }
protected override void OnPaint(PaintEventArgs e) { device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.CornflowerBlue, 1.0f, 0); SetupCamera(); // dRoad += 5.0f; if (speed < 100.0f) speed += 0.1f; if (collision) { collisionTimer -= 0.1f; if (collisionTimer > 0.0f) angle += 0.01f * collisionObstPos; else if (collisionTimer < 0.0f) angle -= 0.01f * collisionObstPos; if (collisionTimer <= -10.0f) { angle = 0.0f; collision = false; collisionTimer = 0.0f; } } mouseX = 0.0f; ReadKeyboard(); if (car.distanceX + mouseX < 100.0f && car.distanceX + mouseX > -100.0f) car.distanceX += mouseX*(60.0f/speed); carsTimer += 0.1f; if (carsTimer >= 20.0f) { Random rnd = new Random(); int rndObstacle = rnd.Next(1, 3); MeshObj newObstacle = new MeshObj(); switch (rndObstacle) { case 1: { newObstacle = new MeshObj(car1); break; } case 2: { newObstacle = new MeshObj(car2); break; } default: break; } int zpos = rnd.Next(0, 11); newObstacle.distanceZ = -5000.0f -zpos * 256.0f; int pos = rnd.Next(0, 2); newObstacle.distanceX = 80.0f - 160.0f * pos; newObstacle.distanceY = 30.0f; obstacles.Add(newObstacle); carsTimer = 0.0f; } obstacles.RemoveAll(removeObst); foreach (MeshObj c in obstacles) { c.distanceZ += 5.0f * ((speed - 20.0f)/20.0f); DrawCarMesh(0, -(float)Math.PI / 2, 0, c.distanceX, 30.0f, c.distanceZ, c); if (Math.Abs(c.distanceZ+300.0f) < c.radius + car.radius) if (Math.Abs(c.distanceX - car.distanceX) < 80.0f) { collision = true; speed /= 2.0f; collisionTimer = 10.0f; if (c.distanceX > 0) { c.distanceX = 200.0f; collisionObstPos = -1; } else { c.distanceX = -200.0f; collisionObstPos = 1; } } } device.BeginScene(); device.SetStreamSource(0, vbhor, 0); DrawBox(0, (float)Math.PI / 2, 0, -500.0f, 8000.0f, -24900.0f, horizon); device.SetStreamSource(0, vb, 0); for (int i = 0; i < roadCoords.Count; i++) { roadCoords[i] += speed; DrawBox(0, 0, 0, 0.0f, 0.0f, roadCoords[i], road); for (int j = 0; j < 10; j++) { DrawBox(0, 0, 0, -1024.0f - 512.0f * j, 0.0f, roadCoords[i], grass); } for (int j = 0; j < 10; j++) { DrawBox(0, 0, 0, 1024.0f + 512.0f * j, 0.0f, roadCoords[i], grass); } } roadCoords.RemoveAll(RemoveAndAdd); if (collision) DrawCarMesh(angle/(float)Math.PI*2.0f, -(float)Math.PI / 2, 0, car.distanceX, 30.0f, -300.0f, car); else DrawCarMesh(0, -(float)Math.PI / 2, 0, car.distanceX, 30.0f, -300.0f, car); // DrawMesh(0.0f, (float)Math.PI/2, 0, car.distanceX, 30.0f, -300.0f, car); device.EndScene(); device.Present(); this.Invalidate(); }
public bool removeObst(MeshObj o) { if (o.distanceZ > 700.0f + 512.0f) return true; return false; }