1 // Created on: 2014-10-15
2 // Created by: Denis Bogolepov
3 // Copyright (c) 2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <OpenGl_Quadric.hxx>
18 #include <NCollection_AlignedAllocator.hxx>
20 // =======================================================================
21 // function : OpenGl_Quadric
23 // =======================================================================
24 OpenGl_Quadric::OpenGl_Quadric()
25 : OpenGl_PrimitiveArray (NULL, Graphic3d_TOPA_TRIANGLES, NULL, NULL, NULL),
29 myDrawMode = GL_TRIANGLES;
32 // =======================================================================
35 // =======================================================================
36 Standard_Boolean OpenGl_Quadric::init (const Standard_Integer theNbSlices,
37 const Standard_Integer theNbStacks)
39 myNbSlices = theNbSlices;
40 myNbStacks = theNbStacks;
41 return createArrays();
44 // =======================================================================
47 // =======================================================================
48 void OpenGl_Quadric::Release (OpenGl_Context* theContext)
52 OpenGl_PrimitiveArray::Release (theContext);
55 // =======================================================================
56 // function : createArrays
58 // =======================================================================
59 Standard_Boolean OpenGl_Quadric::createArrays() const
61 // Evaluate surface points and normals
62 Graphic3d_Attribute anAttribsInfo[] =
64 { Graphic3d_TOA_POS, Graphic3d_TOD_VEC3 },
65 { Graphic3d_TOA_NORM, Graphic3d_TOD_VEC3 }
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))
72 return Standard_False;
75 const Standard_Integer aNbIndices = NbTriangles() * 3;
76 if (aNbIndices < Standard_Integer(USHRT_MAX))
78 if (!myIndices->Init<GLushort> (aNbIndices))
80 return Standard_False;
83 else if (!myIndices->Init<GLuint> (aNbIndices))
85 return Standard_False;
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)
92 const Standard_ShortReal aParamU = aU * aStepU;
93 for (Standard_Integer aV = 0; aV <= myNbStacks; ++aV)
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);
103 // Extract triangle indices
104 for (Standard_Integer aU = 0, aLastIndex = -1; aU < myNbSlices; ++aU)
106 for (Standard_Integer aV = 0; aV < myNbStacks; ++aV)
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);
116 return Standard_True;