Пример #1
0
        void bezierVb_Created(object sender, EventArgs e)
        {
            bezierVbLock.WaitOne();
            ReadOnlyCollection <Q3BspFace> faces = map.Faces;
            Q3BspFace face;

            GraphicsStream bezierVbStream = bezierVb.Lock(0, 0, LockFlags.Discard);

            int vertexBufferindex = 0;

            for (int faceIndex = 0; faceIndex < faces.Count; faceIndex++)
            {
                face = faces [faceIndex];

                if (face.type == Q3BspFaceType.Patch)
                {
                    Q3BspPatch patch = face.patch;

                    if (patch != null)
                    {
                        for (int bezierIndex = 0; bezierIndex < patch.size; bezierIndex++)
                        {
                            Q3BezierPatch bezier = patch.bezier [bezierIndex];

                            bezier.baseVertexIndex = vertexBufferindex;

                            for (uint vertex = 0; vertex < bezier.vertices.Length; vertex++)
                            {
                                Q3BspVertex q3v = bezier.vertices [vertex];
                                bezierVbStream.Write(new Q3VertexFormats.PositionNormalTexturedLightened(
                                                         q3v.position, q3v.normal, q3v.texcoord [0], q3v.texcoord [1]));
                                vertexBufferindex++;
                            }
                        }
                    }
                }
            }

            bezierVb.Unlock();
            bezierVbLock.Set();
        }
Пример #2
0
        void bezierIb_Created(object sender, EventArgs e)
        {
            bezierIbLock.WaitOne();
            GraphicsStream bezierIbStream        = bezierIb.Lock(0, 0, LockFlags.Discard);
            ReadOnlyCollection <Q3BspFace> faces = map.Faces;
            Q3BspFace face;

            int indexBufferindex = 0;

            for (int faceIndex = 0; faceIndex < faces.Count; faceIndex++)
            {
                face = faces [faceIndex];

                if (face.type == Q3BspFaceType.Patch)
                {
                    Q3BspPatch patch = face.patch;

                    if (patch != null)
                    {
                        for (int bezierIndex = 0; bezierIndex < patch.size; bezierIndex++)
                        {
                            Q3BezierPatch bezier = patch.bezier [bezierIndex];
                            bezier.baseBufferIndex = indexBufferindex;

                            for (uint index = 0; index < bezier.indices.Length; index++)
                            {
                                bezierIbStream.Write(bezier.indices [index]);
                                indexBufferindex++;
                            }
                        }
                    }
                }
            }

            bezierIb.Unlock();
            bezierIbLock.Set();
        }
Пример #3
0
        public void FromUnsafe(Q3BspFaceUnsafe s, Q3BspVertex [] vertices)
        {
            texture     = s.texture;
            effect      = s.effect;
            type        = ( Q3BspFaceType )s.type;
            vertex      = s.vertex;
            n_vertexes  = s.n_vertexes;
            meshvert    = s.meshvert;
            n_meshverts = s.n_meshverts;
            n_triangles = s.n_meshverts / 3;
            lm_index    = s.lm_index;

            lm_start = new int [2];
            lm_size  = new int [2];
            lm_vecs  = new Vector3 [2];

            unsafe {
                lm_start [0] = s.lm_start [0];
                lm_start [1] = s.lm_start [1];
                lm_size [0]  = s.lm_size [0];
                lm_size [1]  = s.lm_size [1];
                lm_origin    = new Vector3(s.lm_origin [0], s.lm_origin [2], s.lm_origin [1]);
                lm_vecs [0]  = new Vector3(s.lm_vecs [0], s.lm_vecs [1], s.lm_vecs [2]);
                lm_vecs [1]  = new Vector3(s.lm_vecs [3], s.lm_vecs [4], s.lm_vecs [5]);
                // swapping axes
                normal = new Vector3(s.normal [0], s.normal [2], s.normal [1]);
                width  = s.size [0];
                height = s.size [1];
            }

            if (type == Q3BspFaceType.Patch)
            {
                Q3BspPatch q3patch = new Q3BspPatch();

                int patch_size_x       = (width - 1) / 2;
                int patch_size_y       = (height - 1) / 2;
                int num_bezier_patches = patch_size_y * patch_size_x;

                q3patch.size   = num_bezier_patches;
                q3patch.bezier = new Q3BezierPatch [q3patch.size];

                int patchIndex = 0;
                int ii, n, j, nn;

                for (ii = 0, n = 0; n < patch_size_x; n++, ii = 2 * n)
                {
                    for (j = 0, nn = 0; nn < patch_size_y; nn++, j = 2 * nn)
                    {
                        Q3BezierPatch bezier = new Q3BezierPatch();

                        for (int ctr = 0, index = 0; ctr < 3; ctr++)
                        {
                            int pos  = ctr * width;
                            int vIdx = vertex + ii + width * j + pos;

                            bezier.controls [index++] = vertices [vIdx].Copy();
                            bezier.controls [index++] = vertices [vIdx + 1].Copy();
                            bezier.controls [index++] = vertices [vIdx + 2].Copy();
                        }

                        bezier.Tessellate(5);
                        q3patch.bezier [patchIndex] = bezier;
                        patchIndex++;
                    }
                }

                patch = q3patch;
            }
        }
Пример #4
0
        public Renderer(Q3Map map, Device d3dDevice)
        {
            bufferLocks [0] = ibLock;
            bufferLocks [1] = vbLock;
            bufferLocks [2] = bezierIbLock;
            bufferLocks [3] = bezierVbLock;

            this.d3dDevice = d3dDevice;
            this.Map       = map;

            input = new Input(d3dDevice.PresentationParameters.DeviceWindow,
                              DI.CooperativeLevelFlags.Background | DI.CooperativeLevelFlags.NonExclusive,
                              DI.CooperativeLevelFlags.Background | DI.CooperativeLevelFlags.NonExclusive);
            int w = d3dDevice.PresentationParameters.BackBufferWidth;
            int h = d3dDevice.PresentationParameters.BackBufferHeight;

            fpsCamera = new FpsCamera(input, ( float )w / ( float )h, 105.0f * ( float )System.Math.PI / 360.0f, 1.0f, 10000.0f);

            List <Dictionary <string, object> > respawns;

            if (map.GroupedEntities.TryGetValue("info_player_deathmatch", out respawns))
            {
                Random rnd     = new Random();
                int    respIdx = rnd.Next(respawns.Count);
                Dictionary <string, object> props = respawns [respIdx];
                Vector3 respPos = ( Vector3 )props ["origin"];
                //float angle = ( float ) props ["angle"];
                fpsCamera.Position = respPos;
            }

            // Create Vertex Buffer
            vb = new VertexBuffer(typeof(Q3VertexFormats.PositionNormalTexturedLightened),
                                  map.Vertices.Count,
                                  d3dDevice,
                                  Usage.WriteOnly,
                                  Q3VertexFormats.PositionNormalTexturedLightened.Format,
                                  Pool.Default);
            vb.Created += new EventHandler(vb_Created);
            vb_Created(this, null);

            // Create Index Buffer
            ib = new IndexBuffer(typeof(int),
                                 map.MeshVertices.Count,
                                 d3dDevice,
                                 Usage.WriteOnly,
                                 Pool.Default);
            ib.Created += new EventHandler(ib_Created);
            ib_Created(this, null);

            // Calculate Bezier Index Buffer size and ViertexBuffer Size
            int numIndex  = 0;
            int numVertex = 0;

            ReadOnlyCollection <Q3BspFace> faces = map.Faces;
            Q3BspFace face;

            for (int i = 0; i < faces.Count; i++)
            {
                face = faces [i];

                if (face.type == Q3BspFaceType.Patch)
                {
                    Q3BspPatch patch = face.patch;

                    if (patch != null)
                    {
                        for (int j = 0; j < patch.size; j++)
                        {
                            numIndex  += patch.bezier [j].indices.Length;
                            numVertex += patch.bezier [j].vertices.Length;
                        }
                    }
                }
            }

            if (numIndex != 0 && numVertex != 0)
            {
                // Create bezier buffers
                bezierIb = new IndexBuffer(typeof(int),
                                           numIndex,
                                           d3dDevice,
                                           Usage.WriteOnly,
                                           Pool.Default);
                bezierIb.Created += new EventHandler(bezierIb_Created);
                bezierIb_Created(this, null);

                bezierVb = new VertexBuffer(typeof(Q3VertexFormats.PositionNormalTexturedLightened),
                                            numVertex,
                                            d3dDevice,
                                            Usage.WriteOnly,
                                            Q3VertexFormats.PositionNormalTexturedLightened.Format,
                                            Pool.Default);
                bezierVb.Created += new EventHandler(bezierVb_Created);
                bezierVb_Created(this, null);
            }

            font = new Font(d3dDevice, new SysDraw.Font("Courier New", fontSize, fontStyle));
        }