Original Post
Hi, Im trying to learn how to use index buffers, Im trying just to make a cube and have it rotate on the Y axis, but nothing is showing up. I've looked through the code for an hour and can't see what I'm doing wrong. If anyone can please help me, it's probably something simple.
#include <windows.h>
#include "d3d9.h"
#include <d3dx9.h>
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
IDirect3D9 *g_pD3D;
IDirect3DDevice9 *g_pD3DDevice;
IDirect3DVertexBuffer9 *g_pVB;
IDirect3DIndexBuffer9 *g_pIB;
float angle = 0.0f;
#define SAFE_RELEASE(x){ if(x){ x->Release(); x = NULL; } }
#define sVertexFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)
struct sVertex
{
float x, y, z;
D3DCOLOR color;
};
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam);
bool InitD3D(HWND hWnd);
bool Render();
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX wcex;
wcex.cbClsExtra = 0;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.cbWndExtra = 0;
wcex.hbrBackground = (HBRUSH)GetStockObject(0);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wcex.hInstance = hInst;
wcex.lpfnWndProc = WndProc;
wcex.lpszClassName = "TEST";
wcex.lpszMenuName = NULL;
wcex.style = CS_CLASSDC;
RegisterClassEx(&wcex);
HWND hWnd = CreateWindow("TEST", "TEST", WS_OVERLAPPEDWINDOW,
0, 0, 800, 600, NULL, NULL, hInst, NULL);
UpdateWindow(hWnd);
ShowWindow(hWnd, nShowCmd);
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
InitD3D(hWnd);
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
Render();
}
}
SAFE_RELEASE(g_pIB);
SAFE_RELEASE(g_pVB);
SAFE_RELEASE(g_pD3DDevice);
SAFE_RELEASE(g_pD3D);
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
switch(iMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
}
return DefWindowProc(hWnd, iMsg, wParam, lParam);
}
bool InitD3D(HWND hWnd)
{
if((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
{
return false;
}
D3DPRESENT_PARAMETERS params;
ZeroMemory(¶ms, sizeof(D3DPRESENT_PARAMETERS));
params.Windowed = true;
params.SwapEffect = D3DSWAPEFFECT_DISCARD;
params.BackBufferFormat = D3DFMT_UNKNOWN;
params.EnableAutoDepthStencil = true;
params.AutoDepthStencilFormat = D3DFMT_D16;
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, ¶ms, &g_pD3DDevice)))
{
return false;
}
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, false);
g_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
D3DXMATRIX matProj, matView;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4, 1.333f, 1.0f, 1000.0f);
g_pD3DDevice->SetTransform(D3DTS_WORLD, &matProj);
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3(0.0f, 0.0f, -5.0f),
&D3DXVECTOR3(0.0f, 0.0f, 0.0f),
&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);
sVertex verts[8] =
{
{ -1.0f, -1.0f, -1.0f, D3DCOLOR_RGBA(0, 0, 55, 255), },
{ -1.0f, 1.0f, -1.0f, D3DCOLOR_RGBA(0, 0, 255, 255), },
{ 1.0f, -1.0f, -1.0f, D3DCOLOR_RGBA(0, 0, 255, 255), },
{ 1.0f, 1.0f, -1.0f, D3DCOLOR_RGBA(0, 0, 55, 255), },
{ 1.0f, -1.0f, 1.0f, D3DCOLOR_RGBA(0, 0, 55, 255), },
{ 1.0f, 1.0f, 1.0f, D3DCOLOR_RGBA(0, 0, 255, 255), },
{ -1.0f, -1.0f, 1.0f, D3DCOLOR_RGBA(0, 0, 255, 255), },
{ -1.0f, 1.0f, 1.0f, D3DCOLOR_RGBA(0, 0, 55, 255), }
};
if(FAILED( g_pD3DDevice->CreateVertexBuffer(sizeof(sVertex)*8, 0, sVertexFVF,
D3DPOOL_MANAGED, &g_pVB, NULL) ))
{
return false;
}
VOID *Ptr = NULL;
g_pVB->Lock(0, 0, (VOID**)&Ptr, 0);
memcpy(Ptr, verts, sizeof(sVertex)*8);
g_pVB->Unlock();
short indices[36] =
{
0, 1, 2,
2, 1, 3,
2, 3, 4,
4, 3, 5,
4, 5, 6,
6, 5, 7,
6, 7, 0,
0, 7, 1,
1, 7, 3,
3, 7, 5,
0, 2, 6,
2, 4, 6
};
if(FAILED(g_pD3DDevice->CreateIndexBuffer(sizeof(short)*36, 0, D3DFMT_INDEX16,
D3DPOOL_MANAGED, &g_pIB, NULL)))
{
return false;
}
VOID *iPtr = NULL;
g_pIB->Lock(0, 0, (VOID**)&iPtr, 0);
memcpy(iPtr, indices, sizeof(short)*36);
g_pIB->Unlock();
}
bool Render()
{
D3DXMATRIX matMatrix;
g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_RGBA(255,255,255,255), 1.0f, 0);
g_pD3DDevice->BeginScene();
D3DXMatrixRotationY(&matMatrix, angle);
g_pD3DDevice->SetTransform(D3DTS_WORLD, &matMatrix);
g_pD3DDevice->SetStreamSource(0, g_pVB, 0, sizeof(sVertex));
g_pD3DDevice->SetIndices(g_pIB);
g_pD3DDevice->SetFVF(sVertexFVF);
g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
g_pD3DDevice->EndScene();
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
angle += 0.05f;
return true;
}