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

  // Process mouse button event
  if ((button == GLUT_LEFT) && (state == GLUT_UP)) {
    // Check for double click  
    static RNBoolean double_click = FALSE;
    static RNTime last_mouse_down_time;
    double_click = (!double_click) && (last_mouse_down_time.Elapsed() < 0.4);
    last_mouse_down_time.Read();

    // Set world origin
    if (double_click) {
      R3Ray ray = viewer->WorldRay(x, y);
      RNScalar best_t = FLT_MAX;
      for (int m = 0; m < meshes.NEntries(); m++) {
        R3Mesh *mesh = meshes[m];
        if ((current_mesh != -1) && (m != current_mesh)) continue;
        R3MeshIntersection intersection;
        if (mesh->Intersection(ray, &intersection)) {
          if (intersection.t < best_t) {
            world_origin = intersection.point;
            best_t = intersection.t;
          }
        }
      }
    }
  }


  // 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:
subq $0xb8, %rsp
movl %edi, 0xb4(%rsp)
movl %esi, 0xb0(%rsp)
movl %edx, 0xac(%rsp)
movl %ecx, 0xa8(%rsp)
movl 0x1cbd33(%rip), %eax    # 0x1f4abc
subl 0xa8(%rsp), %eax
movl %eax, 0xa8(%rsp)
cmpl $0x0, 0xb4(%rsp)
jne 0x28f64
cmpl $0x1, 0xb0(%rsp)
jne 0x28f64
cmpb $0x0, 0x1d25ce(%rip)    # 0x1fb388
jne 0x28de7
leaq 0x1d25c5(%rip), %rdi    # 0x1fb388
callq 0x232e0
cmpl $0x0, %eax
je 0x28de7
leaq 0x1d25a4(%rip), %rdi    # 0x1fb378
callq 0xedda0
jmp 0x28ddb
leaq 0x1d25a6(%rip), %rdi    # 0x1fb388
callq 0x22670
xorl %eax, %eax
cmpl $0x0, 0x1d2584(%rip)    # 0x1fb374
movb %al, 0xb(%rsp)
jne 0x28e18
leaq 0x1d257b(%rip), %rdi    # 0x1fb378
callq 0xede60
movaps %xmm0, %xmm1
movsd 0x16425b(%rip), %xmm0   # 0x18d068
ucomisd %xmm1, %xmm0
seta %al
movb %al, 0xb(%rsp)
movb 0xb(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x1d254d(%rip)    # 0x1fb374
leaq 0x1d254a(%rip), %rdi    # 0x1fb378
callq 0x2adc0
cmpl $0x0, 0x1d253a(%rip)    # 0x1fb374
je 0x28f62
movq 0x1d24c1(%rip), %rsi    # 0x1fb308
cvtsi2sdl 0xac(%rsp), %xmm0
cvtsi2sdl 0xa8(%rsp), %xmm1
leaq 0x68(%rsp), %rdi
callq 0x2ced0
movsd 0x164205(%rip), %xmm0   # 0x18d070
movsd %xmm0, 0x60(%rsp)
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0x4(%rsp)
leaq 0x1d1d90(%rip), %rdi    # 0x1fac18
callq 0x2a760
movl %eax, %ecx
movl 0x4(%rsp), %eax
cmpl %ecx, %eax
jge 0x28f60
movl 0x5c(%rsp), %esi
leaq 0x1d1d72(%rip), %rdi    # 0x1fac18
callq 0x2a780
movq (%rax), %rax
movq %rax, 0x50(%rsp)
cmpl $-0x1, 0x1cbbf6(%rip)   # 0x1f4ab0
je 0x28ef2
movl 0x5c(%rsp), %eax
cmpl 0x1cbbea(%rip), %eax    # 0x1f4ab0
je 0x28ef2
jmp 0x28f50
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
leaq 0x1d24a0(%rip), %rdi    # 0x1fb388
callq 0x22650
jmp 0x28feb
leaq 0x10(%rsp), %rdi
callq 0x2ade0
movq 0x50(%rsp), %rdi
movq (%rdi), %rax
leaq 0x68(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq *0x170(%rax)
cmpl $0x0, %eax
je 0x28f4e
movsd 0x48(%rsp), %xmm1
movsd 0x60(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x28f4c
leaq 0x10(%rsp), %rsi
addq $0x20, %rsi
leaq 0x1d1ced(%rip), %rdi    # 0x1fac28
callq 0xcc120
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
jmp 0x28f4e
jmp 0x28f50
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x28e79
jmp 0x28f62
jmp 0x28f64
cmpl $0x0, 0xb4(%rsp)
jne 0x28f75
xorl %eax, %eax
movl %eax, (%rsp)
jmp 0x28f8f
movl 0xb4(%rsp), %edx
movl $0x2, %eax
movl $0x1, %ecx
cmpl $0x1, %edx
cmovel %ecx, %eax
movl %eax, (%rsp)
movl (%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0xb0(%rsp), %ecx
xorl %edx, %edx
movl $0x1, %eax
cmpl $0x0, %ecx
cmovel %eax, %edx
movslq 0xc(%rsp), %rcx
leaq 0x1d23b2(%rip), %rax    # 0x1fb368
movl %edx, (%rax,%rcx,4)
callq 0x1813d0
movl %eax, 0x1d23cc(%rip)    # 0x1fb390
movl 0xac(%rsp), %eax
movl %eax, 0x1d238f(%rip)    # 0x1fb360
movl 0xa8(%rsp), %eax
movl %eax, 0x1d2386(%rip)    # 0x1fb364
callq 0x1791a0
addq $0xb8, %rsp
retq
movq 0xa0(%rsp), %rdi
callq 0x23260
nopl (%rax,%rax)