Sign in to follow this  
Hexparse

Indexed cube

Recommended Posts

Hello, I've been working on a small hobby game engine for a while now, and I decided it was time to clean some of the code.
I somehow screwed up and I can't find out what. At the moment the project is supposed to render a simple indexed cube and rotate it around an arbitrary axis.
All I know is that, the camera and cube position is unchanged and (should) render.

I need all the help I can get. Thanks in advance!

Here's the camera class
[code]
class Camera
{

private:

D3DXVECTOR3 m_vRight;
D3DXVECTOR3 m_vUp;
D3DXVECTOR3 m_vLook;
D3DXVECTOR3 m_vPos;

D3DXMATRIX m_matProj;
D3DXMATRIX m_matView;
D3DXMATRIX m_matRotate;

public:

Camera();
~Camera();

void Transform();
}








Camera::Camera()
{

m_vRight = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
m_vUp = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
m_vLook = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
m_vPos = D3DXVECTOR3(0.0f, 0.0f, 20.0f);
}

void Camera::Transform()
{
D3DXMATRIX matResult;

// Projection matrix
D3DXMatrixIdentity(&matResult);

D3DXMatrixPerspectiveFovLH(&m_matProj,
D3DX_PI / 4, 800 / 600, 1.0f, 2000.0f);

matResult *= m_matProj;

engine->GetDeviceHandle()->SetTransform(D3DTS_PROJECTION, &matResult);

// View matrix
D3DXMatrixIdentity(&matResult);

D3DXMatrixLookAtLH(&m_matView, &m_vPos, &m_vLook, &m_vUp);

matResult *= m_matView;

engine->GetDeviceHandle()->SetTransform(D3DTS_VIEW, &matResult);
}

Camera::~Camera()
{
//
}
[/code]

Here's the mesh class
[code]

class Mesh
{

private:

LPD3DXMESH m_lpMesh;
LPDIRECT3DVERTEXBUFFER9 m_lpVertexBuffer;
LPDIRECT3DINDEXBUFFER9 m_lpIndexBuffer;
D3DXMATRIX m_matWorld;

public:

Mesh();
~Mesh();

void Draw();
void Transform(D3DXVECTOR3 vRotation, float fTheta);
void CreateBox();
};








void Mesh::Draw()
{
engine->GetDeviceHandle()->SetStreamSource(0, m_lpVertexBuffer, 0, sizeof(VERTEX));
engine->GetDeviceHandle()->SetIndices(m_lpIndexBuffer);
engine->GetDeviceHandle()->SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE);
engine->GetDeviceHandle()->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);// 8 vertices, 12 triangles
}

void Mesh::Transform(D3DXVECTOR3 vRotation, float fTheta)
{
D3DXMatrixIdentity(&m_matWorld);

D3DXMATRIX matTranslate;
D3DXMatrixIdentity(&matTranslate);
D3DXMatrixTranslation(&matTranslate, 0.0f, 0.0f, 0.0f);

D3DXMATRIX matScale;
D3DXMatrixIdentity(&matScale);
D3DXMatrixScaling(&matScale, 1.0f, 1.0f, 1.0f);

D3DXMATRIX matRotate;
D3DXMatrixIdentity(&matRotate);

// Rotate on an arbitrary axis
if(vRotation.x != 0.0f || vRotation.y != 0.0f || vRotation.z != 0.0f)
{
float vUnit = sqrtf((vRotation.x * vRotation.x) + (vRotation.y * vRotation.y) + (vRotation.z * vRotation.z));
vRotation.x /= vUnit;
vRotation.y /= vUnit;
vRotation.z /= vUnit;

matRotate._11 = (vRotation.x * vRotation.x) * (1.0f - cos(fTheta)) + cos(fTheta);
matRotate._12 = (vRotation.x * vRotation.y) * (1.0f - cos(fTheta)) - (vRotation.z * sin(fTheta));
matRotate._13 = (vRotation.x * vRotation.z) * (1.0f - cos(fTheta)) + (vRotation.y * sin(fTheta));
matRotate._14 = 0.0f;

matRotate._21 = (vRotation.y * vRotation.x) * (1.0f - cos(fTheta)) + (vRotation.z * sin(fTheta));
matRotate._22 = (vRotation.y * vRotation.y) * (1.0f - cos(fTheta)) + cos(fTheta);
matRotate._23 = (vRotation.y * vRotation.z) * (1.0f - cos(fTheta)) - (vRotation.x * sin(fTheta));
matRotate._24 = 0.0f;

matRotate._31 = (vRotation.z * vRotation.x) * (1.0f - cos(fTheta)) - (vRotation.y * sin(fTheta));
matRotate._32 = (vRotation.z * vRotation.y) * (1.0f - cos(fTheta)) + (vRotation.x * sin(fTheta));
matRotate._33 = (vRotation.z * vRotation.z) * (1.0f - cos(fTheta)) + cos(fTheta);
matRotate._34 = 0.0f;

matRotate._41 = 0.0f;
matRotate._42 = 0.0f;
matRotate._43 = 0.0f;
matRotate._44 = 1.0f;
}

m_matWorld = m_matWorld * matTranslate * matScale * matRotate;

engine->GetDeviceHandle()->SetTransform(D3DTS_WORLD, &m_matWorld);

engine->GetDeviceHandle()->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); // Temp
engine->GetDeviceHandle()->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); // Temp
}

void Mesh::CreateBox()
{
// =====================================================================
// Vertex buffer
// =====================================================================

engine->GetDeviceHandle()->CreateVertexBuffer(sizeof(VERTEX) * 8,
0,
D3DFVF_XYZ | D3DFVF_DIFFUSE,
D3DPOOL_DEFAULT,
&m_lpVertexBuffer,
NULL);

VERTEX *vertices;
m_lpVertexBuffer->Lock(0, sizeof(VERTEX) * 8, (void**)&vertices, 0);


// Closest, top, left
vertices[0].x = -2.0f;
vertices[0].y = -2.0f;
vertices[0].z = 2.0f;
vertices[0].color = 0xFFFFFFFF;

// Closest, top, right
vertices[1].x = 2.0f;
vertices[1].y = -2.0f;
vertices[1].z = 2.0f;
vertices[1].color = 0xFFFFFFFF;

// Closest, bottom, left
vertices[2].x = -2.0f;
vertices[2].y = 2.0f;
vertices[2].z = 2.0f;
vertices[2].color = 0xFFFFFFFF;

// Closest, bottom, right
vertices[3].x = 2.0f;
vertices[3].y = 2.0f;
vertices[3].z = 2.0f;
vertices[3].color = 0xFFFFFFFF;

// Furthest, top, left
vertices[4].x = -2.0f;
vertices[4].y = -2.0f;
vertices[4].z = -2.0f;
vertices[4].color = 0xFFFFFFFF;

// Furthest, top, right
vertices[5].x = 2.0f;
vertices[5].y = -2.0f;
vertices[5].z = -2.0f;
vertices[5].color = 0xFFFFFFFF;

// Furthest, bottom, left
vertices[6].x = -2.0f;
vertices[6].y = 2.0f;
vertices[6].z = -2.0f;
vertices[6].color = 0xFFFFFFFF;

// Furthest, bottom, right
vertices[7].x = 2.0f;
vertices[7].y = 2.0f;
vertices[7].z = -2.0f;
vertices[7].color = 0xFFFFFFFF;

m_lpVertexBuffer->Unlock();

// =====================================================================
// Index buffer
// =====================================================================

engine->GetDeviceHandle()->CreateIndexBuffer(36 * sizeof(WORD),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&m_lpIndexBuffer,
0);

WORD *indices;

m_lpIndexBuffer->Lock(0, 36 * sizeof(WORD), (void**)&indices, 0);

// Front
indices[0] = 0; indices[1] = 1; indices[2] = 2;
indices[3] = 1; indices[4] = 3; indices[5] = 2;

// Back
indices[6] = 4; indices[7] = 6; indices[8] = 5;
indices[9] = 6; indices[10] = 7; indices[11] = 5;

// Left
indices[12] = 4; indices[13] = 0; indices[14] = 6;
indices[15] = 2; indices[16] = 6; indices[17] = 0;

// Right
indices[18] = 1; indices[19] = 5; indices[20] = 3;
indices[21] = 7; indices[22] = 3; indices[23] = 5;

// Top
indices[24] = 4; indices[25] = 5; indices[26] = 0;
indices[27] = 1; indices[28] = 0; indices[29] = 5;

// Bottom
indices[30] = 2; indices[31] = 3; indices[32] = 6;
indices[33] = 7; indices[34] = 6; indices[35] = 3;

m_lpIndexBuffer->Unlock();
}

Mesh::~Mesh()
{
//
}

[/code]

Share this post


Link to post
Share on other sites
[font="Arial"]I can see two things that could be a problem.
1. The order in which you multiply the matricies to work out the world matrix seems to be mixed up,

[/font][font="Arial"]m_matWorld = m_matWorld * matTranslate * matScale * matRotate; have a read of [url="http://msdn.microsoft.com/en-us/library/bb206269%28v=vs.85%29.aspx"]http://msdn.microsof...v=vs.85%29.aspx[/url] , basically you need to go
[/font][font="Arial"]m_matWorld = m_matWorld * [/font][font="Arial"]matRotate[/font][font="Arial"] * [/font][font="Arial"]matScale[/font][font="Arial"] * [/font][font="Arial"]matTranslate ;

Although to be honest you are not translating or scaling so it shouldnt affect it. Is there a reason you are maintaining the world matrix? Does this cube have a parent is needs to rendered in relation to? If not you can leave the m_matWorld out of the multiplication.

2. Both your eye and look vector for the camera is on the positive z axis, which is into the screen for directx, so you will essentially be
rendering the back of the cube, which doesnt solve your problem at all, but may solve others later down the track.[/font]

Share this post


Link to post
Share on other sites
If you mean the D3DXMatrixIdentity(&matWorld), I just forgot to remove it.
I still can't figure out any solution to the problem but I think it will show up sooner or later. It's probably a small bug that's going to result in a facepalm. :)

EDIT:
Finally solved.
I used the void pointers incorrectly when saving the vertex and index data and I also had to turn lighting off since I didn't have any light source.

I guess using void pointers can be tricky.

Share this post


Link to post
Share on other sites
[quote name='Hexparse' timestamp='1313295973' post='4848859']and I also had to turn lighting off since I didn't have any light source.

[/quote]
A small tip here: never clear your back buffer to black or white color during development, that can hide some problems. If you clear to black and your rendering is black (because you have lighting on but don't use any lights), it seems to you that nothing is rendered and it sends you to a wrong track when trying to find the problem.

Use some "random" color.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this