Subversion Repositories Kolibri OS

Rev

Rev 5218 | Rev 5262 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. align 4
  3. proc glopMaterial uses eax ebx ecx edi esi, context:dword, p:dword
  4. ; edi -> GLMaterial *m
  5.         mov eax,[context]
  6.         mov ebx,[p]
  7.         mov ecx,[ebx+4] ;ecx = p[1]
  8.  
  9.         cmp ecx,GL_FRONT_AND_BACK ;if (mode == GL_FRONT_AND_BACK)
  10.         jne @f
  11.                 mov dword[ebx+4],GL_FRONT ;p[1].i=GL_FRONT
  12.                 mov edi,ebp
  13.                 add edi,12
  14.                 stdcall glopMaterial,eax,edi
  15.                 mov ecx,GL_BACK
  16.         @@:
  17.         mov edi,[eax+offs_cont_materials]
  18.         cmp ecx,GL_FRONT ;if (mode == GL_FRONT) m=&context.materials[0]
  19.         je @f
  20.                 add edi,sizeof.GLMaterial ;else m=&context.materials[1]
  21.         @@:
  22.  
  23.         mov ecx,4
  24.         mov esi,ebx ;esi = &p
  25.         add esi,12 ;esi = &p[3]
  26.         mov ebx,[ebx+8] ;ebx = p[2]
  27.         cmp ebx,GL_EMISSION
  28.         jne @f
  29.                 ;add edi,offs_mate_emission ;offs_mate_emission=0
  30.                 rep movsd
  31.                 jmp .end_f
  32.         @@:
  33.         cmp ebx,GL_AMBIENT
  34.         jne @f
  35.                 add edi,offs_mate_ambient
  36.                 rep movsd
  37.                 jmp .end_f
  38.         @@:
  39.         cmp ebx,GL_DIFFUSE
  40.         jne @f
  41.                 add edi,offs_mate_diffuse
  42.                 rep movsd
  43.                 jmp .end_f
  44.         @@:
  45.         cmp ebx,GL_SPECULAR
  46.         jne @f
  47.                 add edi,offs_mate_specular
  48.                 rep movsd
  49.                 jmp .end_f
  50.         @@:
  51.         cmp ebx,GL_SHININESS
  52.         jne @f
  53.                 fld dword[esi]
  54.                 add edi,offs_mate_shininess
  55.                 movsd
  56.                 mov dword[edi],SPECULAR_BUFFER_RESOLUTION
  57.                 fdiv dword[an180f]
  58.                 fimul dword[edi]
  59.                 fistp dword[edi] ;m.shininess_i = (v[0]/128.0f)*SPECULAR_BUFFER_RESOLUTION
  60.                 jmp .end_f
  61.         @@:
  62.         cmp ebx,GL_AMBIENT_AND_DIFFUSE
  63.         jne @f
  64.                 add edi,offs_mate_ambient
  65.                 rep movsd
  66.                 sub esi,16
  67.                 ;edi = &offs_mate_diffuse
  68.                 mov ecx,4
  69.                 rep movsd
  70.                 jmp .end_f
  71.         @@: ;default
  72. ;    assert(0);
  73.         .end_f:
  74.         ret
  75. endp
  76.  
  77. align 4
  78. proc glopColorMaterial uses eax ebx ecx, context:dword, p:dword
  79.         mov eax,[context]
  80.         mov ebx,[p]
  81.         mov ecx,[ebx+4] ;ecx = p[1]
  82.         mov dword[eax+offs_cont_current_color_material_mode],ecx
  83.         mov ecx,[ebx+8] ;ecx = p[2]
  84.         mov dword[eax+offs_cont_current_color_material_type],ecx
  85.         ret
  86. endp
  87.  
  88. align 4
  89. proc glopLight context:dword, p:dword
  90. locals
  91.         pos V4
  92. endl
  93. pushad
  94.         mov eax,[context]
  95.         mov ebx,[p]
  96.         mov edx,[ebx+4] ;edx = p[1]
  97.  
  98. ;  assert(edx >= GL_LIGHT0 && edx < GL_LIGHT0+MAX_LIGHTS );
  99.  
  100.         sub edx,GL_LIGHT0
  101.         imul edx,sizeof.GLLight
  102.         add edx,eax
  103.         add edx,offs_cont_lights
  104.  
  105.         mov ecx,[ebx+8] ;ecx = p[2]
  106.         cmp ecx,GL_AMBIENT
  107.         jne @f
  108.                 mov esi,ebx
  109.                 add esi,12
  110.                 mov edi,edx
  111.                 ;add edi,offs_ligh_ambient ;offs_ligh_ambient = 0
  112.                 mov ecx,4
  113.                 rep movsd ;l.ambient=v
  114.                 jmp .end_f
  115.         @@:
  116.         cmp ecx,GL_DIFFUSE
  117.         jne @f
  118.                 mov esi,ebx
  119.                 add esi,12
  120.                 mov edi,edx
  121.                 add edi,offs_ligh_diffuse
  122.                 mov ecx,4
  123.                 rep movsd ;l.diffuse=v
  124.                 jmp .end_f
  125.         @@:
  126.         cmp ecx,GL_SPECULAR
  127.         jne @f
  128.                 mov esi,ebx
  129.                 add esi,12
  130.                 mov edi,edx
  131.                 add edi,offs_ligh_specular
  132.                 mov ecx,4
  133.                 rep movsd ;l.specular=v
  134.                 jmp .end_f
  135.         @@:
  136.         cmp ecx,GL_POSITION
  137.         jne @f
  138.                 mov edi,ebx ;ebx = [ebp+12] = [p] = &p[0]
  139.                 add edi,12 ;&p[3]
  140.                 mov esi,ebp
  141.                 sub esi,16 ;&pos
  142.                 stdcall gl_M4_MulV4, esi,dword[eax+offs_cont_matrix_stack_ptr],edi
  143.                 mov edi,edx
  144.                 add edi,offs_ligh_position
  145.                 mov ecx,4
  146.                 rep movsd ;l.position=pos
  147.  
  148.                 fld dword[edi-4] ;if(l.position.v[3] == 0)
  149.                 ftst
  150.                 fstsw ax
  151.                 sahf
  152.                 jne .end_i
  153.                         ;mov esi,ebp
  154.                         sub esi,16 ;&pos
  155.                         mov edi,edx
  156.                         add edi,offs_ligh_norm_position
  157.                         mov ecx,3
  158.                         rep movsd ;l.norm_position=pos[1,2,3]
  159.  
  160.                         ;mov edi,edx
  161.                         ;add edi,offs_ligh_norm_position
  162.                         sub edi,12
  163.                         stdcall gl_V3_Norm,edi ;&l.norm_position
  164.                 .end_i:
  165.                 ffree st0
  166.                 fincstp
  167.                 jmp .end_f
  168.         @@:
  169.         cmp ecx,GL_SPOT_DIRECTION
  170.         jne @f
  171.                 mov esi,ebx ;&p[0]
  172.                 add esi,12
  173.                 mov edi,edx
  174.                 add edi,offs_ligh_spot_direction
  175.                 mov ecx,3
  176.                 rep movsd ;l.spot_direction=v[0,1,2]
  177.                 mov esi,ebx
  178.                 add esi,12
  179.                 mov edi,edx
  180.                 add edi,offs_ligh_norm_spot_direction
  181.                 mov ecx,3
  182.                 rep movsd ;l.norm_spot_direction=v[0,1,2]
  183.                 add edx,offs_ligh_norm_spot_direction
  184.                 stdcall gl_V3_Norm,edx
  185.                 jmp .end_f
  186.         @@:
  187.         cmp ecx,GL_SPOT_EXPONENT
  188.         jne @f
  189.                 mov ecx,[ebx+12]
  190.                 mov [edi+offs_ligh_spot_exponent],ecx ;l.spot_exponent=p[3]
  191.                 jmp .end_f
  192.         @@:
  193.         cmp ecx,GL_SPOT_CUTOFF
  194.         jne .end_spot_c
  195.                 fld dword[ebp+12] ;float a=v.v[0]
  196. ;      assert(a == 180 || (a>=0 && a<=90));
  197.                 fst dword[edi+offs_ligh_spot_cutoff] ;l.spot_cutoff=a
  198.                 fcom dword[an180f] ;if (a != 180)
  199.                 fstsw ax
  200.                 sahf
  201.                 jne @f
  202.                         fldpi
  203.                         fmulp
  204.                         fdiv dword[an180f]
  205.                         fcos
  206.                         fstp dword[edi+offs_ligh_spot_cutoff] ;l.cos_spot_cutoff=cos(a * M_PI / 180.0)
  207.                         jmp .end_f
  208.                 @@:
  209.                 ffree st0
  210.                 fincstp
  211.                 jmp .end_f
  212.         .end_spot_c:
  213.         cmp ecx,GL_CONSTANT_ATTENUATION
  214.                 mov ecx,[ebx+12]
  215.                 mov [edi+offs_ligh_attenuation],ecx ;l->attenuation[0]=p[3]
  216.                 jmp .end_f
  217.         @@:
  218.         cmp ecx,GL_LINEAR_ATTENUATION
  219.                 mov ecx,[ebx+12]
  220.                 mov [edi+offs_ligh_attenuation+4],ecx ;l->attenuation[1]=p[3]
  221.                 jmp .end_f
  222.         @@:
  223.         cmp ecx,GL_QUADRATIC_ATTENUATION
  224.                 mov ecx,[ebx+12]
  225.                 mov [edi+offs_ligh_attenuation+8],ecx ;l->attenuation[2]=p[3]
  226.                 jmp .end_f
  227.         @@: ;default:
  228. ;    assert(0);
  229.         .end_f:
  230. popad
  231.         ret
  232. endp
  233.  
  234.  
  235. align 4
  236. proc glopLightModel uses ebx ecx esi edi, context:dword, p:dword
  237.         mov edi,[context]
  238.         mov ebx,[p]
  239.         mov ebx,[ebx+4]
  240.         mov esi,[ebp+12] ;&p[0]
  241.         add esi,8 ;&p[2]
  242.  
  243.         cmp ebx,GL_LIGHT_MODEL_AMBIENT
  244.         jne @f
  245.                 mov ecx,4
  246.                 add edi,offs_cont_ambient_light_model
  247.                 rep movsd ;for(i=0;i<4;i++) context.ambient_light_model.v[i]=v[i]
  248.                 jmp .end_f
  249.         @@:
  250.         cmp ebx,GL_LIGHT_MODEL_LOCAL_VIEWER
  251.         jne @f
  252.                 fld dword[esi] ;st0 = p[2]
  253.                 fistp dword[edi+offs_cont_local_light_model]
  254.                 jmp .end_f
  255.         @@:
  256.         cmp ebx,GL_LIGHT_MODEL_TWO_SIDE
  257.         jne @f
  258.                 fld dword[esi] ;st0 = p[2]
  259.                 fistp dword[edi+offs_cont_light_model_two_side]
  260.                 jmp .end_f
  261.         @@: ;default:
  262. ;    tgl_warning("glopLightModel: illegal pname: 0x%x\n", ebx);
  263. ;    //assert(0);
  264.         .end_f:
  265.         ret
  266. endp
  267.  
  268. macro clampf a, min, max
  269. {
  270. local .o_1
  271. local .o_2
  272. local .end_m
  273.         fld dword a ;if (a<=0.0)
  274.         ftst
  275.         fstsw ax
  276.         sahf
  277.         ja .o_1
  278.                 ffree st0
  279.                 fincstp
  280.                 mov eax,0.0
  281.                 jmp .end_m ;return 0.0
  282.         .o_1:
  283.         fld1 ;else if (a>=1.0)
  284.         fcompp
  285.         fstsw ax
  286.         sahf
  287.         jb .o_2
  288.                 mov eax,1.0
  289.                 jmp .end_m ;return 1.0
  290.         .o_2:
  291.         mov eax,dword a ;else return a
  292.         .end_m:
  293. }
  294.  
  295. align 4
  296. proc gl_enable_disable_light uses eax ebx ecx, context:dword, light:dword, v:dword
  297.         mov eax,[context]
  298.         mov ebx,[light]
  299.         imul ebx,sizeof.GLLight
  300.         add ebx,eax
  301.         add ebx,offs_cont_lights
  302.  
  303.         xor ecx,ecx
  304.         cmp dword[ebx+offs_ligh_enabled],0
  305.         jne @f
  306.                 not ecx
  307.         @@:
  308.         and ecx,[v]
  309.         or ecx,ecx
  310.         jz @f
  311.                 ;if (v && !l.enabled)
  312.                 mov dword[ebx+offs_ligh_enabled],1
  313.                 mov ecx,[eax+offs_cont_first_light]
  314.                 mov [ebx+offs_ligh_next],ecx
  315.                 mov [eax+offs_cont_first_light],ebx ;context.first_light = l
  316.                 mov dword[ebx+offs_ligh_prev],0 ;l.prev = NULL
  317.                 jmp .end_f
  318.         @@:
  319.         xor ecx,ecx
  320.         cmp dword[v],0
  321.         jne @f
  322.                 not ecx
  323.         @@:
  324.         and ecx,[ebx+offs_ligh_enabled]
  325.         or ecx,ecx
  326.         jz .end_f
  327.                 ;else if (!v && l.enabled)
  328.                 mov dword[ebx+offs_ligh_enabled],0 ;l.enabled = 0
  329.                 mov ecx,[ebx+offs_ligh_next]
  330.                 cmp dword[ebx+offs_ligh_prev],0 ;if (l.prev == NULL)
  331.                 jne .els_0
  332.                         mov [eax+offs_cont_first_light],ecx     ;context.first_light = l.next
  333.                         jmp @f
  334.                 .els_0:
  335.                         mov eax,[ebx+offs_ligh_prev]
  336.                         mov [eax+offs_ligh_next],ecx ;l.prev.next = l.next
  337.                 @@:
  338.                 cmp dword[ebx+offs_ligh_next],0
  339.                 je .end_f
  340.                         mov ecx,[ebx+offs_ligh_prev]
  341.                         mov eax,[ebx+offs_ligh_next]
  342.                         mov [eax+offs_ligh_prev],ecx ;l.next.prev = l.prev
  343.         .end_f:
  344.         ret
  345. endp
  346.  
  347. align 4
  348. fl_1e_3 dd 1.0e-3
  349.  
  350. ; non optimized lightening model
  351. align 4
  352. proc gl_shade_vertex, context:dword, v:dword
  353. locals
  354.         R dd ? ;float
  355.         G dd ? ;float
  356.         B dd ? ;float
  357.         A dd ? ;float
  358.         s V3
  359.         d V3
  360.         dist dd ? ;float
  361.         tmp dd ? ;float
  362.         att dd ? ;float
  363.         dot_spot dd ? ;float
  364.         lR dd ? ;float
  365.         lB dd ? ;float
  366.         lG dd ? ;float
  367.         twoside dd ? ;int
  368.         idx dd ? ;int
  369.         n V3 ;ebp-24
  370.         vcoord V3 ;ebp-12
  371. endl
  372. pushad
  373. ; ebx -> GLLight *l
  374. ; ecx -> GLMaterial *m
  375. ; esi -> GLVertex *v
  376.         mov esi,[v]
  377.         mov edx,[context]
  378.         mov ecx,[edx+offs_cont_materials] ;ecx(m) = &context.materials[0]
  379.         mov eax,[edx+offs_cont_light_model_two_side]
  380.         mov [twoside],eax
  381.  
  382.         add esi,offs_vert_normal
  383.         mov edi,ebp
  384.         sub edi,24 ;edi = &n
  385.         movsd ;n.X=v.normal.X
  386.         movsd ;n.Y=v.normal.Y
  387.         movsd ;n.Z=v.normal.Z
  388.         mov esi,[v]
  389.  
  390.         fld dword[edx+offs_cont_ambient_light_model]
  391.         fmul dword[ecx+offs_mate_ambient]
  392.         fadd dword[ecx] ;offs_mate_emission=0
  393.         fstp dword[R] ;R=m.emission.v[0]+m.ambient.v[0]*context.ambient_light_model.v[0]
  394.         fld dword[edx+offs_cont_ambient_light_model+4]
  395.         fmul dword[ecx+offs_mate_ambient+4]
  396.         fadd dword[ecx+offs_mate_emission+4]
  397.         fstp dword[G]
  398.         fld dword[edx+offs_cont_ambient_light_model+8]
  399.         fmul dword[ecx+offs_mate_ambient+8]
  400.         fadd dword[ecx+offs_mate_emission+8]
  401.         fstp dword[B]
  402.         clampf [ecx+offs_mate_diffuse+12],0,1
  403.         mov [A],eax ;A=clampf(m.diffuse.v[3],0,1)
  404.  
  405.         mov ebx,[edx+offs_cont_first_light]
  406.         .cycle_0: ;for(l=context.first_light;l!=NULL;l=l.next)
  407.                 or ebx,ebx
  408.                 jz .cycle_0_end
  409.  
  410.                 ; ambient
  411.                 fld dword[ecx+offs_mate_ambient]
  412.                 fmul dword[ebx+offs_ligh_ambient]
  413.                 fstp dword[lR] ;lR=l.ambient.v[0] * m.ambient.v[0]
  414.                 fld dword[ecx+offs_mate_ambient+4]
  415.                 fmul dword[ebx+offs_ligh_ambient+4]
  416.                 fstp dword[lG] ;lG=l.ambient.v[1] * m.ambient.v[1]
  417.                 fld dword[ecx+offs_mate_ambient+8]
  418.                 fmul dword[ebx+offs_ligh_ambient+8]
  419.                 fstp dword[lB] ;lB=l.ambient.v[2] * m.ambient.v[2]
  420.  
  421.                 cmp dword[ebx+offs_ligh_position+12],0 ;if (l.position.v[3] == 0)
  422.                 jne .els_0
  423.                         ; light at infinity
  424.                         mov eax,[ebx+offs_ligh_position]
  425.                         mov [d],eax ;d.X=l.position.v[0]
  426.                         mov eax,[ebx+offs_ligh_position+4]
  427.                         mov [d+4],eax ;d.Y=l.position.v[1]
  428.                         mov eax,[ebx+offs_ligh_position+8]
  429.                         mov [d+8],eax ;d.Z=l.position.v[2]
  430.                         mov dword[att],1.0
  431.                         jmp .els_0_end
  432.                 .els_0:
  433.                         ; distance attenuation
  434.                         fld dword[ebx+offs_ligh_position]
  435.                         fsub dword[esi+offs_vert_ec]
  436.                         fstp dword[d] ;d.X=l.position.v[0]-v.ec.v[0]
  437.                         fld dword[ebx+offs_ligh_position+offs_Y]
  438.                         fsub dword[esi+offs_vert_ec+offs_Y]
  439.                         fstp dword[d+offs_Y] ;d.Y=l.position.v[1]-v.ec.v[1]
  440.                         fld dword[ebx+offs_ligh_position+offs_Z]
  441.                         fsub dword[esi+offs_vert_ec+offs_Z]
  442.                         fstp dword[d+offs_Z] ;d.Z=l.position.v[2]-v.ec.v[2]
  443.                         fld dword[d]
  444.                         fmul st0,st0
  445.                         fld dword[d+offs_Y]
  446.                         fmul st0,st0
  447.                         faddp
  448.                         fld dword[d+offs_Z]
  449.                         fmul st0,st0
  450.                         faddp
  451.                         fsqrt
  452.                         fst dword[dist] ;dist=sqrt(d.X^2+d^2+d^2)
  453.                         fcom dword[fl_1e_3]
  454.                         fstsw ax
  455.                         sahf
  456.                         jbe @f ;if (dist>1.0e-3)
  457.                                 fld1
  458.                                 fdiv st0,st1
  459.                                 fld dword[d]
  460.                                 fmul st0,st1
  461.                                 fstp dword[d]
  462.                                 fld dword[d+offs_Y]
  463.                                 fmul st0,st1
  464.                                 fstp dword[d+offs_Y]
  465.                                 fld dword[d+offs_Z]
  466.                                 fmul st0,st1
  467.                                 fstp dword[d+offs_Z]
  468.                                 ffree st0 ;1.0/dist
  469.                                 fincstp
  470.                         @@:
  471.                         fld dword[ebx+offs_ligh_attenuation+8]
  472.                         fmul st0,st1 ;st0 = dist * l.attenuation[2]
  473.                         fadd dword[ebx+offs_ligh_attenuation+4]
  474.                         fmul st0,st1
  475.                         fadd dword[ebx+offs_ligh_attenuation]
  476.                         fld1
  477.                         fdiv st0,st1
  478.                         fstp dword[att] ;att = 1.0f/(l.attenuation[0]+dist*(l.attenuation[1]+dist*l.attenuation[2]))
  479.                         ffree st0 ;1.0
  480.                         fincstp
  481.                         ffree st0 ;dist
  482.                         fincstp
  483.                 .els_0_end:
  484.                 fld dword[d]
  485.                 fmul dword[n]
  486.                 fld dword[d+offs_Y]
  487.                 fmul dword[n+offs_Y]
  488.                 faddp
  489.                 fld dword[d+offs_Z]
  490.                 fmul dword[n+offs_Z]
  491.                 faddp ;dot = d.X*n.X+d.Y*n.Y+d.Z*n.Z
  492.                 cmp dword[twoside],0 ;if (twoside && dot < 0)
  493.                 je @f
  494.                 ftst ;if (dot<0)
  495.                 fstsw ax
  496.                 sahf
  497.                 jae @f
  498.                         fchs ;dot = -dot
  499.                 @@:
  500.                 ftst ;if (dot>0)
  501.                 fstsw ax
  502.                 sahf
  503.                 jle .if0_end
  504.                         ; diffuse light
  505.                         fld dword[ecx+offs_mate_diffuse]
  506.                         fmul dword[ebx+offs_ligh_diffuse]
  507.                         fmul st0,st1
  508.                         fadd dword[lR]
  509.                         fstp dword[lR] ;lR+=dot * l.diffuse.v[0] * m.diffuse.v[0]
  510.                         fld dword[ecx+offs_mate_diffuse+4]
  511.                         fmul dword[ebx+offs_ligh_diffuse+4]
  512.                         fmul st0,st1
  513.                         fadd dword[lG]
  514.                         fstp dword[lG] ;lG+=dot * l.diffuse.v[1] * m.diffuse.v[1]
  515.                         fld dword[ecx+offs_mate_diffuse+8]
  516.                         fmul dword[ebx+offs_ligh_diffuse+8]
  517.                         fmul st0,st1
  518.                         fadd dword[lB]
  519.                         fstp dword[lB] ;lB+=dot * l.diffuse.v[2] * m.diffuse.v[2]
  520.                         ffree st0 ;dot
  521.                         fincstp
  522.  
  523.                         ; spot light
  524.                         fld dword[ebx+offs_ligh_spot_cutoff]
  525.                         fcomp dword[an180f] ;if (l.spot_cutoff != 180)
  526.                         fstsw ax
  527.                         sahf
  528.                         jne .if1_end
  529.                                 fld dword[ebx+offs_ligh_norm_spot_direction]
  530.                                 fmul dword[d]
  531.                                 fld dword[ebx+offs_ligh_norm_spot_direction+4]
  532.                                 fmul dword[d+offs_Y]
  533.                                 faddp
  534.                                 fld dword[ebx+offs_ligh_norm_spot_direction+8]
  535.                                 fmul dword[d+offs_Z]
  536.                                 faddp
  537.                                 fchs
  538.                                 fst dword[dot_spot]
  539.                                 cmp dword[twoside],0 ;if (twoside && dot_spot < 0)
  540.                                 je @f
  541.                                 ftst ;if (dot_spot<0)
  542.                                 fstsw ax
  543.                                 sahf
  544.                                 jae @f
  545.                                         fchs ;dot_spot = -dot_spot
  546.                                 @@:
  547.                                 fcom dword[ebx+offs_ligh_cos_spot_cutoff] ;if (dot_spot < l.cos_spot_cutoff)
  548.                                 fstsw ax
  549.                                 sahf
  550.                                 jae .els_1
  551.                                         ; no contribution
  552.                                         ffree st0 ;dot_spot
  553.                                         fincstp
  554.                                         mov ebx,[ebx+offs_ligh_next]
  555.                                         jmp .cycle_0 ;continue
  556.                                 .els_1:
  557.                                         ; TODO: optimize
  558.                                         fld dword[ebx+offs_ligh_spot_exponent]
  559.                                         ftst ;if (l.spot_exponent > 0)
  560.                                         fstsw ax
  561.                                         sahf
  562.                                         jbe @f
  563.                                                 fxch st1 ;dot_spot <--> l.spot_exponent
  564.                                                 ;Вычисляем x^y
  565.                                                 ;fld y
  566.                                                 ;fld x
  567.                                                 fyl2x ;Стек FPU теперь содержит: st0=z=y*log2(x):
  568.                                                 ;Теперь считаем 2**z:
  569.                                                 fld st0 ;Создаем еще одну копию z
  570.                                                 frndint ;Округляем
  571.                                                 fsubr st0,st1  ;st1=z, st0=z-trunc(z)
  572.                                                 f2xm1  ;st1=z, st0=2**(z-trunc(z))-1
  573.                                                 fld1
  574.                                                 faddp  ;st1=z, st0=2**(z-trunc(z))
  575.                                                 fscale ;st1=z, st0=(2**trunc(z))*(2**(z-trunc(z)))=2**t
  576.                                                 fxch st1
  577.                                                 fstp st ;Результат остается на вершине стека st0
  578.                                                 fmul dword[att]
  579.                                                 fstp dword[att] ;att=att*pow(dot_spot,l.spot_exponent)
  580.                                                 jmp .if1_end
  581.                                         @@:
  582.                                         ffree st0 ;l.spot_exponent
  583.                                         fincstp
  584.                                         ffree st0 ;dot_spot
  585.                                         fincstp
  586.                         .if1_end:
  587.  
  588.                         ; specular light
  589.                         cmp dword[edx+offs_cont_local_light_model],0 ;if (c.local_light_model)
  590.                         je .els_2
  591.                                 mov eax,[esi+offs_vert_ec]
  592.                                 mov [vcoord],eax ;vcoord.X=v.ec.X
  593.                                 mov eax,[esi+offs_vert_ec+offs_Y]
  594.                                 mov [vcoord+offs_Y],eax ;vcoord.Y=v.ec.Y
  595.                                 mov eax,[esi+offs_vert_ec+offs_Z]
  596.                                 mov [vcoord+offs_Z],eax ;vcoord.Z=v.ec.Z
  597.                                 mov eax,ebp
  598.                                 sub eax,12 ;eax = &vcoord
  599.                                 stdcall gl_V3_Norm, eax
  600.                                 fld dword[d]
  601.                                 fsub dword[vcoord]
  602.                                 fstp dword[s] ;s.X=d.X-vcoord.X
  603.                                 fld dword[d+offs_Y]
  604.                                 fsub dword[vcoord+offs_Y]
  605.                                 fstp dword[s+offs_Y] ;s.Y=d.Y-vcoord.Y
  606.                                 fld dword[d+offs_Z]
  607.                                 fsub dword[vcoord+offs_Z]
  608.                                 fstp dword[s+offs_Z] ;s.Z=d.Z-vcoord.Z
  609.                                 jmp .els_2_end
  610.                         .els_2:
  611.                                 mov eax,[d]
  612.                                 mov [s],eax ;s.X=d.X
  613.                                 mov eax,[d+offs_Y]
  614.                                 mov [s+offs_Y],eax ;s.Y=d.Y
  615.                                 fld1
  616.                                 fadd dword[d+offs_Z]
  617.                                 fstp dword[s+offs_Z] ;s.Z=d.Z+1.0
  618.                         .els_2_end:
  619.                         fld dword[n]
  620.                         fmul st0,st0
  621.                         fld dword[n+offs_Y]
  622.                         fmul st0,st0
  623.                         faddp
  624.                         fld dword[n+offs_Z]
  625.                         fmul st0,st0
  626.                         faddp ;dot_spec = n.X^2 +n.Y^2 +n.Z^2
  627.                         cmp dword[twoside],0 ;if (twoside && dot_spec < 0)
  628.                         je @f
  629.                         ftst ;if (dot_spec < 0)
  630.                         fstsw ax
  631.                         sahf
  632.                         jae @f
  633.                                 fchs ;dot_spec = -dot_spec
  634.                         @@:
  635.                         ftst ;if (dot_spec > 0)
  636.                         fstsw ax
  637.                         sahf
  638.                         jae .if0_end
  639.                                 fld dword[s]
  640.                                 fmul st0,st0
  641.                                 fld dword[s+offs_Y]
  642.                                 fmul st0,st0
  643.                                 faddp
  644.                                 fld dword[s+offs_Z]
  645.                                 fmul st0,st0
  646.                                 faddp
  647.                                 fsqrt
  648.                                 fcom dword[fl_1e_3]
  649.                                 fstsw ax
  650.                                 sahf
  651.                                 jbe @f ;if (tmp > 1.0e-3)
  652.                                         fdiv st1,st0 ;dot_spec /= tmp
  653.                                 @@:
  654.                                 ffree st0 ;tmp
  655.                                 fincstp
  656.  
  657.                                 ; TODO: optimize
  658.                                 ; testing specular buffer code
  659.                                 ; dot_spec= pow(dot_spec,m.shininess)
  660.                                 stdcall specbuf_get_buffer, edx, dword[ecx+offs_mate_shininess_i], dword[ecx+offs_mate_shininess]
  661.                                 ;eax = specbuf
  662.                                 mov dword[idx],SPECULAR_BUFFER_SIZE
  663.                                 fild dword[idx]
  664.                                 fld1
  665.                                 fcomp
  666.                                 fstsw ax
  667.                                 sahf
  668.                                 jae @f
  669.                                         fmul st0,st1
  670.                                 @@:
  671.                                 fistp dword[idx] ;if (dot_spec < 1.0) idx = (int)(dot_spec*SPECULAR_BUFFER_SIZE)
  672.                                         ;else idx = SPECULAR_BUFFER_SIZE;
  673.                                 ffree st0 ;dot_spec
  674.                                 fincstp
  675.                                 shl dword[idx],2
  676.                                 add eax,dword[idx]
  677.                                 fld dword[eax+offs_spec_buf] ;dot_spec = specbuf.buf[idx]
  678.                                 fld dword[ebx+offs_ligh_specular]
  679.                                 fmul st0,st1
  680.                                 fmul dword[ecx+offs_mate_specular]
  681.                                 fadd dword[lR]
  682.                                 fstp dword[lR] ;lR+=dot_spec * l.specular.v[0] * m.specular.v[0]
  683.                                 fld dword[ebx+offs_ligh_specular+offs_Y]
  684.                                 fmul st0,st1
  685.                                 fmul dword[ecx+offs_mate_specular+offs_Y]
  686.                                 fadd dword[lG]
  687.                                 fstp dword[lG] ;lG+=dot_spec * l.specular.v[1] * m.specular.v[1]
  688.                                 fld dword[ebx+offs_ligh_specular+offs_Z]
  689.                                 fmul st0,st1
  690.                                 fmul dword[ecx+offs_mate_specular+offs_Z]
  691.                                 fadd dword[lB]
  692.                                 fstp dword[lB] ;lB+=dot_spec * l.specular.v[2] * m.specular.v[2]
  693.                                 ffree st0 ;dot_spec
  694.                                 fincstp
  695.                         jmp .if2_end
  696.                 .if0_end:
  697.                 ffree st0 ;dot [or] dot_spec
  698.                 fincstp
  699.                 .if2_end:
  700.  
  701.                 fld dword[att]
  702.                 fld dword[lR]
  703.                 fmul st0,st1
  704.                 fadd dword[R]
  705.                 fstp dword[R] ;R += att * lR
  706.                 fld dword[lG]
  707.                 fmul st0,st1
  708.                 fadd dword[G]
  709.                 fstp dword[G] ;G += att * lG
  710.                 fld dword[lB]
  711.                 fmul st0,st1
  712.                 fadd dword[B]
  713.                 fstp dword[B] ;B += att * lB
  714.                 ffree st0 ;att
  715.                 fincstp
  716.                 mov ebx,[ebx+offs_ligh_next]
  717.                 jmp .cycle_0
  718.         .cycle_0_end:
  719.  
  720.         clampf [R],0,1
  721.         mov [esi+offs_vert_color],eax ;v.color.v[0]=clampf(R,0,1)
  722.         clampf [G],0,1
  723.         mov [esi+offs_vert_color+4],eax ;v.color.v[1]=clampf(G,0,1)
  724.         clampf [B],0,1
  725.         mov [esi+offs_vert_color+8],eax ;v.color.v[2]=clampf(B,0,1)
  726.         mov eax,[A]
  727.         mov [esi+offs_vert_color+12],eax ;v.color.v[3]=A
  728. popad
  729.         ret
  730. endp
  731.  
  732.