2 * $Logfile: /Freespace2/code/nebedit/NebEdit.cpp $
7 * Program to edit nebulas in 2d
10 * Revision 1.1 2002/05/03 03:28:10 root
14 * 7 7/15/99 3:07p Dave
15 * 32 bit detection support. Mouse coord commandline.
17 * 6 5/19/99 4:07p Dave
18 * Moved versioning code into a nice isolated common place. Fixed up
19 * updating code on the pxo screen. Fixed several stub problems.
21 * 5 1/08/99 2:06p Dave
22 * Fixed pfoview for software mode.
24 * 4 12/18/98 1:14a Dave
25 * Rough 1024x768 support for Direct3D. Proper detection and usage through
28 * 3 11/13/98 2:32p Dave
29 * Improved commandline parsing for exe pathname.
31 * 2 10/24/98 9:51p Dave
33 * 1 10/24/98 9:39p Dave
35 * 13 4/13/98 10:11a John
36 * Made timer functions thread safe. Made timer_init be called in all
39 * 12 3/23/98 1:35p Sandeep
41 * 11 3/05/98 11:15p Hoffoss
42 * Changed non-game key checking to use game_check_key() instead of
45 * 10 12/29/97 5:10p Allender
46 * fixed problems with speed not being reported properly in multiplayer
47 * games. Made read_flying_controls take frametime as a parameter. More
48 * ship/weapon select stuff
50 * 9 12/04/97 3:47p John
51 * Made joystick move mouse cursor
53 * 8 11/21/97 11:32a John
54 * Added nebulas. Fixed some warpout bugs.
56 * 7 11/19/97 10:15p Adam
57 * upped maxtris to 200
59 * 6 11/19/97 4:28p Sandeep
60 * added poly/vert counter
62 * 5 11/19/97 1:59p Sandeep
63 * Added multiple vertex editing mode
65 * 4 11/16/97 2:29p John
66 * added versioning to nebulas; put nebula code into freespace.
68 * 3 11/13/97 12:04p Sandeep
69 * Added face editing support, deletion support, and a saving and loading
72 * 2 11/10/97 9:59p John
75 * 1 11/10/97 9:42p John
114 physics_info ViewerPhysics;
115 float ViewerZoom = 1.0f;
118 int Fred_running = 0;
119 int Pofview_running = 0;
120 float flFrametime = 0.0f;
126 vector Global_light_world = { 0.208758f, -0.688253f, -0.694782f };
133 #define MAX_POINTS 300
135 int neb_w = 0, neb_h = 0;
137 int nebula_inited = 0;
140 int x[MAX_POINTS], y[MAX_POINTS], l[MAX_POINTS];
141 float scale_factor = 1.0f;
144 int tri[MAX_TRIS][3];
148 int Mouse_x, Mouse_y;
150 BOOL Selected[MAX_POINTS];
151 BOOL Sel_mode = 0; // 0 = 1 point at a time, 1 = select multiple points
154 int View_mode = 0; // 0 = 2d editor, 1 = 3d viewer
156 int Vert_mode = 0; // 0 = Move vertices/Add vertices, 2 = Move face/Add face
158 int Which_vert = 0; // Current vertex of the faceadd
164 BOOL Draw_sel_box = FALSE;
168 int Nebedit_running = 1;
170 extern int load_nebula_sub(char*);
171 extern void project_2d_onto_sphere(vector *, float, float);
173 void create_default_neb()
178 num_pts = neb_w*neb_h;
180 for (j=0; j<neb_h; j++ ) {
181 for (i=0; i<neb_w; i++ ) {
182 x[i+j*neb_w] = ((i+1)*SCREEN_W)/(neb_w+2);
183 y[i+j*neb_w] = ((j+3)*SCREEN_H)/(neb_h+6);
184 if ( (j==0) || (j==neb_h-1)) {
194 for (j=0; j<neb_h-1; j++ ) {
195 for (i=0; i<neb_w-1; i++ ) {
196 tri[num_tris][0] = i+neb_w*j;
197 tri[num_tris][1] = i+neb_w*j+1;
198 tri[num_tris][2] = i+neb_w*(j+1)+1;
201 tri[num_tris][0] = i+neb_w*j;
202 tri[num_tris][1] = i+neb_w*(j+1)+1;
203 tri[num_tris][2] = i+neb_w*(j+1);
211 #define NEBULA_FILE_ID "NEBU"
212 #define NEBULA_MAJOR_VERSION 1 // Can be 1-?
213 #define NEBULA_MINOR_VERSION 0 // Can be 0-99
215 void save_nebula_sub(char *filename)
221 fp = fopen(filename, "wb");
224 fwrite( "NEBU", 4, 1, fp );
225 version = NEBULA_MAJOR_VERSION*100+NEBULA_MINOR_VERSION;
226 fwrite( &version, sizeof(int), 1, fp );
227 fwrite( &num_pts, sizeof(int), 1, fp );
228 fwrite( &num_tris, sizeof(int), 1, fp );
230 for (int i=0; i<num_pts; i++ ) {
231 xf = float(x[i])/640.0f;
232 yf = float(y[i])/480.0f;
233 fwrite( &xf, sizeof(float), 1, fp );
234 fwrite( &yf, sizeof(float), 1, fp );
235 fwrite( &l[i], sizeof(int), 1, fp );
238 for (i=0; i<num_tris; i++ ) {
239 fwrite( &tri[i][0], sizeof(int), 1, fp );
240 fwrite( &tri[i][1], sizeof(int), 1, fp );
241 fwrite( &tri[i][2], sizeof(int), 1, fp );
249 save_nebula_sub( "autosaved.neb" );
254 char filename[255] = "\0";
255 //char filter[255] = "Nebula Files\0
257 memset(&o,0,sizeof(o));
258 o.lStructSize = sizeof(o);
259 //o.hwndOwner = GetActiveWindow();
260 o.lpstrFilter = "Nebula Files\0*.NEB\0\0";
261 o.lpstrFile = filename;
263 o.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
264 o.lpstrDefExt = "*.NEB";
265 if (!GetSaveFileName(&o)) return;
267 save_nebula_sub(filename);
272 char filename[255] = "\0";
274 memset(&o,0,sizeof(o));
275 o.lStructSize = sizeof(OPENFILENAME);
276 //o.hwndOwner = GetActiveWindow();
277 o.lpstrFilter = "Nebula Files\0*.NEB\0\0";
278 o.lpstrFile = filename;
280 o.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
281 o.lpstrDefExt = "*.NEB";
283 int create_default = 0;
284 if (!GetOpenFileName(&o)) {
287 if ( !load_nebula_sub(filename)) {
292 if ( create_default ) {
293 create_default_neb();
301 if ( nebula_inited ) return;
302 memset(Selected, 0, sizeof(BOOL)*MAX_POINTS);
305 create_default_neb();
306 gr_init_alphacolor( &nebula_color, 0, 255, 0, 255 );
311 void draw_tri_2d( int i, int j, int k )
320 vertex * verts[3] = { &va, &vb, &vc };
321 // gr_zbuffering = 0;
322 for (int v=0; v<3; v++ ) {
323 verts[v]->sx = i2fl(x[index[v]]);
324 verts[v]->sy = i2fl(y[index[v]]);
328 verts[v]->r = verts[v]->g = verts[v]->b = (ubyte)(i2fl(l[index[v]])/31.0f);
332 // gr_set_color( 0, 0, 0 );
333 gr_tmapper(3, verts, TMAP_FLAG_RAMP | TMAP_FLAG_GOURAUD | TMAP_FLAG_NEBULA );
335 if ( !keyd_pressed[KEY_LSHIFT] ) {
336 gr_set_color(100,100,100);
337 gr_line( x[i], y[i], x[j], y[j] );
338 gr_line( x[j], y[j], x[k], y[k] );
339 gr_line( x[k], y[k], x[i], y[i] );
343 void nebula_draw_2d()
347 for (i=0; i<num_tris; i++ ) {
348 draw_tri_2d( tri[i][0], tri[i][1], tri[i][2] );
351 gr_set_color(100,0,0);
352 for (i=0; i<num_pts; i++ ) {
353 gr_circle( x[i], y[i], 4 );
355 if ((Sel_mode==1)) { // multiple selection
357 gr_set_color(200,0,200);
358 gr_line(Orig_pos_x, Orig_pos_y, Orig_pos_x, End_pos_y);
359 gr_line(Orig_pos_x, End_pos_y, End_pos_x, End_pos_y);
360 gr_line(End_pos_x, Orig_pos_y, End_pos_x, End_pos_y);
361 gr_line(End_pos_x, Orig_pos_y, Orig_pos_x, Orig_pos_y);
363 gr_set_color(0,100,0);
364 for (int i=0;i<num_pts;i++)
365 if (Selected[i]) gr_circle( x[i], y[i], 5);
367 } else if ((Vert_mode==0)&&(Current_point>-1)) {
368 gr_set_color(0,100,0);
369 gr_circle( x[Current_point], y[Current_point], 5);
370 } else if (Vert_mode == 1) {
371 for (i=0;i<Which_vert;i++) {
372 gr_set_color(0,0,100);
373 gr_circle( x[tri[num_tris][i]], y[tri[num_tris][i]], 6);
375 gr_set_color(200,200,0);
376 if (Current_face>-1) {
377 gr_line(x[tri[Current_face][0]], y[tri[Current_face][0]],
378 x[tri[Current_face][1]], y[tri[Current_face][1]]);
379 gr_line(x[tri[Current_face][1]], y[tri[Current_face][1]],
380 x[tri[Current_face][2]], y[tri[Current_face][2]]);
381 gr_line(x[tri[Current_face][2]], y[tri[Current_face][2]],
382 x[tri[Current_face][0]], y[tri[Current_face][0]]);
388 void draw_tri_3d( int i, int j, int k )
397 vertex * verts[3] = { &va, &vb, &vc };
399 for (int v=0; v<3; v++ ) {
402 project_2d_onto_sphere( &tmp, 1.0f - i2fl(x[index[v]])/640.0f, i2fl(y[index[v]])/480.0f );
404 vm_vec_scale( &tmp, 10.0f );
406 g3_rotate_faraway_vertex( verts[v], &tmp );
407 //g3_rotate_vertex( verts[v], &tmp );
408 g3_project_vertex( verts[v] );
410 verts[v]->r = verts[v]->g = verts[v]->b = (ubyte)(i2fl(l[index[v]])/31.0f);
414 //gr_set_color_fast( &nebula_color );
415 //gr_set_color( 0, 0, 0 );
416 g3_draw_poly(3, verts, TMAP_FLAG_RAMP | TMAP_FLAG_GOURAUD | TMAP_FLAG_NEBULA );
419 void nebula_draw_3d()
422 for (i=0; i<num_tris; i++ ) {
423 draw_tri_3d( tri[i][0], tri[i][1], tri[i][2] );
431 gr_set_color(0,0,0); // set color to black
432 gr_rect(0,0,SCREEN_W,SCREEN_H); // clear screen
435 light_add_directional( &Global_light_world, 0.1f, 1.0f, 1.0f, 1.0f );
439 g3_set_view_matrix(&ViewerPos, &ViewerOrient,ViewerZoom);
441 if ( View_mode == 0 ) {
445 gr_set_color_fast( &color_green );
446 gr_printf(10,10,"Nebula Editor. Mode :");
448 gr_printf(180, 10, "Multiple Vertex Selection Editing");
449 } else if (Vert_mode ==0 ) {
450 gr_printf(180,10,"Vertex Editing");
451 if(Current_point >= 0){
452 gr_printf(180, 20, "Current vertex intensity : %d\n", l[Current_point]);
454 } else if (Vert_mode ==1) {
455 gr_printf(180,10,"Face Editing");
458 gr_printf(20,30,"# Points:");
459 gr_printf(100,30, itoa(num_pts, blah, 10));
460 gr_printf(220,30,"# Polys:");
461 gr_printf(300,30, itoa(num_tris, blah, 10));
464 model_render( test_model, &ModelOrient, &ModelPos );
472 int get_closest(int mx, int my)
474 int i, closest = -1, cval = 100000;
476 for (i=0; i<num_pts; i++ ) {
483 dist = fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
494 int get_closest_face(int mx, int my)
496 int i, closest = -1, cval = 100000;
498 for (i=0; i<num_tris; i++ ) {
502 dx = x[tri[i][0]] - mx;
503 dy = y[tri[i][0]] - my;
505 dx += x[tri[i][1]] - mx;
506 dy += y[tri[i][1]] - my;
508 dx += x[tri[i][2]] - mx;
509 dy += y[tri[i][2]] - my;
511 dist = fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
513 dist += fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
515 dx = x[tri[i][2]] - mx;
516 dy = y[tri[i][2]] - my;
517 dist += fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
529 void delete_face(int i)
531 for (int j=i;j<num_tris;j++) {
532 memcpy(tri[j], tri[j+1], sizeof(int)*3);
537 void delete_vert(int i)
539 for (int j=0;j<num_tris;j++) {
540 if ((tri[j][0]==i)||(tri[j][1]==i)||(tri[j][2]==i)) {
545 for (j=0;j<num_tris;j++) {
546 if (tri[j][0]>i) tri[j][0]--;
547 if (tri[j][1]>i) tri[j][1]--;
548 if (tri[j][2]>i) tri[j][2]--;
550 for (j=i;j<num_pts;j++) {
558 int add_vert(int mx, int my)
568 void select_by_box(int x1, int y1, int x2, int y2)
580 for (int i=0;i<num_pts;i++) {
581 if ((x[i]<=x2) && (x[i]>=x1) &&
582 (y[i]<=y2) && (y[i]>=y1)) {
588 void sphericalize_nebula()
591 int px = SCREEN_W / (neb_w - 1);
592 int py = SCREEN_H / (neb_h - 1);
594 // flatten out the nebula so that it covers the entire sphere evenly
595 for(idx1=0; idx1<neb_w; idx1++){
596 for(idx2=0; idx2<neb_h; idx2++){
597 x[idx1+idx2*neb_w] = px * idx1;
598 y[idx1+idx2*neb_w] = py * idx2;
603 void controls_read_all(control_info * ci, float sim_time )
608 float temp = ci->heading;
609 float temp1 = ci->pitch;
610 memset( ci, 0, sizeof(control_info) );
616 kh = (key_down_timef(KEY_PAD6) - key_down_timef(KEY_PAD4))/8.0f;
619 else if (kh > 0.0f) {
620 if (ci->heading < 0.0f)
623 if (ci->heading > 0.0f)
627 kh = (key_down_timef(KEY_PAD8) - key_down_timef(KEY_PAD2))/8.0f;
630 else if (kh > 0.0f) {
631 if (ci->pitch < 0.0f)
634 if (ci->pitch > 0.0f)
638 ci->bank = (key_down_timef(KEY_PAD7) - key_down_timef(KEY_PAD9))*.75f;
639 ci->forward = key_down_timef(KEY_A) - key_down_timef(KEY_Z);
640 ci->sideways = key_down_timef(KEY_PAD3) - key_down_timef(KEY_PAD1);
641 ci->vertical = key_down_timef(KEY_PADPLUS) - key_down_timef(KEY_PADENTER);
648 while( (k = key_inkey()) != 0 ) {
649 //mprintf(( "Key = %x\n", k ));
650 if ( k == KEY_ESC ) {
657 Vert_mode = !Vert_mode;
663 if (Vert_mode==1) delete_face(Current_face);
664 else if (Vert_mode==0) {
665 delete_vert(Current_point);
670 scale_factor -= 0.05f;
671 mprintf(( "Scale = %.1f\n", scale_factor ));
676 scale_factor += 0.05f;
677 mprintf(( "Scale = %.1f\n", scale_factor ));
681 Sel_mode = !Sel_mode;
685 View_mode = !View_mode;
691 for (i=0;i<num_pts;i++) if (Selected[i]) {
696 } else if (Vert_mode==0) {
697 if ( Current_point > -1 ) {
698 if ( l[Current_point] > 0 ) {
702 } else if (Vert_mode ==1) {
703 if ( l[tri[Current_face][0]] > 0 ) {
704 l[tri[Current_face][0]]--;
706 if ( l[tri[Current_face][1]] > 0 ) {
707 l[tri[Current_face][1]]--;
709 if ( l[tri[Current_face][2]] > 0 ) {
710 l[tri[Current_face][2]]--;
718 for (i=0;i<num_pts;i++) if (Selected[i]) {
723 } else if (Vert_mode==0) {
724 if ( Current_point > -1 ) {
725 if ( l[Current_point] < 31 ) {
729 } else if (Vert_mode ==1) {
730 if ( l[tri[Current_face][0]] <31 ) {
731 l[tri[Current_face][0]]++;
733 if ( l[tri[Current_face][1]] <31) {
734 l[tri[Current_face][1]]++;
736 if ( l[tri[Current_face][2]] <31 ) {
737 l[tri[Current_face][2]]++;
751 sphericalize_nebula();
767 int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow)
772 float speed = 20.0f; // how fast viewer moves
774 // setup the fred exe directory so CFILE can init properly
776 char *c = GetCommandLine();
778 char *tok = strtok(c, " ");
784 cfile_init(__argv[0]);
785 os_init( "NebEdit", "NebEdit" ); //SCREEN_W, SCREEN_H );
786 palette_load_table( "gamepalette1-01.pcx" );
787 gr_init(GR_640, GR_SOFTWARE, 8);
790 Font1 = gr_init_font( "font01.vf" );
791 gr_init_alphacolor( &color_green, 0,255,0,255 );
793 test_model = model_load( "fighter01.pof", 0, NULL );
795 physics_init( &ViewerPhysics );
796 ViewerPhysics.flags |= PF_ACCELERATES | PF_SLIDE_ENABLED;
798 ViewerPhysics.max_vel.x = 2.0f*speed; //sideways
799 ViewerPhysics.max_vel.y = 2.0f*speed; //up/down
800 ViewerPhysics.max_vel.z = 2.0f*speed; //forward
801 ViewerPhysics.max_rear_vel = 2.0f*speed; //backward -- controlled seperately
803 memset( &ci, 0, sizeof(control_info) );
805 ModelOrient = vmd_identity_matrix;
806 ModelPos.x=0.0f; ModelPos.y = 0.0f; ModelPos.z = 0.0f;
808 ViewerOrient = vmd_identity_matrix;
809 ViewerPos.x=0.0f; ViewerPos.y = 0.0f; ViewerPos.z = -50.0f;
811 flFrametime = 0.033f;
813 t1 = timer_get_fixed_seconds();
817 int some_selected = 0;
820 some_selected = FALSE;
822 for (i=0;i<num_pts;i++) {
824 some_selected = TRUE;
830 mouse_get_pos( &Mouse_x, &Mouse_y );
832 if (Current_face==-1) {
833 Current_face = get_closest_face(Mouse_x, Mouse_y);
841 // Special mouse handlers for multiple selmode
842 if (mouse_down_count(LOWEST_MOUSE_BUTTON)) {
843 Orig_pos_x = Mouse_x;
844 Orig_pos_y = Mouse_y;
846 if (mouse_down(LOWEST_MOUSE_BUTTON)) {
847 for (i=0;i<num_pts;i++) {
849 x[i]+=Mouse_x - Orig_pos_x;
850 y[i]+=Mouse_y - Orig_pos_y;
853 Orig_pos_x = Mouse_x;
854 Orig_pos_y = Mouse_y;
856 if (mouse_down_count(MOUSE_RIGHT_BUTTON)) {
857 Orig_pos_x = Mouse_x;
858 Orig_pos_y = Mouse_y;
859 for (i=0;i<num_pts;i++) {
863 if (mouse_down(MOUSE_RIGHT_BUTTON)) {
868 if (mouse_up_count(MOUSE_RIGHT_BUTTON)) {
869 Draw_sel_box = FALSE;
872 select_by_box(Orig_pos_x, Orig_pos_y, End_pos_x, End_pos_y);
877 if ( mouse_down(LOWEST_MOUSE_BUTTON) ) {
880 x[Current_point] = Mouse_x;
881 y[Current_point] = Mouse_y;
882 } else if (Vert_mode==1) {
883 x[tri[Current_face][0]] += Mouse_x - Orig_pos_x;
884 y[tri[Current_face][0]] += Mouse_y - Orig_pos_y;
885 x[tri[Current_face][1]] += Mouse_x - Orig_pos_x;
886 y[tri[Current_face][1]] += Mouse_y - Orig_pos_y;
887 x[tri[Current_face][2]] += Mouse_x - Orig_pos_x;
888 y[tri[Current_face][2]] += Mouse_y - Orig_pos_y;
889 Orig_pos_x = Mouse_x;
890 Orig_pos_y = Mouse_y;
893 if (Vert_mode == 1) {
894 Current_face = get_closest_face(Mouse_x, Mouse_y);
895 Orig_pos_x = Mouse_x;
896 Orig_pos_y = Mouse_y;
899 Current_point = get_closest(Mouse_x, Mouse_y);
900 mouse_set_pos(x[Current_point], y[Current_point]);
905 if ( mouse_up_count(LOWEST_MOUSE_BUTTON)) {
906 //Current_point = -1;
911 if ( mouse_up_count(MOUSE_RIGHT_BUTTON) ) {
913 Current_point = add_vert(Mouse_x, Mouse_y);
914 } else if (Vert_mode==1) {
915 if ((num_tris<MAX_TRIS-1)) {
916 tri[num_tris][Which_vert] = get_closest(Mouse_x, Mouse_y);
926 controls_read_all(&ci, flFrametime );
927 physics_read_flying_controls( &ViewerOrient, &ViewerPhysics, &ci, flFrametime );
928 physics_sim(&ViewerPos, &ViewerOrient, &ViewerPhysics, flFrametime );
932 t2 = timer_get_fixed_seconds();
934 flFrametime = f2fl(t2 - t1);
945 // Stub functions and variables.
946 // These do nothing but are needed to prevent link errors.
947 void demo_set_playback_filter() {}
949 void freespace_menu_background()
966 vector Dead_player_last_vel;