Trouble getting colors to show on 2 triangles with DirectX

Hi, I am working from a book to build a DirectX program that displays 2 triangles. One is 1 solid color and the other is a blend of 3 colors. I have copied the code just as it is in the book but it the colors are not showing up, both triangles are grey. Everything works in the code but the colors.

Any help would be great, thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "base.h"

//
// Globals
//
IDirect3DDevice9* Device = 0;
const int Width = 640;
const int Height = 480;
D3DXMATRIX World;
// vertex buffer to store our triangle data.
IDirect3DVertexBuffer9* Triangle = 0;

//
// Classes and Structures
//
struct Vertex
{
	Vertex(){}
	Vertex(float x, float y, float z)
	{
		_x = x; _y = y; _z = z;
	}
	Vertex(float x, float y, float z, D3DCOLOR color)
	{
		_x = x; _y = y; _z = z; color = _color;
	}
	
	float _x, _y, _z;
	D3DCOLOR _color;
	static const DWORD FVF;
};

const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;

//
// Framework Functions
//
bool Setup()
{
	//
	// Create the vertex buffer.
	//
	Device->CreateVertexBuffer(
		3 * sizeof(Vertex), // size in bytes
		D3DUSAGE_WRITEONLY, // flags
		Vertex::FVF, // vertex format
		D3DPOOL_MANAGED, // managed memory pool
		&Triangle, // return create vertex buffer
		0); // not used - set to 0
	
	//
	// Fill the buffers with the triangle data.
	//
	Vertex* vertices;
	Triangle->Lock(0, 0, (void**)&vertices, 0);
	vertices[0] = Vertex(-1.0f, 0.0f, 2.0f, D3DCOLOR_XRGB(255, 0, 0));
	vertices[1] = Vertex( 0.0f, 1.0f, 2.0f, D3DCOLOR_XRGB(0, 255, 0));
	vertices[2] = Vertex( 1.0f, 0.0f, 2.0f, D3DCOLOR_XRGB(0, 0, 255));
	Triangle->Unlock();

	//
	// Set the projection matrix.
	//
	D3DXMATRIX proj;
	D3DXMatrixPerspectiveFovLH(
		&proj, // result
		D3DX_PI * 0.5f, // 90 - degrees
		(float)Width / (float)Height, // aspect ratio
		1.0f, // near plane
		1000.0f); // far plane
	Device->SetTransform(D3DTS_PROJECTION, &proj);

	Device->SetRenderState(D3DRS_LIGHTING, false);
	//Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
	return true;
}

void Cleanup()
{
	d3d::Release<IDirect3DVertexBuffer9*>(Triangle);
}

bool Display(float timeDelta)
{
	if( Device )
	{
		Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
		Device->BeginScene();
		Device->SetFVF(Vertex::FVF);
		Device->SetStreamSource(0, Triangle, 0, sizeof(Vertex));

		D3DXMatrixTranslation(&World, -1.25f, 0.0f, 0.0f);
		Device->SetTransform(D3DTS_WORLD, &World);

		Device ->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT);
		Device ->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

		D3DXMatrixTranslation(&World, 1.25f, 0.0f, 0.0f);
		Device->SetTransform(D3DTS_WORLD, &World);

		Device ->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
		Device ->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

		Device->EndScene();
		Device->Present(0, 0, 0, 0);
	}
	return true;
}

//
// WndProc
//
LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch( msg )
	{
		case WM_DESTROY:
			::PostQuitMessage(0);
			break;
		case WM_KEYDOWN:
			if( wParam == VK_ESCAPE )
				::DestroyWindow(hwnd);
			break;
	}

	return ::DefWindowProc(hwnd, msg, wParam, lParam);
}

//
// WinMain
//
int WINAPI WinMain(
	HINSTANCE hinstance,
	HINSTANCE prevInstance,
	PSTR cmdLine,
	int showCmd)
{
	if(!d3d::InitD3D(hinstance,Width, Height, true, D3DDEVTYPE_HAL, &Device))
	{
		::MessageBox(0, "InitD3D() - FAILED", 0, 0);
		return 0;
	}

	if(!Setup())
	{
		::MessageBox(0, "Setup() - FAILED", 0, 0);
		return 0;
	}

	d3d::EnterMsgLoop( Display );
	Cleanup();
	Device->Release();
	return 0;
}
Try this,

D3DFVF_XYZ means untransformed vertices and
D3DFVF_XYZRHW means transformed vertices

const DWORD Vertex::FVF = D3DFVF_XYZRHW | D3DFVF_DIFFUSE;
instead of,
const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;
Last edited on
closed account (zb0S216C)
Try using a DWORD for your vertices colour type.

Wazzak
Topic archived. No new replies allowed.