Rust 当我不';每次渲染屏幕时,是否调用`graphics::clear`?
考虑一下,以及它们之间的区别:Rust 当我不';每次渲染屏幕时,是否调用`graphics::clear`?,rust,rust-piston,Rust,Rust Piston,考虑一下,以及它们之间的区别: $diff flash/src/main.rs不闪烁/src/main.rs 22,23c22 < 让mut cursor_poses:Vec=Vec::new(); 28c27 光标摆姿势。推((x,y)); 32,33c31,33 对于光标_poses.iter()中的&(x,y){ >绘制光标位置([x,y],&c,g); 35,36d34
$diff flash/src/main.rs不闪烁/src/main.rs
22,23c22
<
<设mut i=0;
---
>让mut cursor_poses:Vec=Vec::new();
28c27
光标摆姿势。推((x,y));
32,33c31,33
图形:清晰([1.0;4],g);
>对于光标_poses.iter()中的&(x,y){
>绘制光标位置([x,y],&c,g);
35,36d34
该程序是一个非常基本的绘图程序,只有一个画笔宽度、画笔笔划颜色、画布大小、不保存等;哦,要停止绘图,请将鼠标移出窗口,因为每次在窗口上移动都算作绘图;-)
闪烁。rs
不会在每次到达e.render_args()
时绘制每个像素,但第一次除外。不会闪烁。rs
会在每次到达e.render_args()
时绘制每个像素。这是两个程序之间的唯一区别
虽然在这个程序中生成内容不需要很长时间,所以当鼠标在窗口上移动时,数百次重新生成内容是可以接受的,但这似乎效率低下。理论上,随着越来越多的点被添加到屏幕上,每次迭代gl.draw
都需要越来越长的时间。实际上,callinggraphics::ellipse
一次与一万次在现代硬件上并不重要
我想写的其他程序不会有这样的奢侈,因为生成结果放在屏幕上需要更长的时间
在仔细阅读API时,我没有想到一个明显的方法,就是“什么都不做”。我假设我必须将屏幕更改写入某个缓冲区对象,然后在调用e.render_args()
但我不需要更新屏幕的情况下,将GlGraphics
反馈回该缓冲区对象
问题是,我似乎找不到这个缓冲区对象:-(
如果我的理论是正确的,我怎样才能在没有任何新的东西可以绘制时,将缓冲区返回到屏幕,而不是屏幕
Cargo.toml
[package]
name = "stackoverflow-piston-example"
version = "0.0.0"
authors = ["Fred"]
description = "Note: This program can be used for both of the programs below. Simply use `cargo new` and save either of the below files as `src/main.rs`"
keywords = []
[dependencies]
piston = "0.35.0"
piston2d-opengl_graphics = "0.50.0"
piston2d-graphics = "0.24.0"
piston2d-touch_visualizer = "0.8.0"
pistoncore-sdl2_window = "0.47.0"
不闪烁。rs
extern crate piston;
extern crate opengl_graphics;
extern crate graphics;
extern crate touch_visualizer;
extern crate sdl2_window;
use opengl_graphics::{ GlGraphics, OpenGL };
use graphics::{ Context, Graphics };
use piston::input::*;
use piston::event_loop::*;
use sdl2_window::Sdl2Window as AppWindow;
static CURSOR_POS_COLOR: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
fn main() {
let opengl = OpenGL::V3_2;
let mut window: AppWindow = piston::window::WindowSettings::new("Example for StackOverflow", [600, 600])
.exit_on_esc(true).opengl(opengl).build().unwrap();
let ref mut gl = GlGraphics::new(opengl);
let (mut mx, mut my) = (0., 0.);
let mut cursor_poses: Vec<(f64, f64)> = Vec::new();
let mut events = Events::new(EventSettings::new().lazy(true));
while let Some(e) = events.next(&mut window) {
e.mouse_cursor(|x, y| {
cursor_poses.push((x,y));
});
if let Some(args) = e.render_args() {
gl.draw(args.viewport(), |c, g| {
graphics::clear([1.0; 4], g);
for &(x, y) in cursor_poses.iter() {
draw_cursor_pos([x, y], &c, g);
}
}
);
}
}
}
fn draw_cursor_pos<G: Graphics>(
cursor: [f64; 2],
c: &Context,
g: &mut G,
) {
graphics::ellipse(
CURSOR_POS_COLOR,
graphics::ellipse::circle(cursor[0], cursor[1], 4.0),
c.transform,
g
);
}
extern crate piston;
extern crate opengl_graphics;
extern crate graphics;
extern crate touch_visualizer;
extern crate sdl2_window;
use opengl_graphics::{ GlGraphics, OpenGL };
use graphics::{ Context, Graphics };
use piston::input::*;
use piston::event_loop::*;
use sdl2_window::Sdl2Window as AppWindow;
static CURSOR_POS_COLOR: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
fn main() {
let opengl = OpenGL::V3_2;
let mut window: AppWindow = piston::window::WindowSettings::new("Example for StackOverflow", [600, 600])
.exit_on_esc(true).opengl(opengl).build().unwrap();
let ref mut gl = GlGraphics::new(opengl);
let (mut mx, mut my) = (0., 0.);
let mut i = 0;
let mut events = Events::new(EventSettings::new().lazy(true));
while let Some(e) = events.next(&mut window) {
e.mouse_cursor(|x, y| {
mx = x; my = y;
});
if let Some(args) = e.render_args() {
gl.draw(args.viewport(), |c, g| {
if i == 0 {
graphics::clear([1.0; 4], g);
}
draw_cursor_pos([mx, my], &c, g);
i+=1;
}
);
}
}
}
fn draw_cursor_pos<G: Graphics>(
cursor: [f64; 2],
c: &Context,
g: &mut G,
) {
graphics::ellipse(
CURSOR_POS_COLOR,
graphics::ellipse::circle(cursor[0], cursor[1], 4.0),
c.transform,
g
);
}
外部板条箱活塞;
外部板条箱opengl_图形;
外部板条箱图形;
外部板条箱触摸式可视化仪;
外部板条箱sdl2_窗口;
使用opengl_图形:{GlGraphics,opengl};
使用图形::{Context,graphics};
使用活塞::输入::*;
使用活塞::事件_循环::*;
使用sdl2_window::Sdl2Window作为AppWindow;
静态光标位置颜色:[f32;4]=[0.0,0.0,0.0,1.0];
fn main(){
让opengl=opengl::V3_2;
let mut window:AppWindow=活塞::window::WindowSettings::new(“StackOverflow的示例,[600600])
.exit_on_esc(true).opengl(opengl.build().unwrap();
让ref mut gl=GlGraphics::new(opengl);
设(mutmx,mutmy)=(0,0.);
让mut cursor_poses:Vec=Vec::new();
让mut events=events::new(EventSettings::new().lazy(true));
而让一些(e)=事件。下一步(&mut窗口){
e、 鼠标光标(x,y){
光标摆姿势。推((x,y));
});
如果让一些(args)=e.render_args(){
总图绘制(args.viewport(),|c,g|{
图形:清晰([1.0;4],g);
对于光标_poses.iter()中的&(x,y){
绘制光标位置([x,y],&c,g);
}
}
);
}
}
}
fn绘制光标位置(
光标:[f64;2],
c:上下文(&c),
g:&mut g,
) {
图形:椭圆(
光标位置颜色,
图形::椭圆::圆(光标[0],光标[1],4.0),
c、 转化,,
G
);
}
闪烁。rs
extern crate piston;
extern crate opengl_graphics;
extern crate graphics;
extern crate touch_visualizer;
extern crate sdl2_window;
use opengl_graphics::{ GlGraphics, OpenGL };
use graphics::{ Context, Graphics };
use piston::input::*;
use piston::event_loop::*;
use sdl2_window::Sdl2Window as AppWindow;
static CURSOR_POS_COLOR: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
fn main() {
let opengl = OpenGL::V3_2;
let mut window: AppWindow = piston::window::WindowSettings::new("Example for StackOverflow", [600, 600])
.exit_on_esc(true).opengl(opengl).build().unwrap();
let ref mut gl = GlGraphics::new(opengl);
let (mut mx, mut my) = (0., 0.);
let mut cursor_poses: Vec<(f64, f64)> = Vec::new();
let mut events = Events::new(EventSettings::new().lazy(true));
while let Some(e) = events.next(&mut window) {
e.mouse_cursor(|x, y| {
cursor_poses.push((x,y));
});
if let Some(args) = e.render_args() {
gl.draw(args.viewport(), |c, g| {
graphics::clear([1.0; 4], g);
for &(x, y) in cursor_poses.iter() {
draw_cursor_pos([x, y], &c, g);
}
}
);
}
}
}
fn draw_cursor_pos<G: Graphics>(
cursor: [f64; 2],
c: &Context,
g: &mut G,
) {
graphics::ellipse(
CURSOR_POS_COLOR,
graphics::ellipse::circle(cursor[0], cursor[1], 4.0),
c.transform,
g
);
}
extern crate piston;
extern crate opengl_graphics;
extern crate graphics;
extern crate touch_visualizer;
extern crate sdl2_window;
use opengl_graphics::{ GlGraphics, OpenGL };
use graphics::{ Context, Graphics };
use piston::input::*;
use piston::event_loop::*;
use sdl2_window::Sdl2Window as AppWindow;
static CURSOR_POS_COLOR: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
fn main() {
let opengl = OpenGL::V3_2;
let mut window: AppWindow = piston::window::WindowSettings::new("Example for StackOverflow", [600, 600])
.exit_on_esc(true).opengl(opengl).build().unwrap();
let ref mut gl = GlGraphics::new(opengl);
let (mut mx, mut my) = (0., 0.);
let mut i = 0;
let mut events = Events::new(EventSettings::new().lazy(true));
while let Some(e) = events.next(&mut window) {
e.mouse_cursor(|x, y| {
mx = x; my = y;
});
if let Some(args) = e.render_args() {
gl.draw(args.viewport(), |c, g| {
if i == 0 {
graphics::clear([1.0; 4], g);
}
draw_cursor_pos([mx, my], &c, g);
i+=1;
}
);
}
}
}
fn draw_cursor_pos<G: Graphics>(
cursor: [f64; 2],
c: &Context,
g: &mut G,
) {
graphics::ellipse(
CURSOR_POS_COLOR,
graphics::ellipse::circle(cursor[0], cursor[1], 4.0),
c.transform,
g
);
}
外部板条箱活塞;
外部板条箱opengl_图形;
外部板条箱图形;
外部板条箱触摸式可视化仪;
外部板条箱sdl2_窗口;
使用opengl_图形:{GlGraphics,opengl};
使用图形::{Context,graphics};
使用活塞::输入::*;
使用活塞::事件_循环::*;
使用sdl2_window::Sdl2Window作为AppWindow;
静态光标位置颜色:[f32;4]=[0.0,0.0,0.0,1.0];
fn main(){
让opengl=opengl::V3_2;
let mut window:AppWindow=活塞::window::WindowSettings::new(“StackOverflow的示例,[600600])
.exit_on_esc(true).opengl(opengl.build().unwrap();
让ref mut gl=GlGraphics::new(opengl);
设(mutmx,mutmy)=(0,0.);
设muti=0;
让mut events=events::new(EventSettings::new().lazy(true));
而让一些(e)=事件。下一步(&mut窗口){
e、 鼠标光标(x,y){
mx=x;my=y;
});
如果让一些(args)=e.render_args(){
总图绘制(args.viewport(),|c,g|{
如果i==0{
图形:清晰([1.0;4],g);
}
绘制光标位置([mx,my],&c,g);
i+=1;
}
);
}
}
}
fn绘制光标位置(
光标:[f64;2],
c:上下文(&c),
g:&mut g,
) {
图形:椭圆(
光标位置颜色,
图形::椭圆::圆(光标[0],光标[1],4.0),
c、 转化,,
G
);
}
我认为闪烁是由缓冲区交换引起的:in闪烁。rs
只清除第一个要进入的缓冲区。第二个缓冲区将为全零,或者如果你运气不好,则为剩余的gpu内存。根据OpenGL,调用graphics::clear
:
现代OpenGL程序应始终使用双缓冲。
应该始终清除缓冲区。在旧得多的硬件上