Recent

Author Topic: 3D error  (Read 3476 times)

Key-Real

  • Full Member
  • ***
  • Posts: 202
3D error
« on: June 05, 2021, 08:42:47 pm »
Hi,

if I draw a cube it is desplayed wrong(squeezed) after I move it.

Why?




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


If You need the graphic framework:

https://sourceforge.net/p/vipgfx/code/ci/master/tree/




Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: 3D error
« Reply #1 on: June 06, 2021, 01:16:36 pm »
I didn't check the framework nor test your code, but apparently you're trying to render the cube in perspective as opposed to orthogonal projection?

Key-Real

  • Full Member
  • ***
  • Posts: 202
Re: 3D error
« Reply #2 on: June 06, 2021, 04:01:16 pm »
I'm trying perspective by
 x' = x / z
 y' = y / z
 

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: 3D error
« Reply #3 on: June 11, 2021, 04:37:39 am »
I'm trying perspective by
 x' = x / z
 y' = y / z
 
No idea what this equation means, but try checking the contents of p.v after this code:
Code: Pascal  [Select][+][-]
  1. for i:=0 to poly.numVertex-1 do p.v[i]:=v3dMakeProjection(poly.v[i]);
  2.  
I highly doubt contains the correct coordinates.

 

TinyPortal © 2005-2018