0025576: Visualization - implement AIS_ConnectedInteractive::AcceptDisplayMode()
[occt.git] / src / AIS / AIS_Triangulation.cxx
CommitLineData
973c2be1 1// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 2//
973c2be1 3// This file is part of Open CASCADE Technology software library.
b311480e 4//
d5f74e42 5// This library is free software; you can redistribute it and/or modify it under
6// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 7// by the Free Software Foundation, with special exception defined in the file
8// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9// distribution for complete text of the license and disclaimer of any warranty.
b311480e 10//
973c2be1 11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
b311480e 13
dcc17419 14#include <AIS_DisplayMode.hxx>
7fd59977 15#include <AIS_Triangulation.hxx>
16#include <AIS_InteractiveObject.hxx>
ec357c5c 17#include <Standard_Type.hxx>
7fd59977 18#include <Poly_Array1OfTriangle.hxx>
19#include <Poly_Triangulation.hxx>
6262338c 20#include <Prs3d_Drawer.hxx>
7fd59977 21#include <Prs3d_Root.hxx>
22#include <Prs3d_ShadingAspect.hxx>
23#include <TShort_Array1OfShortReal.hxx>
24#include <TColgp_Array1OfPnt.hxx>
25#include <TColStd_HArray1OfInteger.hxx>
26#include <TShort_HArray1OfShortReal.hxx>
27#include <Graphic3d_Group.hxx>
28#include <Graphic3d_AspectFillArea3d.hxx>
29#include <Graphic3d_ArrayOfTriangles.hxx>
7fd59977 30
31
92efcf78 32IMPLEMENT_STANDARD_RTTIEXT(AIS_Triangulation,AIS_InteractiveObject)
33
7fd59977 34AIS_Triangulation::AIS_Triangulation(const Handle(Poly_Triangulation)& Triangulation)
35{
36 myTriangulation = Triangulation;
37 myNbNodes = Triangulation->NbNodes();
38 myNbTriangles = Triangulation->NbTriangles();
39 myFlagColor = 0;
40}
41
dcc17419 42//=======================================================================
43//function : SetTransparency
44//purpose :
45//=======================================================================
46void AIS_Triangulation::SetTransparency (const Standard_Real theValue)
47{
48 if (!myDrawer->HasOwnShadingAspect())
49 {
50 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
51 if (myDrawer->HasLink())
52 {
53 *myDrawer->ShadingAspect()->Aspect() = *myDrawer->Link()->ShadingAspect()->Aspect();
54 }
55 }
56
57 // override transparency
58 myDrawer->ShadingAspect()->SetTransparency (theValue, myCurrentFacingModel);
59 myTransparency = theValue;
60
61 updatePresentation();
62}
63
64//=======================================================================
65//function : UnsetTransparency
66//purpose :
67//=======================================================================
68void AIS_Triangulation::UnsetTransparency()
69{
70 myTransparency = 0.0;
71 if (!myDrawer->HasOwnShadingAspect())
72 {
73 return;
74 }
75 else if (HasColor() || HasMaterial())
76 {
77 myDrawer->ShadingAspect()->SetTransparency (0.0, myCurrentFacingModel);
78 }
79
80 updatePresentation();
81}
82
83//=======================================================================
84//function : updatePresentation
85//purpose :
86//=======================================================================
87void AIS_Triangulation::updatePresentation()
88{
89 if (HasVertexColors())
90 {
91 SetToUpdate (AIS_WireFrame);
92 }
93 else
94 {
95 // modify shading presentation without re-computation
96 const PrsMgr_Presentations& aPrsList = Presentations();
97 Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->ShadingAspect()->Aspect();
98 for (Standard_Integer aPrsIt = 1; aPrsIt <= aPrsList.Length(); ++aPrsIt)
99 {
100 const PrsMgr_ModedPresentation& aPrsModed = aPrsList.Value (aPrsIt);
101 if (aPrsModed.Mode() != AIS_WireFrame)
102 {
103 continue;
104 }
105
106 const Handle(Prs3d_Presentation)& aPrs = aPrsModed.Presentation()->Presentation();
107
108 if (IsTransparent())
109 {
110 aPrs->SetDisplayPriority (10); // force highest priority for translucent objects
111 }
112
113 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (aPrs->Groups()); aGroupIt.More(); aGroupIt.Next())
114 {
115 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
116 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
117 {
118 aGroup->SetGroupPrimitivesAspect (anAreaAsp);
119 }
120 }
121
122 if (!IsTransparent())
123 {
124 aPrs->ResetDisplayPriority();
125 }
126 }
127
128 myRecomputeEveryPrs = Standard_False; // no mode to recalculate - only viewer update
129 myToRecomputeModes.Clear();
130 }
131}
132
7fd59977 133//=======================================================================
134//function : Compute
135//purpose :
136//=======================================================================
35e08fe8 137void AIS_Triangulation::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aPresentationManager*/,
7fd59977 138 const Handle(Prs3d_Presentation)& aPresentation,
139 const Standard_Integer aMode)
140{
141 switch (aMode)
142 {
143 case 0:
144 const TColgp_Array1OfPnt& nodes = myTriangulation->Nodes(); //Nodes
145 const Poly_Array1OfTriangle& triangles = myTriangulation->Triangles(); //Triangle
7fd59977 146
16e65a03 147 Standard_Boolean hasVNormals = myTriangulation->HasNormals();
dcc17419 148 Standard_Boolean hasVColors = HasVertexColors();
7fd59977 149
871fa103 150 Handle(Graphic3d_ArrayOfTriangles) anArray = new Graphic3d_ArrayOfTriangles (myNbNodes, myNbTriangles * 3,
151 hasVNormals, hasVColors, Standard_False);
7fd59977 152 Handle(Graphic3d_Group) TheGroup = Prs3d_Root::CurrentGroup(aPresentation);
153 Handle(Graphic3d_AspectFillArea3d) aspect = myDrawer->ShadingAspect()->Aspect();
154
155 Standard_Integer i;
156 Standard_Integer j;
157
158 Standard_Real ambient = aspect->FrontMaterial().Ambient();
16e65a03 159 if (hasVNormals)
160 {
161 const TShort_Array1OfShortReal& normals = myTriangulation->Normals();
162 if (hasVColors)
163 {
164 const TColStd_Array1OfInteger& colors = myColor->Array1();
165 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
166 {
167 j = (i - nodes.Lower()) * 3;
dcc17419 168 anArray->AddVertex(nodes(i), attenuateColor(colors(i), ambient));
16e65a03 169 anArray->SetVertexNormal(i, normals(j+1), normals(j+2), normals(j+3));
170 }
171 }
172 else // !hasVColors
173 {
174 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
175 {
176 j = (i - nodes.Lower()) * 3;
177 anArray->AddVertex(nodes(i));
178 anArray->SetVertexNormal(i, normals(j+1), normals(j+2), normals(j+3));
179 }
180 }
181 }
182 else // !hasVNormals
183 {
184 if (hasVColors)
185 {
186 const TColStd_Array1OfInteger& colors = myColor->Array1();
187 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
188 {
dcc17419 189 anArray->AddVertex(nodes(i), attenuateColor(colors(i), ambient));
16e65a03 190 }
191 }
192 else // !hasVColors
193 {
194 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
195 {
196 anArray->AddVertex(nodes(i));
197 }
198 }
7fd59977 199 }
200
201 Standard_Integer indexTriangle[3] = {0,0,0};
202 for ( i = triangles.Lower(); i<= triangles.Upper(); i++ ) {
203 triangles(i).Get(indexTriangle[0], indexTriangle[1], indexTriangle[2]);
b8ddfc2f 204 anArray->AddEdge(indexTriangle[0]);
205 anArray->AddEdge(indexTriangle[1]);
206 anArray->AddEdge(indexTriangle[2]);
7fd59977 207 }
208 TheGroup->SetPrimitivesAspect(aspect);
b8ddfc2f 209 TheGroup->AddPrimitiveArray(anArray);
7fd59977 210 break;
211 }
212}
213
214//=======================================================================
215//function : ComputeSelection
216//purpose :
217//=======================================================================
35e08fe8 218void AIS_Triangulation::ComputeSelection(const Handle(SelectMgr_Selection)& /*aSelection*/,
7fd59977 219 const Standard_Integer /*aMode*/)
220{
221
222}
223
224//=======================================================================
225//function : SetColor
226//purpose : Set the color for each node.
227// Each 32-bit color is Alpha << 24 + Blue << 16 + Green << 8 + Red
228// Order of color components is essential for further usage by OpenGL
229//=======================================================================
230void AIS_Triangulation::SetColors(const Handle(TColStd_HArray1OfInteger)& aColor)
231{
232 myFlagColor = 1;
233 myColor = aColor;
234}
235
236//=======================================================================
237//function : GetColor
238//purpose : Get the color for each node.
239// Each 32-bit color is Alpha << 24 + Blue << 16 + Green << 8 + Red
240// Order of color components is essential for further usage by OpenGL
241//=======================================================================
242
243Handle(TColStd_HArray1OfInteger) AIS_Triangulation::GetColors() const
244{
245 return myColor;
246}
247
248
249//=======================================================================
250//function : SetTriangulation
251//purpose :
252//=======================================================================
253void AIS_Triangulation::SetTriangulation(const Handle(Poly_Triangulation)& aTriangulation)
254{
255 myTriangulation = aTriangulation;
256}
257
258//=======================================================================
259//function : GetTriangulation
260//purpose :
261//=======================================================================
262Handle(Poly_Triangulation) AIS_Triangulation::GetTriangulation() const{
263 return myTriangulation;
264}
265
266//=======================================================================
267//function : AttenuateColor
268//purpose : Attenuates 32-bit color by a given attenuation factor (0...1):
269// aColor = Alpha << 24 + Blue << 16 + Green << 8 + Red
270// All color components are multiplied by aComponent, the result is then packed again as 32-bit integer.
271// Color attenuation is applied to the vertex colors in order to have correct visual result
272// after glColorMaterial(GL_AMBIENT_AND_DIFFUSE). Without it, colors look unnatural and flat.
273//=======================================================================
274
dcc17419 275Graphic3d_Vec4ub AIS_Triangulation::attenuateColor (const Standard_Integer theColor,
276 const Standard_Real theComposition)
7fd59977 277{
7fd59977 278
dcc17419 279 const Graphic3d_Vec4ub& aColor = *reinterpret_cast<const Graphic3d_Vec4ub*> (&theColor);
280 // If IsTranparent() is false alpha value will be ignored anyway.
281 Standard_Byte anAlpha = IsTransparent() ? static_cast<Standard_Byte> (255.0 - myDrawer->ShadingAspect()->Aspect()->FrontMaterial().Transparency() * 255.0)
282 : 255;
7fd59977 283
dcc17419 284 return Graphic3d_Vec4ub ((Standard_Byte)(theComposition * aColor.r()),
285 (Standard_Byte)(theComposition * aColor.g()),
286 (Standard_Byte)(theComposition * aColor.b()),
287 anAlpha);
7fd59977 288}
289