// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
leaq 0xe82c2(%rip), %rax     # 0x11a906
leaq 0x20(%rsp), %r14
movq %rax, (%r14)
leaq 0xe83a3(%rip), %rax     # 0x11a9f6
movq %rax, 0x10(%rsp)
movq 0x1748e9(%rip), %r13    # 0x1a6f48
movl $0x8b31, %edi           # imm = 0x8B31
callq *(%r13)
movl %eax, 0x60c(%rbx)
movq 0x1748c3(%rip), %r12    # 0x1a6f38
pushq $0x1
popq %rsi
movl %eax, %edi
movq %r14, %rdx
xorl %ecx, %ecx
callq *(%r12)
movq 0x17488e(%rip), %rbp    # 0x1a6f18
movl 0x60c(%rbx), %edi
callq *(%rbp)
leaq 0xc(%rsp), %r14
andl $0x0, (%r14)
movq 0x17485d(%rip), %r15    # 0x1a6f00
movl 0x60c(%rbx), %edi
movl $0x8b81, %esi           # imm = 0x8B81
movq %r14, %rdx
callq *(%r15)
cmpl $0x0, (%r14)
je 0x327c3
movl $0x8b30, %edi           # imm = 0x8B30
callq *(%r13)
movl %eax, 0x610(%rbx)
pushq $0x1
popq %rsi
leaq 0x10(%rsp), %rdx
movl %eax, %edi
xorl %ecx, %ecx
callq *(%r12)
movl 0x610(%rbx), %edi
callq *(%rbp)
leaq 0xc(%rsp), %r14
andl $0x0, (%r14)
movl 0x610(%rbx), %edi
movl $0x8b81, %esi           # imm = 0x8B81
movq %r14, %rdx
callq *(%r15)
cmpl $0x0, (%r14)
je 0x3280f
movq 0x174817(%rip), %rax    # 0x1a6f28
callq *(%rax)
movl %eax, 0x608(%rbx)
movq 0x1748c0(%rip), %r14    # 0x1a6fe0
movl 0x60c(%rbx), %esi
movl %eax, %edi
callq *(%r14)
movl 0x608(%rbx), %edi
movl 0x610(%rbx), %esi
callq *(%r14)
movq 0x174877(%rip), %rax    # 0x1a6fb8
movl 0x608(%rbx), %edi
callq *(%rax)
leaq 0x8(%rsp), %r14
andl $0x0, (%r14)
movq 0x17487f(%rip), %r15    # 0x1a6fd8
movl 0x608(%rbx), %edi
movl $0x8b82, %esi           # imm = 0x8B82
movq %r14, %rdx
callq *(%r15)
cmpl $0x0, (%r14)
jne 0x32877
leaq 0x1c(%rsp), %r14
andl $0x0, (%r14)
movl 0x608(%rbx), %edi
movl $0x8b84, %esi           # imm = 0x8B84
movq %r14, %rdx
callq *(%r15)
movslq (%r14), %r15
movq %r15, %rdi
incq %rdi
callq 0x27040
movq %rax, %r12
movq 0x1747aa(%rip), %rax    # 0x1a6f50
movl 0x608(%rbx), %edi
movl %r15d, %esi
movq %r14, %rdx
movq %r12, %rcx
callq *(%rax)
leaq 0xe82f1(%rip), %rdi     # 0x11aaaf
jmp 0x32859
leaq 0x8(%rsp), %r14
andl $0x0, (%r14)
movl 0x60c(%rbx), %edi
movl $0x8b84, %esi           # imm = 0x8B84
movq %r14, %rdx
callq *(%r15)
movslq (%r14), %r15
movq %r15, %rdi
incq %rdi
callq 0x27040
movq %rax, %r12
movq 0x17477b(%rip), %rax    # 0x1a6f70
movl 0x60c(%rbx), %edi
movl %r15d, %esi
movq %r14, %rdx
movq %r12, %rcx
callq *(%rax)
leaq 0xe8240(%rip), %rdi     # 0x11aa4d
jmp 0x32859
leaq 0x8(%rsp), %r14
andl $0x0, (%r14)
movl 0x610(%rbx), %edi
movl $0x8b84, %esi           # imm = 0x8B84
movq %r14, %rdx
callq *(%r15)
movslq (%r14), %r15
movq %r15, %rdi
incq %rdi
callq 0x27040
movq %rax, %r12
movq 0x17472f(%rip), %rax    # 0x1a6f70
movl 0x610(%rbx), %edi
movl %r15d, %esi
movq %r14, %rdx
movq %r12, %rcx
callq *(%rax)
leaq 0xe8224(%rip), %rdi     # 0x11aa7d
movq %r12, %rsi
xorl %eax, %eax
callq 0xd3412
movq %r12, %rdi
callq 0x27c80
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x390(%rax)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq