2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/nebedit/NebEdit.cpp $
15 * Program to edit nebulas in 2d
18 * Revision 1.3 2002/06/09 04:41:23 relnev
19 * added copyright header
21 * Revision 1.2 2002/05/07 03:16:47 theoddone33
22 * The Great Newline Fix
24 * Revision 1.1.1.1 2002/05/03 03:28:10 root
28 * 7 7/15/99 3:07p Dave
29 * 32 bit detection support. Mouse coord commandline.
31 * 6 5/19/99 4:07p Dave
32 * Moved versioning code into a nice isolated common place. Fixed up
33 * updating code on the pxo screen. Fixed several stub problems.
35 * 5 1/08/99 2:06p Dave
36 * Fixed pfoview for software mode.
38 * 4 12/18/98 1:14a Dave
39 * Rough 1024x768 support for Direct3D. Proper detection and usage through
42 * 3 11/13/98 2:32p Dave
43 * Improved commandline parsing for exe pathname.
45 * 2 10/24/98 9:51p Dave
47 * 1 10/24/98 9:39p Dave
49 * 13 4/13/98 10:11a John
50 * Made timer functions thread safe. Made timer_init be called in all
53 * 12 3/23/98 1:35p Sandeep
55 * 11 3/05/98 11:15p Hoffoss
56 * Changed non-game key checking to use game_check_key() instead of
59 * 10 12/29/97 5:10p Allender
60 * fixed problems with speed not being reported properly in multiplayer
61 * games. Made read_flying_controls take frametime as a parameter. More
62 * ship/weapon select stuff
64 * 9 12/04/97 3:47p John
65 * Made joystick move mouse cursor
67 * 8 11/21/97 11:32a John
68 * Added nebulas. Fixed some warpout bugs.
70 * 7 11/19/97 10:15p Adam
71 * upped maxtris to 200
73 * 6 11/19/97 4:28p Sandeep
74 * added poly/vert counter
76 * 5 11/19/97 1:59p Sandeep
77 * Added multiple vertex editing mode
79 * 4 11/16/97 2:29p John
80 * added versioning to nebulas; put nebula code into freespace.
82 * 3 11/13/97 12:04p Sandeep
83 * Added face editing support, deletion support, and a saving and loading
86 * 2 11/10/97 9:59p John
89 * 1 11/10/97 9:42p John
95 #include "wx/wxprec.h"
101 #include "wx/filedlg.h"
110 #include "floating.h"
113 #include "linklist.h"
114 #include "lighting.h"
121 #include "cfilesystem.h"
131 physics_info ViewerPhysics;
132 float ViewerZoom = 1.0f;
135 int Fred_running = 0;
136 int Pofview_running = 0;
137 int Fonttool_running = 0;
138 float flFrametime = 0.0f;
144 vector Global_light_world = { { { 0.208758f, -0.688253f, -0.694782f } } };
151 #define MAX_POINTS 300
153 int neb_w = 0, neb_h = 0;
155 int nebula_inited = 0;
158 static int x[MAX_POINTS], y[MAX_POINTS], l[MAX_POINTS];
159 float scale_factor = 1.0f;
162 int tri[MAX_TRIS][3];
166 int Mouse_x, Mouse_y;
168 bool Selected[MAX_POINTS];
169 bool Sel_mode = false; // false = 1 point at a time, true = select multiple points
172 int View_mode = 0; // 0 = 2d editor, 1 = 3d viewer
174 int Vert_mode = 0; // 0 = Move vertices/Add vertices, 2 = Move face/Add face
176 int Which_vert = 0; // Current vertex of the faceadd
182 bool Draw_sel_box = false;
186 int Nebedit_running = 1;
188 extern void project_2d_onto_sphere(vector *, float, float);
190 class NebeditApp: public wxApp
193 virtual bool OnInit();
196 bool NebeditApp::OnInit()
202 void create_default_neb()
207 num_pts = neb_w*neb_h;
209 for (j=0; j<neb_h; j++ ) {
210 for (i=0; i<neb_w; i++ ) {
211 x[i+j*neb_w] = ((i+1)*SCREEN_W)/(neb_w+2);
212 y[i+j*neb_w] = ((j+3)*SCREEN_H)/(neb_h+6);
213 if ( (j==0) || (j==neb_h-1)) {
223 for (j=0; j<neb_h-1; j++ ) {
224 for (i=0; i<neb_w-1; i++ ) {
225 tri[num_tris][0] = i+neb_w*j;
226 tri[num_tris][1] = i+neb_w*j+1;
227 tri[num_tris][2] = i+neb_w*(j+1)+1;
230 tri[num_tris][0] = i+neb_w*j;
231 tri[num_tris][1] = i+neb_w*(j+1)+1;
232 tri[num_tris][2] = i+neb_w*(j+1);
240 #define NEBULA_FILE_ID "NEBU"
241 #define NEBULA_MAJOR_VERSION 1 // Can be 1-?
242 #define NEBULA_MINOR_VERSION 0 // Can be 0-99
244 void save_nebula_sub(const char *filename)
251 fp = cfopen(filename, "wb");
258 cfwrite( "NEBU", 4, 1, fp );
259 version = NEBULA_MAJOR_VERSION*100+NEBULA_MINOR_VERSION;
260 cfwrite_int(version, fp);
261 cfwrite_int(num_pts, fp);
262 cfwrite_int(num_tris, fp);
264 for (i=0; i<num_pts; i++ ) {
265 xf = INTEL_FLOAT(float(x[i])/640.0f);
266 yf = INTEL_FLOAT(float(y[i])/480.0f);
267 cfwrite_float(xf, fp);
268 cfwrite_float(yf, fp);
269 cfwrite_int(l[i], fp);
272 for (i=0; i<num_tris; i++ ) {
273 cfwrite_int(tri[i][0], fp);
274 cfwrite_int(tri[i][1], fp);
275 cfwrite_int(tri[i][2], fp);
281 // returns 0 if failed
282 int load_nebula_sub(const char *filename)
286 int version, major, minor;
288 fp = cfopen(filename, "rb");
295 cfread( id, 4, 1, fp );
296 if ( strncmp( id, NEBULA_FILE_ID, 4)) {
297 mprintf(( "Not a valid nebula file.\n" ));
301 version = cfread_int(fp);
302 major = version / 100;
303 minor = version % 100;
305 if ( (major != NEBULA_MAJOR_VERSION) && (minor != NEBULA_MINOR_VERSION) ) {
306 mprintf(( "An out of date nebula file.\n" ));
310 num_pts = cfread_int(fp);
311 SDL_assert( num_pts < MAX_POINTS );
312 num_tris = cfread_int(fp);
313 SDL_assert( num_tris < MAX_TRIS );
315 for (int i=0; i<num_pts; i++ ) {
316 x[i] = fl2i(cfread_float(fp) * 640.0f);
317 y[i] = fl2i(cfread_float(fp) * 480.0f);
318 l[i] = cfread_int(fp);
321 for (int i=0; i<num_tris; i++ ) {
322 tri[i][0] = cfread_int(fp);
323 tri[i][1] = cfread_int(fp);
324 tri[i][2] = cfread_int(fp);
334 char a_path[MAX_PATH_LEN];
336 cf_create_default_path_string(a_path, CF_TYPE_CACHE, "autosaved.neb");
338 save_nebula_sub( a_path );
343 wxFileDialog saveFileDialog(NULL, _("Save Nebula File"), wxEmptyString,
344 wxEmptyString, _("Nebula Files (*.neb)|*.neb"),
345 wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
347 if (saveFileDialog.ShowModal() == wxID_OK) {
348 save_nebula_sub(saveFileDialog.GetPath().ToAscii());
354 int create_default = 1;
356 wxFileDialog openFileDialog(NULL, _("Open Nebula File"), wxEmptyString,
357 wxEmptyString, _("Nebula Files (*.neb)|*.neb"),
358 wxFD_OPEN|wxFD_FILE_MUST_EXIST);
360 if (openFileDialog.ShowModal() == wxID_OK) {
361 create_default = !load_nebula_sub(openFileDialog.GetPath().ToAscii());
364 if ( create_default ) {
365 create_default_neb();
373 if ( nebula_inited ) return;
374 memset(Selected, 0, sizeof(bool)*MAX_POINTS);
377 create_default_neb();
378 gr_init_alphacolor( &nebula_color, 0, 255, 0, 255, AC_TYPE_HUD );
383 void draw_tri_2d( int i, int j, int k )
392 vertex * verts[3] = { &va, &vb, &vc };
393 // gr_zbuffering = 0;
394 for (int v=0; v<3; v++ ) {
395 verts[v]->sx = i2fl(x[index[v]]);
396 verts[v]->sy = i2fl(y[index[v]]);
400 verts[v]->b = (ubyte)(i2fl(l[index[v]]*255)/31.0f);
403 // gr_set_color( 0, 0, 0 );
404 gr_tmapper(3, verts, TMAP_FLAG_RAMP | TMAP_FLAG_GOURAUD | TMAP_FLAG_NEBULA );
406 if ( !key_pressed(SDLK_LSHIFT) ) {
407 gr_set_color(100,100,100);
408 gr_line( x[i], y[i], x[j], y[j] );
409 gr_line( x[j], y[j], x[k], y[k] );
410 gr_line( x[k], y[k], x[i], y[i] );
414 void nebula_draw_2d()
418 for (i=0; i<num_tris; i++ ) {
419 draw_tri_2d( tri[i][0], tri[i][1], tri[i][2] );
422 gr_set_color(100,0,0);
423 for (i=0; i<num_pts; i++ ) {
424 gr_circle( x[i], y[i], 4 );
426 if (Sel_mode == 1) { // multiple selection
428 gr_set_color(200,0,200);
429 gr_line(Orig_pos_x, Orig_pos_y, Orig_pos_x, End_pos_y);
430 gr_line(Orig_pos_x, End_pos_y, End_pos_x, End_pos_y);
431 gr_line(End_pos_x, Orig_pos_y, End_pos_x, End_pos_y);
432 gr_line(End_pos_x, Orig_pos_y, Orig_pos_x, Orig_pos_y);
434 gr_set_color(0,100,0);
435 for (i=0;i<num_pts;i++)
436 if (Selected[i]) gr_circle( x[i], y[i], 5);
438 } else if ((Vert_mode==0)&&(Current_point>-1)) {
439 gr_set_color(0,100,0);
440 gr_circle( x[Current_point], y[Current_point], 5);
441 } else if (Vert_mode == 1) {
442 for (i=0;i<Which_vert;i++) {
443 gr_set_color(0,0,100);
444 gr_circle( x[tri[num_tris][i]], y[tri[num_tris][i]], 6);
446 gr_set_color(200,200,0);
447 if (Current_face>-1) {
448 gr_line(x[tri[Current_face][0]], y[tri[Current_face][0]],
449 x[tri[Current_face][1]], y[tri[Current_face][1]]);
450 gr_line(x[tri[Current_face][1]], y[tri[Current_face][1]],
451 x[tri[Current_face][2]], y[tri[Current_face][2]]);
452 gr_line(x[tri[Current_face][2]], y[tri[Current_face][2]],
453 x[tri[Current_face][0]], y[tri[Current_face][0]]);
459 void draw_tri_3d( int i, int j, int k )
468 vertex * verts[3] = { &va, &vb, &vc };
470 for (int v=0; v<3; v++ ) {
473 project_2d_onto_sphere( &tmp, 1.0f - i2fl(x[index[v]])/640.0f, i2fl(y[index[v]])/480.0f );
475 vm_vec_scale( &tmp, 10.0f );
477 g3_rotate_faraway_vertex( verts[v], &tmp );
478 //g3_rotate_vertex( verts[v], &tmp );
479 g3_project_vertex( verts[v] );
481 verts[v]->b = (ubyte)(i2fl(l[index[v]]*255)/31.0f);
485 //gr_set_color_fast( &nebula_color );
486 //gr_set_color( 0, 0, 0 );
487 g3_draw_poly(3, verts, TMAP_FLAG_RAMP | TMAP_FLAG_GOURAUD | TMAP_FLAG_NEBULA );
490 void nebula_draw_3d()
493 for (i=0; i<num_tris; i++ ) {
494 draw_tri_3d( tri[i][0], tri[i][1], tri[i][2] );
502 gr_set_color(0,0,0); // set color to black
503 gr_rect(0,0,SCREEN_W,SCREEN_H); // clear screen
506 light_add_directional( &Global_light_world, 0.1f, 1.0f, 1.0f, 1.0f );
510 g3_set_view_matrix(&ViewerPos, &ViewerOrient,ViewerZoom);
512 if ( View_mode == 0 ) {
516 gr_set_color_fast( &color_green );
517 gr_printf(10,10,"Nebula Editor. Mode :");
519 gr_printf(180, 10, "Multiple Vertex Selection Editing");
520 } else if (Vert_mode ==0 ) {
521 gr_printf(180,10,"Vertex Editing");
522 if(Current_point >= 0){
523 gr_printf(180, 20, "Current vertex intensity : %d\n", l[Current_point]);
525 } else if (Vert_mode ==1) {
526 gr_printf(180,10,"Face Editing");
529 gr_printf(20,30,"# Points:");
530 gr_printf(100,30, SDL_itoa(num_pts, blah, 10));
531 gr_printf(220,30,"# Polys:");
532 gr_printf(300,30, SDL_itoa(num_tris, blah, 10));
535 model_render( test_model, &ModelOrient, &ModelPos );
543 int get_closest(int mx, int my)
545 int i, closest = -1, cval = 100000;
547 for (i=0; i<num_pts; i++ ) {
554 dist = fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
565 int get_closest_face(int mx, int my)
567 int i, closest = -1, cval = 100000;
569 for (i=0; i<num_tris; i++ ) {
573 dx = x[tri[i][0]] - mx;
574 dy = y[tri[i][0]] - my;
576 dx += x[tri[i][1]] - mx;
577 dy += y[tri[i][1]] - my;
579 dx += x[tri[i][2]] - mx;
580 dy += y[tri[i][2]] - my;
582 dist = fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
584 dist += fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
586 dx = x[tri[i][2]] - mx;
587 dy = y[tri[i][2]] - my;
588 dist += fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
600 void delete_face(int i)
602 for (int j=i;j<num_tris;j++) {
603 memcpy(tri[j], tri[j+1], sizeof(int)*3);
608 void delete_vert(int i)
612 for (j=0;j<num_tris;j++) {
613 if ((tri[j][0]==i)||(tri[j][1]==i)||(tri[j][2]==i)) {
618 for (j=0;j<num_tris;j++) {
619 if (tri[j][0]>i) tri[j][0]--;
620 if (tri[j][1]>i) tri[j][1]--;
621 if (tri[j][2]>i) tri[j][2]--;
623 for (j=i;j<num_pts;j++) {
631 int add_vert(int mx, int my)
633 SDL_assert(num_pts<300);
641 void select_by_box(int x1, int y1, int x2, int y2)
653 for (int i=0;i<num_pts;i++) {
654 if ((x[i]<=x2) && (x[i]>=x1) &&
655 (y[i]<=y2) && (y[i]>=y1)) {
661 void sphericalize_nebula()
664 int px = SCREEN_W / (neb_w - 1);
665 int py = SCREEN_H / (neb_h - 1);
667 // flatten out the nebula so that it covers the entire sphere evenly
668 for(idx1=0; idx1<neb_w; idx1++){
669 for(idx2=0; idx2<neb_h; idx2++){
670 x[idx1+idx2*neb_w] = px * idx1;
671 y[idx1+idx2*neb_w] = py * idx2;
676 void controls_read_all(control_info * ci, float sim_time )
681 float temp = ci->heading;
682 float temp1 = ci->pitch;
683 memset( ci, 0, sizeof(control_info) );
689 kh = (key_down_timef(SDLK_KP_6) - key_down_timef(SDLK_KP_4))/8.0f;
692 else if (kh > 0.0f) {
693 if (ci->heading < 0.0f)
696 if (ci->heading > 0.0f)
700 kh = (key_down_timef(SDLK_KP_8) - key_down_timef(SDLK_KP_2))/8.0f;
703 else if (kh > 0.0f) {
704 if (ci->pitch < 0.0f)
707 if (ci->pitch > 0.0f)
711 ci->bank = (key_down_timef(SDLK_KP_7) - key_down_timef(SDLK_KP_9))*.75f;
712 ci->forward = key_down_timef(SDLK_a) - key_down_timef(SDLK_z);
713 ci->sideways = key_down_timef(SDLK_KP_3) - key_down_timef(SDLK_KP_1);
714 ci->vertical = key_down_timef(SDLK_KP_PLUS) - key_down_timef(SDLK_KP_ENTER);
721 while( (k = key_inkey()) != 0 ) {
722 //mprintf(( "Key = %x\n", k ));
723 if ( k == SDLK_ESCAPE ) {
730 Vert_mode = !Vert_mode;
736 if (Vert_mode==1) delete_face(Current_face);
737 else if (Vert_mode==0) {
738 delete_vert(Current_point);
743 scale_factor -= 0.05f;
744 mprintf(( "Scale = %.1f\n", scale_factor ));
749 scale_factor += 0.05f;
750 mprintf(( "Scale = %.1f\n", scale_factor ));
754 Sel_mode = !Sel_mode;
758 View_mode = !View_mode;
764 for (i=0;i<num_pts;i++) if (Selected[i]) {
769 } else if (Vert_mode==0) {
770 if ( Current_point > -1 ) {
771 if ( l[Current_point] > 0 ) {
775 } else if (Vert_mode ==1) {
776 if ( l[tri[Current_face][0]] > 0 ) {
777 l[tri[Current_face][0]]--;
779 if ( l[tri[Current_face][1]] > 0 ) {
780 l[tri[Current_face][1]]--;
782 if ( l[tri[Current_face][2]] > 0 ) {
783 l[tri[Current_face][2]]--;
791 for (i=0;i<num_pts;i++) if (Selected[i]) {
796 } else if (Vert_mode==0) {
797 if ( Current_point > -1 ) {
798 if ( l[Current_point] < 31 ) {
802 } else if (Vert_mode ==1) {
803 if ( l[tri[Current_face][0]] <31 ) {
804 l[tri[Current_face][0]]++;
806 if ( l[tri[Current_face][1]] <31) {
807 l[tri[Current_face][1]]++;
809 if ( l[tri[Current_face][2]] <31 ) {
810 l[tri[Current_face][2]]++;
824 sphericalize_nebula();
841 int main(int argc, char *argv[])
846 float speed = 20.0f; // how fast viewer moves
848 // setup the fred exe directory so CFILE can init properly
850 char *c = GetCommandLine();
851 SDL_assert(c != NULL);
852 char *tok = strtok(c, " ");
853 SDL_assert(tok != NULL);
856 Cmdline_window = 1; // always windowed
861 os_init( "NebEdit", "NebEdit" ); //SCREEN_W, SCREEN_H );
862 os_set_title("NebEdit");
864 palette_load_table( "gamepalette1-01.pcx" );
868 Font1 = gr_init_font( "font01.vf" );
869 gr_init_alphacolor( &color_green, 0,255,0,255,AC_TYPE_HUD );
871 test_model = model_load( "fighter01.pof", 0, NULL );
873 physics_init( &ViewerPhysics );
874 ViewerPhysics.flags |= PF_ACCELERATES | PF_SLIDE_ENABLED;
876 ViewerPhysics.max_vel.xyz.x = 2.0f*speed; //sideways
877 ViewerPhysics.max_vel.xyz.y = 2.0f*speed; //up/down
878 ViewerPhysics.max_vel.xyz.z = 2.0f*speed; //forward
879 ViewerPhysics.max_rear_vel = 2.0f*speed; //backward -- controlled seperately
881 memset( &ci, 0, sizeof(control_info) );
883 ModelOrient = vmd_identity_matrix;
884 ModelPos.xyz.x=0.0f; ModelPos.xyz.y = 0.0f; ModelPos.xyz.z = 0.0f;
886 ViewerOrient = vmd_identity_matrix;
887 ViewerPos.xyz.x=0.0f; ViewerPos.xyz.y = 0.0f; ViewerPos.xyz.z = -50.0f;
889 flFrametime = 0.033f;
891 t1 = timer_get_fixed_seconds();
895 wxApp::SetInstance( new NebeditApp() );
897 wxEntryStart(argc, argv);
899 //bool some_selected = false;
904 /*some_selected = false;
906 for (i=0;i<num_pts;i++) {
908 some_selected = true;
914 mouse_get_pos( &Mouse_x, &Mouse_y );
916 if (Current_face==-1) {
917 Current_face = get_closest_face(Mouse_x, Mouse_y);
925 // Special mouse handlers for multiple selmode
926 if (mouse_down_count(LOWEST_MOUSE_BUTTON)) {
927 Orig_pos_x = Mouse_x;
928 Orig_pos_y = Mouse_y;
930 if (mouse_down(LOWEST_MOUSE_BUTTON)) {
931 for (i=0;i<num_pts;i++) {
933 x[i]+=Mouse_x - Orig_pos_x;
934 y[i]+=Mouse_y - Orig_pos_y;
937 Orig_pos_x = Mouse_x;
938 Orig_pos_y = Mouse_y;
940 if (mouse_down_count(MOUSE_RIGHT_BUTTON)) {
941 Orig_pos_x = Mouse_x;
942 Orig_pos_y = Mouse_y;
943 for (i=0;i<num_pts;i++) {
947 if (mouse_down(MOUSE_RIGHT_BUTTON)) {
952 if (mouse_up_count(MOUSE_RIGHT_BUTTON)) {
953 Draw_sel_box = false;
956 select_by_box(Orig_pos_x, Orig_pos_y, End_pos_x, End_pos_y);
961 if ( mouse_down(LOWEST_MOUSE_BUTTON) ) {
964 x[Current_point] = Mouse_x;
965 y[Current_point] = Mouse_y;
966 } else if (Vert_mode==1) {
967 x[tri[Current_face][0]] += Mouse_x - Orig_pos_x;
968 y[tri[Current_face][0]] += Mouse_y - Orig_pos_y;
969 x[tri[Current_face][1]] += Mouse_x - Orig_pos_x;
970 y[tri[Current_face][1]] += Mouse_y - Orig_pos_y;
971 x[tri[Current_face][2]] += Mouse_x - Orig_pos_x;
972 y[tri[Current_face][2]] += Mouse_y - Orig_pos_y;
973 Orig_pos_x = Mouse_x;
974 Orig_pos_y = Mouse_y;
977 if (Vert_mode == 1) {
978 Current_face = get_closest_face(Mouse_x, Mouse_y);
979 Orig_pos_x = Mouse_x;
980 Orig_pos_y = Mouse_y;
983 Current_point = get_closest(Mouse_x, Mouse_y);
984 mouse_set_pos(x[Current_point], y[Current_point]);
989 if ( mouse_up_count(LOWEST_MOUSE_BUTTON)) {
990 //Current_point = -1;
995 if ( mouse_up_count(MOUSE_RIGHT_BUTTON) ) {
997 Current_point = add_vert(Mouse_x, Mouse_y);
998 } else if (Vert_mode==1) {
999 if ((num_tris<MAX_TRIS-1)) {
1000 tri[num_tris][Which_vert] = get_closest(Mouse_x, Mouse_y);
1010 controls_read_all(&ci, flFrametime );
1011 physics_read_flying_controls( &ViewerOrient, &ViewerPhysics, &ci, flFrametime );
1012 physics_sim(&ViewerPos, &ViewerOrient, &ViewerPhysics, flFrametime );
1016 t2 = timer_get_fixed_seconds();
1018 flFrametime = f2fl(t2 - t1);
1033 // Stub functions and variables.
1034 // These do nothing but are needed to prevent link errors.
1035 void demo_set_playback_filter() {}
1037 void freespace_menu_background()
1043 int game_check_key()
1054 vector Dead_player_last_vel;