// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/apps/scn2img/scn2img.cpp
// Function: DrawNodeWithOpenGL(gaps::R3Camera const&, gaps::R3Scene*, gaps::R3SceneNode*, int, int)
static void 
DrawNodeWithOpenGL(const R3Camera& camera, R3Scene *scene, R3SceneNode *node, int color_scheme, RNBoolean omit_objects = FALSE)
{
  // Check if should omit object
  if (omit_objects && node->Name() && !strncmp(node->Name(), "Object#", 7)) return;

  // Check if has elements and/or references
  if (node->NChildren() > 0) {
    // Recurse to children
    for (int i = 0; i < node->NChildren(); i++) {
      R3SceneNode *child = node->Child(i);
      DrawNodeWithOpenGL(camera, scene, child, color_scheme, omit_objects);
    }
  }
  else {
    // Check color scheme
    if ((color_scheme == RGB_COLOR_SCHEME) || (color_scheme == ALBEDO_COLOR_SCHEME)) {
      // Load lights for node
      if (color_scheme == RGB_COLOR_SCHEME) {
        int max_lights = 8 - headlight;
        if (scene->NLights() > max_lights) {
          scene->LoadLights(node->BBox(), headlight);
        }
      }

      // Draw node with shading
      node->Draw(R3_DEFAULT_DRAW_FLAGS);
    }
    else if ((color_scheme == NODE_COLOR_SCHEME) || (color_scheme == CATEGORY_COLOR_SCHEME) || (color_scheme == ROOM_SURFACE_COLOR_SCHEME)) {
      // Draw integer values per node
      if (color_scheme == NODE_COLOR_SCHEME) {
        LoadInteger(node->SceneIndex() + 1);
      }
      else if (color_scheme == CATEGORY_COLOR_SCHEME) {
        const char *model_index = NULL;
        R3SceneNode *ancestor = node;
        while (!model_index && ancestor) { model_index = ancestor->Info("index"); ancestor = ancestor->Parent(); }
        if (model_index) LoadInteger(atoi(model_index));
        else LoadInteger(0);
      }
      else if (color_scheme == ROOM_SURFACE_COLOR_SCHEME) {
        if (!node->Name()) LoadInteger(0);
        else if (!strncmp(node->Name(), "Wall#", 5)) LoadInteger(1);
        else if (!strncmp(node->Name(), "WallInside#", 11)) LoadInteger(1);
        else if (!strncmp(node->Name(), "WallOutside#", 12)) LoadInteger(1);
        else if (!strncmp(node->Name(), "Ceiling#", 8)) LoadInteger(2);
        else if (!strncmp(node->Name(), "Floor#", 6)) LoadInteger(3);
        else LoadInteger(0);
      }
      node->Draw(R3_SURFACES_DRAW_FLAG);
    }
    else if (color_scheme == MATERIAL_COLOR_SCHEME) {
      // Draw integer values per element
      for (int i = 0; i < node->NElements(); i++) {
        R3SceneElement *element = node->Element(i);
        R3Material *material = element->Material();
        if (!material) LoadInteger(0);
        else LoadInteger(material->SceneIndex() + 1);
        element->Draw(R3_SURFACES_DRAW_FLAG);
      }
    }
    else if (color_scheme == BRDF_COLOR_SCHEME) {
      // Draw color values per element (NOTE: THIS IGNORES TEXTURE)
      for (int i = 0; i < node->NElements(); i++) {
        R3SceneElement *element = node->Element(i);
        const R3Material *material = element->Material();
        const R3Brdf *brdf = (material) ? material->Brdf() : NULL;
        if (!brdf) brdf = &R3default_brdf;
        RNScalar kd = brdf->Diffuse().Luminance();
        RNScalar ks = brdf->Specular().Luminance();
        RNScalar kt = brdf->Transmission().Luminance();
        RNLoadRgb(kd, ks, kt);
        element->Draw(R3_SURFACES_DRAW_FLAG);
      }
    }
    else if (color_scheme == FACE_COLOR_SCHEME) {
      // Draw integer values per triangle
      int face_count = 0;
      for (int i = 0; i < node->NElements(); i++) {
        R3SceneElement *element = node->Element(i);
        for (int j = 0; j < element->NShapes(); j++) {
          R3Shape *shape = element->Shape(j);
          if (shape->ClassID() == R3TriangleArray::CLASS_ID()) {
            R3TriangleArray *triangles = (R3TriangleArray *) shape;
            for (int k = 0; k < triangles->NTriangles(); k++) {
              R3Triangle *triangle = triangles->Triangle(k);
              LoadInteger(face_count + 1);
              triangle->Draw(R3_SURFACES_DRAW_FLAG);
              face_count++;
            }
          }
        }
      }
    }    
    else if ((color_scheme == ANGLE_COLOR_SCHEME) || 
      (color_scheme == XNORMAL_COLOR_SCHEME) || (color_scheme == YNORMAL_COLOR_SCHEME) || (color_scheme == ZNORMAL_COLOR_SCHEME)) {
      // Draw integer values per triangle
      for (int i = 0; i < node->NElements(); i++) {
        R3SceneElement *element = node->Element(i);
        for (int j = 0; j < element->NShapes(); j++) {
          R3Shape *shape = element->Shape(j);
          if (shape->ClassID() == R3TriangleArray::CLASS_ID()) {
            R3TriangleArray *triangles = (R3TriangleArray *) shape;
            for (int k = 0; k < triangles->NTriangles(); k++) {
              R3Triangle *triangle = triangles->Triangle(k);
              R3Vector normal = triangle->Normal();
              if (R3SignedDistance(triangle->Plane(), camera.Origin()) < 0) normal.Flip();
              RNScalar value = 0;
              if (color_scheme == ANGLE_COLOR_SCHEME) value = (RN_PI - acos(normal.Y())) / RN_PI;
              else value = 0.5*normal[color_scheme - XNORMAL_COLOR_SCHEME] + 0.5;
              LoadInteger((int) (65535 * value));
              triangle->Draw(R3_SURFACES_DRAW_FLAG);
            }
          }
        }
      }
    }
    else if ((color_scheme == DEPTH_COLOR_SCHEME) || (color_scheme == NDOTV_COLOR_SCHEME) || (color_scheme == HEIGHT_COLOR_SCHEME)) {
      // Draw scalar values interpolated between triangle vertices
      RNScalar ground_y = (color_scheme == HEIGHT_COLOR_SCHEME) ? EstimateGroundY(camera, scene) : 0;
      RNGrfxBegin(RN_GRFX_TRIANGLES);
      for (int i = 0; i < node->NElements(); i++) {
        R3SceneElement *element = node->Element(i);
        for (int j = 0; j < element->NShapes(); j++) {
          R3Shape *shape = element->Shape(j);
          if (shape->ClassID() == R3TriangleArray::CLASS_ID()) {
            R3TriangleArray *triangles = (R3TriangleArray *) shape;
            for (int k = 0; k < triangles->NTriangles(); k++) {
              R3Triangle *triangle = triangles->Triangle(k);
              for (int m = 0; m < 3; m++) {
                R3TriangleVertex *vertex = triangle->Vertex(m);
                const R3Point& position = vertex->Position();
                if (color_scheme == HEIGHT_COLOR_SCHEME) LoadScalar(position.Y() - ground_y, 6.5535);
                else if (color_scheme == DEPTH_COLOR_SCHEME) LoadScalar((position - camera.Origin()).Dot(camera.Towards()), 6.5535);
                else if (color_scheme == NDOTV_COLOR_SCHEME) {
                  R3Vector v = camera.Origin() - position; v.Normalize();
                  R3Vector normal = (vertex->Flags()[R3_VERTEX_NORMALS_DRAW_FLAG]) ? vertex->Normal() : triangle->Normal();
                  LoadScalar(fabs(normal.Dot(v)), 1.0);
                }
                R3LoadPoint(position);
              }
            }
          }
        }
      }
      RNGrfxEnd();
    }
    else if (color_scheme == VRGB_COLOR_SCHEME) {
      // Draw node with vertex colors
      node->Draw(R3_SURFACES_DRAW_FLAG | R3_VERTEX_COLORS_DRAW_FLAG);
    }
    else {
      // Draw without setting colors
      for (int i = 0; i < node->NElements(); i++) {
        R3SceneElement *element = node->Element(i);
        element->Draw(R3_SURFACES_DRAW_FLAG);
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %r8d, %ebp
movl %ecx, %r14d
movq %rdx, %r13
movq %rsi, %rbx
movq %rdi, 0x8(%rsp)
testl %r8d, %r8d
je 0x293a8
movq 0x1a0(%r13), %rdi
testq %rdi, %rdi
je 0x293a8
leaq 0xabf70(%rip), %rsi     # 0xd5308
pushq $0x7
popq %rdx
callq 0x223d0
testl %eax, %eax
je 0x29a82
movl 0x2c(%r13), %eax
testl %eax, %eax
jle 0x293e2
xorl %r15d, %r15d
cltq
cmpq %rax, %r15
jge 0x29a82
movq 0x20(%r13), %rax
movq (%rax,%r15,8), %rdx
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
movl %r14d, %ecx
movl %ebp, %r8d
callq 0x2935e
incq %r15
movl 0x2c(%r13), %eax
jmp 0x293b3
cmpl $0x9, %r14d
je 0x2941c
cmpl $0x1, %r14d
jne 0x2943c
movzbl 0x11f4bb(%rip), %eax    # 0x1488b0
pushq $0x8
popq %rcx
subl %eax, %ecx
cmpl %ecx, 0x24(%rbx)
jle 0x2941c
movq %r13, %rdi
callq 0x3ae92
movzbl 0x11f4a2(%rip), %edx    # 0x1488b0
pushq $0x7
popq %rcx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2abb6
movl $0x772, %esi            # imm = 0x772
movq %r13, %rdi
xorl %edx, %edx
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3b8c0
leal -0xd(%r14), %eax
cmpl $0x2, %eax
ja 0x294f9
cmpl $0xd, %r14d
je 0x295cf
cmpl $0xf, %r14d
jne 0x295da
movq 0x1a0(%r13), %r14
testq %r14, %r14
je 0x2988f
leaq 0xabe9c(%rip), %rsi     # 0xd5310
pushq $0x5
popq %rdx
movq %r14, %rdi
callq 0x223d0
pushq $0x1
popq %rbx
testl %eax, %eax
je 0x29891
leaq 0xabe85(%rip), %rsi     # 0xd5316
pushq $0xb
popq %rdx
movq %r14, %rdi
callq 0x223d0
testl %eax, %eax
je 0x29891
leaq 0xabe77(%rip), %rsi     # 0xd5322
pushq $0xc
popq %rdx
movq %r14, %rdi
callq 0x223d0
testl %eax, %eax
je 0x29891
leaq 0xabe6a(%rip), %rsi     # 0xd532f
pushq $0x8
popq %rdx
movq %r14, %rdi
callq 0x223d0
testl %eax, %eax
je 0x29aba
leaq 0xabe59(%rip), %rsi     # 0xd5338
pushq $0x6
popq %rdx
movq %r14, %rdi
callq 0x223d0
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leal (%rcx,%rcx,2), %ebx
jmp 0x29891
cmpl $0xa, %r14d
je 0x2965d
cmpl $0xb, %r14d
je 0x29619
cmpl $0xc, %r14d
movq %r13, 0x20(%rsp)
jne 0x29733
xorl %ecx, %ecx
pushq $0x2
popq %rbx
xorl %r15d, %r15d
movslq 0x3c(%r13), %rax
cmpq %rax, %rcx
jge 0x29a82
movq 0x30(%r13), %rax
movq %rcx, 0x18(%rsp)
movq (%rax,%rcx,8), %r12
xorl %r13d, %r13d
movq %r12, 0x8(%rsp)
movslq 0x1c(%r12), %rax
cmpq %rax, %r13
jge 0x295bd
movq 0x10(%r12), %rax
movq (%rax,%r13,8), %r14
movq (%r14), %rax
movq %r14, %rdi
callq *0xb8(%rax)
movl %eax, %ebp
callq 0x617ec
cmpl %eax, %ebp
jne 0x295b8
movl %r15d, 0x10(%rsp)
movl %r15d, %ebp
xorl %r12d, %r12d
movslq 0x24(%r14), %rax
cmpq %rax, %r12
jge 0x295ab
movq 0x18(%r14), %rax
movq (%rax,%r12,8), %r15
leal (%r12,%rbp), %edi
incl %edi
callq 0x29ac2
movq (%r15), %rax
movq %r15, %rdi
movq %rbx, %rsi
callq *0xa8(%rax)
incq %r12
jmp 0x2957b
movl 0x10(%rsp), %r15d
addl %r12d, %r15d
movq 0x8(%rsp), %r12
incq %r13
jmp 0x29546
movq 0x18(%rsp), %rcx
incq %rcx
movq 0x20(%rsp), %r13
jmp 0x29524
movl 0x8(%r13), %ebx
incl %ebx
jmp 0x29891
xorl %eax, %eax
leaq 0xbb2a6(%rip), %rbx     # 0xe4889
movq %r13, %r14
testq %r14, %r14
je 0x29601
testq %rax, %rax
jne 0x29601
movq %r14, %rdi
movq %rbx, %rsi
callq 0x29ba8
movq 0x10(%r14), %r14
jmp 0x295e6
testq %rax, %rax
je 0x2988f
movq %rax, %rdi
callq 0x22ed0
movl %eax, %ebx
jmp 0x29891
xorl %r15d, %r15d
pushq $0x2
popq %rbx
movslq 0x3c(%r13), %rax
cmpq %rax, %r15
jge 0x29a82
movq 0x30(%r13), %rax
movq (%rax,%r15,8), %r14
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x29644
movl 0x8(%rax), %edi
incl %edi
jmp 0x29646
xorl %edi, %edi
callq 0x29ac2
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x3c262
incq %r15
jmp 0x2961f
xorl %r15d, %r15d
leaq 0x11fc19(%rip), %r12    # 0x149280
pushq $0x2
popq %rbx
movslq 0x3c(%r13), %rax
cmpq %rax, %r15
jge 0x29a82
movq 0x30(%r13), %rax
movq (%rax,%r15,8), %r14
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x2968e
movq 0x18(%rax), %rax
jmp 0x29690
xorl %eax, %eax
testq %rax, %rax
jne 0x29698
movq %r12, %rax
movsd 0x40(%rax), %xmm0
movsd 0xab9ab(%rip), %xmm3    # 0xd5050
mulsd %xmm3, %xmm0
movsd 0x38(%rax), %xmm1
movsd 0xab9a2(%rip), %xmm4    # 0xd5058
mulsd %xmm4, %xmm1
addsd %xmm0, %xmm1
movsd 0x48(%rax), %xmm0
movsd 0xab995(%rip), %xmm5    # 0xd5060
mulsd %xmm5, %xmm0
addsd %xmm1, %xmm0
movsd 0x58(%rax), %xmm1
mulsd %xmm3, %xmm1
movsd 0x50(%rax), %xmm2
mulsd %xmm4, %xmm2
addsd %xmm1, %xmm2
movsd 0x60(%rax), %xmm1
mulsd %xmm5, %xmm1
addsd %xmm2, %xmm1
movsd 0x70(%rax), %xmm2
mulsd %xmm3, %xmm2
movsd 0x68(%rax), %xmm3
mulsd %xmm4, %xmm3
addsd %xmm2, %xmm3
movsd 0x78(%rax), %xmm2
mulsd %xmm5, %xmm2
addsd %xmm3, %xmm2
callq 0x22e70
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x3c262
incq %r15
jmp 0x2966a
movl %r14d, %eax
andl $-0x4, %eax
cmpl $0x4, %eax
jne 0x298a0
leal -0x5(%r14), %eax
cltq
movq %rax, 0x10(%rsp)
leaq 0xaffe4(%rip), %rax     # 0xd9738
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
xorl %ecx, %ecx
leaq 0x58(%rsp), %rbx
movslq 0x3c(%r13), %rax
cmpq %rax, %rcx
jge 0x29a82
movq 0x30(%r13), %rax
movq %rcx, 0x28(%rsp)
movq (%rax,%rcx,8), %rcx
xorl %edx, %edx
movq %rcx, 0x30(%rsp)
movslq 0x1c(%rcx), %rax
cmpq %rax, %rdx
jge 0x29882
movq 0x10(%rcx), %rax
movq %rdx, 0x38(%rsp)
movq (%rax,%rdx,8), %r13
movq (%r13), %rax
movq %r13, %rdi
callq *0xb8(%rax)
movl %eax, %ebp
callq 0x617ec
cmpl %eax, %ebp
jne 0x2986b
xorl %r15d, %r15d
movslq 0x24(%r13), %rax
cmpq %rax, %r15
jge 0x2986b
movq 0x18(%r13), %rax
movq (%rax,%r15,8), %rbp
leaq 0x20(%rbp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x6e7e8
movq %r12, %rdi
movq 0x8(%rsp), %rsi
callq 0x4aaf0
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x29802
movq %rbx, %rdi
callq 0x6eb2e
cmpl $0x4, %r14d
jne 0x29827
movsd 0x60(%rsp), %xmm0
callq 0x22820
movsd 0x18(%rsp), %xmm2
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
divsd %xmm2, %xmm1
jmp 0x29842
movq 0x10(%rsp), %rax
movsd 0x58(%rsp,%rax,8), %xmm1
movsd 0xab7f6(%rip), %xmm0    # 0xd5030
mulsd %xmm0, %xmm1
addsd %xmm0, %xmm1
mulsd 0xab7c6(%rip), %xmm1    # 0xd5010
cvttsd2si %xmm1, %edi
callq 0x29ac2
movq (%rbp), %rax
movq %rbp, %rdi
pushq $0x2
popq %rsi
callq *0xa8(%rax)
incq %r15
jmp 0x297bf
movq 0x38(%rsp), %rdx
incq %rdx
movq 0x20(%rsp), %r13
movq 0x30(%rsp), %rcx
jmp 0x29786
movq 0x28(%rsp), %rcx
incq %rcx
jmp 0x29765
xorl %ebx, %ebx
movl %ebx, %edi
callq 0x29ac2
pushq $0x2
popq %rsi
jmp 0x29421
leal -0x2(%r14), %eax
cmpl $0x2, %eax
jb 0x298c3
cmpl $0x8, %r14d
je 0x298c3
cmpl $0x10, %r14d
jne 0x29a94
movl $0x402, %esi            # imm = 0x402
jmp 0x29421
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
cmpl $0x3, %r14d
jne 0x298e6
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x29adf
movsd %xmm0, 0x10(%rsp)
pushq $0x4
popq %rdi
callq 0x22180
movq 0x8(%rsp), %rax
addq $0x48, %rax
movq %rax, 0x38(%rsp)
xorl %edx, %edx
leaq 0x58(%rsp), %r13
leaq 0x70(%rsp), %rbp
movq 0x20(%rsp), %rcx
movslq 0x3c(%rcx), %rax
cmpq %rax, %rdx
jge 0x29a7d
movq 0x30(%rcx), %rax
movq %rdx, 0x40(%rsp)
movq (%rax,%rdx,8), %rcx
xorl %edx, %edx
movq %rcx, 0x48(%rsp)
movslq 0x1c(%rcx), %rax
cmpq %rax, %rdx
jge 0x29a70
movq 0x10(%rcx), %rax
movq %rdx, 0x50(%rsp)
movq (%rax,%rdx,8), %r15
movq (%r15), %rax
movq %r15, %rdi
callq *0xb8(%rax)
movl %eax, %ebx
callq 0x617ec
cmpl %eax, %ebx
jne 0x29a5e
xorl %ecx, %ecx
movq %r15, 0x28(%rsp)
movslq 0x24(%r15), %rax
cmpq %rax, %rcx
jge 0x29a5e
movq 0x18(%r15), %rax
movq %rcx, 0x30(%rsp)
movq (%rax,%rcx,8), %r15
leaq 0x20(%r15), %rax
movq %rax, 0x18(%rsp)
pushq $0x1
popq %r12
cmpq $0x4, %r12
je 0x29a4c
movq (%r15,%r12,8), %rbx
cmpl $0x3, %r14d
jne 0x299b2
movsd 0x8(%rbx), %xmm0
subsd 0x10(%rsp), %xmm0
jmp 0x299e6
cmpl $0x8, %r14d
je 0x29a00
cmpl $0x2, %r14d
jne 0x299f3
movq %r13, %rdi
movq %rbx, %rsi
movq 0x8(%rsp), %rdx
callq 0x6e1f5
movq %rbp, %rdi
movq 0x38(%rsp), %rsi
callq 0x6f13e
movq %r13, %rdi
movq %rbp, %rsi
callq 0x6ea52
movsd 0xab6ca(%rip), %xmm1    # 0xd50b8
callq 0x29b78
movq %rbx, %rdi
callq 0x221a0
incq %r12
jmp 0x29991
movq %r13, %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
callq 0x6e1f5
movq %r13, %rdi
callq 0x6eada
leaq 0x18(%rbx), %rsi
btl $0x8, 0x58(%rbx)
cmovaeq 0x18(%rsp), %rsi
movq %rbp, %rdi
callq 0x6e7e8
movq %rbp, %rdi
movq %r13, %rsi
callq 0x6ea52
andpd 0xab6ae(%rip), %xmm0    # 0xd50f0
movsd 0xab626(%rip), %xmm1    # 0xd5070
jmp 0x299ee
movq 0x30(%rsp), %rcx
incq %rcx
movq 0x28(%rsp), %r15
jmp 0x2996a
movq 0x50(%rsp), %rdx
incq %rdx
movq 0x48(%rsp), %rcx
jmp 0x2992e
movq 0x40(%rsp), %rdx
incq %rdx
jmp 0x29908
callq 0x22c40
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r14d, %r14d
pushq $0x2
popq %rbx
movslq 0x3c(%r13), %rax
cmpq %rax, %r14
jge 0x29a82
movq 0x30(%r13), %rax
movq (%rax,%r14,8), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x3c262
incq %r14
jmp 0x29a9a
pushq $0x2
popq %rbx
jmp 0x29891