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 System.Collections.Generic;
|
---|
42 | using System.Text;
|
---|
43 | using ILNumerics.Drawing.Interfaces;
|
---|
44 | using ILNumerics.Drawing.Lighting;
|
---|
45 | using ILNumerics.Drawing;
|
---|
46 | using ILNumerics.Exceptions;
|
---|
47 |
|
---|
48 | namespace ILNumerics.Drawing.Shapes {
|
---|
49 | /// <summary>
|
---|
50 | /// Base class for all simple, bordered shapes to be used within ILNumerics.Drawing scene graphs
|
---|
51 | /// </summary>
|
---|
52 | /// <typeparam name="VertexType"></typeparam>
|
---|
53 | public abstract class ILLitBorderedShape<VertexType>
|
---|
54 | : ILBorderedShape<VertexType>, IILSupportsLight
|
---|
55 | where VertexType : struct, IILVertexDefinition {
|
---|
56 |
|
---|
57 | #region attributes
|
---|
58 | ILMaterial m_material;
|
---|
59 | bool m_autoNormals;
|
---|
60 | bool m_mustCalcNormals;
|
---|
61 | #endregion
|
---|
62 |
|
---|
63 | #region properties
|
---|
64 | public ILMaterial Material {
|
---|
65 | get {
|
---|
66 | return m_material;
|
---|
67 | }
|
---|
68 | }
|
---|
69 | public bool AutoNormals {
|
---|
70 | get { return m_autoNormals; }
|
---|
71 | set {
|
---|
72 | if (VerticesPerShape < 3 && value)
|
---|
73 | throw new InvalidOperationException(
|
---|
74 | "'auto normals' needs at least 3 vertices per primitive!");
|
---|
75 | m_autoNormals = value;
|
---|
76 | if (value) Configure();
|
---|
77 | }
|
---|
78 | }
|
---|
79 | #endregion
|
---|
80 |
|
---|
81 | public ILLitBorderedShape(ILPanel panel, int numVertices)
|
---|
82 | : base(panel, numVertices) {
|
---|
83 | if (!VertexDefinition.StoresNormals)
|
---|
84 | throw new ILInvalidOperationException("The generic vertex type does not support ligthing operations. Choose a vertext type which is able to store individual normal vectors!");
|
---|
85 | m_autoNormals = true;
|
---|
86 | m_material = new ILMaterial();
|
---|
87 | m_mustCalcNormals = true;
|
---|
88 | }
|
---|
89 |
|
---|
90 | #region public
|
---|
91 | public override void Configure() {
|
---|
92 | if (m_visible) {
|
---|
93 | if (m_mustCalcNormals && m_autoNormals) {
|
---|
94 | Computation.CalculateNormals(m_vertices);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | base.Configure();
|
---|
98 | }
|
---|
99 | public override void Invalidate() {
|
---|
100 | base.Invalidate();
|
---|
101 | m_mustCalcNormals = true;
|
---|
102 | }
|
---|
103 | #endregion
|
---|
104 |
|
---|
105 | private class Computation : ILNumerics.ILMath {
|
---|
106 | public static void CalculateNormals (VertexType[] vertices) {
|
---|
107 | if (vertices.Length < 3) return;
|
---|
108 | // crossproduct of vertex: (#1 - #0) x (#2 - #0)
|
---|
109 | ILPoint3Df cross = ILPoint3Df.crossN(
|
---|
110 | vertices[1].Position - vertices[0].Position,
|
---|
111 | vertices[1].Position - vertices[2].Position);
|
---|
112 | for (int i = 0; i < vertices.Length; i++) {
|
---|
113 | vertices[i].Normal = cross;
|
---|
114 | }
|
---|
115 |
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | }
|
---|