【问题标题】:How to draw the surface of the grid in solid mode?如何在实体模式下绘制网格的表面?
【发布时间】:2019-04-08 17:29:53
【问题描述】:

我有这段代码,我应该修改它来做两件事:

  1. 使用基本照明(lights() 和独特的fill())以实体模式绘制表面和
  2. 使用两种颜色渐变绘制表面(例如,red 表示 z 的低值,yellow 表示高值) ,我被建议在每个 vertex() 之前使用 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;
}

【问题讨论】:

    标签: java image 3d processing


    【解决方案1】:

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

    【讨论】:

    • 非常感谢!我非常感谢,我一直在努力解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 2019-03-25
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多