如何在实心模式下绘制网格表面?

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

  1. 使用基本照明(和独特的)以实体模式绘制表面,以及lights()fill()

  2. 用两种颜色渐变绘制表面(例如,红色表示z的低值,黄色表示高值)为此,我建议在每个顶点()之前使用调用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;

}


紫衣仙女
浏览 64回答 1
1回答

开心每一天1111

lights()&nbsp;已经在代码中正确设置。使用无中风()摆脱线条。 禁用绘图轮廓。noStroke填充区域的颜色可以通过将 RGB(红色、绿色和蓝色)值传递给&nbsp;fill()来设置。这些值是范围[0, 255] 中的整数值。红色的 RGB vlaue 是 (255, 0, 0),黄色的 RGB 值为 (255, 255, 0)。红色到黄色的渐变颜色可以通过以下方式实现:fill(255,&nbsp;z*255,&nbsp;0);其中 z 位于 [0.0, 1.0] 中。如果结果为红色 (255, 0, 0),如果结果为黄色 (255, 255, 0)。z 的所有值在 0.0 和 1.0 之间都会导致读取和黄色之间的线性插值。z = 0.0z = 1.0例如for (j = 0, y = 0.0; j < steps; j++, y+=in_steps) {&nbsp; &nbsp; beginShape(QUAD_STRIP);&nbsp; &nbsp; noStroke(); // no lines&nbsp; &nbsp; for (i = 0, x = 0.0; i <= steps; i++, x+=in_steps) {&nbsp; &nbsp; &nbsp; &nbsp; fill(255, matrix[i][j] * 255, 0); // interpolate between red and yellow&nbsp; &nbsp; &nbsp; &nbsp; vertex(x, y, matrix[i][j]);&nbsp; &nbsp; &nbsp; &nbsp; fill(255, matrix[i][j+1] * 255, 0); // interpolate between red and yellow&nbsp; &nbsp; &nbsp; &nbsp; vertex(x, y + in_steps, matrix[i][j+1]);&nbsp; &nbsp; }&nbsp; &nbsp; endShape();}&nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java