3D Warping
Author |
Message |
Raknarg
|
Posted: Fri Apr 13, 2012 11:57 am Post subject: 3D Warping |
|
|
Alright so this is my first success with getting 3 dimensions and perspective, but I ran into an issue. The object looks fine when it's in the middle of the screen, but when you move it to the edges it warps. Anyone know how I can solve that?
Turing: |
setscreen ("offscreenonly, graphics:500;500")
type square :
record
x, y, z : array 1 .. 4 of real
dx, dy : array 1 .. 4 of int
end record
type cube :
record
face : array 1 .. 6 of square
end record
var poly : cube
var key : array char of boolean
%cube
%front
poly.face (1).x (1) := (maxx div 3)
poly.face (1).x (2) := (maxx div 3)
poly.face (1).x (3) := (maxx div 3) * 2
poly.face (1).x (4) := (maxx div 3) * 2
poly.face (1).y (1) := (maxx div 3)
poly.face (1).y (2) := (maxx div 3) * 2
poly.face (1).y (3) := (maxx div 3) * 2
poly.face (1).y (4) := (maxx div 3)
poly.face (1).z (1) := (maxx div 3)
poly.face (1).z (2) := (maxx div 3)
poly.face (1).z (3) := (maxx div 3)
poly.face (1).z (4) := (maxx div 3)
%right
poly.face (2).x (1) := (maxx div 3) * 2
poly.face (2).x (2) := (maxx div 3) * 2
poly.face (2).x (3) := (maxx div 3) * 2
poly.face (2).x (4) := (maxx div 3) * 2
poly.face (2).y (1) := (maxx div 3)
poly.face (2).y (2) := (maxx div 3) * 2
poly.face (2).y (3) := (maxx div 3) * 2
poly.face (2).y (4) := (maxx div 3)
poly.face (2).z (1) := (maxx div 3)
poly.face (2).z (2) := (maxx div 3)
poly.face (2).z (3) := (maxx div 3) * 2
poly.face (2).z (4) := (maxx div 3) * 2
%down
poly.face (3).x (1) := (maxx div 3)
poly.face (3).x (2) := (maxx div 3)
poly.face (3).x (3) := (maxx div 3) * 2
poly.face (3).x (4) := (maxx div 3) * 2
poly.face (3).y (1) := (maxx div 3)
poly.face (3).y (2) := (maxx div 3)
poly.face (3).y (3) := (maxx div 3)
poly.face (3).y (4) := (maxx div 3)
poly.face (3).z (1) := (maxx div 3) * 2
poly.face (3).z (2) := (maxx div 3)
poly.face (3).z (3) := (maxx div 3)
poly.face (3).z (4) := (maxx div 3) * 2
%left
poly.face (4).x (1) := (maxx div 3)
poly.face (4).x (2) := (maxx div 3)
poly.face (4).x (3) := (maxx div 3)
poly.face (4).x (4) := (maxx div 3)
poly.face (4).y (1) := (maxx div 3)
poly.face (4).y (2) := (maxx div 3) * 2
poly.face (4).y (3) := (maxx div 3) * 2
poly.face (4).y (4) := (maxx div 3)
poly.face (4).z (1) := (maxx div 3) * 2
poly.face (4).z (2) := (maxx div 3) * 2
poly.face (4).z (3) := (maxx div 3)
poly.face (4).z (4) := (maxx div 3)
%top
poly.face (5).x (1) := (maxx div 3)
poly.face (5).x (2) := (maxx div 3)
poly.face (5).x (3) := (maxx div 3) * 2
poly.face (5).x (4) := (maxx div 3) * 2
poly.face (5).y (1) := (maxx div 3) * 2
poly.face (5).y (2) := (maxx div 3) * 2
poly.face (5).y (3) := (maxx div 3) * 2
poly.face (5).y (4) := (maxx div 3) * 2
poly.face (5).z (1) := (maxx div 3)
poly.face (5).z (2) := (maxx div 3) * 2
poly.face (5).z (3) := (maxx div 3) * 2
poly.face (5).z (4) := (maxx div 3)
%back
poly.face (6).x (1) := (maxx div 3)
poly.face (6).x (2) := (maxx div 3)
poly.face (6).x (3) := (maxx div 3) * 2
poly.face (6).x (4) := (maxx div 3) * 2
poly.face (6).y (1) := (maxx div 3)
poly.face (6).y (2) := (maxx div 3) * 2
poly.face (6).y (3) := (maxx div 3) * 2
poly.face (6).y (4) := (maxx div 3)
poly.face (6).z (1) := (maxx div 3) * 2
poly.face (6).z (2) := (maxx div 3) * 2
poly.face (6).z (3) := (maxx div 3) * 2
poly.face (6).z (4) := (maxx div 3) * 2
proc rotate_2D (var px, py : real, rx, ry, angle : real)
var oldx := px - rx
var oldy := py - ry
px := oldx * cosd (angle ) - oldy * sind (angle ) + rx
py := oldx * sind (angle ) + oldy * cosd (angle ) + ry
end rotate_2D
proc projection_3D (x, y, z : real, var dx, dy : int)
var midx := maxx div 2
var midy := maxy div 2
dx := round ((x - midx ) * (200 / (z + 200)) + midx )
dy := round ((y - midy ) * (200 / (z + 200)) + midy )
end projection_3D
proc rotate_3D (var x, y, z : real, rx, ry, rz : real)
var oldx := x
var oldy := y
var oldz := z
if key (KEY_UP_ARROW) then
rotate_2D (z, y, rz, ry, - 1)
elsif key (KEY_DOWN_ARROW) then
rotate_2D (z, y, rz, ry, 1)
end if
if key (KEY_RIGHT_ARROW) then
rotate_2D (z, x, rz, rx, - 1)
elsif key (KEY_LEFT_ARROW) then
rotate_2D (z, x, rz, rx, 1)
end if
if key ('a') then
rotate_2D (x, y, rx, ry, 1)
elsif key ('d') then
rotate_2D (x, y, rx, ry, - 1)
end if
end rotate_3D
proc translate_3D (var x, y, z : real)
if key ('w') then
z + = 1
elsif key ('s') then
z - = 1
end if
if key ('4') then
x - = 1
elsif key ('6') then
x + = 1
end if
if key ('8') then
y + = 1
elsif key ('2') then
y - = 1
end if
end translate_3D
proc swap (var a, b : square )
var tempa := a
var tempb := b
a := tempb
b := tempa
end swap
proc sort_faces (var face : array 1 .. * of square )
for i : 1 .. upper (face )
for j : 1 .. upper (face ) - 1
var avg1 := (face (j ).z (1) + face (j ).z (2) + face (j ).z (3) + face (j ).z (4)) / 4
var avg2 := (face (j + 1).z (1) + face (j + 1).z (2) + face (j + 1).z (3) + face (j + 1).z (4)) / 4
if avg1 < avg2 then
swap (face (j ), face (j + 1))
end if
end for
end for
end sort_faces
var rx, ry, rz := maxx div 2
Input.Pause
loop
Input.Flush
Input.KeyDown (key )
for i : 1 .. 6
for j : 1 .. 4
rotate_3D (poly.face (i ).x (j ), poly.face (i ).y (j ), poly.face (i ).z (j ), rx, ry, rz )
translate_3D (poly.face (i ).x (j ), poly.face (i ).y (j ), poly.face (i ).z (j ))
projection_3D (poly.face (i ).x (j ), poly.face (i ).y (j ), poly.face (i ).z (j ), poly.face (i ).dx (j ), poly.face (i ).dy (j ))
end for
end for
sort_faces (poly.face )
for i : 1 .. 6
Draw.FillPolygon (poly.face (i ).dx, poly.face (i ).dy, 4, cyan)
Draw.Polygon (poly.face (i ).dx, poly.face (i ).dy, 4, black)
end for
View.Update
cls
end loop
|
btw here are the commands:
Up Key/Down Key - Rotate along x axis (left right axis)
Left Key/Right Key - Rotate along y axis (up down axis)
A/D Key - Rotate along z axis (forward back axis)
(with numlock this part works better)
W/S Key - Move forward/back
4/5 Key - Move Left/Right
8/2 Key - Move up/down |
|
|
|
|
|
Sponsor Sponsor
|
|
|
smool
|
Posted: Fri Apr 13, 2012 3:17 pm Post subject: RE:3D Warping |
|
|
You need a different projection formula, you can look up different projection types on wikipedia |
|
|
|
|
|
Raknarg
|
Posted: Sat Apr 14, 2012 6:04 pm Post subject: RE:3D Warping |
|
|
Oh right, I forgot about that. thanks |
|
|
|
|
|
Raknarg
|
Posted: Sun Apr 15, 2012 3:03 pm Post subject: RE:3D Warping |
|
|
Actually, I just realized that my distance from the screen was wrong, I changed it from 200 to 800 and it works now. |
|
|
|
|
|
|
|