#include "GL_Icosahedron.h" #include "X3D_Icosahedron.h" #include #include namespace X3DTK { namespace GL { Icosahedron::Icosahedron() : X3DGeometryNode() { // Defines the tag of the node. define(Recorder::getTypeName("Icosahedron", "Geometry3D")); // Defines an attribute of the node. define(Recorder::getAttribute("radius", &Icosahedron::_radius, 1.0f)); } Icosahedron::~Icosahedron() { } void Icosahedron::update() { X3D::Icosahedron *I = static_cast(x3dReference); // updating the attributes. _radius = I->getRadius(); // building the vertex array. const float c = cos(PI/3.0); const float s = sin(PI/3.0); _vertices = MFVec3f(20); _vertices[0] = SFVec3f(c+2.0*s, s, 0.0); _vertices[1] = SFVec3f(-c-2.0*s, s, 0.0); _vertices[2] = SFVec3f(c+2.0*s, -s, 0.0); _vertices[3] = SFVec3f(-c-2.0*s, -s, 0.0); _vertices[4] = SFVec3f(0.0, 2.0*s+c, s); _vertices[5] = SFVec3f(0.0, -2.0*s-c, s); _vertices[6] = SFVec3f(0.0, 2.0*s+c, -s); _vertices[7] = SFVec3f(0.0, -2.0*s-c, -s); _vertices[8] = SFVec3f(-c-s, -c-s, c+s); _vertices[9] = SFVec3f(-c-s, c+s, c+s); _vertices[10] = SFVec3f(-c-s, -c-s, -c-s); _vertices[11] = SFVec3f(-c-s, c+s, -c-s); _vertices[12] = SFVec3f(c+s, -c-s, c+s); _vertices[13] = SFVec3f(c+s, c+s, c+s); _vertices[14] = SFVec3f(c+s, -c-s, -c-s); _vertices[15] = SFVec3f(c+s, c+s, -c-s); _vertices[16] = SFVec3f(-s, 0.0, -2.0*s-c); _vertices[17] = SFVec3f(s, 0.0, -2.0*s-c); _vertices[18] = SFVec3f(-s, 0.0, 2.0*s+c); _vertices[19] = SFVec3f(s, 0.0, 2.0*s+c); static short array[12][5] = { { 0, 13, 19, 12, 2 }, { 0, 2, 14, 17, 15 } , { 0, 15, 6, 4, 13 } , { 6, 15, 17, 16, 11 } , { 4, 6, 11, 1, 9 } , { 11, 16, 10, 3, 1 } , { 16, 17, 14, 7, 10 } , { 10, 7, 5, 8, 3 } , { 5, 12, 19, 18, 8 } , { 12, 5, 7, 14, 2 } , { 18, 19, 13, 4, 9 } , { 1, 3, 8, 18, 9 } }; _normal = MFVec3f(12); for (int i = 0; i < 12; ++i) { _normal[i] = SFVec3f(0.0, 0.0, 0.0); for (int j = 0; j < 5; ++j) { _indexes[i][j] = array[i][j]; _normal[i] += _vertices[_indexes[i][j]]; } _normal[i].normalize(); } } void Icosahedron::setRadius(const SFFloat &radius) { _radius = radius; } void Icosahedron::draw() const { // drawing the polygon. // Changing the scale for the radius of the object. float s = _radius/4.47f; glMatrixMode(GL_MODELVIEW); glPushMatrix(); glScalef(s, s, s); for (int i = 0; i < 12; ++i) { // drawing the normals and the faces. glNormal3f(_normal[i].x, _normal[i].y, _normal[i].z); glBegin(GL_POLYGON); for (int j = 0; j < 5; ++j) glVertex3f(_vertices[_indexes[i][j]].x, _vertices[_indexes[i][j]].y, _vertices[_indexes[i][j]].z); glEnd(); } glPopMatrix(); } } }