void matrix::setRotateZ(float rx) { init(); // reset to identity data[0][0] = cos_d(rx); // store the rotation values data[0][1] = -sin_d(rx); data[1][0] = sin_d(rx); data[1][1] = cos_d(rx); }
/* * Rotate Vector by angle y(degrees) around the y-axis. */ void vector_rotate_y(double y, Vector* v) { double x; x = cos_d(y) * v->x + 0 * v->y + sin_d(y) * v->z; // v->y = 0 * v->x + 1 * v->y + 0 * v->z; /*omitted*/ v->z = -sin_d(y) * v->x + 0 * v->y + cos_d(y) * v->z; v->x = x; }
/* * Rotate Vector by angle z(degrees) around the z-axis. */ void vector_rotate_z(double z, Vector* v) { double x; x = cos_d(z) * v->x - sin_d(z) * v->y + 0 * v->z; v->y = sin_d(z) * v->x + cos_d(z) * v->y + 0 * v->z; // v->z = 0 * v->x + 0 * v->y + 1 * v->z; /*omitted*/ v->x = x; }
/* * Rotate Vector by angle x(degrees) around the x-axis. */ void vector_rotate_x(double x, Vector* v) { //temp.saving v->y double y; // v->x = 1 * v->x + 0 * v->y + 0 * v->z; /*omitted*/ y = 0 * v->x + cos_d(x) * v->y - sin_d(x) * v->z; v->z = 0 * v->x + sin_d(x) * v->y + cos_d(x) * v->z; v->y = y; }
/* * I guess this is where the magic happens. It's just real simple geometry. We calculate a skeleton line in "solve()" and bend the profile accordingly in "calculate_Profile". */ void loesen(Naca* iterator){ while(iterator!=NULL){ for(int i=0;i<=4;i++){ /* * For the curious: In the given data we have two angles (B12 and B2) and a distance (Lax). We are looking for an arc, which has those angles at the given distance. * At first the radius of a circle is calculated, which would have those two angles at the given distance. Then it's position is determined, thus the beginning * of the skeleton line is at the point (0;0); */ iterator->radius[i]=(iterator->Lax + iterator->deltaLax * i*0.25)/(sin_d((iterator->B12[i])+(iterator->B2[i]))-sin_d(iterator->B2[i])); iterator->Mx[i]=sin_d((iterator->B12[i])+(iterator->B2[i])) * (iterator->radius[i]); iterator->My[i]= -cos_d((iterator->B12[i])+(iterator->B2[i]))*(iterator->radius[i]); } iterator = iterator->next; } }
opentorus::opentorus(int density, int slices, float radius, float height) { setColour(1.0, 1.0, 0.0); // default colour (yellow) setName("opentorus"); // if the user passes an odd number as the "slices" of the opentorus, // round this up to the next even number. if(slices%2 == 1) { slices++; } // calculate the number of vertices and faces from the mesh density vertexCount = density * slices; faceCount = density * slices; polygonCount = faceCount; // allocate memory for the vertex and face arrays vertices.resize(vertexCount); faces.resize(faceCount); float yStepSize = 360.0 / slices; // rotating around Y axis by this amount float zStepSize = 360.0 / density; // rotating around Z axis by this amount // generate the vertex information unsigned int v = 0; // init the vertex counter float zRot = 0; for (int i=0; i<density; i++) // outer loop (Z Rot) { // calculate angle around Z axis - this is the "height" of the horizontal // disc, and the Y coordinate of all vertices on the circumference of that disc float y = height * sin_d(zRot); // also calculate X - this is subtracted from the opentorus radius to // calculate the radius for the horizontal disc float x = height * cos_d(zRot); float discRad = radius - x; float yRot=0; // OK - now split the disc into slices for (int j=0; j<slices; j++) // inner loop (Y rotation) { vertices[v++].set(discRad * cos_d(yRot), y, discRad * sin_d(yRot)); yRot+=yStepSize; // step around for the next division } zRot+=zStepSize; // step around for the next disc } // this first test may never execute if we've overflowed the array (since the program will have crashed by now) if (v > vertexCount) fatal("generated more vertices than you said you would"); if (v < vertexCount) fatal("allocated space for more vertices than you generated"); // now generate the face list unsigned int k=0; // face counter for (int zLoop=0; zLoop<density; zLoop++) // loop for each disc { int offset1 = (zLoop * slices); // this is the "first vertex" for this disc int offset2 = ((offset1 + slices)% (slices * density)); // this is the "first vertex" of the NEXT disc for (int yLoop=0; yLoop<slices; yLoop+=2) // loop around each disc { // external face faces[k++].init(offset1 + yLoop, // the first vertex of this disc offset1 + ((yLoop+1)%slices), // the next is on this disc, one around offset2 + ((yLoop+1)%slices), // the next is on next disc, one around offset2 + yLoop); // the last is on the next disc, below first // internal face (reverse the order of vertexes) faces[k++].init(offset2 + yLoop, // the last is on the next disc, below first offset2 + ((yLoop+1)%slices), // the next is on next disc, one around offset1 + ((yLoop+1)%slices), // the next is on this disc, one around offset1 + yLoop); // the first vertex of this disc } } // this first test may never execute if we've overflowed the array (since the program will have crashed by now) if (k > faceCount) fatal("generated more faces than you said you would"); if (k < faceCount) fatal("allocated space for more faces than you generated"); // calculate the face and vertex normals calculateNormals(); // set the default shading model of the opentorus to diffuse diffuseShading = true; // put the shape onto the shapeVector so it gets draw messages gShapeVector.push_back(this); }
opensphere::opensphere(int density) { setColour(1.0, 0.0, 0.0); // default colour (red) setName("opensphere"); // if the user passes an odd number as the "density" of the opensphere, // round this up to the next even number. if(density%2 == 1) { density++; } // calculate the number of vertices and faces from the mesh density vertexCount = (density * (density-1)) + 2; faceCount = (density * (density - 2)) + (density * 2); polygonCount = faceCount; // allocate memory for the vertex and face arrays vertices.resize(vertexCount); faces.resize(faceCount); // generate the vertex information to build a 2-unit opensphere centred on the origin float stepSize = 180.0 / density; // rotating around Z axis by this amount float zRot = 90.0 - stepSize; // starting Z rot near top; unsigned int v = 0; // init the vertex counter for (int i=1; i<density; i++) // outer loop (Z Rot) { for (int j=0; j<density; j++) // inner loop (Y rot) { // calculate angle around Y axis float yRot = j * (360.0 / density); // calculate x, y & z coordinates float x = cos_d(zRot) * cos_d(yRot); float z = cos_d(zRot) * sin_d(yRot); float y = sin_d(zRot); vertices[v++].set(x, y, z); // init this vertex } zRot = zRot - stepSize; // next row down } vertices[v++].set(0, 1, 0); // the top vertex vertices[v++].set(0, -1, 0); // the bottom vertex // this first test may never execute if we've overflowed the array (since the program will have crashed by now) if (v > vertexCount) fatal("generated more vertices than you said you would"); if (v < vertexCount) fatal("allocated space for more vertices than you generated"); // now set up the faces unsigned int k=0; // face counter // first define the sides as quads for (int i=0; i < density-2; i++) { int offset1 = (density * i); int offset2 = (offset1 + density); for (int j=0; j < density; j+=2) { // external face faces[k++].init(offset1 + j, offset1 + ((j+1) % density), offset2 + ((j+1) % density), offset2 + j); // internal face (reverse the order of vertexes) faces[k++].init(offset2 + j, offset2 + ((j+1) % density), offset1 + ((j+1) % density), offset1 + j); } } // next define the top triangles for (int i=0; i < density; i+=2) { // external face faces[k++].init((density * (density - 1)), (i+1) % density, i); // internal face (reverse the order of vertexes) faces[k++].init(i, (i+1) % density, (density * (density - 1))); } // finally define the bottom triangles for (int i=0; i < density; i+=2) { // external face faces[k++].init( (density * (density -2)) + i, (density * (density -2)) + ((i+1) % density), (density * (density -1)) +1); // internal face (reverse the order of vertexes) faces[k++].init( (density * (density -1)) +1, (density * (density -2)) + ((i+1) % density), (density * (density -2)) + i); } // this first test may never execute if we've overflowed the array (since the program will have crashed by now) if (k > faceCount) fatal("generated more faces than you said you would"); if (k < faceCount) fatal("allocated space for more faces than you generated"); // calculate the face and vertex normals calculateNormals(); // set the default shading model of the opensphere to diffuse diffuseShading = true; // put the shape onto the shapeVector so it gets draw messages gShapeVector.push_back(this); }