簡體   English   中英

SFML繪圖像素陣列

[英]SFML Drawing Pixel Array

我在互聯網上找到了這個( http://lodev.org/cgtutor/raycasting.html )教程,並且很感興趣並希望自己創建。 我想在SFML中做到這一點,我想擴展它,並制作一個3D版本,所以玩家可以走的不同級別。 因此,每個像素需要1條光線,因此必須獨立繪制每個像素。 我發現了這個( 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;

}

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

我的意思是用綠色來填充前10行,但顯然這不是我所做的......我想如果我能弄清楚是什么導致這不起作用,我可以解決主要問題。 另外如果您認為有更好的方法可以做到這一點,您可以讓我知道:)

謝謝!

我認為你誤用了頂點數組。 看看教程表中的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);

sf::Texture::update函數接受一個sf :: UInt8數組。 它們代表紋理的每個像素的顏色。 但由於像素需要為32bit RGBA ,因此sf::UInt8之后的4是像素的RGBA合成物。

替換線:

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

附:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM