我找到了这个 (http://lodev.org/cgtutor/raycasting.html http://lodev.org/cgtutor/raycasting.html)互联网上的教程,我很感兴趣并想自己制作。不过我想在 SFML 中做到这一点,并且我想扩展它,制作一个 3D 版本,这样玩家就可以在不同的关卡上行走。因此,每个像素都需要 1 条光线,因此每个像素都必须独立绘制。我找到了这个 (http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php)教程,并且让数组包含各个顶点似乎很容易。首先,我认为最好的办法是创建一个类,它可以读取光线返回的像素,并将它们绘制到屏幕上。我使用了 VertexArray,但由于某种原因无法正常工作。我试图找出问题所在,但收效甚微。我编写了一个简单的仅包含绿色像素的顶点数组,它应该填充屏幕的一部分,但仍然存在问题。像素仅显示我的代码和图片。我的意思是。
#include "SFML/Graphics.hpp"
int main() {
sf::RenderWindow window(sf::VideoMode(400, 240), "Test Window");
window.setFramerateLimit(30);
sf::VertexArray pointmap(sf::Points, 400 * 10);
for(register int a = 0;a < 400 * 10;a++) {
pointmap[a].position = sf::Vector2f(a % 400,a / 400);
pointmap[a].color = sf::Color::Green;
}
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(pointmap);
//</debug>
window.display();
}
return 0;
}
我的意思是只用绿色填充前 10 行,但显然这不是我所做的...我想如果我能找出导致此不起作用的原因,我可能可以解决主要问题。另外,如果您认为有更好的方法可以做到这一点,您可以让我知道:)
Thanks!
我认为你滥用了顶点数组。看看sf::Quads
教程表格中的图元:您需要定义4个点(坐标)来绘制一个四边形,一个像素只是边长为1的四边形。
所以你需要的是创建一个大小的顶点数组400*10*4
,并对接下来的四个顶点设置相同的位置。
您还可以使用 SFML 提供的另一种方法:直接逐像素绘制纹理并显示它。它可能不是最有效的方法(您必须与顶点进行比较),但它的优点是相当简单。
const unsigned int W = 400;
const unsigned int H = 10; // you can change this to full window size later
sf::UInt8* pixels = new sf::UInt8[W*H*4];
sf::Texture texture;
texture.create(W, H);
sf::Sprite sprite(texture); // needed to draw the texture on screen
// ...
for(register int i = 0; i < W*H*4; i += 4) {
pixels[i] = r; // obviously, assign the values you need here to form your color
pixels[i+1] = g;
pixels[i+2] = b;
pixels[i+3] = a;
}
texture.update(pixels);
// ...
window.draw(sprite);
The sf::Texture::update
函数接受 sf::UInt8 数组。它们代表纹理每个像素的颜色。但由于像素需要32bit RGBA
, 4 个以下sf::UInt8
是像素的 RGBA 分量。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)