# Making a light inside the room

**URL:** https://discourse.processing.org/t/making-a-light-inside-the-room/21164
**Category:** Coding Questions
**Tags:** homework
**Created:** [June 3, 2020, 7:00am UTC](https://discourse.processing.org/t/making-a-light-inside-the-room/21164 "2020-06-03T07:00:03Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![alkyonemis](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@alkyonemis](https://discourse.processing.org/u/alkyonemis)
#### Post date: [June 3, 2020, 7:00am UTC](https://discourse.processing.org/t/making-a-light-inside-the-room/21164/1 "2020-06-03T07:00:03Z")

</div>

Hi, I am a new coder on processing. Normally, my code is 1000 lines.However, I prepared for you a simple code that I can modify on my code. The project last date is very soon. So, please be poilte and gentle. The code which I prepared only for you here:

```auto
void setup()
{
  size(1200, 800, P3D);
  camZ = (height/2) / tan((PI*60.0)/360.0);
  noStroke();
}
float camZ = 0;

void draw()
{
  background(0);
  
  camera(0, 0.0, camZ, //default camera position
  0, 0, -500, //where it is looking to
  0, 1, 0); //eye openness
  
  rotateX(rotX + distY);
  rotateY(rotY + distX);
  
  scale(100);
  drawHouse();
  
  if(keyPressed)
  {
    if(key == 'w')
      camZ -= 5;
    else if(key == 's')
      camZ += 5;
  }
}

void drawHouse()
{
  
  
  beginShape(QUADS);
  fill(255,0,0);
  //+Z face
  vertex(-1, -1, 1); //upper left corner
  vertex(1, -1, 1); //upper right corner
  vertex(1, 1, 1); //bottom right corner
  vertex(-1, 1, 1); //bottom left corner
  
  fill(0,255,0);
  //-Z face
  vertex(-1, -1, -1); //upper left corner
  vertex(1, -1, -1); //upper right corner
  vertex(1, 1, -1); //bottom right corner
  vertex(-1, 1, -1); //bottom left corner
  
  fill(0,0,255);
  //+X face
  vertex(1, -1, 1); //upper left corner
  vertex(1, -1, -1);
  vertex(1, 1, -1);
  vertex(1, 1, 1);
  
  fill(255);
  //-X face
  vertex(-1, -1, 1); //upper left corner
  vertex(-1, -1, -1);
  vertex(-1, 1, -1);
  vertex(-1, 1, 1);
  
  fill(208,13,211);
  //-Y face
  vertex(-1, -1, 1);
  vertex(1, -1, 1);
  vertex(1, -1, -1);
  vertex(-1, -1, -1);
  
  fill(250,150,18);
  //+Y face
  vertex(-1, 1, 1);
  vertex(1, 1, 1);
  vertex(1, 1, -1);
  vertex(-1, 1, -1);
  endShape();
}

float rotX = 0, rotY = 0;
float lastX, lastY;
float distX, distY;

void mousePressed()
{
  lastX = mouseX;
  lastY = mouseY;
}

void mouseDragged()
{
  distX = radians(mouseX - lastX);
  distY = radians(lastY - mouseY);
}

void mouseReleased()
{
  rotX += distY;
  rotY += distX;
  distX = distY = 0;
}

```

So, I want to make a light inside the box which was given in the code. The light should effect only inside the box. How to make that happened?

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [June 3, 2020, 7:00am UTC](https://discourse.processing.org/t/making-a-light-inside-the-room/21164/2 "2020-06-03T07:00:03Z")

</div>

Hello,

> [@alkyonemis](#):
>
> How to make that happened?

Start exploring the resources available to you and start coding!

> **Resources \< Click here to expand !**
>
> I encourage you to review the resources available here:
> 
> - The Processing _website_ has references, examples, tutorials, etc.  
> [https://processing.org/](https://processing.org/)
> - The Processing PDE (IDE) has lots to offer!  
> An exploration of the tabs will reveal what is available; libraries, tools and local examples there.
> - The Coding Train  
> [https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1\_aw](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw)  
> [https://thecodingtrain.com/](https://thecodingtrain.com/)
> - Happy Coding  
> [Processing Tutorials - Happy Coding](https://happycoding.io/tutorials/processing/)
> - The source code can give insight if you are adventurous:  
> [GitHub - processing/processing: Source code for the Processing Core and Development Environment (PDE)](https://github.com/processing/processing)  
> Many of the Processing functions are here:  
> [processing/core/src/processing/core/PApplet.java at master · processing/processing · GitHub](https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java)
> - The Processing Foundation  
> [https://processingfoundation.org/](https://processingfoundation.org/)  
> Check out the tabs.  
> In the education tab, there is a reference to the Coding Train.
> - And the Internet. `:)`

`:)`

---

<div class="post-metadata">

### Author: ![alkyonemis](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@alkyonemis](https://discourse.processing.org/u/alkyonemis)
#### Post date: [June 3, 2020, 7:00am UTC](https://discourse.processing.org/t/making-a-light-inside-the-room/21164/3 "2020-06-03T07:00:03Z")

</div>

Hi glv, thank you so much for your reply and copy paste. However, it seems like a bad jock to me. I tried all light functions and didn’t help. So, anyone can really help to me in here?

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [June 3, 2020, 7:00am UTC](https://discourse.processing.org/t/making-a-light-inside-the-room/21164/4 "2020-06-03T07:00:03Z")

</div>

I explored the resources and found a solution to this in the light references:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8d1451add6e84e23c5a7abbfbc21ad94b3f30333.png)

I also learned something new.

I put a light right in the center of the box.  
I removed two of the faces so you could see inside it.

Here is the view with the light inside a completely enclosed box:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/95a7f6f7e868d38cf4eb5724b9948c68b1f8cc72.png)

> [@alkyonemis](#):
>
> I tried all light functions and didn’t help.

Keep looking and you will see the light.

`:)`

---

<div class="post-metadata">

### Author: ![alkyonemis](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@alkyonemis](https://discourse.processing.org/u/alkyonemis)
#### Post date: [June 3, 2020, 7:00am UTC](https://discourse.processing.org/t/making-a-light-inside-the-room/21164/6 "2020-06-03T07:00:03Z")

</div>

So, I really need help. Does anyone can help? Because this is a project which have a last date soon.

---

<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: [June 3, 2020, 7:00am UTC](https://discourse.processing.org/t/making-a-light-inside-the-room/21164/7 "2020-06-03T07:00:03Z")

</div>

Hi,

Please be polite when you answer on the forum, people are here to help you 🙂  
Check the guidelines : [https://discourse.processing.org/faq#civilized](https://discourse.processing.org/faq#civilized)

After some tweak I found that putting a light instruction (like `directionalLight()` or ` ambientLight()`) doesn’t work when it’s after the function that display your cube.

You need to set your lighting before displaying any 3D objects.

---

<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: [June 3, 2020, 7:00am UTC](https://discourse.processing.org/t/making-a-light-inside-the-room/21164/9 "2020-06-03T07:00:03Z")

</div>

Hi,

Thanks for your answer, however it doesn’t justify this kind of behavior, anyway 😉

Below is a working code where you have a red point light inside your cube. I took freedom to refactor a little bit your code :

```auto
float rotX = 0, rotY = 0;
float lastX, lastY;
float distX, distY;
float camZ = 0;

void setup() {
  size(1200, 800, P3D);
  camZ = (height/2) / tan((PI*60.0)/360.0);
  noStroke();
}

void draw() {
  background(0);

  // Red light
  pointLight(255, 0, 0, 0, 0, 0);

  camera(0, 0.0, camZ, 0, 0, -500, 0, 1, 0); 

  rotateX(rotX + distY);
  rotateY(rotY + distX);

  //box(100);

  drawHouse(100);

  if (keyPressed) {
    if (key == 'w')
      camZ -= 5;
    else if (key == 's')
      camZ += 5;
  }
}

void drawHouse(int size) {
  beginShape(QUADS);
  fill(255, 0, 0);
  //+Z face
  vertex(-size, -size, size); //upper left corner
  vertex(size, -size, size); //upper right corner
  vertex(size, size, size); //bottom right corner
  vertex(-size, size, size); //bottom left corner

  fill(0, 255, 0);
  //-Z face
  vertex(-size, -size, -size); //upper left corner
  vertex(size, -size, -size); //upper right corner
  vertex(size, size, -size); //bottom right corner
  vertex(-size, size, -size); //bottom left corner

  fill(0, 0, 255);
  //+X face
  vertex(size, -size, size); //upper left corner
  vertex(size, -size, -size);
  vertex(size, size, -size);
  vertex(size, size, size);

  fill(255);
  //-X face
  vertex(-size, -size, size); //upper left corner
  vertex(-size, -size, -size);
  vertex(-size, size, -size);
  vertex(-size, size, size);

  fill(208, 13, 211);
  //-Y face
  vertex(-size, -size, size);
  vertex(size, -size, size);
  vertex(size, -size, -size);
  vertex(-size, -size, -size);

  fill(250, 150, 18);
  //+Y face
  vertex(-size, size, size);
  vertex(size, size, size);
  vertex(size, size, -size);
  vertex(-size, size, -size);
  endShape();
}

void mousePressed() {
  lastX = mouseX;
  lastY = mouseY;
}

void mouseDragged() {
  distX = radians(mouseX - lastX);
  distY = radians(lastY - mouseY);
}

void mouseReleased() {
  rotX += distY;
  rotY += distX;
  distX = distY = 0;
}

```

---

<div class="post-metadata">

### Author: ![alkyonemis](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@alkyonemis](https://discourse.processing.org/u/alkyonemis)
#### Post date: [June 3, 2020, 7:00am UTC](https://discourse.processing.org/t/making-a-light-inside-the-room/21164/10 "2020-06-03T07:00:03Z")

</div>

Thanks @josephh

Actually, you are right. I need more calm. I know, I was right but when I show this kind of behaviour. It looks like “I am the wrong one” so, I’ll try my best for keep calm.

Thanks for your effort. However, unfortunatelly the light is in the outside. I want to see the box. The light should be inside the cube.

Then I will put a switch for the light. The light can control with mouse click for off and on.

Thank you

---

<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: [June 3, 2020, 7:00am UTC](https://discourse.processing.org/t/making-a-light-inside-the-room/21164/11 "2020-06-03T07:00:03Z")

</div>

> [@alkyonemis](#):
>
> Thanks for your effort. However, unfortunatelly the light is in the outside. I want to see the box. The light should be inside the cube.

I think you are misleading here.

In the code above, the point light is situated at the center of the coordinate system (meaning 0 for x, y and z). At the same time, you also draw your “house” from the center meaning that the point light is now inside the box.

In your first post, you asked about whether it’s possible to do this :

> [@alkyonemis](#):
>
> The light should effect only inside the box. How to make that happened?

which is exactly what the above code is doing : the red light is inside the box so no light is coming outside → the outside of the box is black so you can’t see it.

So what do you want to achieve?

---

<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: [June 3, 2020, 7:00am UTC](https://discourse.processing.org/t/making-a-light-inside-the-room/21164/13 "2020-06-03T07:00:03Z")

</div>

Yay!

Glad you said that, coding is all about problem solving 😉

For the other light I noticed that using [`lights()`](https://processing.org/reference/lights_.html) for instance is not working because there’s light inside the cube… Maybe it has to do with `beginShape(QUADS)` because it works with `cube()`

---

<div class="post-metadata">

### Author: ![alkyonemis](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@alkyonemis](https://discourse.processing.org/u/alkyonemis)
#### Post date: [June 3, 2020, 7:00am UTC](https://discourse.processing.org/t/making-a-light-inside-the-room/21164/14 "2020-06-03T07:00:03Z")

</div>

ehehh yes I like that.

I wrote some lights:

```auto
  directionalLight(255, 255, 255, 0, height, 0);
  directionalLight(255, 255, 255, width, -height, 0);
  directionalLight(255, 255, 255, -width, -height, 0);
  directionalLight(255, 255, 255, width, height, 0);
  directionalLight(255, 255, 255, 0, 0, 250);
  directionalLight(255, 255, 255, 0, 0, -250);

```

I can see the house from outside. I wrote `pointLight(0, 0, 0, 0, 0, 0);` for inside the house.Then I expect inside the house would be dark but it couldn’t happened. However I understood the logic it is really helped to me.

---

<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: [June 3, 2020, 7:00am UTC](https://discourse.processing.org/t/making-a-light-inside-the-room/21164/15 "2020-06-03T07:00:03Z")

</div>

Interesting

What happens when you switch light in the house on and the lights outside off?

And then when you have the lights in the house off and outside lights on?

Does this work?

---

<div class="post-metadata">

### Author: ![alkyonemis](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@alkyonemis](https://discourse.processing.org/u/alkyonemis)
#### Post date: [June 3, 2020, 7:00am UTC](https://discourse.processing.org/t/making-a-light-inside-the-room/21164/16 "2020-06-03T07:00:03Z")

</div>

Hi @Chrisir

I put a `lights();` outside the house, so I can see the house right know.

I put a `pointLight(0, 0, 0, 0, 0, 0);` inside the house, then I wrote a `key` for switch.

I can close and open lights inside the house with key event, that was my problem.

---

<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: [June 3, 2020, 7:00am UTC](https://discourse.processing.org/t/making-a-light-inside-the-room/21164/17 "2020-06-03T07:00:03Z")

</div>

Can you please post your Sketch?

---

<div class="post-metadata">

### Author: ![alkyonemis](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@alkyonemis](https://discourse.processing.org/u/alkyonemis)
#### Post date: [June 3, 2020, 7:00am UTC](https://discourse.processing.org/t/making-a-light-inside-the-room/21164/18 "2020-06-03T07:00:03Z")

</div>

Hi @Chrisir of course I can share.

```auto
float rotX = 0, rotY = 0;
float lastX, lastY;
float distX, distY;
float camZ = 0;

void setup() {
  size(1200, 800, P3D);
  camZ = (height/2) / tan((PI*60.0)/360.0);
  noStroke();
}

void draw() {
  background(0);
  lights(); // we want to see house from outside
  
  camera(0, 0.0, camZ - 300, 0, 0, -500, 0, 1, 0); 
  
  if (keyPressed) {
    if (key == 'w')
    {
      camZ -= 5;
    }
    else if (key == 's')
    {
      camZ += 5;
    }
    else if (key == 'l')
    {
      pointLight(255, 255, 255, 0, 0, 0); // if user press 'l' (l)ight key, there will be white lamp inside the house.
    }
  }

  rotateX(rotX + distY);
  rotateY(rotY + distX);
  
   // Black light
  pointLight(0, 0, 0, 0, 0, 0); // we want to see dark inside the house
 
  drawHouse(100);

  
}

void drawHouse(int size) {
  beginShape(QUADS);
  fill(255, 0, 0);
  //+Z face
  vertex(-size, -size, size); //upper left corner
  vertex(size, -size, size); //upper right corner
  vertex(size, size, size); //bottom right corner
  vertex(-size, size, size); //bottom left corner

  fill(0, 255, 0);
  //-Z face
  vertex(-size, -size, -size); //upper left corner
  vertex(size, -size, -size); //upper right corner
  vertex(size, size, -size); //bottom right corner
  vertex(-size, size, -size); //bottom left corner

  fill(0, 0, 255);
  //+X face
  vertex(size, -size, size); //upper left corner
  vertex(size, -size, -size);
  vertex(size, size, -size);
  vertex(size, size, size);

  fill(255);
  //-X face
  vertex(-size, -size, size); //upper left corner
  vertex(-size, -size, -size);
  vertex(-size, size, -size);
  vertex(-size, size, size);

  fill(208, 13, 211);
  //-Y face
  vertex(-size, -size, size);
  vertex(size, -size, size);
  vertex(size, -size, -size);
  vertex(-size, -size, -size);

  fill(250, 150, 18);
  //+Y face
  vertex(-size, size, size);
  vertex(size, size, size);
  vertex(size, size, -size);
  vertex(-size, size, -size);
  endShape();
}

void mousePressed() {
  lastX = mouseX;
  lastY = mouseY;
}

void mouseDragged() {
  distX = radians(mouseX - lastX);
  distY = radians(lastY - mouseY);
}

void mouseReleased() {
  rotX += distY;
  rotY += distX;
  distX = distY = 0;
}

```

Actually it didn’t finish yet.  
The lights work with key. However, There will be a light switch on the wall. When the user enters the house, he will click on the light switch and turn on the lights.  
I don’t know how to do it. I work on it.

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [May 26, 2020, 9:38pm UTC](https://discourse.processing.org/t/making-a-light-inside-the-room/21164/19 "2020-05-26T21:38:59Z")

</div>


