问题
i have to develop an application in wich the final step is to export a video (or a single frame) of a 3D animation generated by my software calculating the user input parameters.
I want to use XNA and for this. i need that the software can export FIXED FPS video (or also all single frames of the video separately). It's not a matter the LIVE FPS. I don't need to view on the screen the frames at a fixed fps. As the animation could be very complex, i could accept if the software take 1 minute for each frame.
The important is that i can see the frame while it render and that is not skipped any frame. eg. if the video is 1 minute long, it have to export 24 frames at 24fps also if it will take 20secs to render each frame. After rendered the first frame (so after 20sec) it haven't to render the frame at 21sec. it have to render the frame [2/24 of the first minute]
How can i obtain this?
Thanks!
回答1:
Here is the method for doing this for XNA 4.0, described in code for your Game
class (because it's easy for me):
protected override void Update(GameTime gameTime)
{
// Do Nothing!
}
void RealUpdate()
{
const float deltaTime = 1f/60f; // Fixed 60 FPS
// Update your scene here
}
RenderTarget2D screenshot;
protected override void LoadContent()
{
screenshot = new RenderTarget2D(GraphicsDevice, width, height, false, SurfaceFormat.Color, null);
}
protected override void UnloadContent()
{
screenshot.Dispose();
}
int i = 0;
protected override void Draw(GameTime)
{
RealUpdate(); // Do the update once before drawing each frame.
GraphicsDevice.SetRenderTarget(screenshot); // rendering to the render target
//
// Render your scene here
//
GraphicsDevice.SetRenderTarget(null); // finished with render target
using(FileStream fs = new FileStream(@"screenshot"+(i++)+@".png", FileMode.OpenOrCreate)
{
screenshot.SaveAsPng(fs, width, height); // save render target to disk
}
// Optionally you could render your render target to the screen as well so you can see the result!
if(done)
Exit();
}
Note: I wrote this without compiling or testing - so there might be a minor error or two.
来源:https://stackoverflow.com/questions/3616232/xna-to-render-3d-video-fixed-fps