Subversion Repositories Kolibri OS

Rev

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

  1.  
  2. if DEBUG
  3. align 4
  4. txt_gl_scal db 'glopScale',0
  5. txt_gl_tran db 'glopTranslate',0
  6.  
  7. align 4
  8. proc gl_print_matrix uses eax ebx ecx edi, m:dword, rows:dword
  9.         mov ecx,[rows]
  10.         cmp ecx,1
  11.         jl .end_f
  12.         mov ebx,[m]
  13.         mov word[NumberSymbolsAD],3
  14.         finit
  15.         @@:
  16.                 lea edi,[buf_param]
  17.                 mov byte[edi],0
  18.  
  19.                 fld dword[ebx]
  20.                 fstp qword[Data_Double]
  21.                 call DoubleFloat_to_String
  22.                 stdcall str_cat, edi,Data_String
  23.  
  24.                 stdcall str_n_cat,edi,txt_zp_sp,2
  25.                 stdcall str_len,edi
  26.                 add edi,eax
  27.  
  28.                 fld dword[ebx+4]
  29.                 fstp qword[Data_Double]
  30.                 call DoubleFloat_to_String
  31.                 stdcall str_cat, edi,Data_String
  32.  
  33.                 stdcall str_n_cat,edi,txt_zp_sp,2
  34.                 stdcall str_len,edi
  35.                 add edi,eax
  36.  
  37.                 fld dword[ebx+8]
  38.                 fstp qword[Data_Double]
  39.                 call DoubleFloat_to_String
  40.                 stdcall str_cat, edi,Data_String
  41.  
  42.                 stdcall str_n_cat,edi,txt_zp_sp,2
  43.                 stdcall str_len,edi
  44.                 add edi,eax
  45.  
  46.                 fld dword[ebx+12]
  47.                 fstp qword[Data_Double]
  48.                 call DoubleFloat_to_String
  49.                 stdcall str_cat, edi,Data_String
  50.  
  51.                 stdcall str_n_cat,edi,txt_nl,2
  52.                 stdcall dbg_print,txt_sp,buf_param
  53.                
  54.                 add ebx,16
  55.         dec ecx
  56.         cmp ecx,0
  57.         jg @b
  58.         .end_f:
  59.         ret
  60. endp
  61. end if
  62.  
  63. macro gl_matrix_update context, reg
  64. {
  65.         local .end_0
  66.         xor reg,reg
  67.         cmp dword[context+offs_cont_matrix_mode],1
  68.         jg .end_0
  69.                 inc reg
  70.         .end_0:
  71.         mov dword[context+offs_cont_matrix_model_projection_updated],reg
  72. }
  73.  
  74. align 4
  75. proc glopMatrixMode uses eax ebx, context:dword, p:dword
  76.         mov eax,[context]
  77.         mov ebx,[p]
  78.  
  79.         cmp dword[ebx+4],GL_MODELVIEW ;cmp p[1],...
  80.         jne @f
  81.                 mov dword[eax+offs_cont_matrix_mode],0
  82.                 jmp .end_f
  83.         @@:
  84.         cmp dword[ebx+4],GL_PROJECTION
  85.         jne @f
  86.                 mov dword[eax+offs_cont_matrix_mode],1
  87.                 jmp .end_f
  88.         @@:
  89.         cmp dword[ebx+4],GL_TEXTURE
  90.         jne .def
  91.                 mov dword[eax+offs_cont_matrix_mode],2
  92.                 jmp .end_f
  93.         .def:
  94. ;assert(0);
  95.         .end_f:
  96.         ret
  97. endp
  98.  
  99. align 4
  100. proc glopLoadMatrix uses eax edi esi, context:dword, p:dword
  101.         mov eax,[context]
  102.         mov edi,[eax+offs_cont_matrix_mode]
  103.         shl edi,2
  104.         add edi,eax
  105.         mov edi,dword[edi+offs_cont_matrix_stack_ptr]
  106.  
  107.         mov esi,[p]
  108.         add esi,4
  109.         stdcall gl_M4_Transpose,edi,esi ;транспонируем входную матрицу в матрицу context.matrix_stack_ptr[context.matrix_mode]
  110.  
  111.         gl_matrix_update eax,edi
  112.         ret
  113. endp
  114.  
  115. align 4
  116. proc glopLoadIdentity uses eax ebx, context:dword, p:dword
  117.         mov eax,[context]
  118.         mov ebx,[eax+offs_cont_matrix_mode]
  119.         shl ebx,2
  120.         add ebx,eax
  121.  
  122.         stdcall gl_M4_Id,[ebx+offs_cont_matrix_stack_ptr]
  123. if DEBUG ;glopLoadIdentity
  124.         stdcall gl_print_matrix,[ebx+offs_cont_matrix_stack_ptr],4
  125. end if
  126.         gl_matrix_update eax,ebx
  127.         ret
  128. endp
  129.  
  130. align 4
  131. proc glopMultMatrix uses eax edi esi, context:dword, p:dword
  132. locals
  133.         m M4
  134. endl
  135.         mov esi,[p]
  136.         add esi,4
  137.         mov edi,ebp
  138.         sub edi,sizeof.M4
  139.         stdcall gl_M4_Transpose,edi,esi ;транспонируем входную матрицу в локальную матрицу m
  140.  
  141.         mov eax,[context]
  142.         mov esi,[eax+offs_cont_matrix_mode]
  143.         shl esi,2
  144.         add esi,eax
  145.         stdcall gl_M4_MulLeft,dword[esi+offs_cont_matrix_stack_ptr],edi
  146.  
  147.         gl_matrix_update eax,edi
  148.         ret
  149. endp
  150.  
  151. align 4
  152. proc glopPushMatrix uses eax ebx, context:dword, p:dword
  153.         mov eax,[context]
  154.         mov ebx,[eax+offs_cont_matrix_mode]
  155.  
  156. ;  assert( (c->matrix_stack_ptr[ebx] - c->matrix_stack[ebx] + 1 )
  157. ;          < c->matrix_stack_depth_max[ebx] );
  158.  
  159.         shl ebx,2
  160.         add ebx,eax
  161.         add ebx,offs_cont_matrix_stack_ptr
  162.         add dword[ebx],sizeof.M4
  163.         mov ebx,[ebx] ;ebx = ++context.matrix_stack_ptr[context.matrix_mode]
  164.  
  165.         sub ebx,sizeof.M4
  166.         push ebx
  167.         add ebx,sizeof.M4
  168.         stdcall gl_M4_Move, ebx
  169.  
  170.         gl_matrix_update eax,ebx
  171.         ret
  172. endp
  173.  
  174. align 4
  175. proc glopPopMatrix uses eax ebx, context:dword, p:dword
  176.         mov eax,[context]
  177.         mov ebx,[eax+offs_cont_matrix_mode]
  178.  
  179. ;  assert( c->matrix_stack_ptr[n] > c->matrix_stack[n] );
  180.  
  181.         shl ebx,2
  182.         add ebx,eax
  183.         sub dword[ebx+offs_cont_matrix_stack_ptr],sizeof.M4
  184.  
  185.         gl_matrix_update eax,ebx
  186.         ret
  187. endp
  188.  
  189. align 4
  190. proc glopRotate uses eax ebx ecx edx, context:dword, p:dword
  191. locals
  192.         u0 dd ?
  193.         u1 dd ?
  194.         u2 dd ?
  195.         angle dd ?
  196.         cost dd ?
  197.         sint dd ?
  198.         m M4
  199. endl
  200.  
  201.         mov eax,[context]
  202.         mov ebx,[p]
  203.         mov ecx,ebp
  204.         sub ecx,sizeof.M4 ;ecx=&m
  205.         finit
  206.         fldpi
  207.         fmul dword[ebx+4]
  208.         fdiv dword[an180f]
  209.         fst dword[angle] ;angle = p[1].f * M_PI / 180.0
  210.         ;st0 = angle
  211.  
  212.         ; simple case detection
  213.         xor edx,edx
  214.         fld dword[ebx+16]
  215.         ftst
  216.         fstsw ax
  217.         sahf
  218.         je @f
  219.                 inc edx
  220.         @@:
  221.         fstp dword[u2]
  222.         fld dword[ebx+12]
  223.         ftst
  224.         fstsw ax
  225.         sahf
  226.         je @f
  227.                 or edx,2
  228.         @@:
  229.         fstp dword[u1]
  230.         fld dword[ebx+8]
  231.         ftst
  232.         fstsw ax
  233.         sahf
  234.         je @f
  235.                 or edx,4
  236.         @@:
  237.         fstp dword[u0]
  238.  
  239.         ;st0 = angle
  240.         ;ebx = ((u0 != 0)<<2) | ((u1 != 0)<<1) | (u2 != 0)
  241.         or edx,edx
  242.         jz .end_f ;если нет поворотов выход из функции
  243.         cmp edx,4
  244.         jne @f
  245.                 fld dword[u0]
  246.                 ftst
  247.                 fstsw ax
  248.                 ffree st0
  249.                 fincstp
  250.                 sahf
  251.                 jae .u0ch
  252.                         fchs ;if (u0 < 0) angle *= -1
  253.                 .u0ch:
  254.                 push dword 0
  255.                 fstp dword[esp-4]
  256.                 sub esp,4
  257.                 stdcall gl_M4_Rotate, ecx
  258.                 jmp .end_sw
  259.         @@:
  260.         cmp edx,2
  261.         jne @f
  262.                 fld dword[u1]
  263.                 ftst
  264.                 fstsw ax
  265.                 ffree st0
  266.                 fincstp
  267.                 sahf
  268.                 jae .u1ch
  269.                         fchs ;if (u1 < 0) angle *= -1
  270.                 .u1ch:
  271.                 push dword 1
  272.                 fstp dword[esp-4]
  273.                 sub esp,4
  274.                 stdcall gl_M4_Rotate, ecx
  275.                 jmp .end_sw
  276.         @@:
  277.         cmp edx,1
  278.         jne @f
  279.                 fld dword[u2]
  280.                 ftst
  281.                 fstsw ax
  282.                 ffree st0
  283.                 fincstp
  284.                 sahf
  285.                 jae .u2ch
  286.                         fchs ;if (u2 < 0) angle *= -1
  287.                 .u2ch:
  288.                 push dword 2
  289.                 fstp dword[esp-4]
  290.                 sub esp,4
  291.                 stdcall gl_M4_Rotate, ecx
  292.                 jmp .end_sw
  293.         @@: ;default:
  294. if DEBUG ;glopRotete
  295.                 stdcall dbg_print,txt_sp,m_1
  296. end if
  297.  
  298.                 ; normalize vector
  299.                 fld dword[u0]
  300.                 fmul st0,st0
  301.                 fld dword[u1]
  302.                 fmul st0,st0
  303.                 faddp
  304.                 fld dword[u2]
  305.                 fmul st0,st0
  306.                 faddp
  307. ;               fst dword[len] ;len = u0*u0+u1*u1+u2*u2
  308.                 ftst
  309.                 fstsw ax
  310.                 sahf
  311.                 je .f2 ;if (len == 0.0f) return
  312.                 fsqrt
  313.                 fld1
  314.                 fxch
  315.                 fdivp ;len = 1.0f / sqrt(len)
  316. ;               fst dword[len]
  317.                 fld dword[u0]
  318.                 fmul st0,st1
  319.                 fstp dword[u0] ;u0 *= len
  320.                 fld dword[u1]
  321.                 fmul st0,st1
  322.                 fstp dword[u1] ;u1 *= len
  323.                 fld dword[u2]
  324.                 fmul st0,st1
  325.                 fstp dword[u2] ;u2 *= len
  326.                 ;st0 = len, st1 = angle
  327.                 ffree st0
  328.                 fincstp
  329.  
  330.                 ; store cos and sin values
  331.                 fcos
  332.                 fst dword[cost] ;cost=cos(angle)
  333.                 fld dword[angle]
  334.                 fsin
  335.                 fst dword[sint] ;sint=sin(angle)
  336.  
  337.                 ; fill in the values
  338.                 mov ebx,0.0
  339.                 mov [ecx+3*16   ],ebx ;m[3][0]
  340.                 mov [ecx+3*16 +4],ebx ;m[3][1]
  341.                 mov [ecx+3*16 +8],ebx ;m[3][2]
  342.                 mov [ecx+     12],ebx ;m[0][3]
  343.                 mov [ecx+  16+12],ebx ;m[1][3]
  344.                 mov [ecx+2*16+12],ebx ;m[2][3]
  345.                 mov ebx,1.0
  346.                 mov [ecx+3*16+12],ebx ;m[3][3]
  347.  
  348.                 ; do the math
  349.                 fld dword[u0]
  350.                 fmul st0,st0
  351.                 fld1
  352.                 fsub st0,st1
  353.                 fmul st0,st3 ;st0 = cost*(1-u0^2)
  354.                 faddp
  355.                 fstp dword[ecx] ;m[0][0] = u0*u0+cost*(1-u0*u0)
  356.  
  357.                 fld1
  358.                 fsub st0,st2 ;st0 = 1-cost
  359.  
  360.                 fld st0
  361.                 fmul dword[u0]
  362.                 fmul dword[u1]
  363.                 fld dword[u2]
  364.                 fmul st0,st3 ;st0 = u2*sint
  365.                 fchs
  366.                 faddp
  367.                 fstp dword[ecx+16] ;m[1][0] = u0*u1*(1-cost)-u2*sint
  368.  
  369.                 fld st0
  370.                 fmul dword[u0]
  371.                 fmul dword[u2]
  372.                 fld dword[u1]
  373.                 fmul st0,st3 ;st0 = u1*sint
  374.                 faddp
  375.                 fstp dword[ecx+32] ;m.m[2][0]=u[2]*u[0]*(1-cost)+u[1]*sint
  376.  
  377.                 fld st0
  378.                 fmul dword[u0]
  379.                 fmul dword[u1]
  380.                 fld dword[u2]
  381.                 fmul st0,st3 ;st0 = u2*sint
  382.                 faddp
  383.                 fstp dword[ecx+4] ;m.m[0][1]=u[0]*u[1]*(1-cost)+u[2]*sint
  384.  
  385.                 fld dword[u1]
  386.                 fmul st0,st0
  387.                 fld1
  388.                 fsub st0,st1
  389.                 fmul st0,st4 ;st0 = cost*(1-u1^2)
  390.                 faddp
  391.                 fstp dword[ecx+20] ;m.m[1][1]=u[1]*u[1]+cost*(1-u[1]*u[1])
  392.  
  393.                 fld st0
  394.                 fmul dword[u1]
  395.                 fmul dword[u2]
  396.                 fld dword[u0]
  397.                 fmul st0,st3 ;st0 = u0*sint
  398.                 fchs
  399.                 faddp
  400.                 fstp dword[ecx+36] ;m.m[2][1]=u[1]*u[2]*(1-cost)-u[0]*sint
  401.  
  402.                 fld st0
  403.                 fmul dword[u0]
  404.                 fmul dword[u2]
  405.                 fld dword[u1]
  406.                 fmul st0,st3 ;st0 = u1*sint
  407.                 fchs
  408.                 faddp
  409.                 fstp dword[ecx+8] ;m.m[0][2]=u[2]*u[0]*(1-cost)-u[1]*sint
  410.  
  411.                 fld st0
  412.                 fmul dword[u1]
  413.                 fmul dword[u2]
  414.                 fld dword[u0]
  415.                 fmul st0,st3 ;st0 = u0*sint
  416.                 faddp
  417.                 fstp dword[ecx+24] ;m.m[1][2]=u[1]*u[2]*(1-cost)+u[0]*sint
  418.  
  419.                 ffree st0 ;1-cost
  420.                 fincstp
  421.  
  422.                 fld dword[u2]
  423.                 fmul st0,st0
  424.                 fld1
  425.                 fsub st0,st1
  426.                 fmul st0,st3 ;st0 = cost*(1-u2^2)
  427.                 faddp
  428.                 fstp dword[ecx+40] ;m[2][2] = u2*u2+cost*(1-u2*u2)
  429.  
  430.                 ffree st0 ;sint
  431.   fincstp
  432.                 ffree st0 ;cost
  433.   fincstp
  434.         .end_sw:
  435.  
  436.         mov eax,[context]
  437.         mov ebx,[eax+offs_cont_matrix_mode]
  438.         shl ebx,2
  439.         add ebx,eax
  440.         stdcall gl_M4_MulLeft,dword[ebx+offs_cont_matrix_stack_ptr],ecx
  441. if DEBUG ;glopRotete
  442.                 stdcall gl_print_matrix,ecx,4
  443. end if
  444.         gl_matrix_update eax,ebx
  445.         jmp .end_f
  446.         .f2:
  447.                 ffree st0 ;len
  448.                 fincstp
  449.                 ffree st0 ;angle
  450.                 fincstp
  451.         .end_f:
  452.         ret
  453. endp
  454.  
  455. align 4
  456. proc glopScale uses eax ebx ecx, context:dword, p:dword
  457.         mov ecx,[p]
  458.  
  459.         mov eax,[context]
  460.         mov ebx,[eax+offs_cont_matrix_mode]
  461.         shl ebx,2
  462.         add ebx,eax
  463.         mov ebx,[ebx+offs_cont_matrix_stack_ptr] ;ebx = &m[0]
  464.  
  465.         fld dword[ecx+ 4] ;x
  466.         fld dword[ecx+ 8] ;y
  467.         fld dword[ecx+12] ;z
  468.         mov ecx,4
  469.         @@:
  470.         fld dword[ebx]   ;m[0]
  471.         fmul st0,st3     ;m[0] * x
  472.         fstp dword[ebx]  ;m[0] *= x
  473.         fld dword[ebx+4] ;m[1]
  474.         fmul st0,st2     ;m[1] * y
  475.         fstp dword[ebx+4];m[1] *= y
  476.         fld dword[ebx+8] ;m[2]
  477.         fmul st0,st1     ;m[2] * z
  478.         fstp dword[ebx+8];m[2] *= z
  479.         add ebx,16
  480.         loop @b
  481.         ffree st0
  482.         fincstp
  483.         ffree st0
  484.         fincstp
  485.         ffree st0
  486.         fincstp
  487.  
  488. if DEBUG ;glopScale
  489. pushad
  490.         stdcall dbg_print,txt_gl_scal,txt_nl
  491.         mov ebx,[eax+offs_cont_matrix_mode]
  492.         shl ebx,2
  493.         add ebx,eax
  494.         stdcall gl_print_matrix,[ebx+offs_cont_matrix_stack_ptr],4
  495. popad
  496. end if
  497.         gl_matrix_update eax,ebx
  498.         ret
  499. endp
  500.  
  501. align 4
  502. proc glopTranslate uses eax ebx ecx, context:dword, p:dword
  503.         mov ecx,[p]
  504.  
  505.         mov eax,[context]
  506.         mov ebx,[eax+offs_cont_matrix_mode]
  507.         shl ebx,2
  508.         add ebx,eax
  509.         mov ebx,[ebx+offs_cont_matrix_stack_ptr] ;ebx = &m[0]
  510.  
  511.         fld dword[ecx+ 4] ;x
  512.         fld dword[ecx+ 8] ;y
  513.         fld dword[ecx+12] ;z
  514.         mov ecx,4
  515.         @@:
  516.                 fld dword[ebx]   ;m[0]
  517.                 fmul st0,st3     ;m[0] * x
  518.                 fld dword[ebx+4] ;m[1]
  519.                 fmul st0,st3     ;m[1] * y
  520.                 faddp
  521.                 fld dword[ebx+8] ;m[2]
  522.                 fmul st0,st2     ;m[2] * z
  523.                 faddp
  524.                 fadd dword[ebx+12] ;m[3]
  525.                 fstp dword[ebx+12] ;m[3] = m[0] * x + m[1] * y + m[2] * z + m[3]
  526.                 add ebx,16
  527.         loop @b
  528.         ffree st0
  529.         fincstp
  530.         ffree st0
  531.         fincstp
  532.         ffree st0
  533.         fincstp
  534.  
  535. if DEBUG ;glopTranslate
  536. pushad
  537.         stdcall dbg_print,txt_gl_tran,txt_nl
  538.         mov ebx,[eax+offs_cont_matrix_mode]
  539.         shl ebx,2
  540.         add ebx,eax
  541.         stdcall gl_print_matrix,[ebx+offs_cont_matrix_stack_ptr],4
  542. popad
  543. end if
  544.         gl_matrix_update eax,ebx
  545.         ret
  546. endp
  547.  
  548. align 4
  549. proc glopFrustum uses eax ebx ecx, context:dword, p:dword
  550. locals
  551.         x dd ?
  552.         y dd ?
  553.         A dd ?
  554.         B dd ?
  555.         C dd ?
  556.         D dd ?
  557.         m M4
  558. endl
  559.         mov eax,[context]
  560.         mov ebx,[p]
  561.  
  562.         fld dword[ebx+8]
  563.         fsub dword[ebx+4]  ;st0 = (right-left)
  564.         fld dword[ebx+20]  ;st0 = near
  565.         fadd st0,st0
  566.         fdiv st0,st1
  567.         fstp dword[x]      ;x = (2.0*near) / (right-left)
  568.         fld dword[ebx+16]
  569.         fsub dword[ebx+12] ;st0 = (top-bottom)
  570.         fld dword[ebx+20]  ;st0 = near
  571.         fadd st0,st0
  572.         fdiv st0,st1
  573.         fstp dword[y]      ;y = (2.0*near) / (top-bottom)
  574.         fld dword[ebx+8]
  575.         fadd dword[ebx+4]  ;st0 = (right+left)
  576.         fdiv st0,st2       ;st2 = (right-left)
  577.         fstp dword[A]      ;A = (right+left) / (right-left)
  578.         fld dword[ebx+16]
  579.         fadd dword[ebx+12] ;st0 = (top+bottom)
  580.         fdiv st0,st1       ;st1 = (top-bottom)
  581.         fstp dword[B]      ;B = (top+bottom) / (top-bottom)
  582.         fld dword[ebx+24]
  583.         fsub dword[ebx+20] ;st0 = (farp-near)
  584.         fldz
  585.         fsub dword[ebx+24]
  586.         fsub dword[ebx+20] ;st0 = -(farp+near)
  587.         fdiv st0,st1
  588.         fstp dword[C]      ;C = -(farp+near) / (farp-near)
  589.         fld dword[ebx+24]
  590.         fmul dword[ebx+20] ;st0 = farp*near
  591.         fadd st0,st0
  592.         fchs               ;st0 = -(2.0*farp*near)
  593.         fdiv st0,st1
  594.         fstp dword[D]      ;D = -(2.0*farp*near) / (farp-near)
  595.  
  596.         mov ecx,ebp
  597.         sub ecx,sizeof.M4
  598.  
  599.         mov ebx,[x]
  600.         mov dword[ecx],ebx
  601.         mov dword[ecx+4],0.0
  602.         mov ebx,[A]
  603.         mov dword[ecx+8],ebx
  604.         mov dword[ecx+12],0.0
  605.         mov dword[ecx+16],0.0
  606.         mov ebx,[y]
  607.         mov dword[ecx+20],ebx
  608.         mov ebx,[B]
  609.         mov dword[ecx+24],ebx
  610.         mov dword[ecx+28],0.0
  611.         mov dword[ecx+32],0.0
  612.         mov dword[ecx+36],0.0
  613.         mov ebx,[C]
  614.         mov dword[ecx+40],ebx
  615.         mov ebx,[D]
  616.         mov dword[ecx+44],ebx
  617.         mov dword[ecx+48],0.0
  618.         mov dword[ecx+52],0.0
  619.         mov dword[ecx+56],-1.0
  620.         mov dword[ecx+60],0.0
  621.  
  622.         mov ebx,[eax+offs_cont_matrix_mode]
  623.         shl ebx,2
  624.         add ebx,eax
  625.         stdcall gl_M4_MulLeft,dword[ebx+offs_cont_matrix_stack_ptr],ecx
  626.  
  627. if DEBUG ;glopFrustum
  628.         stdcall gl_print_matrix,ecx,4
  629.         stdcall gl_print_matrix,dword[ebx+offs_cont_matrix_stack_ptr],4
  630. end if
  631.         gl_matrix_update eax,ebx
  632.         ret
  633. endp
  634.  
  635.  
  636.