我正在尝试显示Worley Noise,有人知道为什么输出看起来像这样吗?使用SFML 2.5.1

问题描述

我正在尝试根据The Coding Train的教程制作Worley噪声发生器 https://thecodingtrain.com/challenges/coding-in-the-cabana/004-worley-noise.html

但是运行程序会给我这样的输出

Text

而不是

Text

std::cout << closest << "||" << dist << '\n';输出如下:

1||50
1||51
1||51
1||52
1||53
1||54
1||55
1||34
1||34
1||34
1||34
1||34
1||34
1||34
1||34
1||34
1||34
1||34
1||35
1||35
1||35
1||36
1||36
1||36

0||23
0||23
0||23
0||22
0||22
0||22
0||22
0||22
0||22
0||22
0||22
0||22
0||22
0||22
0||22
0||22
0||23
#include <stdio.h>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <random>
#include <time.h>
#include <math.h>

// Gets the distance between two points
int pythagor(int x,int y,int x2,int y2)
{
    int xdist = abs(x2 - x);
    int ydist = abs(y2 - y);

    //std::cout << sqrt((xdist * xdist) + (ydist * ydist)) << "|";
    return sqrt((xdist * xdist) + (ydist * ydist));
}

// Finds the closest point.
int sort(sf::Vertex points[32],int x,int y)
{
    int start[32];
    for (int i = 0; i < 32; i++)
    {
        start[i] = i;
    }

    for (int i = 1; i < 33; i++)
    {
        int a = start[i-1];
        int b = start[i];
        sf::Vector2f pos = points[i-1].position;
        sf::Vector2f pos2 = points[i].position;

        int dist1 = pythagor(x,y,pos.x,pos.y);
        int dist2 = pythagor(x,pos2.x,pos2.y);

        if (dist1 > dist2)
        {
            start[i-1] = b;
            start[i] = a;
        }

        //std::cout << dist1 << "|" << dist2 << "|" << start[i-1] << "|" << start[i] << '\n' ;
    }

    return start[0];
}

int main(int argc,char const *argv[])
{
    // Variables.
    int pointRes = 32;
    int xRes = 1000,yRes = 1000;

    // Classes.
    sf::RenderWindow window(sf::VideoMode(xRes,yRes),"Whorley noise!");

    // Shapes
    sf::Vertex points[pointRes];
    sf::VertexArray points2(sf::Points,xRes*yRes);

    // Random number generator.
    std::mt19937 gen((unsigned) time(NULL)); // Establish Seed.
    std::uniform_int_distribution<> distrX(0,xRes); // Creates A Range Usabele By Gen On The X Axis.
    std::uniform_int_distribution<> distrY(0,yRes); // Creates A Range Usabele By Gen On The X Axis.

    // Generates initial points.
    for (int i = 0; i < pointRes; i++)
    {
        sf::Vector2f pos;
        pos.x = distrX(gen);
        pos.y = distrY(gen);

        points[i].position = sf::Vector2f(pos);

        //std::cout << pos.x << "|" << pos.y << "|" << i << '\n';
    }

    // Generates pixels.
    for (int i = 0; i < xRes-1; i++)
    {   
        for (int o = 0; o < yRes-1; o++)
        {
            sf::Vector2f pPos = sf::Vector2f(i,o);
            points2[((i+1) * yRes) - o].position = pPos;

            int closest = sort(points,i,o);
            int dist = pythagor(i,o,points[closest].position.x,points[closest].position.y);
            points2[(i * o) + o].color = sf::Color(dist,dist,dist);

            //std::cout << closest << "||" << dist << '\n';
        }
    }   
    
    while (window.isopen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }
        }

        window.clear();
        window.draw(points2);
        window.display();
    }

    return 0;
}

参考代码(处理中)

// Worley Noise
// Coding in the Cabana
// The Coding Train / Daniel Shiffman
// https://thecodingtrain.com/CodingInTheCabana/004-worley-noise.html
// https://youtu.be/4066MndcyCk
// p5 port: https://editor.p5js.org/codingtrain/sketches/QsiCWVczZ

PVector[] points = new PVector[100];

void setup() {
  size(400,400);
  for (int i = 0; i < points.length; i++) {
    points[i] = new PVector(random(width),random(height),random(width));
  }
}

void draw() {
  loadPixels();
  for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {

      float[] distances = new float[points.length];
      for (int i = 0; i < points.length; i++) {
        PVector v = points[i];
        float z = frameCount % width;
        float d = dist(x,z,v.x,v.y,v.z);
        distances[i] = d;
      }
      float[] sorted = sort(distances);
      float r = map(sorted[0],150,255);
      float g = map(sorted[1],50,255,0);
      float b = map(sorted[2],200,0);
      int index = x + y * width;
      pixels[index] = color(r,g,b);
    }
  }
  updatePixels();

  //noLoop();
  //for (PVector v : points) {
  //  stroke(0,0);
  //  strokeWeight(8);
  //  point(v.x,v.y);
  //  //v.x += random(-1,1);
  //  //v.y += random(-1,1);
  //}
} 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)