Rear mirror rendering, how?

Started by
4 comments, last by HaywireGuy 18 years, 11 months ago
Good day everyone, I have a way to do rear mirror rendering found in most racing games, but I am not sure if this is the "right" way to go about it. These are the steps in each game loop iteration:     LPDIRECT3DDEVICE9  m_lpDevice;     LPDIRECT3DTEXTURE9 m_lpMirrorTarget;     LPDIRECT3DSURFACE9 lpMirrorSurface;     LPDIRECT3DSURFACE9 lpCurrSurface; 1. Obtain lpMirrorSurface from m_lpMirrorTarget. 2. m_lpDevice->GetRenderTarget(0, &lpCurrSurface); 3. m_lpDevice->SetRenderTarget(0, lpMirrorSurface); 4. Set up rendering states/view matrices, etc. 5. Render scene (back viewing). 6. lpCurrSurface->Release(); Restore reference count. 7. m_lpDevice->SetRenderTarget(0, lpCurrSurface); Restoring original render target. 8. Restore original rendering states/view matrices, etc. 9. lpMirrorSurface->Release(); Restore reference count. 10. Render m_lpMirrorTarget texture on a quad (mirror object) on screen. 11. Done! :) So what you guys think? Is that feasibile to do? Will there be serious penalty by setting different render targets each frame? Thanks in advance for any help I'm getting!
Advertisement
Other then the fact that you probally don't want to be creating and releasing your surfaces every frame (why not keep them around) it seems like a good choice
You could also do something with the stencil buffer... like render the rear-view mirror (or whatever you are using) to the stencil buffer, then render the back scene so that it only gets drawn on the backbuffer where the mirror is. But, a quad is probably more flexible because you can rotate it and all...

Thanks Arelius, you're right, moving unnecessary getting/releasing of both
IDirect3DSurface9 surfaces out of the game loop would be a wiser choice. And
the partial game loop would look something like this:


LPDIRECT3DDEVICE9 m_lpDevice;
LPDIRECT3DTEXTURE9 m_lpMirrorTarget;
LPDIRECT3DSURFACE9 m_lpMirrorSurface;
LPDIRECT3DSURFACE9 m_lpOrgnlSurface;


One time tasks:
1. Obtain m_lpMirrorSurface from m_lpMirrorTarget.
2. m_lpDevice->GetRenderTarget(0, &m_lpOrgnlSurface);

Game loop tasks:
1. m_lpDevice->SetRenderTarget(0, m_lpMirrorSurface);
2. Set up rendering states/view matrices, etc.
3. Render scene (back viewing).
4. m_lpDevice->SetRenderTarget(0, m_lpOrgnlSurface); Restoring original render target.
5. Restore original rendering states/view matrices, etc.
6. Render m_lpMirrorTarget texture on a quad (mirror object) on screen.

Game exit, one time clean up tasks:
1. Release relevant resources.

Now that sounds more like it.
So any other things I should be aware of?


Kosmon_x, the stencil buffer method is another alternative I am sure, I might
consider using it if necessary (i.e. if the first method does not work well).
Thanks for your help anyway.
Seems to me like you've got a fairly sound algorithm there. A couple of notes:

1. In games I have they often use quite low-resolution render targets and make up for it by using texture filtering. Doing this can help to reduce the fill-rate overhead of effectively rendering the scene from 2 directions.

2. I've also seen in games a lot of cases where they reduce the geometric/effect complexity in mirrors. Can't think of the name of the game, but there was one that I used to play that would never show tyre smoke / dirt / tyre tracks etc... and on low detail would show only the simplest lighting (no specular/per-pixel stuff).

Both cases are performance hacks that, in truth, reduce the overall quality - but for the majority of cases it's not something a player is likely to be too bothered about. You could always leave a slider in to determine how much is dropped for reflections I suppose...

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Thanks Jollyjeffers, I agree with you cases like these would not make that much
difference to the user. In fact I am not making a rear mirror, I just said that
so it's easier for people to relate things.

I am using a 64x64 texture and rendering some patterns on it. After this
texture is being rendered with patterns, I'll use it on a quad that covers the
entire screen. So you see the idea, I am using it as a way to achieve
visualization for my game menu background (animated). Somethin' like the one
you can find on XBox 360 menu. Thanks anyway for all the inputs!

This topic is closed to new replies.

Advertisement