Exemplo n.º 1
0
 internal BitmapSurface(TextureResource texture)
 {
     _pixels        = texture.Pixels;
     _width         = texture.Width;
     _height        = texture.Height;
     _is8bit        = false;
     _containsAlpha = texture.ContainsAlpha;
 }
Exemplo n.º 2
0
        public void Draw(TextureResource texture, Rect destination, Rect?source, Color color, float layerDepth)
        {
            Sprite s;

            s.Texture     = texture;
            s.Source      = (source.HasValue ? source.Value : new Rect(0, 0, texture.Width, texture.Height));
            s.Destination = destination;
            s.Color       = color;

            _sprites.Add(s);
        }
Exemplo n.º 3
0
        public void Draw(TextureResource texture, Math.Vector2 position)
        {
            Rect destination;

            destination.X = position.X;
            destination.Y = position.Y;

            destination.Width  = texture.Width;
            destination.Height = texture.Height;

            Draw(texture, destination, null, 0);
        }
Exemplo n.º 4
0
        public void Draw(TextureResource texture, Math.Vector2 destination, Rect?source, Color color, float layerDepth)
        {
            Sprite s;

            s.Texture            = texture;
            s.Source             = (source.HasValue ? source.Value : new Rect(0, 0, texture.Width, texture.Height));
            s.Destination.X      = destination.X;
            s.Destination.Y      = destination.Y;
            s.Destination.Width  = s.Source.Width;
            s.Destination.Height = s.Source.Height;
            s.Color = color;

            _sprites.Add(s);
        }
Exemplo n.º 5
0
        public void Draw(TextureResource texture, Math.Vector2 position, Rect?source)
        {
            Rect destination;

            destination.X = position.X;
            destination.Y = position.Y;

            if (source.HasValue)
            {
                destination.Width  = source.Value.Width;
                destination.Height = source.Value.Height;
            }
            else
            {
                destination.Width  = texture.Width;
                destination.Height = texture.Height;
            }

            Draw(texture, destination, source, 0);
        }
Exemplo n.º 6
0
        public static void AddBillboard(Math.Vector3 position, float width, float height,
                                        TextureResource texture)
        {
            if (texture == null)
            {
                throw new ArgumentNullException("texture");
            }

            if (_numBillboards < _maxBillboards - 1)
            {
                _billboards[_numBillboards].Position = position;
                _billboards[_numBillboards].Width    = width;
                _billboards[_numBillboards].Height   = height;
                _billboards[_numBillboards].Texture  = texture;
                _billboards[_numBillboards].U        = 0;
                _billboards[_numBillboards].V        = 0;
                _billboards[_numBillboards].USize    = 1.0f;
                _billboards[_numBillboards].VSize    = 1.0f;

                _numBillboards++;
            }
        }
Exemplo n.º 7
0
        internal static void AddBillboard(Math.Vector3 position, float width, float height,
                                          TextureResource diffuse, TextureResource alpha)
        {
            if (diffuse == null)
            {
                throw new ArgumentNullException("diffuse");
            }

            if (_numBillboards < _maxBillboards - 1)
            {
                _billboards[_numBillboards].Position = position;
                _billboards[_numBillboards].Width    = width;
                _billboards[_numBillboards].Height   = height;
                _billboards[_numBillboards].Texture  = diffuse;
                _billboards[_numBillboards].Texture2 = alpha;
                _billboards[_numBillboards].U        = 0;
                _billboards[_numBillboards].V        = 0;
                _billboards[_numBillboards].USize    = 1.0f;
                _billboards[_numBillboards].VSize    = 1.0f;

                _numBillboards++;
            }
        }
Exemplo n.º 8
0
        public void CalcRadiosityPass(Graphics.LightmapResource original, Game.RadiosityMaps radiosity)
        {
            const int batchSize = 256;

            Game.Radiosity.LightmapTexel[] texels = new Game.Radiosity.LightmapTexel[batchSize];

            System.Runtime.InteropServices.GCHandle[] h = new System.Runtime.InteropServices.GCHandle[_surfaces.Length];

            int count = 0;


            int currentBatchIndex = 0;

            foreach (BspSurface surface in _surfaces)
            {
                if (surface.index == 90)
                {
                    count++;
                }

                count++;
                //if (count >950)break;

                h[surface.index] = System.Runtime.InteropServices.GCHandle.Alloc(radiosity.Maps[surface.index].Map, System.Runtime.InteropServices.GCHandleType.Pinned);

                //TextureResource memTex = surface.textureResource;
                TextureResource memTex       = radiosity.Maps[surface.index].MemoryTexture;
                Rect            lightmapRect = original.PackedLightmaps.GetPackedTextureRect((int)surface.index);

                // iterate over each triangle
                for (int tri = 0; tri < surface.VertexCount / 3; tri++)
                {
                    Math.Vector2 a, b, c;

                    a.X = _lightmapcoords[(surface.VertexIndex + tri * 3 + 0) * 2 + 0];
                    a.Y = _lightmapcoords[(surface.VertexIndex + tri * 3 + 0) * 2 + 1];

                    b.X = _lightmapcoords[(surface.VertexIndex + tri * 3 + 1) * 2 + 0];
                    b.Y = _lightmapcoords[(surface.VertexIndex + tri * 3 + 1) * 2 + 1];

                    c.X = _lightmapcoords[(surface.VertexIndex + tri * 3 + 2) * 2 + 0];
                    c.Y = _lightmapcoords[(surface.VertexIndex + tri * 3 + 2) * 2 + 1];

                    Math.Vector3 pa, pb, pc;
                    pa.X = _bspVertices[surface.VertexIndex + tri * 3 + 0].X;
                    pa.Y = _bspVertices[surface.VertexIndex + tri * 3 + 0].Y;
                    pa.Z = _bspVertices[surface.VertexIndex + tri * 3 + 0].Z;

                    pb.X = _bspVertices[surface.VertexIndex + tri * 3 + 1].X;
                    pb.Y = _bspVertices[surface.VertexIndex + tri * 3 + 1].Y;
                    pb.Z = _bspVertices[surface.VertexIndex + tri * 3 + 1].Z;

                    pc.X = _bspVertices[surface.VertexIndex + tri * 3 + 2].X;
                    pc.Y = _bspVertices[surface.VertexIndex + tri * 3 + 2].Y;
                    pc.Z = _bspVertices[surface.VertexIndex + tri * 3 + 2].Z;

                    Math.Vector3 up = (pa - pb).Normalize();
                    Math.Vector3 n  = up.Cross((pa - pc).Normalize()).Normalize();

                    Math.Vector2 minUV, maxUV;
                    minUV.X = System.Math.Min(System.Math.Min(a.X, b.X), c.X);
                    minUV.Y = System.Math.Min(System.Math.Min(a.Y, b.Y), c.Y);
                    maxUV.X = System.Math.Max(System.Math.Max(a.X, b.X), c.X);
                    maxUV.Y = System.Math.Max(System.Math.Max(a.Y, b.Y), c.Y);

                    // map the triangle to the texture
                    int minTX = System.Math.Max((int)(memTex.Width * minUV.X) - 1, 0);
                    int minTY = System.Math.Max((int)(memTex.Height * minUV.Y) - 1, 0);
                    int maxTX = System.Math.Min((int)(System.Math.Ceiling(memTex.Width * maxUV.X + 1)), memTex.Width);
                    int maxTY = System.Math.Min((int)(System.Math.Ceiling(memTex.Height * maxUV.Y + 1)), memTex.Height);

                    float halfPixelU = 1.0f / memTex.Width * 0.5f;
                    float halfPixelV = 1.0f / memTex.Height * 0.5f;

                    // go through each texel and do the radiosity stuff
                    for (int y = minTY; y < maxTY; y++)
                    {
                        for (int x = minTX; x < maxTX; x++)
                        {
                            /* if (x < 0 || y < 0 ||
                             *      x >= radiosity.Maps[surface.index].Width ||
                             *      y >= radiosity.Maps[surface.index].Height)
                             *   continue;*/

                            Math.Vector2 texelUV;

                            texelUV.X = (float)x / memTex.Width + halfPixelU;
                            texelUV.Y = (float)y / memTex.Height + halfPixelV;

                            /*if (memTex.Width > 1)
                             *  texelUV.X = (float)x / memTex.Width + halfPixelU;
                             * else
                             *  texelUV.X = (float)x / memTex.Width;
                             *
                             * if (memTex.Height > 1)
                             *  texelUV.Y = (float)y / memTex.Height + halfPixelV;
                             * else
                             *  texelUV.Y = (float)y / memTex.Height;
                             */

                            // radiosity.Maps[surface.index].Map[(y * radiosity.Maps[surface.index].Width + x) * 3 + 0] = 1000.0f;
                            //radiosity.Maps[surface.index].Map[(y * radiosity.Maps[surface.index].Width + x) * 3 + 1] = 0;
                            // radiosity.Maps[surface.index].Map[(y * radiosity.Maps[surface.index].Width + x) * 3 + 2] = 0;
                            //radiosity.Maps[surface.index].SetColor(x, y, 1000.0f, 0, 0);

                            // are we on the triangle?
                            Math.Vector2 projectedUV;
                            Math.Vector2 texelMin, texelMax;
                            texelMin.X = (float)x / memTex.Width;
                            texelMin.Y = (float)y / memTex.Height;
                            texelMax.X = (float)(x + 1) / memTex.Width;
                            texelMax.Y = (float)(y + 1) / memTex.Height;
                            // if (Gk3Main.Utils.TestTriangleBox(a, b, c, texelMin, texelMax))
                            if (Gk3Main.Utils.TestTriangleBoxAndGetCenterOfMassUV(a, b, c, texelMin, texelMax, out projectedUV))
                            {
                                // Gk3Main.Utils.IsPointInTriangle(texelUV, a, b, c, out projectedUV);

                                // calc the world coordinates
                                Math.Vector3 p3 = pa + (pb - pa) * projectedUV.Y + (pc - pa) * projectedUV.X;
                                //Math.Vector3 p3(p.X, p.Y, p.Z);

                                //Math.Vector4 p4 = worldView * p3;



                                texels[currentBatchIndex].Tag = Gk3Main.Utils.IncrementIntPtr(h[surface.index].AddrOfPinnedObject(), (y * radiosity.Maps[surface.index].Width + x) * 3 * 4);

                                //texels[currentBatchIndex].Tag = (IntPtr)(h[surface.index].AddrOfPinnedObject() + (IntPtr)((y * radiosity.Maps[surface.index].Width + x) * 3 * 4);
                                texels[currentBatchIndex].PosX    = p3.X;
                                texels[currentBatchIndex].PosY    = p3.Y;
                                texels[currentBatchIndex].PosZ    = p3.Z;
                                texels[currentBatchIndex].NormalX = n.X;
                                texels[currentBatchIndex].NormalY = n.Y;
                                texels[currentBatchIndex].NormalZ = n.Z;
                                texels[currentBatchIndex].UpX     = up.X;
                                texels[currentBatchIndex].UpY     = up.Y;
                                texels[currentBatchIndex].UpZ     = up.Z;
                                texels[currentBatchIndex].Red     = 0;
                                texels[currentBatchIndex].Green   = 0;
                                texels[currentBatchIndex].Blue    = 0;

                                //rad_CalcPass(p4.getX(), p4.getY(), p4.getZ(),
                                //    n.X, n.Y, n.Z,
                                //    &results[(y * texture->GetWidth() + x) * 3 + 0],
                                //    &results[(y * texture->GetWidth() + x) * 3 + 1],
                                //    &results[(y * texture->GetWidth() + x) * 3 + 2]);

                                //results[(y * texture->GetWidth() + x) * 3 + 0] *= 0.1f;
                                //results[(y * texture->GetWidth() + x) * 3 + 1] *= 0.1f;
                                //results[(y * texture->GetWidth() + x) * 3 + 2] *= 0.1f;

                                currentBatchIndex++;

                                if (currentBatchIndex >= batchSize)
                                {
                                    Game.Radiosity.CalcPass(texels, currentBatchIndex);

                                    for (int i = 0; i < currentBatchIndex; i++)
                                    {
                                        //int index = texels[i].Tag.ToInt32() * 3;

                                        unsafe
                                        {
                                            float *f = (float *)texels[i].Tag.ToPointer();

                                            /*radiosity.Maps[surface.index].Map[index + 0] = texels[i].Red;
                                             * radiosity.Maps[surface.index].Map[index + 1] = texels[i].Green;
                                             * radiosity.Maps[surface.index].Map[index + 2] = texels[i].Blue;*/


                                            f[0] = System.Math.Max(texels[i].Red, f[0]);
                                            f[1] = System.Math.Max(texels[i].Green, f[1]);
                                            f[2] = System.Math.Max(texels[i].Blue, f[2]);
                                        }

                                        //texels[i].r
                                    }

                                    currentBatchIndex = 0;
                                }
                            }
                            else
                            {
                                currentBatchIndex = currentBatchIndex;
                            }
                        }
                    }
                }
            }

            if (currentBatchIndex > 0)
            {
                Game.Radiosity.CalcPass(texels, currentBatchIndex);

                for (int i = 0; i < currentBatchIndex; i++)
                {
                    unsafe
                    {
                        float *f = (float *)texels[i].Tag.ToPointer();

                        /*radiosity.Maps[surface.index].Map[index + 0] = texels[i].Red;
                         * radiosity.Maps[surface.index].Map[index + 1] = texels[i].Green;
                         * radiosity.Maps[surface.index].Map[index + 2] = texels[i].Blue;*/

                        f[0] = System.Math.Max(texels[i].Red, f[0]);
                        f[1] = System.Math.Max(texels[i].Green, f[1]);
                        f[2] = System.Math.Max(texels[i].Blue, f[2]);
                    }

                    /*int index = texels[i].Tag.ToInt32() * 3;
                     * radiosity.Maps[surface.index].Map[index + 0] = texels[i].Red;
                     * radiosity.Maps[surface.index].Map[index + 1] = texels[i].Green;
                     * radiosity.Maps[surface.index].Map[index + 2] = texels[i].Blue;
                     *
                     */
                    //texels[i].r
                }
            }
        }
Exemplo n.º 9
0
        public void Render(Camera camera, LightmapResource lightmaps, bool calculatingRadiosity)
        {
            Effect currentEffect;
            bool   lightmappingEnabled = false;

            if (SceneManager.CalculatingRadiosity)
            {
                currentEffect       = _radiosityEffect;
                lightmappingEnabled = true;
            }
            else if (SceneManager.LightmapsEnabled && lightmaps != null)
            {
                if (SceneManager.CurrentShadeMode == ShadeMode.Flat)
                {
                    currentEffect       = _lightmapNoTextureEffect;
                    lightmappingEnabled = true;
                }
                else
                {
                    currentEffect       = _lightmapEffect;
                    lightmappingEnabled = true;
                }
            }
            else
            {
                if (SceneManager.CurrentShadeMode == ShadeMode.Textured)
                {
                    currentEffect = _basicTexturedEffect;
                }
                else
                {
                    return; // nothing to render
                }
            }

            if (SceneManager.CurrentFilterMode == TextureFilterMode.None)
            {
                RendererManager.CurrentRenderer.SamplerStates[0] = SamplerState.PointWrap;
                RendererManager.CurrentRenderer.SamplerStates[1] = SamplerState.PointWrap;
            }
            else if (SceneManager.CurrentFilterMode == TextureFilterMode.Anisotropic4X)
            {
                RendererManager.CurrentRenderer.SamplerStates[0] = SamplerState.AnisotropicWrap;
                RendererManager.CurrentRenderer.SamplerStates[1] = SamplerState.AnisotropicWrap;
            }
            else
            {
                RendererManager.CurrentRenderer.SamplerStates[0] = SamplerState.LinearWrap;
                RendererManager.CurrentRenderer.SamplerStates[1] = SamplerState.LinearWrap;
            }


            float lightmapMultiplier;

            if (SceneManager.DoubleLightmapValues)
            {
                lightmapMultiplier = 2.0f;
            }
            else
            {
                lightmapMultiplier = 1.0f;
            }

            RendererManager.CurrentRenderer.SetVertexBuffer(_allVertices);
            currentEffect.Bind();

            if (lightmappingEnabled)
            {
                currentEffect.SetParameter("LightmapMultiplier", lightmapMultiplier);
                TextureResource lightmap = lightmaps.PackedLightmapTexture;
                if (lightmap != null)
                {
                    currentEffect.SetParameter("Lightmap", lightmap, 1);
                }
            }

            currentEffect.SetParameter("ModelViewProjection", camera.ViewProjection);
            currentEffect.Begin();
            for (int i = 0; i < _surfaces.Length; i++)
            {
                if (_surfaces[i].Hidden == false)
                {
                    if (!calculatingRadiosity || (_surfaces[i].flags & BspSurfaceFlags.DontCastShadows) == 0)
                    {
                        drawSurface(_surfaces[i], currentEffect, camera);
                    }
                }
            }
            currentEffect.End();
        }
Exemplo n.º 10
0
        public static void CalculateLightmaps(LightmapSpecs specs)
        {
            if (_currentRoom != null && _currentLightmaps != null)
            {
                Radiosity.Init(new Radiosity.RenderDelegate(renderRadiosityCallback));

                _calculatingRadiosity = true;
                CurrentFilterMode     = TextureFilterMode.None;
                LightmapsEnabled      = true;
                CurrentShadeMode      = ShadeMode.Flat;
                DoubleLightmapValues  = false;
                Graphics.Camera   originalCamera   = CurrentCamera;
                Graphics.Viewport originalViewport = Graphics.RendererManager.CurrentRenderer.Viewport;

                RadiosityMaps radiosityMaps = _currentRoom.GenerateMemoryTextures(specs);

                // generate the omni lights
                _omniLightInfo = new List <OmniInfo>();
                foreach (LightmapSpecs.OmniLight light in specs.OmniLights)
                {
                    OmniInfo o;
                    o.Info = light;
                    Radiosity.GenerateOmniLight((int)light.Radius, light.Color, out o.MemTex, out o.AlphaMask);

                    _omniLightInfo.Add(o);
                }

                Graphics.RendererManager.CurrentRenderer.CullMode = Graphics.CullMode.None;


                Graphics.TextureResource skyboxTop          = Radiosity.GenerateMemoryTexture(16, 16, specs.SkyColor.X, specs.SkyColor.Y, specs.SkyColor.Z);
                Graphics.TextureResource skyboxFront        = Radiosity.GenerateMemoryTexture(16, 16, specs.SkyColor.X, specs.SkyColor.Y, specs.SkyColor.Z);
                Graphics.TextureResource skyboxBack         = Radiosity.GenerateMemoryTexture(16, 16, specs.SkyColor.X, specs.SkyColor.Y, specs.SkyColor.Z);
                Graphics.TextureResource skyboxLeft         = Radiosity.GenerateMemoryTexture(16, 16, specs.SkyColor.X, specs.SkyColor.Y, specs.SkyColor.Z);
                Graphics.TextureResource skyboxRight        = Radiosity.GenerateMemoryTexture(16, 16, specs.SkyColor.X, specs.SkyColor.Y, specs.SkyColor.Z);
                Graphics.TextureResource skyboxBottom       = Radiosity.GenerateMemoryTexture(16, 16, 0, 0, 0);
                Graphics.BitmapSurface   skyboxTopPixels    = new Graphics.BitmapSurface(skyboxTop);
                Graphics.BitmapSurface   skyboxFrontPixels  = new Graphics.BitmapSurface(skyboxFront);
                Graphics.BitmapSurface   skyboxBackPixels   = new Graphics.BitmapSurface(skyboxBack);
                Graphics.BitmapSurface   skyboxLeftPixels   = new Graphics.BitmapSurface(skyboxLeft);
                Graphics.BitmapSurface   skyboxRightPixels  = new Graphics.BitmapSurface(skyboxRight);
                Graphics.BitmapSurface   skyboxBottomPixels = new Graphics.BitmapSurface(skyboxBottom);

                // Graphics.SkyBox.AddSun(specs.SunDirection, specs.SunColor, 0.125f, skyboxFrontPixels, skyboxBackPixels, skyboxLeftPixels, skyboxRightPixels, skyboxTopPixels, true);

                Graphics.SkyBox originalSkybox = _currentSkybox;
                if (_currentSkybox != null)
                {
                    _currentSkybox = new Graphics.SkyBox("box", skyboxFrontPixels, skyboxBackPixels, skyboxLeftPixels, skyboxRightPixels, skyboxTopPixels, skyboxBottomPixels, 0);

                    _currentSkybox.AddSun(specs.SunDirection, specs.SunColor, true);
                }

                Graphics.LightmapResource oldLightmaps = _currentLightmaps;
                _currentLightmaps = radiosityMaps.CreateBigMemoryTexture(_currentLightmaps.Name);
                _currentRoom.CalcRadiosityPass(_currentLightmaps, radiosityMaps);

                Radiosity.Shutdown();

                _currentRoom.FinalizeVertices(_currentLightmaps, true);
                _currentLightmaps = radiosityMaps.ConvertToLightmap(_currentLightmaps.Name, 0.02f);

                Graphics.RendererManager.CurrentRenderer.CullMode = Graphics.CullMode.CounterClockwise;
                Graphics.RendererManager.CurrentRenderer.Viewport = originalViewport;
                CurrentCamera  = originalCamera;
                _currentSkybox = originalSkybox;

                _calculatingRadiosity = false;
            }
        }
Exemplo n.º 11
0
        public void ReplaceTexture(int meshIndex, int groupIndex, string textureName)
        {
            TextureResource texture = _content.Load <TextureResource>(textureName);

            _meshes[meshIndex].sections[groupIndex].textureResource = texture;
        }
Exemplo n.º 12
0
 public abstract void SetParameter(string name, TextureResource parameter, int index);