Jump to content
  • Advertisement
Sign in to follow this  
justin12343

Problem with 2D Camera

This topic is 2557 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using D3DXSprite to draw my sprites, but they are not affected by SetWorldViewLH for some reason. it doesn't work even with D3DXSPRITE_OBJECTSPACE in the begin function.

Here is my drawing code:
[source lang = "cpp"]
void DX9RenderEngine::draw_sprite( int index, int frame, float x, float y)
{
Sprite *tmp = get_sprite( index);
LPD3DXSPRITE spr = tmp->sprite;
RECT *rect = NULL;

if (tmp->FrameCount > 1)
{
int subImage = (int)floor( (float)frame);
rect->left = (subImage % (tmp->ImageWidth/tmp->FrameWidth) * tmp->FrameWidth);
rect->top = (subImage / (tmp->ImageHeight / tmp->FrameHeight) * tmp->FrameHeight);
rect->right = rect->left + tmp->FrameHeight;
rect->bottom = rect->top + tmp->FrameWidth;
}

D3DXMATRIX trans;
D3DXVECTOR2 pos( x, y);

D3DXMatrixTransformation2D( &trans, &D3DXVECTOR2( tmp->CenterX, tmp->CenterY), NULL, NULL, NULL, 0, &pos);
//D3DXMatrixMultiply( &trans, &trans, &camera->matView);

spr->SetWorldViewLH( NULL, &camera->matView);

spr->Begin( D3DXSPRITE_ALPHABLEND || D3DXSPRITE_OBJECTSPACE );
spr->SetTransform( &trans);
spr->Draw( tmp->texture, rect, NULL, NULL, D3DXCOLOR( 1, 1, 1, 1));
spr->End();
}
[/source]

Camera code:
[source lang = "cpp"]
//Setup orthographic projection matrix
float halfWidth = float(get_width()/2.0f);
float halfHeight = float(get_height()/2.0f);

float cameraZoom = get_zoom();

D3DXMatrixIdentity (&matView);
D3DXMatrixIdentity (&matProj);

D3DXMatrixOrthoOffCenterLH(&this->matProj,
-halfWidth / (float)cameraZoom , halfWidth / (float)cameraZoom,
halfHeight / (float)cameraZoom, -halfHeight / (float)cameraZoom,
get_max_zoom(), get_min_zoom());

lpD3dDevice9->SetTransform(D3DTS_PROJECTION, &this->matProj);

//set the camera's view matrix
D3DXVECTOR3 eye(this->get_position().x, this->get_position().y, (float)-cameraZoom);
D3DXVECTOR3 at(eye.x, eye.y, get_max_zoom());

D3DXMatrixLookAtLH(&this->matView, &eye, &at, &D3DXVECTOR3(0.0f,1.0f,0.0f));

lpD3dDevice9->SetTransform(D3DTS_VIEW, &this->matView);
[/source]

The only way to get it to move is to use D3DXMatrixMultiply but I have a zooming camera so that doesn't work unless the zoom is set to 1. So I'm stuck here and can't figure out what to do.


How do I get this to work?

Share this post


Link to post
Share on other sites
Advertisement
It's because Id3DXSprite coordinates are pre transformed to screen coordinates. As far as I know there is no way around this. If you want this kind of control you're going to have to use billboard style quads. Which is alot easier that you might think.

Search for "billboard" or feel free to ask another question.

Share this post


Link to post
Share on other sites
So that means I'll just have to use a vertex buffer and render quads, correct? If thats the case, when rendering the quads do I have to use a separate vertex buffer for each sprite, do I just need to use one vertex buffer for every sprite. I ask this because if all of the sprites were stored in one image this would be a "no brainer" because i'd just be setting texture coordinates, but I'm loading tiles from a map file that are not stored in one big PNG image. So if I use one big vertex buffer, how would I apply the intended texture to each quad?

Share this post


Link to post
Share on other sites

So that means I'll just have to use a vertex buffer and render quads, correct? If thats the case, when rendering the quads do I have to use a separate vertex buffer for each sprite, do I just need to use one vertex buffer for every sprite. I ask this because if all of the sprites were stored in one image this would be a "no brainer" because i'd just be setting texture coordinates, but I'm loading tiles from a map file that are not stored in one big PNG image. So if I use one big vertex buffer, how would I apply the intended texture to each quad?


The way I do it is with a dynamic vertex buffer. I store every drawcall using my own custom struct that stores what I'm drawing, where, and how. At time of rendering I sort the calls by texture, effect or whatever else might need to change. I then fill the vertex buffer, and pass it off to the gpu. Then rinse and repeat for every chunk of quads. Sure this might not be the fastest way to do it it's the easiest.

You could also look into instancing, or even generate your maps just like 3D terrain, but think of it as a flat terrain. Then you don't have to keep rebuilding the vertex buffer every frame. There is a lot of things that you can do just like you would in 3D.

Share this post


Link to post
Share on other sites
I decided not to use a vertex buffer do what d3dxsprite already does, so I changed the way I was drawing my sprites. I'm drawing my sprites in batches and now SetWorldViewLH and D3DXSPRITE_OBJECTSPACE seem to working because nothing appears on screen. Is my view matrix correct or is there some other issue?

Share this post


Link to post
Share on other sites

Is my view matrix correct or is there some other issue?


is your lighting enabled? may be why.
May seem daft to ask but ive had that problem so many times xD

Share this post


Link to post
Share on other sites
I won't see anything lighting enabled because I not using any lights in my scene. When i remove D3DXSPRITE_OBJECTSPACE it draws but the view matrix isn't applied.

Share this post


Link to post
Share on other sites
I got it working now. It was failing because I was calling it after the begin function. I saw the sprites when I was playing around with the zoom.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!