<--애드센스-->

3D 공간 작업의 토대로써 사용하게 될 소스. 카메라, 키보드 마우스를 이용한 이동, 정육면체과 구를 생성할 수 있다.

 wasd: 앞뒤, 양옆이동
 스페이스바: +y축(위로)이동
 쉬프트키:-y축(아래로)이동
 ESC:종료
 마우스 클릭: 마우스 고정해제 & 커서표시
 c:  정육면체 생성
 b: 구 생성

해결해야 할 문제점:: 물체를 너무 많이 생성하면(한 50개쯤) 마우스에 의한 시점이동 시 무척 렉이걸린다.
 이동하면서 물체의 생성이 안된다.(방향키와 c,b 키를 동시에 조작불가)


누군가에게 도움이 될 수 있을까 해서 소스도 올려본다. 그러나 당신이 어떤 문제에 봉착했다면, 우선 스스로 깊게 생각해보는 것이 실력향상에 도움이 될 것이다. 또, 당연한 소리지만 프로세싱 2주차인 나보다 훨씬 효율적인 코드가 분명 있을 것이므로 아래의 것을 모범으로 삼지는 않았으면 한다.

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
//Main//////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
import java.awt.Robot;
import java.lang.System.*;
final static float pi = 3.14;
boolean display;//True: program is running; to exit threads
void setup() {
  frameRate(60);
  size(800600, P3D);
  smooth(2);
  initialization();
  thread("Title");
}
void draw(){
  //Foundation
  directionalLight(255,255,255-0.5-3-1);
  background(0);
  
  Controlers();
  //rendering
  Render_Elements();
  Draw_Axis();
  WindowCenter();
}
void initialization () {
  display = true;
  surface.setResizable(true);
  fill(45);
  stroke(0);
  noCursor();
  keys = new boolean[255];
  for(int i=0;i < keys.length;i++) {
    keys[i] = false;
  }
}
void Draw_Axis() {
  pushMatrix();//Plate
  translate(0,-2,0);
  fill(51,51,51);
  box(20000,-3,20000);
  popMatrix();
  
  pushMatrix();//Axis
  stroke(15000);
  line(-1000000000);
  stroke(25000);
  line(0001000000);
  stroke(01550);
  line(0-100000000);
  stroke(02550);
  line(0000100000);
  stroke(00155);
  line(00-10000000);
  stroke(00255);
  line(0000010000);
  noStroke();
  popMatrix();
}
void Title() {
  for(;;){
    if(display) {
      surface.setTitle("FoundationSource 1.0 by KimDongYeop");
      delay(3000);
      surface.setTitle("Push ESC to Exit");
      delay(3000);
    }
  }
}
boolean centered= false;
void WindowCenter() {
  if(centered){
    surface.setLocation(displayWidth/2-width/2, displayHeight/2-height/2);//move window center
    centered = true;
  }
}
//Controlers////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
void Controlers() {
  CameraUpdate();
  KeyBoard();
  Key_Render();
  Mouse();
}
//Camera
float cpX = 0.0, cpY = 200.0, cpZ = 200.0;
float theta = 0, phi = pi/2;
float fov = radians(90);
float zFar = 600.0;
void CameraUpdate() {
 beginCamera();
 //translate(cpX, cpY, cpZ);
 camera(cpX, cpY, cpZ,
        cpX+cos(theta),cpY+cos(phi),cpZ+sin(theta),
        0.0-1.00.0);
 perspective(fov, float(width)/float(height), 0.1, zFar);
 endCamera();
}
//KeyBoard
float k = 10;//speed
boolean KeyP = false;
boolean[] keys;//a~z:0~25
void KeyBoard() {
  if(keyPressed){
    switch(key) {
      case 'd':
        keys[3= true;
        break;
      case 'a':
        keys[0= true;
        break;
      case 'w':
        keys[22= true;
        break;
      case 's':
        keys[19= true;
        break;
      case ' ':
        cpY+=k;
        break;
      case 'c':
        if(KeyP == false){
          Create_Cell(cpX, cpY, cpZ, 25500);
        }
        break;
      case 'b':
        if(KeyP == false) {
          Create_Ball(cpX, cpY, cpZ, 02550);
        }
        break;
      case ESC:
        display = false;
        exit();
        break;
    }
    if( key ==CODED){
      if( keyCode == SHIFT) {
        cpY-=k;
      }
    }
    KeyP = true;
  }
}
void keyReleased(){
  KeyP = false;
  for(int i =0; i < keys.length; i++) {
    keys[i] = false;
  }
}
void Key_Render() {
  if(keys[0== true) {//Key a
    cpX+=k*cos(theta+pi/2);
    cpZ+=k*sin(theta+pi/2);
    print("a");
  }
  if(keys[3== true) {//Key d
    cpX+=k*cos(theta-pi/2);
    cpZ+=k*sin(theta-pi/2);
    print("d");
  }
  if(keys[22== true) {//Key w
    cpX+=k*cos(theta);
    cpY+=k*cos(phi);
    cpZ+=k*sin(theta);
    print("w");
  }
  if(keys[19== true) {//Key s
    cpX-=k*cos(theta);
    cpY-=k*cos(phi);
    cpZ-=k*sin(theta);
    print("s");
  }
  println();
  /*
  if(keys[0] == true) {//Key 
  }
  if(keys[0] == true) {//Key 
  }
  */
}
//Mouse
Robot robot;
boolean CursorLock = true;
boolean check = true;//controler to hold cursor
int mouseResistance = 720;
void mouseClicked() {
  CursorLock = !CursorLock;
  CursorVisiable();
}
void Mouse() {
  if(CursorLock){
    if(check){
      float abY = abs(mouseY-pmouseY);
      float l = pow(sqrt(mouseX-pmouseX)+sqrt(mouseY-pmouseY),1/2);
      
      theta-=l*(mouseX-pmouseX)*pi/mouseResistance;
      if(0 < phi && phi < pi) {//prevent phi overflow
         phi+=l*(mouseY-pmouseY)*pi/mouseResistance;
      }else{
        if(phi >= pi){
          phi-=l*abY*pi/mouseResistance;
        }
        if(phi <= 0){
          phi+=l*abY*pi/mouseResistance;
        }
      }
      check=false;
    }else{
      try{
        robot = new Robot();
        robot.mouseMove(displayWidth/2,displayHeight/2);
        check=true;
      }catch(Throwable e){
        println("ddd");
      }  
    }
  }
}
void CursorVisiable() {
  if(CursorLock) {
    noCursor();
  }else{
    cursor();
  }
}
//Elements//////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
class Cell implements Elements{
  float positionX, positionY, positionZ;
  int[] Rgb =new int[3];
  int Size = 5;//Size of Cell
  void Draw() {
    pushMatrix();
    fill(Rgb[0],Rgb[1],Rgb[2]);
    translate(positionX, positionY, positionZ);
    box(Size,Size,Size);
    popMatrix();
  }
  
  Cell(float x, float y, float z, int r, int g, int b){
    positionX= x;
    positionY = y;
    positionZ = z;
    
    Rgb[0= r;
    Rgb[1= g;
    Rgb[2= b;
  }
}
class Ball implements Elements{
  float positionX, positionY, positionZ;
  int[] Rgb =new int[3];
  int Size = 5;//Size of Ball
  void Draw() {
    pushMatrix();
    fill(Rgb[0],Rgb[1],Rgb[2]);
    translate(positionX, positionY, positionZ);
    sphere(Size);
    popMatrix();
  }
  
  Ball(float x, float y, float z, int r, int g, int b){
    positionX= x;
    positionY = y;
    positionZ = z;
    
    Rgb[0= r;
    Rgb[1= g;
    Rgb[2= b;
  }
}
interface Elements {
  void Draw();
}
//Functions/////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
Cell[] cells=new Cell[0];
Ball[] balls=new Ball[0];
void Create_Cell(float x, float y, float z, int r, int g, int b) {
  addCell(new Cell(x,y,z,r,g,b));
}
void Create_Ball(float x, float y, float z, int r, int g, int b) {
  addBall(new Ball(x,y,z,r,g,b));
}
void Render_Elements(){
  for(int i=0;i<cells.length;i++){
    cells[i].Draw();
  }
  for(int i=0;i<balls.length;i++){
    balls[i].Draw();
  }
}
void addBall(Ball b){
  Ball[] balls_Copy = new Ball[balls.length+1];
  System.arraycopy(balls, 0, balls_Copy, 0, balls.length);
  balls = balls_Copy;
  balls[balls.length-1= b;
  println("Create Ball!");
  println(balls.length);
}
void addCell(Cell c){
  Cell[] cells_Copy = new Cell[cells.length+1];
  System.arraycopy(cells, 0, cells_Copy, 0, cells.length);
  cells = cells_Copy;
  cells[cells.length-1= c;
  println("Create Cell!");
  println(cells.length);
}
/*
void addElement(Elements[] E, Elements e) {
  Elements[] E_Copy = new Elements[E.length+1];
  System.arraycopy(E, 0, E_Copy, 0, E.length);
  E = E_Copy;
  E[E.length-1] = e;
  println("Create Element!");
}
*/
cs


'프로그래밍 > Processing' 카테고리의 다른 글

Processing 마지막 과제  (0) 2018.12.04
Processing을 이용한 3D World. 그리고 카메라  (1) 2018.10.31

+ Recent posts