// File: /workspace/github_c_makefile_star2_O3/vlenhart[P]q3bsp/main.cpp
// Function: main
int  main(int argc,char **argv)
{

  VFormatOptions option;
  char *options=NULL;
  char *fileArg = NULL;
  
  int argi=1;	// the current argument 

  if (argc>=2 && argv[argi][0]== '-') { // we have options 
	  options = argv[argi];
	  argi++;
  }	

  if ( argi >= argc )
  {
    printf("Usage: q3bsp [-options] <name>.BSP\n");
    printf("Where <name> is the name of a valid Quake3 BSP file.\n\n");
    printf("This utility will convert a Quake3 BSP into a valid\n");
    printf("polygon mesh and output the results into two seperate VRML 1.0\n");
    printf("files.  The first VRML file contains all the U/V mapping and\n");
    printf("texture mapping information for channel #1, and the second\n");
    printf("VRML file will contain all of the U/V mapping and texture names\n");
    printf("for the second U/V channel, which contains all lightmap\n");
    printf("information.  You can then directly import these files into any\n");
    printf("number of 3d editing tools, including 3d Studio Max\n");

    printf("This tool also extracts the lightmap data and saves it out as a\n");
    printf("series of .BMP files.\n\n");
    printf("OpenSourced by John W. Ratcliff on December 5, 2000\n");
    printf("Merry Christmas!\n\n");
    printf("Contact Id Software about using Quake 3 data files in and\n");
    printf("Quake 3 editing tools for commercial software development\n");
    printf("projects.\n");
    printf("Extended Options: \n");
    printf("-1		VRML 1 output\n");
    printf("-2		VRML 2 output 2 files \n");
    printf("-2me	VRML 2 output with MultiTexture extension nodes & effects\n");
    exit(1);
  }

  fileArg = argv[argi];
  if (options) {
	  
	  if (strchr(options,'2'))
			option.vrml2 = true;
	  else if (strchr(options,'1'))
			option.vrml2 = false;
	  
	  if (strchr(options,'m'))
			option.useMultiTexturing = true;

	  if (strchr(options,'M'))
			option.useMat = true;
	  
	  if (strchr(options,'l'))
			option.useLighting = true;
	  
	  if (strchr(options,'b'))
			option.useBsp = true;

	  if (strchr(options,'e'))
			option.useEffects = true;

	  if (strchr(options,'v'))
			option.verbose = true;

	  if (strchr(options,'n'))
			option.noTextureCoordinates = true;

  }	
  
  Quake3BSP q( SGET(fileArg), SGET("a") );

  VertexMesh *mesh = q.GetVertexMesh();
  

  if ( mesh )
  {
    String str = fileArg;
	
	char * del = strrchr(fileArg,'\\');
	if (del) str = (del+1); // use file name part only
	
    String name1 = str + "1";
    String name2 = str + "2";


	if (option.vrml2) { // VRML 2 style 

		if (!option.useMultiTexturing) {
		    // save VRML file using channel #1
			// save VRML file using channel #2
			name1+=".wrl";

			FILE *fph = fopen(name1.c_str(),"wb");
			fprintf(fph,"#VRML V2.0 utf8 generated by QBSP from %s\n",fileArg);

			printf("Saving U/V channel #1 to VRML2 file %s\n",name1.c_str());
			option.tex1= true;
			mesh->SaveVRML2(fph,option);
			
			q.SaveEntitiesVRML2(fph,option);
			
			fclose(fph);


			printf("Saving U/V channel #2 to VRML2 file %s.wrl\n",name2.c_str());
			name2+=".wrl";
			fph = fopen(name2.c_str(),"wb");
			fprintf(fph,"#VRML V2.0 utf8 generated by QBSP from %s\n",fileArg);
			option.tex1=false;
			option.matDefined=false;
			option.useMat = false;
			mesh->SaveVRML2(fph,option);
			fclose(fph);

		} else {
		    name1 = str;
			printf("Saving MultiTexture extended VRML file %s.wrl\n",name1.c_str());

		    String oname = name1+".wrl";
			FILE *fph = fopen(oname.c_str(),"wb");
			fprintf(fph,"#VRML V2.0 utf8 generated by Q3BSP 1.1 from %s\n",fileArg);
			fprintf(fph,"WorldInfo { title \"%s\" }\n\n",fileArg);
			
			fprintf(fph,"NavigationInfo { headlight TRUE avatarSize [0.25 1.3333 2 ]} \n");

			// write some PROTO's for effects 
			if (option.useEffects)
			{
				Fload effects("q3effects.wrl");
				if (!effects.GetData()) {
					printf("***********q3effects.wrl NOT FOUND\n");
				}
				else {
					fprintf(fph,"%s\n",(char *)effects.GetData());
					option.matDefined = true; // assume MAT is defined in this file
				}
			}
  

			option.tex1= true;
			if (option.useBsp) 
				q.SaveNodesBsp(fph,option);
			else mesh->SaveVRML2(fph,option);
			
			q.SaveEntitiesVRML2(fph,option);

			fclose(fph);
		}

	} else {	// VRML 1 style 
		printf("Saving U/V channel #1 to file %s.wrl\n",name1.c_str());
		mesh->SaveVRML(name1,true);
		printf("Saving U/V channel #2 to file %s.wrl\n",name2.c_str());
		mesh->SaveVRML(name2,false);
	}
  }
  else
  {
    printf("Failed to crunch %s\n", fileArg);
	return -1;
  }

  return 0;
  
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x378, %rsp            # imm = 0x378
leaq -0x1a0(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, (%rax)
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
leaq -0x170(%rbp), %rax
movl %ecx, (%rax)
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movb $0x0, 0x78(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rax)
movl $0x1, 0x30(%rax)
movw $0x101, 0x3f(%rax)      # imm = 0x101
movw $0x1, 0x3c(%rax)
movl $0x8, 0x38(%rax)
leaq 0xc7ee(%rip), %rcx      # 0x126b2
movq %rcx, 0x48(%rax)
leaq 0xc7e6(%rip), %rcx      # 0x126b5
movq %rcx, 0x50(%rax)
leaq 0xc7e1(%rip), %rcx      # 0x126bb
movq %rcx, 0x58(%rax)
movb $0x1, 0x3e(%rax)
leaq 0xc7e1(%rip), %rcx      # 0x126ca
movq %rcx, 0x60(%rax)
leaq 0xc802(%rip), %rcx      # 0x126f6
movq %rcx, 0x68(%rax)
movq %rcx, 0x70(%rax)
cmpl $0x2, %edi
jl 0x64e8
movq 0x8(%rsi), %rcx
xorl %r14d, %r14d
xorl %eax, %eax
cmpb $0x2d, (%rcx)
sete %al
cmoveq %rcx, %r14
incl %eax
cmpl %edi, %eax
jae 0x64e8
movl %eax, %eax
movq (%rsi,%rax,8), %rbx
testq %r14, %r14
je 0x600b
movq %r14, %rdi
movl $0x32, %esi
callq 0x2100
testq %rax, %rax
jne 0x5f5c
movq %r14, %rdi
movl $0x31, %esi
callq 0x2100
testq %rax, %rax
je 0x5f5c
movb $0x0, -0x140(%rbp)
movq %r14, %rdi
movl $0x6d, %esi
callq 0x2100
testq %rax, %rax
je 0x5f75
movb $0x1, -0x13e(%rbp)
movq %r14, %rdi
movl $0x4d, %esi
callq 0x2100
testq %rax, %rax
je 0x5f8e
movb $0x1, -0x132(%rbp)
movq %r14, %rdi
movl $0x6c, %esi
callq 0x2100
testq %rax, %rax
je 0x5fa7
movb $0x1, -0x133(%rbp)
movq %r14, %rdi
movl $0x62, %esi
callq 0x2100
testq %rax, %rax
je 0x5fc0
movb $0x1, -0x13d(%rbp)
movq %r14, %rdi
movl $0x65, %esi
callq 0x2100
testq %rax, %rax
je 0x5fd9
movb $0x1, -0x131(%rbp)
movq %r14, %rdi
movl $0x76, %esi
callq 0x2100
testq %rax, %rax
je 0x5ff2
movb $0x1, -0x13f(%rbp)
movq %r14, %rdi
movl $0x6e, %esi
callq 0x2100
testq %rax, %rax
je 0x600b
movb $0x1, -0x12f(%rbp)
callq 0x69ac
movq %rax, %rdi
movq %rbx, %rsi
callq 0x6a3e
movq %rax, -0xf0(%rbp)
callq 0x69ac
leaq 0xc98c(%rip), %rsi      # 0x129ba
movq %rax, %rdi
callq 0x6a3e
leaq -0xa8(%rbp), %rdx
movq %rax, (%rdx)
leaq -0x3a0(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x858c
movq -0x280(%rbp), %r14
testq %r14, %r14
je 0x626c
leaq -0xa8(%rbp), %rdi
leaq -0xf0(%rbp), %rdx
movq %rbx, %rsi
callq 0x69f2
movq %rbx, %rdi
movl $0x5c, %esi
callq 0x2260
testq %rax, %rax
je 0x60b8
movq %rax, %r15
incq %r15
leaq -0xa8(%rbp), %r12
movq 0x8(%r12), %r13
movq %r15, %rdi
callq 0x2120
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rdx
movq %r15, %rcx
movq %rax, %r8
callq 0x2360
leaq -0x38(%rbp), %r12
movq %r12, -0x10(%r12)
movq -0xa8(%rbp), %rsi
movq -0xa0(%rbp), %rdx
addq %rsi, %rdx
leaq -0x48(%rbp), %rdi
callq 0x6cba
leaq 0xc13b(%rip), %rsi      # 0x1221d
leaq -0x48(%rbp), %rdi
callq 0x2400
leaq -0x78(%rbp), %r13
movq %r13, -0x10(%r13)
movq -0xa8(%rbp), %rsi
movq -0xa0(%rbp), %rdx
addq %rsi, %rdx
leaq -0x88(%rbp), %rdi
callq 0x6cba
leaq 0xc2e6(%rip), %rsi      # 0x123fd
leaq -0x88(%rbp), %rdi
callq 0x2400
cmpb $0x1, -0x140(%rbp)
jne 0x6287
cmpb $0x0, -0x13e(%rbp)
je 0x62d6
leaq -0x48(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x2140
movq -0x48(%rbp), %rsi
leaq 0xc326(%rip), %rdi      # 0x1247e
xorl %eax, %eax
callq 0x2050
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
addq %rsi, %rdx
leaq -0x68(%rbp), %rdi
callq 0x6cba
leaq 0xc3be(%rip), %rsi      # 0x12540
leaq -0x68(%rbp), %rdi
callq 0x2400
movq -0x68(%rbp), %rdi
leaq 0xc12e(%rip), %rsi      # 0x122c4
callq 0x22a0
movq %rax, %r15
leaq 0xc308(%rip), %rsi      # 0x124ad
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2300
leaq 0xc324(%rip), %rsi      # 0x124dd
movq %r15, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2300
leaq 0xc32b(%rip), %rdi      # 0x124f8
movl $0x3e, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x2410
cmpb $0x1, -0x131(%rbp)
jne 0x63f1
leaq -0xb8(%rbp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0xc339(%rip), %rsi      # 0x12537
leaq 0xc33f(%rip), %rdx      # 0x12544
leaq -0xc8(%rbp), %rdi
callq 0x6d58
leaq -0xf0(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x7b2c
movq -0xc8(%rbp), %rdi
cmpq %rbx, %rdi
je 0x623f
movq -0xb8(%rbp), %rsi
incq %rsi
callq 0x2230
movq -0xe8(%rbp), %rdx
testq %rdx, %rdx
je 0x63d9
leaq 0xda52(%rip), %rsi      # 0x13ca8
movq %r15, %rdi
xorl %eax, %eax
callq 0x2300
movb $0x1, -0xf8(%rbp)
jmp 0x63e5
leaq 0xc31e(%rip), %rdi      # 0x12591
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2050
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x64b0
leaq -0x48(%rbp), %rbx
movq (%rbx), %rsi
leaq 0xc2b0(%rip), %rdi      # 0x12545
xorl %eax, %eax
callq 0x2050
movq %r14, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0xfb22
leaq -0x88(%rbp), %rbx
movq (%rbx), %rsi
leaq 0xc2ae(%rip), %rdi      # 0x1256b
xorl %eax, %eax
callq 0x2050
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xfb22
jmp 0x6462
leaq 0xc263(%rip), %rsi      # 0x12540
leaq -0x48(%rbp), %rdi
callq 0x2400
movq -0x48(%rbp), %rdi
leaq 0xbfd3(%rip), %rsi      # 0x122c4
callq 0x22a0
movq %rax, %r15
leaq 0xc0ff(%rip), %rsi      # 0x123ff
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2300
movq -0x48(%rbp), %rsi
leaq 0xc112(%rip), %rdi      # 0x1242a
xorl %eax, %eax
callq 0x2050
leaq -0x1a8(%rbp), %rdx
movb $0x1, 0x6c(%rdx)
movq %r14, %rdi
movq %r15, %rsi
callq 0xef52
leaq -0x3a0(%rbp), %rdi
leaq -0x1a8(%rbp), %rdx
movq %r15, %rsi
callq 0xa3f6
movq %r15, %rdi
callq 0x2210
leaq -0x88(%rbp), %r15
movq (%r15), %rsi
leaq 0xc0ee(%rip), %rdi      # 0x12452
xorl %eax, %eax
callq 0x2050
leaq 0xc1ce(%rip), %rsi      # 0x12540
movq %r15, %rdi
callq 0x2400
movq -0x88(%rbp), %rdi
leaq 0xbf3c(%rip), %rsi      # 0x122c4
callq 0x22a0
movq %rax, %r15
leaq 0xc068(%rip), %rsi      # 0x123ff
xorl %r12d, %r12d
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2300
leaq -0x1a8(%rbp), %rdx
movb %r12b, 0x6c(%rdx)
movb %r12b, 0xb0(%rdx)
movb %r12b, 0x76(%rdx)
movq %r14, %rdi
movq %r15, %rsi
callq 0xef52
movq %r15, %rdi
callq 0x2210
leaq -0x38(%rbp), %r12
jmp 0x6462
leaq 0xc349(%rip), %rdi      # 0x12729
callq 0x2350
leaq -0xf0(%rbp), %rdi
callq 0x7c1e
movb $0x1, -0x13c(%rbp)
cmpb $0x1, -0x13d(%rbp)
jne 0x6419
leaq -0x3a0(%rbp), %rdi
leaq -0x1a8(%rbp), %rdx
movq %r15, %rsi
callq 0xa750
jmp 0x642b
leaq -0x1a8(%rbp), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0xef52
leaq -0x3a0(%rbp), %rdi
leaq -0x1a8(%rbp), %rdx
movq %r15, %rsi
callq 0xa3f6
movq %r15, %rdi
callq 0x2210
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x6462
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2230
movq -0x88(%rbp), %rdi
cmpq %r13, %rdi
je 0x647a
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x2230
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
je 0x648f
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2230
leaq -0x98(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x64ae
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x2230
xorl %ebx, %ebx
leaq -0x3a0(%rbp), %rdi
callq 0x94b8
leaq -0x178(%rbp), %rdi
callq 0x6c5c
leaq -0x1a8(%rbp), %rdi
callq 0x6c5c
movl %ebx, %eax
addq $0x378, %rsp            # imm = 0x378
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc25d(%rip), %rdi      # 0x1274c
callq 0x2350
leaq 0xc274(%rip), %rdi      # 0x1276f
callq 0x2350
leaq 0xc29e(%rip), %rdi      # 0x127a5
callq 0x2350
leaq 0xc2c6(%rip), %rdi      # 0x127d9
callq 0x2350
leaq 0xc2f9(%rip), %rdi      # 0x12818
callq 0x2350
leaq 0xc32a(%rip), %rdi      # 0x12855
callq 0x2350
leaq 0xc359(%rip), %rdi      # 0x12890
callq 0x2350
leaq 0xc38d(%rip), %rdi      # 0x128d0
callq 0x2350
leaq 0xc3b9(%rip), %rdi      # 0x12908
callq 0x2350
leaq 0xc3ed(%rip), %rdi      # 0x12948
callq 0x2350
leaq 0xc415(%rip), %rdi      # 0x1297c
callq 0x2350
leaq 0xc449(%rip), %rdi      # 0x129bc
callq 0x2350
leaq 0xc454(%rip), %rdi      # 0x129d3
callq 0x2350
leaq 0xc47c(%rip), %rdi      # 0x12a07
callq 0x2350
leaq 0xc482(%rip), %rdi      # 0x12a19
callq 0x2350
leaq 0xc4b0(%rip), %rdi      # 0x12a53
callq 0x2350
leaq 0xc4de(%rip), %rdi      # 0x12a8d
callq 0x2350
leaq 0xc4dc(%rip), %rdi      # 0x12a97
callq 0x2350
leaq 0xc4e3(%rip), %rdi      # 0x12aaa
callq 0x2350
leaq 0xc4e9(%rip), %rdi      # 0x12abc
callq 0x2350
leaq 0xc4f8(%rip), %rdi      # 0x12ad7
callq 0x2350
movl $0x1, %edi
callq 0x22c0
movq %rax, %r14
movq -0xc8(%rbp), %rdi
cmpq %rbx, %rdi
je 0x6617
movq -0xb8(%rbp), %rsi
incq %rsi
callq 0x2230
jmp 0x6617
jmp 0x6614
jmp 0x6614
jmp 0x6655
movq %rax, %r14
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x6658
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2230
jmp 0x6658
jmp 0x664b
jmp 0x6655
jmp 0x6655
movq %rax, %r14
jmp 0x6670
movq %rax, %r14
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
jne 0x667d
jmp 0x6689
movq %rax, %r14
jmp 0x6689
movq %rax, %r14
jmp 0x66a8
movq %rax, %r14
movq -0x88(%rbp), %rdi
cmpq %r13, %rdi
je 0x6670
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x2230
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x6689
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2230
leaq -0x98(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x66a8
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x2230
leaq -0x3a0(%rbp), %rdi
callq 0x94b8
jmp 0x66bb
jmp 0x66b8
movq %rax, %r14
leaq -0x178(%rbp), %rdi
callq 0x6c5c
leaq -0x1a8(%rbp), %rdi
callq 0x6c5c
movq %r14, %rdi
callq 0x23c0