DX not rendering 2d scene

Started by
12 comments, last by cryo75 20 years, 7 months ago
Hi all, I''ve got the following class:

#ifndef CPANE_H
#define CPANE_H

#include "StdAfx.h"

class CPane
{
//Private member variables

private:
	//Generic

	int		miWidth;					//Viewport / window width

	int		miHeight;					//Viewport / window height	

	int		miTextures;					//Counter of loaded textures

	int		miMaxTextures;				//Maximum number of textures


	//Pane element

	struct ELEMENT {
		LPDIRECT3DTEXTURE9 mptex;
		int				   x, y;
		float			   xSize, ySize;
	};
	ELEMENT* pElements;

	//Custom vertex

	struct PANEVERTEX {
		D3DXVECTOR3 position;
		D3DXVECTOR3 vecNorm;
		FLOAT		tu, tv;
		FLOAT		tu2, tv2;
	};
//	static const DWORD D3DFVF_PANEVERTEX = (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX2);

	DWORD D3DFVF_PANEVERTEX;

	//DirectX

	LPDIRECT3DDEVICE9		mpdev;
	LPDIRECT3DVERTEXBUFFER9	mpvb;

//Private member functions

private:
	HRESULT InitPane();

//Implementation

public:
	HRESULT Create(LPDIRECT3DDEVICE9 pDev, int iWidth, int iHeight);
	HRESULT LoadTexture(int x, int y, float xs, float ys, LPCSTR szFileName);
	HRESULT Render();
	void CleanUp();

//Defaults

public:
	CPane();
	~CPane();
};

#endif

#include "CPane.h"

CPane::CPane()
{
	//Objects

	mpvb		  = NULL;
	mpdev		  = NULL;

	//Textures

	miMaxTextures = 32;
	miTextures	  = 0;
	pElements	  = new ELEMENT[miMaxTextures];

	D3DFVF_PANEVERTEX = (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX2);
}

CPane::~CPane()
{
	CleanUp();
}

void CPane::CleanUp()
{
	//Objects

	SAFE_RELEASE(mpvb);

	//Textures

	SAFE_DELETE(pElements);
	
	//Reset textures count

	miTextures = 0;
}

HRESULT CPane::Create(LPDIRECT3DDEVICE9 pDev, int iWidth, int iHeight)
{
	CleanUp();

	//Get our rendering device

	mpdev = pDev;
	if (!mpdev)
		return E_FAIL;

	//Save the window dimensions

	miWidth  = iWidth;
	miHeight = iHeight;

	//3D projection matrix

	D3DXMATRIX matProj;
	D3DXMatrixOrthoLH(&matProj, (float)miWidth, (float)miHeight, 0, 1);
	mpdev->SetTransform(D3DTS_PROJECTION, &matProj);
	
	//Turn off culling

	mpdev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

	//Turn off lighting

	mpdev->SetRenderState(D3DRS_LIGHTING, FALSE);

	//Turn off z-buffering (no 2d graphics discarded at render time)

	mpdev->SetRenderState(D3DRS_ZENABLE, FALSE);

	//Turn off z-buffer writing (2d graphics not to alter z-buffer)

	mpdev->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);

	//Initialize the vertex buffer

	return InitPane();
}

HRESULT CPane::InitPane()
{
	PANEVERTEX* pVertices;

	//Create the interface object buffer

	if(FAILED(mpdev->CreateVertexBuffer(4*sizeof(PANEVERTEX), 0, D3DFVF_PANEVERTEX, D3DPOOL_DEFAULT, &mpvb, NULL)))
		return E_FAIL;

	//Lock the buffer for editing

	if(FAILED(mpvb->Lock(0, 0, (void**)&pVertices, 0)))
		return E_FAIL;

	//Create a quad made up of 3 vertices (triangle strips)

	//

	// 1          3

	//  X--------X

	//  |x       |

	//  | x      |

	//  |  x     |

	//  |   x    |

	//  |    x   |

	//  |     x  |

	//  |      x |

	//  |       x|

	//  X--------X

	// 0          2

	//

	pVertices[0].position = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
	pVertices[0].tu       = 0.0f;
	pVertices[0].tv       = 1.0f;
	pVertices[0].tu2      = 0.0f;
	pVertices[0].tv2      = 1.0f;
	pVertices[0].vecNorm  = D3DXVECTOR3(0.0f,0.0f,1.0f);

	pVertices[1].position = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
	pVertices[1].tu       = 0.0f;
	pVertices[1].tv       = 0.0f;
	pVertices[1].tu2      = 0.0f;
	pVertices[1].tv2      = 0.0f;
	pVertices[1].vecNorm  = D3DXVECTOR3(0.0f,0.0f,1.0f);

	pVertices[2].position = D3DXVECTOR3( 1.0f, 0.0f, 0.0f );
	pVertices[2].tu       = 1.0f;
	pVertices[2].tv       = 1.0f;
	pVertices[2].tu2      = 1.0f;
	pVertices[2].tv2      = 1.0f;
	pVertices[2].vecNorm  = D3DXVECTOR3(0.0f,0.0f,1.0f);

	pVertices[3].position = D3DXVECTOR3( 1.0f, 1.0f, 0.0f );
	pVertices[3].tu       = 1.0f;
	pVertices[3].tv       = 0.0f;
	pVertices[3].tu2      = 1.0f;
	pVertices[3].tv2      = 0.0f;
	pVertices[3].vecNorm  = D3DXVECTOR3(0.0f,0.0f,1.0f);

	//Done editing, unlock the buffer

	mpvb->Unlock();

	return D3D_OK;
}

HRESULT CPane::LoadTexture(int x, int y, float xs, float ys, LPCSTR szFileName)
{
	//LPDIRECT3DTEXTURE9 mptex;


	//Do we have a device?

	if (!mpdev)
		return E_FAIL;

	//Have we reached the maximum textures?

	if (miTextures == miMaxTextures)
		return E_FAIL;

	//Load the texture from file

	if (FAILED(D3DXCreateTextureFromFile(mpdev, szFileName, &pElements[miTextures].mptex))) {
		if (miTextures > 0)
			miTextures--;
		return E_FAIL;
	}

	//Set the appropriate values in the structure

	//pElements[miTextures].mptex = mptex;

	pElements[miTextures].x		= x;
	pElements[miTextures].y		= y;
	pElements[miTextures].xSize = xs;
	pElements[miTextures].ySize = ys;
	

	//Increment the number of textures we''re using

	miTextures++;

	return D3D_OK;

}

HRESULT CPane::Render()
{
	D3DXMATRIX	matWorld, matRotation, matTranslation, matScale;
	float		fXPos, fYPos;
	
	//Do we have a device?

	if (!mpdev)
		return E_FAIL;

	for (int i = 0; i < miTextures; i++) {
		//Set default position, scale, rotation

		D3DXMatrixIdentity(&matTranslation);

		//Scale the sprite

		D3DXMatrixScaling(&matScale, pElements[i].xSize, pElements[i].ySize, 1.0f);
		D3DXMatrixMultiply(&matTranslation, &matTranslation, &matScale);
		
		//Rotate the sprite

		D3DXMatrixRotationZ(&matRotation, 0.0f);
		D3DXMatrixMultiply(&matWorld, &matTranslation, &matRotation);

		//Calculate the position in screen-space

		fXPos = (float)(-(miWidth / 2) + pElements[i].x);
		fYPos = (float)(-(miHeight / 2) - pElements[i].y + pElements[i].ySize);

		//Move the sprite

		matWorld._41 = fXPos;
		matWorld._42 = fYPos;

		//Set matrix

		mpdev->SetTransform(D3DTS_WORLD, &matWorld);
		mpdev->SetTexture(0, pElements[i].mptex);
		mpdev->SetStreamSource(0, mpvb, 0, sizeof(PANEVERTEX));
		mpdev->SetFVF(D3DFVF_PANEVERTEX);
		mpdev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

		//Dereference texture

		mpdev->SetTexture(0, NULL);
	}

	return D3D_OK;
}
my main render function is the following:

HRESULT CEngine::Render()
{
	if (!mpdev) 
		return E_FAIL;

	mpdev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_ARGB(0,0,0,0), 1.0f, 0);

	if (SUCCEEDED(mpdev->BeginScene())) {
		//mptile->Render();		


		if (mppane)
			mppane->Render();

		UpdateStats();
		mpdev->EndScene();
	}

	mpdev->Present(NULL, NULL, NULL, NULL);

	return D3D_OK;
}
I am loading a 256x256 tga and placing and 0,0. The damn bitmap is not being rendered on the screen and it''s driving me crazy... HELP!!!! Any suggestions?? Ivan
Advertisement
It''s happened to me too...
I''m bumping my thread daily something like 4 days...
and no response...

so PLEASE HELP (US) !!!
How appropriate, you fight like a cow!
Phew! I don't have time to read all that but you can try a few things.

1. Try setting your clear color to something other than black. If you see something, then there's something wrong with your lighting configuration. If you intend to use ligthing you might want to make sure that materials are set properly and that light is positioned such that it, at some angle, lights whatever you are trying to draw. Otherwise make sure that lighting is turned off.

2. If you still don't see anything, try setting culling to D3DCULL_NONE.

UdayK

[edited by - UdayK on September 24, 2003 7:04:39 AM]
Try to move your pane slightly in z-direction. As far as I can see it is exactly on the near clipping plane, don''t know how this cas is handled.
Lightning is off and the culling is already set to cull_none...
How appropriate, you fight like a cow!
Well on OGL, it gives the effect as if Z buffer is disabled, it could be similar here. You could be lucky if it black outs the scene
I DID IT!!!
I KNOW HOW!!!
DO SOMETHING LIKE THIS!
D3DXMATRIX matView;	D3DXMatrixLookAtLH(&matView,                                \                     &D3DXVECTOR3(0.0f, 0.0f,1.0f),       \                     &D3DXVECTOR3(0.0f, 0.0f, 0.0f),          \                     &D3DXVECTOR3(0.0f, 1.0f, 0.0f));  g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView); 

NOW IT WILL WORK!
WOW
AT LAST!!!
AFTER A SLEEPLESS WEEK!
How appropriate, you fight like a cow!
You could try setting your sampler stage to use color arg 1 and set that argument to texture, or modulate your diffuse and texture components.

I don''t see you specifying sampler stage states and also the plane vertex FVF doesn''t have D3DFVF_DIFFUSE.
You weren''t specifying the view transform all this time?? damn!
yes I did, but the Z was something like -100...
lol
well
now it''s working.
How appropriate, you fight like a cow!

This topic is closed to new replies.

Advertisement