// File: /workspace/github_c_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x380, %rsp            # imm = 0x380
leaq -0x160(%rbp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq -0x130(%rbp), %rax
andl $0x0, (%rax)
andq $0x0, 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 0xb4cc(%rip), %rcx      # 0x106b2
movq %rcx, 0x48(%rax)
leaq 0xb4c4(%rip), %rcx      # 0x106b5
movq %rcx, 0x50(%rax)
leaq 0xb4bf(%rip), %rcx      # 0x106bb
movq %rcx, 0x58(%rax)
movb $0x1, 0x3e(%rax)
leaq 0xb4bf(%rip), %rcx      # 0x106ca
movq %rcx, 0x60(%rax)
leaq 0xb4e0(%rip), %rcx      # 0x106f6
movq %rcx, 0x68(%rax)
movq %rcx, 0x70(%rax)
cmpl $0x2, %edi
jl 0x570a
movq 0x8(%rsi), %rcx
xorl %r14d, %r14d
xorl %eax, %eax
cmpb $0x2d, (%rcx)
sete %al
cmoveq %rcx, %r14
incl %eax
cmpl %edi, %eax
jae 0x570a
movl %eax, %eax
movq (%rsi,%rax,8), %rbx
testq %r14, %r14
je 0x531b
pushq $0x32
popq %rsi
movq %r14, %rdi
callq 0x20f0
testq %rax, %rax
jne 0x527a
pushq $0x31
popq %rsi
movq %r14, %rdi
callq 0x20f0
testq %rax, %rax
je 0x527a
movb $0x0, -0x100(%rbp)
pushq $0x6d
popq %rsi
movq %r14, %rdi
callq 0x20f0
testq %rax, %rax
je 0x5291
movb $0x1, -0xfe(%rbp)
pushq $0x4d
popq %rsi
movq %r14, %rdi
callq 0x20f0
testq %rax, %rax
je 0x52a8
movb $0x1, -0xf2(%rbp)
pushq $0x6c
popq %rsi
movq %r14, %rdi
callq 0x20f0
testq %rax, %rax
je 0x52bf
movb $0x1, -0xf3(%rbp)
pushq $0x62
popq %rsi
movq %r14, %rdi
callq 0x20f0
testq %rax, %rax
je 0x52d6
movb $0x1, -0xfd(%rbp)
pushq $0x65
popq %rsi
movq %r14, %rdi
callq 0x20f0
testq %rax, %rax
je 0x52ed
movb $0x1, -0xf1(%rbp)
pushq $0x76
popq %rsi
movq %r14, %rdi
callq 0x20f0
testq %rax, %rax
je 0x5304
movb $0x1, -0xff(%rbp)
pushq $0x6e
popq %rsi
movq %r14, %rdi
callq 0x20f0
testq %rax, %rax
je 0x531b
movb $0x1, -0xef(%rbp)
callq 0x5aca
movq %rax, %rdi
movq %rbx, %rsi
callq 0x5bae
movq %rax, -0xb0(%rbp)
callq 0x5aca
leaq 0xb67c(%rip), %rsi      # 0x109ba
movq %rax, %rdi
callq 0x5bae
leaq -0x68(%rbp), %rdx
movq %rax, (%rdx)
leaq -0x3a0(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x76d4
movq -0x280(%rbp), %r14
testq %r14, %r14
je 0x54e2
leaq -0x68(%rbp), %rdi
leaq -0xb0(%rbp), %rdx
movq %rbx, %rsi
callq 0x5b0c
pushq $0x5c
popq %rsi
movq %rbx, %rdi
callq 0x2260
testq %rax, %rax
je 0x53a2
incq %rax
leaq -0x68(%rbp), %rdi
movq %rax, %rsi
callq 0x2430
leaq 0xae74(%rip), %rdx      # 0x1021d
leaq -0x48(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x5b47
leaq 0xb040(%rip), %rdx      # 0x103fd
leaq -0x88(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x5b47
cmpb $0x1, -0x100(%rbp)
jne 0x54fb
cmpb $0x0, -0xfe(%rbp)
je 0x5548
leaq -0x48(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x2140
leaq -0x48(%rbp), %r15
movq (%r15), %rsi
leaq 0xb07c(%rip), %rdi      # 0x1047e
xorl %eax, %eax
callq 0x2050
leaq 0xb130(%rip), %rdx      # 0x10540
leaq -0x1a8(%rbp), %rdi
movq %r15, %rsi
callq 0x5b47
movq -0x1a8(%rbp), %rdi
leaq 0xae97(%rip), %rsi      # 0x102c4
callq 0x22a0
movq %rax, %r15
leaq 0xb071(%rip), %rsi      # 0x104ad
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2330
leaq 0xb08d(%rip), %rsi      # 0x104dd
movq %r15, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2330
leaq 0xb094(%rip), %rdi      # 0x104f8
pushq $0x3e
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x2440
cmpb $0x1, -0xf1(%rbp)
jne 0x565c
leaq 0xb0b1(%rip), %rsi      # 0x10537
leaq -0x188(%rbp), %rdi
leaq -0x21(%rbp), %rdx
callq 0x5b0c
leaq -0xb0(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0x6bac
leaq -0x188(%rbp), %rdi
callq 0x2120
movq -0xa8(%rbp), %rdx
testq %rdx, %rdx
je 0x5644
leaq 0xc7f0(%rip), %rsi      # 0x11cbc
movq %r15, %rdi
xorl %eax, %eax
callq 0x2330
movb $0x1, -0xb8(%rbp)
jmp 0x5650
leaq 0xb0a8(%rip), %rdi      # 0x10591
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2050
pushq $-0x1
popq %rbx
jmp 0x56e0
leaq -0x48(%rbp), %rbx
movq (%rbx), %rsi
leaq 0xb03c(%rip), %rdi      # 0x10545
xorl %eax, %eax
callq 0x2050
pushq $0x1
popq %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0xe5be
leaq -0x88(%rbp), %rbx
movq (%rbx), %rsi
leaq 0xb03c(%rip), %rdi      # 0x1056b
xorl %eax, %eax
callq 0x2050
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xe5be
jmp 0x56c0
leaq 0xaff1(%rip), %rsi      # 0x10540
leaq -0x48(%rbp), %rdi
callq 0x2420
movq -0x48(%rbp), %rdi
leaq 0xad61(%rip), %rsi      # 0x102c4
callq 0x22a0
movq %rax, %r15
leaq 0xae8d(%rip), %rsi      # 0x103ff
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2330
movq -0x48(%rbp), %rsi
leaq 0xaea0(%rip), %rdi      # 0x1042a
xorl %eax, %eax
callq 0x2050
leaq -0x168(%rbp), %rdx
movb $0x1, 0x6c(%rdx)
movq %r14, %rdi
movq %r15, %rsi
callq 0xdb0e
leaq -0x3a0(%rbp), %rdi
leaq -0x168(%rbp), %rdx
movq %r15, %rsi
callq 0x9162
movq %r15, %rdi
callq 0x2210
leaq -0x88(%rbp), %r15
movq (%r15), %rsi
leaq 0xae7c(%rip), %rdi      # 0x10452
xorl %eax, %eax
callq 0x2050
leaq 0xaf5c(%rip), %rsi      # 0x10540
movq %r15, %rdi
callq 0x2420
movq -0x88(%rbp), %rdi
leaq 0xacca(%rip), %rsi      # 0x102c4
callq 0x22a0
movq %rax, %r15
leaq 0xadf6(%rip), %rsi      # 0x103ff
xorl %r12d, %r12d
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2330
leaq -0x168(%rbp), %rdx
movb %r12b, 0x6c(%rdx)
movb %r12b, 0xb0(%rdx)
movb %r12b, 0x76(%rdx)
movq %r14, %rdi
movq %r15, %rsi
callq 0xdb0e
movq %r15, %rdi
callq 0x2210
jmp 0x56c0
leaq 0xb0de(%rip), %rdi      # 0x10729
callq 0x2380
leaq -0xb0(%rbp), %rdi
callq 0x6c92
movb $0x1, -0xfc(%rbp)
cmpb $0x1, -0xfd(%rbp)
jne 0x5684
leaq -0x3a0(%rbp), %rdi
leaq -0x168(%rbp), %rdx
movq %r15, %rsi
callq 0x94a2
jmp 0x5696
leaq -0x168(%rbp), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0xdb0e
leaq -0x3a0(%rbp), %rdi
leaq -0x168(%rbp), %rdx
movq %r15, %rsi
callq 0x9162
movq %r15, %rdi
callq 0x2210
leaq -0x1a8(%rbp), %rdi
callq 0x2120
leaq -0x88(%rbp), %rdi
callq 0x2120
leaq -0x48(%rbp), %rdi
callq 0x2120
leaq -0x68(%rbp), %rdi
callq 0x2120
xorl %ebx, %ebx
leaq -0x3a0(%rbp), %rdi
callq 0x83c2
leaq -0x168(%rbp), %rdi
callq 0x5b80
movl %ebx, %eax
addq $0x380, %rsp            # imm = 0x380
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb03b(%rip), %rdi      # 0x1074c
callq 0x2380
leaq 0xb052(%rip), %rdi      # 0x1076f
callq 0x2380
leaq 0xb07c(%rip), %rdi      # 0x107a5
callq 0x2380
leaq 0xb0a4(%rip), %rdi      # 0x107d9
callq 0x2380
leaq 0xb0d7(%rip), %rdi      # 0x10818
callq 0x2380
leaq 0xb108(%rip), %rdi      # 0x10855
callq 0x2380
leaq 0xb137(%rip), %rdi      # 0x10890
callq 0x2380
leaq 0xb16b(%rip), %rdi      # 0x108d0
callq 0x2380
leaq 0xb197(%rip), %rdi      # 0x10908
callq 0x2380
leaq 0xb1cb(%rip), %rdi      # 0x10948
callq 0x2380
leaq 0xb1f3(%rip), %rdi      # 0x1097c
callq 0x2380
leaq 0xb227(%rip), %rdi      # 0x109bc
callq 0x2380
leaq 0xb232(%rip), %rdi      # 0x109d3
callq 0x2380
leaq 0xb25a(%rip), %rdi      # 0x10a07
callq 0x2380
leaq 0xb260(%rip), %rdi      # 0x10a19
callq 0x2380
leaq 0xb28e(%rip), %rdi      # 0x10a53
callq 0x2380
leaq 0xb2bc(%rip), %rdi      # 0x10a8d
callq 0x2380
leaq 0xb2ba(%rip), %rdi      # 0x10a97
callq 0x2380
leaq 0xb2c1(%rip), %rdi      # 0x10aaa
callq 0x2380
leaq 0xb2c7(%rip), %rdi      # 0x10abc
callq 0x2380
leaq 0xb2d6(%rip), %rdi      # 0x10ad7
callq 0x2380
pushq $0x1
popq %rdi
callq 0x22c0
movq %rax, %rbx
leaq -0x188(%rbp), %rdi
callq 0x2120
jmp 0x5826
jmp 0x5823
jmp 0x5847
movq %rax, %rbx
leaq -0x1a8(%rbp), %rdi
callq 0x2120
jmp 0x584a
jmp 0x583d
jmp 0x5847
movq %rax, %rbx
jmp 0x5856
movq %rax, %rbx
jmp 0x585f
movq %rax, %rbx
jmp 0x5868
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x2120
leaq -0x48(%rbp), %rdi
callq 0x2120
leaq -0x68(%rbp), %rdi
callq 0x2120
leaq -0x3a0(%rbp), %rdi
callq 0x83c2
jmp 0x587b
jmp 0x5878
movq %rax, %rbx
leaq -0x168(%rbp), %rdi
callq 0x5b80
movq %rbx, %rdi
callq 0x23e0