【问题标题】:Rotate texture sfml and save output to file旋转纹理 sfml 并将输出保存到文件
【发布时间】:2016-02-13 22:30:02
【问题描述】:

我想首先说我是一个完整的 C++ 新手。我不知道任何与之相关的词汇,所以如果这个问题是微不足道的,我很抱歉,因为我现在没有正确的术语或短语来自己搜索解决方案。

目前正在使用我在 github 上找到的这段代码来从我的 3ds 中捕获游戏玩法。 https://github.com/Gotos/Cute3DSCapture/blob/master/main.cpp

我正在尝试添加旋转图像并将其保存到 png 文件的功能。

来自main.cpp 第 267 行左右,我正在尝试添加以下功能。

sprite.setTexture(texture);
sprite.rotate(90);
texture = sprite.getTexture();
texture.copyToImage().saveToFile("Something/Place/img.png");

当前纹理和精灵定义如下。

sf::Texture texture;
sf::Sprite sprite;

当我尝试构建和运行时,我得到以下信息

main.cpp:269:25: error: no viable overloaded '='
                texture = sprite.getTexture();
                ~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~
/usr/local/include/SFML/Graphics/Texture.hpp:421:14: note: candidate function
      not viable: no known conversion from 'const sf::Texture *' to
      'const sf::Texture' for 1st argument; dereference the argument with *
    Texture& operator =(const Texture& right);

任何帮助将不胜感激。

【问题讨论】:

  • 可以直接试试sprite.getTexture()->copyToImage().saveToFile("Something/Place/img.png");
  • @MiMo 这只会生成原始图像的副本,而不是修改后的版本。有关详细信息,请参阅下面的答案。

标签: c++ opengl sfml


【解决方案1】:

sf::Sprite不是纹理操纵器,这意味着sf::Sprite::getTexture()返回的纹理不会被修改。这就是 Sprite 的 ctor 引用 const 纹理实例的原因。

如果您只对图像处理感兴趣,我建议您使用 SFML 以外的其他工具,因为您可能会为特定操作获得更好的功能/性能。可能类似于 imagemagick。

使用 SFML,您可以大致如下进行:

  • 创建一个所需大小的RenderTexture - 将其视为一个虚拟窗口,当.display() 被调用时,您可以访问它的纹理。
  • 使用初始纹理和一些操作(例如旋转)创建精灵——确保渲染纹理的大小正确设置为“显示”生成的图像
  • 在渲染纹理上绘制您的精灵并在其上调用display()
  • 然后你就可以调用sf::RenderTexture::getTexture() 和链copyToImage()saveToFile()

sf::RenderTexture的详细说明使用示例。

【讨论】:

  • 谢谢,我删除了我的答案。我必须承认我几乎没有使用过 SFML,也没有阅读过sf::Sprite 文档。在我看来,OP 在处理图像处理之前需要学习更多基础知识!
  • 谢谢,我没有一般的指针或参考经验。我最初使用 SFML 是因为这是我在网上找到的与我的 3ds 捕获软件兼容并且能够快速输出 iamge 的源代码。我更熟悉 OpenCV 并且能够在那里操作图像。 docs.opencv.org/java/2.4.2/org/opencv/core/Core.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-15
  • 2019-03-26
  • 1970-01-01
  • 2017-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多