Archived

This topic is now archived and is closed to further replies.

Unpredictable behavior using DrawPrimitive and Textured quad

This topic is 5135 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 am trying to convert a very functional 2d rts game using dx9 and sprite interface to a textured quad implementation. I am referencing http://www.gamedev.net/reference/articles/article1972.asp entitled "2D in Direct3D using Textured Quads" for my initial implementation. I am attempting to draw one 32 x 32 colored square. When I run my program the square is drawn as a rectangle and the location of the rectangle changes with each render pass. The location generally proceeds in a left to right, top to bottom fashion across multiple passes. The portion of the square that is rendered changes randomly. I am calling the draw primitive function in a render pass in which I am also doing several hundred sprite->draws(). There appears to be some interaction with what I draw via the sprite interface and what is rendered by the drawPrimitive call. When I run my program with fog of war enabled I generally see the entire square being rendered. When fog is disabled I see only the top left corner. None of this makes sense to me at this point. I could probably figure out the stretching of the square which is occuring but the randomness of the src and dest are baffling. I have examined the parameters passed to my Blit3D3 procedure (srcVertices and destRect). The srcVertices is constant (0,0) (1,1). The destRec is a constant square 32x32. No error/warning statements occur. The code below was cut from my program and is largely verbatim from the article mentioned. I did swap the array positions 2 and 3 in my vertices[] assignment, as shown below. With the positions swapped I at least alway get a rectangle. Previously I was seeing a 5 sided polygon (rectangle with the top edge caved in). Any help would be greatly appreciated, steve const DWORD D3DFVF_TLVERTEX = D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1; //Custom vertex struct TLVERTEX { float x; float y; float z; float rhw; D3DCOLOR colour; float u; float v; }; //Custom float src rectangle struct SRCVERTEX { float l; float t; float r; float b; }; //Set vertex shader m_pd3dDevice->SetVertexShader(NULL); m_pd3dDevice->SetFVF(D3DFVF_TLVERTEX); //Create vertex buffer m_pd3dDevice->CreateVertexBuffer(sizeof(TLVERTEX) * 4, NULL, D3DFVF_TLVERTEX, D3DPOOL_MANAGED, &pVertexBuffer, NULL); m_pd3dDevice->SetStreamSource(0, pVertexBuffer, 0, sizeof(TLVERTEX)); m_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE); m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); m_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); m_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE); HRESULT CD3DApplication::BlitD3D(IDirect3DTexture9 *texture, SRCVERTEX* srcVertices, RECT *rDest, D3DCOLOR vertexColour) { HRESULT ddrval; TLVERTEX* vertices; //Lock the vertex buffer pVertexBuffer->Lock(0, 0, (void**)&vertices, NULL); //Setup vertices vertices[0].colour = vertexColour; vertices[0].x = (float) rDest->left; vertices[0].y = (float) rDest->top; vertices[0].z = 0.0f; vertices[0].rhw = 1.0f; vertices[0].u = srcVertices->l; vertices[0].v = srcVertices->t; vertices[1].colour = vertexColour; vertices[1].x = (float) rDest->right; vertices[1].y = (float) rDest->top; vertices[1].z = 0.0f; vertices[1].rhw = 1.0f; vertices[1].u = srcVertices->r; vertices[1].v = srcVertices->t; vertices[3].colour = vertexColour; vertices[3].x = (float) rDest->right; vertices[3].y = (float) rDest->bottom; vertices[3].z = 0.0f; vertices[3].rhw = 1.0f; vertices[3].u = srcVertices->r; vertices[3].v = srcVertices->b; vertices[2].colour = vertexColour; vertices[2].x = (float) rDest->left; vertices[2].y = (float) rDest->bottom; vertices[2].z = 0.0f; vertices[2].rhw = 1.0f; vertices[2].u = srcVertices->l; vertices[2].v = srcVertices->b; //Unlock the vertex buffer ddrval = pVertexBuffer->Unlock(); //Set texture ddrval = m_pd3dDevice->SetTexture (0, texture); //Draw image ddrval = m_pd3dDevice->DrawPrimitive (D3DPT_TRIANGLEFAN, 0, 2); return(ddrval); } HRESULT CDrawUtilities::ClipView3dDraw(CGroundUnitRules* pRules, CPoint ptClient, int row, int col) { SRCVERTEX srcVertices; CRect destRect; HRESULT ddrval; ddrval = TERRAIN_NOT_BLT; srcVertices.l = (float)(pRules->getTextureOrigin().x + col * pRules->getSpriteLength()) / pRules->getTextureLength(); srcVertices.t = (float)(pRules->getTextureOrigin().y + row * pRules->getSpriteWidth()) / pRules->getTextureWidth(); srcVertices.r = srcVertices.l + (float)(pRules->getSpriteLength()) / pRules->getTextureLength(); srcVertices.b = srcVertices.t + (float)(pRules->getSpriteWidth()) / pRules->getTextureWidth(); destRect.left = ptClient.x + xOff; destRect.top = ptClient.y + yOff; destRect.right = destRect.left + pRules->getSpriteLength(); destRect.bottom = destRect.top + pRules->getSpriteWidth(); D3DCOLOR vertexColour; vertexColour = 0xFFFFFFFF; ddrval = m_pD3DApp->BlitD3D(pRules->getTexture(), &srcVertices, &destRect, vertexColour); return(ddrval); } m_pDrawUtilities->ClipView3dDraw(RED_ID, CPoint(100,100), 0, 0);

Share this post


Link to post
Share on other sites
Got it to work by moving setstreamsource call from initialization section to right before drawprimitive call. Also swapped back positions 2 and 3 in vertice array so it matched again the article that I was referencing.

Share this post


Link to post
Share on other sites