#include "include.h" #include "globals.h" #include "geom.h" #include "geom_menu.h" #include "disp_menu.h" #include "image.h" #include "ppm.h" #include #include "params.h" void ClearBackground() { gstate.bg_sizeX = 0; gstate.bg_sizeY = 0; gstate.bg_sizeZ = 0; if (gstate.bg_data != NULL) delete[] gstate.bg_data; } int LoadBackground(char *file) { char extension[4]; int i, str_len, size, result; str_len = strlen(file); for(i = 0; i < 3; i++) { extension[i] = file[str_len-3+i]; } extension[3] = NULL; if (!strcmp(extension, "ppm")) { PpmImage ppm; if ((result = LoadPpmFile(file, &ppm)) != 1) { return result; } size = ppm.width * ppm.height * 3; gstate.bg_sizeX = ppm.width; gstate.bg_sizeY = ppm.height; gstate.bg_sizeZ = 3; if (!(gstate.bg_data = new unsigned char[size])) { printf("Couldn't allocate memory for background image pixel\n"); return -1; } memcpy(gstate.bg_data, ppm.data, size*sizeof(unsigned char)); delete[] ppm.data; } else if (!strcmp(extension, "rgb")) { TK_RGBImageRec *rgb; if (!(rgb = tkRGBImageLoad(file))) { return -1; } size = rgb->sizeX * rgb->sizeY * rgb->sizeZ; gstate.bg_sizeX = rgb->sizeX; gstate.bg_sizeY = rgb->sizeY; gstate.bg_sizeZ = rgb->sizeZ; if (!(gstate.bg_data = new unsigned char[size])) { printf("Couldn't allocate memory for background image pixel\n"); return -1; } memcpy(gstate.bg_data, rgb->data, size*sizeof(unsigned char)); delete[] rgb->data; delete rgb; } else { printf("Unrecognizable background format, can only be .ppm or .rgb\n"); return 0; } return 1; } int AutoRotateCB(struct Togl *togl, int argc, char *argv[]) { Tcl_Interp *interp = Togl_Interp(togl); /* error checking */ if (argc != 3) { Tcl_SetResult(interp, "wrong # args: should be \"pathName AutoRotate ?value?\"", TCL_STATIC); return TCL_ERROR; } idle_counter = 0; gstate.auto_rotate = atoi(argv[2]); Togl_PostRedisplay(togl); /* Let result string equal value */ strcpy(interp->result, argv[2]); return TCL_OK; } int AutoWobbleCB(struct Togl *togl, int argc, char *argv[]) { Tcl_Interp *interp = Togl_Interp(togl); /* error checking */ if (argc != 3) { Tcl_SetResult(interp, "wrong # args: should be \"pathName AutoWobble ?value?\"", TCL_STATIC); return TCL_ERROR; } idle_counter = 0; gstate.auto_wobble = atoi(argv[2]); Togl_PostRedisplay(togl); /* Let result string equal value */ strcpy(interp->result, argv[2]); return TCL_OK; } int WireFrameCB(struct Togl *togl, int argc, char *argv[]) { Tcl_Interp *interp = Togl_Interp(togl); /* error checking */ if (argc != 3) { Tcl_SetResult(interp, "wrong # args: should be \"pathName WireFrame ?value?\"", TCL_STATIC); return TCL_ERROR; } idle_counter = 0; gstate.wire_frame = atoi(argv[2]); if (gstate.wire_frame) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); Togl_PostRedisplay(togl); /* Let result string equal value */ strcpy(interp->result, argv[2]); return TCL_OK; } int TexMapCB(struct Togl *togl, int argc, char *argv[]) { Tcl_Interp *interp = Togl_Interp(togl); static int first = 1; /* error checking */ if (argc != 3) { Tcl_SetResult(interp, "wrong # args: should be \"pathName TexMap ?value?\"", TCL_STATIC); return TCL_ERROR; } idle_counter = 0; gstate.texmap = atoi(argv[2]); if (gstate.texmap) { glEnable(GL_TEXTURE_2D); } else { glDisable(GL_TEXTURE_2D); } glDeleteLists(1, 1); glNewList(1, GL_COMPILE); gstate.num_polygons = geometry_display_list(); glEndList(); Togl_PostRedisplay(togl); /* Let result string equal value */ strcpy(interp->result, argv[2]); return TCL_OK; } int EnvMapCB(struct Togl *togl, int argc, char *argv[]) { Tcl_Interp *interp = Togl_Interp(togl); /* error checking */ if (argc != 3) { Tcl_SetResult(interp, "wrong # args: should be \"pathName EnvMap ?value?\"", TCL_STATIC); return TCL_ERROR; } idle_counter = 0; gstate.envmap = atoi(argv[2]); glDeleteLists(1, 1); glNewList(1, GL_COMPILE); gstate.num_polygons = geometry_display_list(); glEndList(); Togl_PostRedisplay(togl); /* Let result string equal value */ strcpy(interp->result, argv[2]); return TCL_OK; } int BackgroundCB(struct Togl *togl, int argc, char *argv[]) { Tcl_Interp *interp = Togl_Interp(togl); /* error checking */ if (argc != 3) { Tcl_SetResult(interp, "wrong # args: should be \"pathName BackgroundColor ?value?\"", TCL_STATIC); return TCL_ERROR; } idle_counter = 0; gstate.bg_on = atoi(argv[2]); Togl_PostRedisplay(togl); /* Let result string equal value */ strcpy(interp->result, argv[2]); return TCL_OK; } int BackgroundColorCB(struct Togl *togl, int argc, char *argv[]) { Tcl_Interp *interp = Togl_Interp(togl); /* error checking */ if (argc != 5) { Tcl_SetResult(interp, "wrong # args: should be \"pathName BackgroundColor ?r? ?g? ?b?\"", TCL_STATIC); return TCL_ERROR; } idle_counter = 0; gstate.bg_r = atoi(argv[2]); gstate.bg_g = atoi(argv[3]); gstate.bg_b = atoi(argv[4]); Togl_PostRedisplay(togl); /* Let result string equal value */ strcpy(interp->result, argv[2]); return TCL_OK; } int MaterialColorCB(struct Togl *togl, int argc, char *argv[]) { Tcl_Interp *interp = Togl_Interp(togl); int r, g, b; /* error checking */ if (argc < 6) { Tcl_SetResult(interp, "wrong # args: should be \"pathName MaterialColor ?r? ?g? ?b? ?#...?\"", TCL_STATIC); return TCL_ERROR; } idle_counter = 0; r = atoi(argv[2]); g = atoi(argv[3]); b = atoi(argv[4]); if (argc == 6) { int which; which = atoi(argv[5]); if ((which >= 0) && (which < 6)) { scherk_mat[SCHERK_FIRST_MATERIAL+which][R] = r; scherk_mat[SCHERK_FIRST_MATERIAL+which][G] = g; scherk_mat[SCHERK_FIRST_MATERIAL+which][B] = b; scherk_material(SCHERK_FIRST_MATERIAL+which, r, g, b); } else { Tcl_SetResult( interp, "unknown face/rim", TCL_STATIC); return TCL_ERROR; } } else if (argc > 6) { for (int i = 0; i < (argc-5); i++) { if (atoi(argv[i+5]) == 1) { scherk_mat[SCHERK_FIRST_MATERIAL+i][R] = r; scherk_mat[SCHERK_FIRST_MATERIAL+i][G] = g; scherk_mat[SCHERK_FIRST_MATERIAL+i][B] = b; scherk_material(SCHERK_FIRST_MATERIAL+i, r, g, b); } } } glDeleteLists(1, 1); glNewList(1, GL_COMPILE); gstate.num_polygons = geometry_display_list(); glEndList(); Togl_PostRedisplay(togl); /* Let result string equal value */ strcpy(interp->result, argv[2]); return TCL_OK; } int TextureFileCB(struct Togl *togl, int argc, char *argv[]) { Tcl_Interp *interp = Togl_Interp(togl); int i, str_len; /* error checking */ if (argc != 3) { Tcl_SetResult(interp, "wrong # args: should be \"pathName TextureFile ?filename?\"", TCL_STATIC); return TCL_ERROR; } idle_counter = 0; delete[] gstate.tex_file; str_len = strlen(argv[2]); gstate.tex_file = new char[str_len + 1]; strcpy(gstate.tex_file, argv[2]); clear_all_texture(); for (i=SCHERK_FIRST_TEXTURE;iresult, argv[2]); return TCL_OK; } int BackgroundFileCB(struct Togl *togl, int argc, char *argv[]) { Tcl_Interp *interp = Togl_Interp(togl); int str_len; /* error checking */ if (argc != 3) { Tcl_SetResult(interp, "wrong # args: should be \"pathName BackgroundFile ?filename?\"", TCL_STATIC); return TCL_ERROR; } idle_counter = 0; delete[] gstate.bg_file; str_len = strlen(argv[2]); gstate.bg_file = new char[str_len + 1]; strcpy(gstate.bg_file, argv[2]); ClearBackground(); if (LoadBackground(gstate.bg_file) == -1) { exit(0xdeadbeef); } if (gstate.bg_on) { Togl_PostRedisplay(togl); } /* Let result string equal value */ strcpy(interp->result, argv[2]); return TCL_OK; } int StartOverCB(struct Togl *togl, int argc, char *argv[]) { Tcl_Interp *interp = Togl_Interp(togl); int i; /* error checking */ if (argc != 2) { Tcl_SetResult(interp, "wrong # args: should be \"pathName StartOver\"", TCL_STATIC); return TCL_ERROR; } idle_counter = 0; scherk_branches = 2; scherk_storeys = 2; scherk_height = 1.5f; scherk_flange = 1.5f; scherk_thickness = 0.15f; scherk_rim_bulge = 1.5f; scherk_twist = 0.0f; scherk_warp = 0.0f; scherk_azimuth = 0.0f; scherk_tex_tiles = 1; scherk_detail = 5; if (gstate.texmap) glDisable(GL_TEXTURE_2D); if (gstate.wire_frame) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); gstate.auto_rotate = 0; gstate.auto_wobble = 0; gstate.wire_frame = 0; gstate.texmap = 0; gstate.envmap = 0; gstate.bg_on = 0; gstate.bg_r = gstate.bg_g = gstate.bg_b = 0; for (i=SCHERK_FIRST_MATERIAL; iresult, argv[1]); return TCL_OK; } int SaveSpecCB(struct Togl *togl, int argc, char *argv[]) { Tcl_Interp *interp = Togl_Interp(togl); int save_result; char str[256]; /* error checking */ if (argc != 3) { Tcl_SetResult(interp, "wrong # args: should be \"pathName SaveSpec ?filename?\"", TCL_STATIC); return TCL_ERROR; } idle_counter = 0; save_result = SaveParams(argv[2]); if (save_result == 0) { sprintf(str, " Cannot open file \"%s\" for save", argv[2]); Tcl_SetResult(interp, str, TCL_STATIC); return TCL_ERROR; } else if (save_result == -1) { sprintf(str, " Cannot save file \"%s\"", argv[2]); Tcl_SetResult(interp, str, TCL_STATIC); return TCL_ERROR; } /* Let result string equal value */ strcpy(interp->result, argv[2]); return TCL_OK; } int LoadSpecCB(struct Togl *togl, int argc, char *argv[]) { Tcl_Interp *interp = Togl_Interp(togl); int load_result; char str[256]; /* error checking */ if (argc != 3) { Tcl_SetResult(interp, "wrong # args: should be \"pathName LoadSpec ?filename?\"", TCL_STATIC); return TCL_ERROR; } idle_counter = 0; load_result = LoadParams(argv[2]); if (load_result == 0) { sprintf(str, " Cannot open file \"%s\" for save", argv[2]); Tcl_SetResult(interp, str, TCL_STATIC); ProccessParams(togl); return TCL_ERROR; } else if (load_result == -1) { sprintf(str, " Cannot load all parameters in \"%s\"", argv[2]); Tcl_SetResult(interp, str, TCL_STATIC); ProccessParams(togl); return TCL_ERROR; } ProccessParams(togl); Togl_PostRedisplay(togl); /* Let result string equal value */ strcpy(interp->result, argv[2]); return TCL_OK; }