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
109 #include "floating.h"
112 #include "linklist.h"
113 #include "lighting.h"
128 physics_info ViewerPhysics;
129 float ViewerZoom = 1.0f;
132 int Fred_running = 0;
133 int Pofview_running = 0;
134 float flFrametime = 0.0f;
140 vector Global_light_world = { 0.208758f, -0.688253f, -0.694782f };
147 #define MAX_POINTS 300
149 int neb_w = 0, neb_h = 0;
151 int nebula_inited = 0;
154 int x[MAX_POINTS], y[MAX_POINTS], l[MAX_POINTS];
155 float scale_factor = 1.0f;
158 int tri[MAX_TRIS][3];
162 int Mouse_x, Mouse_y;
164 BOOL Selected[MAX_POINTS];
165 BOOL Sel_mode = 0; // 0 = 1 point at a time, 1 = select multiple points
168 int View_mode = 0; // 0 = 2d editor, 1 = 3d viewer
170 int Vert_mode = 0; // 0 = Move vertices/Add vertices, 2 = Move face/Add face
172 int Which_vert = 0; // Current vertex of the faceadd
178 BOOL Draw_sel_box = FALSE;
182 int Nebedit_running = 1;
184 extern int load_nebula_sub(char*);
185 extern void project_2d_onto_sphere(vector *, float, float);
187 void create_default_neb()
192 num_pts = neb_w*neb_h;
194 for (j=0; j<neb_h; j++ ) {
195 for (i=0; i<neb_w; i++ ) {
196 x[i+j*neb_w] = ((i+1)*SCREEN_W)/(neb_w+2);
197 y[i+j*neb_w] = ((j+3)*SCREEN_H)/(neb_h+6);
198 if ( (j==0) || (j==neb_h-1)) {
208 for (j=0; j<neb_h-1; j++ ) {
209 for (i=0; i<neb_w-1; i++ ) {
210 tri[num_tris][0] = i+neb_w*j;
211 tri[num_tris][1] = i+neb_w*j+1;
212 tri[num_tris][2] = i+neb_w*(j+1)+1;
215 tri[num_tris][0] = i+neb_w*j;
216 tri[num_tris][1] = i+neb_w*(j+1)+1;
217 tri[num_tris][2] = i+neb_w*(j+1);
225 #define NEBULA_FILE_ID "NEBU"
226 #define NEBULA_MAJOR_VERSION 1 // Can be 1-?
227 #define NEBULA_MINOR_VERSION 0 // Can be 0-99
229 void save_nebula_sub(char *filename)
235 fp = fopen(filename, "wb");
238 fwrite( "NEBU", 4, 1, fp );
239 version = NEBULA_MAJOR_VERSION*100+NEBULA_MINOR_VERSION;
240 fwrite( &version, sizeof(int), 1, fp );
241 fwrite( &num_pts, sizeof(int), 1, fp );
242 fwrite( &num_tris, sizeof(int), 1, fp );
244 for (int i=0; i<num_pts; i++ ) {
245 xf = float(x[i])/640.0f;
246 yf = float(y[i])/480.0f;
247 fwrite( &xf, sizeof(float), 1, fp );
248 fwrite( &yf, sizeof(float), 1, fp );
249 fwrite( &l[i], sizeof(int), 1, fp );
252 for (i=0; i<num_tris; i++ ) {
253 fwrite( &tri[i][0], sizeof(int), 1, fp );
254 fwrite( &tri[i][1], sizeof(int), 1, fp );
255 fwrite( &tri[i][2], sizeof(int), 1, fp );
263 save_nebula_sub( "autosaved.neb" );
268 char filename[255] = "\0";
269 //char filter[255] = "Nebula Files\0
271 memset(&o,0,sizeof(o));
272 o.lStructSize = sizeof(o);
273 //o.hwndOwner = GetActiveWindow();
274 o.lpstrFilter = "Nebula Files\0*.NEB\0\0";
275 o.lpstrFile = filename;
277 o.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
278 o.lpstrDefExt = "*.NEB";
279 if (!GetSaveFileName(&o)) return;
281 save_nebula_sub(filename);
286 char filename[255] = "\0";
288 memset(&o,0,sizeof(o));
289 o.lStructSize = sizeof(OPENFILENAME);
290 //o.hwndOwner = GetActiveWindow();
291 o.lpstrFilter = "Nebula Files\0*.NEB\0\0";
292 o.lpstrFile = filename;
294 o.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
295 o.lpstrDefExt = "*.NEB";
297 int create_default = 0;
298 if (!GetOpenFileName(&o)) {
301 if ( !load_nebula_sub(filename)) {
306 if ( create_default ) {
307 create_default_neb();
315 if ( nebula_inited ) return;
316 memset(Selected, 0, sizeof(BOOL)*MAX_POINTS);
319 create_default_neb();
320 gr_init_alphacolor( &nebula_color, 0, 255, 0, 255 );
325 void draw_tri_2d( int i, int j, int k )
334 vertex * verts[3] = { &va, &vb, &vc };
335 // gr_zbuffering = 0;
336 for (int v=0; v<3; v++ ) {
337 verts[v]->sx = i2fl(x[index[v]]);
338 verts[v]->sy = i2fl(y[index[v]]);
342 verts[v]->r = verts[v]->g = verts[v]->b = (ubyte)(i2fl(l[index[v]])/31.0f);
346 // gr_set_color( 0, 0, 0 );
347 gr_tmapper(3, verts, TMAP_FLAG_RAMP | TMAP_FLAG_GOURAUD | TMAP_FLAG_NEBULA );
349 if ( !key_pressed(SDLK_LSHIFT) ) {
350 gr_set_color(100,100,100);
351 gr_line( x[i], y[i], x[j], y[j] );
352 gr_line( x[j], y[j], x[k], y[k] );
353 gr_line( x[k], y[k], x[i], y[i] );
357 void nebula_draw_2d()
361 for (i=0; i<num_tris; i++ ) {
362 draw_tri_2d( tri[i][0], tri[i][1], tri[i][2] );
365 gr_set_color(100,0,0);
366 for (i=0; i<num_pts; i++ ) {
367 gr_circle( x[i], y[i], 4 );
369 if ((Sel_mode==1)) { // multiple selection
371 gr_set_color(200,0,200);
372 gr_line(Orig_pos_x, Orig_pos_y, Orig_pos_x, End_pos_y);
373 gr_line(Orig_pos_x, End_pos_y, End_pos_x, End_pos_y);
374 gr_line(End_pos_x, Orig_pos_y, End_pos_x, End_pos_y);
375 gr_line(End_pos_x, Orig_pos_y, Orig_pos_x, Orig_pos_y);
377 gr_set_color(0,100,0);
378 for (int i=0;i<num_pts;i++)
379 if (Selected[i]) gr_circle( x[i], y[i], 5);
381 } else if ((Vert_mode==0)&&(Current_point>-1)) {
382 gr_set_color(0,100,0);
383 gr_circle( x[Current_point], y[Current_point], 5);
384 } else if (Vert_mode == 1) {
385 for (i=0;i<Which_vert;i++) {
386 gr_set_color(0,0,100);
387 gr_circle( x[tri[num_tris][i]], y[tri[num_tris][i]], 6);
389 gr_set_color(200,200,0);
390 if (Current_face>-1) {
391 gr_line(x[tri[Current_face][0]], y[tri[Current_face][0]],
392 x[tri[Current_face][1]], y[tri[Current_face][1]]);
393 gr_line(x[tri[Current_face][1]], y[tri[Current_face][1]],
394 x[tri[Current_face][2]], y[tri[Current_face][2]]);
395 gr_line(x[tri[Current_face][2]], y[tri[Current_face][2]],
396 x[tri[Current_face][0]], y[tri[Current_face][0]]);
402 void draw_tri_3d( int i, int j, int k )
411 vertex * verts[3] = { &va, &vb, &vc };
413 for (int v=0; v<3; v++ ) {
416 project_2d_onto_sphere( &tmp, 1.0f - i2fl(x[index[v]])/640.0f, i2fl(y[index[v]])/480.0f );
418 vm_vec_scale( &tmp, 10.0f );
420 g3_rotate_faraway_vertex( verts[v], &tmp );
421 //g3_rotate_vertex( verts[v], &tmp );
422 g3_project_vertex( verts[v] );
424 verts[v]->r = verts[v]->g = verts[v]->b = (ubyte)(i2fl(l[index[v]])/31.0f);
428 //gr_set_color_fast( &nebula_color );
429 //gr_set_color( 0, 0, 0 );
430 g3_draw_poly(3, verts, TMAP_FLAG_RAMP | TMAP_FLAG_GOURAUD | TMAP_FLAG_NEBULA );
433 void nebula_draw_3d()
436 for (i=0; i<num_tris; i++ ) {
437 draw_tri_3d( tri[i][0], tri[i][1], tri[i][2] );
445 gr_set_color(0,0,0); // set color to black
446 gr_rect(0,0,SCREEN_W,SCREEN_H); // clear screen
449 light_add_directional( &Global_light_world, 0.1f, 1.0f, 1.0f, 1.0f );
453 g3_set_view_matrix(&ViewerPos, &ViewerOrient,ViewerZoom);
455 if ( View_mode == 0 ) {
459 gr_set_color_fast( &color_green );
460 gr_printf(10,10,"Nebula Editor. Mode :");
462 gr_printf(180, 10, "Multiple Vertex Selection Editing");
463 } else if (Vert_mode ==0 ) {
464 gr_printf(180,10,"Vertex Editing");
465 if(Current_point >= 0){
466 gr_printf(180, 20, "Current vertex intensity : %d\n", l[Current_point]);
468 } else if (Vert_mode ==1) {
469 gr_printf(180,10,"Face Editing");
472 gr_printf(20,30,"# Points:");
473 gr_printf(100,30, itoa(num_pts, blah, 10));
474 gr_printf(220,30,"# Polys:");
475 gr_printf(300,30, itoa(num_tris, blah, 10));
478 model_render( test_model, &ModelOrient, &ModelPos );
486 int get_closest(int mx, int my)
488 int i, closest = -1, cval = 100000;
490 for (i=0; i<num_pts; i++ ) {
497 dist = fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
508 int get_closest_face(int mx, int my)
510 int i, closest = -1, cval = 100000;
512 for (i=0; i<num_tris; i++ ) {
516 dx = x[tri[i][0]] - mx;
517 dy = y[tri[i][0]] - my;
519 dx += x[tri[i][1]] - mx;
520 dy += y[tri[i][1]] - my;
522 dx += x[tri[i][2]] - mx;
523 dy += y[tri[i][2]] - my;
525 dist = fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
527 dist += fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
529 dx = x[tri[i][2]] - mx;
530 dy = y[tri[i][2]] - my;
531 dist += fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
543 void delete_face(int i)
545 for (int j=i;j<num_tris;j++) {
546 memcpy(tri[j], tri[j+1], sizeof(int)*3);
551 void delete_vert(int i)
553 for (int j=0;j<num_tris;j++) {
554 if ((tri[j][0]==i)||(tri[j][1]==i)||(tri[j][2]==i)) {
559 for (j=0;j<num_tris;j++) {
560 if (tri[j][0]>i) tri[j][0]--;
561 if (tri[j][1]>i) tri[j][1]--;
562 if (tri[j][2]>i) tri[j][2]--;
564 for (j=i;j<num_pts;j++) {
572 int add_vert(int mx, int my)
582 void select_by_box(int x1, int y1, int x2, int y2)
594 for (int i=0;i<num_pts;i++) {
595 if ((x[i]<=x2) && (x[i]>=x1) &&
596 (y[i]<=y2) && (y[i]>=y1)) {
602 void sphericalize_nebula()
605 int px = SCREEN_W / (neb_w - 1);
606 int py = SCREEN_H / (neb_h - 1);
608 // flatten out the nebula so that it covers the entire sphere evenly
609 for(idx1=0; idx1<neb_w; idx1++){
610 for(idx2=0; idx2<neb_h; idx2++){
611 x[idx1+idx2*neb_w] = px * idx1;
612 y[idx1+idx2*neb_w] = py * idx2;
617 void controls_read_all(control_info * ci, float sim_time )
622 float temp = ci->heading;
623 float temp1 = ci->pitch;
624 memset( ci, 0, sizeof(control_info) );
630 kh = (key_down_timef(SDLK_KP_6) - key_down_timef(SDLK_KP_4))/8.0f;
633 else if (kh > 0.0f) {
634 if (ci->heading < 0.0f)
637 if (ci->heading > 0.0f)
641 kh = (key_down_timef(SDLK_KP_8) - key_down_timef(SDLK_KP_2))/8.0f;
644 else if (kh > 0.0f) {
645 if (ci->pitch < 0.0f)
648 if (ci->pitch > 0.0f)
652 ci->bank = (key_down_timef(SDLK_KP_7) - key_down_timef(SDLK_KP_9))*.75f;
653 ci->forward = key_down_timef(SDLK_a) - key_down_timef(SDLK_z);
654 ci->sideways = key_down_timef(SDLK_KP_3) - key_down_timef(SDLK_KP_1);
655 ci->vertical = key_down_timef(SDLK_KP_PLUS) - key_down_timef(SDLK_KP_ENTER);
662 while( (k = key_inkey()) != 0 ) {
663 //mprintf(( "Key = %x\n", k ));
664 if ( k == SDLK_ESCAPE ) {
671 Vert_mode = !Vert_mode;
677 if (Vert_mode==1) delete_face(Current_face);
678 else if (Vert_mode==0) {
679 delete_vert(Current_point);
684 scale_factor -= 0.05f;
685 mprintf(( "Scale = %.1f\n", scale_factor ));
690 scale_factor += 0.05f;
691 mprintf(( "Scale = %.1f\n", scale_factor ));
695 Sel_mode = !Sel_mode;
699 View_mode = !View_mode;
705 for (i=0;i<num_pts;i++) if (Selected[i]) {
710 } else if (Vert_mode==0) {
711 if ( Current_point > -1 ) {
712 if ( l[Current_point] > 0 ) {
716 } else if (Vert_mode ==1) {
717 if ( l[tri[Current_face][0]] > 0 ) {
718 l[tri[Current_face][0]]--;
720 if ( l[tri[Current_face][1]] > 0 ) {
721 l[tri[Current_face][1]]--;
723 if ( l[tri[Current_face][2]] > 0 ) {
724 l[tri[Current_face][2]]--;
732 for (i=0;i<num_pts;i++) if (Selected[i]) {
737 } else if (Vert_mode==0) {
738 if ( Current_point > -1 ) {
739 if ( l[Current_point] < 31 ) {
743 } else if (Vert_mode ==1) {
744 if ( l[tri[Current_face][0]] <31 ) {
745 l[tri[Current_face][0]]++;
747 if ( l[tri[Current_face][1]] <31) {
748 l[tri[Current_face][1]]++;
750 if ( l[tri[Current_face][2]] <31 ) {
751 l[tri[Current_face][2]]++;
765 sphericalize_nebula();
781 int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow)
786 float speed = 20.0f; // how fast viewer moves
788 // setup the fred exe directory so CFILE can init properly
790 char *c = GetCommandLine();
792 char *tok = strtok(c, " ");
798 cfile_init(__argv[0]);
799 os_init( "NebEdit", "NebEdit" ); //SCREEN_W, SCREEN_H );
800 palette_load_table( "gamepalette1-01.pcx" );
801 gr_init(GR_640, GR_SOFTWARE, 8);
804 Font1 = gr_init_font( "font01.vf" );
805 gr_init_alphacolor( &color_green, 0,255,0,255 );
807 test_model = model_load( "fighter01.pof", 0, NULL );
809 physics_init( &ViewerPhysics );
810 ViewerPhysics.flags |= PF_ACCELERATES | PF_SLIDE_ENABLED;
812 ViewerPhysics.max_vel.x = 2.0f*speed; //sideways
813 ViewerPhysics.max_vel.y = 2.0f*speed; //up/down
814 ViewerPhysics.max_vel.z = 2.0f*speed; //forward
815 ViewerPhysics.max_rear_vel = 2.0f*speed; //backward -- controlled seperately
817 memset( &ci, 0, sizeof(control_info) );
819 ModelOrient = vmd_identity_matrix;
820 ModelPos.x=0.0f; ModelPos.y = 0.0f; ModelPos.z = 0.0f;
822 ViewerOrient = vmd_identity_matrix;
823 ViewerPos.x=0.0f; ViewerPos.y = 0.0f; ViewerPos.z = -50.0f;
825 flFrametime = 0.033f;
827 t1 = timer_get_fixed_seconds();
831 int some_selected = 0;
834 some_selected = FALSE;
836 for (i=0;i<num_pts;i++) {
838 some_selected = TRUE;
844 mouse_get_pos( &Mouse_x, &Mouse_y );
846 if (Current_face==-1) {
847 Current_face = get_closest_face(Mouse_x, Mouse_y);
855 // Special mouse handlers for multiple selmode
856 if (mouse_down_count(LOWEST_MOUSE_BUTTON)) {
857 Orig_pos_x = Mouse_x;
858 Orig_pos_y = Mouse_y;
860 if (mouse_down(LOWEST_MOUSE_BUTTON)) {
861 for (i=0;i<num_pts;i++) {
863 x[i]+=Mouse_x - Orig_pos_x;
864 y[i]+=Mouse_y - Orig_pos_y;
867 Orig_pos_x = Mouse_x;
868 Orig_pos_y = Mouse_y;
870 if (mouse_down_count(MOUSE_RIGHT_BUTTON)) {
871 Orig_pos_x = Mouse_x;
872 Orig_pos_y = Mouse_y;
873 for (i=0;i<num_pts;i++) {
877 if (mouse_down(MOUSE_RIGHT_BUTTON)) {
882 if (mouse_up_count(MOUSE_RIGHT_BUTTON)) {
883 Draw_sel_box = FALSE;
886 select_by_box(Orig_pos_x, Orig_pos_y, End_pos_x, End_pos_y);
891 if ( mouse_down(LOWEST_MOUSE_BUTTON) ) {
894 x[Current_point] = Mouse_x;
895 y[Current_point] = Mouse_y;
896 } else if (Vert_mode==1) {
897 x[tri[Current_face][0]] += Mouse_x - Orig_pos_x;
898 y[tri[Current_face][0]] += Mouse_y - Orig_pos_y;
899 x[tri[Current_face][1]] += Mouse_x - Orig_pos_x;
900 y[tri[Current_face][1]] += Mouse_y - Orig_pos_y;
901 x[tri[Current_face][2]] += Mouse_x - Orig_pos_x;
902 y[tri[Current_face][2]] += Mouse_y - Orig_pos_y;
903 Orig_pos_x = Mouse_x;
904 Orig_pos_y = Mouse_y;
907 if (Vert_mode == 1) {
908 Current_face = get_closest_face(Mouse_x, Mouse_y);
909 Orig_pos_x = Mouse_x;
910 Orig_pos_y = Mouse_y;
913 Current_point = get_closest(Mouse_x, Mouse_y);
914 mouse_set_pos(x[Current_point], y[Current_point]);
919 if ( mouse_up_count(LOWEST_MOUSE_BUTTON)) {
920 //Current_point = -1;
925 if ( mouse_up_count(MOUSE_RIGHT_BUTTON) ) {
927 Current_point = add_vert(Mouse_x, Mouse_y);
928 } else if (Vert_mode==1) {
929 if ((num_tris<MAX_TRIS-1)) {
930 tri[num_tris][Which_vert] = get_closest(Mouse_x, Mouse_y);
940 controls_read_all(&ci, flFrametime );
941 physics_read_flying_controls( &ViewerOrient, &ViewerPhysics, &ci, flFrametime );
942 physics_sim(&ViewerPos, &ViewerOrient, &ViewerPhysics, flFrametime );
946 t2 = timer_get_fixed_seconds();
948 flFrametime = f2fl(t2 - t1);
959 // Stub functions and variables.
960 // These do nothing but are needed to prevent link errors.
961 void demo_set_playback_filter() {}
963 void freespace_menu_background()
980 vector Dead_player_last_vel;