This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
3d.sh
137 lines (124 loc) · 2.01 KB
/
3d.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#! /bin/bash
FACTOR=100000000000
function to
{
echo `expr $1 \* $FACTOR`
}
. sincos.sh
. vec.sh
. mat.sh
WIDTH=
HEIGHT=
COL=
LINE=
HALF_WIDTH=
HALF_HEIGHT=
SCR_BUFF=
function glInit()
{
COL=`tput cols`
LINE=`tput lines`
WIDTH=$(expr $FACTOR \* $COL)
HEIGHT=$(expr $FACTOR \* $LINE)
HALF_WIDTH=`expr $WIDTH \/ 2`
HALF_HEIGHT=`expr $HEIGHT \/ 2`
}
glInit
function glClear()
{
size=$(expr $COL \* $LINE)
SCR_BUFF=$(head -c $size < /dev/zero | tr '\0' '_')
}
function glSwap()
{
tput clear
tput cup 0 0
echo -n $SCR_BUFF | sed "s/_/ /g"
}
MODEL_MAT=
PROJ_MAT=
mIdent MODEL_MAT
mIdent PROJ_MAT
function glLoadIdentity() {
mIdent MODEL_MAT
}
function glTranslate()
{
v=
mTrans $1 $2 $3 v
mMul v MODEL_MAT MODEL_MAT
}
function glScale()
{
v=
mScale $1 $2 $3 v
mMul v MODEL_MAT MODEL_MAT
}
function glRotate()
{
v=
mRotate $1 $2 $3 $4 v
mMul v MODEL_MAT MODEL_MAT
}
function glFrustum()
{
mFrustum $1 $2 $3 $4 $5 $6 PROJ_MAT
}
function gToScreen()
{
in=($(eval echo $(eval echo "\$\{$1[@]\}")))
eval $2=\$\(expr \\\( ${in[1]} \\\* $HALF_WIDTH \\\/ ${in[4]} \\\+ $HALF_WIDTH \\\) \\\/ $FACTOR \)
eval $3=\$\(expr \\\( ${in[2]} \\\* $HALF_HEIGHT \\\/ ${in[4]} \\\+ $HALF_HEIGHT \\\) \\\/ $FACTOR \)
}
function gLine()
{
if(($1 < $3));then
dx=`expr $3 - $1`
sx=1
else
dx=`expr $1 - $3`
sx=-1
fi
if(($2 < $4));then
dy=`expr $4 - $2`
sy=1
else
dy=`expr $2 - $4`
sy=-1
fi
x=$1
y=$2
err=`expr $dx - $dy`
while true; do
pos=$(expr $y \* $COL \+ $x \+ 1)
SCR_BUFF="${SCR_BUFF:0:${pos}}*${SCR_BUFF:${pos}+1}"
if [ $x -eq $3 -a $y -eq $4 ]; then
return 0
fi
e2=`expr 2 \* $err`
if [ $e2 -gt -$dy ]; then
err=`expr $err - $dy`
x=`expr $x \+ $sx`
fi
if [ $e2 -lt $dx ]; then
err=`expr $err + $dx`
y=`expr $y \+ $sy`
fi
done
}
function glLine()
{
v1=(VEC $1 $2 $3 $FACTOR)
mvMul MODEL_MAT v1 v1
mvMul PROJ_MAT v1 v1
x1=
y1=
gToScreen v1 x1 y1
v2=(VEC $4 $5 $6 $FACTOR)
mvMul MODEL_MAT v2 v2
mvMul PROJ_MAT v2 v2
x2=
y2=
gToScreen v2 x2 y2
gLine $x1 $y1 $x2 $y2
}