// File: /workspace/github_cpp_makefile_star2_O1/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 0x119314(%rip), %ebp    # 0x13f9b8
subl %ecx, %ebp
testl %esi, %esi
je 0x26750
testl %r15d, %r15d
jne 0x267a7
movb 0x11e2fb(%rip), %al     # 0x1449b8
testb %al, %al
je 0x26879
xorl %eax, %eax
cmpl $0x0, 0x11e2d6(%rip)    # 0x1449a4
jne 0x266ed
leaq 0x11e2d1(%rip), %rdi    # 0x1449a8
callq 0x88182
movsd 0xb5984(%rip), %xmm1    # 0xdc068
xorl %eax, %eax
ucomisd %xmm0, %xmm1
seta %al
movl %eax, 0x11e2b1(%rip)    # 0x1449a4
leaq 0x11e2ae(%rip), %rdi    # 0x1449a8
xorl %esi, %esi
callq 0x21fc0
cmpl $0x63, 0x11e288(%rip)   # 0x144990
jg 0x267a2
cmpl $0x0, 0x11e28f(%rip)    # 0x1449a4
je 0x267f7
movq %rsp, %r12
movq %r12, %rdi
callq 0x762b8
movq 0x11e1ab(%rip), %rdi    # 0x1448d8
movl %ebx, %esi
movl %ebp, %edx
xorl %ecx, %ecx
movq %r12, %r8
callq 0x268c1
testl %eax, %eax
je 0x267a2
leaq 0x11e172(%rip), %rdi    # 0x1448b8
movq %rsp, %rsi
callq 0x76628
jmp 0x267a2
movl $0x0, 0x11e236(%rip)    # 0x144990
testl %r15d, %r15d
je 0x267f2
cmpl $0x4, %r15d
je 0x26787
cmpl $0x3, %r15d
jne 0x267a7
movq 0x11e172(%rip), %rdi    # 0x1448e8
leaq 0x11e13b(%rip), %rsi    # 0x1448b8
movsd 0xb58f3(%rip), %xmm0    # 0xdc078
jmp 0x2679d
movq 0x11e15a(%rip), %rdi    # 0x1448e8
leaq 0x11e123(%rip), %rsi    # 0x1448b8
movsd 0xb58d3(%rip), %xmm0    # 0xdc070
callq 0x3e798
testl %r15d, %r15d
je 0x267ee
xorl %eax, %eax
cmpl $0x1, %r15d
setne %al
incq %rax
xorl %ecx, %ecx
testl %r14d, %r14d
sete %cl
leaq 0x11e1d6(%rip), %rdx    # 0x144998
movl %ecx, (%rdx,%rax,4)
callq 0xd435c
movl %eax, 0x11e1c4(%rip)    # 0x144994
movl %ebx, 0x11e1b2(%rip)    # 0x144988
movl %ebp, 0x11e1b0(%rip)    # 0x14498c
callq 0xcf414
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x267b3
movl %r15d, %eax
jmp 0x267b3
movq %rsp, %r12
movq %r12, %rdi
callq 0x762b8
leaq 0x18(%rsp), %rdi
callq 0x76d50
movq $0x0, 0x11e0f9(%rip) # 0x144910
leaq 0x11e0f2(%rip), %rcx    # 0x144910
movq 0x11e0b3(%rip), %rdi    # 0x1448d8
movl %ebx, %esi
movl %ebp, %edx
movq %r12, %r8
callq 0x268c1
testl %eax, %eax
je 0x267a2
movq 0x11e0d0(%rip), %rax    # 0x144910
movq 0x1a0(%rax), %rax
testq %rax, %rax
leaq 0xb5953(%rip), %rsi     # 0xdc1a4
cmovneq %rax, %rsi
movsd (%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
leaq 0xb591e(%rip), %rdi     # 0xdc18b
movb $0x3, %al
callq 0x21080
jmp 0x267a2
leaq 0x11e138(%rip), %rdi    # 0x1449b8
callq 0x22140
testl %eax, %eax
je 0x266c5
leaq 0x11e114(%rip), %rdi    # 0x1449a8
callq 0x88120
leaq 0x11e118(%rip), %rdi    # 0x1449b8
callq 0x215d0
jmp 0x266c5
movq %rax, %rbx
leaq 0x11e104(%rip), %rdi    # 0x1449b8
callq 0x215b0
movq %rbx, %rdi
callq 0x220d0