Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Drawing/Platform/OpenGL/ILOGLWorldRenderer.cs @ 9102

Last change on this file since 9102 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 12.1 KB
Line 
1///
2///    This file is part of ILNumerics Community Edition.
3///
4///    ILNumerics Community Edition - high performance computing for applications.
5///    Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
6///
7///    ILNumerics Community Edition is free software: you can redistribute it and/or modify
8///    it under the terms of the GNU General Public License version 3 as published by
9///    the Free Software Foundation.
10///
11///    ILNumerics Community Edition is distributed in the hope that it will be useful,
12///    but WITHOUT ANY WARRANTY; without even the implied warranty of
13///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14///    GNU General Public License for more details.
15///
16///    You should have received a copy of the GNU General Public License
17///    along with ILNumerics Community Edition. See the file License.txt in the root
18///    of your distribution package. If not, see <http://www.gnu.org/licenses/>.
19///
20///    In addition this software uses the following components and/or licenses:
21///
22///    =================================================================================
23///    The Open Toolkit Library License
24///   
25///    Copyright (c) 2006 - 2009 the Open Toolkit library.
26///   
27///    Permission is hereby granted, free of charge, to any person obtaining a copy
28///    of this software and associated documentation files (the "Software"), to deal
29///    in the Software without restriction, including without limitation the rights to
30///    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
31///    the Software, and to permit persons to whom the Software is furnished to do
32///    so, subject to the following conditions:
33///
34///    The above copyright notice and this permission notice shall be included in all
35///    copies or substantial portions of the Software.
36///
37///    =================================================================================
38///   
39
40using System;
41using System.Text;
42using System.Drawing; 
43using System.Collections.Generic;
44using ILNumerics.Drawing.Interfaces;
45using OpenTK;
46using OpenTK.Graphics;
47using OpenTK.Graphics.OpenGL;
48using OpenTK.Graphics.OpenGL.Enums;
49using ILNumerics.Drawing;
50using ILNumerics.Drawing.Labeling; 
51
52namespace ILNumerics.Drawing.Platform.OpenGL {
53    /// <summary>
54    /// Basic OpenGL implementation for IILTextRenderer
55    /// </summary>
56    [ILRenderer(GraphicDeviceType.OpenGL,"Outline","OpenGL cached, outlined textures, world coords",true,CoordSystem.World3D)]
57    public class ILOGLWorldRenderer : IILTextRenderer {
58
59        #region event handling
60        /// <summary>
61        /// fires when the texture storage has been cleared
62        /// </summary>
63        public event EventHandler CacheCleared;
64        void m_textureManager_TextureCacheCleared(object sender, EventArgs e) {
65            if (CacheCleared != null)
66                CacheCleared(this,null);
67        }
68        #endregion
69
70        #region member / properties
71        ILTextureManager m_textureManager;
72        float[] m_curPosition = new float[16];
73        Color m_colorOverride;
74        #endregion
75
76        #region constructors
77
78        public ILOGLWorldRenderer (ILPanel panel) {
79            m_textureManager = panel.TextureManager;
80            m_textureManager.TextureCacheCleared += new EventHandler(m_textureManager_TextureCacheCleared);
81        }
82           
83        #endregion
84
85        #region IILTextRenderer Member
86       
87        public CoordSystem CoordSystem {
88            get { return CoordSystem.World3D; }
89        }
90
91        public bool Cached {
92            get {
93                return true;
94            }
95        }
96
97        public string Name {
98            get { return "Outline fonts"; }
99        }
100
101        public string NameLong {
102            get { return "OpenGL outline fonts (OpenTK)"; }
103        }
104
105        public GraphicDeviceType DeviceType {
106            get { return GraphicDeviceType.OpenGL; }
107        }
108
109        public bool DrawAfterBufferSwapped {
110            get { return false; }
111        }
112
113        public void Begin(ILRenderProperties p) {
114            if (GraphicsContext.CurrentContext == null)
115                throw new GraphicsContextException("No GraphicsContext is current in the calling thread.");
116
117            GL.PushAttrib(AttribMask.TextureBit | AttribMask.EnableBit | AttribMask.ColorBufferBit);
118
119            GL.Enable(EnableCap.Texture2D);
120            GL.Enable(EnableCap.Blend);
121            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
122            GL.Disable(EnableCap.DepthTest);
123        }
124        public void Begin (ILRenderProperties p, ref double[] modelview) {
125            if (modelview == null || modelview.Length < 12) {
126                modelview = new double[16];
127            }
128            GL.GetDouble(GetPName.ModelviewMatrix,modelview);
129            Begin(p);
130        }
131
132        public void End(ILRenderProperties p) {
133            GL.PopAttrib();
134        }
135        #endregion
136
137        #region IILTextRenderer Member
138        /// <summary>
139        /// Place a new item into the texture cache
140        /// </summary>
141        /// <param name="key">unique key to identify the item for later reference</param>
142        /// <param name="bmp">bitmap containing the item data</param>
143        /// <param name="rect">area in <paramref name="bmp"/> containing the item's data</param>
144        public void Cache(string key, Bitmap bmp, RectangleF rect) {
145            m_textureManager.StoreTextureItem(key,bmp,rect);
146        }
147
148        /// <summary>
149        /// Test if the cache contains an item with a specific key
150        /// </summary>
151        /// <param name="key">key</param>
152        /// <returns>true if the item was cached, false otherwise</returns>
153        public bool ExistsKey(string key) {
154            return m_textureManager.Exists(key);
155        }
156
157        /// <summary>
158        /// try to get the size (screen pixels) of an item
159        /// </summary>
160        /// <param name="key">unique key identifying the item</param>
161        /// <param name="size">[output] size of the item (if found)</param>
162        /// <returns>true, if the item was found, false otherwise</returns>
163        public bool TryGetSize(string key, ref Size size) {
164            ILTextureData item;
165            if (m_textureManager.TryGetTextureItem(key,out item)) {
166                size = new Size(item.Width,item.Height);
167                return true;
168            } else {
169                return false;
170            }
171        }
172
173        /// <summary>
174        /// Draws all items contained in a given render queue
175        /// </summary>
176        /// <param name="queue">render queue</param>
177        /// <param name="x1">x1-position</param>
178        /// <param name="y1">y1-position</param>
179        /// <param name="z1">z1-position</param>
180        /// <param name="x2">x2-position</param>
181        /// <param name="y2">y2-position</param>
182        /// <param name="z2">z2-position</param>
183        /// <param name="color">base color for items not containing individual colors</param>
184        /// <remarks><para>The render queue must contain only keys for already cached items!</para>
185        /// <para>The color parameter serves as a global color definition. It may be overwritten
186        /// by individual color specifications of the queue items.</para>
187        /// <para>Use this function to draw the render queue in world coords. The position parameters mark the upper left
188        /// and lower right corner of the quads to contain the render queue content.</para>
189        /// </remarks>
190        public void Draw(ILRenderQueue queue,
191                        float x1, float y1, float z1, float x2, float y2, float z2,
192                        Color color) {
193            float x, y, xm, ym;
194
195            xm = (x2 - x1)/(queue.Size.Width);
196            ym = (y2 - y1)/(queue.Size.Height);
197            x = 0.5f; y = 0.5f;
198            int lineHeight = 0;
199            m_textureManager.Reset();
200            GLColor3(color);
201           
202            foreach (ILRenderQueueItem item in queue) {
203                // special symbols & control sequences
204                switch (item.Key) {
205                case "\r":
206                    x = 0.5f;
207                    break;
208                case "\n":
209                    x = 0.5f;
210                    y += lineHeight;
211                    lineHeight = 0;
212                    break;
213                default:
214                    //since textures (may) lay on different sheets in GL memory,
215                    // we cannot switch them in between Begin() and End(),
216                    // we must start a new Begin with each Quad :(
217                    // possible workaround: If only one sheet is used, draw all
218                    // quads at once.
219                    ILTextureData textData = m_textureManager.GetTextureItem(item.Key,true);
220                    if (item.Color != Color.Empty) {
221                        GLColor3(item.Color);
222                    } else {
223                        GLColor3(color);
224                    }
225                    GL.Begin(BeginMode.Quads);
226                    RectangleF rectF = textData.TextureRectangle;
227                    GL.TexCoord2(rectF.Left,rectF.Bottom);   
228                    GL.Vertex3(x*xm + x1,(y + textData.Height + item.Offset.Y-1)*ym+y1,z1);      // ul
229                    GL.TexCoord2(rectF.Left,rectF.Top);
230                    GL.Vertex3(x*xm + x1,(y+ item.Offset.Y)*ym+y1,z1);                    // bl
231                    x += textData.Width-1;
232                    GL.TexCoord2(rectF.Right,rectF.Top);
233                    GL.Vertex3(x*xm + x1,(y+ item.Offset.Y)*ym+y1,z1);                    // br
234                    GL.TexCoord2(rectF.Right,rectF.Bottom);
235                    GL.Vertex3(x*xm + x1,(y + textData.Height+ item.Offset.Y-1)*ym+y1,z1);      // tr
236                    if (textData.Height > lineHeight)
237                        lineHeight = textData.Height;
238                    GL.End();
239#if BOUNDINGBOXES_ITEM
240            //// define DEBUG symbol to draw bounding box around each item (debug feature)
241            // todo: modify to draw in world coords if needed!!
242            //GL.Color3(Color.Red);
243            //GL.Begin(BeginMode.LineLoop);
244            //    w-= textData.Width;
245            //    GL.Vertex2(w,h + textData.Height);      // ul
246            //    GL.Vertex2(w,h);                    // bl
247            //    w += textData.Width;
248            //    GL.Vertex2(w,h);                    // br
249            //    GL.Vertex2(w,h + textData.Height);      // tr
250            //GL.End();
251#endif
252                    break;
253                }
254            }
255#if BOUNDINGBOXES
256            //// define debugsymbol "BOUNDINGBOXES", to draw bounding box around whole expression
257            // todo: modify to draw in world coords if needed!!
258            //GL.Disable(EnableCap.Texture2D);
259            //GL.Color3(Color.Red);
260            //GL.LineWidth(1);
261            //GL.Begin(BeginMode.LineLoop);
262            //    GL.Vertex2(0,0);      // ul
263            //    GL.Vertex2(0,queue.Size.Height);                    // bl
264            //    GL.Vertex2(queue.Size.Width,queue.Size.Height);                    // br
265            //    GL.Vertex2(queue.Size.Width,0);      // tr
266            //GL.End();
267            //GL.Enable(EnableCap.Texture2D);
268#endif
269        }
270
271        public void Draw(ILRenderQueue renderQueue, Point position, TextOrientation orientation, Color color) {
272            Draw(renderQueue,position.X,position.Y,0,position.X + renderQueue.Size.Width,position.Y+renderQueue.Size.Height,0,color);
273        }
274
275        public Color ColorOverride {
276            get {
277                return m_colorOverride;
278            }
279            set {
280                m_colorOverride = value;
281            }
282        }
283
284        #endregion
285
286        private void GLColor3(Color color) {
287            if (m_colorOverride.IsEmpty) {
288                GL.Color3(color);
289            } else {
290                GL.Color3(m_colorOverride);
291            }
292        }
293
294    }
295}
Note: See TracBrowser for help on using the repository browser.