sfml 生成的纹理未绘制到 window
sfml generated texture is not drawn to window
我想做什么:
- 绘制一个生成的sf::Texture到一个window
预期结果:
- 一个window填充纹理(示例代码中:绿色背景)
结果:
- 空白(黑色)window
到目前为止我做了什么:
- 尝试从文件加载纹理:有效
- 尝试将生成的纹理保存到文件:有效
- 尝试在 Texture.getMaximumSize()
的范围内生成较小的纹理
- 将代码压缩到最少
我使用的工具:
- OS: Kde Neon 5.10 (基于 Ubuntu 16.04), Linux 内核 4.4
- 构建系统:qmake
- sfml 版本:2.3
示例代码(完全精简,在这种形式中没有用,但它显示了问题):
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
int main()
{
// width and height of the window
// this will not exceed max size of texture
uint width = 128;
uint height = 128;
// create new window
sf::RenderWindow * window = new sf::RenderWindow(sf::VideoMode(height,width), "Mandelbrot");
// create pixel array
vector<sf::Uint8> * pixels = new vector<sf::Uint8>(height * width * 4);
// fill array with collors;
for (uint i = 0; i < height*width; i++)
{
pixels->at(i*4 + 2) = 255;
}
// create texture
sf::Texture * texture = new sf::Texture();
texture->create(width,height);
texture->update(pixels->data());
// create sprite to hold the texture
sf::Sprite * sprite = new sf::Sprite();
sprite->setTexture(*texture);
sprite->setTextureRect(sf::IntRect(0, 0, width, height));
// draw the sprite
window->draw(*sprite);
// draw buffer to screen
window->display();
// close window on system close action
sf::Event event;
while (window->waitEvent(event))
{
if (event.type == sf::Event::Closed)
{
// close window
window->close();
return 0;
}
}
return 1;
}
我认为您没有为纹理设置所有像素,而是从 2 开始设置每四个像素一次。试试这个:
for (uint i = 0; i < height*width; i++)
{
pixels.at(i*4 + 0) = 255;
pixels.at(i*4 + 1) = 221;
pixels.at(i*4 + 2) = 255;
pixels.at(i*4 + 3) = 255;
}
您没有设置每个像素(RGBA 格式)的 alpha 通道。对于完全不透明的图像,它需要为 255:
for (uint i = 0; i < height*width; i++)
{
pixels->at(i*4 + 3) = 255; // alpha
pixels->at(i*4 + 2) = 255; // blue
}
我想做什么:
- 绘制一个生成的sf::Texture到一个window
预期结果:
- 一个window填充纹理(示例代码中:绿色背景)
结果:
- 空白(黑色)window
到目前为止我做了什么:
- 尝试从文件加载纹理:有效
- 尝试将生成的纹理保存到文件:有效
- 尝试在 Texture.getMaximumSize() 的范围内生成较小的纹理
- 将代码压缩到最少
我使用的工具:
- OS: Kde Neon 5.10 (基于 Ubuntu 16.04), Linux 内核 4.4
- 构建系统:qmake
- sfml 版本:2.3
示例代码(完全精简,在这种形式中没有用,但它显示了问题):
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
int main()
{
// width and height of the window
// this will not exceed max size of texture
uint width = 128;
uint height = 128;
// create new window
sf::RenderWindow * window = new sf::RenderWindow(sf::VideoMode(height,width), "Mandelbrot");
// create pixel array
vector<sf::Uint8> * pixels = new vector<sf::Uint8>(height * width * 4);
// fill array with collors;
for (uint i = 0; i < height*width; i++)
{
pixels->at(i*4 + 2) = 255;
}
// create texture
sf::Texture * texture = new sf::Texture();
texture->create(width,height);
texture->update(pixels->data());
// create sprite to hold the texture
sf::Sprite * sprite = new sf::Sprite();
sprite->setTexture(*texture);
sprite->setTextureRect(sf::IntRect(0, 0, width, height));
// draw the sprite
window->draw(*sprite);
// draw buffer to screen
window->display();
// close window on system close action
sf::Event event;
while (window->waitEvent(event))
{
if (event.type == sf::Event::Closed)
{
// close window
window->close();
return 0;
}
}
return 1;
}
我认为您没有为纹理设置所有像素,而是从 2 开始设置每四个像素一次。试试这个:
for (uint i = 0; i < height*width; i++)
{
pixels.at(i*4 + 0) = 255;
pixels.at(i*4 + 1) = 221;
pixels.at(i*4 + 2) = 255;
pixels.at(i*4 + 3) = 255;
}
您没有设置每个像素(RGBA 格式)的 alpha 通道。对于完全不透明的图像,它需要为 255:
for (uint i = 0; i < height*width; i++)
{
pixels->at(i*4 + 3) = 255; // alpha
pixels->at(i*4 + 2) = 255; // blue
}