// File: /workspace/github_cpp_makefile_star2_O2/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 %rbx
subq $0x38, %rsp
movl %edx, %ebx
movl %esi, %r14d
movl %edi, %r15d
movl 0x1090d5(%rip), %ebp    # 0x12e9d8
subl %ecx, %ebp
testl %esi, %esi
je 0x259b8
testl %r15d, %r15d
jne 0x259f5
movb 0x10e0bc(%rip), %al     # 0x1339d8
testb %al, %al
je 0x25abf
xorl %eax, %eax
cmpl $0x0, 0x10e097(%rip)    # 0x1339c4
jne 0x2594c
leaq 0x10e092(%rip), %rdi    # 0x1339c8
callq 0x7bcb8
movsd 0x9f725(%rip), %xmm1    # 0xc5068
xorl %eax, %eax
ucomisd %xmm0, %xmm1
seta %al
movl %eax, 0x10e072(%rip)    # 0x1339c4
leaq 0x10e06f(%rip), %rdi    # 0x1339c8
callq 0x27372
cmpl $0x63, 0x10e04b(%rip)   # 0x1339b0
jg 0x25a84
cmpl $0x0, 0x10e052(%rip)    # 0x1339c4
je 0x25a0b
leaq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x6b872
movq 0x10df6c(%rip), %rdi    # 0x1338f8
movl %ebx, %esi
movl %ebp, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x25b07
testl %eax, %eax
je 0x25a84
leaq 0x10df2f(%rip), %rdi    # 0x1338d8
leaq 0x8(%rsp), %rsi
callq 0x6bb5e
jmp 0x25a84
andl $0x0, 0x10dff1(%rip)    # 0x1339b0
testl %r15d, %r15d
je 0x25a06
cmpl $0x3, %r15d
je 0x259da
cmpl $0x4, %r15d
jne 0x259f5
movsd 0x9f6a0(%rip), %xmm0    # 0xc5078
jmp 0x259e2
movsd 0x9f68e(%rip), %xmm0    # 0xc5070
movq 0x10df1f(%rip), %rdi    # 0x133908
leaq 0x10dee8(%rip), %rsi    # 0x1338d8
callq 0x3a0be
xorl %eax, %eax
cmpl $0x1, %r15d
setne %al
incq %rax
jmp 0x25a86
movl %r15d, %eax
jmp 0x25a86
leaq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x6b872
leaq 0x20(%rsp), %rdi
callq 0x6c032
andq $0x0, 0x10df06(%rip)    # 0x133930
leaq 0x10deff(%rip), %rcx    # 0x133930
movq 0x10dec0(%rip), %rdi    # 0x1338f8
movl %ebx, %esi
movl %ebp, %edx
movq %r15, %r8
callq 0x25b07
testl %eax, %eax
je 0x25a84
movq 0x10dee1(%rip), %rax    # 0x133930
movq 0x1a0(%rax), %rax
testq %rax, %rax
leaq 0x9f744(%rip), %rsi     # 0xc51a4
cmovneq %rax, %rsi
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
leaq 0x9f70e(%rip), %rdi     # 0xc518b
movb $0x3, %al
callq 0x21080
xorl %eax, %eax
xorl %ecx, %ecx
testl %r14d, %r14d
sete %cl
leaq 0x10df23(%rip), %rdx    # 0x1339b8
movl %ecx, (%rdx,%rax,4)
callq 0xbd22a
movl %eax, 0x10df11(%rip)    # 0x1339b4
movl %ebx, 0x10deff(%rip)    # 0x1339a8
movl %ebp, 0x10defd(%rip)    # 0x1339ac
callq 0xb878c
addq $0x38, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10df12(%rip), %rdi    # 0x1339d8
callq 0x22150
testl %eax, %eax
je 0x25924
leaq 0x10deee(%rip), %rdi    # 0x1339c8
callq 0x7bc7e
leaq 0x10def2(%rip), %rdi    # 0x1339d8
callq 0x215d0
jmp 0x25924
movq %rax, %rbx
leaq 0x10dede(%rip), %rdi    # 0x1339d8
callq 0x215b0
movq %rbx, %rdi
callq 0x220e0