//=========================================================================== // SISL - SINTEF Spline Library, version 4.5.0. // Definition and interrogation of NURBS curves and surfaces. // // Copyright (C) 2000-2005, 2010 SINTEF ICT, Applied Mathematics, Norway. // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation version 2 of the License. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., // 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. // // Contact information: E-mail: tor.dokken@sintef.no // SINTEF ICT, Department of Applied Mathematics, // P.O. Box 124 Blindern, // 0314 Oslo, Norway. // // Other licenses are also available for this software, notably licenses // for: // - Building commercial software. // - Building software whose source code you wish to keep private. //=========================================================================== 00029 #ifndef GL_AUX_INCLUDED 00030 #define GL_AUX_INCLUDED 00031 00032 #include <stdio.h> 00033 #include <GL/glut.h> 00034 #include <algorithm> 00035 00036 #include "transfutils.h" 00037 #include "jonvec.h" 00038 00039 extern const int predefined_colours; 00040 extern const double predef_col_table[]; 00041 extern const double col_setting_back_old[]; 00042 extern const double col_setting_back[]; 00043 00044 00045 // 00046 // 020723: I don't remember when the stuff above is used... May be that the 00047 // class below could/should replace them... 00048 // 00049 // This class is used in this way: The glut application should make a 00050 // (global) instance of this class, and call it's 'Key' member from 00051 // the 'Key' callback function. The "multi purpose standard 00052 // Key-callback" defined in xxxxx does just this. 00053 // 00054 // 020822: This crap should be fixed, updated, cleaned up. 00055 // 00056 00057 class material_appearance 00058 { 00059 private: 00060 #ifndef MICROSOFT 00061 static const int schemes=7; 00062 #else 00063 // 00064 // @@@ 021027: Remember to ask afr about this... 00065 // 00066 # define schemes 7 00067 #endif 00068 00069 int cubemap_mode, col_scheme; 00070 float front_mat_shininess[schemes]; 00071 float front_mat_specular[4*schemes]; 00072 float front_mat_diffuse[4*schemes]; 00073 float front_mat_ambient[4*schemes]; 00074 float front_mat_emission[4*schemes]; 00075 float back_mat_shininess[schemes]; 00076 float back_mat_specular[4*schemes]; 00077 float back_mat_diffuse[4*schemes]; 00078 float back_mat_ambient[4*schemes]; 00079 float back_mat_emission[4*schemes]; 00080 int spec_mode; 00081 int col_face; // State variable for chosen face to modify. 00082 int col_param; // State variable for chosen param to modify. 00083 float *property; // Property data to modify. 00084 public: 00085 double ztrans_delta; 00086 private: 00087 int rescale, bg_invert; 00088 00089 public: 00090 material_appearance(void): 00091 cubemap_mode(0), col_scheme(0), spec_mode(0), col_face(0), col_param(0), 00092 property(front_mat_specular), ztrans_delta(0.0), rescale(0), bg_invert(0) 00093 { 00094 static const float front_mat_shininess0[schemes] ={200.0, 00095 200.0, 00096 200.0, 00097 200.0, 00098 200.0, 00099 200.0, 00100 200.0}; 00101 static const float front_mat_specular0[4*schemes]={0.8, 0.8, 0.8, 1.0, 00102 0.8, 0.8, 0.8, 1.0, 00103 0.8, 0.8, 0.8, 1.0, 00104 0.8, 0.8, 0.8, 1.0, 00105 0.8, 0.8, 0.8, 1.0, 00106 0.8, 0.8, 0.8, 1.0, 00107 0.8, 0.8, 0.8, 1.0}; 00108 static const float front_mat_diffuse0[4*schemes] ={0.7, 0.7, 0.7, 1.0, 00109 0.7, 0.4, 0.4, 1.0, 00110 0.4, 0.7, 0.7, 1.0, 00111 0.0, 0.8, 0.2, 1.0, 00112 0.7, 0.7, 0.4, 1.0, 00113 0.4, 0.4, 0.7, 1.0, 00114 0.5, 0.4, 0.4, 1.0}; 00115 static const float front_mat_ambient0[4*schemes] ={0.3, 0.3, 0.3, 1.0, 00116 0.8, 0.8, 0.3, 1.0, 00117 0.3, 0.3, 0.8, 1.0, 00118 0.5, 0.0, 0.0, 1.0, 00119 0.8, 0.3, 0.8, 1.0, 00120 0.3, 0.8, 0.3, 1.0, 00121 0.0, 0.0, 0.0, 1.0}; 00122 static const float front_mat_emission0[4*schemes]={0.3, 0.0, 0.0, 1.0, 00123 0.3, 0.2, 0.0, 1.0, 00124 0.0, 0.2, 0.0, 1.0, 00125 0.3, 0.2, 0.3, 1.0, 00126 0.1, 0.2, 0.1, 1.0, 00127 0.0, 0.2, 0.4, 1.0, 00128 0.0, 0.0, 0.0, 1.0}; 00129 static const float back_mat_shininess0[schemes] ={200.0, 00130 200.0, 00131 228.0, 00132 228.0, 00133 228.0, 00134 228.0, 00135 228.0}; 00136 static const float back_mat_specular0[4*schemes] ={0.8, 0.0, 0.0, 1.0, 00137 0.0, 0.0, 0.0, 1.0, 00138 0.8, 0.9, 0.0, 1.0, 00139 0.8, 0.9, 0.0, 1.0, 00140 0.8, 0.9, 0.0, 1.0, 00141 0.8, 0.9, 0.0, 1.0, 00142 0.8, 0.9, 0.0, 1.0}; 00143 static const float back_mat_diffuse0[4*schemes] ={0.7, 0.7, 0.7, 1.0, 00144 0.6, 0.9, 0.6, 1.0, 00145 0.0, 0.7, 0.9, 1.0, 00146 0.8, 0.9, 0.0, 1.0, 00147 0.8, 0.9, 0.0, 1.0, 00148 0.9, 0.7, 0.9, 1.0, 00149 0.4, 0.4, 0.4, 1.0}; 00150 static const float back_mat_ambient0[4*schemes] ={0.3, 0.3, 0.3, 1.0, 00151 0.9, 0.3, 0.3, 1.0, 00152 0.3, 0.9, 0.3, 1.0, 00153 0.8, 0.9, 0.0, 1.0, 00154 0.8, 0.9, 0.0, 1.0, 00155 0.9, 0.9, 0.3, 1.0, 00156 0.0, 0.0, 0.0, 1.0}; 00157 static const float back_mat_emission0[4*schemes] ={0.0, 0.3, 0.0, 1.0, 00158 0.9, 0.3, 0.0, 1.0, 00159 0.0, 0.9, 0.9, 1.0, 00160 0.8, 0.9, 0.0, 1.0, 00161 0.8, 0.9, 0.0, 1.0, 00162 0.9, 0.0, 0.9, 1.0, 00163 0.0, 0.0, 0.0, 1.0}; 00164 00165 memcpy(front_mat_shininess, front_mat_shininess0, schemes*sizeof(float)); 00166 memcpy(front_mat_specular, front_mat_specular0, 4*schemes*sizeof(float)); 00167 memcpy(front_mat_diffuse, front_mat_diffuse0, 4*schemes*sizeof(float)); 00168 memcpy(front_mat_ambient, front_mat_ambient0, 4*schemes*sizeof(float)); 00169 memcpy(front_mat_emission, front_mat_emission0, 4*schemes*sizeof(float)); 00170 memcpy(back_mat_shininess, back_mat_shininess0, 4*schemes*sizeof(float)); 00171 memcpy(back_mat_specular, back_mat_specular0, 4*schemes*sizeof(float)); 00172 memcpy(back_mat_diffuse, back_mat_diffuse0, 4*schemes*sizeof(float)); 00173 memcpy(back_mat_ambient, back_mat_ambient0, 4*schemes*sizeof(float)); 00174 memcpy(back_mat_emission, back_mat_emission0, 4*schemes*sizeof(float)); 00175 } 00176 00177 int col_scheme_selected(void) const { return col_scheme; } 00178 void col_scheme_select(const int i) 00179 { 00180 col_scheme=i%schemes; 00181 } 00182 00183 void set_material(void) 00184 { 00185 glMaterialfv(GL_FRONT, GL_SHININESS, front_mat_shininess+col_scheme); 00186 glMaterialfv(GL_FRONT, GL_SPECULAR, front_mat_specular+col_scheme*4); 00187 glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse+col_scheme*4); 00188 glMaterialfv(GL_FRONT, GL_AMBIENT, front_mat_ambient+col_scheme*4); 00189 glMaterialfv(GL_FRONT, GL_EMISSION, front_mat_emission+col_scheme*4); 00190 00191 glMaterialfv(GL_BACK, GL_SHININESS, back_mat_shininess+col_scheme); 00192 glMaterialfv(GL_BACK, GL_SPECULAR, back_mat_specular+col_scheme*4); 00193 glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse+col_scheme*4); 00194 glMaterialfv(GL_BACK, GL_AMBIENT, back_mat_ambient+col_scheme*4); 00195 glMaterialfv(GL_BACK, GL_EMISSION, back_mat_emission+col_scheme*4); 00196 } 00197 00198 void redefine_material_f(const vector3t<double> &diff, 00199 const vector3t<double> &amb, 00200 const vector3t<double> &em, 00201 const vector3t<double> &spec, 00202 const double shin) 00203 { 00204 front_mat_diffuse[col_scheme*4+0]=diff.x(); 00205 front_mat_diffuse[col_scheme*4+1]=diff.y(); 00206 front_mat_diffuse[col_scheme*4+2]=diff.z(); 00207 front_mat_diffuse[col_scheme*4+3]=1.0; 00208 00209 front_mat_ambient[col_scheme*4+0]=amb.x(); 00210 front_mat_ambient[col_scheme*4+1]=amb.y(); 00211 front_mat_ambient[col_scheme*4+2]=amb.z(); 00212 front_mat_ambient[col_scheme*4+3]=1.0; 00213 00214 front_mat_emission[col_scheme*4+0]=em.x(); 00215 front_mat_emission[col_scheme*4+1]=em.y(); 00216 front_mat_emission[col_scheme*4+2]=em.z(); 00217 front_mat_emission[col_scheme*4+3]=1.0; 00218 00219 front_mat_specular[col_scheme*4+0]=spec.x(); 00220 front_mat_specular[col_scheme*4+1]=spec.y(); 00221 front_mat_specular[col_scheme*4+2]=spec.z(); 00222 front_mat_specular[col_scheme*4+3]=1.0; 00223 00224 front_mat_shininess[col_scheme]=shin; 00225 } 00226 00227 void Key(unsigned char key) 00228 { 00229 bool adjust_material=false; 00230 int comp=0; // Colour component to modify. 00231 double delta=0.0; // Amount of modification. 00232 00233 switch (key) 00234 { 00235 // case 'm': // @H@ ------ Material and lighting properties prefix: 00236 00237 case 's': // @H@ Toggle on/off specular property of material. 00238 spec_mode=1-spec_mode; 00239 printf("spec_mode=%d\n", spec_mode); 00240 { 00241 int i; 00242 00243 for (i=0; i<3; i++) 00244 if (spec_mode) 00245 front_mat_specular[4*schemes+i]= 00246 back_mat_specular[4*schemes+i]=1.0; 00247 else 00248 front_mat_specular[4*schemes+i]= 00249 back_mat_specular[4*schemes+i]=0.0; 00250 } 00251 glMaterialfv(GL_FRONT, GL_SPECULAR, 00252 front_mat_specular+4*col_scheme); 00253 glMaterialfv(GL_BACK, GL_SPECULAR, 00254 back_mat_specular+4*col_scheme); 00255 //material_prefix_pressed=false; 00256 break; 00257 00258 case 'c': // @H@ Cycle through color schemes. 00259 col_scheme=(col_scheme+1)%schemes; 00260 printf("col_scheme=%d\n", col_scheme); 00261 adjust_material=true; 00262 //material_prefix_pressed=false; 00263 break; 00264 00265 case 'b': // @H@ Toggle which face to adjust colour for. 00266 col_face=1-col_face; 00267 printf("col_face=%s\n", col_face ? "back" : "front"); 00268 break; 00269 00270 case 'p': // @H@ Cycle through colour parameter to tune. 00271 col_param=(col_param+1)%5; 00272 printf("Colour parameter to be tuned: '"); 00273 switch (col_param) 00274 { 00275 case 0: 00276 printf("shininess"); 00277 property=col_face ? back_mat_shininess : front_mat_shininess; 00278 break; 00279 case 1: 00280 printf("specular"); 00281 property=col_face ? back_mat_specular : front_mat_specular; 00282 break; 00283 case 2: 00284 printf("diffuse"); 00285 property=col_face ? back_mat_diffuse : front_mat_diffuse; 00286 break; 00287 case 3: 00288 printf("ambient"); 00289 property=col_face ? back_mat_ambient : front_mat_ambient; 00290 break; 00291 case 4: 00292 printf("emission"); 00293 property=col_face ? back_mat_emission : front_mat_emission; 00294 break; 00295 } 00296 printf("'.\n"); 00297 break; 00298 00299 case 'r': // @H@ Increase red-comp. of current colour. 00300 comp=0; adjust_material=true; delta=1.0/16.0; break; 00301 case 'f': // @H@ Decrease red-comp. of current colour. 00302 comp=0; adjust_material=true; delta=-1.0/16.0; break; 00303 case 't': // @H@ Increase green-comp. of current colour. 00304 comp=1; adjust_material=true; delta=1.0/16.0; break; 00305 case 'g': // @H@ Decrease green-comp. of current colour. 00306 comp=1; adjust_material=true; delta=-1.0/16.0; break; 00307 case 'y': // @H@ Increase blue-comp. of current colour. 00308 comp=2; adjust_material=true; delta=1.0/16.0; break; 00309 case 'h': // @H@ Decrease blue-comp. of current colour. 00310 comp=2; adjust_material=true; delta=-1.0/16.0; break; 00311 00312 case 'H': // @H@ Help for the 'material property' mode. 00313 00314 00315 puts("\nUse the following keys:\n"); 00316 // system("cat "PWD"/"__FILE__" | " 00317 // "gawk '/[c]ase.*@H@/ { match($0, " 00318 // "'\"/[c]ase.*'.*'/\"'); printf(\"%c) \", substr($0, " 00319 // "RSTART+RLENGTH-2, 1)); match($0, /@H@/); print(substr($0, " 00320 // "RSTART+RLENGTH+1)) } /@[H]H@/ { match($0, /@[H]H@/); " 00321 // "print(substr($0, RSTART+RLENGTH+1)) }'"); 00322 00323 00324 puts("\nSpecific help for material property selection:\n" 00325 " Note that the following keys do not exit 'material' mode\n" 00326 " after their application. (The key 'm' does that.) Use:\n" 00327 " b - for toggling of face, 'back'/'front',\n" 00328 " p - for cycling through allowed properties,\n" 00329 " 'shininess', 'specular', 'diffuse', 'ambient' and\n" 00330 " 'emission',\n" 00331 " r - for increasing the red-component,\n" 00332 " f - for decreasing the red-component,\n" 00333 " t - for increasing the green-component,\n" 00334 " g - for decreasing the green-component,\n" 00335 " y - for increasing the blue-component and\n" 00336 " h - for decreasing the blue-component.\n" 00337 " Also, these are available:\n" 00338 " i - invert background," 00339 " d - dump numbers.\n"); 00340 break; 00341 00342 case 'i': // @H@ Invert background colour. 00343 bg_invert=1-bg_invert; 00344 printf("bg_invert=%d\n", bg_invert); 00345 if (bg_invert) 00346 glClearColor(1.0, 1.0, 1.0, 0.0); 00347 else 00348 glClearColor(0.0, 0.0, 0.0, 0.0); 00349 break; 00350 00351 case 'd': 00352 { 00353 int i; 00354 00355 puts("\nThe numbers for front properties, listed in the order of\n" 00356 "shininess, specular, diffuse, ambient and emmisive light,\n" 00357 "is:\n"); 00358 printf("%.5f\n", *(front_mat_shininess+col_scheme)); 00359 for (i=0; i<4; i++) 00360 printf("%.5f ", (front_mat_specular+col_scheme*4)[i]); 00361 printf("\n"); 00362 for (i=0; i<4; i++) 00363 printf("%.5f ", (front_mat_diffuse+col_scheme*4)[i]); 00364 printf("\n"); 00365 for (i=0; i<4; i++) 00366 printf("%.5f ", (front_mat_ambient+col_scheme*4)[i]); 00367 printf("\n"); 00368 for (i=0; i<4; i++) 00369 printf("%.5f ", (front_mat_emission+col_scheme*4)[i]); 00370 puts("\n\nThe numbers for back properties, listed in the order of\n" 00371 "shininess, specular, diffuse, ambient and emmisive light,\n" 00372 "is:\n"); 00373 printf("%.5f\n", *(back_mat_shininess+col_scheme)); 00374 for (i=0; i<4; i++) 00375 printf("%.5f ", (back_mat_specular+col_scheme*4)[i]); 00376 printf("\n"); 00377 for (i=0; i<4; i++) 00378 printf("%.5f ", (back_mat_diffuse+col_scheme*4)[i]); 00379 printf("\n"); 00380 for (i=0; i<4; i++) 00381 printf("%.5f ", (back_mat_ambient+col_scheme*4)[i]); 00382 printf("\n"); 00383 for (i=0; i<4; i++) 00384 printf("%.5f ", (back_mat_emission+col_scheme*4)[i]); 00385 printf("\n\n"); 00386 } 00387 break; 00388 00389 case 'z': // @H@ Reduce the amount of 'perspective'. 00390 if (ztrans_delta==0.0) 00391 ztrans_delta=0.01; 00392 ztrans_delta=std::min(ztrans_delta*1.1, 1000.0); 00393 ztrans=-6-ztrans_delta; 00394 printf("ztrans_delta=%f\n", ztrans_delta); 00395 printf("ztrans=%f\n", ztrans); 00396 glMatrixMode(GL_PROJECTION); 00397 glLoadIdentity(); 00398 glFrustum(-1.0, 1.0, -1.0, 1.0, 5+ztrans_delta, 20+ztrans_delta); 00399 glMatrixMode(GL_MODELVIEW); 00400 glLoadIdentity(); 00401 printf("trans=%f %f %f\n", xtrans, ytrans, ztrans); 00402 glTranslated(xtrans, ytrans, ztrans); 00403 printf("scale=%f %f %f\n", xscale, yscale, zscale); 00404 glScaled(xscale, yscale, zscale); 00405 break; 00406 case 'x': // @H@ Increase the amount of 'perspective'. 00407 ztrans_delta*=0.9; 00408 ztrans=-6-ztrans_delta; 00409 printf("ztrans_delta=%f\n", ztrans_delta); 00410 printf("ztrans=%f\n", ztrans); 00411 glMatrixMode(GL_PROJECTION); 00412 glLoadIdentity(); 00413 glFrustum(-1.0, 1.0, -1.0, 1.0, 5+ztrans_delta, 20+ztrans_delta); 00414 glMatrixMode(GL_MODELVIEW); 00415 glLoadIdentity(); 00416 glTranslated(xtrans, ytrans, ztrans); 00417 glScaled(xscale, yscale, zscale); 00418 break; 00419 00420 case 'm': // @H@ ------ Material prefix section ends here. 00421 //material_prefix_pressed=false; 00422 break; 00423 00424 case 'q': // @H@ Exit the program. 00425 exit(0); 00426 break; 00427 00428 } 00429 00430 if (adjust_material) 00431 { 00432 float * const &tmp=(property+ 00433 col_scheme*(col_param==0 ? 1 : 4)+ 00434 (col_param==0 ? 0 : comp)); 00435 printf("Old value %.4f replaced by new value ", *tmp); 00436 if (col_param==0) 00437 *tmp=std::min(std::max((*tmp)*(1+delta), 0.0), 1000.0); 00438 else 00439 *tmp=std::min(std::max(*tmp+delta, 0.0), 1.0); 00440 printf("%.4f\n", *tmp); 00441 00442 set_material(); 00443 } 00444 00445 } // end of 'Key' 00446 00447 }; 00448 00449 00450 00451 00452 00453 00454 void draw_cylinder(double x0, double y0, double z0, 00455 double x1, double y1, double z1, 00456 double radius, double radius2, int n); 00457 00458 void draw_gl_axes_old(int n=10, double r=0.7, double radius=0.01, 00459 double rim=0.04, double l=0.1); 00460 00461 void draw_grid(const int n1, const int n2, const int n3); 00462 00463 void draw_grid_planes(const int n1, const int n2, const int n3); 00464 00465 void gl_init(int argc, char *argv[], 00466 const int xs, const int ys, 00467 const int x0, const int y0, 00468 const int doubleBuffer, 00469 const int two_sided, 00470 const int lighting, 00471 const int normalize, 00472 const int smooth, 00473 const double xtrans, const double ytrans, const double ztrans, 00474 const double xscale, const double yscale, const double zscale, 00475 int &tx, 00476 int &ty, 00477 const int texture_mode, 00478 const char * const texfile=NULL); 00479 00480 void print_gl_matrix(const int m); 00481 00482 void reshape_window(int width, int height); 00483 00484 // 00485 // Writing and reading the GL transformation matrices to and from 00486 // files. 00487 // 020505: Copied from the sisl_view version of gl_aux... Here is some 00488 // more merging to do... 00489 // 00490 void write_gl_matrices(FILE *f); 00491 void read_gl_matrices(FILE *f); 00492 00493 00494 00495 00496 00497 00498 #endif
1.6.3