如何在实体模式下绘制网格表面?
How to draw the surface of the grid in solid mode?
我有这段代码,我应该修改它来做两件事:
- 使用基本光照(
lights()
和独特的 fill()
)和 在实体模式下绘制表面
- 用两种颜色渐变绘制表面(red 用于 z 和 yellow[=25 的低值=] 对于高值,例如)为此,我被建议在每个顶点之前使用
fill()
调用()
这是我的第一个代码,问题是我不想在应用颜色后显示网格。
// Drawing a 3D function
float rotX = 0.0, rotY = 0.0;
int lastX, lastY;
float distX = 0.0, distY = 0.0;
// Function steps
int steps = 50;
// z scale
float scaleZ = 200.0;
// z zoom
float zoomZ = -300.0;
// Graphic size
float gX = 500.0, gY = 500.0;
void setup()
{
size(500, 500, P3D);
noFill();
strokeWeight(0.005);
}
float function(float x, float y)
{
return x*x*x + y*y*y;
}
void draw() {
lights();
background(0);
// We center the results on window
translate(gX/2, gY/2, zoomZ);
// Rotation
rotateY(rotY + distX);
rotateX(rotX + distY);
// Centering around (0, 0);
translate(-gX/2, -gY/2);
// Function covers
// 400 x 400 x scaleZ
scale(gX, gY, scaleZ);
// Drawing the function
fill(167);
drawFunction();
// Drawing axes
stroke(255, 0, 0);
line(0,0,0,2000,0,0);
stroke(0,255,0);
line(0,0,0,0,2000,0);
stroke(0,0,255);
line(0,0,0,0,0,2000);
}
void drawFunction()
{
float x, y, z;
int i = 0, j = 0;
float in_steps = 1.0 / steps;
float[][] matrix = new float[steps+1][steps+1];
for (y = 0.0, j = 0; y <= 1.0; y+=in_steps, j++)
for (x = 0.0, i = 0; x <= 1.0; x+=in_steps, i++)
matrix[i][j] = function(x, y);
stroke(167);
for (j = 0, y = 0.0; j < steps; j++, y+=in_steps) {
beginShape(QUAD_STRIP);
for (i = 0, x = 0.0; i <= steps; i++, x+=in_steps) {
vertex(x, y, matrix[i][j]);
vertex(x, y + in_steps, matrix[i][j+1]);
}
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.0;
}
lights()
已经在您的代码中正确设置。
使用noStroke()
去掉线条。 noStroke
禁用绘图轮廓。
可以通过将 RGB(红色、绿色和蓝色)值传递给 fill()
来设置填充区域的颜色。
这些值是 [0, 255] 范围内的整数值。红色的 RGB 值为 (255, 0, 0),黄色的 RGB 值为 (255, 255, 0)。
可以通过
实现从红色到黄色的渐变颜色
fill(255, z*255, 0);
其中 z 在 [0.0, 1.0] 中。如果 z = 0.0
结果为红色 (255, 0, 0),如果 z = 1.0
结果为黄色 (255, 255, 0)。 0.0 和 1.0 之间的所有 z 值都会导致读取和黄色之间的线性插值。
例如
for (j = 0, y = 0.0; j < steps; j++, y+=in_steps) {
beginShape(QUAD_STRIP);
noStroke(); // no lines
for (i = 0, x = 0.0; i <= steps; i++, x+=in_steps) {
fill(255, matrix[i][j] * 255, 0); // interpolate between red and yellow
vertex(x, y, matrix[i][j]);
fill(255, matrix[i][j+1] * 255, 0); // interpolate between red and yellow
vertex(x, y + in_steps, matrix[i][j+1]);
}
endShape();
}
我有这段代码,我应该修改它来做两件事:
- 使用基本光照(
lights()
和独特的fill()
)和 在实体模式下绘制表面
- 用两种颜色渐变绘制表面(red 用于 z 和 yellow[=25 的低值=] 对于高值,例如)为此,我被建议在每个顶点之前使用
fill()
调用()
这是我的第一个代码,问题是我不想在应用颜色后显示网格。
// Drawing a 3D function
float rotX = 0.0, rotY = 0.0;
int lastX, lastY;
float distX = 0.0, distY = 0.0;
// Function steps
int steps = 50;
// z scale
float scaleZ = 200.0;
// z zoom
float zoomZ = -300.0;
// Graphic size
float gX = 500.0, gY = 500.0;
void setup()
{
size(500, 500, P3D);
noFill();
strokeWeight(0.005);
}
float function(float x, float y)
{
return x*x*x + y*y*y;
}
void draw() {
lights();
background(0);
// We center the results on window
translate(gX/2, gY/2, zoomZ);
// Rotation
rotateY(rotY + distX);
rotateX(rotX + distY);
// Centering around (0, 0);
translate(-gX/2, -gY/2);
// Function covers
// 400 x 400 x scaleZ
scale(gX, gY, scaleZ);
// Drawing the function
fill(167);
drawFunction();
// Drawing axes
stroke(255, 0, 0);
line(0,0,0,2000,0,0);
stroke(0,255,0);
line(0,0,0,0,2000,0);
stroke(0,0,255);
line(0,0,0,0,0,2000);
}
void drawFunction()
{
float x, y, z;
int i = 0, j = 0;
float in_steps = 1.0 / steps;
float[][] matrix = new float[steps+1][steps+1];
for (y = 0.0, j = 0; y <= 1.0; y+=in_steps, j++)
for (x = 0.0, i = 0; x <= 1.0; x+=in_steps, i++)
matrix[i][j] = function(x, y);
stroke(167);
for (j = 0, y = 0.0; j < steps; j++, y+=in_steps) {
beginShape(QUAD_STRIP);
for (i = 0, x = 0.0; i <= steps; i++, x+=in_steps) {
vertex(x, y, matrix[i][j]);
vertex(x, y + in_steps, matrix[i][j+1]);
}
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.0;
}
lights()
已经在您的代码中正确设置。
使用noStroke()
去掉线条。 noStroke
禁用绘图轮廓。
可以通过将 RGB(红色、绿色和蓝色)值传递给 fill()
来设置填充区域的颜色。
这些值是 [0, 255] 范围内的整数值。红色的 RGB 值为 (255, 0, 0),黄色的 RGB 值为 (255, 255, 0)。
可以通过
实现从红色到黄色的渐变颜色fill(255, z*255, 0);
其中 z 在 [0.0, 1.0] 中。如果 z = 0.0
结果为红色 (255, 0, 0),如果 z = 1.0
结果为黄色 (255, 255, 0)。 0.0 和 1.0 之间的所有 z 值都会导致读取和黄色之间的线性插值。
例如
for (j = 0, y = 0.0; j < steps; j++, y+=in_steps) {
beginShape(QUAD_STRIP);
noStroke(); // no lines
for (i = 0, x = 0.0; i <= steps; i++, x+=in_steps) {
fill(255, matrix[i][j] * 255, 0); // interpolate between red and yellow
vertex(x, y, matrix[i][j]);
fill(255, matrix[i][j+1] * 255, 0); // interpolate between red and yellow
vertex(x, y + in_steps, matrix[i][j+1]);
}
endShape();
}