0027860: Visualization - clean up Transformation Persistence API
[occt.git] / src / OpenGl / OpenGl_Quadric.cxx
1 // Created on: 2014-10-15
2 // Created by: Denis Bogolepov
3 // Copyright (c) 2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <OpenGl_Quadric.hxx>
17
18 #include <NCollection_AlignedAllocator.hxx>
19
20 // =======================================================================
21 // function : OpenGl_Quadric
22 // purpose  :
23 // =======================================================================
24 OpenGl_Quadric::OpenGl_Quadric()
25 : OpenGl_PrimitiveArray (NULL, Graphic3d_TOPA_TRIANGLES, NULL, NULL, NULL),
26   myNbSlices (0),
27   myNbStacks (0)
28 {
29   myDrawMode = GL_TRIANGLES;
30 }
31
32 // =======================================================================
33 // function : init
34 // purpose  :
35 // =======================================================================
36 Standard_Boolean OpenGl_Quadric::init (const Standard_Integer theNbSlices,
37                                        const Standard_Integer theNbStacks)
38 {
39   myNbSlices = theNbSlices;
40   myNbStacks = theNbStacks;
41   return createArrays();
42 }
43
44 // =======================================================================
45 // function : Release
46 // purpose  :
47 // =======================================================================
48 void OpenGl_Quadric::Release (OpenGl_Context* theContext)
49 {
50   myNbSlices = 0;
51   myNbStacks = 0;
52   OpenGl_PrimitiveArray::Release (theContext);
53 }
54
55 // =======================================================================
56 // function : createArrays
57 // purpose  :
58 // =======================================================================
59 Standard_Boolean OpenGl_Quadric::createArrays() const
60 {
61   // Evaluate surface points and normals
62   Graphic3d_Attribute anAttribsInfo[] =
63   {
64     { Graphic3d_TOA_POS,  Graphic3d_TOD_VEC3 },
65     { Graphic3d_TOA_NORM, Graphic3d_TOD_VEC3 }
66   };
67   Handle(NCollection_AlignedAllocator) anAlloc = new NCollection_AlignedAllocator (16);
68   myAttribs = new Graphic3d_Buffer      (anAlloc);
69   myIndices = new Graphic3d_IndexBuffer (anAlloc);
70   if (!myAttribs->Init (NbVertices(), anAttribsInfo, 2))
71   {
72     return Standard_False;
73   }
74
75   const Standard_Integer aNbIndices = NbTriangles() * 3;
76   if (aNbIndices < Standard_Integer(USHRT_MAX))
77   {
78     if (!myIndices->Init<GLushort> (aNbIndices))
79     {
80       return Standard_False;
81     }
82   }
83   else if (!myIndices->Init<GLuint> (aNbIndices))
84   {
85     return Standard_False;
86   }
87
88   const Standard_ShortReal aStepU = 1.0f / myNbSlices;
89   const Standard_ShortReal aStepV = 1.0f / myNbStacks;
90   for (Standard_Integer aU = 0; aU <= myNbSlices; ++aU)
91   {
92     const Standard_ShortReal aParamU = aU * aStepU;
93     for (Standard_Integer aV = 0; aV <= myNbStacks; ++aV)
94     {
95       const Standard_ShortReal aParamV = aV * aStepV;
96       const Standard_Integer   aVertId = aU * (myNbStacks + 1) + aV;
97       Graphic3d_Vec3* aVertData = reinterpret_cast<Graphic3d_Vec3* >(myAttribs->changeValue (aVertId));
98       aVertData[0] = evalVertex (aParamU, aParamV);
99       aVertData[1] = evalNormal (aParamU, aParamV);
100     }
101   }
102
103   // Extract triangle indices
104   for (Standard_Integer aU = 0, aLastIndex = -1; aU < myNbSlices; ++aU)
105   {
106     for (Standard_Integer aV = 0; aV < myNbStacks; ++aV)
107     {
108       myIndices->SetIndex (++aLastIndex, aU       * (myNbStacks + 1) + aV);
109       myIndices->SetIndex (++aLastIndex, (aU + 1) * (myNbStacks + 1) + aV);
110       myIndices->SetIndex (++aLastIndex, (aU + 1) * (myNbStacks + 1) + (aV + 1));
111       myIndices->SetIndex (++aLastIndex, (aU + 1) * (myNbStacks + 1) + (aV + 1));
112       myIndices->SetIndex (++aLastIndex, aU       * (myNbStacks + 1) + (aV + 1));
113       myIndices->SetIndex (++aLastIndex, aU       * (myNbStacks + 1) + aV);
114     }
115   }
116   return Standard_True;
117 }