Jump to content
  • Advertisement
Sign in to follow this  

Help getting a rectangled textured

This topic is 4946 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

Im using directx8, and im trying to load a texture onto a rectangle nothing too fancy. Im having problems getting it to load and im thinking im missing a texture stage or something. Im loading a 24 bit bitmap image just as a test. Here is the code if you guys could maybe take a look id greatly appreciate it. Im having trouble figuring out what is going wrong and im new to direct3d so im definately not an expert.
//Purpose: Initialize Direct3d
//Returns: Return 1 on success 0 on error 
int CDIRECT3D::Initialize_Direct3d(HWND &window_handle)
	HRESULT hresult;

	//create device
	//Make Direct3D object
    if((D3D = Direct3DCreate8(D3D_SDK_VERSION))==NULL)
		return 0; 

		return 0;

	d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP;
	d3dpp.hDeviceWindow = window_handle;
	d3dpp.BackBufferCount = 1;

	d3dpp.Windowed = false;
    d3dpp.BackBufferWidth = SCREEN_WIDTH;
    d3dpp.BackBufferHeight = SCREEN_HEIGHT;
    d3dpp.BackBufferFormat = d3d_displaymode.Format;
	d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
	d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_ONE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
    d3dpp.EnableAutoDepthStencil = 1;

		return 0;

	//Create vertex buffer
	if(D3D_OK!=(d3d_device->CreateVertexBuffer(sizeof(VERTEX) * 4, NULL,
		D3DFVF_VERTEX, D3DPOOL_MANAGED, &vertexbuffer)))
		return 0;

	if(D3D_OK!=(d3d_device->SetStreamSource(0, vertexbuffer,sizeof(VERTEX))))
		return 0;

	//Set vertex shader
		return 0;

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

	d3d_device->SetVertexShader(D3DFVF_VERTEX );

	return 1;

IDirect3DTexture8* CDIRECT3D::LoadTexture(char *fileName)
  IDirect3DTexture8 *d3dTexture;
  D3DXIMAGE_INFO SrcInfo;      //Optional

  //Use a magenta colourkey
  D3DCOLOR colorkey = 0xffffffff;

  // Load image from file
  if (FAILED(D3DXCreateTextureFromFileEx (d3d_device, fileName, 0, 0, 1, 0, 
        colorkey, &SrcInfo, NULL, &d3dTexture)))
    return NULL;
  //Return the newly made texture
  return d3dTexture;

void CDIRECT3D::BlitD3D(RECT *rDest,
    D3DCOLOR vertexColour)
  VERTEX* vertices;

  //Lock the vertex buffer
  vertexbuffer->Lock(0, 0,(unsigned char**) &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].acolor = 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].acolor = 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].acolor = 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].acolor = 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

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

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

int CDIRECT3D::Draw_Text_GDI(char *message, int x,int y,int color,HDC hdc)
	SetTextColor(hdc,color );
	SetBkMode(hdc, TRANSPARENT);

	return 1;

int Game_Main(void *parms)
       PostMessage(main_window_handle, WM_DESTROY,0,0);

	//clear backbuffer
	direct3d.d3d_device->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_RGBA(0,60,128,255),1.0f,0);





Share this post

Link to post
Share on other sites
Is it because you have no ambient light to accully see the texture. That could be the problem if all you are getting is a black rectangle.
Just if you didn't know use SetRenderState with the D3DRS_AMBIENT flag passed and the second value is the color value for the light. The color white would be 0xFFFFFF.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!