programmers resources
  http://www.intel-assembler.it/  (c)2017 intel-assembler.it   info@intel-assembler.it
 
Search :  
Lingua Italiana    English Language   
Index
 
just an empty assembly space
just an arrow Intel Platform
just an arrow Article & Guides
just an arrow Download Software


23/01/2009 Featured Article: How to remove Buzus Virus (permalink)




:::3061910:::
Bottone Scambio Directory Pubblicitaonline.it
Home Page | Articles & Guides | Download | Intel Platform | Contacts

Google
 


Bookmark and Share
Download 
Tell a friend



Math.inc: General 3D math functions

A library with many math functions for 3d graphics

(by john mccarthy)

A collection of asm functions that perform various operations.


This article is online from 2873 days and has been seen 4420 times


;같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같
;
; Filename : Math.inc
; Description : General 3D math functions.
;
; Written by: John McCarthy
; 1316 Redwood Lane
; Pickering, Ontario.
; Canada, Earth, Milky Way (for those out-of-towners)
; L1X 1C5
;
; Internet/Usenet: BRIAN.MCCARTHY@CANREM.COM
; Fidonet: Brian McCarthy 1:229/15
; RIME/Relaynet: ->CRS
;
; Send me your protected mode source code!
; Send me your Objects!
; But most of all, Send me a postcard!!!!
;
; 02/01/1995
;-----
a note from jes: this is the summary of the exposed functions.

_rotate_point 32 bit _rotate_point point using _vmatrix
_user3d scale 3d point into 2d point
_user3dy scale 3d point into 2d point on x axis only
_user3dx scale 3d point into 2d point on y axis only
_rotate_by_camera 32 bit _rotate_point point using _ematrix
_zsolve 32 bit rotate point using _ematrix - solve one variable
_ysolve 32 bit rotate point using _ematrix - solve one variable
_sine 16 bit theta to 32bit sin(@)
_cosine 16 bit theta to 32bit cos(@)
Arctan 32 bit rise/run to 16bit arctan(rise/run)
_temp_matrix generate temp matrix, 12 imul's, from object esi
_temp_rotate_point 32 bit _rotate_point point using _tmatrix
_matrix_multiply multiply _tmatrix by _vmatrix, [_vmatrix]=[_tmatrix][_vmatrix]
_sqrt Routine courtesy TRAN
_lambert generate _lambert shading 1x3 matrix, made in 6 imuls
_pre_cal_lambert Pre-calculate all neccessary stuff for object DI
_calc_normal calculate surface normal
_calc_d Calculate D portion of equation of a plane
_set_up_all_lambert set up all _lambert normals from object si to object di
_l_rotate_point
_rotate_point surface normal through _lambert matrix
Generate null Y matrix (for camera)
Inverse Rotate by Camera
Rotate point along X axis
Rotate point along Y axis
Rotate point along Z axis
_Point_Z: Calculate Z roll direction to line object up with point

;같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_rotate_point - 32 bit _rotate_point point using _vmatrix
In:
EBX - x point
ECX - y point
EBP - z point
_vmatrix - 32 bit rotation matrix - set up by "_compound" routine
Out:
EBX - x point
ECX - y point
EBP - z point

Notes:

All rotations (_rotate_by_camera,_rotate_point,_temp_rotate_point) are 32 bit.
_f_rotate_point uses rotation along a plane and uses _ematrix with _precal147

point rotation
ebx = x ecx = y ebp = z 32 bit rotation!
clobbers edx,esi,eax

remember , matrix offsets are:

0 1 2 multiply those by 4 for the word address of the matrix
3 4 5
6 7 8

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_user3d - scale 3d point into 2d point
In:
EBX - x point
ECX - y point
EBP - z point
Out:
EBX - x point
ECX - y point
EBP - z point

Notes:

fast ratios found in macros.inc since multiplication has been substituted with
fast lea

trashes eax,edx,edi

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_user3dy - scale 3d point into 2d point on x axis only
In:
ECX - y point
EBP - z point
Out:
ECX - y point
EBP - z point

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_user3dx - scale 3d point into 2d point on y axis only
In:
EDI - x point
ESI - z point
Out:
EDI - x point
ESI - z point

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

Checkfront: checks if a side is visible. (counter-clockwise)

In:
(EDI,EBP) - xy of point 1
(ESI,ECX) - xy of point 2
(EDX,EBX) - xy of point 3
Out:
ECX < 0 if side counter-clockwise

Notes: routine courtesy of "RAZOR"
eg:
call checkfront
cmp ecx,0
jng dontdraw

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_rotate_by_camera - 32 bit _rotate_point point using _ematrix
In:
EBX - x point
ECX - y point
EBP - z point
_ematrix - 32 bit rotation matrix - set up by "_setsincose" routine
Out:
EBX - x point
ECX - y point
EBP - z point

Notes:

point rotation for eye - solves all x,y,z parameters
camera rotation is 32 bit and uses _ematrix

remember , matrix offsets are:

0 1 2 multiply those by 4 for the doubleword address of the matrix
3 4 5
6 7 8

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_zsolve - 32 bit rotate point using _ematrix - solve one variable only
In:
EBX - x point
ECX - y point
EBP - z point
_ematrix - 32 bit rotation matrix - set up by "_setsincose" routine

Out:
EBX - x point (same as entry)
ECX - y point (same as entry)
EBP - z point (same as entry)
ESI - new z point/location

Notes:

solve z from _ematrix - same as above _rotate_by_camera but only solves z
for fast test of where object is - result is in esi

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_xsolve - 32 bit rotate point using _ematrix - solve one variable only
In:
EBX - x point
ECX - y point
EBP - z point
_ematrix - 32 bit rotation matrix - set up by "_setsincose" routine

Out:
EBX - x point (same as entry)
ECX - y point (same as entry)
EBP - z point (same as entry)
EDI - new x point/location

Notes:
If object z test from above routine is positive, this routine will solve
the rest of the rotation matrix. this is so we don't waste time solving
for x and y locations if the object is behind the camera anyway.
Saves imuls.

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_ysolve - 32 bit rotate point using _ematrix - solve one variable only
In:
EBX - x point
ECX - y point
EBP - z point
ESI - new z point
EDI - new x point
_ematrix - 32 bit rotation matrix - set up by "_setsincose" routine

Out:
EBX - x new point from EDI
ECX - y new point
EBP - z new point from ESI

Notes:

Solve y from _ematrix - same as above _xsolve but solves y for fast
test of where object is. Final variables are then cleaned up to
immitate the _rotate_by_camera function in parts.

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_sine - 16 bit theta to 32bit sin(@)
In:
AX - theta 0 - 65536 (0-360)
Out:
EAX - sin (@) (-4000h to 4000h)

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_cosine - 16 bit theta to 32bit cos(@)
In:
AX - theta 0 - 65536 (0-360)
Out:
EAX - cos (@) (-4000h to 4000h)

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

Notes:
calculate sin into eax, from ax, smashes bx
after imul by sin, shr eax,14 to compensate for decimal factor!
eg:
mov eax,sin(@)
mov ebx,32bitnumber
imul ebx
shrd eax,edx,14
eax = ebx*sin(@)

mov ax,sin(@)
mov bx,16bitnumber
imul bx
shrd ax,dx,14
eax = bx*sin(@)

eax is only a sign extended ax and will contain either ffffxxxx or 0000xxxx

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

Arctan - 32 bit rise/run to 16bit arctan(rise/run)
In:
EAX - Run
ECX - Rise
Out:
AX - arctan(ECX/EAX)

Notes:
smashes cx,ax,dx,si
arctan(ecx/0) is valid and tested for

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_compound - generate object matrix, 12 imul's first
In:
ESI - Object # to get angles from
v_vxs[esi*2] - object x angle (0-65536)
v_vys[esi*2] - object y angle (0-65536)
v_vzs[esi*2] - object z angle (0-65536)
Out:
_vmatrix - resulting rotation matrix including camera matrix
ESI = ESI

Notes:
x y z

x= cz * cy - sx * sy * sz - sz * cy - sx * sy * cz - cx * sy

y= sz * cx cx * cz - sx

z= cz * sy + sx * sz * cy - sy * sz + sx * cy * cz cx * cy

then perform matrix multiply by negative x and z matricies

-x matrix -z matrix
x y z x y z

x 1 0 0 cz sz 0

y 0 cx sx -sz cz 0

z 0 -sx cx 0 0 1

Notice original object matrix takes 12 imuls, camera modify takes 24, can
you do this faster? (less imuls)

A call to _setsincose MUST have taken place for this routine to calculate
the camera matrix correctly.

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_setsincose - generate rotation matrix for y,x,z camera rotation

In:
eyeax - camera x angle (0-65536)
eyeay - camera y angle (0-65536)
eyeaz - camera z angle (0-65536)
Out:
_vmatrix - resulting rotation matrix including camera matrix

Notes:
called only once every frame. completed in 12 multiplys
matrix is also used for objects with no rotation (always angle 0,0,0)

where is my postcard! see readme.doc for info.

x y z

x= cz * cy + sx * sy * sz -cx * sz - sy * cz + sx * cy * sz

y= sz * cy - sx * sy * cz cx * cz - sy * sz - sx * cy * cz

z= cx * sy sx cx * cy


matrix offsets: (doublewords)

x y z

x 0 4 8
y 12 16 20
z 24 28 32

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_temp_matrix: generate temp matrix, 12 imul's, from object esi

In:
ESI - Object # to get angles from
v_vxs[esi*2] - object x angle (0-65536)
v_vys[esi*2] - object y angle (0-65536)
v_vzs[esi*2] - object z angle (0-65536)
Out:
_tmatrix - resulting rotation matrix (excluding camera matrix)
ESI = ESI

Notes:
x y z

x= cz * cy - sx * sy * sz - sz * cy - sx * sy * cz - cx * sy

y= sz * cx cx * cz - sx

z= cz * sy + sx * sz * cy - sy * sz + sx * cy * cz cx * cy

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_temp_rotate_point - 32 bit _rotate_point point using _tmatrix
In:
EBX - x point
ECX - y point
EBP - z point
_tmatrix - 32 bit rotation matrix - set up by "_temp_matrix" routine
Out:
EBX - x point
ECX - y point
EBP - z point

Notes:
Same as _rotate_point and _rotate_by_camera
같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_matrix_multiply: multiply _tmatrix by _vmatrix, [_vmatrix]=[_tmatrix][_vmatrix]

In:
_vmatrix - rotation matrix
_tmatrix - rotation matrix
Out:
_vmatrix - resulting rotation matrix

Notes:

[ _tmatrix+ 0 _tmatrix+ 2 _tmatrix+ 4 ] [ _vmatrix+ 0 _vmatrix+ 2 _vmatrix+ 4 ]
[ ] [ ]
[ _tmatrix+ 6 _tmatrix+ 8 _tmatrix+10 ] [ _vmatrix+ 6 _vmatrix+ 8 _vmatrix+10 ]
[ ] [ ]
[ _tmatrix+12 _tmatrix+14 _tmatrix+16 ] [ _vmatrix+12 _vmatrix+14 _vmatrix+16 ]

Think of it this way, this routine will generate a resulting matrix as if
you _rotate_pointd an object by _tmatrix, then _rotate_pointd the object
by _vmatrix. Instead, call this routine then you will only have to
_rotate_point the object by _vmatrix!.

Notice _tmatrix is done before _vmatrix!! This is used for calculating the
positions of arms on bodies, hands on arms, fingers on hands...etc...
같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_sqrt: Routine courtesy TRAN

In:
EAX - number to take root of
Out:
EAX - root

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_lambert: generate _lambert shading 1x3 matrix, completed in 6 imuls

In:
ESI - Object # to get angles from
v_vxs[esi*2] - object x angle (0-65536)
v_vys[esi*2] - object y angle (0-65536)
v_vzs[esi*2] - object z angle (0-65536)
_y_angle_of_sun - (0-65536)

Out:
_lmatrix - shading matrix
ESI - ?

Notes:

z= ( sz ( cx + ( sx * cy )) + cz * sy ) * 45degrees [x]
( cz ( cx + ( sx * cy )) - sz * sy ) * 45degrees [y]
( cx * cy - sx ) * 45 degrees [z]

note cos45=sin45=2d41h, but we will use 2d00h (99.2% accurate)
you can change the y angle of the sun/light but not the x angle.
changing the x angle would require a new formula.

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_pre_cal_lambert: Pre-calculate all neccessary stuff for object DI

In:
EDI - Object # to pre-calculate normals for
_objbase[esi*4] -> offset of object data
Out:
ESI -> minimum address of object data
EDI -> maximum address of object data
EBP -> points to header for object

Notes:

Precalculate surface normals for object di. This is so you don't
have to type them in when de_sineing new objects. Imagine, 400 points,
with 350 surfaces, calculating them all manually? This routine also
figures out the iteration skip offset (if you have surfaces dependant
on other surfaces) and also sets bit 1 if it is a line (two points),
and sets bit 4 if 1 point.This routine also sets the number of points
to skip if an iteration is found. It counts the number of points
within iterations (even iterations within iterations) and sets the
skip value so any iterations skipped will have a pre-calculated point
offset. Did that make sense?

Things done here:

set point command if only 1 connection
set line command if only 2 connections
set normal bit in commands if shading used in texture
calculate and set shading normals
calculate offsets for iteration jumps (in case surface not visible)
calculate n
er of points to skip for iterations (in case surface not
visible)
set offset flag if iteration uses a point offset (4'th future use word)
calculate and set auto-intensity of color if auto_s bit set

Most of the above is done so the user wont have to calculate this stuff
yourself - makes object modification much easier.

If you find the routine to be sloppy remember it is only used
for object initialization.

This routine will probably crash if your object is not set up correctly
The entire 3dvect source will crash if this routine isn't run and the
chances of you knowing how to do all that this routine does manually
are pretty slim since most of you out there are doughheads.

The minimum and maximum addresses are returned in ESI and EDI so you can
output the resulting object to a binary file. EBP points to the starting
header of the object (usually, but not neccessaraly, the minimum address)

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_calc_normal: calculate surface normal

In:
_lx1 - x of point 1 on triangle
_ly1 - y of point 1 on triangle
_lz1 - z of point 1 on triangle
_lx2 - x of point 2 on triangle
_ly2 - y of point 2 on triangle
_lz2 - z of point 2 on triangle
_lx3 - x of point 3 on triangle
_ly3 - y of point 3 on triangle
_lz3 - z of point 3 on triangle

Out:
EBX = finx = x of surface normal of triangle
ECX = finy = y of surface normal of triangle
EBP = finz = z of surface normal of triangle

Notes:
_x2 = _x2 - _x1
_y2 = _y2 - _y1
z2 = z2 - z1

x3 = x3 - _x1
y3 = y3 - _y1
z3 = z3 - z1

x = _y2 * z3 - z2 * y3
y = z2 * x3 - _x2 * z3
z = _x2 * y3 - _y2 * x3

a = SQR(x ^ 2 + y ^ 2 + z ^ 2)

x = INT(x / a * 256 + .5)
y = INT(y / a * 256 + .5)
z = INT(z / a * 256 + .5)

This worked for me on the first try!

If you wanted to get the equation of a plane, you could do this after:
d = - x * _x1 - y * _y1 - z * z1

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같
_calc_d: Calculate D portion of equation of a plane
In:
EBX = x of surface normal of triangle
ECX = y of surface normal of triangle
EBP = z of surface normal of triangle
_lx1 - x of point on triangle (any point)
_ly1 - y of point on triangle
_lz1 - z of point on triangle
Out:
EAX = D (Ax+By+Cz=D)
같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_set_up_all_lambert: set up all _lambert normals from object si to object di

In:
ESI - object # to start at
EDI - object # to end at
_objbase[esi*4 - edi*4] -> offsets to object data
Out:
null

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

_l_rotate_point: _rotate_point surface normal through _lambert matrix

In:
BX - x of surface normal
CX - y of surface normal
BP - z of surfac, normal
_lmatrix - 16 bit, 1x3 _lambert shading matrix, set up by "_lambert"
routine
Out:
BX - x of surface normal (untouched)
CX - y of surface normal (untouched)
BP - z of surface normal (untouched)
EDI - colour intensity for surface (-255 to +255)

Notes:
Your mother is a hamster.

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

Shading tables used for fake cosin colour intensity - 256 bytes
Default is 16 colours per _lambert calculation. But you could have 32, 48
or whatever you want, even an odd number like 53. Use the SHADING.BAS
program to make the table to your custom size.

Shading_bits is the variable for use with the texture command "LAST". This
variable tells the routine what bits to pluck off when determining the
shading intensity. Obviosly this cant be used if your palette shading
length is not a function of 2's complement.

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같

Generate null Y matrix (for camera)

In: ECX => matrix for storage
Out:
ECX => 9 doubleword resulting rotation matrix (excluding camera y angle)

Notes:
x y z

x= cz -cx * sz + sx * sz

y= sz cx * cz - sx * cz

z= 0 sx cx


Matrix offsets: (doublewords)

x y z

x 0 4 8
y 12 16 20
z 24 28 32

Notes: This routine generates the camera matrix without the Y component.

A call to _setsincose MUST have taken place for this routine to work
correctly!

같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같
Inverse Rotate by Camera
In:
EBX = x
ECX = y
EBP = z
ESI => indexer to angles (object number)
Out:
EBX = x
ECX = y
EBP = z
같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같
Rotate point along X axis
In:
ECX = Y
EBP = Z
AX = angle to rotate by
같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같
Rotate point along Y axis
In:
EBX = X
EBP = Z
AX = angle to rotate by
같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같
Rotate point along Z axis
In:
EBX = X
ECX = Y
AX = angle to rotate by
같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같
_Point_Z: Calculate Z roll direction to line object up with point
In:
EBX = x point
ECX = y point
EBP = z point
AX = current Y angle of object
DI = current X angle of object
Out:
AX = Z angle
Notes:
u = x*cos(Y) - z*sin(Y)
v = x*sin(Y) + z*cos(Y)
y = y*cos(X) - v*sin(X)
Z = arctan (u/y) = AX
같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같같


Top
Download 
Tell a friend
Bookmark and Share



Similar Articles

2D/3D Rotation asm demos
Source code for 2D and 3D rotation
(by Ash [NLB/BD])

3D Rotating Cube Assembler source
An improved asm code for 3d graphics
(by Arno W. Brouwer)

3D Texture Mapping theory and asm code
a mid-length text about texture logic and coding
(by Sean Barrett)

4K 3D Asm Demo
4096 byte 3D assembler graphical demo
(by Jari Kytjoki)

ALIB Version 3.0
Asm Library for DB, Memory, Math and others
(by Jeff Owens)

Basic Introduction to 3D Programming
A short doc about howto plot in 3D
(by Synergist)

FREELIB 3.0
The FREE Assembly Language Library
(by Tenie Remmel)

Perspective Transforms
A two page document with the perspective formulas
(by Andre Yew)

REFRACTION 4k intro
Assembler graphic demo, 3D chessboard
(by G.O.D.)

Star Field
3D Assembly star field
(by VLA)

Three Dimensional Rotations For Computer Graphics
A short document on 3D rotation
(by VLA)

Three Dimensional Shading In Computer Graphics
A brief intro on 3d shading theory
(by VLA)

VGA Trainer Program
A course for graphic programming in Pascal/ASM
(by Grant Smith Denthor of ASPHYXIA)

VGOPHONG Phong lighting and specular highlights
Theory, practice of phong lightnin and shade model
(by TimJ/Vertigo)

X-Sharp 3-D animation package v22
3D ball demo with zoom, share and move
(by Michael Abrash)

Zed3D programming guide
A compact reference for 3d computer graphics
(by Sbastien Loisel)

 Tags: 3d, asm library


webmaster jes
writers rguru, tech-g, aiguru, drAx

site optimized for IE/Firefox/Chrome with 1024x768 resolution

Valid HTML 4.01 Transitional


ALL TRADEMARKS ® ARE PROPERTY OF LEGITTIMATE OWNERS.
© ALL RIGHTS RESERVED.

hosting&web - www.accademia3.it

grossocactus
find rguru on
http://www.twitter.com/sicurezza3/
... send an email ...
Your name

Destination email

Message

captcha! Code