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 |
|
---|
40 | using System;
|
---|
41 | using ILNumerics.Drawing.Shapes;
|
---|
42 | using OpenTK.Graphics;
|
---|
43 | using OpenTK.Graphics.OpenGL;
|
---|
44 | using ILNumerics.Drawing.Interfaces;
|
---|
45 |
|
---|
46 | namespace ILNumerics.Drawing.Platform.OpenGL {
|
---|
47 | /// <summary>
|
---|
48 | /// An OpenGL vertex renderer, capable of rendering
|
---|
49 | /// vertex arrays of type C4fN3fV3f (for lit shapes).
|
---|
50 | /// </summary>
|
---|
51 | public unsafe class ILOGLVertexRendererC4fN3fV3f : ILVertexRenderer {
|
---|
52 |
|
---|
53 | #region attributes
|
---|
54 | BeginMode m_primitiveType;
|
---|
55 | InterleavedArrayFormat m_vertexFormat;
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | #region properties
|
---|
59 | public BeginMode PrimitiveType {
|
---|
60 | get {
|
---|
61 | return m_primitiveType;
|
---|
62 | }
|
---|
63 | set {
|
---|
64 | m_primitiveType = value;
|
---|
65 | }
|
---|
66 | }
|
---|
67 | #endregion
|
---|
68 |
|
---|
69 | public ILOGLVertexRendererC4fN3fV3f (BeginMode primitiveType) {
|
---|
70 | m_primitiveType = primitiveType;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public override void Draw(ILRenderProperties props, ILShape shape) {
|
---|
74 | if (shape.VertexCount == 0) return;
|
---|
75 | GL.Enable(EnableCap.Blend);
|
---|
76 | GL.BlendFunc (BlendingFactorSrc.SrcAlpha,
|
---|
77 | BlendingFactorDest.OneMinusSrcAlpha);
|
---|
78 | GL.Enable(EnableCap.DepthTest);
|
---|
79 | //GL.Disable(EnableCap.PolygonOffsetFill);
|
---|
80 | ILShape<C4fN3fV3f> cShape = (shape as ILShape<C4fN3fV3f>);
|
---|
81 | fixed (C4fN3fV3f* pVertices = cShape.Vertices) {
|
---|
82 | if (UseLight && (cShape is IILSupportsLight)) {
|
---|
83 | setupLight(cShape as IILSupportsLight);
|
---|
84 | } else {
|
---|
85 | GL.Disable(EnableCap.Lighting);
|
---|
86 | }
|
---|
87 | GL.InterleavedArrays(InterleavedArrayFormat.C4fN3fV3f, 0, (IntPtr)pVertices);
|
---|
88 | if (cShape.Shading == ShadingStyles.Interpolate)
|
---|
89 | GL.ShadeModel(ShadingModel.Smooth);
|
---|
90 | else {
|
---|
91 | GL.ShadeModel(ShadingModel.Flat);
|
---|
92 | GL.DisableClientState(EnableCap.ColorArray);
|
---|
93 | GL.Color4(shape.FillColor);
|
---|
94 | }
|
---|
95 | GL.DrawArrays(m_primitiveType, 0, cShape.VertexCount);
|
---|
96 | GL.Finish();
|
---|
97 | }
|
---|
98 | GL.Disable(EnableCap.Lighting);
|
---|
99 | ILBorderedShape<C4fN3fV3f> bShape = (shape as ILBorderedShape<C4fN3fV3f>);
|
---|
100 | if (bShape != null && bShape.Border.Visible) {
|
---|
101 | fixed (C4fN3fV3f* pVertices = bShape.Vertices) {
|
---|
102 | ILOGLPanel.SetupLineStyle(bShape.Border);
|
---|
103 | GL.InterleavedArrays(InterleavedArrayFormat.C4fN3fV3f, 0, (IntPtr)pVertices);
|
---|
104 | GL.DisableClientState(EnableCap.ColorArray);
|
---|
105 | GL.DrawArrays(BeginMode.LineLoop, 0, bShape.VertexCount);
|
---|
106 | GL.Finish();
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | public override void Draw(ILRenderProperties props, ILShape shape, int[] indices) {
|
---|
111 | System.Diagnostics.Debug.Assert(indices != null);
|
---|
112 | GL.Enable(EnableCap.DepthTest);
|
---|
113 | GL.Enable(EnableCap.Blend);
|
---|
114 | GL.BlendFunc (BlendingFactorSrc.SrcAlpha,
|
---|
115 | BlendingFactorDest.OneMinusSrcAlpha);
|
---|
116 | // draw shape
|
---|
117 | ILShape<C4fN3fV3f> cShape = (shape as ILShape<C4fN3fV3f>);
|
---|
118 | fixed (int* pIndices = indices)
|
---|
119 | fixed (C4fN3fV3f* pVertices = cShape.Vertices) {
|
---|
120 | if (UseLight && (cShape is ILLitCompositeShape<C4fN3fV3f>)) {
|
---|
121 | setupLight(cShape as ILLitCompositeShape<C4fN3fV3f>);
|
---|
122 | } else {
|
---|
123 | GL.Disable(EnableCap.Lighting);
|
---|
124 | }
|
---|
125 | GL.InterleavedArrays(InterleavedArrayFormat.C4fN3fV3f, 0, (IntPtr)pVertices);
|
---|
126 | if (cShape.Shading == ShadingStyles.Interpolate)
|
---|
127 | GL.ShadeModel(ShadingModel.Smooth);
|
---|
128 | else {
|
---|
129 | GL.ShadeModel(ShadingModel.Flat);
|
---|
130 | GL.DisableClientState(EnableCap.ColorArray);
|
---|
131 | GL.Color4(shape.FillColor);
|
---|
132 | }
|
---|
133 | GL.DrawElements(m_primitiveType, indices.Length,DrawElementsType.UnsignedInt, (IntPtr)pIndices);
|
---|
134 | //IntPtr pObj = Glu.NewQuadric();
|
---|
135 | //GL.MatrixMode(MatrixMode.Modelview);
|
---|
136 | //GL.PushMatrix();
|
---|
137 | //GL.Translate(cShape.Vertices[0].Position.X, cShape.Vertices[0].Position.Y, cShape.Vertices[0].Position.Z);
|
---|
138 | //Glu.QuadricDrawStyle(pObj, QuadricDrawStyle.Fill);
|
---|
139 | //Glu.Sphere(pObj, 2, 20, 10);
|
---|
140 | //GL.PopMatrix();
|
---|
141 | GL.Finish();
|
---|
142 | }
|
---|
143 | // draw border
|
---|
144 | ILBorderedShape<C4fN3fV3f> bShape = (shape as ILBorderedShape<C4fN3fV3f>);
|
---|
145 | if (bShape != null && bShape.Border.Visible) {
|
---|
146 | fixed (int* pIndices = indices)
|
---|
147 | fixed (C4fN3fV3f* pVertices = bShape.Vertices) {
|
---|
148 | ILOGLPanel.SetupLineStyle(bShape.Border);
|
---|
149 | GL.InterleavedArrays(
|
---|
150 | InterleavedArrayFormat.C4fN3fV3f, 0, (IntPtr)pVertices);
|
---|
151 | GL.DisableClientState(EnableCap.ColorArray);
|
---|
152 | GL.DrawElements(BeginMode.LineLoop, indices.Length,
|
---|
153 | DrawElementsType.UnsignedInt, (IntPtr)pIndices);
|
---|
154 | GL.Finish();
|
---|
155 |
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | #if RENDERER_DRAW_NORMALS
|
---|
160 | #region DEBUG_DRAW_NORMALS
|
---|
161 | GL.Color3(0,0,0);
|
---|
162 | GL.Begin(BeginMode.Lines);
|
---|
163 | for (int i = 0; i < shape.VertexCount; i++) {
|
---|
164 | GL.Vertex3(cShape.Vertices[i].XPosition,cShape.Vertices[i].YPosition,cShape.Vertices[i].ZPosition);
|
---|
165 | GL.Vertex3( cShape.Vertices[i].XPosition+cShape.Vertices[i].Normal.X,
|
---|
166 | cShape.Vertices[i].YPosition+cShape.Vertices[i].Normal.Y,
|
---|
167 | cShape.Vertices[i].ZPosition+cShape.Vertices[i].Normal.Z);
|
---|
168 | }
|
---|
169 | GL.End();
|
---|
170 | #endregion
|
---|
171 | #endif
|
---|
172 | GL.Disable(EnableCap.Lighting);
|
---|
173 |
|
---|
174 | }
|
---|
175 | private void setupLight(IILSupportsLight shape) {
|
---|
176 | GL.Enable(EnableCap.Lighting);
|
---|
177 | GL.Enable(EnableCap.ColorMaterial);
|
---|
178 | //GL.Enable(EnableCap.Normalize);
|
---|
179 | GL.ColorMaterial(MaterialFace.FrontAndBack,ColorMaterialParameter.AmbientAndDiffuse);
|
---|
180 |
|
---|
181 | //GL.LightModel(LightModelParameter.LightModelAmbient, 0.2f);
|
---|
182 | float[] tmp = new float[4] {
|
---|
183 | shape.Material.Specular.R / 255f
|
---|
184 | , shape.Material.Specular.G / 255f
|
---|
185 | , shape.Material.Specular.B / 255f
|
---|
186 | , shape.Material.Specular.A / 255f};
|
---|
187 | GL.Materialv(MaterialFace.FrontAndBack, MaterialParameter.Specular, tmp);
|
---|
188 |
|
---|
189 | GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Shininess, shape.Material.Shininess );
|
---|
190 |
|
---|
191 | tmp = new float[4] {
|
---|
192 | shape.Material.Emission.R / 255f
|
---|
193 | , shape.Material.Emission.G / 255f
|
---|
194 | , shape.Material.Emission.B / 255f
|
---|
195 | , shape.Material.Emission.A / 255f};
|
---|
196 | GL.Materialv(MaterialFace.FrontAndBack, MaterialParameter.Emission, tmp);
|
---|
197 |
|
---|
198 | }
|
---|
199 |
|
---|
200 | }
|
---|
201 | }
|
---|