Code:
//==============================================================================
// 3D graph drawer
//==============================================================================
//Must divide by each other, or else it'll fuck up big time
define WIDTH,50;
define TRIS_IN_FRAME,10;
define HEIGHT,50;
mov #regHWClear,0;
mov EDX,HEIGHT; mul EDX,WIDTH; div EDX,TRIS_IN_FRAME;
cmp #Frame,EDX; jge @DrawExit; jmp Render.Start;
@DrawExit: dexit;
Render.Start:
cmp #Frame,0; jne Render.SkipInitialization;
dclrscr cBackgroundColor;
//Apply aspect ratio correction
mov #vPerspective.Y,1.6;
//Initialize perspective transform matrix
mperspective mProjectionMatrix,vPerspective;
Render.SkipInitialization:
dcvxpipe 3; //Set -1..1 coordinate system
dvxpipe 5; //Use matrix projection routine
call ApplyTransform;
dcolor cDefaultColor;
mloadproj mProjectionMatrix;
dsetlight 0,lSun;//vPerspective
denable 0; //Vertex buffer
denable 2; //Lighting
mov EDX,WIDTH;
div EDX,TRIS_IN_FRAME;
//set coordinates
mov EBX,#Frame; div EBX,EDX; fint EBX;
mov EAX,#Frame; mod EAX,EDX; fint EAX; mul EAX,TRIS_IN_FRAME;
//draw triangle stripe
mov EDX,TRIS_IN_FRAME;
@DrawLoop:
call DrawTriangle;
inc EAX;
loopd @DrawLoop;
dvxflush;
inc #Frame;
dexit;
//= Apply transformations ======================================================
ApplyTransform:
//Compute look-at matrix (for camera)
mlookat mViewMatrix,vLookAt;
//Rotate & translate target model
mrotate mRotateMatrix,vRotate;
mtranslate mTranslateMatrix,vTranslate;
mscale mScaleMatrix,vScale;
//Create Model matrix
mmov mModelMatrix,mRotateMatrix;
mmul mModelMatrix,mTranslateMatrix;
mmul mModelMatrix,mScaleMatrix;
//Compute and load ModelView matrix
mmov mModelViewMatrix,mViewMatrix;
mmul mModelViewMatrix,mModelMatrix;
mload mModelViewMatrix;
ret
//EAX: X, EBX: Y, Z -> ECX
GenerateHeight:
push eax; push ebx;
add EAX,#vTranslate.X;
add EBX,#vTranslate.Y;
mul EAX,EAX;
mul EBX,EBX;
add EAX,EBX; mul EAX,0.5;
fpwr EAX,0.5;
add EAX,0.1;
fsin ECX,EAX;
div ECX,EAX;
mul ECX,10;
// fsin EAX,EAX;
// fcos ECX,EBX;
// mul ECX,EAX;
//Z = Sin(R) / R
pop ebx; pop eax;
ret
DrawTriangle:
mov EBP,VertexData;
//Face 1
call GenerateHeight;
mov #EBP,EAX; inc EBP;
mov #EBP,EBX; inc EBP;
mov #EBP,ECX; inc EBP;
inc EAX;
call GenerateHeight;
mov #EBP,EAX; inc EBP;
mov #EBP,EBX; inc EBP;
mov #EBP,ECX; inc EBP;
inc EBX;
call GenerateHeight;
mov #EBP,EAX; inc EBP;
mov #EBP,EBX; inc EBP;
mov #EBP,ECX; inc EBP;
//Face 2
dec EBX; dec EAX;
call GenerateHeight;
mov #EBP,EAX; inc EBP;
mov #EBP,EBX; inc EBP;
mov #EBP,ECX; inc EBP;
inc EBX; inc EAX;
call GenerateHeight;
mov #EBP,EAX; inc EBP;
mov #EBP,EBX; inc EBP;
mov #EBP,ECX; inc EBP;
dec EAX;
call GenerateHeight;
mov #EBP,EAX; inc EBP;
mov #EBP,EBX; inc EBP;
mov #EBP,ECX; inc EBP;
dec EBX;
dvxdata_3f VertexData,2;
ret
//= Rendering settings and parameters ==========================================
matrix mRotateMatrix; matrix mTranslateMatrix; matrix mScaleMatrix;
matrix mProjectionMatrix; matrix mViewMatrix; matrix mModelMatrix;
matrix mPRotateMatrix; matrix mPTranslateMatrix; matrix mModelViewMatrix;
//------------------------------------------------------------------------------
vector4f vRotate, 0, 0, 1, 0; //<AXIS X Y Z> <ANGLE W>
vector4f vTranslate, -25, -25, 0, 0; //<TRANLSATION X Y Z> <0>
vector4f vScale, 1, 1, 1, 0; //<SCALE X Y Z> <0>
vector4f vPerspective, 30, 1.6, 1, 20; //<FOV> <ASPECT RATIO> <ZNEAR> <ZFAR>
vLookAt: vector3f vLookAt_Eye, 20, 20, 30; //Where our camera is
vector3f vLookAt_Center, 0, 0, 0; //What we look at
vector3f vLookAt_Up, 0, 0, -1; //Where our head points
color cBackgroundColor, 0, 0, 0;
color cDefaultColor, 255, 255, 255;
alloc Frame;
lSun: vector4f Sun.Pos, 100, 100, 100, 0; //x y z <unused, will be falloff>
color Sun.Col, 255, 255, 255, 0.5; //R G B Brightness
//==============================================================================
VertexData:
db 0,0,0; //Face1
db 0,0,0;
db 0,0,0;
db 0,0,0; //Face2
db 0,0,0;
db 0,0,0;
Bookmarks