Subversion Repositories Kolibri OS

Rev

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

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