-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCylinder.cpp
183 lines (149 loc) · 5.14 KB
/
Cylinder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/***********************************************************************
Cylinder - Class for cylindrical structural units.
Copyright (c) 2008-2011 Oliver Kreylos
The Nanotech Construction Kit is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Nanotech Construction Kit is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with the Nanotech Construction Kit; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#include <Misc/ConfigurationFile.h>
#include <Misc/StandardValueCoders.h>
#include <Math/Math.h>
#include <GL/gl.h>
#include <GL/GLContextData.h>
#include <GL/GLGeometryWrappers.h>
#include <GL/GLTransformationWrappers.h>
#include "Cylinder.h"
namespace NCK {
/*********************************
Methods of class CylinderRenderer:
*********************************/
void CylinderRenderer::initContext(GLContextData& contextData) const
{
/* Create context data item: */
DataItem* dataItem=new DataItem;
contextData.addDataItem(this,dataItem);
/* Create model display list: */
glNewList(dataItem->displayListId,GL_COMPILE);
/* Render cylinder: */
Scalar hradius=Cylinder::radius/Scalar(3);
glNormal3f(0.0f,0.0f,-1.0);
glBegin(GL_POLYGON);
for(int i=7;i>=0;--i)
{
Scalar angle=Scalar(2)*Math::Constants<Scalar>::pi*Scalar(i)/Scalar(8);
Scalar c=Math::cos(angle);
Scalar s=Math::sin(angle);
glVertex(Point(c*hradius,s*hradius,-Cylinder::radius));
}
glEnd();
glBegin(GL_QUAD_STRIP);
for(int i=0;i<=8;++i)
{
Scalar angle=Scalar(2)*Math::Constants<Scalar>::pi*Scalar(i%8)/Scalar(8);
Scalar c=Math::cos(angle);
Scalar s=Math::sin(angle);
glNormal(Vector(c,s,Scalar(0)));
glVertex(Point(c*hradius,s*hradius,Cylinder::radius));
glVertex(Point(c*hradius,s*hradius,-Cylinder::radius));
}
glEnd();
glNormal3f(0.0f,0.0f,1.0);
glBegin(GL_POLYGON);
for(int i=0;i<8;++i)
{
Scalar angle=Scalar(2)*Math::Constants<Scalar>::pi*Scalar(i)/Scalar(8);
Scalar c=Math::cos(angle);
Scalar s=Math::sin(angle);
glVertex(Point(c*hradius,s*hradius,Cylinder::radius));
}
glEnd();
/* Finish model display list: */
glEndList();
}
/*********************************
Static elements of class Cylinder:
*********************************/
Scalar Cylinder::radius;
Scalar Cylinder::radius2;
Scalar Cylinder::mass;
Vector Cylinder::vertexOffsets[4];
CylinderRenderer* Cylinder::unitRenderer=0;
/*************************
Methods of class Cylinder:
*************************/
void Cylinder::calculateShape(void)
{
/* Calculate vertex offset radius based on force parameters: */
Scalar vertexRadius=calcVertexRadius(radius);
/* Recalculate vertex offsets: */
vertexOffsets[0]=Vector(-radius/Scalar(3),Scalar(0),-vertexRadius);
vertexOffsets[1]=Vector(radius/Scalar(3),Scalar(0),-vertexRadius);
vertexOffsets[2]=Vector(-radius/Scalar(3),Scalar(0),vertexRadius);
vertexOffsets[3]=Vector(radius/Scalar(3),Scalar(0),vertexRadius);
}
void Cylinder::initClass(const Misc::ConfigurationFileSection& configFileSection)
{
/* Read class settings: */
radius=configFileSection.retrieveValue<Scalar>("./radius",2.0);
radius2=Math::sqr(radius);
mass=configFileSection.retrieveValue<Scalar>("./mass",4.0);
/* Calculate cylinder shape: */
calculateShape();
/* Create cylinder renderer: */
unitRenderer=new CylinderRenderer;
}
void Cylinder::deinitClass(void)
{
/* Destroy cylinder renderer: */
delete unitRenderer;
unitRenderer=0;
}
void Cylinder::setRadius(Scalar newRadius)
{
/* Set cylinder size: */
radius=newRadius;
radius2=Math::sqr(radius);
/* Re-calculate cylinder shape: */
calculateShape();
}
void Cylinder::setMass(Scalar newMass)
{
/* Set cylinder mass: */
mass=newMass;
}
void Cylinder::applyVertexForce(int index,const Vector& force,Scalar timeStep)
{
/* Apply linear acceleration: */
linearVelocity+=force*(timeStep/mass);
/* Calculate torque: */
Vector torque=Geometry::cross(orientation.transform(vertexOffsets[index]),force);
/* Apply angular acceleration: */
angularVelocity+=torque*(timeStep/mass); // Should be moment of inertia instead of mass here
}
void Cylinder::applyCentralForce(const Vector& force,Scalar timeStep)
{
/* Apply linear acceleration: */
linearVelocity+=force*(timeStep/mass);
}
void Cylinder::glRenderAction(GLContextData& contextData) const
{
/* Retrieve data item from GL context: */
CylinderRenderer::DataItem* dataItem=contextData.retrieveDataItem<CylinderRenderer::DataItem>(unitRenderer);
/* Move model coordinate system to cylinder's position and orientation: */
glPushMatrix();
glTranslate(position.getComponents());
glRotate(orientation);
/* Render cylinder: */
glCallList(dataItem->displayListId);
/* Reset model coordinates: */
glPopMatrix();
}
}