예제 #1
0
        public void FinalizeVertices(LightmapResource lightmap, bool regen)
        {
            _lightmapcoords = new float[_bspVertices.Count * 2];

            for (int i = 0; i < _surfaces.Length; i++)
            {
                if (i == 45)
                {
                    i = i;
                }

                for (int j = 0; j < _surfaces[i].indices.Length; j++)
                {
                    float u = (_surfaces[i].uCoord + _texcoords[_surfaces[i].indices[j] * 2 + 0]) * _surfaces[i].uScale;
                    float v = (_surfaces[i].vCoord + _texcoords[_surfaces[i].indices[j] * 2 + 1]) * _surfaces[i].vScale;

                    //_lightmapcoords[_surfaces[i].indices[j] * 2 + 0] = u;
                    //_lightmapcoords[_surfaces[i].indices[j] * 2 + 1] = v;

                    _lightmapcoords[(_surfaces[i].VertexIndex + j) * 2 + 0] = u;
                    _lightmapcoords[(_surfaces[i].VertexIndex + j) * 2 + 1] = v;

                    if (lightmap != null)
                    {
                        Rect lightmapRect = lightmap.PackedLightmaps.GetPackedTextureRect(i);

                        //_lightmapcoords[_surfaces[i].indices[j] * 2 + 0] += 1.0f / lightmapRect.Width * 0.5f;
                        //_lightmapcoords[_surfaces[i].indices[j] * 2 + 1] += 1.0f / lightmapRect.Height * 0.5f;

                        float lu = (lightmapRect.X + 0.5f) / lightmap.PackedLightmapTexture.Width;
                        float lv = (lightmapRect.Y + 0.5f) / lightmap.PackedLightmapTexture.Height;
                        float lw = (lightmapRect.Width - 1.0f) / lightmap.PackedLightmapTexture.Width;
                        float lh = (lightmapRect.Height - 1.0f) / lightmap.PackedLightmapTexture.Height;

                        BspVertex vertex = _bspVertices[_surfaces[i].VertexIndex + j];
                        vertex.LU = lu + u * lw;
                        vertex.LV = lv + v * lh;
                        _bspVertices[_surfaces[i].VertexIndex + j] = vertex;
                    }
                    else
                    {
                        BspVertex vertex = _bspVertices[_surfaces[i].VertexIndex + j];
                        vertex.LU = u;
                        vertex.LV = v;
                        _bspVertices[_surfaces[i].VertexIndex + j] = vertex;
                    }
                }
            }

            _allVertices = RendererManager.CurrentRenderer.CreateVertexBuffer(VertexBufferUsage.Static, _bspVertices.ToArray(), _bspVertices.Count, _vertexDeclaration);
        }
예제 #2
0
        public Resource.Resource Load(string name, Resource.ResourceManager content)
        {
            if (name.IndexOf('.') < 0)
            {
                name += ".MUL";
            }

            System.IO.Stream stream = FileSystem.Open(name);

            LightmapResource resource = new LightmapResource(name, stream);

            stream.Close();

            return(resource);
        }
예제 #3
0
        /// <summary>
        /// Recursively draws the BSP tree, beginning at the specified node. SLOW!!
        /// </summary>
        private void drawBspNode(int nodeIndex, Effect effect, LightmapResource lightmaps, Camera camera)
        {
            if (_boundingSpheres[nodeIndex].W > 100.0f &&
                camera.Frustum.IsSphereOutside(_boundingSpheres[nodeIndex]))
            {
                return;
            }

            BspNode node = _nodes[nodeIndex];

            if (node.Left < 0 && node.Right < 0)
            {
                // draw the polygon
                for (int i = node.PolygonStartIndex; i < node.PolygonStartIndex + node.NumPolygons; i++)
                {
                    //drawSurface(_surfaces[_polygons[i].surfaceIndex], lightmaps[_polygons[i].surfaceIndex], effect, camera);
                }
            }
            else
            {
                if (node.Left >= 0)
                {
                    drawBspNode(node.Left, effect, lightmaps, camera);
                }
                if (node.Right >= 0)
                {
                    drawBspNode(node.Right, effect, lightmaps, camera);
                }

                // draw the polygon
                for (int i = node.PolygonStartIndex; i < node.PolygonStartIndex + node.NumPolygons; i++)
                {
                    //drawSurface(_surfaces[_polygons[i].surfaceIndex], lightmaps[_polygons[i].surfaceIndex], effect, camera);
                }
            }
        }
예제 #4
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();
        }
예제 #5
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;
            }
        }