void HeightMapEditor::onRenderButtonClicked() { { boost::recursive_mutex::scoped_lock guard(myMutex); for (auto task : myTasks) task->cancellationFlag.store(true); myTasks.clear(); } myUi->renderView->scene()->clear(); Json::Value graphJson = myUi->graphBuilder->toJson(); myGraph.reset(new NoiseGraph); PhysFS::ifstream fs("map/heightmap.png"); std::vector<char> data; data.assign(std::istreambuf_iterator<char>(fs), std::istreambuf_iterator<char>()); QImage heightmapImage; heightmapImage.loadFromData((const uchar *)data.data(), data.size()); boost::multi_array<double, 2> heights; heights.resize(boost::extents[heightmapImage.width()][heightmapImage.height()]); for (int y = 0; y < heightmapImage.height(); y++) for (int x = 0; x < heightmapImage.width(); x++) { QColor col(heightmapImage.pixel(x, y)); int height = col.red() | col.green() | col.blue(); heights[x][y] = (height / 256.0) * 2 - 1; } std::shared_ptr<HeightMapModule> heightmap(new HeightMapModule(heights, 400, 240)); try { myGraph->module = buildNoiseGraph(graphJson, myGraph->modules, myGraph->defs, heightmap, 0); } catch (std::exception & e) { QMessageBox::critical(this, "Error", e.what()); myGraph.reset(); return; } catch (noise::ExceptionInvalidParam & e) { QMessageBox::critical(this, "Error", "Invalid parameter"); myGraph.reset(); return; } catch (noise::ExceptionNoModule & e) { QMessageBox::critical(this, "Error", "Missing module"); myGraph.reset(); return; } catch (noise::ExceptionOutOfMemory & e) { QMessageBox::critical(this, "Error", "Out of memory"); myGraph.reset(); return; } catch (noise::ExceptionUnknown & e) { QMessageBox::critical(this, "Error", "Unknown exception"); myGraph.reset(); return; } generateRect(myUi->renderView->sceneRect()); }
void HeightMapEditor::onRenderButtonClicked() { Json::Value graphJson = myUi->graphBuilder->toJson(); std::cerr << graphJson.toStyledString() << std::endl; std::vector<std::shared_ptr<noise::module::Module>> modules; std::map<std::string, std::shared_ptr<noise::module::Module>> defs; PhysFS::ifstream fs("map/heightmap.png"); std::vector<char> data; data.assign(std::istreambuf_iterator<char>(fs), std::istreambuf_iterator<char>()); QImage heightmapImage; heightmapImage.loadFromData((const uchar *)data.data(), data.size()); boost::multi_array<double, 2> heights; heights.resize(boost::extents[heightmapImage.width()][heightmapImage.height()]); for (int y = 0; y < heightmapImage.height(); y++) for (int x = 0; x < heightmapImage.width(); x++) { QColor col(heightmapImage.pixel(x, y)); int height = col.red() | col.green() | col.blue(); heights[x][y] = (height / 256.0) * 2 - 1; } std::shared_ptr<HeightMapModule> heightmap(new HeightMapModule(heights, 400, 240)); std::shared_ptr<noise::module::Module> graph = buildNoiseGraph(graphJson, modules, defs, heightmap, 0); QImage img(400 * 4, 240*4, QImage::Format::Format_ARGB32); for (int y = 0; y < img.height(); y++) for (int x = 0; x < img.width(); x++) { double value = graph->GetValue((double)x, (double)y, 0.0); double height = (value + 1.0) * 0.5; // std::cerr << value << std::endl; if (height < 0) height = 0; if (height > 1) height = 1; QColor col (height * 255.0, height * 255.0, height * 255.0); img.setPixel(x, y, col.rgb()); } myUi->renderView->scene()->clear(); myUi->renderView->scene()->addPixmap(QPixmap::fromImage(img, Qt::ColorOnly)); }