Subversion Repositories Kolibri OS

Rev

Rev 5418 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5418 Rev 6017
1
; Some simple mathematical functions. Don't look for some logic in
1
; Some simple mathematical functions. Don't look for some logic in
2
; the function names :-)
2
; the function names :-)
3
 
3
 
4
; ******* Gestion des matrices 4x4 ******
4
; ******* Gestion des matrices 4x4 ******
5
 
5
 
6
if DEBUG
6
if DEBUG
7
f_m4m db 'gl_M4_Mul',0
7
f_m4m db 'gl_M4_Mul',0
8
f_m4ml db 'gl_M4_MulLeft',0
8
f_m4ml db 'gl_M4_MulLeft',0
9
end if
9
end if
10
 
10
 
11
align 4
11
align 4
12
proc gl_M4_Id uses eax ecx edi, a:dword
12
proc gl_M4_Id uses eax ecx edi, a:dword
13
	mov edi,[a]
13
	mov edi,[a]
14
	add edi,4
14
	add edi,4
15
	mov ecx,14
15
	mov ecx,14
16
	mov eax,0.0
16
	mov eax,0.0
17
	rep stosd
17
	rep stosd
18
	mov eax,1.0
18
	mov eax,1.0
19
	stosd
19
	stosd
20
	mov edi,[a]
20
	mov edi,[a]
21
	stosd
21
	stosd
22
	add edi,16
22
	add edi,16
23
	stosd
23
	stosd
24
	add edi,16
24
	add edi,16
25
	stosd
25
	stosd
26
	ret
26
	ret
27
endp
27
endp
28
 
28
 
29
align 4
29
align 4
30
proc gl_M4_IsId uses ebx ecx, a:dword
30
proc gl_M4_IsId uses ebx ecx, a:dword
31
	mov eax,[a]
31
	mov eax,[a]
32
	xor ebx,ebx
32
	xor ebx,ebx
33
	xor ecx,ecx
33
	xor ecx,ecx
34
	.cycle_01:
34
	.cycle_01:
35
		fld dword[eax]
35
		fld dword[eax]
36
		cmp ecx,ebx
36
		cmp ecx,ebx
37
		je .once
37
		je .once
38
			ftst ;ñðàâíåíèå ñ 0.0
38
			ftst ;ñðàâíåíèå ñ 0.0
39
			fstsw ax
39
			fstsw ax
40
			sahf
40
			sahf
41
			je @f
41
			je @f
42
			jmp .not_1 ;åñëè äèàãîíàëüíûå ÷èñëà íå ðàâíû 0.0 ìàòðèöà íå åäèíè÷íàÿ
42
			jmp .not_1 ;åñëè äèàãîíàëüíûå ÷èñëà íå ðàâíû 0.0 ìàòðèöà íå åäèíè÷íàÿ
43
		.once:
43
		.once:
44
			fld1
44
			fld1
45
			fcomp st1 ;ñðàâíåíèå ñ 1.0
45
			fcomp st1 ;ñðàâíåíèå ñ 1.0
46
			fstsw ax
46
			fstsw ax
47
			test ah,0x40
47
			test ah,0x40
48
			je .not_1 ;åñëè íå ðàâíî 1.0 ìàòðèöà íå åäèíè÷íàÿ
48
			je .not_1 ;åñëè íå ðàâíî 1.0 ìàòðèöà íå åäèíè÷íàÿ
49
		@@:
49
		@@:
50
		ffree st0
50
		ffree st0
51
		fincstp
51
		fincstp
52
		add eax,4
52
		add eax,4
53
		inc ebx
53
		inc ebx
54
		btr ebx,2
54
		btr ebx,2
55
		jnc .cycle_01
55
		jnc .cycle_01
56
	inc ecx
56
	inc ecx
57
	bt ecx,2 ;ïðîâåðÿåì ðàâåíñòâî ecx==4
57
	bt ecx,2 ;ïðîâåðÿåì ðàâåíñòâî ecx==4
58
	jnc .cycle_01
58
	jnc .cycle_01
59
 
59
 
60
	mov eax,1
60
	mov eax,1
61
	jmp @f
61
	jmp @f
62
	.not_1:
62
	.not_1:
63
		ffree st0
63
		ffree st0
64
		fincstp
64
		fincstp
65
		xor eax,eax
65
		xor eax,eax
66
	@@:
66
	@@:
67
	ret
67
	ret
68
endp
68
endp
69
 
69
 
70
align 4
70
align 4
71
proc gl_M4_Mul, c:dword,a:dword,b:dword
71
proc gl_M4_Mul, c:dword,a:dword,b:dword
72
pushad
72
pushad
73
	mov edx,[c]
73
	mov edx,[c]
74
	xor eax,eax
74
	xor eax,eax
75
	.cycle_0: ;i
75
	.cycle_0: ;i
76
		xor ebx,ebx
76
		xor ebx,ebx
77
		.cycle_1: ;j
77
		.cycle_1: ;j
78
			fldz ;sum=0
78
			fldz ;sum=0
79
			xor ecx,ecx
79
			xor ecx,ecx
80
			M4_reg edi,[a],eax,0
80
			M4_reg edi,[a],eax,0
81
			.cycle_2: ;k
81
			.cycle_2: ;k
82
				fld dword[edi]
82
				fld dword[edi]
83
				add edi,4
83
				add edi,4
84
				M4_reg esi,[b],ecx,ebx
84
				M4_reg esi,[b],ecx,ebx
85
				fmul dword[esi]
85
				fmul dword[esi]
86
				faddp ;sum += a[i][k] * b[k][j]
86
				faddp ;sum += a[i][k] * b[k][j]
87
				inc ecx
87
				inc ecx
88
				cmp ecx,4
88
				cmp ecx,4
89
				jl .cycle_2
89
				jl .cycle_2
90
			fstp dword[edx] ;c[i][j] = sum
90
			fstp dword[edx] ;c[i][j] = sum
91
			add edx,4
91
			add edx,4
92
			inc ebx
92
			inc ebx
93
			cmp ebx,4
93
			cmp ebx,4
94
			jl .cycle_1
94
			jl .cycle_1
95
		inc eax
95
		inc eax
96
		cmp eax,4
96
		cmp eax,4
97
		jl .cycle_0
97
		jl .cycle_0
98
if DEBUG ;gl_M4_Mul
98
if DEBUG ;gl_M4_Mul
99
	stdcall dbg_print,f_m4m,txt_nl
99
	stdcall dbg_print,f_m4m,txt_nl
100
	stdcall gl_print_matrix,[c],4
100
	stdcall gl_print_matrix,[c],4
101
	stdcall dbg_print,txt_sp,txt_nl
101
	stdcall dbg_print,txt_sp,txt_nl
102
end if
102
end if
103
popad
103
popad
104
	ret
104
	ret
105
endp
105
endp
106
 
106
 
107
; c=c*a
107
; c=c*a
108
align 4
108
align 4
109
proc gl_M4_MulLeft, c:dword,b:dword
109
proc gl_M4_MulLeft, c:dword,b:dword
110
locals
110
locals
111
	i dd ?
111
	i dd ?
112
	a M4
112
	a M4
113
endl
113
endl
114
pushad
114
pushad
115
	mov ecx,16
115
	mov ecx,16
116
	mov esi,[c]
116
	mov esi,[c]
117
	mov edi,ebp
117
	mov edi,ebp
118
	sub edi,sizeof.M4
118
	sub edi,sizeof.M4
119
	rep movsd ;êîïèðîâàíèå ìàòðèö [a]=[c]
119
	rep movsd ;êîïèðîâàíèå ìàòðèö [a]=[c]
120
 
120
 
121
	mov edx,[c]
121
	mov edx,[c]
122
	mov dword[i],0
122
	mov dword[i],0
123
	mov eax,ebp
123
	mov eax,ebp
124
	sub eax,sizeof.M4
124
	sub eax,sizeof.M4
125
	.cycle_0: ;i
125
	.cycle_0: ;i
126
		xor ebx,ebx ;j=0
126
		xor ebx,ebx ;j=0
127
		.cycle_1: ;j
127
		.cycle_1: ;j
128
			fldz ;sum=0
128
			fldz ;sum=0
129
			xor ecx,ecx ;k=0
129
			xor ecx,ecx ;k=0
130
			M4_reg edi,eax,dword[i],0
130
			M4_reg edi,eax,dword[i],0
131
			.cycle_2: ;k
131
			.cycle_2: ;k
132
				fld dword[edi]
132
				fld dword[edi]
133
				add edi,4
133
				add edi,4
134
				M4_reg esi,[b],ecx,ebx
134
				M4_reg esi,[b],ecx,ebx
135
				fmul dword[esi]
135
				fmul dword[esi]
136
				faddp ;sum += a[i][k] * b[k][j]
136
				faddp ;sum += a[i][k] * b[k][j]
137
				inc ecx
137
				inc ecx
138
				cmp ecx,4
138
				cmp ecx,4
139
				jl .cycle_2
139
				jl .cycle_2
140
			fstp dword[edx] ;c[i][j] = sum
140
			fstp dword[edx] ;c[i][j] = sum
141
			add edx,4
141
			add edx,4
142
			inc ebx
142
			inc ebx
143
			cmp ebx,4
143
			cmp ebx,4
144
			jl .cycle_1
144
			jl .cycle_1
145
		inc dword[i]
145
		inc dword[i]
146
		cmp dword[i],4
146
		cmp dword[i],4
147
		jl .cycle_0
147
		jl .cycle_0
148
if DEBUG ;gl_M4_MulLeft
148
if DEBUG ;gl_M4_MulLeft
149
	stdcall dbg_print,f_m4ml,txt_nl
149
	stdcall dbg_print,f_m4ml,txt_nl
150
	stdcall gl_print_matrix,[c],4
150
	stdcall gl_print_matrix,[c],4
151
	stdcall dbg_print,txt_sp,txt_nl
151
	stdcall dbg_print,txt_sp,txt_nl
152
end if
152
end if
153
popad
153
popad
154
	ret
154
	ret
155
endp
155
endp
156
 
156
 
157
align 4
157
align 4
158
proc gl_M4_Move uses ecx edi esi, a:dword,b:dword
158
proc gl_M4_Move uses ecx edi esi, a:dword,b:dword
159
	mov edi,[a]
159
	mov edi,[a]
160
	mov esi,[b]
160
	mov esi,[b]
161
	mov ecx,sizeof.M4/4
161
	mov ecx,sizeof.M4/4
162
	rep movsd
162
	rep movsd
163
	ret
163
	ret
164
endp
164
endp
165
 
165
 
166
align 4
166
align 4
167
proc gl_MoveV3 uses edi esi, a:dword,b:dword
167
proc gl_MoveV3 uses edi esi, a:dword,b:dword
168
	mov edi,[a]
168
	mov edi,[a]
169
	mov esi,[b]
169
	mov esi,[b]
170
	movsd
170
	movsd
171
	movsd
171
	movsd
172
	movsd
172
	movsd
173
	ret
173
	ret
174
endp
174
endp
175
 
175
 
176
;void gl_MulM4V3(V3 *a,M4 *b,V3 *c)
176
;void gl_MulM4V3(V3 *a,M4 *b,V3 *c)
177
;{
177
;{
178
;        a->X=b->m[0][0]*c->X+b->m[0][1]*c->Y+b->m[0][2]*c->Z+b->m[0][3];
178
;        a->X=b->m[0][0]*c->X+b->m[0][1]*c->Y+b->m[0][2]*c->Z+b->m[0][3];
179
;        a->Y=b->m[1][0]*c->X+b->m[1][1]*c->Y+b->m[1][2]*c->Z+b->m[1][3];
179
;        a->Y=b->m[1][0]*c->X+b->m[1][1]*c->Y+b->m[1][2]*c->Z+b->m[1][3];
180
;        a->Z=b->m[2][0]*c->X+b->m[2][1]*c->Y+b->m[2][2]*c->Z+b->m[2][3];
180
;        a->Z=b->m[2][0]*c->X+b->m[2][1]*c->Y+b->m[2][2]*c->Z+b->m[2][3];
181
;}
181
;}
182
 
182
 
183
;void gl_MulM3V3(V3 *a,M4 *b,V3 *c)
183
;void gl_MulM3V3(V3 *a,M4 *b,V3 *c)
184
;{
184
;{
185
;        a->X=b->m[0][0]*c->X+b->m[0][1]*c->Y+b->m[0][2]*c->Z;
185
;        a->X=b->m[0][0]*c->X+b->m[0][1]*c->Y+b->m[0][2]*c->Z;
186
;        a->Y=b->m[1][0]*c->X+b->m[1][1]*c->Y+b->m[1][2]*c->Z;
186
;        a->Y=b->m[1][0]*c->X+b->m[1][1]*c->Y+b->m[1][2]*c->Z;
187
;        a->Z=b->m[2][0]*c->X+b->m[2][1]*c->Y+b->m[2][2]*c->Z;
187
;        a->Z=b->m[2][0]*c->X+b->m[2][1]*c->Y+b->m[2][2]*c->Z;
188
;}
188
;}
189
 
189
 
190
align 4
190
align 4
191
proc gl_M4_MulV4 uses ebx ecx edx, a:dword, b:dword, c:dword ;V4 *a, M4 *b, V4 *c
191
proc gl_M4_MulV4 uses ebx ecx edx, a:dword, b:dword, c:dword ;V4 *a, M4 *b, V4 *c
192
	mov ebx,[b]
192
	mov ebx,[b]
193
	mov edx,[c]
193
	mov edx,[c]
194
	fld dword[edx]
194
	fld dword[edx]
195
	fld dword[edx+4]
195
	fld dword[edx+4]
196
	fld dword[edx+8]
196
	fld dword[edx+8]
197
	fld dword[edx+12]
197
	fld dword[edx+12]
198
	mov edx,[a]
198
	mov edx,[a]
199
	mov ecx,4
199
	mov ecx,4
200
	.cycle_1:
200
	.cycle_1:
201
		fld dword[ebx]    ;st0 = m[_][0]
201
		fld dword[ebx]    ;st0 = m[_][0]
202
		fmul st0,st4      ;st0 *= c.X
202
		fmul st0,st4      ;st0 *= c.X
203
		fld dword[ebx+4]  ;st0 = m[_][1]
203
		fld dword[ebx+4]  ;st0 = m[_][1]
204
		fmul st0,st4      ;st0 *= c.Y
204
		fmul st0,st4      ;st0 *= c.Y
205
		faddp
205
		faddp
206
		fld dword[ebx+8]  ;st0 = m[_][2]
206
		fld dword[ebx+8]  ;st0 = m[_][2]
207
		fmul st0,st3      ;st0 *= c.Z
207
		fmul st0,st3      ;st0 *= c.Z
208
		faddp
208
		faddp
209
		fld dword[ebx+12] ;st0 += m[_][3]
209
		fld dword[ebx+12] ;st0 += m[_][3]
210
		fmul st0,st2      ;st0 *= c.Z
210
		fmul st0,st2      ;st0 *= c.W
211
		faddp
211
		faddp
212
		fstp dword[edx]   ;a.X = b.m[_][0]*c.X +b.m[_][1]*c.Y +b.m[_][2]*c.Z +b.m[_][3]*c.W
212
		fstp dword[edx]   ;a.X = b.m[_][0]*c.X +b.m[_][1]*c.Y +b.m[_][2]*c.Z +b.m[_][3]*c.W
213
		add ebx,16 ;ñëåäóùàÿ ñòðîêà ìàòðèöû
213
		add ebx,16 ;ñëåäóùàÿ ñòðîêà ìàòðèöû
214
		add edx,4  ;ñëåäóùàÿ êîîðäèíàòà âåêòîðà
214
		add edx,4  ;ñëåäóùàÿ êîîðäèíàòà âåêòîðà
215
	loop .cycle_1
215
	loop .cycle_1
216
	ffree st0
216
	ffree st0
217
	fincstp
217
	fincstp
218
	ffree st0
218
	ffree st0
219
	fincstp
219
	fincstp
220
	ffree st0
220
	ffree st0
221
	fincstp
221
	fincstp
222
	ffree st0
222
	ffree st0
223
	fincstp
223
	fincstp
224
	ret
224
	ret
225
endp
225
endp
226
 
226
 
227
; transposition of a 4x4 matrix
227
; transposition of a 4x4 matrix
228
align 4
228
align 4
229
proc gl_M4_Transpose uses eax ecx edx, a:dword, b:dword
229
proc gl_M4_Transpose uses eax ecx edx, a:dword, b:dword
230
	mov eax,[a]
230
	mov eax,[a]
231
	mov ecx,[b]
231
	mov ecx,[b]
232
 
232
 
233
	mov edx,[ecx]
233
	mov edx,[ecx]
234
	mov [eax],edx
234
	mov [eax],edx
235
	mov edx,[ecx+0x10]
235
	mov edx,[ecx+0x10]
236
	mov [eax+0x4],edx
236
	mov [eax+0x4],edx
237
	mov edx,[ecx+0x20]
237
	mov edx,[ecx+0x20]
238
	mov [eax+0x8],edx
238
	mov [eax+0x8],edx
239
	mov edx,[ecx+0x30]
239
	mov edx,[ecx+0x30]
240
	mov [eax+0x0c],edx
240
	mov [eax+0x0c],edx
241
 
241
 
242
	mov edx,[ecx+0x4]
242
	mov edx,[ecx+0x4]
243
	mov [eax+0x10],edx
243
	mov [eax+0x10],edx
244
	mov edx,[ecx+0x14]
244
	mov edx,[ecx+0x14]
245
	mov [eax+0x14],edx
245
	mov [eax+0x14],edx
246
	mov edx,[ecx+0x24]
246
	mov edx,[ecx+0x24]
247
	mov [eax+0x18],edx
247
	mov [eax+0x18],edx
248
	mov edx,[ecx+0x34]
248
	mov edx,[ecx+0x34]
249
	mov [eax+0x1c],edx
249
	mov [eax+0x1c],edx
250
 
250
 
251
	mov edx,[ecx+0x8]
251
	mov edx,[ecx+0x8]
252
	mov [eax+0x20],edx
252
	mov [eax+0x20],edx
253
	mov edx,[ecx+0x18]
253
	mov edx,[ecx+0x18]
254
	mov [eax+0x24],edx
254
	mov [eax+0x24],edx
255
	mov edx,[ecx+0x28]
255
	mov edx,[ecx+0x28]
256
	mov [eax+0x28],edx
256
	mov [eax+0x28],edx
257
	mov edx,[ecx+0x38]
257
	mov edx,[ecx+0x38]
258
	mov [eax+0x2c],edx
258
	mov [eax+0x2c],edx
259
 
259
 
260
	mov edx,[ecx+0x0c]
260
	mov edx,[ecx+0x0c]
261
	mov [eax+0x30],edx
261
	mov [eax+0x30],edx
262
	mov edx,[ecx+0x1c]
262
	mov edx,[ecx+0x1c]
263
	mov [eax+0x34],edx
263
	mov [eax+0x34],edx
264
	mov edx,[ecx+0x2c]
264
	mov edx,[ecx+0x2c]
265
	mov [eax+0x38],edx
265
	mov [eax+0x38],edx
266
	mov edx,[ecx+0x3c]
266
	mov edx,[ecx+0x3c]
267
	mov [eax+0x3c],edx
267
	mov [eax+0x3c],edx
268
	ret
268
	ret
269
endp
269
endp
270
 
270
 
271
; inversion of an orthogonal matrix of type Y=M.X+P
271
; inversion of an orthogonal matrix of type Y=M.X+P
272
;void gl_M4_InvOrtho(M4 *a,M4 b)
272
;void gl_M4_InvOrtho(M4 *a,M4 b)
273
;{
273
;{
274
;       int i,j;
274
;       int i,j;
275
;       float s;
275
;       float s;
276
;       for(i=0;i<3;i++)
276
;       for(i=0;i<3;i++)
277
;       for(j=0;j<3;j++) a->m[i][j]=b.m[j][i];
277
;       for(j=0;j<3;j++) a->m[i][j]=b.m[j][i];
278
;       a->m[3][0]=0.0; a->m[3][1]=0.0; a->m[3][2]=0.0; a->m[3][3]=1.0;
278
;       a->m[3][0]=0.0; a->m[3][1]=0.0; a->m[3][2]=0.0; a->m[3][3]=1.0;
279
;       for(i=0;i<3;i++) {
279
;       for(i=0;i<3;i++) {
280
;               s=0;
280
;               s=0;
281
;               for(j=0;j<3;j++) s-=b.m[j][i]*b.m[j][3];
281
;               for(j=0;j<3;j++) s-=b.m[j][i]*b.m[j][3];
282
;               a->m[i][3]=s;
282
;               a->m[i][3]=s;
283
;       }
283
;       }
284
;}
284
;}
285
 
285
 
286
; Inversion of a general nxn matrix.
286
; Inversion of a general nxn matrix.
287
; Note : m is destroyed
287
; Note : m is destroyed
288
 
288
 
289
align 4
289
align 4
290
proc Matrix_Inv uses ebx ecx edx edi esi, r:dword, m:dword, n:dword ;(float *r,float *m,int n)
290
proc Matrix_Inv uses ebx ecx edx edi esi, r:dword, m:dword, n:dword ;(float *r,float *m,int n)
291
locals
291
locals
292
	max dd ? ;float
292
	max dd ? ;float
293
	tmp dd ?
293
	tmp dd ?
294
endl
294
endl
295
 
295
 
296
	; identitée dans r
296
	; identitée dans r
297
	mov eax,0.0
297
	mov eax,0.0
298
	mov ecx,[n]
298
	mov ecx,[n]
299
	imul ecx,ecx
299
	imul ecx,ecx
300
	mov edi,[r]
300
	mov edi,[r]
301
	rep stosd ;for(i=0;i
301
	rep stosd ;for(i=0;i
302
	mov eax,1.0
302
	mov eax,1.0
303
	xor ebx,ebx
303
	xor ebx,ebx
304
	mov edi,[r]
304
	mov edi,[r]
305
	mov ecx,[n]
305
	mov ecx,[n]
306
	shl ecx,2
306
	shl ecx,2
307
	@@: ;for(i=0;i
307
	@@: ;for(i=0;i
308
		cmp ebx,[n]
308
		cmp ebx,[n]
309
		jge .end_0
309
		jge .end_0
310
		stosd ;r[i*n+i]=1
310
		stosd ;r[i*n+i]=1
311
		add edi,ecx
311
		add edi,ecx
312
		inc ebx
312
		inc ebx
313
		jmp @b
313
		jmp @b
314
	.end_0:
314
	.end_0:
315
 
315
 
316
	; ebx -> n
316
	; ebx -> n
317
	; ecx -> j
317
	; ecx -> j
318
	; edx -> k
318
	; edx -> k
319
	; edi -> i
319
	; edi -> i
320
	; esi -> l
320
	; esi -> l
321
	mov ebx,[n]
321
	mov ebx,[n]
322
	xor ecx,ecx
322
	xor ecx,ecx
323
	.cycle_0: ;for(j=0;j
323
	.cycle_0: ;for(j=0;j
324
	cmp ecx,ebx
324
	cmp ecx,ebx
325
	jge .cycle_0_end
325
	jge .cycle_0_end
326
		; recherche du nombre de plus grand module sur la colonne j
326
		; recherche du nombre de plus grand module sur la colonne j
327
		mov eax,ecx
327
		mov eax,ecx
328
		imul eax,ebx
328
		imul eax,ebx
329
		add eax,ecx
329
		add eax,ecx
330
		shl eax,2
330
		shl eax,2
331
		add eax,[m]
331
		add eax,[m]
332
		mov eax,[eax]
332
		mov eax,[eax]
333
		mov [max],eax ;max=m[j*n+j]
333
		mov [max],eax ;max=m[j*n+j]
334
		mov edx,ecx ;k=j
334
		mov edx,ecx ;k=j
335
		mov edi,ecx
335
		mov edi,ecx
336
		inc edi
336
		inc edi
337
		.cycle_1: ;for(i=j+1;i
337
		.cycle_1: ;for(i=j+1;i
338
		cmp edi,ebx
338
		cmp edi,ebx
339
		jge .cycle_1_end
339
		jge .cycle_1_end
340
			mov eax,edi
340
			mov eax,edi
341
			imul eax,ebx
341
			imul eax,ebx
342
			add eax,ecx
342
			add eax,ecx
343
			shl eax,2
343
			shl eax,2
344
			add eax,[m]
344
			add eax,[m]
345
			fld dword[eax]
345
			fld dword[eax]
-
 
346
			fld st0
-
 
347
			fabs
-
 
348
			fld dword[max]
-
 
349
			fabs
346
			fcom dword[max] ;if (fabs(m[i*n+j])>fabs(max))
350
			fcompp ;if (fabs(m[i*n+j])>fabs(max))
347
			fstsw ax
351
			fstsw ax
348
			sahf
352
			sahf
349
			jbe @f
353
			jae @f
350
				mov edx,edi ;k=i
354
				mov edx,edi ;k=i
351
				fst dword[max]
355
				fst dword[max]
352
			@@:
356
			@@:
353
			ffree st0
357
			ffree st0 ;m[i*n+j]
354
			fincstp
358
			fincstp
355
		inc edi
359
		inc edi
356
		jmp .cycle_1
360
		jmp .cycle_1
357
		.cycle_1_end:
361
		.cycle_1_end:
358
 
362
 
359
		; non intersible matrix
363
		; non intersible matrix
360
		fld dword[max]
364
		fld dword[max]
361
		ftst ;if (max==0)
365
		ftst ;if (max==0)
362
		fstsw ax
366
		fstsw ax
363
		ffree st0
367
		ffree st0
364
		fincstp
368
		fincstp
365
		sahf
369
		sahf
366
		jne @f
370
		jne @f
367
			xor eax,eax
371
			xor eax,eax
368
			inc eax
372
			inc eax
369
			jmp .end_f ;return 1
373
			jmp .end_f ;return 1
370
		@@:
374
		@@:
371
 
375
 
372
		; permutation des lignes j et k
376
		; permutation des lignes j et k
373
		cmp ecx,edx ;if (j!=k)
377
		cmp ecx,edx ;if (j!=k)
374
		je .cycle_2_end
378
		je .cycle_2_end
375
			xor edi,edi
379
			xor edi,edi
376
			.cycle_2: ;for(i=0;i
380
			.cycle_2: ;for(i=0;i
377
			cmp edi,ebx
381
			cmp edi,ebx
378
			jge .cycle_2_end
382
			jge .cycle_2_end
379
				;òóò ïîêà esi != l
383
				;òóò ïîêà esi != l
380
				mov eax,ecx
384
				mov eax,ecx
381
				imul eax,ebx
385
				imul eax,ebx
382
				add eax,edi
386
				add eax,edi
383
				shl eax,2
387
				shl eax,2
384
				add eax,[m]
388
				add eax,[m]
385
				mov esi,[eax]
389
				mov esi,[eax]
386
				mov [tmp],esi ;tmp=m[j*n+i]
390
				mov [tmp],esi ;tmp=m[j*n+i]
387
				mov esi,edx
391
				mov esi,edx
388
				imul esi,ebx
392
				imul esi,ebx
389
				add esi,edi
393
				add esi,edi
390
				shl esi,2
394
				shl esi,2
391
				add esi,[m]
395
				add esi,[m]
392
				m2m dword[eax],dword[esi] ;m[j*n+i]=m[k*n+i]
396
				m2m dword[eax],dword[esi] ;m[j*n+i]=m[k*n+i]
393
				mov eax,[tmp]
397
				mov eax,[tmp]
394
				mov [esi],eax ;m[k*n+i]=tmp
398
				mov [esi],eax ;m[k*n+i]=tmp
395
 
399
 
396
				mov eax,ecx
400
				mov eax,ecx
397
				imul eax,ebx
401
				imul eax,ebx
398
				add eax,edi
402
				add eax,edi
399
				shl eax,2
403
				shl eax,2
400
				add eax,[r]
404
				add eax,[r]
401
				mov esi,[eax]
405
				mov esi,[eax]
402
				mov [tmp],esi ;tmp=r[j*n+i]
406
				mov [tmp],esi ;tmp=r[j*n+i]
403
				mov esi,edx
407
				mov esi,edx
404
				imul esi,ebx
408
				imul esi,ebx
405
				add esi,edi
409
				add esi,edi
406
				shl esi,2
410
				shl esi,2
407
				add esi,[r]
411
				add esi,[r]
408
				m2m dword[eax],dword[esi] ;r[j*n+i]=r[k*n+i]
412
				m2m dword[eax],dword[esi] ;r[j*n+i]=r[k*n+i]
409
				mov eax,[tmp]
413
				mov eax,[tmp]
410
				mov [esi],eax ;r[k*n+i]=tmp
414
				mov [esi],eax ;r[k*n+i]=tmp
411
			inc edi
415
			inc edi
412
			jmp .cycle_2
416
			jmp .cycle_2
413
		.cycle_2_end:
417
		.cycle_2_end:
414
 
418
 
415
		; multiplication de la ligne j par 1/max
419
		; multiplication de la ligne j par 1/max
416
		fld1
420
		fld1
417
		fdiv dword[max]
421
		fdiv dword[max]
418
		fst dword[max] ;max=1/max
422
		fst dword[max] ;max=1/max
419
		xor edi,edi
423
		xor edi,edi
420
		mov eax,ecx
424
		mov eax,ecx
421
		imul eax,ebx
425
		imul eax,ebx
422
		shl eax,2
426
		shl eax,2
423
		.cycle_3: ;for(i=0;i
427
		.cycle_3: ;for(i=0;i
424
		cmp edi,ebx
428
		cmp edi,ebx
425
		jge .cycle_3_end
429
		jge .cycle_3_end
426
			add eax,[m]
430
			add eax,[m]
427
			fld dword[eax]
431
			fld dword[eax]
428
			fmul st0,st1
432
			fmul st0,st1
429
			fstp dword[eax] ;m[j*n+i]*=max
433
			fstp dword[eax] ;m[j*n+i]*=max
430
			sub eax,[m]
434
			sub eax,[m]
431
			add eax,[r]
435
			add eax,[r]
432
			fld dword[eax]
436
			fld dword[eax]
433
			fmul st0,st1
437
			fmul st0,st1
434
			fstp dword[eax] ;r[j*n+i]*=max
438
			fstp dword[eax] ;r[j*n+i]*=max
435
			sub eax,[r]
439
			sub eax,[r]
436
			add eax,4
440
			add eax,4
437
		inc edi
441
		inc edi
438
		jmp .cycle_3
442
		jmp .cycle_3
439
		.cycle_3_end:
443
		.cycle_3_end:
440
		ffree st0 ;max
444
		ffree st0 ;max
441
		fincstp
445
		fincstp
442
 
446
 
443
		xor esi,esi
447
		xor esi,esi
444
		.cycle_4: ;for(l=0;l
448
		.cycle_4: ;for(l=0;l
445
		cmp esi,ebx
449
		cmp esi,ebx
446
		jge .cycle_4_end
450
		jge .cycle_4_end
447
			cmp esi,ecx ;if (l!=j)
451
			cmp esi,ecx ;if (l!=j)
448
			je .if_end
452
			je .if_end
449
			mov eax,esi
453
			mov eax,esi
450
			imul eax,ebx
454
			imul eax,ebx
451
			add eax,ecx
455
			add eax,ecx
452
			shl eax,2
456
			shl eax,2
453
			add eax,[m]
457
			add eax,[m]
454
			fld dword[eax] ;t=m[l*n+j]
458
			fld dword[eax] ;t=m[l*n+j]
455
			xor edi,edi
459
			xor edi,edi
456
			.cycle_5: ;for(i=0;i
460
			.cycle_5: ;for(i=0;i
457
			cmp edi,ebx
461
			cmp edi,ebx
458
			jge .cycle_5_end
462
			jge .cycle_5_end
459
				mov eax,ecx
463
				mov eax,ecx
460
				imul eax,ebx
464
				imul eax,ebx
461
				add eax,edi
465
				add eax,edi
462
				shl eax,2
466
				shl eax,2
463
				add eax,[m]
467
				add eax,[m]
464
				fld dword[eax]
468
				fld dword[eax]
465
				fmul st0,st1
469
				fmul st0,st1
466
				mov eax,esi
470
				mov eax,esi
467
				imul eax,ebx
471
				imul eax,ebx
468
				add eax,edi
472
				add eax,edi
469
				shl eax,2
473
				shl eax,2
470
				add eax,[m]
474
				add eax,[m]
471
				fsub dword[eax]
475
				fsub dword[eax]
472
				fchs
476
				fchs
473
				fstp dword[eax] ;m[l*n+i]-=m[j*n+i]*t
477
				fstp dword[eax] ;m[l*n+i]-=m[j*n+i]*t
474
				mov eax,ecx
478
				mov eax,ecx
475
				imul eax,ebx
479
				imul eax,ebx
476
				add eax,edi
480
				add eax,edi
477
				shl eax,2
481
				shl eax,2
478
				add eax,[r]
482
				add eax,[r]
479
				fld dword[eax]
483
				fld dword[eax]
480
				fmul st0,st1
484
				fmul st0,st1
481
				mov eax,esi
485
				mov eax,esi
482
				imul eax,ebx
486
				imul eax,ebx
483
				add eax,edi
487
				add eax,edi
484
				shl eax,2
488
				shl eax,2
485
				add eax,[r]
489
				add eax,[r]
486
				fsub dword[eax]
490
				fsub dword[eax]
487
				fchs
491
				fchs
488
				fstp dword[eax] ;r[l*n+i]-=r[j*n+i]*t
492
				fstp dword[eax] ;r[l*n+i]-=r[j*n+i]*t
489
			inc edi
493
			inc edi
490
			jmp .cycle_5
494
			jmp .cycle_5
491
			.cycle_5_end:
495
			.cycle_5_end:
492
			ffree st0 ;t
496
			ffree st0 ;t
493
			fincstp
497
			fincstp
494
			.if_end:
498
			.if_end:
495
		inc esi
499
		inc esi
496
		jmp .cycle_4
500
		jmp .cycle_4
497
		.cycle_4_end:
501
		.cycle_4_end:
498
	inc ecx
502
	inc ecx
499
	jmp .cycle_0
503
	jmp .cycle_0
500
	.cycle_0_end:
504
	.cycle_0_end:
501
 
505
 
502
	xor eax,eax ;return 0
506
	xor eax,eax ;return 0
503
	.end_f:
507
	.end_f:
504
	ret
508
	ret
505
endp
509
endp
506
 
510
 
507
; inversion of a 4x4 matrix
511
; inversion of a 4x4 matrix
508
 
512
 
509
align 4
513
align 4
510
proc gl_M4_Inv uses eax ecx edi esi, a:dword, b:dword
514
proc gl_M4_Inv uses eax ecx edi esi, a:dword, b:dword
511
locals
515
locals
512
	tmp M4
516
	tmp M4
513
endl
517
endl
514
	mov esi,[b]
518
	mov esi,[b]
515
	mov edi,ebp
519
	mov edi,ebp
516
	sub edi,sizeof.M4 ;edi = &tmp
520
	sub edi,sizeof.M4 ;edi = &tmp
517
	mov ecx,16
521
	mov ecx,16
518
	rep movsd
522
	rep movsd
519
	sub edi,sizeof.M4 ;edi = &tmp
523
	sub edi,sizeof.M4 ;edi = &tmp
520
	stdcall Matrix_Inv,[a],edi,4 ;ïîðòèò eax ïîòîìó â uses åñòü eax
524
	stdcall Matrix_Inv,[a],edi,4 ;ïîðòèò eax ïîòîìó â uses åñòü eax
521
	ret
525
	ret
522
endp
526
endp
523
 
527
 
524
align 4
528
align 4
525
proc gl_M4_Rotate uses eax ecx, a:dword,t:dword,u:dword
529
proc gl_M4_Rotate uses eax ecx, a:dword,t:dword,u:dword
526
locals
530
locals
527
	s dd ? ;float
531
	s dd ? ;float
528
	c dd ? ;float
532
	c dd ? ;float
529
	v dd ? ;int
533
	v dd ? ;int
530
	w dd ? ;int
534
	w dd ? ;int
531
endl
535
endl
532
	mov eax,[u]
536
	mov eax,[u]
533
	inc eax
537
	inc eax
534
	cmp eax,2
538
	cmp eax,2
535
	jle @f
539
	jle @f
536
		xor eax,eax
540
		xor eax,eax
537
	@@:
541
	@@:
538
	mov [v],eax
542
	mov [v],eax
539
	inc eax
543
	inc eax
540
	cmp eax,2
544
	cmp eax,2
541
	jle @f
545
	jle @f
542
		xor eax,eax
546
		xor eax,eax
543
	@@:
547
	@@:
544
	mov [w],eax
548
	mov [w],eax
545
	fld dword [t]
549
	fld dword [t]
546
	fsin
550
	fsin
547
	fstp dword [s]
551
	fstp dword [s]
548
	fld dword [t]
552
	fld dword [t]
549
	fcos
553
	fcos
550
	fstp dword [c]
554
	fstp dword [c]
551
 
555
 
552
	stdcall gl_M4_Id,[a]
556
	stdcall gl_M4_Id,[a]
553
 
557
 
554
	M4_reg ecx,[a],[v],[v]
558
	M4_reg ecx,[a],[v],[v]
555
	mov eax,[c]
559
	mov eax,[c]
556
	mov [ecx],eax
560
	mov [ecx],eax
557
 
561
 
558
	M4_reg ecx,[a],[v],[w]
562
	M4_reg ecx,[a],[v],[w]
559
	fld dword [s]
563
	fld dword [s]
560
	fchs
564
	fchs
561
	fstp dword [ecx]
565
	fstp dword [ecx]
562
 
566
 
563
	M4_reg ecx,[a],[w],[v]
567
	M4_reg ecx,[a],[w],[v]
564
	mov eax,[s]
568
	mov eax,[s]
565
	mov [ecx],eax
569
	mov [ecx],eax
566
 
570
 
567
	M4_reg ecx,[a],[w],[w]
571
	M4_reg ecx,[a],[w],[w]
568
	mov eax,[c]
572
	mov eax,[c]
569
	mov [ecx],eax
573
	mov [ecx],eax
570
 
574
 
571
	ret
575
	ret
572
endp
576
endp
573
 
577
 
574
; inverse of a 3x3 matrix
578
; inverse of a 3x3 matrix
575
;void gl_M3_Inv(M3 *a,M3 *m)
579
;void gl_M3_Inv(M3 *a,M3 *m)
576
;{
580
;{
577
;        float det;
581
;        float det;
578
 
582
 
579
;        det = m->m[0][0]*m->m[1][1]*m->m[2][2]-m->m[0][0]*m->m[1][2]*m->m[2][1]-
583
;        det = m->m[0][0]*m->m[1][1]*m->m[2][2]-m->m[0][0]*m->m[1][2]*m->m[2][1]-
580
;                m->m[1][0]*m->m[0][1]*m->m[2][2]+m->m[1][0]*m->m[0][2]*m->m[2][1]+
584
;                m->m[1][0]*m->m[0][1]*m->m[2][2]+m->m[1][0]*m->m[0][2]*m->m[2][1]+
581
;                m->m[2][0]*m->m[0][1]*m->m[1][2]-m->m[2][0]*m->m[0][2]*m->m[1][1];
585
;                m->m[2][0]*m->m[0][1]*m->m[1][2]-m->m[2][0]*m->m[0][2]*m->m[1][1];
582
 
586
 
583
;        a->m[0][0] = (m->m[1][1]*m->m[2][2]-m->m[1][2]*m->m[2][1])/det;
587
;        a->m[0][0] = (m->m[1][1]*m->m[2][2]-m->m[1][2]*m->m[2][1])/det;
584
;        a->m[0][1] = -(m->m[0][1]*m->m[2][2]-m->m[0][2]*m->m[2][1])/det;
588
;        a->m[0][1] = -(m->m[0][1]*m->m[2][2]-m->m[0][2]*m->m[2][1])/det;
585
;        a->m[0][2] = -(-m->m[0][1]*m->m[1][2]+m->m[0][2]*m->m[1][1])/det;
589
;        a->m[0][2] = -(-m->m[0][1]*m->m[1][2]+m->m[0][2]*m->m[1][1])/det;
586
 
590
 
587
;        a->m[1][0] = -(m->m[1][0]*m->m[2][2]-m->m[1][2]*m->m[2][0])/det;
591
;        a->m[1][0] = -(m->m[1][0]*m->m[2][2]-m->m[1][2]*m->m[2][0])/det;
588
;        a->m[1][1] = (m->m[0][0]*m->m[2][2]-m->m[0][2]*m->m[2][0])/det;
592
;        a->m[1][1] = (m->m[0][0]*m->m[2][2]-m->m[0][2]*m->m[2][0])/det;
589
;        a->m[1][2] = -(m->m[0][0]*m->m[1][2]-m->m[0][2]*m->m[1][0])/det;
593
;        a->m[1][2] = -(m->m[0][0]*m->m[1][2]-m->m[0][2]*m->m[1][0])/det;
590
 
594
 
591
;        a->m[2][0] = (m->m[1][0]*m->m[2][1]-m->m[1][1]*m->m[2][0])/det;
595
;        a->m[2][0] = (m->m[1][0]*m->m[2][1]-m->m[1][1]*m->m[2][0])/det;
592
;        a->m[2][1] = -(m->m[0][0]*m->m[2][1]-m->m[0][1]*m->m[2][0])/det;
596
;        a->m[2][1] = -(m->m[0][0]*m->m[2][1]-m->m[0][1]*m->m[2][0])/det;
593
;        a->m[2][2] = (m->m[0][0]*m->m[1][1]-m->m[0][1]*m->m[1][0])/det;
597
;        a->m[2][2] = (m->m[0][0]*m->m[1][1]-m->m[0][1]*m->m[1][0])/det;
594
;}
598
;}
595
 
599
 
596
; vector arithmetic
600
; vector arithmetic
597
 
601
 
598
align 4
602
align 4
599
proc gl_V3_Norm uses ebx, a:dword
603
proc gl_V3_Norm uses ebx, a:dword
600
	mov ebx,[a]
604
	mov ebx,[a]
601
	fld dword[ebx]
605
	fld dword[ebx]
602
	fmul st0,st0
606
	fmul st0,st0
603
	fld dword[ebx+4]
607
	fld dword[ebx+offs_Y]
604
	fmul st0,st0
608
	fmul st0,st0
605
	faddp
609
	faddp
606
	fld dword[ebx+8]
610
	fld dword[ebx+offs_Z]
607
	fmul st0,st0
611
	fmul st0,st0
608
	faddp
612
	faddp
609
	fsqrt ;st0 = sqrt(a.X^2 +a.Y^2 +a.Z^2)
613
	fsqrt ;st0 = sqrt(a.X^2 +a.Y^2 +a.Z^2)
610
	ftst
614
	ftst
611
	fstsw ax
615
	fstsw ax
612
	sahf
616
	sahf
613
	je .r1 ;if (sqrt(...)==0) return 1
617
	je .r1 ;if (sqrt(...)==0) return 1
614
		fld dword[ebx] ;offs_X = 0
618
		fld dword[ebx] ;offs_X = 0
615
		fdiv st0,st1
619
		fdiv st0,st1
616
		fstp dword[ebx] ;a.X/=sqrt(...)
620
		fstp dword[ebx] ;a.X/=sqrt(...)
617
		fld dword[ebx+4]
621
		fld dword[ebx+offs_Y]
618
		fdiv st0,st1
622
		fdiv st0,st1
619
		fstp dword[ebx+4] ;a.Y/=sqrt(...)
623
		fstp dword[ebx+offs_Y] ;a.Y/=sqrt(...)
620
		fld dword[ebx+8]
624
		fld dword[ebx+offs_Z]
621
		fdiv st0,st1
625
		fdiv st0,st1
622
		fstp dword[ebx+8] ;a.Z/=sqrt(...)
626
		fstp dword[ebx+offs_Z] ;a.Z/=sqrt(...)
623
		xor eax,eax
627
		xor eax,eax
624
		jmp @f
628
		jmp @f
625
	.r1:
629
	.r1:
626
		xor eax,eax
630
		xor eax,eax
627
		inc eax
631
		inc eax
628
	@@:
632
	@@:
629
	ffree st0
633
	ffree st0
630
	fincstp
634
	fincstp
631
	ret
635
	ret
632
endp
636
endp
633
 
637
 
634
macro gl_V3_New p_mem, x, y, z
638
macro gl_V3_New p_mem, x, y, z
635
{
639
{
636
	mov dword[p_mem],x
640
	mov dword[p_mem],x
637
	mov dword[p_mem+4],y
641
	mov dword[p_mem+4],y
638
	mov dword[p_mem+8],z
642
	mov dword[p_mem+8],z
639
}
643
}
640
 
644
 
641
macro gl_V4_New p_mem, x, y, z, w
645
macro gl_V4_New p_mem, x, y, z, w
642
{
646
{
643
	mov dword[p_mem],x
647
	mov dword[p_mem],x
644
	mov dword[p_mem+4],y
648
	mov dword[p_mem+4],y
645
	mov dword[p_mem+8],z
649
	mov dword[p_mem+8],z
646
	mov dword[p_mem+12],w
650
	mov dword[p_mem+12],w
647
}
651
}
648
>
652
>
649
>
653
>
650
>
654
>
651
>
655
>
652
>
656
>
653
>
657
>
654
>
658
>
655
;>
659
;>