Rotating a quad in D3D

Started by
1 comment, last by Aardvajk 16 years, 3 months ago
Ive got this function for drawing a sprite. However how should I go about rotating it around the sprite[id].offset_x and sprite[id].offset_y point?

void draw_sprite(const int id, float x, float y, D3DCOLOR c1 = c_white, D3DCOLOR c2 = NULL, D3DCOLOR c3 = NULL, D3DCOLOR c4 = NULL)
{
	if (c4 == NULL)
	{
		c2 = c1;
		c3 = c1;
		c4 = c1;
	}

	x -= 0.5f + sprite[id].offset_x;
	y -= 0.5f + sprite[id].offset_y;

	cgm::d3d_vertex* vertices;
	cgm::d3d_vertex_buffer->Lock(0, 0, (void**)&vertices, NULL);

	vertices[0].colour = c1;
	vertices[0].x = x;
	vertices[0].y = y;
	vertices[0].z = 0.0;
	vertices[0].rhw = 1.0;
	vertices[0].u = 0.0;
	vertices[0].v = 0.0;

	vertices[1].colour = c2;
	vertices[1].x = x  + sprite[id].w;
	vertices[1].y = y;
	vertices[1].z = 0.0;
	vertices[1].rhw = 1.0;
	vertices[1].u = 1.0;
	vertices[1].v = 0.0;

	vertices[2].colour = c3;
	vertices[2].x = x + sprite[id].w;
	vertices[2].y = y + sprite[id].h;
	vertices[2].z = 0.0;
	vertices[2].rhw = 1.0;
	vertices[2].u = 1.0;
	vertices[2].v = 1.0;

	vertices[3].colour = c4;
	vertices[3].x = x;
	vertices[3].y = y + sprite[id].h;
	vertices[3].z = 0.0;
	vertices[3].rhw = 1.0;
	vertices[3].u = 0.0;
	vertices[3].v = 1.0;

	cgm::d3d_vertex_buffer->Unlock();
	cgm::d3d_device->SetTexture (0, sprite[id].image);
	cgm::d3d_device->DrawPrimitive (D3DPT_TRIANGLEFAN, 0, 2);
}

Advertisement
For your purposes, you're probably better off using a D3DXSprite rather than a textured quad. The D3DXSprite supports setting a hotspot which will be the focal point for any rotations you perform on it. It also maintains its own transform matrix so you don't have to set the world transform for the device to get it where you want on the screen. Toymaker has a nice tutorial on it. Hope that helps.
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...
Unrelated to your question, but using NULL to represent a D3DCOLOR is not a good way to represent an invalid color - NULL assigned to a D3DCOLOR would equal transparent if alpha blending, or black if not.

I'd handle your situation here with function overloads:

void draw_sprite(const int id, float x, float y, D3DCOLOR c1, D3DCOLOR c2, D3DCOLOR c3, D3DCOLOR c4){   // your function, but assume all four colors valid}void draw_sprite(const int id, float x, float y, D3DCOLOR c1=c_white){    draw_sprite(id,x,y,c1,c1,c1,c1);}


That gives you rougly the same functionality, but allows you to pass transparent or black as a valid color.

This topic is closed to new replies.

Advertisement