3

I found this (http://lodev.org/cgtutor/raycasting.html) tutorial on the Internet and was interested and wanted to make my own. I wanted to do it in SFML though, and I wanted to extend it, and make a 3D version, so there could be different levels the player can walk on. Thus, you would need 1 ray for every pixel, and thus each pixel would have to be drawn independently. I found this (http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php) tutorial, and it seemed easy enough to have the array be of individual vertices. To start, I figured the best thing to do would be to create a class that could read the pixels returned by the rays, and draw them to the screen. I used the VertexArray, but things were not working for some reason. I tried to isolate the problem, but I've had little success. I wrote a simple vertex array of just green pixels that should fill up part of the screen, and still there are problems. The pixels only show my code and the pic. of what I mean.

#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;

}

http://oi43.tinypic.com/s5d9aw.jpg

I meant for this to just fill in the top 10 rows with Green, but apparently that is not what I did... I think if I can figure out what is causing this not to work, I can probably fix the main problem. Also if you think there is a better way to do this instead, you could let me know :)

Thanks!

4

2 回答 2

6

我认为您滥用了顶点数组。看一下sf::Quads教程表格中的图元:您需要定义 4 个点(坐标)来绘制一个四边形,一个像素只是边长为 1 的四边形。

因此,您需要创建一个 size 的顶点数组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);

sf::Texture::update函数接受一个 sf::UInt8 数组。它们代表纹理每个像素的颜色。但由于像素需要32bit RGBA,以下4个sf::UInt8是像素的RGBA组合。

于 2013-09-15T10:45:44.247 回答
0

换行:

pointmap[a].position = sf::Vector2f(a % 400,a / 400);

和:

pointmap[a].position = sf::Vector2f(a % 400,(a/400) % 400);
于 2018-08-17T07:44:19.410 回答