Trouble getting colors to show on 2 triangles
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Trouble getting colors to show on 2 triangles

  1. #1
    Join Date
    Apr 2011
    Posts
    3

    Trouble getting colors to show on 2 triangles

    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.

    Code:
    #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;
    }

  2. #2
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,553

    Re: Trouble getting colors to show on 2 triangles

    Quote Originally Posted by mlwhiteley View Post
    ... I have copied the code just as it is in the book ...
    I think you should double-check on the accuracy of your copying. In the first few lines, I noticed the following, which is probably a copy error:

    Code:
    #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;
    
    		// the above line is probably wrong
    		// and probably should read as follows:
    
    		_x = x; _y = y; _z = z; _color = color;
    	}
    	
    	float _x, _y, _z;
    	D3DCOLOR _color;
    	static const DWORD FVF;
    };
    
    //  ... remainder of code omitted
    You might have noticed this particular error more quickly if you had adhered to the code-formatting advice of "each line of code on a line of its own". If you had, then the error would have been much more evident:
    Code:
    	Vertex(float x, float y, float z, D3DCOLOR color)
    	{
    		_x = x; 
    		_y = y; 
    		color = _color;  // note how easy it is to spot this error
    	}
    With this formatting of the code, the error jumps out at you, even without the color highlighting provided above.

    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center