Original Post
So when I run this code, it rotates a triangle. Then when it comes to the back of the triangle, you don't see it. Why not? Don't tell how yet though! :D
#define WIN32_LEAN_AND_MEAN
#include<windows.h>
#include<windowsx.h>
#include<d3d9.h>
#include<d3dx9.h>
#define WINCLASS "winclass"
#define KEYDOWN(VK_CODE) ((GetAsyncKeyState(VK_CODE)& 0x8000) ? 1 : 0)
float a = 0.0f, b = 200, c = 400;
int d = 255;
float scaleIndex = 1.0f;
typedef struct
{
FLOAT x,y,z;
DWORD color;
} sVertex;
#define VertexFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)
IDirect3D9 *g_D3D;
IDirect3DDevice9 *g_pD3DDevice;
IDirect3DVertexBuffer9 *pD3DVB = NULL;
IDirect3DTexture9 *pD3DTexture;
HWND windowhandle;
int windowclosed = 0;
int Game_Main();
int Game_Init();
int Game_ShutDown();
LRESULT CALLBACK WinProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
switch(msg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}break;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX winclass;
HWND hwnd;
MSG msg;
winclass.cbClsExtra = 0;
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.cbWndExtra = 0;
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
winclass.hInstance = hinstance;
winclass.lpfnWndProc = WinProc;
winclass.lpszClassName = WINCLASS;
winclass.lpszMenuName = NULL;
winclass.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
if(!RegisterClassEx(&winclass))
return 0;
if(!(hwnd = CreateWindowEx(NULL,
WINCLASS,
"Paintz",
WS_POPUP | WS_VISIBLE,
0,0,
800,600,
NULL,
NULL,
hinstance,
NULL)))
return 0;
windowhandle = hwnd;
Game_Init();
while(TRUE)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Game_Main();
}
Game_ShutDown();
return msg.lParam;
}
int Game_Main()
{
if(KEYDOWN(VK_ESCAPE))
{
SendMessage(windowhandle, WM_DESTROY, NULL, NULL);
windowclosed = 1;
}
if(windowclosed == 1)
return 0;
if(FAILED(g_pD3DDevice->CreateVertexBuffer(sizeof(sVertex)* 4, 0, VertexFVF, D3DPOOL_MANAGED, &pD3DVB, NULL)))
return 0;
if(d == 255)
d = 0;
else
d++;
sVertex Verts[4] =
{
{ 2.5f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, d), },
{ 0.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, d, 0), },
{ -2.5f, -3.0f, 0.0f, D3DCOLOR_XRGB(d, 0, 0), },
};
BYTE *ptr;
if(SUCCEEDED(pD3DVB->Lock(0,0, (void**)&ptr,0)))
{
memcpy(ptr, Verts, sizeof(Verts));
pD3DVB->Unlock();
}
if(FAILED(g_pD3DDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_RGBA(0,0,0,0),1.0f,0)))
return 0;
/*if(FAILED(D3DXCreateTextureFromFile(g_pD3DDevice, "grass.bmp", &pD3DTexture)))
return 0;
if(FAILED(g_pD3DDevice->SetTexture(0, pD3DTexture)))
return 0;*/
if(FAILED(g_pD3DDevice->SetFVF(VertexFVF)))
return 0;
if(FAILED(g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE)))
return 0;
if(FAILED(g_pD3DDevice->BeginScene()))
return 0;
D3DXMATRIX matRotateY; // a matrix to store the rotation information
D3DXMATRIX matRotateX; // a matrix to store the rotation information
D3DXMATRIX matRotateZ; // a matrix to store the rotation information
static float index = 0.0f; index-=0.05f; // an ever-increasing float value
D3DXMatrixRotationY(&matRotateY, index);
D3DXMatrixRotationX(&matRotateX, index);
D3DXMatrixRotationX(&matRotateZ, index);
D3DXMATRIX matScale;
scaleIndex -= 0.00f;
D3DXMatrixScaling(&matScale, scaleIndex, scaleIndex, scaleIndex);
if(FAILED(g_pD3DDevice->SetTransform(D3DTS_WORLD, &(matScale * matRotateY * matRotateX * matRotateZ))))
return 0;
D3DXMATRIX matView; // the view transform matrix
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3 (0.0f, 0.0f, 10.0f), // the camera position
&D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
if(FAILED(g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView)))
return 0;// set the view transform to matView
a += 0.1f;
D3DXMATRIX matProjection; // the projection transform matrix
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(45), // the horizontal field of view
800 / 600, // the aspect ratio
0.1f, // the near view-plane
100.0f); // the far view-plane
if(FAILED(g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProjection)))
return 0;// set the projection
if(FAILED(g_pD3DDevice->SetStreamSource(0,pD3DVB,0,sizeof(sVertex))))
return 0;
if(FAILED(g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,1)))
return 0;
if(FAILED(g_pD3DDevice->EndScene()))
return 0;
if(FAILED(g_pD3DDevice->Present(NULL,NULL,NULL,NULL)))
return 0;
/*if(FAILED(g_pD3DDevice->SetTexture(0, NULL)))
return 0;*/
return 0;
}
int Game_Init()
{
if((g_D3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
{
return 0;
}
D3DDISPLAYMODE d3ddm;
d3ddm.Width = 800;
d3ddm.Height = 600;
d3ddm.RefreshRate = 0;
d3ddm.Format = D3DFMT_R5G6B5;
D3DFORMAT d3df = D3DFMT_R5G6B5;
if(FAILED(g_D3D->CheckDeviceType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,d3df,d3df, FALSE)))
{
return 0;
}
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
d3dpp.Windowed = FALSE;
d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
d3dpp.BackBufferHeight = 600;
d3dpp.BackBufferWidth = 800;
if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, windowhandle, D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp, &g_pD3DDevice)))
return 0;
return 0;
}
int Game_ShutDown()
{
return 0;
}