Ejemplo n.º 1
0
        void buttonpressed(object o, EventArgs args)
        {
            Button thisbutton = o as Button;

            Highlight(thisbutton);
            foreach (Button button in buttons)
            {
                Unhighlight(button);
            }

            string unitname = thisbutton.Name;

            LogFile.GetInstance().WriteLine(o.ToString() + " " + unitname + " pressed");

            if (!UnitCache.GetInstance().UnitsByName.ContainsKey(unitname))
            {
                Unit unit = new S3oLoader().LoadS3o("objects3d" + "/" + unitname + ".s3o");
                UnitCache.GetInstance().UnitsByName.Add(unitname, unit);
            }
            (BrushEffectController.GetInstance().brusheffects[typeof(AddFeature)] as AddFeature)
            .currentfeature = UnitCache.GetInstance().UnitsByName[unitname];
        }
Ejemplo n.º 2
0
        // returns byte buffer in raw RGBA format, 32bits per pixel
        // opengl needs to be initialized for this to run
        // normally the renderer is initialized before anything else (in MapDesigner.cs), so this should be ok
        public byte[] CreateUnitPic(string s3ofilepath, int width, int height)
        {
            int picturewidth  = 96;
            int pictureheight = 96;

            GraphicsHelperGl g = new GraphicsHelperGl();

            Unit unit = new S3oLoader().LoadS3o(s3ofilepath);

            Gl.glViewport(0, 0, picturewidth, pictureheight);                                   // Set Our Viewport (Match Texture Size)
            //Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);				// Set The Clear Color To Medium Blue

            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glPushMatrix();
            Gl.glLoadIdentity();
            Glu.gluPerspective(60.0, (float)picturewidth / (float)pictureheight, 0.1, 1000.0);
            Gl.glMatrixMode(Gl.GL_MODELVIEW);

            /*
             * float[] ambientLight = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
             * float[] diffuseLight = new float[] { 0.6f, 0.6f, 0.6f, 1.0f };
             * float[] specularLight = new float[] { 0.2f, 0.2f, 0.2f, 1.0f };
             * float[] position = new float[] { -1.5f, 1.0f, -4.0f, 1.0f };
             *
             * Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_AMBIENT, ambientLight);
             * Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_DIFFUSE, diffuseLight);
             * Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_SPECULAR, specularLight);
             * Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_POSITION, position);
             */
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);                // Clear The Screen And Depth Buffer

            Gl.glPushMatrix();
            Gl.glLoadIdentity();

            Gl.glRotated(-20, 0, 1, 0);
            Gl.glTranslated(-8, -8, -27);

            Gl.glRotated(-90, 1, 0, 0);

            Gl.glEnable(Gl.GL_LIGHTING);

            unit.Render();

            Gl.glPopMatrix();
            //g.Bind2DTexture(0);
            //Gl.glDeleteTextures(2, new int[] { unittexture1, unittexture2 });
            Gl.glViewport(0, 0, RendererSdl.GetInstance().OuterWindowWidth, RendererSdl.GetInstance().OuterWindowHeight);
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glPopMatrix();
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            g.CheckError();

            byte[] buffer = new byte[pictureheight * picturewidth * 4];
            IntPtr ptr    = Marshal.AllocHGlobal(picturewidth * pictureheight * 4);

            Gl.glReadPixels(0, 0, picturewidth, pictureheight, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, ptr);
            Marshal.Copy(ptr, buffer, 0, picturewidth * pictureheight * 4);
            Marshal.FreeHGlobal(ptr);

            // flip

            /* or not
             * byte[] newbuffer = new byte[pictureheight * picturewidth * 4];
             * for (int x = 0; x < picturewidth; x++)
             * {
             *  for (int y = 0; y < picturewidth; y++)
             *  {
             *      for (int i = 0; i < 4; i++)
             *      {
             *          newbuffer[( pictureheight - y - 1 ) * picturewidth * 4 + x * 4 + i] = buffer[y * picturewidth * 4 + x * 4 + i];
             *      }
             *  }
             * }
             */

            return(buffer);
        }
        // returns byte buffer in raw RGBA format, 32bits per pixel
        // opengl needs to be initialized for this to run
        // normally the renderer is initialized before anything else (in MapDesigner.cs), so this should be ok
        public byte[] CreateUnitPic(string s3ofilepath, int width, int height )
        {
            int picturewidth = 96;
            int pictureheight = 96;

            GraphicsHelperGl g = new GraphicsHelperGl();

            Unit unit = new S3oLoader().LoadS3o(s3ofilepath);

            Gl.glViewport(0, 0, picturewidth, pictureheight);					// Set Our Viewport (Match Texture Size)
            //Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);				// Set The Clear Color To Medium Blue

            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glPushMatrix();
            Gl.glLoadIdentity();
            Glu.gluPerspective(60.0, (float)picturewidth / (float)pictureheight, 0.1, 1000.0);
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            /*
            float[] ambientLight = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
            float[] diffuseLight = new float[] { 0.6f, 0.6f, 0.6f, 1.0f };
            float[] specularLight = new float[] { 0.2f, 0.2f, 0.2f, 1.0f };
            float[] position = new float[] { -1.5f, 1.0f, -4.0f, 1.0f };

            Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_AMBIENT, ambientLight);
            Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_DIFFUSE, diffuseLight);
            Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_SPECULAR, specularLight);
            Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_POSITION, position);
             */
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);		// Clear The Screen And Depth Buffer

            Gl.glPushMatrix();
            Gl.glLoadIdentity();

            Gl.glRotated(-20, 0, 1, 0);
            Gl.glTranslated(-8, -8, -27);

            Gl.glRotated(-90, 1, 0, 0);

            Gl.glEnable(Gl.GL_LIGHTING);

            unit.Render();

            Gl.glPopMatrix();
            //g.Bind2DTexture(0);
            //Gl.glDeleteTextures(2, new int[] { unittexture1, unittexture2 });
            Gl.glViewport(0, 0, RendererSdl.GetInstance().OuterWindowWidth, RendererSdl.GetInstance().OuterWindowHeight);
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glPopMatrix();
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            g.CheckError();

            byte[] buffer = new byte[pictureheight * picturewidth * 4];
            IntPtr ptr = Marshal.AllocHGlobal(picturewidth * pictureheight * 4);
            Gl.glReadPixels(0, 0, picturewidth, pictureheight, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, ptr);
            Marshal.Copy(ptr, buffer, 0, picturewidth * pictureheight * 4);
            Marshal.FreeHGlobal(ptr);

            // flip
            /* or not
            byte[] newbuffer = new byte[pictureheight * picturewidth * 4];
            for (int x = 0; x < picturewidth; x++)
            {
                for (int y = 0; y < picturewidth; y++)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        newbuffer[( pictureheight - y - 1 ) * picturewidth * 4 + x * 4 + i] = buffer[y * picturewidth * 4 + x * 4 + i];
                    }
                }
            }
             */

            return buffer;
        }
        void buttonpressed(object o, EventArgs args)
        {
            Button thisbutton = o as Button;
            Highlight(thisbutton);
            foreach (Button button in buttons)
            {
                Unhighlight(button);
            }

            string unitname = thisbutton.Name;
            LogFile.GetInstance().WriteLine(o.ToString() + " " + unitname + " pressed");

            if (!UnitCache.GetInstance().UnitsByName.ContainsKey(unitname))
            {
                Unit unit = new S3oLoader().LoadS3o("objects3d" + "/" + unitname + ".s3o");
                UnitCache.GetInstance().UnitsByName.Add(unitname, unit);
            }
            ( BrushEffectController.GetInstance().brusheffects[ typeof( AddFeature ) ] as AddFeature )
                .currentfeature = UnitCache.GetInstance().UnitsByName[unitname];
        }
Ejemplo n.º 5
0
        public void LoadFeatures(string featurelistfilename, string featuredatafilename)
        {
            LogFile.GetInstance().WriteLine("LoadFeatures()");
            TdfParser tdfparser = TdfParser.FromFile(featurelistfilename);
            Terrain   terrain   = Terrain.GetInstance();

            terrain.FeatureMap = new Unit[terrain.MapWidth, terrain.MapHeight];
            Dictionary <int, string> featurenamebynumber = new Dictionary <int, string>();
            int numfeaturetypes = tdfparser.RootSection.GetIntValue("map/featuretypes/numtypes");

            LogFile.GetInstance().WriteLine("Num types: " + numfeaturetypes);
            for (int i = 0; i < numfeaturetypes; i++)
            {
                string featurename = tdfparser.RootSection.GetStringValue("map/featuretypes/type" + i);
                if (!File.Exists(Path.Combine("objects3d", featurename + ".s3o")))
                {
                    MainUI.GetInstance().uiwindow.WarningMessage("Warning: objects3d/" + featurename + ".s3o not found");
                }
                else
                {
                    LogFile.GetInstance().WriteLine("Feature type " + i + " " + featurename.ToLower());
                    featurenamebynumber.Add(i, featurename.ToLower());
                }
            }

            List <Sm3Feature> features = new List <Sm3Feature>();

            // from FeaturePlacer Form1.cs by Jelmer Cnossen
            FileStream fs = new FileStream(featuredatafilename, FileMode.Open);

            if (fs != null)
            {
                BinaryReader br = new BinaryReader(fs);
                if (br.ReadByte() != 0)
                {
                    MainUI.GetInstance().uiwindow.WarningMessage("The featuredata you are trying to load was saved using a different version.");
                    return;
                }

                int numFeatures = br.ReadInt32();
                features.Clear();
                for (int a = 0; a < numFeatures; a++)
                {
                    Sm3Feature f = new Sm3Feature();
                    features.Add(f);
                    f.type     = br.ReadInt32();
                    f.x        = br.ReadSingle();
                    f.y        = br.ReadSingle();
                    f.z        = br.ReadSingle();
                    f.rotation = br.ReadSingle();
                }
            }

            foreach (Sm3Feature sm3feature in features)
            {
                if (featurenamebynumber.ContainsKey(sm3feature.type))
                {
                    string featurename = featurenamebynumber[sm3feature.type].ToLower();
                    if (!UnitCache.GetInstance().UnitsByName.ContainsKey(featurename))
                    {
                        LogFile.GetInstance().WriteLine("Loading unit " + Path.Combine("objects3d", featurename + ".s3o") + " ... ");
                        Unit unit = new S3oLoader().LoadS3o(Path.Combine("objects3d", featurename + ".s3o"));
                        UnitCache.GetInstance().UnitsByName.Add(featurename, unit);
                    }
                    LogFile.GetInstance().WriteLine("Adding " + featurename + " at " + (int)(sm3feature.x / Terrain.SquareSize) + " " + (int)(sm3feature.y / Terrain.SquareSize));
                    terrain.FeatureMap[(int)(sm3feature.x / Terrain.SquareSize), (int)(sm3feature.y / Terrain.SquareSize)] = UnitCache.GetInstance().UnitsByName[featurename];
                }
            }

            terrain.OnTerrainModified();
        }