   [DX10] Sprites?
  Hello everyone,I've been trying to use sprites, but i don't seem to figure out why this code doesn't work as it should:////_SpriteV is a vector of a structure, and sp is a member of that structure, that countains the D3DX10_SPRITE structure//D3DXMatrixTransformation2D ( &_SpriteV[0].sp.matWorld,&D3DXVECTOR2 ( 0.0f, 0.0f ), 0.0f,&D3DXVECTOR2 ( 1.0f, 1.0f ),&D3DXVECTOR2 ( 0.0f, 0.0f ),0.0f,&D3DXVECTOR2 ( 0.0f, 0.0f ) );_mainSprite->Begin ( D3DX10_SPRITE_SORT_TEXTURE );D3DX10_SPRITE* spArr = new D3DX10_SPRITE[1];spArr[0] = _SpriteV[0].sp;_mainSprite->DrawSpritesBuffered ( spArr, 1 );_mainSprite->End ( );shouldn't this draw the sprite at 0,0? well it draws it at the center always, changing the last parameter of D3DXMatrixTransformation2D to D3DXVECTOR2 ( 1.0f, 0.0f ) draws it at the top-center of the screen, i don't even know why changing the x impact the y position..So what's wrong there? i can't find any tutorials for d3d10 2D, and msdn is pretty much vague about most parametersAlso, should i use one sprite for all images, or one sprite for each image?Thanks in advance-Hassan   
  Are you using any shader to draw the sprite? if yes can you post it?   ;  No im not, the above is the whole sprite rendering code at the moment   ;  anyone? oh well, can anyone tell me how to properly render a sprite?   ;  Set Vertex and Pixelshader? In DX10 there's no FFP.   ;  He is using D3DX10Sprite, which sets vertex, geometry, and pixel shaders. Did you call SetProjectionTransform on the D3DX10Sprite?   ;  Probably you rotated your camera 90º or something.   ;  Here's a bit more that should help. This is what I use for my projection matrix. It gives you the standard 2D coordinate system where (0, 0) is in the upper left and one unit is equal to one pixel. (_sizeWidth and _sizeHeight are the size of the window)D3DXMATRIX projection;D3DXMatrixOrthoOffCenterLH(&projection, 0, (float)_sizeWidth, (float)_sizeHeight, 0, 0, 1);_sprite->SetProjectionTransform(&projection);Since this sort of flips the coordinate system across the x-axis, it reverses the winding of the vertices, causing everything drawn with ID3DXSprite to be culled. So you'll probably want to disable culling. (See D3D10_RASTERIZER_DESC)Also, you will need to apply scaling. If matWorld is the identity matrix, ID3DX10Sprite draws a sprite from min (-0.5, -0.5) to max (0.5, 0.5). So when using the above projection matrix, the scale should be the width and the height of your image in pixels.As for the other parameters:spr.ColorModulate = D3DXCOLOR(1, 1, 1, 1);spr.pTexture = _textureView;spr.TexCoord = D3DXVECTOR2(0, 0);spr.TexSize = D3DXVECTOR2(1, 1);spr.TextureIndex = 0;- The color of your sprite is multiplied by ColorModulate. You probably want this to be white.- TexCoord is the starting texture coordinate of your sprite. Unless you are using spritesheets or only drawing part of a sprite, this should be <0, 0>- TexSize is the size of the sprite in texture coordinates. Once again, unless you are using spritesheets, etc, this should probably be <1, 1>. Unfortunately, D3DX10Sprite sets a sampler state that uses clamp, so you can not tile the sprite by using numbers higher than 1. I had to create my own workaround for this.- I'm guessing you know what pTexture and TextureIndex do.Hope that helps.-Jeff(Also, are you from TGC?)   ;Quote:Original post by jrh2365Here's a bit more that should help. This is what I use for my projection matrix. It gives you the standard 2D coordinate system where (0, 0) is in the upper left and one unit is equal to one pixel. (_sizeWidth and _sizeHeight are the size of the window)D3DXMATRIX projection;D3DXMatrixOrthoOffCenterLH(&projection, 0, (float)_sizeWidth, (float)_sizeHeight, 0, 0, 1);_sprite->SetProjectionTransform(&projection);Since this sort of flips the coordinate system across the x-axis, it reverses the winding of the vertices, causing everything drawn with ID3DXSprite to be culled. So you'll probably want to disable culling. (See D3D10_RASTERIZER_DESC)Also, you will need to apply scaling. If matWorld is the identity matrix, ID3DX10Sprite draws a sprite from min (-0.5, -0.5) to max (0.5, 0.5). So when using the above projection matrix, the scale should be the width and the height of your image in pixels.As for the other parameters:spr.ColorModulate = D3DXCOLOR(1, 1, 1, 1);spr.pTexture = _textureView;spr.TexCoord = D3DXVECTOR2(0, 0);spr.TexSize = D3DXVECTOR2(1, 1);spr.TextureIndex = 0;- The color of your sprite is multiplied by ColorModulate. You probably want this to be white.- TexCoord is the starting texture coordinate of your sprite. Unless you are using spritesheets or only drawing part of a sprite, this should be <0, 0>- TexSize is the size of the sprite in texture coordinates. Once again, unless you are using spritesheets, etc, this should probably be <1, 1>. Unfortunately, D3DX10Sprite sets a sampler state that uses clamp, so you can not tile the sprite by using numbers higher than 1. I had to create my own workaround for this.- I'm guessing you know what pTexture and TextureIndex do.Hope that helps.-Jeff(Also, are you from TGC?)First off, yes i am from TGC =)Second, i wasn't setting the projection matrix and that's why it appeared in the middle of the screen, but after this, it doesn't appear at all, in dx9, i didn't have to set any matrix for my sprites, all i had to do is D3DXMatrixTransformation2D and use SetTransform method, it looks quite different now, anyway, i still can't get it to work, what else need to be set? the view matrix? if so, how?as far as i understand now, i need to set each D3DX10_SPRITE's world matrix, and set the ID3DX10Sprite projection matrix, and disable culling in the rasterizer state, that's it? am i missing something? because it still shows nothing   ;  Here is some sample code (10.1, but similar enough to 10.0), though it uses some other classes I have for setting up the device/window etc, so you won't be able to compile it. This draws a 32x32 sprite near the upper left corner. Hopefully it is evident what most of it does, since I'm to lazy to comment it right now, hehe.You shouldn't need to set the view matrix. Did you apply scaling to the sprite? Otherwise it would be only one pixel after setting that projection matrix.[EDIT]Also, instead of disabling culling, you can negate the scaling along one axis, so instead of (32, 32), you'd do (-32, 32), or (32, -32).[/EDIT]#include "Base.h"#include <d3d10_1.h>#include <d3dx10.h>#pragma comment(lib, "Framework.lib")#pragma comment(lib, "d3d10_1.lib")#pragma comment(lib, "d3dx10.lib")class MyGame : public Base{public:MyGame(){}~MyGame(){_textureView->Release();_texture->Release();_sprite->Release();_rasterizerState->Release();}void MyGame::LoadContent(){D3DX10CreateSprite(_device, 1000, &_sprite);D3DX10CreateTextureFromFile(_device, "player.png", NULL, NULL,(ID3D10Resource**)&_texture, NULL);_device->CreateShaderResourceView1(_texture, NULL, &_textureView);D3DXMATRIX projection;D3DXMatrixOrthoOffCenterLH(&projection, 0, (float)_sizeWidth,(float)_sizeHeight, 0, 0, 1);_sprite->SetProjectionTransform(&projection);D3DXMatrixTransformation2D(&_sprDesc.matWorld, &D3DXVECTOR2(0, 0), 0,&D3DXVECTOR2(32, 32), &D3DXVECTOR2(0, 0), 0, &D3DXVECTOR2(48, 48));_sprDesc.TexCoord = D3DXVECTOR2(0, 0);_sprDesc.TexSize = D3DXVECTOR2(1, 1);_sprDesc.ColorModulate = D3DXCOLOR(1, 1, 1, 1);_sprDesc.pTexture = _textureView;_sprDesc.TextureIndex = 0;D3D10_RASTERIZER_DESC rasterizerDesc = {D3D10_FILL_SOLID, D3D10_CULL_NONE, FALSE, 0, 0.0f, 0.0f,TRUE, FALSE, FALSE, FALSE};_device->CreateRasterizerState(&rasterizerDesc, &_rasterizerState);_device->RSSetState(_rasterizerState);}void MyGame::Update(float elapsedTime){if (_inputManager->KeyDown(VK_ESCAPE)) { PostQuitMessage(0); }}void MyGame::Draw(float elapsedTime){float clearColor[4] = {0.0, 0.0, 0.0, 1.0};_device->ClearRenderTargetView(_viewRenderTarget, clearColor);_device->ClearDepthStencilView(_viewDepthStencil, D3D10_CLEAR_DEPTH |D3D10_CLEAR_STENCIL, 1.0f, 0);_sprite->Begin(0);_sprite->DrawSpritesImmediate(&_sprDesc, 1, 0, 0);_sprite->End();_swapChain->Present(1, 0);}private:ID3DX10Sprite* _sprite;D3DX10_SPRITE _sprDesc;ID3D10RasterizerState* _rasterizerState;ID3D10Texture2D* _texture;ID3D10ShaderResourceView1* _textureView;};int main(int argc, char* argv[]){MyGame *game = new MyGame();game->Initialize();game->Run();delete game;}-Jeff   