-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
336 lines (280 loc) · 9.05 KB
/
main.c
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// ____ _ _ ___
//| _ \ ___ _ __ | |_| |__ |_ _|_ __ ___ __ _ __ _ ___
//| | | |/ _ \ '_ \| __| '_ \ | || '_ ` _ \ / _` |/ _` |/ _ \
//| |_| | __/ |_) | |_| | | | | || | | | | | (_| | (_| | __/
//|____/ \___| .__/ \__|_| |_| |___|_| |_| |_|\__,_|\__, |\___|
// |_| |___/
//the idea is to shoot straight rays from xy and get the min z and map it to ascii and color to determine it is distance
// to get a 3d - depth image so it will be simple - fast - and with tons of stitches .
// the idea comes from something like the moving sticks box that creates 3d shapes
//
//to add - find starting points to model
// find why some shapes cant be seen and fix it
// add bvh
// add nultithreading
// refactoring the code to be the best it can be .
#include <stdio.h>
#include <unistd.h>
#include <sys/termios.h>
#include <sys/poll.h>
#include "objReader.h"
#include "vectorM.h"
#include "face.h"
#define WIDTH 200
#define HEIGHT 200
#define GREEN "32m"
#define RED "31m"
#define BLUE "32m"
#define YELLOW "33m"
#define PURPLE "35m"
#define CYAN "36m"
#define BLUE2 "34;1m"
#define GREY "30;1m"
#define GREEN2 "32;1m"
#define RED2 "31;1m"
#define YELLOW2 "33;1m"
#define PURPLE2 "35;1m"
#define CYAN2 "36;1m"
#define WHITE "37m"
#define colorLen 12
#define mapLen 12
#define MaxChar ' '
#define Maxcolor WHITE
enum bool {false =0 , true = 1 } ;
double denX =0.1;
double denY = 0.1 ;
double pushX = -2 ;
double pushY=-1 ;
double pushZ =0 ;
double scale =1;
double scaleModel = 0.1;
const char depth [] =".,-~:;=!*#$@";
const char * colors [] = {GREEN,GREEN2,CYAN , CYAN2 , PURPLE,PURPLE2 ,YELLOW,YELLOW2,RED,RED2,BLUE, BLUE2,};
typedef struct angleAxsis{
double angle ;
vectorM axsis ;
};
typedef struct minVer{
double y ;
double x ;
double min ;
char changed ;
vector * vertex ;
}minVer;
typedef struct mapPack{
char * color ;
char depth ; // max 255 depth
}mapPack;
void changeColor(char * color ) {
printf("\033[0;%s",color);
}
void set_term_quiet_input()
{
struct termios tc;
tcgetattr(0, &tc);
tc.c_lflag &= ~(ICANON | ECHO);
tc.c_cc[VMIN] = 0;
tc.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &tc);
}
double getZFromPlane(vertex p1 , vertex p2 , vertex p3 ,double y , double x){
vectorM p12 = createVectorM(p1,p2);
vectorM p13 = createVectorM(p1,p3);
vectorM normal = crossProduct(p12,p13);
return (-normal.y*y - normal.x*x)/normal.z;
}
short isInsideFace( double y , double x , face f , vector * vertexes ){
vertex p0 ={ .x =x , .y = y , .z =pushZ };
int len = f.sidesNum ;
vectorM v = {.x =0 , .y=0 , .z = 1 } ;
int sign = 0;
for(size_t i=0 ;i < len ; i ++ ){
vectorM veci = createVectorM(*((vertex * )getIndexVector(f.sides_array[i]-1,vertexes)),p0);
vectorM veci2 = createVectorM(*((vertex * )getIndexVector(f.sides_array[(i+1)%len]-1,vertexes)),p0);
vectorM normal = crossProduct(veci , veci2);
float dotProd = dotProduct(v,normal);
if(i==0){
sign = dotProd > 0 ? 1 : -1;
}
if(!((sign < 0 && dotProd < 0) || (sign > 0 && dotProd > 0)) || dotProd == 0 ){
return false ;
}
}
return true;
}
mapPack map(minVer z){
double sz = z.min * scale ;
mapPack pack = {
.depth = !z.changed || sz < 0 ? MaxChar : depth[(int)fmin(sz,mapLen-1)],
.color = !z.changed || sz < 0 ? Maxcolor : colors[(int)fmin(sz,colorLen-1)]
};
return pack ;
}
void findMin(face * fc , minVer * min){
if(isInsideFace(min->y , min->x , *fc , min->vertex)){
// add check that they not colinear
vertex p1 = *((vertex * )getIndexVector(fc->sides_array[0]-1,min->vertex));
vertex p2= *((vertex * )getIndexVector(fc->sides_array[1]-1,min->vertex));
vertex p3 =*((vertex * )getIndexVector(fc->sides_array[2]-1,min->vertex));
double val = getZFromPlane(p1,p2,p3,min->y,min->x);
val +=pushZ;
val= abs(val);
if(!min->changed ||(val >= 0 &&val < min->min))
{
min->min=val ;
min->changed = true;
}
}
}
void rotateVertexes(vertex * data , struct angleAxsis * ax ){
*data = rotate(*data ,ax->axsis,ax->angle );
}
void scaleModelI(vertex * data , double * scale){
data->x*=*scale;
data->y*=*scale;
data->z*=*scale;
}
double getMinZValue(obj data ){ // To align the 3d object to be at good starting point woithout messing around
double min = INT32_MAX ;
for(size_t i =0 ; i< data.faces->len ; i++){
face * fc = getIndexVector(i , data.faces);
for(size_t j=0 ; j< fc->sidesNum ; j++ ){
if(((vertex * )getIndexVector(fc->sides_array[j]-1,data.vertexes))->z< min)
min=((vertex * )getIndexVector(fc->sides_array[j]-1,data.vertexes))->z;
}
}
return min ;
}
minMax * mapFaceBound(face * fc , vector * vx ){
return getMinMaxFace(*fc , vx);
}
int main() {
struct angleAxsis ax2 ;
vectorM axsis = {
.x=0,
.z=0,
.y=0
};
double angle = 0;
obj data = getObjData("/Users/idang/CLionProjects/ascii3DViewer/models/ninja.obj");
printf("\x1b[2J");
struct pollfd pfd = { .fd = 0, .events = POLLIN };
set_term_quiet_input();
angle = 1 ;
pushZ = getMinZValue(data);
pushZ = (pushZ < 0 ? -pushZ : 0 );
while (true) {
//vector * bounds = mapIter(mapFaceBound , data.faces , data.vertexes , sizeof(minMax));
//minMax mainBound = getMinMaxVector(bounds);
if (poll(&pfd, 1, 0)>0) {
int c = getchar();
switch(c){
case 'q':
pushZ+=2.5;
break;
case 'e':
pushZ-=2.5;
break;
case 'a':
pushX+=2.5;
break;
case 'd':
pushX-=2.5;
break;
case 'w':
pushY+=2.5;
break;
case 's':
pushY-=2.5;
break;
case 'h':
scaleModel= 1.5;
forEachIter(scaleModelI,data.vertexes,&scaleModel);
break;
case 'j':
scaleModel= 1/1.5;
forEachIter(scaleModelI,data.vertexes,&scaleModel);
break;
case 'y':
scale *=1.1;
break;
case 'u':
scale /=1.1;
break;
case '1':
axsis.x+=1;
break;
case '2':
axsis.x-=1;
break;
case '3':
axsis.y+=1;
break;
case '4':
axsis.y-=1;
break;
case '5':
axsis.z+=1;
break;
case '6':
axsis.z-=1;
break;
case '0':
angle =0;
break;
case '[':
denX/=10;
break;
case ']':
denX*=10;
break;
case 'f':
ax2.angle = 30 ;
ax2.axsis.x=0;
ax2.axsis.y=1;
ax2.axsis.z=0;
forEachIter(rotateVertexes,data.vertexes,&ax2);
angle = 0 ;
break;
case 'r':
ax2.angle = 30 ;
ax2.axsis.x=1;
ax2.axsis.y=0;
ax2.axsis.z=0;
forEachIter(rotateVertexes,data.vertexes,&ax2);
angle = 0 ;
break;
case 't':
ax2.angle = 30 ;
ax2.axsis.x=0;
ax2.axsis.y=0;
ax2.axsis.z=1;
forEachIter(rotateVertexes,data.vertexes,&ax2);
angle = 0 ;
break;
}
}
struct angleAxsis ax ;
ax.angle = angle ;
ax.axsis = axsis;
forEachIter(rotateVertexes,data.vertexes,&ax);
printf("\x1b[H");
for (double y = 0; y < HEIGHT*denX; y+=denX) {
for (double x = 0; x < WIDTH*denY; x+=denY) {
minVer min;
min.min = INT32_MIN;
min.vertex = data.vertexes;
min.changed = false ;
min.y = y + pushY ;
min.x = x + pushX ;
forEachIter(findMin, data.faces, &min);
mapPack pack = map(min);
changeColor(pack.color);
printf("%3c",pack.depth);
fflush(stdout);
}
putchar(10);
}
}
return 0;
}