我正在编写一个能够在屏幕上绘制像素的框架。但是现在我正在尝试更新屏幕,前 4 个像素显示随机颜色。
当我只是将一个指向图像数据的指针发送到我的窗口模块时,我没有遇到这个问题,但是现在我将一个绘图函数作为闭包传递,这开始发生了。我不认为这是我的绘图功能中的问题,因为我写了这些像素并且它们仍然被修改。
我的主要绘图功能:
// Define drawing function
fn rand_pixel(image_width: usize, image_height: usize) -> *const std::ffi::c_void
{
// Define colors
let black_pixel = image::Color { red: 0, green: 0, blue: 0 };
let white_pixel = image::Color { red: 255, green: 255, blue: 255 };
// Create blank image
let mut pixels: image::Image = image::Image::new(image_width, image_height, black_pixel);
// Get two random numbers
use rand::Rng;
let mut rng = rand::thread_rng();
let rand_x = rng.gen::<usize>() % image_width;
let rand_y = rng.gen::<usize>() % image_height;
// Change pixels
pixels.set_pixel(rand_x, rand_y, white_pixel);
// return pointer
pixels.get_ptr()
}
我的窗口绘图例程:
// Execute draw function
let image_ptr = draw_function();
unsafe
{
// Create texture
let mut tex_id: gl::types::GLuint = 0;
gl::GenTextures(1, &mut tex_id);
gl::BindTexture(gl::TEXTURE_2D, tex_id);
// Move pixels to texture
gl::TexImage2D(gl::TEXTURE_2D, 0, gl::RGBA as i32,
image_width, image_height, 0, gl::RGBA, gl::UNSIGNED_BYTE, image_ptr);
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::NEAREST as i32);
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::NEAREST as i32);
// Create read framebuffer
let mut read_fbo_id: gl::types::GLuint = 0;
gl::GenFramebuffers(1, &mut read_fbo_id);
gl::BindFramebuffer(gl::READ_FRAMEBUFFER, read_fbo_id);
// Bind texture to read framebuffer
gl::FramebufferTexture2D(gl::READ_FRAMEBUFFER, gl::COLOR_ATTACHMENT0, gl::TEXTURE_2D, tex_id, 0);
// Transfer from read framebuffer to draw framebuffer
gl::BlitFramebuffer(0, 0, image_width, image_height,
0, 0, window_width as i32, window_height as i32,
gl::COLOR_BUFFER_BIT, gl::NEAREST);
// Clear out read framebuffer
gl::BindFramebuffer(gl::READ_FRAMEBUFFER, 0);
gl::DeleteFramebuffers(1, &mut read_fbo_id);
};
// Update screen
let _ = windowed_context.swap_buffers();
std::thread::sleep(std::time::Duration::from_millis(15));
可以找到完整的项目: https ://github.com/Pandabear314/sam_graphics
我期待一个黑屏,每帧在屏幕上随机绘制一个白色像素。然而,左下角包括 4 个随机像素。第一个和第三个像素发生变化,而第二个和第四个像素不变。
显示结果的图像:http: //prntscr.com/nwl2gk