// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/apps/scnview/scnview.cpp
// Function: GLUTMouse(int, int, int, int)
void GLUTMouse(int button, int state, int x, int y)
{
  // Invert y coordinate
  y = GLUTwindow_height - y;

  // Mouse is going down
  if (state == GLUT_DOWN) {
    // Reset mouse drag
    GLUTmouse_drag = 0;

    // Process thumbwheel
    if (button == 3) viewer->ScaleWorld(center, 0.9);
    else if (button == 4) viewer->ScaleWorld(center, 1.1);
  }
  else if (button == 0) {
    // Check for double click  
    static RNBoolean double_click = FALSE;
    static RNTime last_mouse_up_time;
    double_click = (!double_click) && (last_mouse_up_time.Elapsed() < 0.4);
    last_mouse_up_time.Read();

    // Check for click (rather than drag)
    if (GLUTmouse_drag < 100) {
      // Check for double click
      if (double_click) {
        // Set viewing center point 
        R3Point intersection_point;
        if (Pick(scene, x, y, NULL, &intersection_point)) {
          center = intersection_point;
        }
      }
      else {
        // Select node 
        R3Point position;
        R3Vector normal;
        selected_node = NULL;;
#if 1
        if (Pick(scene, x, y, &selected_node, &position)) {
          printf("Selected %s    %g %g %g\n", (selected_node->Name()) ? selected_node->Name() : "NoName",
            position.X(), position.Y(), position.Z());
        }
#else
        R3Ray ray = viewer->WorldRay(x, y);
        R3Material *material = NULL;
        if (scene->Intersects(ray, &selected_node, &material, NULL, &position, &normal)) {
          printf("Selected %s    %g %g %g    %g %g %g\n", (selected_node->Name()) ? selected_node->Name() : "NoName",
            position.X(), position.Y(), position.Z(), normal.X(), normal.Y(), normal.Z());
          const char *material_name = (material) ? material->Name() : "-";
          const R3Brdf *brdf = (material) ? material->Brdf() : NULL;
          const char *brdf_name = (brdf) ? brdf->Name() : "-";
          RNRgb diffuse = (brdf) ? brdf->Diffuse() : RNblack_rgb;
          RNRgb specular = (brdf) ? brdf->Specular() : RNblack_rgb;
          RNRgb transmission = (brdf) ? brdf->Transmission() : RNblack_rgb;
          RNRgb emission = (brdf) ? brdf->Emission() : RNblack_rgb;
          RNScalar shininess = (brdf) ? brdf->Shininess() : 0;
          const R2Texture *texture = (material) ? material->Texture() : NULL;
          const char *texture_name = (texture) ? texture->Name() : "-";
          printf("  Material %s : brdf %s  kd=(%g %g %g) ks=(%g %g %g) kt=(%g %g %g) ke=(%g %g %g) ns=%g : texture %s\n", material_name,
            brdf_name, diffuse.R(), diffuse.G(), diffuse.B(), specular.R(), specular.G(), specular.B(),
            transmission.R(), transmission.G(), transmission.B(), emission.R(), emission.G(), emission.B(),
            shininess, texture_name);
        }
#endif
      }
    }
  }

  // Remember button state 
  int b = (button == GLUT_LEFT_BUTTON) ? 0 : ((button == GLUT_MIDDLE_BUTTON) ? 1 : 2);
  GLUTbutton[b] = (state == GLUT_DOWN) ? 1 : 0;

  // Remember modifiers 
  GLUTmodifiers = glutGetModifiers();

   // Remember mouse position 
  GLUTmouse[0] = x;
  GLUTmouse[1] = y;

  // Redraw
  glutPostRedisplay();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x30, %rsp
movl %edx, %ebx
movl %esi, %r14d
movl %edi, %r15d
movl 0x11731e(%rip), %ebp    # 0x13d9b8
subl %ecx, %ebp
testl %esi, %esi
je 0x26750
testl %r15d, %r15d
jne 0x26790
movb 0x11c305(%rip), %al     # 0x1429b8
testb %al, %al
je 0x26860
xorl %eax, %eax
cmpl $0x0, 0x11c2e0(%rip)    # 0x1429a4
jne 0x266e3
leaq 0x11c2db(%rip), %rdi    # 0x1429a8
callq 0x87052
movsd 0xb398e(%rip), %xmm1    # 0xda068
xorl %eax, %eax
ucomisd %xmm0, %xmm1
seta %al
movl %eax, 0x11c2bb(%rip)    # 0x1429a4
leaq 0x11c2b8(%rip), %rdi    # 0x1429a8
xorl %r12d, %r12d
xorl %esi, %esi
callq 0x21fb0
cmpl $0x63, 0x11c28f(%rip)   # 0x142990
jg 0x2679e
cmpl $0x0, 0x11c296(%rip)    # 0x1429a4
je 0x267df
movq %rsp, %r15
movq %r15, %rdi
callq 0x753c8
movq 0x11c1b2(%rip), %rdi    # 0x1428d8
movl %ebx, %esi
movl %ebp, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x268a8
testl %eax, %eax
je 0x26858
leaq 0x11c175(%rip), %rdi    # 0x1428b8
movq %rsp, %rsi
callq 0x75732
jmp 0x26858
movl $0x0, 0x11c236(%rip)    # 0x142990
testl %r15d, %r15d
je 0x267da
cmpl $0x3, %r15d
je 0x26775
cmpl $0x4, %r15d
jne 0x26790
movsd 0xb3905(%rip), %xmm0    # 0xda078
jmp 0x2677d
movsd 0xb38f3(%rip), %xmm0    # 0xda070
movq 0x11c164(%rip), %rdi    # 0x1428e8
leaq 0x11c12d(%rip), %rsi    # 0x1428b8
callq 0x3e2d8
xorl %r12d, %r12d
cmpl $0x1, %r15d
setne %r12b
incq %r12
xorl %eax, %eax
testl %r14d, %r14d
sete %al
leaq 0x11c1eb(%rip), %rcx    # 0x142998
movl %eax, (%rcx,%r12,4)
callq 0xd2449
movl %eax, 0x11c1d8(%rip)    # 0x142994
movl %ebx, 0x11c1c6(%rip)    # 0x142988
movl %ebp, 0x11c1c4(%rip)    # 0x14298c
callq 0xcd590
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl %r15d, %r12d
jmp 0x2679e
movq %rsp, %r15
movq %r15, %rdi
callq 0x753c8
leaq 0x18(%rsp), %rdi
callq 0x75e5c
movq $0x0, 0x11c111(%rip) # 0x142910
leaq 0x11c10a(%rip), %rcx    # 0x142910
movq 0x11c0cb(%rip), %rdi    # 0x1428d8
movl %ebx, %esi
movl %ebp, %edx
movq %r15, %r8
callq 0x268a8
testl %eax, %eax
je 0x26858
movq 0x11c0ec(%rip), %rax    # 0x142910
movq 0x1a0(%rax), %rax
testq %rax, %rax
leaq 0xb396f(%rip), %rsi     # 0xda1a4
cmovneq %rax, %rsi
movsd (%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
leaq 0xb393a(%rip), %rdi     # 0xda18b
movb $0x3, %al
callq 0x21080
xorl %r12d, %r12d
jmp 0x2679e
leaq 0x11c151(%rip), %rdi    # 0x1429b8
callq 0x22130
testl %eax, %eax
je 0x266bb
leaq 0x11c12d(%rip), %rdi    # 0x1429a8
callq 0x86ff0
leaq 0x11c131(%rip), %rdi    # 0x1429b8
callq 0x215d0
jmp 0x266bb
movq %rax, %rbx
leaq 0x11c11d(%rip), %rdi    # 0x1429b8
callq 0x215b0
movq %rbx, %rdi
callq 0x220c0