Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

[SOLVED] Polygon being drawn entirely black

Started by wademcgillis Jan 28, 2011 at 5:40 PM 1 replies 950+ views
Original Post
wademcgillis
wademcgillis
This is either my first post or one of my first posts on these forums.

Hi, I'm currently taking an RPG development class at college and we're using "Programming Role Playing Games with Direct X: Second Edition" by Jim Adams.
At the moment, I've copied the code directly from the book, with a few changes to the vertices of the triangle that is supposed to be drawn and the position of the camera.

What is supposed to be drawn is a pure white triangle, but it comes out black. Would anyone with DirectX knowledge please help?

[spoiler]#include <d3d9.h>
#include <d3dx9.h>
#include <windows.h>
#include <stdio.h>
#include <stdarg.h>

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

typedef struct {
//FLOAT x, y, z, rhw; // 2D
FLOAT x, y, z; // 3D
//FLOAT nx, ny, nz;
D3DCOLOR diffuse;
//FLOAT u, v;
} sVertex;

#define VertexFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)

HINSTANCE g_hInst;
HWND g_hWnd;
IDirect3D9 *g_pD3D;
IDirect3DDevice9 *g_pD3DDevice;
IDirect3DTexture9 *g_pD3DTexture;
#define WNDWIDTH 400
#define WNDHEIGHT 400
#define WNDTYPE WS_OVERLAPPEDWINDOW
const char g_szClass[] = "FrameClass";
const char g_szCaption[] = "FrameCaption";

int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow);

void AppError(BOOL Fatal, char *Text, ...);

long FAR PASCAL WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

BOOL RegisterWindowClasses(HINSTANCE hInst);
BOOL UnregisterWindowClasses(HINSTANCE hInst);

HWND CreateMainWindow(HINSTANCE hInst);

BOOL DoInit();
BOOL DoShutdown();
BOOL DoPreFrame();
BOOL DoFrame();
BOOL DoPostFrame();

int WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow)
{
MSG Msg;
g_hInst = hInst;
if (RegisterWindowClasses(hInst) == FALSE)
return FALSE;
if ((g_hWnd = CreateMainWindow(hInst)) == NULL)
return FALSE;
if (DoInit() == TRUE)
{
ZeroMemory(&Msg, sizeof(MSG));
while(Msg.message != WM_QUIT)
{
if (PeekMessage(&Msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
else
{
if (DoPreFrame() == FALSE)
break;
if (DoFrame() == FALSE)
break;
if (DoPostFrame() == FALSE)
break;
}
}
}
DoShutdown();
UnregisterWindowClasses(hInst);
return TRUE;
}

BOOL RegisterWindowClasses(HINSTANCE hInst)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(wcex);
wcex.style = CS_CLASSDC;
wcex.lpfnWndProc = WindowProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInst;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = g_szClass;
wcex.hIconSm = LoadIcon(NULL,IDI_APPLICATION);

if (!RegisterClassEx(&wcex))
return FALSE;
return TRUE;
}

BOOL UnregisterWindowClasses(HINSTANCE hInst)
{
UnregisterClass(g_szClass, hInst);
return TRUE;
}

HWND CreateMainWindow(HINSTANCE hInst)
{
HWND hWnd;
hWnd = CreateWindow(g_szClass, g_szCaption, WNDTYPE, 0,0, WNDWIDTH, WNDHEIGHT, NULL, NULL, hInst, NULL);
if (!hWnd)
return NULL;
ShowWindow(hWnd, SW_NORMAL);
UpdateWindow(hWnd);
return hWnd;
}

void AppError(BOOL Fatal, char *Text, ...)
{
char CaptionText[12];
char ErrorText[2048];
va_list valist;
if (Fatal == FALSE)
strcpy(CaptionText,"Error");
else
strcpy(CaptionText,"Fatal Error");
va_start(valist, Text);
vsprintf(ErrorText, Text, valist);
va_end(valist);
MessageBox(NULL,ErrorText,CaptionText, MB_OK | MB_ICONEXCLAMATION);
if (Fatal == TRUE)
PostQuitMessage(0);
}

long FAR PASCAL WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

BOOL DoInit()
{
if ((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
AppError(TRUE,"D3D9 object not created");
/*
FULLSCREEN
D3DFORMAT d3df;
_D3DDISPLAYMODE d3ddm;
d3ddm.Width = WNDWIDTH;
d3ddm.Height = WNDHEIGHT;
d3ddm.RefreshRate = 0;
d3ddm.Format = D3DFMT_A8R8G8B8;
if (FAILED(g_pD3D->CheckDeviceType(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,&d3df,&d3df,FALSE)))
AppError(TRUE,"Color format not set");
*/
D3DDISPLAYMODE d3ddm;
if (FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm)))//GetDisplayMode
AppError(TRUE,"Color format not set");
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp,sizeof(D3DPRESENT_PARAMETERS));
/*
FULLSCREEN
d3dpp.Windowed = FALSE;
d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
d3dpp.BackBufferFormat = d3dmm.Format;
*/
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;

d3dpp.BackBufferWidth = WNDWIDTH;
d3dpp.BackBufferHeight = WNDHEIGHT;

if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,g_hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp, &g_pD3DDevice)))
AppError(TRUE,"Failed to create D3D9 Device");

if (FAILED(D3DXCreateTextureFromFile(g_pD3DDevice,"texture.png", &g_pD3DTexture)))
AppError(TRUE,"No texture!");

return TRUE;
}

BOOL DoShutdown()
{
return TRUE;
}

BOOL DoPreFrame()
{
return TRUE;
}

BOOL DoFrame()
{
printf("Moo\n");
static FLOAT xx;
xx += 5.0f;
// Clear the backbuffer
if (FAILED(g_pD3DDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_RGBA(96,64,192,255),1.0f,0)))
AppError(TRUE,"Clear failed");
// Enter the Matrix
D3DXMATRIX matXRot, matYRot, matZRot;
D3DXMATRIX matTrans, matScale;
D3DXMatrixRotationX(&matXRot, 0.785f);
D3DXMatrixRotationY(&matYRot, 0.785f);
D3DXMatrixRotationZ(&matZRot, 0.785f);
D3DXMatrixTranslation(&matTrans,100.0f,200.0f,300.0f);
D3DXMatrixScaling(&matScale, 2.0f, 2.0f, 2.0f);
// Transform the world
D3DXMATRIX matWorld;
D3DXMatrixIdentity(&matWorld);
D3DXMatrixMultiply(&matWorld,&matWorld,&matScale);
D3DXMatrixMultiply(&matWorld,&matWorld,&matXRot);
D3DXMatrixMultiply(&matWorld,&matWorld,&matYRot);
D3DXMatrixMultiply(&matWorld,&matWorld,&matZRot);
D3DXMatrixMultiply(&matWorld,&matWorld,&matTrans);
g_pD3DDevice->SetTransform(D3DTS_WORLD,&matWorld);
// Transform the view
D3DXMATRIX matView;
D3DXVECTOR3 vecVP, vecTP, vecUp(0.0f, 1.0f, 0.0f);
vecVP.x = 10;
vecVP.y = 0;
vecVP.z = 150;
vecTP.x = vecTP.y = vecTP.z = xx;//0.0f;
D3DXMatrixLookAtLH(&matView, &vecVP, &vecTP, &vecUp);
g_pD3DDevice->SetTransform(D3DTS_VIEW,&matView);
// Transform the Projection
FLOAT Aspect = (FLOAT) WNDWIDTH / (FLOAT) WNDHEIGHT;
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj,D3DX_PI/4,Aspect,1.0f,1000.0f);
g_pD3DDevice->SetTransform(D3DTS_PROJECTION,&matProj);
//Create vertex buffer
IDirect3DVertexBuffer9 *pD3DVB = NULL;
if(FAILED(g_pD3DDevice->CreateVertexBuffer(sizeof(sVertex)*3,0/*D3DCREATE_WRITEONLY*/, VertexFVF, D3DPOOL_MANAGED, &pD3DVB, NULL)))
AppError(TRUE,"CreateVertexBuffer failed");
// Oh look, a triangle//square...
sVertex Verts[3] = {
{ -100.0f, 100.0f, 100.0f, D3DCOLOR_RGBA( 255, 255, 255, 255) },
{ 100.0f, 100.0f, 100.0f, D3DCOLOR_RGBA( 255, 255, 255, 255) },
{ 100.0f, -100.0f, 100.0f, D3DCOLOR_RGBA( 255, 255, 255, 255) } };//,
//{ -100.0f, -100.0f, 100.0f, D3DCOLOR_RGBA( 0, 0, 255, 255) } };
BYTE *Ptr;
// Lock vertex buffer into memory
if(FAILED(pD3DVB->Lock(0,sizeof(Verts),(void**)&Ptr,0)))
AppError(TRUE,"Lock failed");
memcpy(Ptr, Verts, sizeof(Verts));
pD3DVB->Unlock();
// Materials!
D3DMATERIAL9 d3dm;
ZeroMemory(&d3dm,sizeof(D3DMATERIAL9));
d3dm.Diffuse.r = d3dm.Ambient.r = 1.0f;
d3dm.Diffuse.g = d3dm.Ambient.g = 1.0f;
d3dm.Diffuse.b = d3dm.Ambient.b = 1.0f;
d3dm.Diffuse.a = d3dm.Ambient.a = 1.0f;
// Begin scene
if(FAILED(g_pD3DDevice->BeginScene()))
AppError(TRUE,"BeginScene failed");
// Set stream source and shader
if(FAILED(g_pD3DDevice->SetMaterial(&d3dm)))
AppError(TRUE,"SetMaterial failed");
if(FAILED(g_pD3DDevice->SetStreamSource(0, pD3DVB, 0, sizeof(sVertex))))
AppError(TRUE,"SetStreamSource failed");
if(FAILED(g_pD3DDevice->SetFVF(VertexFVF)))
AppError(TRUE,"SetFVF failed");
if(FAILED(g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1)))
AppError(TRUE,"DrawPrimitive failed");
if(FAILED(g_pD3DDevice->EndScene()))
AppError(TRUE,"EndScene failed");
// I believe this is similar to glFlush();
if (FAILED(g_pD3DDevice->Present(NULL,NULL,NULL,NULL)))
AppError(TRUE,"Present failed");

return TRUE;
}

BOOL DoPostFrame()
{
return TRUE;
}


[/spoiler]
Erik Rufelt
Erik Rufelt
Try using g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE), as lighting is enabled by default and requires normals.
wademcgillis
wademcgillis
Thanks! That worked.

The author never mentioned the code wouldn't work because of that reason, but I also haven't read the lighting section yet.

I expected the code he gave to work after each section was finished, but I guess I was wrong.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.