0022819: Redesign of OpenGl driver
[occt.git] / src / OpenGl / OpenGl_Polyline.cxx
1 // File:      OpenGl_Polyline.cxx
2 // Created:   13 July 2011
3 // Author:    Sergey ZERCHANINOV
4 // Copyright: OPEN CASCADE 2011
5
6 #include <OpenGl_Polyline.hxx>
7
8 #include <OpenGl_tgl_all.hxx>
9 #include <GL/gl.h>
10
11 #include <OpenGl_AspectLine.hxx>
12 #include <OpenGl_Structure.hxx>
13
14 /*----------------------------------------------------------------------*/
15
16 OpenGl_Polyline::OpenGl_Polyline (const Graphic3d_Array1OfVertex& AListVertex)
17 : myNbVertices(AListVertex.Length()),
18   myVertices(NULL),
19   myColors(NULL)
20 {
21   myVertices = new TEL_POINT[myNbVertices];
22   memcpy( myVertices, &AListVertex(AListVertex.Lower()), myNbVertices*sizeof(TEL_POINT) );
23 }
24
25 /*----------------------------------------------------------------------*/
26
27 OpenGl_Polyline::OpenGl_Polyline (const Graphic3d_Array1OfVertexC& AListVertex)
28 : myNbVertices(AListVertex.Length()),
29   myVertices(NULL),
30   myColors(NULL)
31 {
32   myVertices = new TEL_POINT[myNbVertices];
33   myColors = new TEL_COLOUR[myNbVertices];
34
35   Standard_Integer i = 0, j = AListVertex.Lower();
36   Standard_Real X, Y, Z;
37   for ( ; i < myNbVertices; i++, j++)
38   {
39     AListVertex(j).Coord(X, Y, Z);
40     myVertices[i].xyz[0] = float (X);
41     myVertices[i].xyz[1] = float (Y);
42     myVertices[i].xyz[2] = float (Z);
43     AListVertex(j).Color().Values(X, Y, Z, Quantity_TOC_RGB);
44     myColors[i].rgb[0] = float (X);
45     myColors[i].rgb[1] = float (Y);
46     myColors[i].rgb[2] = float (Z);
47     myColors[i].rgb[3] = 1.0F;
48   }
49 }
50
51 /*----------------------------------------------------------------------*/
52
53 OpenGl_Polyline::OpenGl_Polyline (const Standard_Real X1,const Standard_Real Y1,const Standard_Real Z1,
54                                 const Standard_Real X2,const Standard_Real Y2,const Standard_Real Z2)
55 : myNbVertices(2),
56   myVertices(new TEL_POINT[2]),
57   myColors(NULL)
58 {
59   myVertices[0].xyz[0] = float (X1);
60   myVertices[0].xyz[1] = float (Y1);
61   myVertices[0].xyz[2] = float (Z1);
62   myVertices[1].xyz[0] = float (X2);
63   myVertices[1].xyz[1] = float (Y2);
64   myVertices[1].xyz[2] = float (Z2);
65 }
66
67 /*----------------------------------------------------------------------*/
68
69 OpenGl_Polyline::~OpenGl_Polyline ()
70 {
71   if ( myVertices )
72     delete[] myVertices;
73   if( myColors )
74     delete[] myColors;
75 }
76
77 /*----------------------------------------------------------------------*/
78
79 void OpenGl_Polyline::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
80 {
81   // Apply line aspect
82   const OpenGl_AspectLine* anAspectLine = theWorkspace->AspectLine (Standard_True);
83
84   // Temporarily disable environment mapping
85   glPushAttrib (GL_ENABLE_BIT);
86   glDisable (GL_TEXTURE_1D);
87   glDisable (GL_TEXTURE_2D);
88
89   glBegin (theWorkspace->DegenerateModel != 3 ? GL_LINE_STRIP : GL_POINTS);
90   if (!myColors || (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT))
91   {
92     // Use highlight colors
93     glColor3fv ((theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) ? theWorkspace->HighlightColor->rgb : anAspectLine->Color().rgb);
94
95     for (Tint i = 0; i < myNbVertices; ++i)
96       glVertex3fv (myVertices[i].xyz);
97   }
98   else
99   {
100     for (Tint i = 0; i < myNbVertices; ++i)
101     {
102       glColor3fv (myColors[i].rgb);
103       glVertex3fv (myVertices[i].xyz);
104     }
105   }
106   glEnd();
107
108   glPopAttrib();
109 }
110
111 /*----------------------------------------------------------------------*/