// File: /workspace/github_c_makefile_star2_O2/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 $0x1e8, %rsp            # imm = 0x1E8
movq %r9, 0x80(%rsp)
movq %r8, 0x68(%rsp)
movss %xmm0, 0xb0(%rsp)
movl %ecx, %r14d
movl %edx, 0x78(%rsp)
movl %esi, %ebx
movl %edi, %r12d
movl 0x290(%rsp), %eax
imull 0x288(%rsp), %eax
xorps %xmm0, %xmm0
movss %xmm0, 0x7c(%rsp)
testl %eax, %eax
je 0x413369
cvtsi2ss %eax, %xmm0
movss 0xbf109(%rip), %xmm2    # 0x4d2460
divss %xmm0, %xmm2
mulss 0xc1f95(%rip), %xmm2    # 0x4d52f8
movss %xmm2, 0x7c(%rsp)
movq 0x278(%rsp), %r15
movq 0x258(%rsp), %r13
movl 0x250(%rsp), %ebp
mulss 0xbf0f4(%rip), %xmm1    # 0x4d247c
movaps %xmm1, %xmm0
callq 0x4158e0
cvtsi2ssl 0x78(%rsp), %xmm1
movss %xmm1, 0xac(%rsp)
cvtsi2ss %r14d, %xmm1
movss %xmm1, 0xa8(%rsp)
movss 0xc088b(%rip), %xmm1    # 0x4d3c40
divss %xmm0, %xmm1
movss %xmm1, 0xa4(%rsp)
movq 0x230(%rsp), %rax
addq $0xc, %rax
movq %rax, 0x190(%rsp)
movslq %r12d, %rdx
movl %ebp, %r12d
movslq %ebx, %rcx
movq %rcx, 0x198(%rsp)
pushq $0x2
popq %rbx
leaq 0x130(%rsp), %r14
movq %rdx, 0x188(%rsp)
cmpq 0x198(%rsp), %rdx
jge 0x413a51
movq 0x230(%rsp), %rax
andl $0x0, 0x18(%rax)
movq 0x220(%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, 0x1a0(%rsp)
movl %edx, %eax
cltd
idivl 0x78(%rsp)
cvtsi2ss %edx, %xmm0
cvtsi2ss %eax, %xmm1
divss 0xac(%rsp), %xmm0
divss 0xa8(%rsp), %xmm1
movss 0xbeff0(%rip), %xmm2    # 0x4d2460
subss %xmm1, %xmm2
addss %xmm0, %xmm0
movss 0xc07c0(%rip), %xmm1    # 0x4d3c40
addss %xmm1, %xmm0
addss %xmm2, %xmm2
addss %xmm1, %xmm2
mulss 0xb0(%rsp), %xmm0
movss %xmm0, 0x124(%rsp)
movss %xmm2, 0x128(%rsp)
movss 0xa4(%rsp), %xmm0
movss %xmm0, 0x12c(%rsp)
movq 0x190(%rsp), %rbp
movq %rbp, %rdi
movq 0x228(%rsp), %rsi
leaq 0x124(%rsp), %rdx
callq 0x4079d0
movq %rbp, %rdi
movq %rbp, %rsi
callq 0x406f42
xorps %xmm1, %xmm1
xorps %xmm0, %xmm0
pushq $0x1
popq %rax
movq 0x68(%rsp), %rdx
movq 0x80(%rsp), %rbp
testl %eax, %eax
jle 0x413a20
movaps %xmm1, 0xc0(%rsp)
movss %xmm0, 0x54(%rsp)
andl $0x0, 0xe8(%rsp)
andq $0x0, 0xe0(%rsp)
andl $0x0, 0xd8(%rsp)
andq $0x0, 0xd0(%rsp)
movq %rax, 0x98(%rsp)
decl %eax
movq %rax, 0x58(%rsp)
movq %rax, %rcx
shlq $0x5, %rcx
movq 0x230(%rsp), %rax
movups (%rax,%rcx), %xmm0
movq %rcx, 0x70(%rsp)
movups 0x10(%rax,%rcx), %xmm1
movaps %xmm1, 0x100(%rsp)
movaps %xmm0, 0xf0(%rsp)
leaq 0x1b0(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x1c4(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0xb4(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %r13, 0x8(%rsp)
movl %r12d, (%rsp)
leaq 0xf0(%rsp), %rdi
movq 0x238(%rsp), %rsi
movq %rbp, %rcx
movq 0x240(%rsp), %r8
movq 0x248(%rsp), %r9
callq 0x412f6b
testb %al, %al
je 0x413a12
movq %rbp, %rax
movq 0x1b0(%rsp), %rbp
leaq 0xd0(%rsp), %rcx
movq %rcx, 0x48(%rsp)
leaq 0xe0(%rsp), %rcx
movq %rcx, 0x40(%rsp)
movl 0x270(%rsp), %ecx
movl %ecx, 0x38(%rsp)
movq 0x268(%rsp), %rcx
movq %rcx, 0x30(%rsp)
movq %r13, 0x20(%rsp)
movl %r12d, 0x18(%rsp)
movq 0x248(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq 0x240(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movq %rax, (%rsp)
leaq 0xf0(%rsp), %rdi
movq %rbp, %rsi
leaq 0xb4(%rsp), %rdx
leaq 0x1c4(%rsp), %r12
movq %r12, %rcx
movq 0x238(%rsp), %r8
movq 0x68(%rsp), %r9
callq 0x413125
movss 0x8(%rbp), %xmm2
mulss 0xe8(%rsp), %xmm2
addss 0xd8(%rsp), %xmm2
movss 0x10c(%rsp), %xmm3
movq %rbp, 0x1a8(%rsp)
movsd (%rbp), %xmm4
movsd 0xe0(%rsp), %xmm6
movsd 0xd0(%rsp), %xmm5
movl 0x290(%rsp), %r13d
testl %r13d, %r13d
je 0x4137aa
movl 0x288(%rsp), %eax
cmpl %eax, 0x108(%rsp)
jge 0x4137aa
movq %r12, %rdx
movaps %xmm6, 0x140(%rsp)
movaps %xmm5, 0x150(%rsp)
movaps %xmm4, 0x160(%rsp)
movaps %xmm3, 0x170(%rsp)
movss %xmm2, 0x64(%rsp)
leaq 0x8c(%rsp), %r12
movq %r12, %rdi
leaq 0xfc(%rsp), %rsi
callq 0x407a15
movq %r12, %rdi
movq %r12, %rsi
callq 0x406f42
cmpl $0x1, 0x290(%rsp)
movl 0x250(%rsp), %r12d
jne 0x4137df
movq 0x70(%rsp), %rcx
addq 0x230(%rsp), %rcx
movsd 0xb8(%rsp), %xmm0
movss 0x10c(%rsp), %xmm1
mulss 0xc1bac(%rip), %xmm1    # 0x4d52fc
movss 0xb4(%rsp), %xmm2
movss %xmm1, 0x1c(%rcx)
movl 0x108(%rsp), %eax
incl %eax
movl %eax, 0x18(%rcx)
movlhps %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
movss 0x8c(%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, (%rcx)
movsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x10(%rcx)
movss 0x54(%rsp), %xmm0
movaps 0xc0(%rsp), %xmm1
movq 0x98(%rsp), %rsi
jmp 0x4139b8
movq 0x80(%rsp), %rbp
movq 0x68(%rsp), %rdx
movq 0x258(%rsp), %r13
movl 0x250(%rsp), %r12d
movss 0x54(%rsp), %xmm0
movaps 0xc0(%rsp), %xmm1
movq 0x58(%rsp), %rax
jmp 0x4139f5
jle 0x41399b
movsd 0x90(%rsp), %xmm0
shufps $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movlps %xmm0, 0x118(%rsp)
movss 0x8c(%rsp), %xmm0
xorps 0xc0446(%rip), %xmm0    # 0x4d3c50
movss %xmm0, 0x120(%rsp)
leaq 0x1b8(%rsp), %rdi
leaq 0x8c(%rsp), %rsi
leaq 0x118(%rsp), %rdx
callq 0x406eba
movl 0x58(%rsp), %esi
xorl %r13d, %r13d
cmpl 0x290(%rsp), %r13d
je 0x4139a2
movq %rsi, 0x98(%rsp)
movq 0x1a8(%rsp), %rax
movss 0x18(%rax), %xmm0
movss %xmm0, 0x58(%rsp)
mulss %xmm0, %xmm0
movss %xmm0, 0x70(%rsp)
movq %r15, %rdi
movl %ebx, %esi
movq %r14, %rdx
callq 0x409364
cvtsd2ss 0x130(%rsp), %xmm0
movss 0x58(%rsp), %xmm1
mulss %xmm1, %xmm0
cvtsd2ss 0x138(%rsp), %xmm3
mulss %xmm1, %xmm3
movaps %xmm3, %xmm1
mulss %xmm3, %xmm1
movaps %xmm0, %xmm2
mulss %xmm0, %xmm2
addss %xmm1, %xmm2
ucomiss 0x70(%rsp), %xmm2
ja 0x41386a
leaq 0x1dc(%rsp), %rbp
movq %rbp, %rdi
leaq 0x118(%rsp), %rsi
movss %xmm3, 0x70(%rsp)
callq 0x406fe5
movss 0x58(%rsp), %xmm0
mulss 0x70(%rsp), %xmm0
leaq 0x1d0(%rsp), %r12
movq %r12, %rdi
leaq 0x1b8(%rsp), %rsi
callq 0x406fe5
movq %r14, %rdi
leaq 0x8c(%rsp), %rsi
movq %rbp, %rdx
callq 0x406f8f
movq %r14, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x406f8f
movq %r14, %rdi
movq %r14, %rsi
callq 0x406f42
movq 0x98(%rsp), %rsi
movq %rsi, %rax
shlq $0x5, %rax
movsd 0xb8(%rsp), %xmm0
movss 0x10c(%rsp), %xmm1
mulss 0x7c(%rsp), %xmm1
movss 0xb4(%rsp), %xmm2
movq 0x230(%rsp), %rdx
movss %xmm1, 0x1c(%rdx,%rax)
movl 0x108(%rsp), %ecx
incl %ecx
movl %ecx, 0x18(%rdx,%rax)
movlhps %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
movss 0x130(%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 0x134(%rsp), %xmm0
movsd %xmm0, 0x10(%rdx,%rax)
incq %rsi
incl %r13d
jmp 0x413837
movq 0x58(%rsp), %rsi
jmp 0x4139aa
movl 0x250(%rsp), %r12d
movss 0x54(%rsp), %xmm0
movaps 0xc0(%rsp), %xmm1
movss 0x64(%rsp), %xmm2
movaps 0x170(%rsp), %xmm3
movaps 0x160(%rsp), %xmm4
movaps 0x150(%rsp), %xmm5
movaps 0x140(%rsp), %xmm6
movl %esi, %eax
movq 0x80(%rsp), %rbp
movq 0x68(%rsp), %rdx
movq 0x258(%rsp), %r13
mulps %xmm6, %xmm4
addps %xmm5, %xmm4
mulss %xmm3, %xmm2
shufps $0x0, %xmm3, %xmm3      # xmm3 = xmm3[0,0,0,0]
mulps %xmm4, %xmm3
addps %xmm3, %xmm1
addss %xmm2, %xmm0
jmp 0x4134fa
movss 0x54(%rsp), %xmm0
movaps 0xc0(%rsp), %xmm1
movq 0x1a0(%rsp), %rdx
movq %rdx, %rax
subq 0x188(%rsp), %rax
imulq $0xc, %rax, %rax
movq 0x280(%rsp), %rcx
movlps %xmm1, (%rcx,%rax)
movss %xmm0, 0x8(%rcx,%rax)
incq %rdx
jmp 0x4133fa
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq