Reputation: 229
I am a new coder in processing, so please be gentle. Normally, my code is more long and complex, but I prepared to you simple code which I can convert to my code.So, the code is:
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?
Upvotes: 2
Views: 177
Reputation: 3207
Great question! It made me curious and I had to investigate. I'm afraid that this is outside my confort zone, but I found something which may help you.
Your simplified code was great but I simplified it further to make things more obvious. First the code, then some explanations:
float camZ = 0;
float rotX = 0, rotY = 0;
float lastX, lastY;
float distX, distY;
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 what's outside the cube, too.
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);
pointLight(255, 0, 0, 0, 0, 0); // this light is inside the cube. You cannot see it from outside (it's red, too)
box(2); //I made a white cube so the lightning will be more obvious
if (keyPressed)
{
if (key == 'w')
camZ -= 5;
else if (key == 's')
camZ += 5;
}
}
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;
}
Have you tried to run it? From the outside, the box will be grey-white. If you move the camera inside the box, though, you'll see that the walls have red tints. Here's what's happening:
I used lights() to illuminate the sketch. It's basically a default ambientLight in case you want to change it's values - it didn't matter for this demonstration. If you comment the line, you'll notice that you can't see anything outside the cube.
I added a pointLight in the middle of the cube. A pointlight will act like a small lightbulb: it's omnidirectional.
My pointlight is red, si it will light only red surfaces (in this case, the white walls looks red because white has a red part in RGB).
I hope this will get you started on the right path. Good luck!
Upvotes: 1