Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Drawing/Platform/OpenGL/ILOGLStyledMarkerShape.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: 8.4 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.Collections.Generic;
42using System.Text;
43using System.Drawing;
44using ILNumerics.Drawing.Marker;
45using ILNumerics.Drawing;
46using OpenTK.Graphics;
47using OpenTK.Graphics.OpenGL;
48using OpenTK.Platform;
49using OpenTK.Graphics.OpenGL.Enums;
50using ILNumerics.Drawing.Labeling;
51using ILNumerics.Drawing.Shapes;
52
53
54namespace ILNumerics.Drawing.Platform.OpenGL {
55    public class ILOGLStyledMarkerShape : ILStyledMarkerShape {
56
57        #region constructors
58        internal ILOGLStyledMarkerShape (ILPanel panel, MarkerStyle style)
59            : base (panel,style) { }
60        #endregion
61
62        #region public interface
63
64        internal override void Draw(ILRenderProperties p, ILMarker marker, C4bV3f[] vertices, int startID, int vertCount) {
65            // some implementations need to know we are drawing in
66            // screen coords, vertcount give the signal: -1
67            if (vertCount == 0 || vertCount < -1) return;
68            if (m_style != MarkerStyle.None) {
69               if (m_style == MarkerStyle.Dot || m_style == MarkerStyle.Square) {
70                    SetupMarkerStyle(marker);
71                    unsafe {
72                        fixed(C4bV3f* pVertArr = vertices) {
73                            //GL.TexCoord2(0.5,0.5);
74                            GL.InterleavedArrays(InterleavedArrayFormat.V2f,0,(IntPtr)pVertArr);
75                            GL.DrawArrays(BeginMode.Points,0,Math.Abs(vertCount));
76                        }
77                    }
78                } else {
79                    #region draw textured points (slow version: textured quads)
80                    string markerTexKey = Hash();
81                    ILTextureData texData;
82                    if (!m_panel.TextureManager.Exists(markerTexKey)) {
83                        CacheMarkerBitmap();
84                    }
85                    texData = m_panel.TextureManager.GetTextureItem(markerTexKey,true);
86        System.Diagnostics.Debug.Assert(texData != null,"The texture for marker was expected to exist in texture storage, but it was not found!");
87                    // prepare for plotting
88                    GL.Color3(marker.Color);
89                    GL.PushAttrib(AttribMask.AllAttribBits);
90
91                    GL.Enable(EnableCap.Texture2D);
92                    GL.Enable(EnableCap.Blend);
93                    GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
94                    GL.Disable(EnableCap.DepthTest);
95                    RectangleF rectF = texData.TextureRectangle;
96                    float w,h;
97                    ILClippingData clip = m_panel.Limits;
98                    float s05x;
99                    float s05y;
100                    //if (m_marker.)
101                    s05x = Math.Abs(marker.Size * clip.WidthF / (m_panel.ClientSize.Width));
102                    s05y = Math.Abs(marker.Size * clip.HeightF / (m_panel.ClientSize.Height));
103                    if (vertCount > 0) {
104                        #region determine size for markers in world coords (graph limits)
105
106                        #endregion
107                        // draw all markers using quads.
108                        // this is slow! Todo: replace by point sprites!
109                        GL.Begin(BeginMode.Quads);
110                        for (int i = 0; i < vertCount; i++) {
111                            w = vertices[i].XPosition;
112                            h = vertices[i].YPosition;           
113                            if (m_panel.ClipViewData && (w < clip.XMin || w > clip.XMax || h < clip.YMin || h > clip.YMax))
114                                continue;
115                            w -= s05x;             
116                            h -= s05y;
117                            GL.TexCoord2(rectF.Left,rectF.Bottom);             
118                            GL.Vertex2(w,h);                                    // ul
119                            GL.TexCoord2(rectF.Left,rectF.Top);                 
120                            GL.Vertex2(w,h + 2 * s05y);                         // bl
121                            w += 2 * s05x;                               
122                            GL.TexCoord2(rectF.Right,rectF.Top);               
123                            GL.Vertex2(w,h + 2 * s05y);                         // br
124                            GL.TexCoord2(rectF.Right,rectF.Bottom);             
125                            GL.Vertex2(w,h);                                    // tr
126                        }
127                        GL.End();
128                    } else if (vertCount == -1) {
129                        GL.Begin(BeginMode.Quads);
130                            w = vertices[0].XPosition - marker.Size;
131                            h = vertices[0].YPosition - marker.Size;           
132                            GL.TexCoord2(rectF.Left,rectF.Top);             
133                            GL.Vertex2(w,h);                                    // ul
134                            GL.TexCoord2(rectF.Left,rectF.Bottom);                 
135                            GL.Vertex2(w,h + marker.Size * 2);                  // bl
136                            w += marker.Size;                               
137                            GL.TexCoord2(rectF.Right,rectF.Bottom);               
138                            GL.Vertex2(w,h + marker.Size * 2);                  // br
139                            GL.TexCoord2(rectF.Right,rectF.Top);             
140                            GL.Vertex2(w,h);                                    // tr
141                        GL.End();
142                    }
143                    GL.PopAttrib();
144                    #endregion
145                }
146           }
147        }
148        #endregion
149
150        #region private helper
151        private void SetupMarkerStyle(ILMarker marker) {
152            GL.PointSize(marker.Size);
153            GL.Color3(marker.Color);
154            GL.Disable(EnableCap.DepthTest);
155            switch (m_style) {
156                case MarkerStyle.Dot:
157                    GL.Enable(EnableCap.PointSmooth);
158                    break;
159                case MarkerStyle.Square:
160                    GL.Disable(EnableCap.PointSmooth);
161                    break;
162                case MarkerStyle.None:
163                    GL.PointSize(0.0f);
164                    break;
165                default:
166                    throw new NotImplementedException();
167            }
168        }
169
170        #endregion
171
172    }
173}
Note: See TracBrowser for help on using the repository browser.