public DialogResult ShowDialog(IWin32Window owner, GLTexture texture)
 {
     panel.Texture = texture;
     this.Width = texture.Width + 16;
     this.Height = texture.Height + 32;
     try { return this.ShowDialog(owner); }
     finally { panel.Texture = null; }
 }
        public static unsafe GLTexture CreateBG(TKContext ctx)
        {
            GLTexture tex = new GLTexture(16, 16);
            tex._texId = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, tex._texId);

            int* pixelData = stackalloc int[16 * 16];
            RGBAPixel* p = (RGBAPixel*)pixelData;

            for (int y = 0; y < 16; y++)
                for (int x = 0; x < 16; x++)
                    *p++ = ((x & 8) == (y & 8)) ? _left : _right;

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Four, 16, 16, 0, PixelFormat.Rgba, PixelType.UnsignedByte, (VoidPtr)pixelData);

            return tex;
        }
        private unsafe void Load(int index, int program, PLT0Node palette)
        {
            if (_context == null)
                return;

            Source = null;

            if (Texture != null)
                Texture.Delete();
            Texture = new GLTexture();
            Texture.Bind(index, program, _context);

            //ctx._states[String.Format("{0}_TexRef", Name)] = Texture;

            Bitmap bmp = null;
            TEX0Node tNode = null;

            if (_context._states.ContainsKey("_Node_Refs"))
            {
                List<ResourceNode> nodes = _context._states["_Node_Refs"] as List<ResourceNode>;
                List<ResourceNode> searched = new List<ResourceNode>(nodes.Count);

                foreach (ResourceNode n in nodes)
                {
                    ResourceNode node = n.RootNode;
                    if (searched.Contains(node))
                        continue;
                    searched.Add(node);

                    //Search node itself first
                    if ((tNode = node.FindChild("Textures(NW4R)/" + Name, true) as TEX0Node) != null)
                    {
                        Source = tNode;
                        if (palette != null)
                            Texture.Attach(tNode, palette);
                        else
                            Texture.Attach(tNode);
                        return;
                    }
                    else
                    {
                        //Then search node directory
                        string path = node._origPath;
                        if (path != null)
                        {
                            DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(path));
                            if (dir.Exists && Name != "<null>")
                                foreach (FileInfo file in dir.GetFiles(Name + ".*"))
                                {
                                    if (file.Name.EndsWith(".tga"))
                                    {
                                        Source = file.FullName;
                                        bmp = TGA.FromFile(file.FullName);
                                        break;
                                    }
                                    else if (file.Name.EndsWith(".png") || file.Name.EndsWith(".tiff") || file.Name.EndsWith(".tif"))
                                    {
                                        Source = file.FullName;
                                        bmp = (Bitmap)Bitmap.FromFile(file.FullName);
                                        break;
                                    }
                                }
                        }
                    }
                    if (bmp != null)
                        break;
                }
                searched.Clear();

                if (bmp != null)
                    Texture.Attach(bmp);
            }
        }
 internal override void Unbind()
 {
     if (Texture != null)
     {
         Texture.Delete();
         Texture = null;
     }
     _context = null;
     Rendered = false;
 }
Ejemplo n.º 5
0
        public void RenderBackground()
        {
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            GL.Disable(EnableCap.DepthTest);
            GL.Disable(EnableCap.Lighting);
            GL.Disable(EnableCap.CullFace);

            GL.MatrixMode(MatrixMode.Projection);
            GL.PushMatrix();
            GL.LoadIdentity();
            Matrix p = Matrix.OrthographicMatrix(0, Width, 0, Height, -1, 1);

            GL.LoadMatrix((float *)&p);

            GL.MatrixMode(MatrixMode.Modelview);
            GL.PushMatrix();
            GL.LoadIdentity();

            GL.Color4(Color.White);
            GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);

            GL.Enable(EnableCap.Texture2D);

            if (_updateImage)
            {
                if (_bgImage != null)
                {
                    _bgImage.Delete();
                    _bgImage = null;
                }

                GL.ClearColor(Color.Black);

                Bitmap bmp = BackgroundImage as Bitmap;

                _bgImage = new GLTexture(bmp);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 1);
                _bgImage.Bind();

                _updateImage = false;
            }
            else
            {
                GL.BindTexture(TextureTarget.Texture2D, _bgImage._texId);
            }

            float *points  = stackalloc float[8];
            float  tAspect = (float)_bgImage.Width / _bgImage.Height;
            float  wAspect = (float)Width / Height;

            switch (_bgType)
            {
            case BackgroundType.Stretch:

                points[0] = points[1] = points[3] = points[6] = 0.0f;
                points[2] = points[4] = Width;
                points[5] = points[7] = Height;

                break;

            case BackgroundType.Center:

                if (tAspect > wAspect)
                {
                    points[1] = points[3] = 0.0f;
                    points[5] = points[7] = Height;

                    points[0] = points[6] = Width * ((Width - ((float)Height / _bgImage.Height * _bgImage.Width)) / Width / 2.0f);
                    points[2] = points[4] = Width - points[0];
                }
                else
                {
                    points[0] = points[6] = 0.0f;
                    points[2] = points[4] = Width;

                    points[1] = points[3] = Height * (((Height - ((float)Width / _bgImage.Width * _bgImage.Height))) / Height / 2.0f);
                    points[5] = points[7] = Height - points[1];
                }
                break;

            case BackgroundType.ResizeWithBars:

                if (tAspect > wAspect)
                {
                    points[0] = points[6] = 0.0f;
                    points[2] = points[4] = Width;

                    points[1] = points[3] = Height * (((Height - ((float)Width / _bgImage.Width * _bgImage.Height))) / Height / 2.0f);
                    points[5] = points[7] = Height - points[1];
                }
                else
                {
                    points[1] = points[3] = 0.0f;
                    points[5] = points[7] = Height;

                    points[0] = points[6] = Width * ((Width - ((float)Height / _bgImage.Height * _bgImage.Width)) / Width / 2.0f);
                    points[2] = points[4] = Width - points[0];
                }

                break;
            }

            GL.Begin(BeginMode.Quads);

            GL.TexCoord2(0.0f, 0.0f);
            GL.Vertex2(&points[0]);
            GL.TexCoord2(1.0f, 0.0f);
            GL.Vertex2(&points[2]);
            GL.TexCoord2(1.0f, 1.0f);
            GL.Vertex2(&points[4]);
            GL.TexCoord2(0.0f, 1.0f);
            GL.Vertex2(&points[6]);

            GL.End();

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);

            GL.Disable(EnableCap.Texture2D);
            GL.Enable(EnableCap.DepthTest);
            GL.Enable(EnableCap.Lighting);

            GL.PopMatrix();
            GL.MatrixMode(MatrixMode.Projection);
            GL.PopMatrix();
        }
Ejemplo n.º 6
0
        protected override void OnPaint(PaintEventArgs e)
        {
            if (_updateCounter > 0)
            {
                return;
            }

            if (_ctx == null)
            {
                base.OnPaint(e);
            }
            else if (Monitor.TryEnter(_ctx))
            {
                try
                {
                    //Direct OpenGL calls to this panel
                    Capture();

                    //Render background image
                    if (BackgroundImage != null)
                    {
                        RenderBackground();
                    }
                    else if (_updateImage && _bgImage != null)
                    {
                        _bgImage.Delete();
                        _bgImage     = null;
                        _updateImage = false;
                    }

                    if (_bgColorChanged)
                    {
                        Vector3 v = (Vector3)BackColor;
                        GL.ClearColor(v._x, v._y, v._z, 0.0f);
                        _bgColorChanged = false;
                    }

                    //Set projection
                    if (_projectionChanged)
                    {
                        OnResized();
                        _projectionChanged = false;
                    }

                    //Apply camera
                    if (_camera != null)
                        fixed(Matrix *p = &_camera._matrix)
                        {
                            GL.MatrixMode(MatrixMode.Modelview);
                            GL.LoadMatrix((float *)p);
                        }

                    //Render 3D scene
                    OnRender(_ctx, e);

                    //Render selection overlay and/or text overlays
                    if ((_selecting && !_forceNoSelection) || _text.Count != 0)
                    {
                        GL.Color4(Color.White);

                        GL.PushAttrib(AttribMask.AllAttribBits);

                        GL.Disable(EnableCap.DepthTest);
                        GL.Disable(EnableCap.Lighting);
                        GL.Disable(EnableCap.CullFace);
                        GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);

                        GL.MatrixMode(MatrixMode.Projection);
                        GL.PushMatrix();
                        GL.LoadIdentity();
                        Matrix p = Matrix.OrthographicMatrix(0, Width, 0, Height, -1, 1);
                        GL.LoadMatrix((float *)&p);

                        GL.MatrixMode(MatrixMode.Modelview);
                        GL.PushMatrix();
                        GL.LoadIdentity();

                        if (_text.Count != 0 && _textEnabled)
                        {
                            _text.Draw();
                        }

                        if (_selecting && !_forceNoSelection)
                        {
                            RenderSelection();
                        }

                        GL.PopAttrib();

                        GL.PopMatrix();
                        GL.MatrixMode(MatrixMode.Projection);
                        GL.PopMatrix();
                    }

                    GL.Finish();
                    _ctx.Swap();

                    ErrorCode code = GL.GetError();
                    if (code != ErrorCode.NoError)
                    {
                        this.Reset(); //Stops the red X of death in its tracks
                    }
                }
                finally { Monitor.Exit(_ctx); }
            }

            //Clear text values
            //This will be filled until the next render
            _text.Clear();
        }