Subversion Repositories Kolibri OS

Rev

Rev 5269 | Rev 5415 | 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, 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.         fldz
  213.         fild dword[ebx+8]
  214.         fstp dword[u0]
  215.         fild dword[ebx+12]
  216.         fstp dword[u1]
  217.         fild dword[ebx+16]
  218.         fst dword[u2]
  219.  
  220.         ; simple case detection
  221.         xor ebx,ebx
  222.         fcomp st1 ;u2 ... 0
  223.         fstsw ax
  224.         sahf
  225.         je @f
  226.                 inc ebx
  227.         @@:
  228.         fcom dword[u1] ;0 ... u1
  229.         fstsw ax
  230.         sahf
  231.         je @f
  232.                 or ebx,2
  233.         @@:
  234.         fcom dword[u0] ;0 ... u0
  235.         fstsw ax
  236.         sahf
  237.         je @f
  238.                 or ebx,4
  239.         @@:
  240.         ;st0 = 0, st1 = angle
  241.  
  242.         ;ebx = ((u0 != 0)<<2) | ((u1 != 0)<<1) | (u2 != 0)
  243.         cmp ebx,0
  244.         je .end_f ;если нет поворотов выход из функции
  245.         cmp ebx,4
  246.         jne @f
  247.                 fcomp dword[u0] ;0 ... u0
  248.                 fstsw ax
  249.                 sahf
  250.                 jae .u0ch
  251.                         fchs
  252.                         fstp dword[angle] ;if (u0 < 0) angle *= -1
  253.                 .u0ch:
  254.                 stdcall gl_M4_Rotate, ecx,[angle],0
  255.                 jmp .end_sw
  256.         @@:
  257.         cmp ebx,2
  258.         jne @f
  259.                 fcomp dword[u1] ;0 ... u1
  260.                 fstsw ax
  261.                 sahf
  262.                 jae .u1ch
  263.                         fchs
  264.                         fstp dword[angle] ;if (u1 < 0) angle *= -1
  265.                 .u1ch:
  266.                 stdcall gl_M4_Rotate, ecx,[angle],1
  267.                 jmp .end_sw
  268.         @@:
  269.         cmp ebx,1
  270.         jne @f
  271.                 fcomp dword[u2] ;0 ... u2
  272.                 fstsw ax
  273.                 sahf
  274.                 jae .u2ch
  275.                         fchs
  276.                         fstp dword[angle] ;if (u2 < 0) angle *= -1
  277.                 .u2ch:
  278.                 stdcall gl_M4_Rotate, ecx,[angle],2
  279.                 jmp .end_sw
  280.         @@: ;default:
  281. if DEBUG ;glopRotete
  282.                 stdcall dbg_print,txt_sp,m_1
  283. end if
  284.  
  285.                 ; normalize vector
  286.                 fld dword[u0]
  287.                 fmul st0,st0
  288.                 fld dword[u1]
  289.                 fmul st0,st0
  290.                 fld dword[u2]
  291.                 fmul st0,st0
  292.                 fadd st0,st1
  293.                 fadd st0,st2
  294. ;               fst dword[len] ;len = u0*u0+u1*u1+u2*u2
  295.                 fcom st1
  296.                 fstsw ax
  297.                 sahf
  298.                 je .end_f ;if (len == 0.0f) return
  299.                 fsqrt
  300.                 fld1
  301.                 fdiv st0,st1
  302. ;               fst dword[len] ;len = 1.0f / sqrt(len)
  303.                 fld dword[u0]
  304.                 fmul st0,st1
  305.                 fstp dword[u0] ;u0 *= len
  306.                 fld dword[u1]
  307.                 fmul st0,st1
  308.                 fstp dword[u1] ;u1 *= len
  309.                 fld dword[u2]
  310.                 fmul st0,st1
  311.                 fstp dword[u2] ;u2 *= len
  312.                 ;st0 = len, st1=..., st2=..., st3 = 0, st4 = angle
  313.  
  314.                 ; store cos and sin values
  315.                 finit
  316.                 fld dword[angle]
  317.                 fcos
  318.                 fst dword[cost] ;cost=cos(angle)
  319.                 fld dword[angle]
  320.                 fsin
  321.                 fst dword[sint] ;sint=sin(angle)
  322.  
  323.                 ; fill in the values
  324.                 mov ebx,0.0
  325.                 mov [ecx+3*16   ],ebx ;m[3][0]
  326.                 mov [ecx+3*16 +4],ebx ;m[3][1]
  327.                 mov [ecx+3*16 +8],ebx ;m[3][2]
  328.                 mov [ecx+     12],ebx ;m[0][3]
  329.                 mov [ecx+  16+12],ebx ;m[1][3]
  330.                 mov [ecx+2*16+12],ebx ;m[2][3]
  331.                 mov ebx,1.0
  332.                 mov [ecx+3*16+12],ebx ;m[3][3]
  333.  
  334.                 ; do the math
  335. ;      m.m[0][0]=u[0]*u[0]+cost*(1-u[0]*u[0]);
  336. ;      m.m[1][0]=u[0]*u[1]*(1-cost)-u[2]*sint;
  337. ;      m.m[2][0]=u[2]*u[0]*(1-cost)+u[1]*sint;
  338. ;      m.m[0][1]=u[0]*u[1]*(1-cost)+u[2]*sint;
  339. ;      m.m[1][1]=u[1]*u[1]+cost*(1-u[1]*u[1]);
  340. ;      m.m[2][1]=u[1]*u[2]*(1-cost)-u[0]*sint;
  341. ;      m.m[0][2]=u[2]*u[0]*(1-cost)-u[1]*sint;
  342. ;      m.m[1][2]=u[1]*u[2]*(1-cost)+u[0]*sint;
  343. ;      m.m[2][2]=u[2]*u[2]+cost*(1-u[2]*u[2]);
  344.         .end_sw:
  345.  
  346.         mov eax,[context]
  347.         mov ebx,[eax+offs_cont_matrix_mode]
  348.         shl ebx,2
  349.         add ebx,eax
  350.         stdcall gl_M4_MulLeft,dword[ebx+offs_cont_matrix_stack_ptr],ecx
  351. if DEBUG ;glopRotete
  352.                 stdcall gl_print_matrix,ecx,4
  353. end if
  354.         gl_matrix_update eax,ebx
  355.         .end_f:
  356.         ret
  357. endp
  358.  
  359. align 4
  360. proc glopScale uses eax ebx ecx, context:dword, p:dword
  361.         mov ecx,[p]
  362.  
  363.         mov eax,[context]
  364.         mov ebx,[eax+offs_cont_matrix_mode]
  365.         shl ebx,2
  366.         add ebx,eax
  367.         mov ebx,[ebx+offs_cont_matrix_stack_ptr] ;ebx = &m[0]
  368.  
  369.         fld dword[ecx+ 4] ;x
  370.         fld dword[ecx+ 8] ;y
  371.         fld dword[ecx+12] ;z
  372.         mov ecx,4
  373.         @@:
  374.         fld dword[ebx]   ;m[0]
  375.         fmul st0,st3     ;m[0] * x
  376.         fstp dword[ebx]  ;m[0] *= x
  377.         fld dword[ebx+4] ;m[1]
  378.         fmul st0,st2     ;m[1] * y
  379.         fstp dword[ebx+4];m[1] *= y
  380.         fld dword[ebx+8] ;m[2]
  381.         fmul st0,st1     ;m[2] * z
  382.         fstp dword[ebx+8];m[2] *= z
  383.         add ebx,16
  384.         loop @b
  385.  
  386. if DEBUG ;glopScale
  387. pushad
  388.         stdcall dbg_print,txt_gl_scal,txt_nl
  389.         mov ebx,[eax+offs_cont_matrix_mode]
  390.         shl ebx,2
  391.         add ebx,eax
  392.         stdcall gl_print_matrix,[ebx+offs_cont_matrix_stack_ptr],4
  393. popad
  394. end if
  395.         gl_matrix_update eax,ebx
  396.         ret
  397. endp
  398.  
  399. align 4
  400. proc glopTranslate uses eax ebx ecx, context:dword, p:dword
  401.         mov ecx,[p]
  402.  
  403.         mov eax,[context]
  404.         mov ebx,[eax+offs_cont_matrix_mode]
  405.         shl ebx,2
  406.         add ebx,eax
  407.         mov ebx,[ebx+offs_cont_matrix_stack_ptr] ;ebx = &m[0]
  408.  
  409.         fld dword[ecx+ 4] ;x
  410.         fld dword[ecx+ 8] ;y
  411.         fld dword[ecx+12] ;z
  412.         mov ecx,4
  413.         @@:
  414.                 fld dword[ebx]   ;m[0]
  415.                 fmul st0,st3     ;m[0] * x
  416.                 fld dword[ebx+4] ;m[1]
  417.                 fmul st0,st3     ;m[1] * y
  418.                 faddp
  419.                 fld dword[ebx+8] ;m[2]
  420.                 fmul st0,st2     ;m[2] * z
  421.                 faddp
  422.                 fadd dword[ebx+12] ;m[3]
  423.                 fstp dword[ebx+12] ;m[3] = m[0] * x + m[1] * y + m[2] * z + m[3]
  424.                 add ebx,16
  425.         loop @b
  426.         ffree st0
  427.         fincstp
  428.         ffree st0
  429.         fincstp
  430.         ffree st0
  431.         fincstp
  432.  
  433. if DEBUG ;glopTranslate
  434. pushad
  435.         stdcall dbg_print,txt_gl_tran,txt_nl
  436.         mov ebx,[eax+offs_cont_matrix_mode]
  437.         shl ebx,2
  438.         add ebx,eax
  439.         stdcall gl_print_matrix,[ebx+offs_cont_matrix_stack_ptr],4
  440. popad
  441. end if
  442.         gl_matrix_update eax,ebx
  443.         ret
  444. endp
  445.  
  446. align 4
  447. proc glopFrustum uses eax ebx ecx, context:dword, p:dword
  448. locals
  449.         x dd ?
  450.         y dd ?
  451.         A dd ?
  452.         B dd ?
  453.         C dd ?
  454.         D dd ?
  455.         m M4
  456. endl
  457.         mov eax,[context]
  458.         mov ebx,[p]
  459.  
  460.         fld dword[ebx+8]
  461.         fsub dword[ebx+4]  ;st0 = (right-left)
  462.         fld dword[ebx+20]  ;st0 = near
  463.         fadd st0,st0
  464.         fdiv st0,st1
  465.         fstp dword[x]      ;x = (2.0*near) / (right-left)
  466.         fld dword[ebx+16]
  467.         fsub dword[ebx+12] ;st0 = (top-bottom)
  468.         fld dword[ebx+20]  ;st0 = near
  469.         fadd st0,st0
  470.         fdiv st0,st1
  471.         fstp dword[y]      ;y = (2.0*near) / (top-bottom)
  472.         fld dword[ebx+8]
  473.         fadd dword[ebx+4]  ;st0 = (right+left)
  474.         fdiv st0,st2       ;st2 = (right-left)
  475.         fstp dword[A]      ;A = (right+left) / (right-left)
  476.         fld dword[ebx+16]
  477.         fadd dword[ebx+12] ;st0 = (top+bottom)
  478.         fdiv st0,st1       ;st1 = (top-bottom)
  479.         fstp dword[B]      ;B = (top+bottom) / (top-bottom)
  480.         fld dword[ebx+24]
  481.         fsub dword[ebx+20] ;st0 = (farp-near)
  482.         fldz
  483.         fsub dword[ebx+24]
  484.         fsub dword[ebx+20] ;st0 = -(farp+near)
  485.         fdiv st0,st1
  486.         fstp dword[C]      ;C = -(farp+near) / (farp-near)
  487.         fld dword[ebx+24]
  488.         fmul dword[ebx+20] ;st0 = farp*near
  489.         fadd st0,st0
  490.         fchs               ;st0 = -(2.0*farp*near)
  491.         fdiv st0,st1
  492.         fstp dword[D]      ;D = -(2.0*farp*near) / (farp-near)
  493.  
  494.         mov ecx,ebp
  495.         sub ecx,sizeof.M4
  496.  
  497.         mov ebx,[x]
  498.         mov dword[ecx],ebx
  499.         mov dword[ecx+4],0.0
  500.         mov ebx,[A]
  501.         mov dword[ecx+8],ebx
  502.         mov dword[ecx+12],0.0
  503.         mov dword[ecx+16],0.0
  504.         mov ebx,[y]
  505.         mov dword[ecx+20],ebx
  506.         mov ebx,[B]
  507.         mov dword[ecx+24],ebx
  508.         mov dword[ecx+28],0.0
  509.         mov dword[ecx+32],0.0
  510.         mov dword[ecx+36],0.0
  511.         mov ebx,[C]
  512.         mov dword[ecx+40],ebx
  513.         mov ebx,[D]
  514.         mov dword[ecx+44],ebx
  515.         mov dword[ecx+48],0.0
  516.         mov dword[ecx+52],0.0
  517.         mov dword[ecx+56],-1.0
  518.         mov dword[ecx+60],0.0
  519.  
  520.         mov ebx,[eax+offs_cont_matrix_mode]
  521.         shl ebx,2
  522.         add ebx,eax
  523.         stdcall gl_M4_MulLeft,dword[ebx+offs_cont_matrix_stack_ptr],ecx
  524.  
  525. if DEBUG ;glopFrustum
  526.         stdcall gl_print_matrix,ecx,4
  527.         stdcall gl_print_matrix,dword[ebx+offs_cont_matrix_stack_ptr],4
  528. end if
  529.         gl_matrix_update eax,ebx
  530.         ret
  531. endp
  532.  
  533.  
  534.