Making a light inside the room

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:

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?

1 Like

Hello,

Start exploring the resources available to you and start coding!

Resources < Click here to expand !

I encourage you to review the resources available here:

:)

2 Likes

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?

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

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:

Keep looking and you will see the light.

:)

2 Likes

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

Hi,

Please be polite when you answer on the forum, people are here to help you :slight_smile:
Check the guidelines : 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.

4 Likes

Hi,

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

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 :

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;
}

2 Likes

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

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 :

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?

2 Likes

Yay!

Glad you said that, coding is all about problem solving :wink:

For the other light I noticed that using lights() 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()

1 Like

ehehh yes I like that.

I wrote some lights:

  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.

2 Likes

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?

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.

1 Like

Can you please post your Sketch?

2 Likes

Hi @Chrisir of course I can share.

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.

2 Likes