private void InitializeGraphics() { m_cullinfo = new CULLINFO(); InitD3D(); InitDSound(); DplayForm dialog = new DplayForm(); if (dialog.ShowDialog(this) == DialogResult.OK) { StartServer(); } else { InitDirectPlay(); } Cursor.Hide(); map = new Radar(d3dDevice, this); // init objects geometry landscape = new Landscape(d3dDevice); myAirplane = new MyAirplane(d3dDevice, this.Font, dsoundDevice, landscape); aplane = new airplane(d3dDevice); myAirplane.m_vPosition.X = 22000.0f; myAirplane.m_vPosition.Z = 44000.0f; myAirplane.m_vPosition.Y = myAirplane.m_vPosition.Y - landscape.Intersect(myAirplane.m_vPosition, new Vector3(0f, -1f, 0f)) + 1.1f; myAirplane.prepos = myAirplane.m_vPosition; myhangar = new hangar(d3dDevice); }
//----------------------------------------------------------------------------- // Name: UpdateCullInfo() // Desc: Sets up the frustum planes, endpoints, and center for the frustum // defined by a given view matrix and projection matrix. This info will // be used when culling each object in CullObject(). //----------------------------------------------------------------------------- private void UpdateCullInfo(CULLINFO pCullInfo, Matrix pMatView, Matrix pMatProj) { Matrix mat = Matrix.Multiply(pMatView, pMatProj); mat.Invert(); pCullInfo.vecFrustum[0] = new Vector3(-1.0f, -1.0f, 0.0f); // xyz pCullInfo.vecFrustum[1] = new Vector3(1.0f, -1.0f, 0.0f); // Xyz pCullInfo.vecFrustum[2] = new Vector3(-1.0f, 1.0f, 0.0f); // xYz pCullInfo.vecFrustum[3] = new Vector3(1.0f, 1.0f, 0.0f); // XYz pCullInfo.vecFrustum[4] = new Vector3(-1.0f, -1.0f, 1.0f); // xyZ pCullInfo.vecFrustum[5] = new Vector3(1.0f, -1.0f, 1.0f); // XyZ pCullInfo.vecFrustum[6] = new Vector3(-1.0f, 1.0f, 1.0f); // xYZ pCullInfo.vecFrustum[7] = new Vector3(1.0f, 1.0f, 1.0f); // XYZ for (int i = 0; i < 8; i++) { pCullInfo.vecFrustum[i] = Vector3.TransformCoordinate(pCullInfo.vecFrustum[i], mat); } pCullInfo.planeFrustum[0] = Plane.FromPoints(pCullInfo.vecFrustum[0], pCullInfo.vecFrustum[1], pCullInfo.vecFrustum[2]); // Near pCullInfo.planeFrustum[1] = Plane.FromPoints(pCullInfo.vecFrustum[6], pCullInfo.vecFrustum[7], pCullInfo.vecFrustum[5]); // Far pCullInfo.planeFrustum[2] = Plane.FromPoints(pCullInfo.vecFrustum[2], pCullInfo.vecFrustum[6], pCullInfo.vecFrustum[4]); // Left pCullInfo.planeFrustum[3] = Plane.FromPoints(pCullInfo.vecFrustum[7], pCullInfo.vecFrustum[3], pCullInfo.vecFrustum[5]); // Right pCullInfo.planeFrustum[4] = Plane.FromPoints(pCullInfo.vecFrustum[2], pCullInfo.vecFrustum[3], pCullInfo.vecFrustum[6]); // Top pCullInfo.planeFrustum[5] = Plane.FromPoints(pCullInfo.vecFrustum[1], pCullInfo.vecFrustum[0], pCullInfo.vecFrustum[4]); // Bottom }
protected bool InitD3D() { m_cullinfo = new CULLINFO(); m_cullinfo.vecFrustum = new Vector3[8]; m_cullinfo.planeFrustum = new Plane[6]; // direct 3d parameters PresentParameters d3dpp = new PresentParameters(); d3dpp.Windowed = true; d3dpp.SwapEffect = SwapEffect.Discard; d3dpp.EnableAutoDepthStencil = true; if (Manager.CheckDeviceFormat(0, DeviceType.Hardware, Manager.Adapters.Default.CurrentDisplayMode.Format, Usage.DepthStencil, ResourceType.Surface, DepthFormat.D24S8) && Manager.CheckDepthStencilMatch(0, DeviceType.Hardware, Manager.Adapters.Default.CurrentDisplayMode.Format, Manager.Adapters.Default.CurrentDisplayMode.Format, DepthFormat.D24S8)) { d3dpp.AutoDepthStencilFormat = DepthFormat.D24S8; } else { MessageBox.Show(this, "Your hardware doesn't support 32 bits Stencil Buffer, try setting the display to 32 bits mode.\n We will use 16 bits format but the game is going to look funny."); // simple=true; d3dpp.AutoDepthStencilFormat = DepthFormat.D16; } // create the direct3d device d3dDevice = new Device(0, DeviceType.Hardware, this, CreateFlags.MultiThreaded | CreateFlags.SoftwareVertexProcessing, d3dpp); // Setup the event handlers for our device // d3dDevice.DeviceLost += new System.EventHandler(this.InvalidateDeviceObjects); d3dDevice.DeviceReset += new System.EventHandler(this.RestoreDeviceObjects); // d3dDevice.Disposing += new System.EventHandler(this.DeleteDeviceObjects); // d3dDevice.DeviceResizing += new System.ComponentModel.CancelEventHandler(this.EnvironmentResized); RestoreDeviceObjects(null, null); return(true); }
public void Render(CULLINFO m_cullinfo, Vector3 myposition) { if (drawing) { return; } drawing = true; if (!simple) { for (int i = 0; i < 81; i++) { float distance = DistanceToLand(myposition, i); if (distance < 70000f) { // if close enough, load it into memory loadgrid(i); // if visible on camara, render it if (CullObject(m_cullinfo, map[i].m_vecBoundsWorld, map[i].m_planeBoundsWorld)) { RenderSquare(i); } } else if (distance > 125000f) { // if it is very far away, remove it from memory // so we free some ram disposegrid(i); } } } else { RenderSquare(0); } drawing = false; }
//----------------------------------------------------------------------------- // Name: CullObject() // Desc: Determine the cullstate for an object bounding box (OBB). // The algorithm is: // 1) If any OBB corner pt is inside the frustum, return CS_INSIDE // 2) Else if all OBB corner pts are outside a single frustum plane, // return CS_OUTSIDE // 3) Else if any frustum edge penetrates a face of the OBB, return // CS_INSIDE_SLOW // 4) Else if any OBB edge penetrates a face of the frustum, return // CS_INSIDE_SLOW // 5) Else if any point in the frustum is outside any plane of the // OBB, return CS_OUTSIDE_SLOW // 6) Else return CS_INSIDE_SLOW //----------------------------------------------------------------------------- private bool CullObject(CULLINFO pCullInfo, Vector3[] pVecBounds, Plane[] pPlaneBounds) { byte[] bOutside = { 0, 0, 0, 0, 0, 0, 0, 0 }; // Check boundary vertices in the box (8) against all 6 frustum planes, // and store result (1 if outside) in a bitfield for (int iPoint = 0; iPoint < 8; iPoint++) { int k = 0; for (int iPlane = 0; iPlane < 6; iPlane++) { if ((pCullInfo.planeFrustum[iPlane].A * pVecBounds[iPoint].X + pCullInfo.planeFrustum[iPlane].B * pVecBounds[iPoint].Y + pCullInfo.planeFrustum[iPlane].C * pVecBounds[iPoint].Z + pCullInfo.planeFrustum[iPlane].D) < 0.0f) { // this vertex is outside this plane, shift 1 bOutside[iPoint] |= (byte)(1 << iPlane); k++; } } // If any point is inside all 6 frustum planes, it is inside // the frustum, so the object must be rendered. if (bOutside[iPoint] == 0) { return(true); } } // If all points are outside any single frustum plane, the object is // outside the frustum if ((bOutside[0] & bOutside[1] & bOutside[2] & bOutside[3] & bOutside[4] & bOutside[5] & bOutside[6] & bOutside[7]) != 0) { return(false); } // Now see if any of the frustum edges penetrate any of the faces of // the bounding box Vector3 [][] edge = { new Vector3[2] { pCullInfo.vecFrustum[0], pCullInfo.vecFrustum[1] }, // front bottom new Vector3[2] { pCullInfo.vecFrustum[2], pCullInfo.vecFrustum[3] }, // front top new Vector3[2] { pCullInfo.vecFrustum[0], pCullInfo.vecFrustum[2] }, // front left new Vector3[2] { pCullInfo.vecFrustum[1], pCullInfo.vecFrustum[3] }, // front right new Vector3[2] { pCullInfo.vecFrustum[4], pCullInfo.vecFrustum[5] }, // back bottom new Vector3[2] { pCullInfo.vecFrustum[6], pCullInfo.vecFrustum[7] }, // back top new Vector3[2] { pCullInfo.vecFrustum[4], pCullInfo.vecFrustum[6] }, // back left new Vector3[2] { pCullInfo.vecFrustum[5], pCullInfo.vecFrustum[7] }, // back right new Vector3[2] { pCullInfo.vecFrustum[0], pCullInfo.vecFrustum[4] }, // left bottom new Vector3[2] { pCullInfo.vecFrustum[2], pCullInfo.vecFrustum[6] }, // left top new Vector3[2] { pCullInfo.vecFrustum[1], pCullInfo.vecFrustum[5] }, // right bottom new Vector3[2] { pCullInfo.vecFrustum[3], pCullInfo.vecFrustum[7] }, // right top }; Vector3 [][] face = { new Vector3[4] { pVecBounds[0], pVecBounds[2], pVecBounds[3], pVecBounds[1] }, // front new Vector3[4] { pVecBounds[4], pVecBounds[5], pVecBounds[7], pVecBounds[6] }, // back new Vector3[4] { pVecBounds[0], pVecBounds[4], pVecBounds[6], pVecBounds[2] }, // left new Vector3[4] { pVecBounds[1], pVecBounds[3], pVecBounds[7], pVecBounds[5] }, // right new Vector3[4] { pVecBounds[2], pVecBounds[6], pVecBounds[7], pVecBounds[3] }, // top new Vector3[4] { pVecBounds[0], pVecBounds[4], pVecBounds[5], pVecBounds[1] }, // bottom }; int iEdge; for (iEdge = 0; iEdge < 12; iEdge++) { for (int iFace = 0; iFace < 6; iFace++) { if (EdgeIntersectsFace(edge[iEdge], face[iFace], pPlaneBounds[iFace])) { return(true); } } } // Now see if any of the bounding box edges penetrate any of the faces of // the frustum Vector3 [][] edge2 = { new Vector3[2] { pVecBounds[0], pVecBounds[1] }, // front bottom new Vector3[2] { pVecBounds[2], pVecBounds[3] }, // front top new Vector3[2] { pVecBounds[0], pVecBounds[2] }, // front left new Vector3[2] { pVecBounds[1], pVecBounds[3] }, // front right new Vector3[2] { pVecBounds[4], pVecBounds[5] }, // back bottom new Vector3[2] { pVecBounds[6], pVecBounds[7] }, // back top new Vector3[2] { pVecBounds[4], pVecBounds[6] }, // back left new Vector3[2] { pVecBounds[5], pVecBounds[7] }, // back right new Vector3[2] { pVecBounds[0], pVecBounds[4] }, // left bottom new Vector3[2] { pVecBounds[2], pVecBounds[6] }, // left top new Vector3[2] { pVecBounds[1], pVecBounds[5] }, // right bottom new Vector3[2] { pVecBounds[3], pVecBounds[7] }, // right top }; Vector3 [][] face2 = { new Vector3[4] { pCullInfo.vecFrustum[0], pCullInfo.vecFrustum[2], pCullInfo.vecFrustum[3], pCullInfo.vecFrustum[1] }, // front new Vector3[4] { pCullInfo.vecFrustum[4], pCullInfo.vecFrustum[5], pCullInfo.vecFrustum[7], pCullInfo.vecFrustum[6] }, // back new Vector3[4] { pCullInfo.vecFrustum[0], pCullInfo.vecFrustum[4], pCullInfo.vecFrustum[6], pCullInfo.vecFrustum[2] }, // left new Vector3[4] { pCullInfo.vecFrustum[1], pCullInfo.vecFrustum[3], pCullInfo.vecFrustum[7], pCullInfo.vecFrustum[5] }, // right new Vector3[4] { pCullInfo.vecFrustum[2], pCullInfo.vecFrustum[6], pCullInfo.vecFrustum[7], pCullInfo.vecFrustum[3] }, // top new Vector3[4] { pCullInfo.vecFrustum[0], pCullInfo.vecFrustum[4], pCullInfo.vecFrustum[5], pCullInfo.vecFrustum[1] }, // bottom }; for (iEdge = 0; iEdge < 12; iEdge++) { for (int iFace = 0; iFace < 6; iFace++) { if (EdgeIntersectsFace(edge2[iEdge], face2[iFace], pCullInfo.planeFrustum[iFace])) { return(true); } } } // Now see if frustum is contained in bounding box // If any frustum corner point is outside any plane of the bounding box, // the frustum is not contained in the bounding box, so the object // is outside the frustum for (int iPlane = 0; iPlane < 6; iPlane++) { if (pPlaneBounds[iPlane].A * pCullInfo.vecFrustum[0].X + pPlaneBounds[iPlane].B * pCullInfo.vecFrustum[0].Y + pPlaneBounds[iPlane].C * pCullInfo.vecFrustum[0].Z + pPlaneBounds[iPlane].D < 0.0f) { return(false); } } // Bounding box must contain the frustum, so render the object return(true); }