public static bool Fetch(string url_mfstring, out object resource)
        {
            if (X3DTypeConverters.IsMFString(url_mfstring))
            {
                string[] urls = X3DTypeConverters.GetMFString(url_mfstring);

                foreach (string url in urls)
                {
                    if (FetchSingle(url, out resource))
                    {
                        return(true);
                    }
                }
                resource = null;
                return(false);
            }
            else
            {
                return(FetchSingle(url_mfstring, out resource));
            }

            //resource = null;
            //return false;
        }
        //[XmlIgnore]
        //public string ShaderSource;

        #region Rendering Methods

        public override void Load()
        {
            base.Load();

            parentShape  = GetParent <Shape>();
            parentShader = GetParent <ComposedShader>();

            string file;

            string[] mf_urls;

            if (!string.IsNullOrEmpty(ShaderSource))
            {
                LinkShaderSource(ShaderSource);
            }
            else if (Urls != null)
            {
                file = Urls.FirstOrDefault();

                if (!string.IsNullOrEmpty(file))
                {
                    _url = _url.TrimStart();

                    if (_url.StartsWith(X3DTypeConverters.DATA_TEXT_PLAIN))
                    {
                        ShaderSource = _url.Remove(0, X3DTypeConverters.DATA_TEXT_PLAIN.Length).TrimStart();

                        LinkShaderSource(ShaderSource);
                    }
                    else
                    {
                        file = file.Replace("\"", "");
                        file = SceneManager.CurrentLocation + "\\" + file;

                        if (X3DTypeConverters.IsMFString(file))
                        {
                            object resource;

                            mf_urls = X3DTypeConverters.GetMFString(file);

                            foreach (string url in mf_urls)
                            {
                                if (SceneManager.FetchSingle(url, out resource))
                                {
                                    Stream s;

                                    s = (Stream)resource;

                                    StreamReader sr = new StreamReader(s);
                                    ShaderSource = sr.ReadToEnd();

                                    s.Close();

                                    break;
                                }
                            }
                        }
                        else
                        {
                            ShaderSource = File.ReadAllText(file);

                            LinkShaderSource(ShaderSource);
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private bool GetTextureImageFromMFString2(string mfstring)
        {
            Rectangle imgRect;

            int[] textureMaxSize;
            int   glTexWidth;
            int   glTexHeight;

            string[] urls;
            object   resource;
            bool     actually_loaded_something;

            if (X3DTypeConverters.IsMFString(mfstring))
            {
                actually_loaded_something = false;
                urls = X3DTypeConverters.GetMFString(mfstring);

                foreach (string url in urls)
                {
                    if (SceneManager.FetchSingle(url, out resource))
                    {
                        Stream s;

                        s          = (Stream)resource;
                        this.image = new Bitmap(s);
                        s.Close();
                        actually_loaded_something = true;
                        break;
                    }
                }

                if (!actually_loaded_something)
                {
                    this.image = Properties.Resources.ErrorTexture;
                }
            }
            else
            {
                this.image = new Bitmap(mfstring);
            }

            if (this.image == null)
            {
                return(false);
            }

            /*	Get the maximum texture size supported by OpenGL: */
            textureMaxSize = new int[] { 0 };
            GL.GetInteger(GetPName.MaxTextureSize, textureMaxSize);
            //gl.GetInteger(OpenGL.GL_MAX_TEXTURE_SIZE,textureMaxSize);

            /*	Find the target width and height sizes, which is just the highest
             *	posible power of two that'll fit into the image. */
            glTexWidth  = textureMaxSize[0];
            glTexHeight = textureMaxSize[0];
            for (int size = 1; size <= textureMaxSize[0]; size *= 2)
            {
                if (image.Width < size)
                {
                    glTexWidth = size / 2;
                    break;
                }
                if (image.Width == size)
                {
                    glTexWidth = size;
                }
            }

            for (int size = 1; size <= textureMaxSize[0]; size *= 2)
            {
                if (image.Height < size)
                {
                    glTexHeight = size / 2;
                    break;
                }
                if (image.Height == size)
                {
                    glTexHeight = size;
                }
            }

            if (image.Width != glTexWidth || image.Height != glTexHeight)
            {
                /* Scale the image according to OpenGL requirements */
                Image newImage = image.GetThumbnailImage(glTexWidth, glTexHeight, null, IntPtr.Zero);

                image.Dispose();
                image = (Bitmap)newImage;
            }

            //if(file.ToLower().EndsWith(".bmp")) {
            image.RotateFlip(RotateFlipType.RotateNoneFlipY); //TODO: figure out more efficient code

            /* Another way to rotate texture on draw()
             * gl.MatrixMode(OpenGL.GL_TEXTURE);
             * gl.LoadIdentity();
             * gl.Scale(1.0f,-1.0f,1.0f);
             * gl.MatrixMode(OpenGL.GL_MODELVIEW);
             */
            //}
            imgRect   = new Rectangle(0, 0, image.Width, image.Height);
            pixelData = image.LockBits(imgRect, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            pTexImage = pixelData.Scan0;
            Width     = image.Width;
            Height    = image.Height;
            _type     = InternalImageType.WindowsHandle;

            return(true);
        }