-
Notifications
You must be signed in to change notification settings - Fork 7
/
Octahedron.cpp
181 lines (148 loc) · 5.15 KB
/
Octahedron.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
/***********************************************************************
Octahedron - Class for aluminum oxide structural units (used to build
heterogeneous silicate crystals).
Copyright (c) 2003-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 "Octahedron.h"
namespace NCK {
/***********************************
Methods of class OctahedronRenderer:
***********************************/
void OctahedronRenderer::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 octahedron: */
static const int faceVertexIndices[8][3]={{2,0,4},{1,2,4},{3,1,4},{0,3,4},
{0,2,5},{2,1,5},{1,3,5},{3,0,5}};
glBegin(GL_TRIANGLES);
for(int face=0;face<8;++face)
{
glNormal(Octahedron::renderNormals[face]);
for(int i=0;i<3;++i)
glVertex(Octahedron::renderVertices[faceVertexIndices[face][i]]);
}
glEnd();
/* Finish model display list: */
glEndList();
}
/***********************************
Static elements of class Octahedron:
***********************************/
Scalar Octahedron::radius;
Scalar Octahedron::radius2;
Scalar Octahedron::mass;
Vector Octahedron::vertexOffsets[6];
Point Octahedron::renderVertices[6];
Vector Octahedron::renderNormals[8];
OctahedronRenderer* Octahedron::unitRenderer=0;
/***************************
Methods of class Octahedron:
***************************/
void Octahedron::calculateShape(void)
{
/* Calculate vertex offset radius based on force parameters: */
Scalar vertexRadius=calcVertexRadius(radius);
/* Recalculate vertex offsets and render vertices: */
for(int i=0;i<6;++i)
{
vertexOffsets[i]=Vector::zero;
renderVertices[i]=Point::origin;
}
for(int i=0;i<3;++i)
{
vertexOffsets[2*i+0][i]=-vertexRadius;
vertexOffsets[2*i+1][i]=vertexRadius;
renderVertices[2*i+0][i]=-vertexRadius;
renderVertices[2*i+1][i]=vertexRadius;
}
/* Recalculate render normals: */
Scalar n=Scalar(1.0)/Math::sqrt(3.0);
renderNormals[0]=Vector(-n,-n,-n);
renderNormals[1]=Vector(n,-n,-n);
renderNormals[2]=Vector(n,n,-n);
renderNormals[3]=Vector(-n,n,-n);
renderNormals[4]=Vector(-n,-n,n);
renderNormals[5]=Vector(n,-n,n);
renderNormals[6]=Vector(n,n,n);
renderNormals[7]=Vector(-n,n,n);
}
void Octahedron::initClass(const Misc::ConfigurationFileSection& configFileSection)
{
/* Read class settings: */
radius=configFileSection.retrieveValue<Scalar>("./radius",1.9);
radius2=Math::sqr(radius);
mass=configFileSection.retrieveValue<Scalar>("./mass",4.0/3.0);
/* Calculate octahedron shape: */
calculateShape();
/* Create octahedron renderer: */
unitRenderer=new OctahedronRenderer;
}
void Octahedron::deinitClass(void)
{
/* Destroy octahedron renderer: */
delete unitRenderer;
unitRenderer=0;
}
void Octahedron::setRadius(Scalar newRadius)
{
/* Set octahedron size: */
radius=newRadius;
radius2=Math::sqr(radius);
/* Re-calculate octahedron shape: */
calculateShape();
}
void Octahedron::setMass(Scalar newMass)
{
/* Set octahedron mass: */
mass=newMass;
}
void Octahedron::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 Octahedron::applyCentralForce(const Vector& force,Scalar timeStep)
{
/* Apply linear acceleration: */
linearVelocity+=force*(timeStep/mass);
}
void Octahedron::glRenderAction(GLContextData& contextData) const
{
/* Retrieve data item from GL context: */
OctahedronRenderer::DataItem* dataItem=contextData.retrieveDataItem<OctahedronRenderer::DataItem>(unitRenderer);
/* Move model coordinate system to octahedron's position and orientation: */
glPushMatrix();
glTranslate(position.getComponents());
glRotate(orientation);
/* Render octahedron: */
glCallList(dataItem->displayListId);
/* Reset model coordinates: */
glPopMatrix();
}
}