Jump to content
  • Advertisement
Sign in to follow this  

Texture not being displayed.

This topic is 4932 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 having a lot of trouble trying to render a textured quad on-screen. What happens is that I load a 32-bit ARGB texture from a bitmap file, then draw a quad using two triangle fan primitives with the texture set. However, when it displays on-screen, the texture is just all-black, nothing else. It alpha blends with the screen, can be placed wherever I like, and the black area even matches the 'actual drawn' portion of the texture (i.e. any dead space made so the loaded texture's width and height are powers of two are not shown) but the texture won't be displayed. There isn't a problem with the texture itself, as calling D3DXSaveTextureToFile correctly saves the texture to a bitmap, and using the same bitmap in another pre=made application works, but not in this one. I suspect it has something to do with the BlitD3D function, but I copied it from article on this site, so I can't be sure.
int CWorld::D3D_Init(int windowed) {

if( NULL == ( lpd3d = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        return 0;

    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    if(windowed) d3dpp.Windowed = TRUE; else d3dpp.Windowed = FALSE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP;
    d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
	if( FAILED( hr = lpd3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, mainWindowHandle, D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp, &lpd3dDevice ) ) )
        return 0;

    // Device state would normally be set here
	//Set vertex shader

	//Create vertex buffer
	lpd3dDevice->CreateVertexBuffer(sizeof(TLVERTEX) * 4, NULL,
	lpd3dDevice->SetStreamSource(0, vertexBuffer, 0, sizeof(TLVERTEX));

	lpd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	lpd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
	lpd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
	lpd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
	lpd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
	lpd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);

    return 1;


//The function for making the textured quad
void CWorld::BlitD3D (IDirect3DTexture9 *texture, RECT *rDest,
    D3DCOLOR vertexColour)
  TLVERTEX* vertices;

  //Lock the vertex buffer
  vertexBuffer->Lock(0, 0, (void**)&vertices, NULL);

  //Setup vertices
  //A -0.5f modifier is applied to vertex coordinates to match texture
  //and screen coords. Some drivers may compensate for this
  //automatically, but on others texture alignment errors are introduced
  //More information on this can be found in the Direct3D 9 documentation
  vertices[0].colour = vertexColour;
  vertices[0].x = (float) rDest->left - 0.5f;
  vertices[0].y = (float) rDest->top - 0.5f;
  vertices[0].z = 0.0f;
  vertices[0].rhw = 1.0f;
  vertices[0].u = 0.0f;
  vertices[0].v = 0.0f;

  vertices[1].colour = vertexColour;
  vertices[1].x = (float) rDest->right - 0.5f;
  vertices[1].y = (float) rDest->top - 0.5f;
  vertices[1].z = 0.0f;
  vertices[1].rhw = 1.0f;
  vertices[1].u = 1.0f;
  vertices[1].v = 0.0f;

  vertices[2].colour = vertexColour;
  vertices[2].x = (float) rDest->right - 0.5f;
  vertices[2].y = (float) rDest->bottom - 0.5f;
  vertices[2].z = 0.0f;
  vertices[2].rhw = 1.0f;
  vertices[2].u = 1.0f;
  vertices[2].v = 1.0f;

  vertices[3].colour = vertexColour;
  vertices[3].x = (float) rDest->left - 0.5f;
  vertices[3].y = (float) rDest->bottom - 0.5f;
  vertices[3].z = 0.0f;
  vertices[3].rhw = 1.0f;
  vertices[3].u = 0.0f;
  vertices[3].v = 1.0f;

  //Unlock the vertex buffer

  //Saves texture to file to make sure nothing happened to it

  //Set texture
  lpd3dDevice->SetTexture (0, texture);

  //Draw image
  lpd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);


//Here's a call to BlitD3D in another function. Assume blitRect is a valid
//rectangle matching the size of the texture. render->GetT() refers to a UCHAR
//value determining translucency, which is & with the vertex color to perform
//alpha blending
this->BlitD3D(scrTexture,&blitRect,0xFFFFFFFF & (render->GetT()<<24));

//Here is where the texture is loaded, in the Init function of the program
D3DXCreateTextureFromFileEx(lpd3dDevice,"texture.bmp",0,0,1,0,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,D3DX_FILTER_NONE,D3DX_DEFAULT,0xFFFF00FF,NULL, NULL, &scrTexture);

Any help would be appreciated.

Share this post

Link to post
Share on other sites
Your bitwise and of the vertex color with the GetT() translucency value is the problem. It's blocking out all of your RGB values, and thus you're getting a black vertex color. Try this: 0x00FFFFFF | (render->GetT() << 24)

Share this post

Link to post
Share on other sites

Thank you very much, that fixed the problem immediately... I have no idea why I didn't see that sooner, I work with bitwise AND/ORs all the time when I'm messing with ASM stuff >_>

Hours and hours wasted on one simple keystroke. Pretty typical I guess :/

Share this post

Link to post
Share on other sites
Heh, I know the feeling. Sucks thoroughly. Well, at least you're on your way now. Glad I could help. [smile]

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!