// File: tomfunkhouser[P]gaps/apps/scn2img/scn2img.cpp
// Function: DrawSceneWithOpenGL(gaps::R3Camera const&, gaps::R3Scene*, int, int)
static int
DrawSceneWithOpenGL(const R3Camera& camera, R3Scene *scene, int color_scheme, RNBoolean omit_objects = FALSE)
{
  // Clear window
  glClearColor(background.R(), background.G(), background.B(), 1.0);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  // Check color scheme
  if (color_scheme == RGB_COLOR_SCHEME) {
    // Draw scene
    glEnable(GL_LIGHTING);
    scene->LoadLights(headlight);
    DrawRootWithOpenGL(camera, scene, color_scheme, omit_objects);
  }
  else if (color_scheme == ALBEDO_COLOR_SCHEME) {
    // Load ambient light
    static GLfloat ambient[4];
    ambient[0] = 1;
    ambient[1] = 1;
    ambient[2] = 1;
    ambient[3] = 1;
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);

    // Copy diffuse reflections into ambient reflections
    RNRgb *saved_ambients = NULL;
    if (scene->NBrdfs() > 0) {
      saved_ambients = new RNRgb [ scene->NBrdfs() ];
      for (int i = 0; i < scene->NBrdfs(); i++) {
        R3Brdf *brdf = scene->Brdf(i);
        saved_ambients[i] = brdf->Ambient();
        brdf->SetAmbient(brdf->Diffuse());
      }
    }

    // Disable lights
    for (int i = 0; i < 8; i++) {
      glDisable(GL_LIGHT0 + i);
    }

    // Draw scene with only ambient light
    RNLoadRgb(1.0, 1.0, 1.0);
    glEnable(GL_LIGHTING);
    DrawRootWithOpenGL(camera, scene, color_scheme, omit_objects);

    // Restore lights
    if (headlight) {
      glEnable(GL_LIGHT0);
    }

    // Restore ambient reflections
    if (saved_ambients) {
      for (int i = 0; i < scene->NBrdfs(); i++) {
        R3Brdf *brdf = scene->Brdf(i);
        brdf->SetAmbient(saved_ambients[i]);
      }
      delete [] saved_ambients;
    }
  }
  else {
    // Draw scene
    glDisable(GL_LIGHTING);
    RNLoadRgb(1.0, 1.0, 1.0);
    DrawRootWithOpenGL(camera, scene, color_scheme, omit_objects);
  }

  // Return success
  return 1;
}

// Assembly:
subq $0xa8, %rsp
movq %rdi, 0xa0(%rsp)
movq %rsi, 0x98(%rsp)
movl %edx, 0x94(%rsp)
movl %ecx, 0x90(%rsp)
leaq 0x1f4864(%rip), %rdi    # 0x21eb90
callq 0x2efa0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x5c(%rsp)
leaq 0x1f484e(%rip), %rdi    # 0x21eb90
callq 0x2efb0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x58(%rsp)
leaq 0x1f4838(%rip), %rdi    # 0x21eb90
callq 0x2efc0
movss 0x58(%rsp), %xmm1
movaps %xmm0, %xmm2
movss 0x5c(%rsp), %xmm0
cvtsd2ss %xmm2, %xmm2
movss 0x182d38(%rip), %xmm3   # 0x1ad0b0
callq 0x23fb0
movl $0x4100, %edi           # imm = 0x4100
callq 0x23210
cmpl $0x1, 0x94(%rsp)
jne 0x2a3db
movl $0xb50, %edi            # imm = 0xB50
callq 0x238e0
movq 0x98(%rsp), %rdi
movl 0x1f4837(%rip), %esi    # 0x21ebe0
movl $0x7, %edx
callq 0x32090
movq 0xa0(%rsp), %rdi
movq 0x98(%rsp), %rsi
movl 0x94(%rsp), %edx
movl 0x90(%rsp), %ecx
callq 0x2c7b0
jmp 0x2a714
cmpl $0x9, 0x94(%rsp)
jne 0x2a6d2
movss 0x182cbf(%rip), %xmm0   # 0x1ad0b0
movss %xmm0, 0x1f4867(%rip)   # 0x21ec60
movss 0x182caf(%rip), %xmm0   # 0x1ad0b0
movss %xmm0, 0x1f485b(%rip)   # 0x21ec64
movss 0x182c9f(%rip), %xmm0   # 0x1ad0b0
movss %xmm0, 0x1f484f(%rip)   # 0x21ec68
movss 0x182c8f(%rip), %xmm0   # 0x1ad0b0
movss %xmm0, 0x1f4843(%rip)   # 0x21ec6c
movl $0xb53, %edi            # imm = 0xB53
leaq 0x1f482b(%rip), %rsi    # 0x21ec60
callq 0x243b0
movq $0x0, 0x88(%rsp)
movq 0x98(%rsp), %rdi
callq 0x2efd0
cmpl $0x0, %eax
jle 0x2a5bc
movq 0x98(%rsp), %rdi
callq 0x2efd0
cltq
movq %rax, 0x48(%rsp)
movl $0x18, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, 0x40(%rsp)
callq 0x23040
movq %rax, %rcx
movq 0x48(%rsp), %rax
movq %rcx, 0x50(%rsp)
cmpq $0x0, %rax
je 0x2a4f0
movq 0x50(%rsp), %rax
movq 0x48(%rsp), %rcx
imulq $0x18, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x10e890
jmp 0x2a4d8
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %rax
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, 0x38(%rsp)
jne 0x2a4c7
movq 0x50(%rsp), %rax
movq %rax, 0x88(%rsp)
movl $0x0, 0x78(%rsp)
movl 0x78(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0x98(%rsp), %rdi
callq 0x2efd0
movl %eax, %ecx
movl 0x24(%rsp), %eax
cmpl %ecx, %eax
jge 0x2a5ba
movq 0x98(%rsp), %rdi
movl 0x78(%rsp), %esi
callq 0x2eff0
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rdi
callq 0x2f020
movq %rax, %rsi
movq 0x88(%rsp), %rdi
movslq 0x78(%rsp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x10ea60
movq 0x70(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x70(%rsp), %rdi
callq 0x2eea0
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x685a0
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x2a505
movq 0x40(%rsp), %rsi
movq 0x50(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
callq 0x24460
jmp 0x2a721
jmp 0x2a5bc
movl $0x0, 0x6c(%rsp)
cmpl $0x8, 0x6c(%rsp)
jge 0x2a5e7
movl 0x6c(%rsp), %edi
addl $0x4000, %edi           # imm = 0x4000
callq 0x23220
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x2a5c4
movsd 0x182a59(%rip), %xmm2   # 0x1ad048
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x2f030
movl $0xb50, %edi            # imm = 0xB50
callq 0x238e0
movq 0xa0(%rsp), %rdi
movq 0x98(%rsp), %rsi
movl 0x94(%rsp), %edx
movl 0x90(%rsp), %ecx
callq 0x2c7b0
cmpl $0x0, 0x1f45b2(%rip)    # 0x21ebe0
je 0x2a63a
movl $0x4000, %edi           # imm = 0x4000
callq 0x238e0
cmpq $0x0, 0x88(%rsp)
je 0x2a6d0
movl $0x0, 0x68(%rsp)
movl 0x68(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x98(%rsp), %rdi
callq 0x2efd0
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x2a6b1
movq 0x98(%rsp), %rdi
movl 0x68(%rsp), %esi
callq 0x2eff0
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rdi
movq 0x88(%rsp), %rsi
movslq 0x68(%rsp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rsi
callq 0x685a0
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x2a651
movq 0x88(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x2a6ce
movq 0x8(%rsp), %rdi
callq 0x23d80
jmp 0x2a6d0
jmp 0x2a712
movl $0xb50, %edi            # imm = 0xB50
callq 0x23220
movsd 0x182964(%rip), %xmm2   # 0x1ad048
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x2f030
movq 0xa0(%rsp), %rdi
movq 0x98(%rsp), %rsi
movl 0x94(%rsp), %edx
movl 0x90(%rsp), %ecx
callq 0x2c7b0
jmp 0x2a714
movl $0x1, %eax
addq $0xa8, %rsp
retq
movq 0x80(%rsp), %rdi
callq 0x24270
nop