Original Post
Hey,
I am trying to render a scene with triangle in a driect3d window. However I get only a blue screen. I can't figure out why I can't get anything triangle to display on screen. As this is my first program with Direct3D I am sure I am doing something really stupid. Here is my code
This is my main.cpp file.
Hope someone can figure out what I am doing wrong.
Thank you.
[edited by - CRACK123 on May 23, 2003 10:21:37 AM]
#include "framework.h"
#include "vertex.h"
#include <mmsystem.h>
LPDIRECT3D8 g_pD3D = NULL;
LPDIRECT3DDEVICE8 g_pD3dDevice = NULL;
HWND g_hWnd = NULL;
LPDIRECT3DVERTEXBUFFER8 g_pVB = NULL;
void InitGeometry()
{
CUSTOMVERTEX vertices[] = {
{ -1.0f,-1.0f, 0.0f, 0xffff0000, },
{ 1.0f,-1.0f, 0.0f, 0xff0000ff, },
{ 0.0f, 1.0f, 0.0f, 0xffffffff, },
};
g_pD3dDevice->CreateVertexBuffer(3 * sizeof(CUSTOMVERTEX),
0, MYCUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB);
VOID *pVertices;
g_pVB->Lock(0, sizeof(vertices), (BYTE **)&pVertices, 0);
memcpy(pVertices, vertices, sizeof(vertices));
g_pVB->Unlock();
}
void SetupMatrices()
{
D3DXMATRIX matWorld;
D3DXMatrixRotationY(&matWorld, timeGetTime() / 150);
g_pD3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0, 3, -5),
&D3DXVECTOR3(0, 0, 0),
&D3DXVECTOR3(0, 1, 0));
g_pD3dDevice->SetTransform(D3DTS_VIEW, &matView);
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1, 1, 100);
g_pD3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}
void Render()
{
g_pD3dDevice->Clear(0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB(0, 0, 128), 1, 0);
g_pD3dDevice->BeginScene();
SetupMatrices();
g_pD3dDevice->SetStreamSource(0, g_pVB, sizeof(CUSTOMVERTEX));
g_pD3dDevice->SetVertexShader(MYCUSTOMVERTEX);
g_pD3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 1);
g_pD3dDevice->EndScene();
g_pD3dDevice->Present( NULL, NULL, NULL, NULL );
}
void Cleanup()
{
if(g_pVB != NULL) {
g_pVB->Release();
g_pVB = NULL;
}
DestroyD3D();
}
LRESULT CALLBACK ChickenWindowProc(HWND hWnd, UINT Msg,
WPARAM wParam, LPARAM lParam)
{
switch(Msg) {
case WM_DESTROY :
DestroyD3D();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
HWND InitWindow(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(wc));
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = ChickenWindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hIconSm = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "Chicken Hunting";
RegisterClassEx(&wc);
hWnd = CreateWindowEx(0, "Chicken Hunting", "Chicken Hunting",WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 256, 256,
GetDesktopWindow(), NULL, hInstance, NULL);
if(!hWnd)
exit(-1);
return hWnd;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
bool done = false;
g_hWnd = InitWindow(hInstance, nCmdShow);
InitD3D();
InitGeometry();
while (!done) {
PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE);
if (msg.message == WM_QUIT) {
done = true;
} else {
Render();
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Cleanup();
UnregisterClass("Chicken Hunting", hInstance);
return 0;
}
This is my framework to setup direct3d 8
#include "framework.h"
void InitD3D()
{
g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
D3DDISPLAYMODE d3ddm;
g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
// setup back buffer width and height
d3dpp.BackBufferWidth = 0;
d3dpp.BackBufferHeight = 0;
// request a back buffer format similar to display format
d3dpp.BackBufferFormat = d3ddm.Format;
// 1 back buffer only requested
d3dpp.BackBufferCount = 1;
// turn of multi sampling
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
// dont care about front buffer contents, render every loop
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
// use same window handle
d3dpp.hDeviceWindow = g_hWnd;
// windowed mode
d3dpp.Windowed = TRUE;
// don't manage depth buffer
d3dpp.EnableAutoDepthStencil = FALSE;
d3dpp.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
// no special flags
d3dpp.Flags = 0;
// setup a default refresh rate
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
// create a d3d device
g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
g_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pD3dDevice);
// Turn off culling, so we see the front and back of the triangle
g_pD3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
// Turn off D3D lighting, since we are providing our own vertex colors
g_pD3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
}
void DestroyD3D()
{
if(g_pD3dDevice != NULL) {
g_pD3dDevice->Release();
g_pD3dDevice = NULL;
}
if(g_pD3D != NULL) {
g_pD3D->Release();
g_pD3D = NULL;
}
}
This is my header file to setup a vertex structure
#ifndef VERTEX_H
#define VERTEX_H
#define MYCUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
struct CUSTOMVERTEX {
FLOAT x, y, z;
DWORD color;
};
#endif