// File: tomfunkhouser[P]gaps/pkgs/R3Utils/R3SurfelViewer.cpp
// Function: gaps::R3SurfelViewer::CompileShaders()
void R3SurfelViewer::
CompileShaders(void)
{
  // Inline GLSL 1.1 vertex shader definition
  const GLchar* vertex_shader_source =
       "#version 110\n"
       "attribute vec3 inPosition;"
       "attribute vec3 inColor;"
       "uniform float pointSize;"
       "varying vec4 vertexColor;"
       "void main()"
       "{"
       "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"
       "    vertexColor = gl_Color;"
       "    gl_PointSize = pointSize;" // Replicate the current functionality
       // Alternative: uncomment to divide by clip space w to scale based on distance.
       // If we do this, we also need to increase the pointSize to compensate:
       //"    gl_PointSize = pointSize / gl_Position.w;"
       "}";
                                                                                  // Inline GLSL 1.1 fragment shader definition
  const GLchar* fragment_shader_source =
       "#version 110\n"
       "varying vec4 vertexColor;"
       "void main()"
       "{"
       "        gl_FragColor = vertexColor;"
       "}";

  // Compile the vertex shader
  vertex_shader = glCreateShader(GL_VERTEX_SHADER);
  glShaderSource(vertex_shader, 1, &vertex_shader_source, 0);
  glCompileShader(vertex_shader);

  // Check if vertex shader is compiled
  int is_compiled = 0;
  glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &is_compiled);
  if (!is_compiled) {
    int log_length = 0;
    glGetShaderiv(vertex_shader, GL_INFO_LOG_LENGTH, &log_length);
    char* log_contents = new char[log_length + 1];
    glGetShaderInfoLog(vertex_shader, log_length, &log_length, log_contents);
    RNFail("Error: Failed to compile the vertex shader: %s\n", log_contents);
    delete[] log_contents;
    DeleteShaders();
    return;
  }
                                                                                     
  // Compile the fragment shader
  fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
  glShaderSource(fragment_shader, 1, &fragment_shader_source, 0);
  glCompileShader(fragment_shader);

  // Check if fragment shader is compiled
  is_compiled = 0;
  glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &is_compiled);
  if (!is_compiled) {
    int log_length = 0;
    glGetShaderiv(fragment_shader, GL_INFO_LOG_LENGTH, &log_length);
    char* log_contents = new char[log_length + 1];
    glGetShaderInfoLog(fragment_shader, log_length, &log_length, log_contents);
    RNFail("Error: Failed to compile the fragment shader: %s\n", log_contents);
    delete[] log_contents;
    DeleteShaders();
    return;
  }
                                                                                     
  // Link the shader program
  shader_program = glCreateProgram();
  glAttachShader(shader_program, vertex_shader);
  glAttachShader(shader_program, fragment_shader);
  glLinkProgram(shader_program);

  // Check if shader is linked
  int is_linked = 0;
  glGetProgramiv(shader_program, GL_LINK_STATUS, &is_linked);
  if (!is_linked) {
    int log_length = 0;
    glGetProgramiv(shader_program, GL_INFO_LOG_LENGTH, &log_length);
    char* log_contents = new char[log_length + 1];
    glGetProgramInfoLog(shader_program, log_length, &log_length, log_contents);
    RNFail("Error: Failed to link the shader program: %s\n", log_contents);
    delete[] log_contents;
    DeleteShaders();
    return;
  }
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0x204e00(%rip), %rax    # 0x241b1a
movq %rax, 0x58(%rsp)
leaq 0x204ee4(%rip), %rax    # 0x241c0a
movq %rax, 0x50(%rsp)
movl $0x8b31, %edi           # imm = 0x8B31
movq 0x286239(%rip), %rax    # 0x2c2f70
callq *(%rax)
movq 0x18(%rsp), %rcx
movl %eax, 0x60c(%rcx)
movq 0x286215(%rip), %rax    # 0x2c2f60
movq (%rax), %rax
movl 0x60c(%rcx), %edi
movl $0x1, %esi
leaq 0x58(%rsp), %rdx
xorl %ecx, %ecx
callq *%rax
movq 0x18(%rsp), %rcx
movq 0x2861da(%rip), %rax    # 0x2c2f48
movq (%rax), %rax
movl 0x60c(%rcx), %edi
callq *%rax
movq 0x18(%rsp), %rcx
movl $0x0, 0x4c(%rsp)
movq 0x2861ab(%rip), %rax    # 0x2c2f38
movq (%rax), %rax
movl 0x60c(%rcx), %edi
movl $0x8b81, %esi           # imm = 0x8B81
leaq 0x4c(%rsp), %rdx
callq *%rax
cmpl $0x0, 0x4c(%rsp)
jne 0x3ce4f
movq 0x18(%rsp), %rcx
movl $0x0, 0x48(%rsp)
movq 0x286177(%rip), %rax    # 0x2c2f38
movq (%rax), %rax
movl 0x60c(%rcx), %edi
movl $0x8b84, %esi           # imm = 0x8B84
leaq 0x48(%rsp), %rdx
callq *%rax
movl 0x48(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rdi
callq 0x28040
movq 0x18(%rsp), %rcx
movq %rax, 0x40(%rsp)
movq 0x28619a(%rip), %rax    # 0x2c2f90
movq (%rax), %rax
movl 0x60c(%rcx), %edi
movl 0x48(%rsp), %esi
movq 0x40(%rsp), %rcx
leaq 0x48(%rsp), %rdx
callq *%rax
movq 0x40(%rsp), %rsi
leaq 0x204e46(%rip), %rdi    # 0x241c61
movb $0x0, %al
callq 0x1a7770
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x3ce3c
movq 0x10(%rsp), %rdi
callq 0x28e00
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
callq *0x390(%rax)
jmp 0x3d0a2
movl $0x8b30, %edi           # imm = 0x8B30
movq 0x286115(%rip), %rax    # 0x2c2f70
callq *(%rax)
movq 0x18(%rsp), %rcx
movl %eax, 0x610(%rcx)
movq 0x2860f1(%rip), %rax    # 0x2c2f60
movq (%rax), %rax
movl 0x610(%rcx), %edi
movl $0x1, %esi
leaq 0x50(%rsp), %rdx
xorl %ecx, %ecx
callq *%rax
movq 0x18(%rsp), %rcx
movq 0x2860b6(%rip), %rax    # 0x2c2f48
movq (%rax), %rax
movl 0x610(%rcx), %edi
callq *%rax
movq 0x18(%rsp), %rcx
movl $0x0, 0x4c(%rsp)
movq 0x286087(%rip), %rax    # 0x2c2f38
movq (%rax), %rax
movl 0x610(%rcx), %edi
movl $0x8b81, %esi           # imm = 0x8B81
leaq 0x4c(%rsp), %rdx
callq *%rax
cmpl $0x0, 0x4c(%rsp)
jne 0x3cf73
movq 0x18(%rsp), %rcx
movl $0x0, 0x3c(%rsp)
movq 0x286053(%rip), %rax    # 0x2c2f38
movq (%rax), %rax
movl 0x610(%rcx), %edi
movl $0x8b84, %esi           # imm = 0x8B84
leaq 0x3c(%rsp), %rdx
callq *%rax
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rdi
callq 0x28040
movq 0x18(%rsp), %rcx
movq %rax, 0x30(%rsp)
movq 0x286076(%rip), %rax    # 0x2c2f90
movq (%rax), %rax
movl 0x610(%rcx), %edi
movl 0x3c(%rsp), %esi
movq 0x30(%rsp), %rcx
leaq 0x3c(%rsp), %rdx
callq *%rax
movq 0x30(%rsp), %rsi
leaq 0x204d52(%rip), %rdi    # 0x241c91
movb $0x0, %al
callq 0x1a7770
movq 0x30(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x3cf60
movq 0x8(%rsp), %rdi
callq 0x28e00
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
callq *0x390(%rax)
jmp 0x3d0a2
movq 0x285fd6(%rip), %rax    # 0x2c2f50
callq *(%rax)
movq 0x18(%rsp), %rcx
movl %eax, 0x608(%rcx)
movq 0x28605a(%rip), %rax    # 0x2c2fe8
movq (%rax), %rax
movl 0x608(%rcx), %edi
movl 0x60c(%rcx), %esi
callq *%rax
movq 0x18(%rsp), %rcx
movq 0x28603d(%rip), %rax    # 0x2c2fe8
movq (%rax), %rax
movl 0x608(%rcx), %edi
movl 0x610(%rcx), %esi
callq *%rax
movq 0x18(%rsp), %rcx
movq 0x286000(%rip), %rax    # 0x2c2fc8
movq (%rax), %rax
movl 0x608(%rcx), %edi
callq *%rax
movq 0x18(%rsp), %rcx
movl $0x0, 0x2c(%rsp)
movq 0x285ff9(%rip), %rax    # 0x2c2fe0
movq (%rax), %rax
movl 0x608(%rcx), %edi
movl $0x8b82, %esi           # imm = 0x8B82
leaq 0x2c(%rsp), %rdx
callq *%rax
cmpl $0x0, 0x2c(%rsp)
jne 0x3d0a2
movq 0x18(%rsp), %rcx
movl $0x0, 0x28(%rsp)
movq 0x285fc5(%rip), %rax    # 0x2c2fe0
movq (%rax), %rax
movl 0x608(%rcx), %edi
movl $0x8b84, %esi           # imm = 0x8B84
leaq 0x28(%rsp), %rdx
callq *%rax
movl 0x28(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rdi
callq 0x28040
movq 0x18(%rsp), %rcx
movq %rax, 0x20(%rsp)
movq 0x285f28(%rip), %rax    # 0x2c2f78
movq (%rax), %rax
movl 0x608(%rcx), %edi
movl 0x28(%rsp), %esi
movq 0x20(%rsp), %rcx
leaq 0x28(%rsp), %rdx
callq *%rax
movq 0x20(%rsp), %rsi
leaq 0x204c4e(%rip), %rdi    # 0x241cc3
movb $0x0, %al
callq 0x1a7770
movq 0x20(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x3d094
movq (%rsp), %rdi
callq 0x28e00
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
callq *0x390(%rax)
addq $0x68, %rsp
retq
nopw (%rax,%rax)