Recent

Author Topic: Strached Model / Software Renderer  (Read 1177 times)

Key-Real

  • Full Member
  • ***
  • Posts: 231
Strached Model / Software Renderer
« on: March 05, 2022, 01:38:00 pm »
Hi,
I'm writing a software renderer.
I can not understand why my box is streched when I move it. What I do wrong?



Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2.  
  3. uses tools,vipgfx,math;
  4. const
  5.         v3dMaxVertex=8;
  6. type
  7.        
  8.     v3dVector = record
  9.           x,y,z,w:single;
  10.     end;
  11.  
  12.    
  13.         v3dPoly = record
  14.           v:array [0..v3dMaxVertex] of v3dVector;
  15.           numVertex:dword;
  16.           n:v3dVector;
  17.           matNum:dword;
  18.     end;
  19.  
  20.     v3dObject = record
  21.           name:string;
  22.          
  23.           position:v3dVector;
  24.           polys:array of v3dPoly;
  25.           translatedPolys:array of v3dPoly;
  26.           numPolys:dword;
  27.     end;
  28.  
  29. function aVector(x,y,z:single):v3dVector;
  30. var v:v3dVector;
  31. begin
  32.         v.x:=X;
  33.         v.Y:=Y;
  34.         v.z:=Z;
  35.         v.w:=1;
  36.         aVector:=v;
  37. end;
  38.  
  39. procedure v3dRasterizeWire(poly:v3dPoly; color:dword);
  40. var i,ii:longint;
  41.         x1,y1,x2,y2:longint;
  42. begin
  43.  
  44.         ii:=0;
  45.         for i:=0 to poly.numVertex-1 do begin
  46.                 inc(ii);
  47.                 if ii>=poly.numVertex then ii:=0;
  48.                 x1:=ceil(poly.v[i].x);
  49.                 y1:=ceil(poly.v[i].y);
  50.                 x2:=ceil(poly.v[ii].x);
  51.                 y2:=ceil(poly.v[ii].y);
  52.                 drawline(vscreen,x1,y1,x2,y2,color);
  53.         end;
  54.  
  55. end;
  56.  
  57. function v3dCreatePoly(v1,v2,v3:v3dVector):v3dPoly;
  58. var i:dword;
  59.         p:v3dPoly;
  60. begin
  61.  
  62.         p.numVertex:=3;
  63.  
  64.     p.v[0]:=v1;
  65.     p.v[1]:=v2;
  66.     p.v[2]:=v3;
  67.  
  68.         v3dCreatePoly:=p;
  69. end;
  70.  
  71. function v3dCreateObject(name:string; x,y,z:single):v3dObject;
  72. var o:v3dObject;
  73. begin
  74.         o.name:=name;
  75.         setLength(o.polys,0);
  76.         setLength(o.translatedPolys,0);
  77.         o.numPolys:=0;
  78.         o.position:=aVector(0,0,0);
  79.         v3dCreateObject:=o;
  80. end;
  81.  
  82. procedure v3dAddPolyToObject(var o:v3dObject; p:v3dPoly);
  83. begin
  84.         inc(o.numPolys);
  85.  
  86.         setLength(o.polys,o.numPolys);
  87.         setLength(o.translatedPolys,o.numPolys);
  88.         o.polys[o.numPolys-1]:=p;
  89. end;
  90.  
  91. const
  92.         width=1366;
  93.         height=512;
  94.         fullscreen=false;
  95. var
  96.         theObject:v3dObject;
  97.  
  98.  
  99.  
  100. operator + (a,b:v3dVector):v3dVector;
  101. begin
  102.         result.x:=a.x + b.x;
  103.         result.y:=a.y + b.y;
  104.         result.z:=a.z + b.z;
  105.         result.w:=1;
  106. end;
  107.  
  108.  
  109.  
  110.  
  111.  
  112. function v3dMakeProjection(v:v3dVector):v3dVector;
  113. var outV:v3dVector;
  114.     centerX,centerY:longint;
  115.     fNear,fFar,fFov,fAspectRatio,fFovRad:single;
  116. begin
  117.  
  118.  
  119.                 fNear := 0.01;
  120.                 fFar := 1000.0;
  121.                 fFov := 90.0;
  122.                 fAspectRatio := vScreen.height / vScreen.width;
  123.                 fFovRad := 1.0 / tan(fFov * 0.5 / 180.0 * 3.14159);
  124.  
  125.         outV:=v;
  126.  
  127.         centerX:=vscreen.width div 2;
  128.         centerY:=vscreen.height div 2;
  129.  
  130.        
  131.         outV.x:=fAspectRatio*fFovRad*outV.x / outV.z  * (vScreen.width /  2) + centerX;
  132.         outV.y:=-fFovRad * outV.y / outV.z  * (vScreen.height / 2) + centerY;
  133.         // outV.z:=( (fFar/(fFar-fNear)) - ((fFar*fNear) / (fFar-fNear)) ) * outV.z;
  134.  
  135.         v3dMakeProjection:=outV;
  136. end;
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143. procedure v3dRenderPoly(poly:v3dPoly);
  144. var
  145.         i:dword;
  146.         p:v3dPoly;
  147.        
  148. begin
  149.         for i:=0 to poly.numVertex-1 do p.v[i]:=v3dMakeProjection(poly.v[i]);
  150.         p.numVertex:=poly.numVertex;
  151.  
  152.         v3dRasterizeWire(p,$ffffffff);
  153. end;
  154.  
  155.  
  156. procedure v3dRenderObject(var o:v3dObject);
  157. var i,ii:dword;
  158. begin
  159.  
  160.         for i:=0 to o.numPolys-1 do begin
  161.  
  162.                         o.translatedPolys[i].numVertex:=o.polys[i].numVertex;
  163.                         for ii:=0 to o.polys[i].numVertex-1 do o.translatedPolys[i].v[ii]:=o.Polys[i].v[ii] +  o.position;
  164.                        
  165.  
  166.                         v3dRenderPoly(o.translatedPolys[i]);
  167.  
  168.         end;
  169.  
  170. end;
  171.  
  172. const
  173.  
  174.         speed=0.1;
  175.  
  176.  
  177. var
  178.         rx:single=0;
  179.         ry:single=0;
  180.         rz:single=0;
  181.         r:single=0;
  182.         mx:single=0;
  183.         my:single=0;
  184.         mz:single=10;
  185.  
  186.  
  187.  
  188. procedure move;
  189. begin
  190.         if KEYBOARD[KEY_T] then mz:=mz-speed;
  191.         if KEYBOARD[KEY_G] then mz:=mz+speed;
  192.         if KEYBOARD[KEY_UP] then my:=my-speed;
  193.         if KEYBOARD[KEY_DOWN] then my:=my+speed;
  194.         if KEYBOARD[KEY_LEFT] then mx:=mx-speed;
  195.         if KEYBOARD[KEY_RIGHT] then mx:=mx+speed;
  196.          
  197.         theObject.position:=aVector(mx,my,mz);
  198. end;
  199.  
  200. function cube:v3dObject;
  201. var
  202.     v1,v2,v3:v3dVector;
  203.     p:v3dPoly;
  204.     o:v3dObject;
  205. begin
  206.     o:=v3dCreateObject('box',0,0,0);
  207.  
  208.     v1:=aVector(-1.0,-1.0,-1.0);
  209.     v2:=aVector(-1.0,-1.0, 1.0);
  210.     v3:=aVector(-1.0, 1.0, 1.0);
  211.     p:=v3dCreatePoly(v1,v2,v3);
  212.     v3dAddPolyToObject(o,p);
  213.  
  214.     v1:=aVector(1.0, 1.0,-1.0);
  215.     v2:=aVector(-1.0,-1.0,-1.0);
  216.     v3:=aVector(-1.0, 1.0,-1.0);
  217.     p:=v3dCreatePoly(v1,v2,v3);
  218.     v3dAddPolyToObject(o,p);
  219.    
  220.     v1:=aVector(1.0,-1.0, 1.0);
  221.     v2:=aVector(-1.0,-1.0,-1.0);
  222.     v3:=aVector(1.0,-1.0,-1.0);
  223.     p:=v3dCreatePoly(v1,v2,v3);
  224.     v3dAddPolyToObject(o,p);
  225.    
  226.     v1:=aVector(1.0, 1.0,-1.0);
  227.     v2:=aVector(1.0,-1.0,-1.0);
  228.     v3:=aVector(-1.0,-1.0,-1.0);
  229.     p:=v3dCreatePoly(v1,v2,v3);
  230.     v3dAddPolyToObject(o,p);
  231.    
  232.     v1:=aVector(-1.0,-1.0,-1.0);
  233.     v2:=aVector(-1.0, 1.0, 1.0);
  234.     v3:=aVector(-1.0, 1.0,-1.0);
  235.     p:=v3dCreatePoly(v1,v2,v3);
  236.     v3dAddPolyToObject(o,p);
  237.    
  238.     v1:=aVector(1.0,-1.0, 1.0);
  239.     v2:=aVector(-1.0,-1.0, 1.0);
  240.     v3:=aVector(-1.0,-1.0,-1.0);
  241.     p:=v3dCreatePoly(v1,v2,v3);
  242.     v3dAddPolyToObject(o,p);
  243.    
  244.     v1:=aVector(-1.0, 1.0, 1.0);
  245.     v2:=aVector(-1.0,-1.0, 1.0);
  246.     v3:=aVector(1.0,-1.0, 1.0);
  247.     p:=v3dCreatePoly(v1,v2,v3);
  248.     v3dAddPolyToObject(o,p);
  249.    
  250.     v1:=aVector(1.0, 1.0, 1.0);
  251.     v2:=aVector(1.0,-1.0,-1.0);
  252.     v3:=aVector(1.0, 1.0,-1.0);
  253.     p:=v3dCreatePoly(v1,v2,v3);
  254.     v3dAddPolyToObject(o,p);
  255.    
  256.     v1:=aVector(1.0,-1.0,-1.0);
  257.     v2:=aVector(1.0, 1.0, 1.0);
  258.     v3:=aVector(1.0,-1.0, 1.0);
  259.     p:=v3dCreatePoly(v1,v2,v3);
  260.     v3dAddPolyToObject(o,p);
  261.    
  262.     v1:=aVector(1.0, 1.0, 1.0);
  263.     v2:=aVector(1.0, 1.0,-1.0);
  264.     v3:=aVector(-1.0, 1.0,-1.0);
  265.     p:=v3dCreatePoly(v1,v2,v3);
  266.     v3dAddPolyToObject(o,p);
  267.    
  268.     v1:=aVector(1.0, 1.0, 1.0);
  269.     v2:=aVector(-1.0, 1.0,-1.0);
  270.     v3:=aVector(-1.0, 1.0, 1.0);
  271.     p:=v3dCreatePoly(v1,v2,v3);
  272.     v3dAddPolyToObject(o,p);
  273.    
  274.     v1:=aVector(1.0, 1.0, 1.0);
  275.     v2:=aVector(-1.0, 1.0, 1.0);
  276.     v3:=aVector(1.0,-1.0, 1.0);
  277.     p:=v3dCreatePoly(v1,v2,v3);
  278.     v3dAddPolyToObject(o,p);
  279.  
  280.     cube:=o;
  281. end;
  282.  
  283. begin
  284.        
  285.         theObject:=cube;
  286.  
  287.         initGFXsystem(width,height,fullscreen);
  288.        
  289.  
  290.         repeat
  291.                 fastfill(vscreen.data,vscreen.width*vscreen.height,0);
  292.  
  293.                
  294.  
  295.                 move;
  296.  
  297.  
  298.                
  299.                 v3dRenderObject(theObject);
  300.  
  301.  
  302.  
  303.                 updateGFXsystem;
  304.         until gfxDone or keyboard[KEY_ESCAPE];
  305.  
  306.  
  307.         finishGFXsystem;
  308.  
  309.  
  310.         ReturnFPSstring;
  311. end.
  312.  
Just press the arrow key



to Compile you need the GFX Framework "vipgfx4"
https://sourceforge.net/projects/vipgfx/files
« Last Edit: March 05, 2022, 07:52:54 pm by Key-Real »

 

TinyPortal © 2005-2018