//--------------------------------------------------------- // Hmwk 13 // // Display a wireframe model using an orthogonal projection // or a perspective projection. // // D. Searls // REPLACE THIS LINE WITH YOUR NAME!! // Asbury University //--------------------------------------------------------- #include "worldType.h" #include #include #include #include using namespace std; const int MAX_VERTICES = 1500; const int MAX_EDGES = 3000; const double PI = 3.14159265358979; struct point3D { double x, y, z; // Coordinates of point }; struct edgeType { int first; // Index of first endpoint int second; // Index of second endpoint }; struct wireframe { int numVertices; point3D vertex[MAX_VERTICES]; int numEdges; edgeType edge[MAX_EDGES]; }; // COPY AND PASTE YOUR matrixType CLASS IMPLEMENTATION HERE //--------------------------------------------------------- // Global Variables //--------------------------------------------------------- wireframe model; worldType w(401, 401, 600, 10, -1.5, -1.5, 1.5, 1.5, "Wireframe Model"); bool viewIsOrthogonal = true; matrixType t; //--------------------------------------------------------- // readModel // // Read the wireframe data from the specified file into the // wireframe model. // // Return true if the read operation was successful and // false otherwise. // // In Parameter: filename // // Global Access: // wireframe model //--------------------------------------------------------- bool readModel(string filename) { } //--------------------------------------------------------- // viewModel // // If the global variable, viewIsOrthogonal, is true view // the current model using an orthogonal projection. // Otherwise, view this model using a perspective // projection. // // Global Access: // bool viewIsOrthogonal // wireframe model // worldType w // matrixType t //--------------------------------------------------------- void viewModel() { } //--------------------------------------------------------- // toLower // // Return the lowercase version of the specified string. // // In Parameter: str //--------------------------------------------------------- string toLower(string str) { string newString(str); for (unsigned int i = 0; i < str.length(); i++) { newString.at(i) = tolower(newString.at(i)); } return newString; } //--------------------------------------------------------- // displayHelp // // Explain how to use the commands. //--------------------------------------------------------- void displayHelp() { cout << "\nIn all the commands, items within square brackets are optional. For ex-\n"; cout << "ample, the help command can be entered as \"h\" or as \"help\". Commands\n"; cout << "are not case sensitive so the help command could also be entered as \"H\".\n\n"; cout << "c[lear] - clear (reset) the transformation matrix.\n\n"; cout << "h[elp] - Display this help message.\n\n"; cout << "list - display the names of the available wireframe data files.\n\n"; cout << "l[oad] filename - load a wireframe model into memory from the specified\n"; cout << " file.\n\n"; cout << "m[ove] dx# dy# dz# - move the current scene using the specified increments.\n\n"; cout << "q[uit] - Terminate the application.\n\n"; cout << "r[otate] axis angle# - rotate the current view about the axis which is\n"; cout << " entered as a single character ('x', 'y', or 'z'). The angle is\n"; cout << " measured in degrees. Alternate versions of this command include\n"; cout << " rx angle#, ry angle# and rz angle#\n\n"; cout << "s[cale] factor# - scale the current view by the given factor.\n\n"; cout << "t[ranslate] dx# dy# dz# - same as move\n\n"; cout << "v[iew] o[rthogonal] or vo - Set the viewing mode to orthogonal projection.\n\n"; cout << "v[iew] p[erspective] or vp - Set the viewing mode to perspective view.\n\n"; } //--------------------------------------------------------- // execute // // Execute the task specified by the command line. // // In Parameter: line //--------------------------------------------------------- void execute(string line) { string command; string filename; double dx, dy, dz; char axis; double angle; double scaleFactor; istringstream commandLine(line); ifstream infile; commandLine >> command; if (command == "clear" || command == "c") { t.reset(); viewModel(); } else if (command == "help" || command == "h") { displayHelp(); } else if (command == "list") { cout << endl; system("dir *.wf /b"); cout << endl; } else if (command == "load" || command == "l") { commandLine >> filename; if (commandLine.fail()) { cout << "\nYou must provide a file name.\n\n"; } else { readModel(filename); viewModel(); } } else if ( command == "move" || command == "m" || command == "translate" || command == "t") { commandLine >> dx >> dy >> dz; if (commandLine.fail()) { cout << "\nYou must specify the increments in all three directions (x, y, and z).\n\n"; } else { t.translate(dx, dy, dz); viewModel(); } } else if (command == "quit" || command == "q") { // Don't do anything } else if (command == "rotate" || command == "r") { commandLine >> axis; axis = tolower(axis); if (commandLine.fail() || (axis != 'x' && axis != 'y' && axis != 'z')) { cout << "\nYou must specify the axis ('x', 'y', or 'z')\n\n"; } else { commandLine >> angle; if (commandLine.fail()) { cout << "\nYou must enter the angle of rotation (in degrees).\n\n"; } else { t.rotate(axis, angle); viewModel(); } } } else if (command == "rx" || command == "ry" || command == "rz") { commandLine >> angle; if (commandLine.fail()) { cout << "\nYou must enter the angle of rotation (in degrees).\n\n"; } else { axis = command.at(1); t.rotate(axis, angle); viewModel(); } } else if (command == "scale" || command == "s") { commandLine >> scaleFactor; if (commandLine.fail()) { cout << "\nYou must enter the scaling factor.\n\n"; } else { t.scale(scaleFactor); viewModel(); } } else if (command == "view" || command == "v") { commandLine >> command; if (command == "orthogonal" || command == "o") { viewIsOrthogonal = true; viewModel(); } else if (command == "perspective" || command == "p") { viewIsOrthogonal = false; viewModel(); } else { cout << "\n" << command << " is an invalid view option.\n\n"; } } else if (command == "vo") { viewIsOrthogonal = true; viewModel(); } else if (command == "vp") { viewIsOrthogonal = false; viewModel(); } else { cout << "\nUnrecognized command. Enter \"help\" to see list of commands.\n\n"; } } //********************************************************* // M A I N D R I V E R //********************************************************* int main() { string command; istringstream commandStream; cout << "Wireframe Model Viewer\n\n"; cout << "This is a command line interface. To see a list of the available\n"; cout << "commands enter the \"help\" command.\n\n"; setcolor(BLACK); setbkcolor(WHITE); cleardevice(); do { cout << "> "; getline(cin, command); command = toLower(command); execute(command); } while (command != "q" && command != "quit"); return 0; }