void idlefunc() { bool change = false; if (scene.active_camera_valid() && scene.cameras[scene.active_camera]->focus == NULL) { if (keys['w']) { scene.cameras[scene.active_camera]->position += -0.25f*ror3(vec3f(0.0, 0.0, 1.0), scene.cameras[scene.active_camera]->orientation); change = true; } if (keys['s']) { scene.cameras[scene.active_camera]->position += 0.25f*ror3(vec3f(0.0, 0.0, 1.0), scene.cameras[scene.active_camera]->orientation); change = true; } if (keys['a']) { scene.cameras[scene.active_camera]->position += -0.25f*ror3(vec3f(1.0, 0.0, 0.0), scene.cameras[scene.active_camera]->orientation); change = true; } if (keys['d']) { scene.cameras[scene.active_camera]->position += 0.25f*ror3(vec3f(1.0, 0.0, 0.0), scene.cameras[scene.active_camera]->orientation); change = true; } if (keys['q']) { scene.cameras[scene.active_camera]->position += -0.25f*ror3(vec3f(0.0, 1.0, 0.0), scene.cameras[scene.active_camera]->orientation); change = true; } if (keys['e']) { scene.cameras[scene.active_camera]->position += 0.25f*ror3(vec3f(0.0, 1.0, 0.0), scene.cameras[scene.active_camera]->orientation); change = true; } } else if (scene.active_camera_valid() && scene.cameras[scene.active_camera]->focus != NULL) { if (keys['w']) { scene.cameras[scene.active_camera]->radius -= 0.25; change = true; } if (keys['s']) { scene.cameras[scene.active_camera]->radius += 0.25; change = true; } } if (change) glutPostRedisplay(); }
void init(string working_directory) { for (int i = 0; i < 256; i++) keys[i] = false; canvas.working_directory = working_directory; scene.canvas = &canvas; scene.cameras.push_back(new frustumhdl()); scene.objects.push_back(new pyramidhdl(1.0, 1.0, 8)); for (int k = 0; k < scene.objects.back()->rigid.size(); k++) for (int i = 0; i < scene.objects.back()->rigid[k].geometry.size(); i++) { swap(scene.objects.back()->rigid[k].geometry[i][1], scene.objects.back()->rigid[k].geometry[i][2]); scene.objects.back()->rigid[k].geometry[i][1] *= -1.0; swap(scene.objects.back()->rigid[k].geometry[i][4], scene.objects.back()->rigid[k].geometry[i][5]); scene.objects.back()->rigid[k].geometry[i][4] *= -1.0; } swap(scene.objects.back()->bound[2], scene.objects.back()->bound[4]); swap(scene.objects.back()->bound[3], scene.objects.back()->bound[5]); scene.cameras.back()->model = scene.objects.back(); if (!scene.active_camera_valid()) { scene.active_camera = scene.cameras.size()-1; scene.cameras[scene.active_camera]->project(&canvas); } scene.cameras[scene.active_camera]->position[2] = 10.0; }
void init() { for (int i = 0; i < 256; i++) keys[i] = false; scene.cameras.push_back(new frustumhdl()); scene.objects.push_back(new pyramidhdl(1.0, 1.0, 8)); for (int k = 0; k < scene.objects.back()->rigid.size(); k++) for (int i = 0; i < scene.objects.back()->rigid[k].geometry.size(); i++) { swap(scene.objects.back()->rigid[k].geometry[i][1], scene.objects.back()->rigid[k].geometry[i][2]); scene.objects.back()->rigid[k].geometry[i][1] *= -1.0; swap(scene.objects.back()->rigid[k].geometry[i][4], scene.objects.back()->rigid[k].geometry[i][5]); scene.objects.back()->rigid[k].geometry[i][4] *= -1.0; } swap(scene.objects.back()->bound[2], scene.objects.back()->bound[4]); swap(scene.objects.back()->bound[3], scene.objects.back()->bound[5]); scene.cameras.back()->model = scene.objects.back(); if (!scene.active_camera_valid()) { scene.active_camera = scene.cameras.size()-1; scene.cameras[scene.active_camera]->project(); } scene.cameras[scene.active_camera]->position[2] = 10.0; glEnable(GL_DEPTH_TEST); }
void pmotionfunc(int x, int y) { if (bound) { glutSetMenu(canvas_menu_id); int deltax = x - mousex; int deltay = y - mousey; mousex = x; mousey = y; bool warp = false; if (mousex > 3*canvas.get_width()/4 || mousex < canvas.get_width()/4) { mousex = canvas.get_width()/2; warp = true; } if (mousey > 3*canvas.get_height()/4 || mousey < canvas.get_height()/4) { mousey = canvas.get_height()/2; warp = true; } if (warp) glutWarpPointer(mousex, mousey); if (scene.active_camera_valid()) { scene.cameras[scene.active_camera]->orientation[1] -= (float)deltax/500.0; scene.cameras[scene.active_camera]->orientation[0] -= (float)deltay/500.0; } glutPostRedisplay(); } else if (scene.active_camera_valid()) { vec3f direction; vec3f position; if (scene.active_camera_valid()) { if (scene.cameras[scene.active_camera]->type == "ortho") { position = canvas.unproject(canvas.to_window(vec2i(x, y))); direction = ror3(vec3f(0.0f, 0.0f, 1.0f), scene.cameras[scene.active_camera]->orientation); } else { position = scene.cameras[scene.active_camera]->position; direction = norm(canvas.unproject(canvas.to_window(vec2i(x, y)))); } } int old_active_object = scene.active_object; scene.active_object = -1; for (int i = 0; i < scene.objects.size(); i++) { if (scene.objects[i] != NULL && scene.cameras[scene.active_camera]->model != scene.objects[i]) { bool is_light = false; bool is_camera = false; // TODO Assignment 3: uncomment this //for (int j = 0; j < scene.lights.size() && !is_light; j++) // if (scene.lights[j] != NULL && scene.lights[j]->model == scene.objects[i]) // is_light = true; for (int j = 0; j < scene.cameras.size() && !is_camera; j++) if (scene.cameras[j] != NULL && scene.cameras[j]->model == scene.objects[i]) is_camera = true; if ((!is_light && !is_camera) || (is_light && scene.render_lights) || (is_camera && scene.render_cameras)) { vec3f invdir = 1.0f/direction; vec3i sign((int)(invdir[0] < 0), (int)(invdir[1] < 0), (int)(invdir[2] < 0)); vec3f origin = position - scene.objects[i]->position; float tmin, tmax, tymin, tymax, tzmin, tzmax; tmin = (scene.objects[i]->bound[0 + sign[0]]*scene.objects[i]->scale - origin[0])*invdir[0]; tmax = (scene.objects[i]->bound[0 + 1-sign[0]]*scene.objects[i]->scale - origin[0])*invdir[0]; tymin = (scene.objects[i]->bound[2 + sign[1]]*scene.objects[i]->scale - origin[1])*invdir[1]; tymax = (scene.objects[i]->bound[2 + 1-sign[1]]*scene.objects[i]->scale - origin[1])*invdir[1]; if ((tmin <= tymax) && (tymin <= tmax)) { if (tymin > tmin) tmin = tymin; if (tymax < tmax) tmax = tymax; tzmin = (scene.objects[i]->bound[4 + sign[2]]*scene.objects[i]->scale - origin[2])*invdir[2]; tzmax = (scene.objects[i]->bound[4 + 1-sign[2]]*scene.objects[i]->scale - origin[2])*invdir[2]; if ((tmin <= tzmax) && (tzmin <= tmax)) { scene.active_object = i; i = scene.objects.size(); } } } } } if (scene.active_object != old_active_object) { bool is_light = false; bool is_camera = false; // TODO Assignment 3: uncomment this //for (int i = 0; i < scene.lights.size() && !is_light; i++) // if (scene.lights[i] != NULL && scene.active_object_valid() && scene.lights[i]->model == scene.objects[scene.active_object]) // is_light = true; for (int i = 0; i < scene.cameras.size() && !is_camera; i++) if (scene.cameras[i] != NULL && scene.active_object_valid() && scene.cameras[i]->model == scene.objects[scene.active_object]) is_camera = true; glutDetachMenu(GLUT_RIGHT_BUTTON); if (scene.active_object == -1) glutSetMenu(canvas_menu_id); else if (is_light) glutSetMenu(light_menu_id); else if (is_camera) glutSetMenu(camera_menu_id); else glutSetMenu(object_menu_id); glutAttachMenu(GLUT_RIGHT_BUTTON); glutPostRedisplay(); } } }
void object_menu(int num) { if (num == 0) { if (scene.active_object >= 0 && scene.active_object < scene.objects.size()) { if (scene.objects[scene.active_object] != NULL) { // TODO Assignment 3: uncomment this /*for (int i = 0; i < scene.lights.size(); ) { if (scene.lights[i] != NULL && scene.lights[i]->model == scene.objects[scene.active_object]) { delete scene.lights[i]; scene.lights.erase(scene.lights.begin() + i); } else i++; }*/ for (int i = 0; i < scene.cameras.size(); ) { if (scene.cameras[i] != NULL && scene.cameras[i]->model == scene.objects[scene.active_object]) { delete scene.cameras[i]; if (scene.active_camera > i) scene.active_camera--; scene.cameras.erase(scene.cameras.begin() + i); } else i++; } delete scene.objects[scene.active_object]; } scene.objects.erase(scene.objects.begin() + scene.active_object); glutPostRedisplay(); } } else if (num == 4) { scene.active_camera = -1; for (int i = 0; i < scene.cameras.size(); i++) if (scene.cameras[i] != NULL && scene.active_object_valid() && scene.cameras[i]->model == scene.objects[scene.active_object]) scene.active_camera = i; if (scene.active_camera_valid()) scene.cameras[scene.active_camera]->project(&canvas); glutPostRedisplay(); } else if (num == 1) manipulator = manipulate::translate; else if (num == 2) manipulator = manipulate::rotate; else if (num == 3) manipulator = manipulate::scale; else if (num == 5 && scene.active_object_valid() && scene.active_camera_valid()) { scene.cameras[scene.active_camera]->focus = scene.objects[scene.active_object]; scene.cameras[scene.active_camera]->radius = dist(scene.objects[scene.active_object]->position, scene.cameras[scene.active_camera]->position); } // TODO Assignment 4: uncomment this /*else if (num == 6 && scene.active_object_valid()) { for (map<string, materialhdl*>::iterator i = scene.objects[scene.active_object]->material.begin(); i != scene.objects[scene.active_object]->material.end(); i++) { if (i->second != NULL) delete i->second; i->second = new texturehdl(); } glutPostRedisplay(); }*/ // TODO Assignment 3: uncomment this /*else if (num == 7 && scene.active_object_valid()) { for (map<string, materialhdl*>::iterator i = scene.objects[scene.active_object]->material.begin(); i != scene.objects[scene.active_object]->material.end(); i++) { if (i->second != NULL) delete i->second; i->second = new customhdl(); } glutPostRedisplay(); } else if (num == 8 && scene.active_object_valid()) { for (map<string, materialhdl*>::iterator i = scene.objects[scene.active_object]->material.begin(); i != scene.objects[scene.active_object]->material.end(); i++) { if (i->second != NULL) delete i->second; i->second = new phonghdl(); } glutPostRedisplay(); } else if (num == 9 && scene.active_object_valid()) { for (map<string, materialhdl*>::iterator i = scene.objects[scene.active_object]->material.begin(); i != scene.objects[scene.active_object]->material.end(); i++) { if (i->second != NULL) delete i->second; i->second = new gouraudhdl(); } glutPostRedisplay(); } else if (num == 10 && scene.active_object_valid()) { for (map<string, materialhdl*>::iterator i = scene.objects[scene.active_object]->material.begin(); i != scene.objects[scene.active_object]->material.end(); i++) { if (i->second != NULL) delete i->second; i->second = new whitehdl(); } glutPostRedisplay(); }*/ }
void canvas_menu(int num) { if (num == 0) exit(0); else if (num == 1) scene.objects.push_back(new boxhdl(1.0, 1.0, 1.0)); else if (num == 2) scene.objects.push_back(new cylinderhdl(1.0, 1.0, 20)); else if (num == 3) scene.objects.push_back(new spherehdl(1.0, 10, 20)); else if (num == 4) scene.objects.push_back(new pyramidhdl(1.0, 1.0, 20)); else if (num == 5) { const char* filters[1]; filters[0] = "*.obj"; const char *path = tinyfd_openFileDialog("Load a Model", "", 1, filters, 0); if (path != NULL && strlen(path) > 0) scene.objects.push_back(new modelhdl(path)); } // TODO Assignment 3: uncomment this /*else if (num == 6) scene.render_lights = !scene.render_lights; else if (num == 7) { scene.lights.push_back(new directionalhdl()); scene.objects.push_back(new cylinderhdl(0.25, 1.0, 8)); for (int k = 0; k < scene.objects.back()->rigid.size(); k++) for (int i = 0; i < scene.objects.back()->rigid[k].geometry.size(); i++) { swap(scene.objects.back()->rigid[k].geometry[i][1], scene.objects.back()->rigid[k].geometry[i][2]); scene.objects.back()->rigid[k].geometry[i][1] *= -1.0; swap(scene.objects.back()->rigid[k].geometry[i][4], scene.objects.back()->rigid[k].geometry[i][5]); scene.objects.back()->rigid[k].geometry[i][4] *= -1.0; } swap(scene.objects.back()->bound[2], scene.objects.back()->bound[4]); swap(scene.objects.back()->bound[3], scene.objects.back()->bound[5]); scene.lights.back()->model = scene.objects.back(); } else if (num == 8) { scene.lights.push_back(new pointhdl()); scene.objects.push_back(new spherehdl(0.25, 4, 8)); scene.lights.back()->model = scene.objects.back(); } else if (num == 9) { scene.lights.push_back(new spothdl()); scene.objects.push_back(new pyramidhdl(0.25, 1.0, 8)); for (int k = 0; k < scene.objects.back()->rigid.size(); k++) for (int i = 0; i < scene.objects.back()->rigid[k].geometry.size(); i++) { swap(scene.objects.back()->rigid[k].geometry[i][1], scene.objects.back()->rigid[k].geometry[i][2]); scene.objects.back()->rigid[k].geometry[i][1] *= -1.0; swap(scene.objects.back()->rigid[k].geometry[i][4], scene.objects.back()->rigid[k].geometry[i][5]); scene.objects.back()->rigid[k].geometry[i][4] *= -1.0; } swap(scene.objects.back()->bound[2], scene.objects.back()->bound[4]); swap(scene.objects.back()->bound[3], scene.objects.back()->bound[5]); scene.lights.back()->model = scene.objects.back(); }*/ else if (num == 10) manipulator = manipulate::fovy; else if (num == 11) manipulator = manipulate::aspect; else if (num == 12) manipulator = manipulate::width; else if (num == 13) manipulator = manipulate::height; else if (num == 14) manipulator = manipulate::front; else if (num == 15) manipulator = manipulate::back; else if (num == 16) scene.render_cameras = !scene.render_cameras; else if (num == 17 && scene.active_camera_valid()) scene.cameras[scene.active_camera]->focus = NULL; else if (num == 18) { scene.cameras.push_back(new orthohdl()); scene.objects.push_back(new pyramidhdl(1.0, 1.0, 8)); for (int k = 0; k < scene.objects.back()->rigid.size(); k++) for (int i = 0; i < scene.objects.back()->rigid[k].geometry.size(); i++) { swap(scene.objects.back()->rigid[k].geometry[i][1], scene.objects.back()->rigid[k].geometry[i][2]); scene.objects.back()->rigid[k].geometry[i][1] *= -1.0; swap(scene.objects.back()->rigid[k].geometry[i][4], scene.objects.back()->rigid[k].geometry[i][5]); scene.objects.back()->rigid[k].geometry[i][4] *= -1.0; } swap(scene.objects.back()->bound[2], scene.objects.back()->bound[4]); swap(scene.objects.back()->bound[3], scene.objects.back()->bound[5]); scene.cameras.back()->model = scene.objects.back(); if (!scene.active_camera_valid()) { scene.active_camera = scene.cameras.size()-1; scene.cameras[scene.active_camera]->project(&canvas); } } else if (num == 19) { scene.cameras.push_back(new frustumhdl()); scene.objects.push_back(new pyramidhdl(1.0, 1.0, 8)); for (int k = 0; k < scene.objects.back()->rigid.size(); k++) for (int i = 0; i < scene.objects.back()->rigid[k].geometry.size(); i++) { swap(scene.objects.back()->rigid[k].geometry[i][1], scene.objects.back()->rigid[k].geometry[i][2]); scene.objects.back()->rigid[k].geometry[i][1] *= -1.0; swap(scene.objects.back()->rigid[k].geometry[i][4], scene.objects.back()->rigid[k].geometry[i][5]); scene.objects.back()->rigid[k].geometry[i][4] *= -1.0; } swap(scene.objects.back()->bound[2], scene.objects.back()->bound[4]); swap(scene.objects.back()->bound[3], scene.objects.back()->bound[5]); scene.cameras.back()->model = scene.objects.back(); if (!scene.active_camera_valid()) { scene.active_camera = scene.cameras.size()-1; scene.cameras[scene.active_camera]->project(&canvas); } } else if (num == 20) { scene.cameras.push_back(new perspectivehdl()); scene.objects.push_back(new pyramidhdl(1.0, 1.0, 8)); for (int k = 0; k < scene.objects.back()->rigid.size(); k++) for (int i = 0; i < scene.objects.back()->rigid[k].geometry.size(); i++) { swap(scene.objects.back()->rigid[k].geometry[i][1], scene.objects.back()->rigid[k].geometry[i][2]); scene.objects.back()->rigid[k].geometry[i][1] *= -1.0; swap(scene.objects.back()->rigid[k].geometry[i][4], scene.objects.back()->rigid[k].geometry[i][5]); scene.objects.back()->rigid[k].geometry[i][4] *= -1.0; } swap(scene.objects.back()->bound[2], scene.objects.back()->bound[4]); swap(scene.objects.back()->bound[3], scene.objects.back()->bound[5]); scene.cameras.back()->model = scene.objects.back(); if (!scene.active_camera_valid()) { scene.active_camera = scene.cameras.size()-1; scene.cameras[scene.active_camera]->project(&canvas); } } else if (num == 21) canvas.polygon_mode = canvashdl::point; else if (num == 22) canvas.polygon_mode = canvashdl::line; else if (num == 23) canvas.polygon_mode = canvashdl::fill; // TODO Assignment 3: uncomment this //else if (num == 25) // canvas.shade_model = canvashdl::flat; //else if (num == 26) // canvas.shade_model = canvashdl::smooth; else if (num == 28) canvas.culling = canvashdl::disable; else if (num == 29) canvas.culling = canvashdl::backface; else if (num == 30) canvas.culling = canvashdl::frontface; else if (num == 31) scene.render_normals = scenehdl::none; else if (num == 32) scene.render_normals = scenehdl::face; else if (num == 33) scene.render_normals = scenehdl::vertex; glutPostRedisplay(); }
void motionfunc(int x, int y) { if (!bound && !menu) { int deltax = x - mousex; int deltay = mousey - y; mousex = x; mousey = y; vec3f direction; vec3f position; if (scene.active_camera_valid()) { if (scene.cameras[scene.active_camera]->type == "ortho") { position = canvas.unproject(canvas.to_window(vec2i(x, y))); direction = ror3(vec3f(0.0f, 0.0f, 1.0f), scene.cameras[scene.active_camera]->orientation); } else { position = scene.cameras[scene.active_camera]->position; direction = norm(canvas.unproject(canvas.to_window(vec2i(x, y)))); } } if (scene.active_object_valid() && scene.active_camera_valid()) { if (manipulator == manipulate::translate) { float d = mag(scene.objects[scene.active_object]->position - position); scene.objects[scene.active_object]->position = d*direction + position; } else if (manipulator == manipulate::rotate) scene.objects[scene.active_object]->orientation += vec3f(-(float)deltay/100.0, (float)deltax/100.0, 0.0); else if (manipulator == manipulate::scale) scene.objects[scene.active_object]->scale += (float)deltay/100.0; for (int i = 0; i < scene.cameras.size(); i++) if (scene.cameras[i]->model == scene.objects[scene.active_object]) { scene.cameras[i]->position = scene.objects[scene.active_object]->position; scene.cameras[i]->orientation = scene.objects[scene.active_object]->orientation; } } if (scene.active_camera_valid()) { if (manipulator == manipulate::fovy && scene.cameras[scene.active_camera]->type == "perspective") ((perspectivehdl*)scene.cameras[scene.active_camera])->fovy += (float)deltay/100.0; else if (manipulator == manipulate::aspect && scene.cameras[scene.active_camera]->type == "perspective") ((perspectivehdl*)scene.cameras[scene.active_camera])->aspect += (float)deltay/100.0; else if (manipulator == manipulate::width && scene.cameras[scene.active_camera]->type == "ortho") { ((orthohdl*)scene.cameras[scene.active_camera])->right += (float)deltay/200.0; ((orthohdl*)scene.cameras[scene.active_camera])->left -= (float)deltay/200.0; } else if (manipulator == manipulate::width && scene.cameras[scene.active_camera]->type == "frustum") { ((frustumhdl*)scene.cameras[scene.active_camera])->right += (float)deltay/200.0; ((frustumhdl*)scene.cameras[scene.active_camera])->left -= (float)deltay/200.0; } else if (manipulator == manipulate::height && scene.cameras[scene.active_camera]->type == "ortho") { ((orthohdl*)scene.cameras[scene.active_camera])->top += (float)deltay/200.0; ((orthohdl*)scene.cameras[scene.active_camera])->bottom -= (float)deltay/200.0; } else if (manipulator == manipulate::height && scene.cameras[scene.active_camera]->type == "frustum") { ((frustumhdl*)scene.cameras[scene.active_camera])->top += (float)deltay/200.0; ((frustumhdl*)scene.cameras[scene.active_camera])->bottom -= (float)deltay/200.0; } else if (manipulator == manipulate::front && scene.cameras[scene.active_camera]->type == "ortho") ((orthohdl*)scene.cameras[scene.active_camera])->front += (float)deltay/100.0; else if (manipulator == manipulate::front && scene.cameras[scene.active_camera]->type == "frustum") ((frustumhdl*)scene.cameras[scene.active_camera])->front += (float)deltay/100.0; else if (manipulator == manipulate::front && scene.cameras[scene.active_camera]->type == "perspective") ((perspectivehdl*)scene.cameras[scene.active_camera])->front += (float)deltay/100.0; else if (manipulator == manipulate::back && scene.cameras[scene.active_camera]->type == "ortho") ((orthohdl*)scene.cameras[scene.active_camera])->back += (float)deltay/100.0; else if (manipulator == manipulate::back && scene.cameras[scene.active_camera]->type == "frustum") ((frustumhdl*)scene.cameras[scene.active_camera])->back += (float)deltay/100.0; else if (manipulator == manipulate::back && scene.cameras[scene.active_camera]->type == "perspective") ((perspectivehdl*)scene.cameras[scene.active_camera])->back += (float)deltay/100.0; if (manipulator == manipulate::fovy || manipulator == manipulate::aspect || manipulator == manipulate::width || manipulator == manipulate::height || manipulator == manipulate::front || manipulator == manipulate::back) scene.cameras[scene.active_camera]->project(&canvas); } glutPostRedisplay(); } else if (!bound) { menu = false; pmotionfunc(x, y); } }
void object_menu(int num) { if (num == 0) { if (scene.active_object >= 0 && scene.active_object < scene.objects.size()) { if (scene.objects[scene.active_object] != NULL) { for (int i = 0; i < scene.lights.size(); ) { if (scene.lights[i] != NULL && scene.lights[i]->model == scene.objects[scene.active_object]) { delete scene.lights[i]; scene.lights.erase(scene.lights.begin() + i); } else i++; } for (int i = 0; i < scene.cameras.size(); ) { if (scene.cameras[i] != NULL && scene.cameras[i]->model == scene.objects[scene.active_object]) { delete scene.cameras[i]; if (scene.active_camera > i) scene.active_camera--; scene.cameras.erase(scene.cameras.begin() + i); } else i++; } delete scene.objects[scene.active_object]; } scene.objects.erase(scene.objects.begin() + scene.active_object); glutPostRedisplay(); } } else if (num == 4) { scene.active_camera = -1; for (int i = 0; i < scene.cameras.size(); i++) if (scene.cameras[i] != NULL && scene.active_object_valid() && scene.cameras[i]->model == scene.objects[scene.active_object]) scene.active_camera = i; if (scene.active_camera_valid()) scene.cameras[scene.active_camera]->project(); glutPostRedisplay(); } else if (num == 1) manipulator = manipulate::translate; else if (num == 2) manipulator = manipulate::rotate; else if (num == 3) manipulator = manipulate::scale; else if (num == 5 && scene.active_object_valid() && scene.active_camera_valid()) { scene.cameras[scene.active_camera]->focus = scene.objects[scene.active_object]; scene.cameras[scene.active_camera]->radius = dist(scene.objects[scene.active_object]->position, scene.cameras[scene.active_camera]->position); } else if (num == 6 && scene.active_object_valid()) { for (map<string, materialhdl*>::iterator i = scene.objects[scene.active_object]->material.begin(); i != scene.objects[scene.active_object]->material.end(); i++) { if (i->second != NULL) delete i->second; i->second = new texturehdl(); } glutPostRedisplay(); } else if (num == 7 && scene.active_object_valid()) { for (map<string, materialhdl*>::iterator i = scene.objects[scene.active_object]->material.begin(); i != scene.objects[scene.active_object]->material.end(); i++) { if (i->second != NULL) delete i->second; i->second = new customhdl(); } glutPostRedisplay(); } else if (num == 8 && scene.active_object_valid()) { for (map<string, materialhdl*>::iterator i = scene.objects[scene.active_object]->material.begin(); i != scene.objects[scene.active_object]->material.end(); i++) { if (i->second != NULL) delete i->second; i->second = new phonghdl(); } glutPostRedisplay(); } else if (num == 9 && scene.active_object_valid()) { for (map<string, materialhdl*>::iterator i = scene.objects[scene.active_object]->material.begin(); i != scene.objects[scene.active_object]->material.end(); i++) { if (i->second != NULL) delete i->second; i->second = new gouraudhdl(); } glutPostRedisplay(); } else if (num == 10 && scene.active_object_valid()) { for (map<string, materialhdl*>::iterator i = scene.objects[scene.active_object]->material.begin(); i != scene.objects[scene.active_object]->material.end(); i++) { if (i->second != NULL) delete i->second; i->second = new whitehdl(); } glutPostRedisplay(); } else if (num == 11) manipulator = manipulate::stepsize; else if (num == 12 && scene.active_object_valid()) scene.objects[scene.active_object]->position_interpolator = 0; else if (num == 13 && scene.active_object_valid()) scene.objects[scene.active_object]->position_interpolator = 1; else if (num == 14 && scene.active_object_valid()) scene.objects[scene.active_object]->position_interpolator = 2; else if (num == 15 && scene.active_object_valid()) scene.objects[scene.active_object]->position_interpolator = 3; else if (num == 16 && scene.active_object_valid()) scene.objects[scene.active_object]->position_interpolator = 4; else if (num == 20 && scene.active_object_valid()) scene.objects[scene.active_object]->orientation_interpolator = 0; else if (num == 21 && scene.active_object_valid()) scene.objects[scene.active_object]->orientation_interpolator = 1; else if (num == 22 && scene.active_object_valid()) scene.objects[scene.active_object]->orientation_interpolator = 2; else if (num == 23 && scene.active_object_valid()) scene.objects[scene.active_object]->orientation_interpolator = 3; else if (num == 24 && scene.active_object_valid()) scene.objects[scene.active_object]->orientation_interpolator = 4; }
void canvas_menu(int num) { if (num == 0) exit(0); else if (num == 1) scene.objects.push_back(new boxhdl(1.0, 1.0, 1.0)); else if (num == 2) scene.objects.push_back(new cylinderhdl(1.0, 1.0, 20)); else if (num == 3) scene.objects.push_back(new spherehdl(1.0, 10, 20)); else if (num == 4) scene.objects.push_back(new pyramidhdl(1.0, 1.0, 20)); else if (num == 5) { const char* filters[2]; filters[0] = "*.obj"; filters[1] = "*.wrl"; const char *path = tinyfd_openFileDialog("Load a Model", "", 2, filters, 0); if (path != NULL && path[0] != '\0') { string pathstr = path; scene.objects.push_back(new modelhdl(path)); } } else if (num == 6) scene.render_lights = !scene.render_lights; else if (num == 7) { scene.lights.push_back(new directionalhdl()); scene.objects.push_back(new cylinderhdl(0.25, 1.0, 8)); for (int k = 0; k < scene.objects.back()->rigid.size(); k++) for (int i = 0; i < scene.objects.back()->rigid[k].geometry.size(); i++) { swap(scene.objects.back()->rigid[k].geometry[i][1], scene.objects.back()->rigid[k].geometry[i][2]); scene.objects.back()->rigid[k].geometry[i][1] *= -1.0; swap(scene.objects.back()->rigid[k].geometry[i][4], scene.objects.back()->rigid[k].geometry[i][5]); scene.objects.back()->rigid[k].geometry[i][4] *= -1.0; } swap(scene.objects.back()->bound[2], scene.objects.back()->bound[4]); swap(scene.objects.back()->bound[3], scene.objects.back()->bound[5]); scene.lights.back()->model = scene.objects.back(); } else if (num == 8) { scene.lights.push_back(new pointhdl()); scene.objects.push_back(new spherehdl(0.25, 4, 8)); scene.lights.back()->model = scene.objects.back(); } else if (num == 9) { scene.lights.push_back(new spothdl()); scene.objects.push_back(new pyramidhdl(0.25, 1.0, 8)); for (int k = 0; k < scene.objects.back()->rigid.size(); k++) for (int i = 0; i < scene.objects.back()->rigid[k].geometry.size(); i++) { swap(scene.objects.back()->rigid[k].geometry[i][1], scene.objects.back()->rigid[k].geometry[i][2]); scene.objects.back()->rigid[k].geometry[i][1] *= -1.0; swap(scene.objects.back()->rigid[k].geometry[i][4], scene.objects.back()->rigid[k].geometry[i][5]); scene.objects.back()->rigid[k].geometry[i][4] *= -1.0; } swap(scene.objects.back()->bound[2], scene.objects.back()->bound[4]); swap(scene.objects.back()->bound[3], scene.objects.back()->bound[5]); scene.lights.back()->model = scene.objects.back(); } else if (num == 10) manipulator = manipulate::fovy; else if (num == 11) manipulator = manipulate::aspect; else if (num == 12) manipulator = manipulate::width; else if (num == 13) manipulator = manipulate::height; else if (num == 14) manipulator = manipulate::front; else if (num == 15) manipulator = manipulate::back; else if (num == 16) scene.render_cameras = !scene.render_cameras; else if (num == 17 && scene.active_camera_valid()) scene.cameras[scene.active_camera]->focus = NULL; else if (num == 18) { scene.cameras.push_back(new orthohdl()); scene.objects.push_back(new pyramidhdl(1.0, 1.0, 8)); for (int k = 0; k < scene.objects.back()->rigid.size(); k++) for (int i = 0; i < scene.objects.back()->rigid[k].geometry.size(); i++) { swap(scene.objects.back()->rigid[k].geometry[i][1], scene.objects.back()->rigid[k].geometry[i][2]); scene.objects.back()->rigid[k].geometry[i][1] *= -1.0; swap(scene.objects.back()->rigid[k].geometry[i][4], scene.objects.back()->rigid[k].geometry[i][5]); scene.objects.back()->rigid[k].geometry[i][4] *= -1.0; } swap(scene.objects.back()->bound[2], scene.objects.back()->bound[4]); swap(scene.objects.back()->bound[3], scene.objects.back()->bound[5]); scene.cameras.back()->model = scene.objects.back(); if (!scene.active_camera_valid()) { scene.active_camera = scene.cameras.size()-1; scene.cameras[scene.active_camera]->project(); } } else if (num == 19) { scene.cameras.push_back(new frustumhdl()); scene.objects.push_back(new pyramidhdl(1.0, 1.0, 8)); for (int k = 0; k < scene.objects.back()->rigid.size(); k++) for (int i = 0; i < scene.objects.back()->rigid[k].geometry.size(); i++) { swap(scene.objects.back()->rigid[k].geometry[i][1], scene.objects.back()->rigid[k].geometry[i][2]); scene.objects.back()->rigid[k].geometry[i][1] *= -1.0; swap(scene.objects.back()->rigid[k].geometry[i][4], scene.objects.back()->rigid[k].geometry[i][5]); scene.objects.back()->rigid[k].geometry[i][4] *= -1.0; } swap(scene.objects.back()->bound[2], scene.objects.back()->bound[4]); swap(scene.objects.back()->bound[3], scene.objects.back()->bound[5]); scene.cameras.back()->model = scene.objects.back(); if (!scene.active_camera_valid()) { scene.active_camera = scene.cameras.size()-1; scene.cameras[scene.active_camera]->project(); } } else if (num == 20) { scene.cameras.push_back(new perspectivehdl()); scene.objects.push_back(new pyramidhdl(1.0, 1.0, 8)); for (int k = 0; k < scene.objects.back()->rigid.size(); k++) for (int i = 0; i < scene.objects.back()->rigid[k].geometry.size(); i++) { swap(scene.objects.back()->rigid[k].geometry[i][1], scene.objects.back()->rigid[k].geometry[i][2]); scene.objects.back()->rigid[k].geometry[i][1] *= -1.0; swap(scene.objects.back()->rigid[k].geometry[i][4], scene.objects.back()->rigid[k].geometry[i][5]); scene.objects.back()->rigid[k].geometry[i][4] *= -1.0; } swap(scene.objects.back()->bound[2], scene.objects.back()->bound[4]); swap(scene.objects.back()->bound[3], scene.objects.back()->bound[5]); scene.cameras.back()->model = scene.objects.back(); if (!scene.active_camera_valid()) { scene.active_camera = scene.cameras.size()-1; scene.cameras[scene.active_camera]->project(); } } else if (num == 21) glPolygonMode(GL_FRONT_AND_BACK, GL_POINT); else if (num == 22) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else if (num == 23) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); else if (num == 28) glDisable(GL_CULL_FACE); else if (num == 29) { glEnable(GL_CULL_FACE); glCullFace(GL_BACK); } else if (num == 30) { glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); } else if (num == 31) scene.render_normals = scenehdl::none; else if (num == 32) scene.render_normals = scenehdl::face; else if (num == 33) scene.render_normals = scenehdl::vertex; glutPostRedisplay(); }
void motionfunc(int x, int y) { if (!bound && !menu) { int deltax = x - mousex; int deltay = mousey - y; mousex = x; mousey = y; vec3f direction; vec3f position; if (scene.active_camera_valid()) { if (scene.cameras[scene.active_camera]->type == "ortho") { GLdouble model[16]; GLdouble proj[16]; GLint view[4]; glGetDoublev(GL_MODELVIEW_MATRIX, model); glGetDoublev(GL_PROJECTION_MATRIX, proj); glGetIntegerv(GL_VIEWPORT, view); vec<double, 3> p; gluUnProject(x, height-y, 0.0, model, proj, view, &p[0], &p[1], &p[2]); position = p; direction = ror3(vec3f(0.0f, 0.0f, 1.0f), scene.cameras[scene.active_camera]->orientation); } else { GLdouble model[16]; GLdouble proj[16]; GLint view[4]; glGetDoublev(GL_MODELVIEW_MATRIX, model); glGetDoublev(GL_PROJECTION_MATRIX, proj); glGetIntegerv(GL_VIEWPORT, view); vec<double, 3> p; gluUnProject(x, height-y, 0.0, model, proj, view, &p[0], &p[1], &p[2]); position = scene.cameras[scene.active_camera]->position; direction = norm(p - position); } } if (scene.active_object_valid() && scene.active_camera_valid()) { if (manipulator == manipulate::translate) { float d = mag(scene.objects[scene.active_object]->position - position); scene.objects[scene.active_object]->position = d*direction + position; } else if (manipulator == manipulate::rotate) scene.objects[scene.active_object]->orientation += vec3f(-(float)deltay/100.0, (float)deltax/100.0, 0.0); else if (manipulator == manipulate::scale) scene.objects[scene.active_object]->scale += (float)deltay/100.0; else if (manipulator == manipulate::stepsize) { scene.objects[scene.active_object]->minstep += (float)deltay/100.0; if (scene.objects[scene.active_object]->minstep < 0.0) scene.objects[scene.active_object]->minstep = 0.0; } for (int i = 0; i < scene.cameras.size(); i++) if (scene.cameras[i]->model == scene.objects[scene.active_object]) { scene.cameras[i]->position = scene.objects[scene.active_object]->position; scene.cameras[i]->orientation = scene.objects[scene.active_object]->orientation; } } if (scene.active_camera_valid()) { if (manipulator == manipulate::fovy && scene.cameras[scene.active_camera]->type == "perspective") ((perspectivehdl*)scene.cameras[scene.active_camera])->fovy += (float)deltay/100.0; else if (manipulator == manipulate::aspect && scene.cameras[scene.active_camera]->type == "perspective") ((perspectivehdl*)scene.cameras[scene.active_camera])->aspect += (float)deltay/100.0; else if (manipulator == manipulate::width && scene.cameras[scene.active_camera]->type == "ortho") { ((orthohdl*)scene.cameras[scene.active_camera])->right += (float)deltay/200.0; ((orthohdl*)scene.cameras[scene.active_camera])->left -= (float)deltay/200.0; } else if (manipulator == manipulate::width && scene.cameras[scene.active_camera]->type == "frustum") { ((frustumhdl*)scene.cameras[scene.active_camera])->right += (float)deltay/200.0; ((frustumhdl*)scene.cameras[scene.active_camera])->left -= (float)deltay/200.0; } else if (manipulator == manipulate::height && scene.cameras[scene.active_camera]->type == "ortho") { ((orthohdl*)scene.cameras[scene.active_camera])->top += (float)deltay/200.0; ((orthohdl*)scene.cameras[scene.active_camera])->bottom -= (float)deltay/200.0; } else if (manipulator == manipulate::height && scene.cameras[scene.active_camera]->type == "frustum") { ((frustumhdl*)scene.cameras[scene.active_camera])->top += (float)deltay/200.0; ((frustumhdl*)scene.cameras[scene.active_camera])->bottom -= (float)deltay/200.0; } else if (manipulator == manipulate::front && scene.cameras[scene.active_camera]->type == "ortho") ((orthohdl*)scene.cameras[scene.active_camera])->front += (float)deltay/100.0; else if (manipulator == manipulate::front && scene.cameras[scene.active_camera]->type == "frustum") ((frustumhdl*)scene.cameras[scene.active_camera])->front += (float)deltay/100.0; else if (manipulator == manipulate::front && scene.cameras[scene.active_camera]->type == "perspective") ((perspectivehdl*)scene.cameras[scene.active_camera])->front += (float)deltay/100.0; else if (manipulator == manipulate::back && scene.cameras[scene.active_camera]->type == "ortho") ((orthohdl*)scene.cameras[scene.active_camera])->back += (float)deltay/100.0; else if (manipulator == manipulate::back && scene.cameras[scene.active_camera]->type == "frustum") ((frustumhdl*)scene.cameras[scene.active_camera])->back += (float)deltay/100.0; else if (manipulator == manipulate::back && scene.cameras[scene.active_camera]->type == "perspective") ((perspectivehdl*)scene.cameras[scene.active_camera])->back += (float)deltay/100.0; if (manipulator == manipulate::fovy || manipulator == manipulate::aspect || manipulator == manipulate::width || manipulator == manipulate::height || manipulator == manipulate::front || manipulator == manipulate::back) scene.cameras[scene.active_camera]->project(); } glutPostRedisplay(); } else if (!bound) { menu = false; pmotionfunc(x, y); } }
void pmotionfunc(int x, int y) { if (bound) { glutSetMenu(canvas_menu_id); int deltax = x - mousex; int deltay = y - mousey; mousex = x; mousey = y; bool set_warp = false; if (mousex > 3*width/4 || mousex < width/4) { mousex = width/2; set_warp = true; } if (mousey > 3*height/4 || mousey < height/4) { mousey = height/2; set_warp = true; } if (!set_warp) warp = false; if (scene.active_camera_valid() && !warp) { scene.cameras[scene.active_camera]->orientation[1] -= (float)deltax/500.0; scene.cameras[scene.active_camera]->orientation[0] -= (float)deltay/500.0; } if (set_warp) { glutWarpPointer(mousex, mousey); warp = true; } glutPostRedisplay(); } else if (scene.active_camera_valid()) { vec3f direction; vec3f position; if (scene.cameras[scene.active_camera]->type == "ortho") { GLdouble model[16]; GLdouble proj[16]; GLint view[4]; glGetDoublev(GL_MODELVIEW_MATRIX, model); glGetDoublev(GL_PROJECTION_MATRIX, proj); glGetIntegerv(GL_VIEWPORT, view); vec<double, 3> p; gluUnProject(x, height-y, 0.0f, model, proj, view, &p[0], &p[1], &p[2]); position = p; direction = ror3(vec3f(0.0f, 0.0f, 1.0f), scene.cameras[scene.active_camera]->orientation); } else { GLdouble model[16]; GLdouble proj[16]; GLint view[4]; glGetDoublev(GL_MODELVIEW_MATRIX, model); glGetDoublev(GL_PROJECTION_MATRIX, proj); glGetIntegerv(GL_VIEWPORT, view); vec<double, 3> p; gluUnProject(x, height-y, 0.0f, model, proj, view, &p[0], &p[1], &p[2]); position = scene.cameras[scene.active_camera]->position; direction = norm(p - position); } int old_active_object = scene.active_object; scene.active_object = -1; for (int i = 0; i < scene.objects.size(); i++) { if (scene.objects[i] != NULL && scene.cameras[scene.active_camera]->model != scene.objects[i]) { bool is_light = false; bool is_camera = false; for (int j = 0; j < scene.lights.size() && !is_light; j++) if (scene.lights[j] != NULL && scene.lights[j]->model == scene.objects[i]) is_light = true; for (int j = 0; j < scene.cameras.size() && !is_camera; j++) if (scene.cameras[j] != NULL && scene.cameras[j]->model == scene.objects[i]) is_camera = true; if ((!is_light && !is_camera) || (is_light && scene.render_lights) || (is_camera && scene.render_cameras)) { vec3f invdir = 1.0f/direction; vec3i sign((int)(invdir[0] < 0), (int)(invdir[1] < 0), (int)(invdir[2] < 0)); vec3f origin = position - scene.objects[i]->position; float tmin, tmax, tymin, tymax, tzmin, tzmax; tmin = (scene.objects[i]->bound[0 + sign[0]]*scene.objects[i]->scale - origin[0])*invdir[0]; tmax = (scene.objects[i]->bound[0 + 1-sign[0]]*scene.objects[i]->scale - origin[0])*invdir[0]; tymin = (scene.objects[i]->bound[2 + sign[1]]*scene.objects[i]->scale - origin[1])*invdir[1]; tymax = (scene.objects[i]->bound[2 + 1-sign[1]]*scene.objects[i]->scale - origin[1])*invdir[1]; if ((tmin <= tymax) && (tymin <= tmax)) { if (tymin > tmin) tmin = tymin; if (tymax < tmax) tmax = tymax; tzmin = (scene.objects[i]->bound[4 + sign[2]]*scene.objects[i]->scale - origin[2])*invdir[2]; tzmax = (scene.objects[i]->bound[4 + 1-sign[2]]*scene.objects[i]->scale - origin[2])*invdir[2]; if ((tmin <= tzmax) && (tzmin <= tmax)) { scene.active_object = i; i = scene.objects.size(); } } } } } if (scene.active_object != old_active_object) { bool is_light = false; bool is_camera = false; for (int i = 0; i < scene.lights.size() && !is_light; i++) if (scene.lights[i] != NULL && scene.active_object_valid() && scene.lights[i]->model == scene.objects[scene.active_object]) is_light = true; for (int i = 0; i < scene.cameras.size() && !is_camera; i++) if (scene.cameras[i] != NULL && scene.active_object_valid() && scene.cameras[i]->model == scene.objects[scene.active_object]) is_camera = true; glutDetachMenu(GLUT_RIGHT_BUTTON); if (scene.active_object == -1) glutSetMenu(canvas_menu_id); else if (is_light) glutSetMenu(light_menu_id); else if (is_camera) glutSetMenu(camera_menu_id); else glutSetMenu(object_menu_id); glutAttachMenu(GLUT_RIGHT_BUTTON); glutPostRedisplay(); } } }