7fd59977 |
1 | #include <AIS_Drawer.hxx> |
2 | #include <AIS_Triangulation.hxx> |
3 | #include <AIS_InteractiveObject.hxx> |
4 | #include <Standard_DefineHandle.hxx> |
5 | #include <Poly_Array1OfTriangle.hxx> |
6 | #include <Poly_Triangulation.hxx> |
7 | #include <Prs3d_Root.hxx> |
8 | #include <Prs3d_ShadingAspect.hxx> |
9 | #include <TShort_Array1OfShortReal.hxx> |
10 | #include <TColgp_Array1OfPnt.hxx> |
11 | #include <TColStd_HArray1OfInteger.hxx> |
12 | #include <TShort_HArray1OfShortReal.hxx> |
13 | #include <Graphic3d_Group.hxx> |
14 | #include <Graphic3d_AspectFillArea3d.hxx> |
15 | #include <Graphic3d_ArrayOfTriangles.hxx> |
16 | #include <Graphic3d_ArrayOfPrimitives.hxx> |
17 | |
18 | |
19 | IMPLEMENT_STANDARD_HANDLE(AIS_Triangulation, AIS_InteractiveObject) |
20 | IMPLEMENT_STANDARD_RTTIEXT(AIS_Triangulation, AIS_InteractiveObject) |
21 | |
22 | AIS_Triangulation::AIS_Triangulation(const Handle(Poly_Triangulation)& Triangulation) |
23 | { |
24 | myTriangulation = Triangulation; |
25 | myNbNodes = Triangulation->NbNodes(); |
26 | myNbTriangles = Triangulation->NbTriangles(); |
27 | myFlagColor = 0; |
28 | } |
29 | |
30 | //======================================================================= |
31 | //function : Compute |
32 | //purpose : |
33 | //======================================================================= |
34 | void AIS_Triangulation::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager, |
35 | const Handle(Prs3d_Presentation)& aPresentation, |
36 | const Standard_Integer aMode) |
37 | { |
38 | switch (aMode) |
39 | { |
40 | case 0: |
41 | const TColgp_Array1OfPnt& nodes = myTriangulation->Nodes(); //Nodes |
42 | const Poly_Array1OfTriangle& triangles = myTriangulation->Triangles(); //Triangle |
43 | const TShort_Array1OfShortReal& normals = myTriangulation->Normals(); //Normal |
44 | |
45 | Standard_Boolean hasVNormals = Standard_False; |
46 | Standard_Boolean hasVColors = Standard_False; |
47 | if( normals.Length() > 0 ) |
48 | hasVNormals = Standard_True; |
49 | if( myFlagColor == 1 ) |
50 | hasVColors = Standard_True; |
51 | |
52 | Handle(Graphic3d_ArrayOfTriangles) array = |
53 | new Graphic3d_ArrayOfTriangles ( myNbNodes, //maxVertexs |
54 | myNbTriangles * 3,//maxEdges |
55 | hasVNormals, //hasVNormals |
56 | hasVColors, //hasVColors |
57 | Standard_False, //hasTexels |
58 | Standard_True //hasEdgeInfos |
59 | ); |
60 | Handle(Graphic3d_Group) TheGroup = Prs3d_Root::CurrentGroup(aPresentation); |
61 | Handle(Graphic3d_AspectFillArea3d) aspect = myDrawer->ShadingAspect()->Aspect(); |
62 | |
63 | Standard_Integer i; |
64 | Standard_Integer j; |
65 | |
66 | Standard_Real ambient = aspect->FrontMaterial().Ambient(); |
67 | for ( i = nodes.Lower(); i<= nodes.Upper(); i++ ){ |
68 | if( myFlagColor == 1 ) |
69 | array->AddVertex( nodes(i), AttenuateColor(myColor->Value(i),ambient)); |
70 | if( myFlagColor == 0 ) |
71 | array->AddVertex( nodes(i) ); |
72 | j = (i - nodes.Lower()) * 3; |
73 | array->SetVertexNormal(i, normals(j+1), normals(j+2), normals(j+3)); |
74 | } |
75 | |
76 | Standard_Integer indexTriangle[3] = {0,0,0}; |
77 | for ( i = triangles.Lower(); i<= triangles.Upper(); i++ ) { |
78 | triangles(i).Get(indexTriangle[0], indexTriangle[1], indexTriangle[2]); |
79 | array->AddEdge(indexTriangle[0]); |
80 | array->AddEdge(indexTriangle[1]); |
81 | array->AddEdge(indexTriangle[2]); |
82 | } |
83 | TheGroup->SetPrimitivesAspect(aspect); |
84 | TheGroup->BeginPrimitives(); |
85 | TheGroup->AddPrimitiveArray(array); |
86 | TheGroup->EndPrimitives(); |
87 | break; |
88 | } |
89 | } |
90 | |
91 | //======================================================================= |
92 | //function : ComputeSelection |
93 | //purpose : |
94 | //======================================================================= |
95 | void AIS_Triangulation::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection, |
96 | const Standard_Integer /*aMode*/) |
97 | { |
98 | |
99 | } |
100 | |
101 | //======================================================================= |
102 | //function : SetColor |
103 | //purpose : Set the color for each node. |
104 | // Each 32-bit color is Alpha << 24 + Blue << 16 + Green << 8 + Red |
105 | // Order of color components is essential for further usage by OpenGL |
106 | //======================================================================= |
107 | void AIS_Triangulation::SetColors(const Handle(TColStd_HArray1OfInteger)& aColor) |
108 | { |
109 | myFlagColor = 1; |
110 | myColor = aColor; |
111 | } |
112 | |
113 | //======================================================================= |
114 | //function : GetColor |
115 | //purpose : Get the color for each node. |
116 | // Each 32-bit color is Alpha << 24 + Blue << 16 + Green << 8 + Red |
117 | // Order of color components is essential for further usage by OpenGL |
118 | //======================================================================= |
119 | |
120 | Handle(TColStd_HArray1OfInteger) AIS_Triangulation::GetColors() const |
121 | { |
122 | return myColor; |
123 | } |
124 | |
125 | |
126 | //======================================================================= |
127 | //function : SetTriangulation |
128 | //purpose : |
129 | //======================================================================= |
130 | void AIS_Triangulation::SetTriangulation(const Handle(Poly_Triangulation)& aTriangulation) |
131 | { |
132 | myTriangulation = aTriangulation; |
133 | } |
134 | |
135 | //======================================================================= |
136 | //function : GetTriangulation |
137 | //purpose : |
138 | //======================================================================= |
139 | Handle(Poly_Triangulation) AIS_Triangulation::GetTriangulation() const{ |
140 | return myTriangulation; |
141 | } |
142 | |
143 | //======================================================================= |
144 | //function : AttenuateColor |
145 | //purpose : Attenuates 32-bit color by a given attenuation factor (0...1): |
146 | // aColor = Alpha << 24 + Blue << 16 + Green << 8 + Red |
147 | // All color components are multiplied by aComponent, the result is then packed again as 32-bit integer. |
148 | // Color attenuation is applied to the vertex colors in order to have correct visual result |
149 | // after glColorMaterial(GL_AMBIENT_AND_DIFFUSE). Without it, colors look unnatural and flat. |
150 | //======================================================================= |
151 | |
152 | Standard_Integer AIS_Triangulation::AttenuateColor( const Standard_Integer aColor, |
153 | const Standard_Real aComposition) |
154 | { |
155 | Standard_Integer red, |
156 | green, |
157 | blue, |
158 | alpha; |
159 | |
160 | alpha = aColor&0xff000000; |
161 | alpha >>= 24; |
162 | |
163 | blue = aColor&0x00ff0000; |
164 | blue >>= 16; |
165 | |
166 | green = aColor&0x0000ff00; |
167 | green >>= 8; |
168 | |
169 | red = aColor&0x000000ff; |
170 | red >>= 0; |
171 | |
172 | red = (Standard_Integer)(aComposition * red); |
173 | green = (Standard_Integer)(aComposition * green); |
174 | blue = (Standard_Integer)(aComposition * blue); |
175 | |
176 | Standard_Integer color; |
177 | color = red; |
178 | color += green << 8; |
179 | color += blue << 16; |
180 | color += alpha << 24; |
181 | return color; |
182 | } |
183 | |