Text/Geometry won't draw

Started by
-1 comments, last by Hoggel 12 years, 5 months ago
Hi.

I've been using Directx for 2D some time now and recently started out with 3D.

So i made this test program with a class representing a plane, but for some reason it won't draw, and neither will the text that display the characters position, I think that the problem has to do with renderstates but I don't really know how to solve them.

CPlane.h



#ifndef _CPLANE_H_
#define _CPLANE_H_
#include <Windows.h>
#include <d3dx9math.h>
#include <d3dx9tex.h>

struct CUSTOMVERTEX;
#define CUSTOMFVF ( D3DFVF_XYZ | D3DFVF_TEX1 )

typedef LPDIRECT3DTEXTURE9 LPD3DTEXTURE;

class CPlane
{
public:

enum Planes


{
XY,
ZY,
XZ,
};

CPlane(LPDIRECT3DDEVICE9 d3ddev);
CPlane(LPDIRECT3DDEVICE9 d3ddev, D3DXVECTOR3 position, Planes p, float x, float y, float u, float v);
CPlane(LPDIRECT3DDEVICE9 d3ddev, float x_pos, float y_pos, float z_pos, Planes p, float x, float y, float u, float v);

const int GetNumTextures() const;
const LPD3DTEXTURE GetTextureFromIndex(const unsigned int i) const;

void DeleteTextureFromIndex(const unsigned int index);

void AddTexture(LPD3DTEXTURE texture);
void AddTexture(const char* filename);

void SetTextureByIndex(const unsigned int index);

void CreatePlane(D3DXVECTOR3 position, Planes p, float x, float y, float u, float v);
void CreatePlane(float x_pos, float y_pos, float z_pos, Planes p, float x, float y, float u, float v);
virtual void Draw();
private:
void CreatePlaneXY(float width, float height, float u, float v);
void CreatePlaneZY(float depth, float height, float u, float v);
void CreatePlaneXZ(float width, float depth , float u, float v);

LPD3DTEXTURE *_textures;
int _numTextures;
int _activeTexture;
bool _isReady;

CUSTOMVERTEX *_vertices;

protected:
D3DXVECTOR3 _position;
D3DXVECTOR3 _rotation;

LPDIRECT3DDEVICE9 _d3ddev;

LPDIRECT3DVERTEXBUFFER9 v_buffer;
LPDIRECT3DINDEXBUFFER9 i_buffer;
};

#endif



CPlane.cpp



#include "cPlane.h"
struct CUSTOMVERTEX
{
float x, y, z, u, v;
void Init(float _x, float _y, float _z, float _u, float _v)
{
x = _x;
y = _y;
z = _z;
u = _u;
v = _v;
}
};

void CPlane::CreatePlaneXY(float width, float height, float u, float v)
{
_vertices = new CUSTOMVERTEX[4];
_vertices[0].Init(0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
_vertices[1].Init(width, 0.0f, 0.0f, u, 0.0f);
_vertices[2].Init(0.0f, height, 0.0f, 0.0f, v);
_vertices[3].Init(width, height, 0.0f, u, v);

}

void CPlane::CreatePlaneZY(float depth, float height, float u, float v)
{
_vertices = new CUSTOMVERTEX[4];
_vertices[0].Init(0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
_vertices[1].Init(0.0f, 0.0f, depth, u, 0.0f);
_vertices[2].Init(0.0f, height, 0.0f, 0.0f, v);
_vertices[3].Init(0.0f, height, depth, u, v);
}

void CPlane::CreatePlaneXZ(float width, float depth , float u, float v)
{
_vertices = new CUSTOMVERTEX[4];
_vertices[0].Init(0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
_vertices[1].Init(width, 0.0f, 0.0f, u, 0.0f);
_vertices[2].Init(0.0f, 0.0f, depth, 0.0f, v);
_vertices[3].Init(width, 0.0f, depth, u, v);
}

CPlane::CPlane(LPDIRECT3DDEVICE9 d3ddev) : _d3ddev(d3ddev)
{
v_buffer = 0;
i_buffer = 0;

_position = D3DXVECTOR3(0, 0, 0);
_rotation = D3DXVECTOR3(0, 0, 0);

_textures = 0;
_numTextures = 0;
_activeTexture = -1;
_isReady = false;
}

CPlane::CPlane(LPDIRECT3DDEVICE9 d3ddev, D3DXVECTOR3 position, Planes p, float x, float y, float u, float v) : _d3ddev(d3ddev)
{
_position = position;
_rotation = D3DXVECTOR3(0, 0, 0);

CreatePlane(_position, p, x, y, u, v);

_textures = 0;
_numTextures = 0;
_activeTexture = -1;
_isReady = true;
}

CPlane::CPlane(LPDIRECT3DDEVICE9 d3ddev, float x_pos, float y_pos, float z_pos, Planes p, float x, float y, float u, float v) : _d3ddev(d3ddev)
{
_position = D3DXVECTOR3(x_pos, y_pos, z_pos);
_rotation = D3DXVECTOR3(0, 0, 0);

CreatePlane(_position, p, x, y, u, v);

_textures = 0;
_numTextures = 0;
_activeTexture = -1;
_isReady = true;
}

const int CPlane::GetNumTextures() const
{
return _numTextures;
}

const LPD3DTEXTURE CPlane::GetTextureFromIndex(const unsigned int i) const
{
if (i > _numTextures)
return 0;

return _textures;
}

void CPlane::AddTexture(LPD3DTEXTURE texture)
{
LPD3DTEXTURE* temp_array = new LPD3DTEXTURE[_numTextures + 1];

for (int i = 0; i < _numTextures; i++)
{
temp_array = _textures;
}
temp_array[_numTextures] = texture;

delete []_textures;
_textures = temp_array;

_numTextures++;
return;
}

void CPlane::AddTexture(const char* filename)
{
LPD3DTEXTURE texture;
D3DXCreateTextureFromFile(_d3ddev, filename, &texture);

LPD3DTEXTURE* temp_array = new LPD3DTEXTURE[_numTextures + 1];

for (int i = 0; i < _numTextures; i++)
{
temp_array = _textures;
}
temp_array[_numTextures] = texture;

delete []_textures;
_textures = temp_array;

_numTextures++;
return;
}

void CPlane::SetTextureByIndex(const unsigned int index)
{
if (index < _numTextures)
_activeTexture = index;
}

void CPlane::DeleteTextureFromIndex(const unsigned int index)
{/*
LPDIRECT3DTEXTURE9 *temp = new LPDIRECT3DTEXTURE9[_numTextures - 1];
for (int i = 0; i < _numTextures; i++)
{
if (i != index)
temp = _textures;
}*/
}

void CPlane::CreatePlane(D3DXVECTOR3 position, Planes p, float x, float y, float u, float v)
{
_position = position;
CUSTOMVERTEX _vertices[4] =
{
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
};
switch(p)
{
case XY:
CreatePlaneXY(x, y, u, v);
break;
case ZY:
CreatePlaneZY(x, y, u, v);
break;
case XZ:
CreatePlaneXZ(x, y, u, v);
break;
default:
return;
};

short indices[] =
{
0, 1, 2,
2, 1, 3,
};

_d3ddev->CreateVertexBuffer(4 * sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);

VOID* pVoid;
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, _vertices, 4 * sizeof(CUSTOMVERTEX));
v_buffer->Unlock();

_d3ddev->CreateIndexBuffer(6 * sizeof(short),
0,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&i_buffer,
NULL);

i_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, indices, sizeof(indices));
i_buffer->Unlock();
}

void CPlane::Draw()
{

_d3ddev->SetFVF(CUSTOMFVF);

D3DXMATRIX worldMatrix;
D3DXMatrixTranslation(&worldMatrix, _position.x, _position.y, _position.z);
D3DXMATRIX rotationMatrix;
D3DXMatrixRotationY(&rotationMatrix, 0.0f);

_d3ddev->SetTransform(D3DTS_WORLD, &worldMatrix);

_d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
_d3ddev->SetIndices(i_buffer);
if (_activeTexture != -1)
_d3ddev->SetTexture(0, _textures[_activeTexture]);

_d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4, 0, 2);
}



Mario.h

#ifndef _MARIO_H_
#define _MARIO_H_

#include "cPlane.h"
#include <d3d9.h>
#include <d3dx9.h>

#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")

class Mario : public CPlane
{
public:
Mario(LPDIRECT3DDEVICE9 d3ddev);
void Draw();
void Update();

D3DXVECTOR3& GetPosition();
private:
bool direction; //Left = false, Right = true;
bool isWalking;
};

#endif



Mario.cpp

#include "Mario.h"

struct CUSTOMVERTEX
{
float x, y, z, u, v;
};

Mario::Mario(LPDIRECT3DDEVICE9 d3ddev) : CPlane(d3ddev, D3DXVECTOR3(-1.0f, 3.0f, 0.0f), Planes::XY, 2, -3, 1, 1)
{
AddTexture("mario\\idle.png");
AddTexture("mario\\walking.png");

direction = false; //Left
}

D3DXVECTOR3& Mario::GetPosition()
{
return _position;
}

void Mario::Update()
{
isWalking = false;
if (GetAsyncKeyState(VK_LEFT) & 0x8000)
{
direction = false;
_position.x -= 4.0f;
isWalking = true;
}
else if (GetAsyncKeyState(VK_RIGHT) & 0x8000)
{
direction = true;
_position.x += 4.0f;
isWalking = true;
}
if (GetAsyncKeyState(VK_UP) & 0x8000)
{
_position.z += 4.0f;
isWalking = true;
}
else if (GetAsyncKeyState(VK_DOWN) & 0x8000)
{
_position.z -= 4.0f;
isWalking = true;
}
}

void Mario::Draw()
{

_d3ddev->SetFVF(CUSTOMFVF);
static float index = 0.0f;
static int x = 0;
static bool changeTex = false;

x++;
if (x > 3)
{
changeTex= !changeTex ;
x = 0;
}
//index += 0.05f;


D3DXMATRIX WorldMatrix;
D3DXMATRIX RotationMatrix;
D3DXMatrixRotationY(&RotationMatrix, direction ? D3DXToRadian(180) : 0);
D3DXMatrixTranslation(&WorldMatrix,
_position.x,
_position.y,
_position.z);
_d3ddev->SetTransform(D3DTS_WORLD, &(RotationMatrix * WorldMatrix));

_d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
_d3ddev->SetIndices(i_buffer);

_d3ddev->SetTexture(0, (isWalking) ? (changeTex ? GetTextureFromIndex(1) : GetTextureFromIndex(0)) : GetTextureFromIndex(0));

_d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4, 0, 2);
}



main.cpp

#include <Windows.h>
#include "Mario.h"
#include "cPlane.h"
#include <d3d9.h>
#include <d3dx9.h>
#include <vector>
#include <sstream>

#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")

LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
LPD3DXFONT d3dfont;


Mario* mario;
CPlane* test_plane;
CPlane* test_floor;

LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 1;
} break;
}

return DefWindowProc(hWnd, message, wParam, lParam);
}

void InitD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);

D3DPRESENT_PARAMETERS d3dpp;
memset(&d3dpp, 0, sizeof(D3DPRESENT_PARAMETERS));
d3dpp.Windowed = TRUE;
d3dpp.hDeviceWindow = hWnd;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);

D3DXCreateFont(d3ddev,
15,
0,
FW_NORMAL,
1,
false,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE,
"Arial",
&d3dfont);

d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);

d3ddev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);

}

void InitLight()
{
//D3DLIGHT9 light;
D3DMATERIAL9 material;

//memset(&light, 0, sizeof(D3DLIGHT9));
//light.Type = D3DLIGHT_DIRECTIONAL;
//light.Diffuse = D3DXCOLOR(.5f, .5f, .5f, 1.0f);
//light.Direction = D3DXVECTOR3(-0.5f, -1.0f, 0.5f);

//d3ddev->SetLight(0, &light);
//d3ddev->LightEnable(0, TRUE);

memset(&material, 0, sizeof(D3DMATERIAL9));
material.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
material.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);

d3ddev->SetMaterial(&material);
}

void render_frame()
{
d3ddev->Clear(0, 0, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->Clear(0, 0, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

d3ddev->BeginScene();


D3DXVECTOR3 MarioCurrentPosition = mario->GetPosition();
D3DXMATRIX ViewMatrix;
D3DXMatrixLookAtLH(&ViewMatrix,
new D3DXVECTOR3(MarioCurrentPosition.x, MarioCurrentPosition.y + 10.0f, (MarioCurrentPosition.z - 10.0f)),
new D3DXVECTOR3(MarioCurrentPosition.x, MarioCurrentPosition.y + 1.5f, MarioCurrentPosition.z),
new D3DXVECTOR3(0.0f, 1.0f, 0.0f));
d3ddev->SetTransform(D3DTS_VIEW, &ViewMatrix);

D3DXMATRIX ProjectionMatrix;
D3DXMatrixPerspectiveFovLH(&ProjectionMatrix,
D3DXToRadian(45),
(float)800 / (float)600,
0.1f,
100.0f);
d3ddev->SetTransform(D3DTS_PROJECTION, &ProjectionMatrix);

test_floor->Draw();
test_plane->Draw();
mario->Draw();

static RECT TextBox; SetRect(&TextBox, 0, 0, 640, 480);
std::stringstream ss;
ss << "Marios position: " << mario->GetPosition().x << " " << mario->GetPosition().y << " " << mario->GetPosition().z;
d3dfont->DrawTextA(NULL,
ss.str().c_str(),
0,
&TextBox,
DT_LEFT | DT_VCENTER,
D3DCOLOR_XRGB(255, 255, 255));

d3ddev->EndScene();

d3ddev->Present(0, 0, 0, 0);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
HWND hWnd;
WNDCLASSEX wcx;
memset(&wcx, 0, sizeof(WNDCLASSEX));

wcx.lpfnWndProc = WinProc;
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.hbrBackground = (HBRUSH)COLOR_WINDOW;
wcx.hCursor = (HCURSOR)LoadCursor(0, IDC_ARROW);
wcx.hInstance = hInstance;
wcx.lpszClassName = "WindowClass1";

RegisterClassEx(&wcx);

hWnd = CreateWindowEx(0,
"WindowClass1",
"Paper Mario test",
WS_OVERLAPPED | WS_THICKFRAME,
CW_USEDEFAULT,
CW_USEDEFAULT,
800, 600,
0,
0,
hInstance,
0);

ShowWindow(hWnd, nShowCmd);

MSG message;
InitD3D(hWnd);
InitLight();

mario = new Mario(d3ddev);
test_plane = new CPlane(d3ddev, D3DXVECTOR3(0.0f, 1.0f, 0.0f), (CPlane::Planes::XY), 1.0f, 1.0f, 1.0f, 1.0f);
test_floor = new CPlane(d3ddev, D3DXVECTOR3(-10.0f, 0.0f, 10.0f), (CPlane::Planes::XZ), 20.0f, -20.0f, 5.0f, 5.0f);

test_floor->AddTexture("mario\\grass.png");
test_floor->SetTextureByIndex(0);

while (true)
{
if (PeekMessage(&message, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
if (message.message == WM_QUIT)
break;

if (GetAsyncKeyState(VK_ESCAPE) & 0x8000)
{
if (MessageBox(0, "Are you sure you want to quit?", "Prompt", MB_YESNO) == IDYES)
PostMessage(hWnd, WM_DESTROY, 0, 0);
}

mario->Update();
render_frame();
}

return message.wParam;
}



I know that the code is a bit messy but I hope that it's readable.

I hope that any of you can find the problem.

Thanks.

This topic is closed to new replies.

Advertisement