# P3D Processing source code / help me

**URL:** https://discourse.processing.org/t/p3d-processing-source-code-help-me/28626
**Category:** Coding Questions
**Created:** [March 18, 2021, 12:54am UTC](https://discourse.processing.org/t/p3d-processing-source-code-help-me/28626 "2021-03-18T00:54:59Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [March 18, 2021, 12:54am UTC](https://discourse.processing.org/t/p3d-processing-source-code-help-me/28626/1 "2021-03-18T00:54:59Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

 ![zz](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c2f21820ab87a0665f9c8d88b3ad1570b813fb26.jpeg)

1. How do I set the initial starting point position?

I want to set a location that starts as soon as the program is turned on.  
Set where the graphics are visible.

1. I want to decide the section that can be viewed with the camera.Is there a way to set it up?

I want the camera to be visible only in the (0,0,500,500) square area in the full screen.  
How can it be implemented?

source code download :  
[https://cmail.daum.net/v2/mails/0000000000006JR/attachments/MjoxLjI6NDMxNDozMDc4Mzg4OmFwcGxpY2F0aW9uL3ppcDpiYXNlNjQ6aHBmSGJ1a0FPWTQzUkJQcEFCS3VUdw/download/sketch\_3d\_test.zip](https://cmail.daum.net/v2/mails/0000000000006JR/attachments/MjoxLjI6NDMxNDozMDc4Mzg4OmFwcGxpY2F0aW9uL3ppcDpiYXNlNjQ6aHBmSGJ1a0FPWTQzUkJQcEFCS3VUdw/download/sketch_3d_test.zip)

> import java.io.FileOutputStream;
> 
> import peasy.\*;
> 
> PeasyCam camera;  
> PShape s;
> 
> void settings() {  
> size(1000, 1000,P3D);  
> PJOGL.setIcon(“icon.png”);  
> }
> 
> void setup() {
> 
> s = loadShape(“Untitled2.obj”);  
> camera = new PeasyCam(this, 200);
> 
> shapeMode(CENTER);
> 
> }
> 
> void draw() {  
> // background(255);  
> background(255);
> 
> shape(s, 0,0,500,500);
> 
> }

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [March 18, 2021, 11:04am UTC](https://discourse.processing.org/t/p3d-processing-source-code-help-me/28626/2 "2021-03-18T11:04:58Z")

</div>

Hi,

> [@GWAK](#):
>
> How do I set the initial starting point position?

The starting point position of what? The 3D model? If yes, you can just specify the coordinates when you display the shape with `shape(s, x, y)`

> [@GWAK](#):
>
> I want to set a location that starts as soon as the program is turned on.

What do you mean by a “location that starts”? You want to animate the model?

> [@GWAK](#):
>
> I want to decide the section that can be viewed with the camera.Is there a way to set it up?

Yes, you can take a look at the [`camera()`](https://processing.org/reference/camera_.html) function :

> `camera(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ)`  
> Sets the position of the camera through setting the eye position, the center of the scene, and which axis is facing upward.

Hope it helps 😉

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [March 18, 2021, 11:32am UTC](https://discourse.processing.org/t/p3d-processing-source-code-help-me/28626/3 "2021-03-18T11:32:04Z")

</div>

@josephh

Thank you for answer.

1. starting point position // can be solved with your advice.

2. I want to decide the section that can be viewed with the camera  
-Details are shown in the picture.  
-Like the picture, is it possible to implement it?

 ![TT](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/461e1eac36777cfda105a55406146a9b4a1fbd3f.jpeg)

> import java.io.FileOutputStream;
> 
> import peasy.\*;
> 
> PeasyCam camera;  
> PShape s;
> 
> void settings() {  
> size(1000, 1000,P3D);  
> PJOGL.setIcon(“icon.png”);  
> }
> 
> void setup() {
> 
> s = loadShape(“Untitled2.obj”);  
> camera = new PeasyCam(this, 200);  
> // camera(0,0,0,0,0,0,0,0,0);  
> // camera.setMinimumDistance(50);  
> // camera.setMaximumDistance(500);  
> // camera.setViewport(0,0,200,200);  
> // camera.lookAt(200,200,200);
> 
> shapeMode(CENTER);
> 
> }
> 
> void draw() {  
> // background(255);  
> background(255);  
> shape(s, 0,0,100,100);
> 
> }

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [March 18, 2021, 11:42am UTC](https://discourse.processing.org/t/p3d-processing-source-code-help-me/28626/4 "2021-03-18T11:42:55Z")

</div>

Yeah sure, you need to use a separate [`PGraphics`](https://processing.org/reference/PGraphics.html) object where you are going to display your model.

From the documentation :

> Use this class if you need to draw into an off-screen graphics buffer. A PGraphics object can be constructed with the **createGraphics()** function. The **beginDraw()** and **endDraw()** methods (see above example) are necessary to set up the buffer and to finalize it. The fields and methods for this class are extensive.

Check out this simple example :

```auto
PGraphics pg;
float angle = 0;

void setup() {
  size(500, 500, P2D);
  pg = createGraphics(200, 200, P3D);
}

void draw() {
  pg.beginDraw();
  
  pg.background(255);
  pg.translate(pg.width / 2, pg.height / 2);
  pg.rotateX(angle);
  pg.rotateY(angle * 2);
  
  pg.box(50);
  
  pg.endDraw();
  
  image(pg, 0, 0); 
  
  angle += 0.02;
}

```

In order to have a 3D context for the PGraphics, you need to use `P2D` or `P3D` for the main window.

You use `PGraphics` the same way you use drawing commands in Processing but you need to prefix it with the name of the graphics instance (`pg.drawingFunction(...)`)

![p3d](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/506a93ad7e7c8145c5143a07c39924f39b23e064.png)

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [March 18, 2021, 2:58pm UTC](https://discourse.processing.org/t/p3d-processing-source-code-help-me/28626/5 "2021-03-18T14:58:27Z")

</div>

@josephh

There are two things I am curious about.

Q1) Can’t it be resolved with’PShape’ and’PeasyCam’?

Q2) Can’t I load’obj’ file with’PGraphics’?

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [March 18, 2021, 3:22pm UTC](https://discourse.processing.org/t/p3d-processing-source-code-help-me/28626/6 "2021-03-18T15:22:33Z")

</div>

> import java.io.FileOutputStream;
> 
> import peasy.\*;
> 
> PeasyCam camera;  
> PShape s;  
> PGraphics pg;
> 
> void settings() {  
> size(1000, 1000, P3D);  
> PJOGL.setIcon(“icon.png”);  
> }
> 
> void setup() {
> 
> pg = createGraphics(400, 400, P3D);  
> s = loadShape(“Untitled2.obj”);  
> camera = new PeasyCam(this, pg, 200);  
> camera.setViewport(0, 0, 400, 400);  
> shapeMode(CENTER);  
> // pg.beginDraw(); pg.endDraw();  
> }
> 
> void draw() {
> 
> background(255);
> 
> pg.beginDraw();  
> pg.background(255);  
> pg.lights();  
> [//pg.translate](https://pg.translate)(pg.width/2, pg.height/2 + 100, -200);  
> [//pg.rotateX](https://pg.rotateX)(PI);  
> [//pg.rotateY](https://pg.rotateY)(frameCount \* 0.01f);  
> pg.shape(s);  
> pg.endDraw();  
> image(pg, 0,0,400, 400);
> 
> }

Is it correct to do something like the source code above?

Or is there another way?

 ![abc](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/cffe3c23ed39ebdefb18bfeb5b7d2ce50456eb03.png)

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [March 18, 2021, 5:59pm UTC](https://discourse.processing.org/t/p3d-processing-source-code-help-me/28626/7 "2021-03-18T17:59:56Z")

</div>

> [@GWAK](#):
>
> Can’t it be resolved with’PShape’ and’PeasyCam’?

A `PShape` object is just a datatype for storing shapes (SVG or OBJ) and PeasyCam :

> provides a dead-simple mouse-driven camera for Processing

(from the [repo](https://github.com/jdf/peasycam))

So those are not related to what you described earlier with your picture, you need to use a `PGraphics` object in order to draw on a separate buffer isolated from the rest of your sketch.

> [@GWAK](#):
>
> Can’t I load’obj’ file with’PGraphics’?

You only need to use your instance of `PGraphics` in order to display what you want. You can still use global methods in order to load your OBJ file. (like you did in the code above)

The above code looks correct so you are good to go! 😉 (remember that to format your code on the forum, you can use the `</>` button instead of the quote one)

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [March 19, 2021, 6:46am UTC](https://discourse.processing.org/t/p3d-processing-source-code-help-me/28626/8 "2021-03-19T06:46:27Z")

</div>

Dear josephh,

Thank you very much for your interest. What I want is simply to open the .obj file. And this is a method that works only in a defined area.

Thank you.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 19, 2021, 6:51am UTC](https://discourse.processing.org/t/p3d-processing-source-code-help-me/28626/9 "2021-03-19T06:51:58Z")

</div>

When you look at the documentation of peasycam  
you should find stuff to move the camera to a certain position

You can make a menu or have keys to manage different camera positions

But it’s not totally easy and I don’t know whether peasycam was made for this
