// File: /workspace/github_c_makefile_star2_O3/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 $0x1d8, %rsp            # imm = 0x1D8
movq %r9, %rbp
movq %r8, 0x58(%rsp)
movss %xmm0, 0xa0(%rsp)
movl %ecx, %r14d
movl %edx, 0x7c(%rsp)
movl %esi, 0x80(%rsp)
movl %edi, %ebx
movl 0x280(%rsp), %eax
imull 0x278(%rsp), %eax
xorps %xmm0, %xmm0
movss %xmm0, 0x84(%rsp)
testl %eax, %eax
je 0x416aab
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss 0xc0e8a(%rip), %xmm2    # 0x4d7920
divss %xmm0, %xmm2
mulss 0xc29ea(%rip), %xmm2    # 0x4d948c
movss %xmm2, 0x84(%rsp)
mulss 0xbf9d5(%rip), %xmm1    # 0x4d6488
movaps %xmm1, %xmm0
callq 0x419040
cmpl 0x80(%rsp), %ebx
jge 0x4171a2
movq 0x268(%rsp), %r12
movq 0x220(%rsp), %rax
xorps %xmm1, %xmm1
cvtsi2ssl 0x7c(%rsp), %xmm1
movss %xmm1, 0x9c(%rsp)
xorps %xmm1, %xmm1
cvtsi2ss %r14d, %xmm1
movss %xmm1, 0x98(%rsp)
movss 0xc2981(%rip), %xmm1    # 0x4d9484
divss %xmm0, %xmm1
movss %xmm1, 0x94(%rsp)
leaq 0xc(%rax), %rcx
movq %rcx, 0x180(%rsp)
movslq %ebx, %rdx
leaq 0x110(%rsp), %rbx
movq %rdx, 0x178(%rsp)
movq %rbp, 0x190(%rsp)
movl $0x0, 0x18(%rax)
movq 0x210(%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, 0x188(%rsp)
movl %edx, %eax
cltd
idivl 0x7c(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
divss 0x9c(%rsp), %xmm0
divss 0x98(%rsp), %xmm1
movss 0xc0d80(%rip), %xmm2    # 0x4d7920
subss %xmm1, %xmm2
addss %xmm0, %xmm0
movss 0xc28d4(%rip), %xmm1    # 0x4d9484
addss %xmm1, %xmm0
addss %xmm2, %xmm2
addss %xmm1, %xmm2
mulss 0xa0(%rsp), %xmm0
movss %xmm0, 0x104(%rsp)
movss %xmm2, 0x108(%rsp)
movss 0x94(%rsp), %xmm0
movss %xmm0, 0x10c(%rsp)
movq 0x180(%rsp), %r14
movq %r14, %rdi
movq 0x218(%rsp), %rsi
leaq 0x104(%rsp), %rdx
callq 0x40865a
movq %r14, %rdi
movq %r14, %rsi
callq 0x40761b
movq 0x238(%rsp), %r13
movq 0x248(%rsp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x160(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x68(%rsp)
movl $0x1, %edx
movq 0x58(%rsp), %r15
movq %rbp, %r9
xorl %ecx, %ecx
movl %ecx, 0xc8(%rsp)
movq %rcx, 0xc0(%rsp)
movl %ecx, 0xb8(%rsp)
movq %rcx, 0xb0(%rsp)
movq %rdx, 0x70(%rsp)
leal -0x1(%rdx), %r14d
movq %r14, %rbp
shlq $0x5, %rbp
movq 0x220(%rsp), %rcx
movups (%rcx,%rbp), %xmm0
movups 0x10(%rcx,%rbp), %xmm1
movaps %xmm1, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
leaq 0x1a0(%rsp), %rcx
movq %rcx, 0x28(%rsp)
leaq 0x1b4(%rsp), %rcx
movq %rcx, 0x20(%rsp)
leaq 0xa4(%rsp), %rcx
movq %rcx, 0x18(%rsp)
movq %rax, 0x8(%rsp)
movl 0x240(%rsp), %ecx
movl %ecx, (%rsp)
leaq 0xd0(%rsp), %rdi
movq 0x228(%rsp), %rsi
movq %r15, %rdx
movq %r9, %rcx
movq 0x230(%rsp), %r8
movq %r9, %r15
movq %r13, %r9
callq 0x416687
testb %al, %al
je 0x41714d
movq %r15, %rax
movq 0x1a0(%rsp), %r15
leaq 0xb0(%rsp), %rcx
movq %rcx, 0x48(%rsp)
leaq 0xc0(%rsp), %rcx
movq %rcx, 0x40(%rsp)
movl 0x260(%rsp), %ecx
movl %ecx, 0x38(%rsp)
movq 0x258(%rsp), %rcx
movq %rcx, 0x30(%rsp)
movq 0x248(%rsp), %rcx
movq %rcx, 0x20(%rsp)
movl 0x240(%rsp), %ecx
movl %ecx, 0x18(%rsp)
movq %r13, 0x10(%rsp)
movq 0x230(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movq %rax, (%rsp)
leaq 0xd0(%rsp), %rdi
movq %r15, %rsi
leaq 0xa4(%rsp), %rdx
leaq 0x1b4(%rsp), %r13
movq %r13, %rcx
movq 0x228(%rsp), %r8
movq 0x58(%rsp), %r9
callq 0x416841
movss 0x8(%r15), %xmm1
mulss 0xc8(%rsp), %xmm1
addss 0xb8(%rsp), %xmm1
movss 0xec(%rsp), %xmm2
movq %r15, 0x198(%rsp)
movsd (%r15), %xmm0
movsd 0xc0(%rsp), %xmm4
movsd 0xb0(%rsp), %xmm3
movl 0x280(%rsp), %r15d
testl %r15d, %r15d
je 0x416ebf
movl 0x278(%rsp), %eax
cmpl %eax, 0xe8(%rsp)
jge 0x416ebf
movq %r13, %rdx
movaps %xmm4, 0x120(%rsp)
movaps %xmm3, 0x130(%rsp)
movaps %xmm0, 0x140(%rsp)
movaps %xmm2, 0x150(%rsp)
movss %xmm1, 0x64(%rsp)
leaq 0x88(%rsp), %r13
movq %r13, %rdi
leaq 0xdc(%rsp), %rsi
callq 0x40869b
movq %r13, %rdi
movq %r13, %rsi
callq 0x40761b
cmpl $0x1, %r15d
jne 0x416ed9
addq 0x220(%rsp), %rbp
movsd 0xa8(%rsp), %xmm0
movss 0xec(%rsp), %xmm1
mulss 0xc261b(%rip), %xmm1    # 0x4d9490
movss 0xa4(%rsp), %xmm2
movss %xmm1, 0x1c(%rbp)
movl 0xe8(%rsp), %eax
incl %eax
movl %eax, 0x18(%rbp)
movlhps %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
movss 0x88(%rsp), %xmm1
shufps $0xd4, %xmm0, %xmm1     # xmm1 = xmm1[0,1],xmm0[1,3]
shufps $0x28, %xmm1, %xmm2     # xmm2 = xmm2[0,2],xmm1[2,0]
movups %xmm2, (%rbp)
movsd 0x8c(%rsp), %xmm0
movsd %xmm0, 0x10(%rbp)
movq 0x70(%rsp), %r14
jmp 0x4170c8
movq 0x58(%rsp), %r15
movq 0x248(%rsp), %rax
movq 0x238(%rsp), %r13
jmp 0x417103
movq 0x58(%rsp), %r15
movq 0x248(%rsp), %rax
movq 0x238(%rsp), %r13
movss 0x64(%rsp), %xmm1
movaps 0x150(%rsp), %xmm2
movaps 0x140(%rsp), %xmm0
movaps 0x130(%rsp), %xmm3
movaps 0x120(%rsp), %xmm4
jle 0x417103
movsd 0x8c(%rsp), %xmm0
shufps $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movlps %xmm0, 0xf8(%rsp)
movss 0x88(%rsp), %xmm0
xorps 0xc0a11(%rip), %xmm0    # 0x4d7950
movss %xmm0, 0x100(%rsp)
leaq 0x1a8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
leaq 0xf8(%rsp), %rdx
callq 0x407586
movl 0x70(%rsp), %r14d
decq %r14
xorl %ebp, %ebp
movq 0x198(%rsp), %rax
movss 0x18(%rax), %xmm0
movss %xmm0, 0x70(%rsp)
mulss %xmm0, %xmm0
movss %xmm0, 0x6c(%rsp)
movq %r12, %rdi
movl $0x2, %esi
movq %rbx, %rdx
callq 0x40a59a
xorps %xmm0, %xmm0
cvtsd2ss 0x110(%rsp), %xmm0
movss 0x70(%rsp), %xmm1
mulss %xmm1, %xmm0
xorps %xmm3, %xmm3
cvtsd2ss 0x118(%rsp), %xmm3
mulss %xmm1, %xmm3
movaps %xmm3, %xmm1
mulss %xmm3, %xmm1
movaps %xmm0, %xmm2
mulss %xmm0, %xmm2
addss %xmm1, %xmm2
ucomiss 0x6c(%rsp), %xmm2
ja 0x416f8c
leaq 0x1cc(%rsp), %r15
movq %r15, %rdi
leaq 0xf8(%rsp), %rsi
movss %xmm3, 0x6c(%rsp)
callq 0x4076c7
movss 0x70(%rsp), %xmm0
mulss 0x6c(%rsp), %xmm0
leaq 0x1c0(%rsp), %r13
movq %r13, %rdi
leaq 0x1a8(%rsp), %rsi
callq 0x4076c7
movq %rbx, %rdi
leaq 0x88(%rsp), %rsi
movq %r15, %rdx
callq 0x407671
movq %rbx, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x407671
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x40761b
movq %r14, %rax
shlq $0x5, %rax
movsd 0xa8(%rsp), %xmm0
movss 0xec(%rsp), %xmm1
mulss 0x84(%rsp), %xmm1
movss 0xa4(%rsp), %xmm2
movq 0x220(%rsp), %rdx
movss %xmm1, 0x1c(%rdx,%rax)
movl 0xe8(%rsp), %ecx
incl %ecx
movl %ecx, 0x18(%rdx,%rax)
movlhps %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
movss 0x110(%rsp), %xmm1
shufps $0xd4, %xmm0, %xmm1     # xmm1 = xmm1[0,1],xmm0[1,3]
shufps $0x28, %xmm1, %xmm2     # xmm2 = xmm2[0,2],xmm1[2,0]
movups %xmm2, (%rdx,%rax)
movsd 0x114(%rsp), %xmm0
movsd %xmm0, 0x10(%rdx,%rax)
incq %r14
incl %ebp
cmpl 0x280(%rsp), %ebp
jne 0x416f6f
movq 0x58(%rsp), %r15
movq 0x248(%rsp), %rax
movq 0x238(%rsp), %r13
movss 0x64(%rsp), %xmm1
movaps 0x150(%rsp), %xmm2
movaps 0x140(%rsp), %xmm0
movaps 0x130(%rsp), %xmm3
movaps 0x120(%rsp), %xmm4
mulps %xmm4, %xmm0
addps %xmm3, %xmm0
mulss %xmm2, %xmm1
shufps $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0]
mulps %xmm0, %xmm2
movaps 0x160(%rsp), %xmm0
addps %xmm2, %xmm0
movaps %xmm0, 0x160(%rsp)
movss 0x68(%rsp), %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x68(%rsp)
movl %r14d, %edx
testl %r14d, %r14d
movq 0x190(%rsp), %r9
jg 0x416c45
jmp 0x417150
movq %r15, %r9
movq %r9, %rbp
movq 0x188(%rsp), %rdx
movq %rdx, %rax
subq 0x178(%rsp), %rax
leaq (%rax,%rax,2), %rax
movq 0x270(%rsp), %rcx
movaps 0x160(%rsp), %xmm0
movlps %xmm0, (%rcx,%rax,4)
movss 0x68(%rsp), %xmm0
movss %xmm0, 0x8(%rcx,%rax,4)
incq %rdx
cmpl 0x80(%rsp), %edx
movq 0x220(%rsp), %rax
jne 0x416b37
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq