// File: /workspace/github_cpp_makefile_star2_O1/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 0x107688(%rip), %rax    # 0x13b8f6
leaq 0x20(%rsp), %r14
movq %rax, (%r14)
leaq 0x107769(%rip), %rax    # 0x13b9e6
movq %rax, 0x10(%rsp)
movq 0x18aca7(%rip), %r13    # 0x1bef30
movl $0x8b31, %edi           # imm = 0x8B31
callq *(%r13)
movl %eax, 0x60c(%rbx)
movq 0x18ac79(%rip), %r12    # 0x1bef18
movl %eax, %edi
movl $0x1, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq *(%r12)
movq 0x18ac42(%rip), %rbp    # 0x1beef8
movl 0x60c(%rbx), %edi
callq *(%rbp)
leaq 0xc(%rsp), %r14
movl $0x0, (%r14)
movq 0x18ac0e(%rip), %r15    # 0x1beee0
movl 0x60c(%rbx), %edi
movl $0x8b81, %esi           # imm = 0x8B81
movq %r14, %rdx
callq *(%r15)
cmpl $0x0, (%r14)
je 0x343fd
movl $0x8b30, %edi           # imm = 0x8B30
callq *(%r13)
movl %eax, 0x610(%rbx)
leaq 0x10(%rsp), %rdx
movl %eax, %edi
movl $0x1, %esi
xorl %ecx, %ecx
callq *(%r12)
movl 0x610(%rbx), %edi
callq *(%rbp)
leaq 0xc(%rsp), %r14
movl $0x0, (%r14)
movl 0x610(%rbx), %edi
movl $0x8b81, %esi           # imm = 0x8B81
movq %r14, %rdx
callq *(%r15)
cmpl $0x0, (%r14)
je 0x3444c
movq 0x18abc3(%rip), %rax    # 0x1bef08
callq *(%rax)
movl %eax, 0x608(%rbx)
movq 0x18ac84(%rip), %r14    # 0x1befd8
movl 0x60c(%rbx), %esi
movl %eax, %edi
callq *(%r14)
movl 0x608(%rbx), %edi
movl 0x610(%rbx), %esi
callq *(%r14)
movq 0x18ac3b(%rip), %rax    # 0x1befb0
movl 0x608(%rbx), %edi
callq *(%rax)
leaq 0x8(%rsp), %r14
movl $0x0, (%r14)
movq 0x18ac40(%rip), %r15    # 0x1befd0
movl 0x608(%rbx), %edi
movl $0x8b82, %esi           # imm = 0x8B82
movq %r14, %rdx
callq *(%r15)
cmpl $0x0, (%r14)
jne 0x344b7
leaq 0x1c(%rsp), %r14
movl $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 0x18ab58(%rip), %rax    # 0x1bef38
movl 0x608(%rbx), %edi
movl %r15d, %esi
movq %r14, %rdx
movq %r12, %rcx
callq *(%rax)
leaq 0x1076a7(%rip), %rdi    # 0x13ba9f
jmp 0x34499
leaq 0x8(%rsp), %r14
movl $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 0x18ab26(%rip), %rax    # 0x1bef58
movl 0x60c(%rbx), %edi
movl %r15d, %esi
movq %r14, %rdx
movq %r12, %rcx
callq *(%rax)
leaq 0x1075f3(%rip), %rdi    # 0x13ba3d
jmp 0x34499
leaq 0x8(%rsp), %r14
movl $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 0x18aad7(%rip), %rax    # 0x1bef58
movl 0x610(%rbx), %edi
movl %r15d, %esi
movq %r14, %rdx
movq %r12, %rcx
callq *(%rax)
leaq 0x1075d4(%rip), %rdi    # 0x13ba6d
movq %r12, %rsi
xorl %eax, %eax
callq 0xea082
movq %r12, %rdi
callq 0x27c90
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