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 float flFrametime = 0.0f;
143 vector Global_light_world = { 0.208758f, -0.688253f, -0.694782f };
150 #define MAX_POINTS 300
152 int neb_w = 0, neb_h = 0;
154 int nebula_inited = 0;
157 int x[MAX_POINTS], y[MAX_POINTS], l[MAX_POINTS];
158 float scale_factor = 1.0f;
161 int tri[MAX_TRIS][3];
165 int Mouse_x, Mouse_y;
167 bool Selected[MAX_POINTS];
168 bool Sel_mode = false; // false = 1 point at a time, true = select multiple points
171 int View_mode = 0; // 0 = 2d editor, 1 = 3d viewer
173 int Vert_mode = 0; // 0 = Move vertices/Add vertices, 2 = Move face/Add face
175 int Which_vert = 0; // Current vertex of the faceadd
181 bool Draw_sel_box = false;
185 int Nebedit_running = 1;
187 extern void project_2d_onto_sphere(vector *, float, float);
189 class NebeditApp: public wxApp
192 virtual bool OnInit();
195 bool NebeditApp::OnInit()
201 void create_default_neb()
206 num_pts = neb_w*neb_h;
208 for (j=0; j<neb_h; j++ ) {
209 for (i=0; i<neb_w; i++ ) {
210 x[i+j*neb_w] = ((i+1)*SCREEN_W)/(neb_w+2);
211 y[i+j*neb_w] = ((j+3)*SCREEN_H)/(neb_h+6);
212 if ( (j==0) || (j==neb_h-1)) {
222 for (j=0; j<neb_h-1; j++ ) {
223 for (i=0; i<neb_w-1; i++ ) {
224 tri[num_tris][0] = i+neb_w*j;
225 tri[num_tris][1] = i+neb_w*j+1;
226 tri[num_tris][2] = i+neb_w*(j+1)+1;
229 tri[num_tris][0] = i+neb_w*j;
230 tri[num_tris][1] = i+neb_w*(j+1)+1;
231 tri[num_tris][2] = i+neb_w*(j+1);
239 #define NEBULA_FILE_ID "NEBU"
240 #define NEBULA_MAJOR_VERSION 1 // Can be 1-?
241 #define NEBULA_MINOR_VERSION 0 // Can be 0-99
243 void save_nebula_sub(const char *filename)
250 fp = cfopen(filename, "wb");
257 cfwrite( "NEBU", 4, 1, fp );
258 version = NEBULA_MAJOR_VERSION*100+NEBULA_MINOR_VERSION;
259 cfwrite_int(version, fp);
260 cfwrite_int(num_pts, fp);
261 cfwrite_int(num_tris, fp);
263 for (i=0; i<num_pts; i++ ) {
264 xf = INTEL_FLOAT(float(x[i])/640.0f);
265 yf = INTEL_FLOAT(float(y[i])/480.0f);
266 cfwrite_float(xf, fp);
267 cfwrite_float(yf, fp);
268 cfwrite_int(l[i], fp);
271 for (i=0; i<num_tris; i++ ) {
272 cfwrite_int(tri[i][0], fp);
273 cfwrite_int(tri[i][1], fp);
274 cfwrite_int(tri[i][2], fp);
280 // returns 0 if failed
281 int load_nebula_sub(const char *filename)
285 int version, major, minor;
287 fp = cfopen(filename, "rb");
294 cfread( id, 4, 1, fp );
295 if ( strncmp( id, NEBULA_FILE_ID, 4)) {
296 mprintf(( "Not a valid nebula file.\n" ));
300 version = cfread_int(fp);
301 major = version / 100;
302 minor = version % 100;
304 if ( (major != NEBULA_MAJOR_VERSION) && (minor != NEBULA_MINOR_VERSION) ) {
305 mprintf(( "An out of date nebula file.\n" ));
309 num_pts = cfread_int(fp);
310 SDL_assert( num_pts < MAX_POINTS );
311 num_tris = cfread_int(fp);
312 SDL_assert( num_tris < MAX_TRIS );
314 for (int i=0; i<num_pts; i++ ) {
315 x[i] = fl2i(cfread_float(fp) * 640.0f);
316 y[i] = fl2i(cfread_float(fp) * 480.0f);
317 l[i] = cfread_int(fp);
320 for (int i=0; i<num_tris; i++ ) {
321 tri[i][0] = cfread_int(fp);
322 tri[i][1] = cfread_int(fp);
323 tri[i][2] = cfread_int(fp);
333 char a_path[MAX_PATH];
335 cf_create_default_path_string(a_path, CF_TYPE_CACHE, "autosaved.neb");
337 save_nebula_sub( a_path );
342 wxFileDialog saveFileDialog(NULL, _("Save Nebula File"), wxEmptyString,
343 wxEmptyString, _("Nebula Files (*.neb)|*.neb"),
344 wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
346 if (saveFileDialog.ShowModal() == wxID_OK) {
347 save_nebula_sub(saveFileDialog.GetPath().ToAscii());
353 int create_default = 1;
355 wxFileDialog openFileDialog(NULL, _("Open Nebula File"), wxEmptyString,
356 wxEmptyString, _("Nebula Files (*.neb)|*.neb"),
357 wxFD_OPEN|wxFD_FILE_MUST_EXIST);
359 if (openFileDialog.ShowModal() == wxID_OK) {
360 create_default = !load_nebula_sub(openFileDialog.GetPath().ToAscii());
363 if ( create_default ) {
364 create_default_neb();
372 if ( nebula_inited ) return;
373 memset(Selected, 0, sizeof(bool)*MAX_POINTS);
376 create_default_neb();
377 gr_init_alphacolor( &nebula_color, 0, 255, 0, 255, AC_TYPE_HUD );
382 void draw_tri_2d( int i, int j, int k )
391 vertex * verts[3] = { &va, &vb, &vc };
392 // gr_zbuffering = 0;
393 for (int v=0; v<3; v++ ) {
394 verts[v]->sx = i2fl(x[index[v]]);
395 verts[v]->sy = i2fl(y[index[v]]);
399 verts[v]->b = (ubyte)(i2fl(l[index[v]]*255)/31.0f);
402 // gr_set_color( 0, 0, 0 );
403 gr_tmapper(3, verts, TMAP_FLAG_RAMP | TMAP_FLAG_GOURAUD | TMAP_FLAG_NEBULA );
405 if ( !key_pressed(SDLK_LSHIFT) ) {
406 gr_set_color(100,100,100);
407 gr_line( x[i], y[i], x[j], y[j] );
408 gr_line( x[j], y[j], x[k], y[k] );
409 gr_line( x[k], y[k], x[i], y[i] );
413 void nebula_draw_2d()
417 for (i=0; i<num_tris; i++ ) {
418 draw_tri_2d( tri[i][0], tri[i][1], tri[i][2] );
421 gr_set_color(100,0,0);
422 for (i=0; i<num_pts; i++ ) {
423 gr_circle( x[i], y[i], 4 );
425 if ((Sel_mode==1)) { // multiple selection
427 gr_set_color(200,0,200);
428 gr_line(Orig_pos_x, Orig_pos_y, Orig_pos_x, End_pos_y);
429 gr_line(Orig_pos_x, End_pos_y, End_pos_x, End_pos_y);
430 gr_line(End_pos_x, Orig_pos_y, End_pos_x, End_pos_y);
431 gr_line(End_pos_x, Orig_pos_y, Orig_pos_x, Orig_pos_y);
433 gr_set_color(0,100,0);
434 for (int i=0;i<num_pts;i++)
435 if (Selected[i]) gr_circle( x[i], y[i], 5);
437 } else if ((Vert_mode==0)&&(Current_point>-1)) {
438 gr_set_color(0,100,0);
439 gr_circle( x[Current_point], y[Current_point], 5);
440 } else if (Vert_mode == 1) {
441 for (i=0;i<Which_vert;i++) {
442 gr_set_color(0,0,100);
443 gr_circle( x[tri[num_tris][i]], y[tri[num_tris][i]], 6);
445 gr_set_color(200,200,0);
446 if (Current_face>-1) {
447 gr_line(x[tri[Current_face][0]], y[tri[Current_face][0]],
448 x[tri[Current_face][1]], y[tri[Current_face][1]]);
449 gr_line(x[tri[Current_face][1]], y[tri[Current_face][1]],
450 x[tri[Current_face][2]], y[tri[Current_face][2]]);
451 gr_line(x[tri[Current_face][2]], y[tri[Current_face][2]],
452 x[tri[Current_face][0]], y[tri[Current_face][0]]);
458 void draw_tri_3d( int i, int j, int k )
467 vertex * verts[3] = { &va, &vb, &vc };
469 for (int v=0; v<3; v++ ) {
472 project_2d_onto_sphere( &tmp, 1.0f - i2fl(x[index[v]])/640.0f, i2fl(y[index[v]])/480.0f );
474 vm_vec_scale( &tmp, 10.0f );
476 g3_rotate_faraway_vertex( verts[v], &tmp );
477 //g3_rotate_vertex( verts[v], &tmp );
478 g3_project_vertex( verts[v] );
480 verts[v]->b = (ubyte)(i2fl(l[index[v]]*255)/31.0f);
484 //gr_set_color_fast( &nebula_color );
485 //gr_set_color( 0, 0, 0 );
486 g3_draw_poly(3, verts, TMAP_FLAG_RAMP | TMAP_FLAG_GOURAUD | TMAP_FLAG_NEBULA );
489 void nebula_draw_3d()
492 for (i=0; i<num_tris; i++ ) {
493 draw_tri_3d( tri[i][0], tri[i][1], tri[i][2] );
501 gr_set_color(0,0,0); // set color to black
502 gr_rect(0,0,SCREEN_W,SCREEN_H); // clear screen
505 light_add_directional( &Global_light_world, 0.1f, 1.0f, 1.0f, 1.0f );
509 g3_set_view_matrix(&ViewerPos, &ViewerOrient,ViewerZoom);
511 if ( View_mode == 0 ) {
515 gr_set_color_fast( &color_green );
516 gr_printf(10,10,"Nebula Editor. Mode :");
518 gr_printf(180, 10, "Multiple Vertex Selection Editing");
519 } else if (Vert_mode ==0 ) {
520 gr_printf(180,10,"Vertex Editing");
521 if(Current_point >= 0){
522 gr_printf(180, 20, "Current vertex intensity : %d\n", l[Current_point]);
524 } else if (Vert_mode ==1) {
525 gr_printf(180,10,"Face Editing");
528 gr_printf(20,30,"# Points:");
529 gr_printf(100,30, SDL_itoa(num_pts, blah, 10));
530 gr_printf(220,30,"# Polys:");
531 gr_printf(300,30, SDL_itoa(num_tris, blah, 10));
534 model_render( test_model, &ModelOrient, &ModelPos );
542 int get_closest(int mx, int my)
544 int i, closest = -1, cval = 100000;
546 for (i=0; i<num_pts; i++ ) {
553 dist = fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
564 int get_closest_face(int mx, int my)
566 int i, closest = -1, cval = 100000;
568 for (i=0; i<num_tris; i++ ) {
572 dx = x[tri[i][0]] - mx;
573 dy = y[tri[i][0]] - my;
575 dx += x[tri[i][1]] - mx;
576 dy += y[tri[i][1]] - my;
578 dx += x[tri[i][2]] - mx;
579 dy += y[tri[i][2]] - my;
581 dist = fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
583 dist += fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
585 dx = x[tri[i][2]] - mx;
586 dy = y[tri[i][2]] - my;
587 dist += fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
599 void delete_face(int i)
601 for (int j=i;j<num_tris;j++) {
602 memcpy(tri[j], tri[j+1], sizeof(int)*3);
607 void delete_vert(int i)
611 for (j=0;j<num_tris;j++) {
612 if ((tri[j][0]==i)||(tri[j][1]==i)||(tri[j][2]==i)) {
617 for (j=0;j<num_tris;j++) {
618 if (tri[j][0]>i) tri[j][0]--;
619 if (tri[j][1]>i) tri[j][1]--;
620 if (tri[j][2]>i) tri[j][2]--;
622 for (j=i;j<num_pts;j++) {
630 int add_vert(int mx, int my)
632 SDL_assert(num_pts<300);
640 void select_by_box(int x1, int y1, int x2, int y2)
652 for (int i=0;i<num_pts;i++) {
653 if ((x[i]<=x2) && (x[i]>=x1) &&
654 (y[i]<=y2) && (y[i]>=y1)) {
660 void sphericalize_nebula()
663 int px = SCREEN_W / (neb_w - 1);
664 int py = SCREEN_H / (neb_h - 1);
666 // flatten out the nebula so that it covers the entire sphere evenly
667 for(idx1=0; idx1<neb_w; idx1++){
668 for(idx2=0; idx2<neb_h; idx2++){
669 x[idx1+idx2*neb_w] = px * idx1;
670 y[idx1+idx2*neb_w] = py * idx2;
675 void controls_read_all(control_info * ci, float sim_time )
680 float temp = ci->heading;
681 float temp1 = ci->pitch;
682 memset( ci, 0, sizeof(control_info) );
688 kh = (key_down_timef(SDLK_KP_6) - key_down_timef(SDLK_KP_4))/8.0f;
691 else if (kh > 0.0f) {
692 if (ci->heading < 0.0f)
695 if (ci->heading > 0.0f)
699 kh = (key_down_timef(SDLK_KP_8) - key_down_timef(SDLK_KP_2))/8.0f;
702 else if (kh > 0.0f) {
703 if (ci->pitch < 0.0f)
706 if (ci->pitch > 0.0f)
710 ci->bank = (key_down_timef(SDLK_KP_7) - key_down_timef(SDLK_KP_9))*.75f;
711 ci->forward = key_down_timef(SDLK_a) - key_down_timef(SDLK_z);
712 ci->sideways = key_down_timef(SDLK_KP_3) - key_down_timef(SDLK_KP_1);
713 ci->vertical = key_down_timef(SDLK_KP_PLUS) - key_down_timef(SDLK_KP_ENTER);
720 while( (k = key_inkey()) != 0 ) {
721 //mprintf(( "Key = %x\n", k ));
722 if ( k == SDLK_ESCAPE ) {
729 Vert_mode = !Vert_mode;
735 if (Vert_mode==1) delete_face(Current_face);
736 else if (Vert_mode==0) {
737 delete_vert(Current_point);
742 scale_factor -= 0.05f;
743 mprintf(( "Scale = %.1f\n", scale_factor ));
748 scale_factor += 0.05f;
749 mprintf(( "Scale = %.1f\n", scale_factor ));
753 Sel_mode = !Sel_mode;
757 View_mode = !View_mode;
763 for (i=0;i<num_pts;i++) if (Selected[i]) {
768 } else if (Vert_mode==0) {
769 if ( Current_point > -1 ) {
770 if ( l[Current_point] > 0 ) {
774 } else if (Vert_mode ==1) {
775 if ( l[tri[Current_face][0]] > 0 ) {
776 l[tri[Current_face][0]]--;
778 if ( l[tri[Current_face][1]] > 0 ) {
779 l[tri[Current_face][1]]--;
781 if ( l[tri[Current_face][2]] > 0 ) {
782 l[tri[Current_face][2]]--;
790 for (i=0;i<num_pts;i++) if (Selected[i]) {
795 } else if (Vert_mode==0) {
796 if ( Current_point > -1 ) {
797 if ( l[Current_point] < 31 ) {
801 } else if (Vert_mode ==1) {
802 if ( l[tri[Current_face][0]] <31 ) {
803 l[tri[Current_face][0]]++;
805 if ( l[tri[Current_face][1]] <31) {
806 l[tri[Current_face][1]]++;
808 if ( l[tri[Current_face][2]] <31 ) {
809 l[tri[Current_face][2]]++;
823 sphericalize_nebula();
840 int main(int argc, char *argv[])
845 float speed = 20.0f; // how fast viewer moves
847 // setup the fred exe directory so CFILE can init properly
849 char *c = GetCommandLine();
850 SDL_assert(c != NULL);
851 char *tok = strtok(c, " ");
852 SDL_assert(tok != NULL);
855 Cmdline_window = 1; // always windowed
860 os_init( "NebEdit", "NebEdit" ); //SCREEN_W, SCREEN_H );
861 os_set_title("NebEdit");
862 gr_init(GR_640, GR_OPENGL, 16);
863 palette_load_table( "gamepalette1-01.pcx" );
867 Font1 = gr_init_font( "font01.vf" );
868 gr_init_alphacolor( &color_green, 0,255,0,255,AC_TYPE_HUD );
870 test_model = model_load( "fighter01.pof", 0, NULL );
872 physics_init( &ViewerPhysics );
873 ViewerPhysics.flags |= PF_ACCELERATES | PF_SLIDE_ENABLED;
875 ViewerPhysics.max_vel.xyz.x = 2.0f*speed; //sideways
876 ViewerPhysics.max_vel.xyz.y = 2.0f*speed; //up/down
877 ViewerPhysics.max_vel.xyz.z = 2.0f*speed; //forward
878 ViewerPhysics.max_rear_vel = 2.0f*speed; //backward -- controlled seperately
880 memset( &ci, 0, sizeof(control_info) );
882 ModelOrient = vmd_identity_matrix;
883 ModelPos.xyz.x=0.0f; ModelPos.xyz.y = 0.0f; ModelPos.xyz.z = 0.0f;
885 ViewerOrient = vmd_identity_matrix;
886 ViewerPos.xyz.x=0.0f; ViewerPos.xyz.y = 0.0f; ViewerPos.xyz.z = -50.0f;
888 flFrametime = 0.033f;
890 t1 = timer_get_fixed_seconds();
894 wxApp::SetInstance( new NebeditApp() );
896 wxEntryStart(argc, argv);
898 //bool some_selected = false;
903 /*some_selected = false;
905 for (i=0;i<num_pts;i++) {
907 some_selected = true;
913 mouse_get_pos( &Mouse_x, &Mouse_y );
915 if (Current_face==-1) {
916 Current_face = get_closest_face(Mouse_x, Mouse_y);
924 // Special mouse handlers for multiple selmode
925 if (mouse_down_count(LOWEST_MOUSE_BUTTON)) {
926 Orig_pos_x = Mouse_x;
927 Orig_pos_y = Mouse_y;
929 if (mouse_down(LOWEST_MOUSE_BUTTON)) {
930 for (i=0;i<num_pts;i++) {
932 x[i]+=Mouse_x - Orig_pos_x;
933 y[i]+=Mouse_y - Orig_pos_y;
936 Orig_pos_x = Mouse_x;
937 Orig_pos_y = Mouse_y;
939 if (mouse_down_count(MOUSE_RIGHT_BUTTON)) {
940 Orig_pos_x = Mouse_x;
941 Orig_pos_y = Mouse_y;
942 for (i=0;i<num_pts;i++) {
946 if (mouse_down(MOUSE_RIGHT_BUTTON)) {
951 if (mouse_up_count(MOUSE_RIGHT_BUTTON)) {
952 Draw_sel_box = false;
955 select_by_box(Orig_pos_x, Orig_pos_y, End_pos_x, End_pos_y);
960 if ( mouse_down(LOWEST_MOUSE_BUTTON) ) {
963 x[Current_point] = Mouse_x;
964 y[Current_point] = Mouse_y;
965 } else if (Vert_mode==1) {
966 x[tri[Current_face][0]] += Mouse_x - Orig_pos_x;
967 y[tri[Current_face][0]] += Mouse_y - Orig_pos_y;
968 x[tri[Current_face][1]] += Mouse_x - Orig_pos_x;
969 y[tri[Current_face][1]] += Mouse_y - Orig_pos_y;
970 x[tri[Current_face][2]] += Mouse_x - Orig_pos_x;
971 y[tri[Current_face][2]] += Mouse_y - Orig_pos_y;
972 Orig_pos_x = Mouse_x;
973 Orig_pos_y = Mouse_y;
976 if (Vert_mode == 1) {
977 Current_face = get_closest_face(Mouse_x, Mouse_y);
978 Orig_pos_x = Mouse_x;
979 Orig_pos_y = Mouse_y;
982 Current_point = get_closest(Mouse_x, Mouse_y);
983 mouse_set_pos(x[Current_point], y[Current_point]);
988 if ( mouse_up_count(LOWEST_MOUSE_BUTTON)) {
989 //Current_point = -1;
994 if ( mouse_up_count(MOUSE_RIGHT_BUTTON) ) {
996 Current_point = add_vert(Mouse_x, Mouse_y);
997 } else if (Vert_mode==1) {
998 if ((num_tris<MAX_TRIS-1)) {
999 tri[num_tris][Which_vert] = get_closest(Mouse_x, Mouse_y);
1009 controls_read_all(&ci, flFrametime );
1010 physics_read_flying_controls( &ViewerOrient, &ViewerPhysics, &ci, flFrametime );
1011 physics_sim(&ViewerPos, &ViewerOrient, &ViewerPhysics, flFrametime );
1015 t2 = timer_get_fixed_seconds();
1017 flFrametime = f2fl(t2 - t1);
1032 // Stub functions and variables.
1033 // These do nothing but are needed to prevent link errors.
1034 void demo_set_playback_filter() {}
1036 void freespace_menu_background()
1042 int game_check_key()
1053 vector Dead_player_last_vel;