Skip to main content
gamedev.net
🔒 Locked

Rendering a quad problem

Started by streamer Jan 23, 2007 at 5:02 AM 8 replies 900+ views
Original Post
streamer
streamer
Hi all. I created one function that render quad on screen. But all I can see is black quad without any texture on it. Here is the code:

typedef struct _D3DTVERTEX {
    float sx; /* Screen coordinates */
    float sy;
    float sz;
    D3DCOLOR color; /* Vertex color */
    float tu; /* Texture coordinates */
    float tv;
    _D3DTVERTEX() { }
    _D3DTVERTEX(const D3DVECTOR& v,
                 D3DCOLOR _color, 
                 float _tu, float _tv)
    { sx = v.x; sy = v.y; sz = v.z; 
      color = _color; 
      tu = _tu; tv = _tv;
    }
	_D3DTVERTEX(const D3DVECTOR& v,               
                 float _tu, float _tv)
    { sx = v.x; sy = v.y; sz = v.z; 
      color = 0xffffffff; 
      tu = _tu; tv = _tv;
    }
} D3DTVERTEX, *LPD3DTVERTEX;

#define D3DFVF_TVERTEX D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1
void BlitRect(LPDIRECT3DDEVICE9 lpDevice, float x, float y, float w, float h, float z, DWORD col)
{

	static D3DTVERTEX verts[4];
	static float u,v,u1,v1;

	verts[0]=D3DTVERTEX(D3DXVECTOR3(x,y,z),0,0); 
	verts[1]=D3DTVERTEX(D3DXVECTOR3(w,y,z),1.0f,0); 
	verts[2]=D3DTVERTEX(D3DXVECTOR3(w,h,z),1.0f,1.0f);
	verts[3]=D3DTVERTEX(D3DXVECTOR3(x,h,z),0,1.0f);

	
	verts[0].color = 
	verts[1].color = 
	verts[2].color = 
	verts[3].color = col;

	// configure shader for vertex type
	lpDevice->SetFVF(D3DFVF_TVERTEX);
	// draw the rectangle
	lpDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN,2,verts,sizeof(D3DTVERTEX));
	

}


and I'm calling it with: pDevice->SetTexture(0,texture); BlitRect(pDevice, 0,0, 1280,1024,0.1f,0xffffffff); It drives me mad. What seems to be the problem? Thanks in advance
redeemer90
redeemer90
Make sure you have set up your rendering states properly. Set lighting to false and clear the bg to grey to make sure the black that you see on the screen is not from the background.
streamer
streamer
Quote:
Original post by redeemer90
Make sure you have set up your rendering states properly. Set lighting to false and clear the bg to grey to make sure the black that you see on the screen is not from the background.


I did that before. Actually if I set render states not to modulate between texture and diffuse color, but just texture or just diffuse, diffuse color rendering is ok, only texture doesn't show up. I thought that my face is backed up, but when I set d3dcull to none, nothing happens. Same thing.
There IS something with render states or with sampler stage, because when I put my instructions for draw between sprite->begin and sprite->end call (sprite->begin call, set render states to bunch of values), everything draws normally.
ET3D
ET3D
I'd guess it's a render state issue.

If your colour is black, the rendering will be black, because the default render states multiply the diffuse colour with the texture colour. Sprite rendering might be changing this. It's a good practice to set the render states (or pixel shader) yourself, and not count on default behaviour.
streamer
streamer
Yeah but my color is not black but white
...
BlitRect(pDevice, 0,0, 1280,1024,0.1f,0xffffffff);
...
and I don't have problem with color but with texture. color is now shown ok ( i turned off lighting and now quad is white but without a texture) and texture stages are this:

pDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
pDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
pDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);

pDevice->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
pDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP );
pDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP );


but something is preventing texture for showing up.

And I'm setting my renderstates, bypassing default behaviour, but still something is missing. Only I don't know what [sad].
Todo
Todo
EDIT: wrong info. Sorry about that ;-)
Namethatnobodyelsetook
Namethatnobodyelsetook
Why are you setting the texture transform flags? These are used when you want to multiply your UVs by a matrix, which you don't want to do. You've told it to use a matrix, but you haven't specified the matrix in SetTransform(D3DTS_TEXTURE0, &mat). Get rid of the line that sets the transform flags to count2.
streamer
streamer
Quote:
Original post by Namethatnobodyelsetook
Why are you setting the texture transform flags? These are used when you want to multiply your UVs by a matrix, which you don't want to do. You've told it to use a matrix, but you haven't specified the matrix in SetTransform(D3DTS_TEXTURE0, &mat). Get rid of the line that sets the transform flags to count2.


Actually I'm setting the matrix, because I'm scrolling the texture, like this.

static float offset = 0.0f;
offset+=(0.0005f*scroll_speed);
if(offset >1.0f) offset = 0.0f;

D3DXMATRIX mat;
D3DXMatrixIdentity(&mat);

// scroll x.y
mat._31=offset;
mat._32=offset*2;
// angle x,y
mat._12 = 0.5f;
mat._21 = -0.5f;

pDevice->SetTransform( D3DTS_TEXTURE0, &mat );


Trick I wanted to achieve is one texture stretched across whole screen that scrolls in one direction. So the whole code look like this:

BlitRect.cpp

typedef struct _D3DTVERTEX {    float sx; /* Screen coordinates */    float sy;    float sz;    D3DCOLOR color; /* Vertex color */    float tu; /* Texture coordinates */    float tv;    _D3DTVERTEX() { }    _D3DTVERTEX(const D3DVECTOR& v,                 D3DCOLOR _color,                  float _tu, float _tv)    { sx = v.x; sy = v.y; sz = v.z;       color = _color;       tu = _tu; tv = _tv;    }	_D3DTVERTEX(const D3DVECTOR& v,                                float _tu, float _tv)    { sx = v.x; sy = v.y; sz = v.z;       color = 0xffffffff;       tu = _tu; tv = _tv;    }} D3DTVERTEX, *LPD3DTVERTEX;#define D3DFVF_TVERTEX D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1void BlitRect(LPDIRECT3DDEVICE9 lpDevice, float x, float y, float w, float h, float z, DWORD col){	static D3DTVERTEX verts[4];	static float u,v,u1,v1;	verts[0]=D3DTVERTEX(D3DXVECTOR3(x,y,z),0,0); 	verts[1]=D3DTVERTEX(D3DXVECTOR3(w,y,z),1.0f,0); 	verts[2]=D3DTVERTEX(D3DXVECTOR3(w,h,z),1.0f,1.0f);	verts[3]=D3DTVERTEX(D3DXVECTOR3(x,h,z),0,1.0f);		verts[0].color = 	verts[1].color = 	verts[2].color = 	verts[3].color = col;	// configure shader for vertex type	lpDevice->SetFVF(D3DFVF_TVERTEX);	// draw the rectangle	lpDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN,2,verts,sizeof(D3DTVERTEX));	}


main.cpp

static float offset = 0.0f;offset+=(0.0005f*scroll_speed);if(offset >1.0f) offset = 0.0f;D3DXMATRIX mat;D3DXMatrixIdentity(&mat);// scroll x.ymat._31=offset;mat._32=offset*2;// angle x,ymat._12 = 0.5f;mat._21 = -0.5f;pDevice->SetTransform( D3DTS_TEXTURE0, &mat );pDevice->SetRenderState( D3DRS_LIGHTING, FALSE );pDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );pDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);pDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);pDevice->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);pDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP );pDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP );pDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );pDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );pDevice->SetTexture(0,texture);BlitRect(pDevice, 0,0, 1280,1024,0.1f,0xffffffff);


and if I put BlitRect between sprite begin , end call it works, otherwise not.

...main.cpp...(correction)

m_pSprite->Begin(D3DXSPRITE_ALPHABLEND);pDevice->SetTexture(0,texture);BlitRect(pDevice, 0,0, 1280,1024,0.1f,0xffffffff);m_pSprite->End();
ET3D
ET3D
I'd suggest simplifying the code, removing the texture transform, and possibly even going without the diffuse colour. This would help narrow things down.

BTW, you should set stage 1 to disabled (in the colour channel). This is important in case a previous setting was using higher stages. (Which I imagine you aren't using, but it's a good practice to get into for the future.)

streamer
streamer
Quote:
Original post by ET3D
I'd suggest simplifying the code, removing the texture transform, and possibly even going without the diffuse colour. This would help narrow things down.

BTW, you should set stage 1 to disabled (in the colour channel). This is important in case a previous setting was using higher stages. (Which I imagine you aren't using, but it's a good practice to get into for the future.)


That was first thing I did. Simple quad with texture. But didn't work. Then I realized that my quad rendering shows diffuse color well, but not the texture. My first try was simple quad with texture and without color. Few lines of code. But unfortunately that wasn't working too. I'm not using higher stages.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.