// File: /workspace/github_c_makefile_star2_O1/archer1357[P]Offline-Raytracer/utils.c
// Function: renderScene
void renderScene(int updateFrom,int updateTo,
                 int width, int height,
                 float aspect,float yFov,
                 const float *minBound,const float *maxBound,
                 const float *origin,
                 const float *rotMat,
                 struct Ray *rayStack,
                 struct KdTraverseNode *traverseStack,
                 struct KdNode *rootNode,
                 struct Triangle *triangles,int trianglesNum,
                 struct Sphere *spheres,int spheresNum,
                 struct PointLight *pointLights,int pointLightsNum,


                 sobol_state_t *qrnd,


                 float *canvas,

                 int maxBounces,int maxSplits) {

  int denom=maxBounces*maxSplits;
  float glossyRatio=(denom!=0)?(1.0f/(float)denom)*0.2f:0.0f;


  float d;
  int i;

  //
  d=1.0f/tanf(yFov/2.0f);

  //
  int raycastsNum;


  for(i=updateFrom;i<updateTo;i++) {
    int x,y;
    float u,v,px,py;
    float pixelCol[3]={0.0f,0.0f,0.0f};

    raycastsNum=1;

    rayStack[raycastsNum-1].bounces=0;
    rayStack[raycastsNum-1].origin[0]=origin[0];
    rayStack[raycastsNum-1].origin[1]=origin[1];
    rayStack[raycastsNum-1].origin[2]=origin[2];
    rayStack[raycastsNum-1].ratio=1.0f;

    x=i%width;
    y=i/width;
    u=x/(float)width;
    v=1.0f-y/(float)height; //flip
    px=2.0f*u-1.0f;
    py=2.0f*v-1.0f;

    float bla[3]={px*aspect,py,-d};//rhs


    mat3_mulVector(rayStack[raycastsNum-1].vec,rotMat,bla);
    vec3_normal(rayStack[raycastsNum-1].vec,rayStack[raycastsNum-1].vec);

    while(raycastsNum > 0) {
      float curCol[3]={0.0f,0.0f,0.0f};
      float nor[3],colPt[3];
      float illumCol[3]={0.0f,0.0f,0.0f};
      float specCol[3]={0.0f,0.0f,0.0f};
      struct Material *material;

      //
      raycastsNum--;
      struct Ray ray=rayStack[raycastsNum];

      if(!intersectSceneObject(&ray,traverseStack,minBound,maxBound,rootNode,
                               triangles,trianglesNum,spheres,spheresNum,
                               colPt,nor,&material)) {
        // pixelCol[0]=1.0f;
        break;
      }

      // pixelCol[0]=material->col[0];
      // pixelCol[1]=material->col[1];
      // pixelCol[2]=material->col[2];
      ////////////////
      calcLights(&ray,material,colPt,nor,
                 traverseStack,minBound,maxBound,rootNode,
                 triangles,trianglesNum,spheres,spheresNum,
                 pointLights,pointLightsNum,illumCol,specCol);

      //
      curCol[0]=(material->col[0]*illumCol[0]+specCol[0]);
      curCol[1]=material->col[1]*illumCol[1]+specCol[1];
      curCol[2]=material->col[2]*illumCol[2]+specCol[2];

      curCol[0]*=ray.ratio;
      curCol[1]*=ray.ratio;
      curCol[2]*=ray.ratio;

      pixelCol[0]+=curCol[0];
      pixelCol[1]+=curCol[1];
      pixelCol[2]+=curCol[2];

      ///////////////
      // pixelCol[0]+=material->col[0];
      // pixelCol[1]+=material->col[1];
      // pixelCol[2]+=material->col[2];


      // pixelCol[0]=(1.0f+nor[0])/2.0f;
      // pixelCol[1]=(1.0f+nor[1])/2.0f;
      // pixelCol[2]=(1.0f+nor[2])/2.0f;

      // pixelCol[0]+=illumCol[0]*0.1f+specCol[0];
      // pixelCol[1]+=illumCol[1]*0.1f+specCol[1];
      // pixelCol[2]+=illumCol[2]*0.1f+specCol[2];

      //pixelCol[0]+=illumCol[0];
      //pixelCol[1]+=illumCol[1];
      //pixelCol[2]+=illumCol[2];




      if(maxSplits !=0 && ray.bounces<maxBounces) {
        float reflectVec[3];
        reflect(reflectVec,ray.vec,nor);
        vec3_normal(reflectVec,reflectVec);

        if(1==maxSplits) {
          rayStack[raycastsNum].origin[0]=colPt[0];
          rayStack[raycastsNum].origin[1]=colPt[1];
          rayStack[raycastsNum].origin[2]=colPt[2];
          //(float)powi(RAY_MAX_SPLITS,RAY_MAX_BOUNCES);

          rayStack[raycastsNum].ratio=ray.ratio*0.15f;
          rayStack[raycastsNum].bounces=ray.bounces+1;

          rayStack[raycastsNum].vec[0]=reflectVec[0];
          rayStack[raycastsNum].vec[1]=reflectVec[1];
          rayStack[raycastsNum].vec[2]=reflectVec[2];
          raycastsNum++;
        } else if(maxSplits > 1) {
          int j;
          float RN1[3],RN2[3],rotMat[9];

          RN1[0]=reflectVec[2];
          RN1[1]=reflectVec[1];
          RN1[2]=-reflectVec[0];
          vec3_cross(RN2,reflectVec,RN1);
          rotMat[0]=RN1[0];
          rotMat[3]=RN1[1];
          rotMat[6]=RN1[2];

          rotMat[1]=RN2[0];
          rotMat[4]=RN2[1];
          rotMat[7]=RN2[2];

          rotMat[2]=reflectVec[0];
          rotMat[5]=reflectVec[1];
          rotMat[8]=reflectVec[2];
          // int x,y;

          // for(x=-3;x<=3;x++) {
          //   for(y=-3;y<=3;y++) {
          //     if(x==0 && y==0) {
          //       //continue;
          //     }
          //     float v[3];
          //     v[0]=(float)x;
          //     v[1]=(float)y;
          //     v[2]=6.0f;

          //     mat3_mulVector(v,rotMat,v);
          //     vec3_normal(v,v);


          //     rayStack[raycastsNum].origin[0]=colPt[0];
          //     rayStack[raycastsNum].origin[1]=colPt[1];
          //     rayStack[raycastsNum].origin[2]=colPt[2];
          //     rayStack[raycastsNum].ratio=ray.ratio*0.01f;
          //     rayStack[raycastsNum].bounces=ray.bounces+1;

          //     rayStack[raycastsNum].vec[0]=v[0];
          //     rayStack[raycastsNum].vec[1]=v[1];
          //     rayStack[raycastsNum].vec[2]=v[2];

          //     raycastsNum++;
          //   }
          // }
          // reflectVec[0]=nor[0];
          // reflectVec[1]=nor[1];
          // reflectVec[2]=nor[2];
          for(j=0;j<maxSplits;j++) {
            float drefl=material->diffuseRefl;
            float xoffs, yoffs;

            do {
              // xoffs =((float)(rand()%1000))/1000.0f * drefl;
              // yoffs = ((float)(rand()%1000))/1000.0f * drefl;


              double v[2];
              sobol_get(qrnd,2,v);


              xoffs=(float)v[0]* drefl;
              yoffs=(float)v[1]* drefl;

            } while((xoffs*xoffs+yoffs*yoffs)>(drefl*drefl));
            float R[3],Ra[3],Rb[3];
            vec3_mulScalar(Ra,RN1,xoffs);
            vec3_mulScalar(Rb,RN2,yoffs*drefl);
            vec3_add(R,reflectVec,Ra);
            vec3_add(R,R,Rb);
            vec3_normal(R,R);



            rayStack[raycastsNum].origin[0]=colPt[0];
            rayStack[raycastsNum].origin[1]=colPt[1];
            rayStack[raycastsNum].origin[2]=colPt[2];
            //(float)powi(RAY_MAX_SPLITS,RAY_MAX_BOUNCES);

            rayStack[raycastsNum].ratio=ray.ratio*glossyRatio;
            rayStack[raycastsNum].bounces=ray.bounces+1;

            rayStack[raycastsNum].vec[0]=R[0];
            rayStack[raycastsNum].vec[1]=R[1];
            rayStack[raycastsNum].vec[2]=R[2];
            raycastsNum++;

          }

          // for(j=0;j<raycastMaxGlossySplits;j++) {
          //   // float vec[3];
          //   // vec3_sub(vec,colPt,raycast.origin);
          //   // vec3_normal(vec,vec);

          //   raycastStack[raycastsNum].origin[0]=colPt[0];
          //   raycastStack[raycastsNum].origin[1]=colPt[1];
          //   raycastStack[raycastsNum].origin[2]=colPt[2];
          //   raycastStack[raycastsNum].ratio=raycast.ratio*0.1f;
          //   raycastStack[raycastsNum].steps=raycast.steps-1;

          //   raycastStack[raycastsNum].vec[0]=reflectVec[0];
          //   raycastStack[raycastsNum].vec[1]=reflectVec[1];
          //   raycastStack[raycastsNum].vec[2]=reflectVec[2];
          //   // reflectf(raycastStack[raycastsNum].vec,vec,nor);

          //   raycastsNum++;
          // }
        }
      }



    }

    //
    canvas[(i-updateFrom)*3]=pixelCol[0];
    canvas[(i-updateFrom)*3+1]=pixelCol[1];
    canvas[(i-updateFrom)*3+2]=pixelCol[2];
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %r9, 0xa8(%rsp)
movq %r8, 0x78(%rsp)
movss %xmm0, 0xec(%rsp)
movl %ecx, %ebp
movl %edx, 0x84(%rsp)
movl %esi, 0x88(%rsp)
movl %edi, %ebx
movl 0x240(%rsp), %eax
imull 0x238(%rsp), %eax
xorps %xmm0, %xmm0
movss %xmm0, 0x8c(%rsp)
testl %eax, %eax
je 0x415a17
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss 0xbfa8e(%rip), %xmm2    # 0x4d5490
divss %xmm0, %xmm2
mulss 0xc28fe(%rip), %xmm2    # 0x4d830c
movss %xmm2, 0x8c(%rsp)
mulss 0xbfa8d(%rip), %xmm1    # 0x4d54ac
movaps %xmm1, %xmm0
callq 0x418150
cmpl 0x88(%rsp), %ebx
jge 0x416179
movq 0x228(%rsp), %r12
movq 0x1e0(%rsp), %rax
xorps %xmm1, %xmm1
cvtsi2ssl 0x84(%rsp), %xmm1
movss %xmm1, 0xe8(%rsp)
xorps %xmm1, %xmm1
cvtsi2ss %ebp, %xmm1
movss %xmm1, 0xe4(%rsp)
movss 0xc2893(%rip), %xmm1    # 0x4d8304
divss %xmm0, %xmm1
movss %xmm1, 0xe0(%rsp)
leaq 0xc(%rax), %rcx
movq %rcx, 0x148(%rsp)
movslq %ebx, %rdx
leaq 0xf0(%rsp), %r15
movq %rdx, 0x140(%rsp)
movl $0x0, 0x18(%rax)
movq 0x1d0(%rsp), %rcx
movss (%rcx), %xmm0
movss %xmm0, (%rax)
movss 0x4(%rcx), %xmm0
movss %xmm0, 0x4(%rax)
movss 0x8(%rcx), %xmm0
movss %xmm0, 0x8(%rax)
movl $0x3f800000, 0x1c(%rax) # imm = 0x3F800000
movq %rdx, 0x150(%rsp)
movl %edx, %eax
cltd
idivl 0x84(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
divss 0xe8(%rsp), %xmm0
divss 0xe4(%rsp), %xmm1
movss 0xbf987(%rip), %xmm2    # 0x4d5490
subss %xmm1, %xmm2
addss %xmm0, %xmm0
movss 0xc27eb(%rip), %xmm1    # 0x4d8304
addss %xmm1, %xmm0
addss %xmm2, %xmm2
addss %xmm1, %xmm2
mulss 0xec(%rsp), %xmm0
movss %xmm0, 0x134(%rsp)
movss %xmm2, 0x138(%rsp)
movss 0xe0(%rsp), %xmm0
movss %xmm0, 0x13c(%rsp)
movq 0x148(%rsp), %rbx
movq %rbx, %rdi
movq 0x1d8(%rsp), %rsi
leaq 0x134(%rsp), %rdx
callq 0x408297
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x40759c
movq 0x1f8(%rsp), %rbp
movq 0x208(%rsp), %rax
movl $0x1, %edx
xorps %xmm0, %xmm0
movss %xmm0, 0x74(%rsp)
movss %xmm0, 0x70(%rsp)
movss %xmm0, 0x6c(%rsp)
movq 0xa8(%rsp), %r14
movq 0x78(%rsp), %r13
testl %edx, %edx
jle 0x41611f
xorl %ecx, %ecx
movl %ecx, 0xd8(%rsp)
movq %rcx, 0xd0(%rsp)
movl %ecx, 0xc8(%rsp)
movq %rcx, 0xc0(%rsp)
movq %rdx, 0xb8(%rsp)
leal -0x1(%rdx), %ebx
movq %rbx, %rdx
shlq $0x5, %rdx
movq 0x1e0(%rsp), %rcx
movups (%rcx,%rdx), %xmm0
movq %rdx, 0xb0(%rsp)
movups 0x10(%rcx,%rdx), %xmm1
movaps %xmm1, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
leaq 0x160(%rsp), %rcx
movq %rcx, 0x28(%rsp)
leaq 0x174(%rsp), %rcx
movq %rcx, 0x20(%rsp)
leaq 0x9c(%rsp), %rcx
movq %rcx, 0x18(%rsp)
movq %rax, 0x8(%rsp)
movl 0x200(%rsp), %ecx
movl %ecx, (%rsp)
leaq 0x100(%rsp), %rdi
movq 0x1e8(%rsp), %rsi
movq %r13, %rdx
movq %r14, %rcx
movq 0x1f0(%rsp), %r8
movq %rbp, %r9
callq 0x41557c
movl %eax, %ecx
testb %al, %al
je 0x415eb2
movb %cl, 0x5f(%rsp)
movq %rbx, 0x60(%rsp)
movq 0x160(%rsp), %rbx
leaq 0xc0(%rsp), %rcx
movq %rcx, 0x48(%rsp)
leaq 0xd0(%rsp), %rcx
movq %rcx, 0x40(%rsp)
movl 0x220(%rsp), %ecx
movl %ecx, 0x38(%rsp)
movq 0x218(%rsp), %rcx
movq %rcx, 0x30(%rsp)
movq 0x208(%rsp), %rcx
movq %rcx, 0x20(%rsp)
movl 0x200(%rsp), %ecx
movl %ecx, 0x18(%rsp)
movq %rbp, 0x10(%rsp)
movq 0x1f0(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movq %r14, (%rsp)
leaq 0x100(%rsp), %rdi
movq %rbx, %rsi
leaq 0x9c(%rsp), %rdx
leaq 0x174(%rsp), %rbp
movq %rbp, %rcx
movq 0x1e8(%rsp), %r8
movq %r13, %r9
callq 0x415743
movss (%rbx), %xmm0
movss 0x4(%rbx), %xmm1
mulss 0xd0(%rsp), %xmm0
addss 0xc0(%rsp), %xmm0
mulss 0xd4(%rsp), %xmm1
addss 0xc4(%rsp), %xmm1
movq %rbx, 0x158(%rsp)
movss 0x8(%rbx), %xmm2
mulss 0xd8(%rsp), %xmm2
addss 0xc8(%rsp), %xmm2
movss 0x11c(%rsp), %xmm3
mulss %xmm3, %xmm0
mulss %xmm3, %xmm1
mulss %xmm3, %xmm2
movss 0x6c(%rsp), %xmm3
addss %xmm0, %xmm3
movss %xmm3, 0x6c(%rsp)
movss 0x70(%rsp), %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x70(%rsp)
movss 0x74(%rsp), %xmm0
addss %xmm2, %xmm0
movss %xmm0, 0x74(%rsp)
movl 0x240(%rsp), %r13d
testl %r13d, %r13d
je 0x415ebf
movl 0x238(%rsp), %eax
cmpl %eax, 0x118(%rsp)
jge 0x415ebf
movq %rbp, %rdx
leaq 0x90(%rsp), %rbp
movq %rbp, %rdi
leaq 0x10c(%rsp), %rsi
callq 0x4082e0
movq %rbp, %rdi
movq %rbp, %rsi
callq 0x40759c
cmpl $0x1, 0x240(%rsp)
movq 0x60(%rsp), %rbx
jne 0x415eea
movq 0xb0(%rsp), %rcx
addq 0x1e0(%rsp), %rcx
movss 0x9c(%rsp), %xmm0
movss %xmm0, (%rcx)
movss 0xa0(%rsp), %xmm0
movss %xmm0, 0x4(%rcx)
movss 0xa4(%rsp), %xmm0
movss %xmm0, 0x8(%rcx)
movss 0x11c(%rsp), %xmm0
mulss 0xc24bb(%rip), %xmm0    # 0x4d8310
movss %xmm0, 0x1c(%rcx)
movl 0x118(%rsp), %eax
incl %eax
movl %eax, 0x18(%rcx)
movss 0x90(%rsp), %xmm0
movss %xmm0, 0xc(%rcx)
movss 0x94(%rsp), %xmm0
movss %xmm0, 0x10(%rcx)
movss 0x98(%rsp), %xmm0
movss %xmm0, 0x14(%rcx)
movq 0x78(%rsp), %r13
movq 0x208(%rsp), %rax
movq 0x1f8(%rsp), %rbp
movq 0xb8(%rsp), %rdx
jmp 0x4160ee
movq 0x208(%rsp), %rax
jmp 0x416115
movq 0xa8(%rsp), %r14
movq 0x78(%rsp), %r13
movq 0x208(%rsp), %rax
movq 0x1f8(%rsp), %rbp
movq 0x60(%rsp), %rbx
movb 0x5f(%rsp), %cl
jmp 0x416115
movb 0x5f(%rsp), %cl
jle 0x4160f4
movq 0x94(%rsp), %rax
rolq $0x20, %rax
movq %rax, 0x128(%rsp)
movss 0x90(%rsp), %xmm0
xorps 0xc0d38(%rip), %xmm0    # 0x4d6c50
movss %xmm0, 0x130(%rsp)
leaq 0x168(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0x128(%rsp), %rdx
callq 0x4074f2
movslq %ebx, %rdx
xorl %r14d, %r14d
movq 0x1e0(%rsp), %rbp
movq %rdx, 0xb8(%rsp)
movq 0x158(%rsp), %rax
movss 0x18(%rax), %xmm0
movss %xmm0, 0xb0(%rsp)
mulss %xmm0, %xmm0
movss %xmm0, 0x60(%rsp)
movq %r12, %rdi
movl $0x2, %esi
movq %r15, %rdx
callq 0x40a139
xorps %xmm0, %xmm0
cvtsd2ss 0xf0(%rsp), %xmm0
movss 0xb0(%rsp), %xmm1
mulss %xmm1, %xmm0
xorps %xmm3, %xmm3
cvtsd2ss 0xf8(%rsp), %xmm3
mulss %xmm1, %xmm3
movaps %xmm3, %xmm1
mulss %xmm3, %xmm1
movaps %xmm0, %xmm2
mulss %xmm0, %xmm2
addss %xmm1, %xmm2
ucomiss 0x60(%rsp), %xmm2
ja 0x415f74
leaq 0x18c(%rsp), %r13
movq %r13, %rdi
leaq 0x128(%rsp), %rsi
movss %xmm3, 0x60(%rsp)
callq 0x40766c
movss 0xb0(%rsp), %xmm0
mulss 0x60(%rsp), %xmm0
leaq 0x180(%rsp), %rbx
movq %rbx, %rdi
leaq 0x168(%rsp), %rsi
callq 0x40766c
movq %r15, %rdi
leaq 0x90(%rsp), %rsi
movq %r13, %rdx
callq 0x407616
movq %r15, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x407616
movq %r15, %rdi
movq %r15, %rsi
callq 0x40759c
movss 0x9c(%rsp), %xmm0
movq 0xb8(%rsp), %rdx
movq %rdx, %rax
shlq $0x5, %rax
movss %xmm0, (%rbp,%rax)
movss 0xa0(%rsp), %xmm0
movss %xmm0, 0x4(%rbp,%rax)
movss 0xa4(%rsp), %xmm0
movss %xmm0, 0x8(%rbp,%rax)
movss 0x11c(%rsp), %xmm0
mulss 0x8c(%rsp), %xmm0
movss %xmm0, 0x1c(%rbp,%rax)
movl 0x118(%rsp), %ecx
incl %ecx
movl %ecx, 0x18(%rbp,%rax)
movss 0xf0(%rsp), %xmm0
movss %xmm0, 0xc(%rbp,%rax)
movss 0xf4(%rsp), %xmm0
movss %xmm0, 0x10(%rbp,%rax)
movss 0xf8(%rsp), %xmm0
movss %xmm0, 0x14(%rbp,%rax)
incq %rdx
incl %r14d
cmpl 0x240(%rsp), %r14d
jne 0x415f4c
movq 0x78(%rsp), %r13
movq 0x208(%rsp), %rax
movq 0x1f8(%rsp), %rbp
movb 0x5f(%rsp), %cl
jmp 0x41610b
movl %ebx, %edx
movq 0x78(%rsp), %r13
movq 0x208(%rsp), %rax
movq 0x1f8(%rsp), %rbp
movl %edx, %ebx
movq 0xa8(%rsp), %r14
movl %ebx, %edx
testb %cl, %cl
jne 0x415bb4
movq 0x150(%rsp), %rdx
movq %rdx, %rax
subq 0x140(%rsp), %rax
leaq (%rax,%rax,2), %rax
movq 0x230(%rsp), %rcx
movss 0x6c(%rsp), %xmm0
movss %xmm0, (%rcx,%rax,4)
movss 0x70(%rsp), %xmm0
movss %xmm0, 0x4(%rcx,%rax,4)
movss 0x74(%rsp), %xmm0
movss %xmm0, 0x8(%rcx,%rax,4)
incq %rdx
cmpl 0x88(%rsp), %edx
movq 0x1e0(%rsp), %rax
jne 0x415a9d
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq