1 // File: NIS_TriangulatedDrawer.cxx
2 // Created: 19.07.07 12:09
3 // Author: Alexander GRIGORIEV
4 // Copyright: Open Cascade 2007
6 #include <NIS_TriangulatedDrawer.hxx>
7 #include <NIS_InteractiveObject.hxx>
8 #include <NIS_Triangulated.hxx>
10 #include <Standard_ProgramError.hxx>
17 // Comment this line if you see no hilighting of triangulations due to negative
18 // polygon offsets. Disabling this macro means that all offsets will be created
19 // in the positive scale.
21 // But before changing this macro please play with your OpenGL video card
22 // settings in the direction of performance improvement. Particularly, I had a
23 // good result after checking "Enable write combining" option in NVIDIA 6600.
25 #define NEGATIVE_POFFSET
27 IMPLEMENT_STANDARD_HANDLE (NIS_TriangulatedDrawer, NIS_Drawer)
28 IMPLEMENT_STANDARD_RTTIEXT (NIS_TriangulatedDrawer, NIS_Drawer)
30 //=======================================================================
31 //function : NIS_TriangulatedDrawer()
32 //purpose : Constructor
33 //=======================================================================
35 NIS_TriangulatedDrawer::NIS_TriangulatedDrawer
36 (const Quantity_Color theNormal,
37 const Quantity_Color theHilight,
38 const Quantity_Color theDynHilight)
40 myIsDrawPolygons (Standard_False)
42 myColor[Draw_Normal] = theNormal;
43 myColor[Draw_Transparent] = theNormal;
44 myColor[Draw_Hilighted] = theHilight;
45 myColor[Draw_DynHilighted] = theDynHilight;
48 //=======================================================================
51 //=======================================================================
53 void NIS_TriangulatedDrawer::Assign (const Handle_NIS_Drawer& theOther)
55 if (theOther.IsNull() == Standard_False) {
56 NIS_Drawer::Assign (theOther);
57 const Handle(NIS_TriangulatedDrawer)& anOther =
58 static_cast <const Handle(NIS_TriangulatedDrawer)&> (theOther);
59 myColor[Draw_Normal] = anOther->myColor[Draw_Normal];
60 myColor[Draw_Transparent] = anOther->myColor[Draw_Transparent];
61 myColor[Draw_Hilighted] = anOther->myColor[Draw_Hilighted];
62 myColor[Draw_DynHilighted] = anOther->myColor[Draw_DynHilighted];
63 myLineWidth = anOther->myLineWidth;
64 myIsDrawPolygons = anOther->myIsDrawPolygons;
68 //=======================================================================
70 //purpose : Comparison of two Drawers (for Map impementation)
71 //=======================================================================
73 Standard_Boolean NIS_TriangulatedDrawer::IsEqual
74 (const Handle_NIS_Drawer& theOther)const
76 static const Standard_Real anEpsilon2 (1e-7);
77 Standard_Boolean aResult (Standard_False);
78 const Handle(NIS_TriangulatedDrawer) anOther =
79 Handle(NIS_TriangulatedDrawer)::DownCast (theOther);
80 if (NIS_Drawer::IsEqual(theOther))
81 aResult = (anOther->myColor[Draw_Normal]
82 .SquareDistance (myColor[Draw_Normal]) < anEpsilon2 &&
83 anOther->myColor[Draw_Hilighted]
84 .SquareDistance (myColor[Draw_Hilighted]) < anEpsilon2 &&
85 anOther->myColor[Draw_DynHilighted]
86 .SquareDistance (myColor[Draw_DynHilighted]) < anEpsilon2 &&
87 (anOther->myLineWidth - myLineWidth) *
88 (anOther->myLineWidth - myLineWidth) < 0.01 &&
89 anOther->myIsDrawPolygons == myIsDrawPolygons);
93 //=======================================================================
94 //function : BeforeDraw
96 //=======================================================================
98 void NIS_TriangulatedDrawer::BeforeDraw (const DrawType theType,
101 Quantity_Parameter aValue[3];
102 Quantity_TypeOfColor bidTC (Quantity_TOC_RGB);
103 GLfloat aLineWidth (myLineWidth);
104 Standard_Integer anOffsetHilighted = 0;
106 case Draw_DynHilighted:
107 aLineWidth = myLineWidth + 1.f;
108 #ifdef NEGATIVE_POFFSET
109 anOffsetHilighted = -11;
112 if (myIsDrawPolygons)
113 glEnable(GL_POLYGON_OFFSET_LINE);
115 glEnable(GL_POLYGON_OFFSET_FILL);
116 if (theType == Draw_Hilighted)
118 #ifdef NEGATIVE_POFFSET
119 anOffsetHilighted = -10;
121 anOffsetHilighted = 1;
126 case Draw_Transparent:
127 #ifndef NEGATIVE_POFFSET
128 anOffsetHilighted = 11;
134 if (anOffsetHilighted)
135 glPolygonOffset(1.f, static_cast<GLfloat>(anOffsetHilighted));
137 myColor[theType].Values (aValue[0], aValue[1], aValue[2], bidTC);
138 glColor3d (aValue[0], aValue[1], aValue[2]);
139 if (myIsDrawPolygons)
140 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
142 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
143 glEnableClientState (GL_VERTEX_ARRAY);
144 glLineWidth (aLineWidth);
145 glShadeModel(GL_FLAT);
146 glDisable(GL_LIGHTING);
149 //=======================================================================
150 //function : AfterDraw
152 //=======================================================================
154 void NIS_TriangulatedDrawer::AfterDraw (const DrawType theType,
159 case Draw_DynHilighted:
160 if (myIsDrawPolygons)
161 glDisable(GL_POLYGON_OFFSET_LINE);
163 glDisable(GL_POLYGON_OFFSET_FILL);
165 case Draw_Transparent:
166 glDisableClientState(GL_VERTEX_ARRAY);
169 if (myIsDrawPolygons)
170 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
173 //=======================================================================
176 //=======================================================================
178 void NIS_TriangulatedDrawer::Draw (const Handle_NIS_InteractiveObject& theObj,
179 const DrawType /*theType*/,
182 // Assertion for the type of the drawn object
184 static const Handle(Standard_Type) ThisType = STANDARD_TYPE(NIS_Triangulated);
185 if (theObj->IsKind(ThisType) == Standard_False)
186 Standard_ProgramError::Raise ("NIS_Triangulated::Draw: "
187 "irrelevant object type");
189 const NIS_Triangulated * pObject =
190 static_cast <const NIS_Triangulated *> (theObj.operator->());
191 glVertexPointer (3, GL_FLOAT, 0, pObject->Node(0));
192 if (myIsDrawPolygons == Standard_False) {
193 if (pObject->IsTriangulation())
194 glDrawElements (GL_TRIANGLES, pObject->NTriangles()*3,
195 GL_UNSIGNED_INT, pObject->Triangle(0));
197 if (pObject->IsPolygons()) {
198 const Standard_Integer nPoly = pObject->NPolygons();
199 for (Standard_Integer i = 0; i < nPoly; i++) {
200 Standard_Integer * arrNodes;
201 const Standard_Integer nSize = pObject->Polygon (i, arrNodes);
202 glDrawElements (GL_LINE_LOOP, nSize, GL_UNSIGNED_INT, arrNodes);
206 if (pObject->IsSegments())
207 glDrawElements (GL_LINES, pObject->NLineNodes(),
208 GL_UNSIGNED_INT, pObject->LineNode(0));
210 Standard_Boolean isLoop;
211 if (pObject->IsLine(isLoop))
213 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
214 glDrawElements (GL_LINE_LOOP, pObject->NLineNodes(),
215 GL_UNSIGNED_INT, pObject->LineNode(0));
216 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
218 glDrawElements (GL_LINE_STRIP, pObject->NLineNodes(),
219 GL_UNSIGNED_INT, pObject->LineNode(0));