Direct3D quick question

This is a discussion on Direct3D quick question within the C++ Programming forums, part of the General Programming Boards category; This code compiles and runs but refuses to display 2 vertex buffers at the same time. This part is the ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    9

    Direct3D quick question

    This code compiles and runs but refuses to display 2 vertex buffers at the same time.

    This part is the setup for the buffers:
    Code:
    MyVertexType vertArray[] =
    	{
    		{0,0,0,norm ,D3DCOLOR_XRGB(0,255,255)},
    		{0,240,0,norm ,D3DCOLOR_XRGB(0,255,255)},
    		{320,0,0,norm ,D3DCOLOR_XRGB(0,255,255)},
    		{320,240,0,norm ,D3DCOLOR_XRGB(0,255,255)},
    
    	};
    
    	//primitive points vertex buffer
    	MyVertexType pointListArray[] =
    	{
    		{10,10,0,norm ,D3DCOLOR_XRGB(255,255,255)},
    		{300,200,0,norm ,D3DCOLOR_XRGB(255,255,255)},
    		{280,195,0,norm ,D3DCOLOR_XRGB(255,255,255)},
    		{220,110,0,norm ,D3DCOLOR_XRGB(255,255,255)},
    		{115,25,0,norm ,D3DCOLOR_XRGB(255,255,255)},
    		{193,94,0,norm ,D3DCOLOR_XRGB(255,255,255)},
    	};
    And this part is used to draw these vectors:
    Code:
    void MyApp::Render()
    {
    	D3DCOLOR background = 0x000000;
    
    	if (pDevice)
    	{
    		pDevice->Clear(0,0,(D3DCLEAR_TARGET| D3DCLEAR_ZBUFFER),background,1.0f,0);  //set background colour
    		pDevice->BeginScene();
    		pDevice->SetFVF(MY_VERTEX_TYPE);
    
    		//draw rectangle
                    pDevice->SetStreamSource(0,verBuff,0,sizeof(MyVertexType));
    		pDevice->SetIndices( inBuff );
    		pDevice->DrawIndexedPrimitive(D3DPT_LINELIST,0,0,4,0,4);
    
    		//draw points
                    /*pDevice->SetStreamSource(0,pointVerBuff,0,sizeof(MyVertexType));
    		pDevice->SetFVF(MY_VERTEX_TYPE);
    		pDevice->DrawPrimitive(D3DPT_POINTLIST,0,6);*/
    		
    		//moves camera around
    		Transforms();
    		//attributes for rendering
    		RenderStates();
    		pDevice->EndScene();
    		pDevice->Present(0,0,0,0);
    	}
    }
    This code draws a rectangle, if you comment out the 3 lines under "draw rectangle" and remove the comments around "draw points" then it will draw the points instead. How do I make it do both at once?

    Cheers

  2. #2
    NotSoAvgProgrammer
    Join Date
    Jul 2007
    Location
    Virginia, U.S.
    Posts
    57
    I only know openGL, but did you make sure that the points are different colors than the rectangle, to insure that you can see them visibly?
    A quality job is not necessarily a *good* job. A quality job is, by definition, conformance to requirements. Therefore a *good* job, may not be a quality job.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    There is no way to tell what is going on. You do not show your vertex declaration, your projection matrix, and you are not setting a world matrix.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    9
    Sorry, here's the code for that class:

    Code:
    //includes
    #include "StdAfx.h"
    #include "MyApp.h"
    #include "d3d9.h"
    #include "mmsystem.h"
    #include <math.h>
    
    //definitions
    const DWORD MY_VERTEX_TYPE = (  D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE);
    struct MyVertexType
    {
    	float x;
    	float y;
    	float z;
    	D3DXVECTOR3 normal;
    	DWORD color;
    };
    
    //default constructor
    MyApp::MyApp(void)
    {
    }
    
    //default deconstructor
    MyApp::~MyApp(void)
    {
    }
    
    MyApp::MyApp(HWND hWnd)
    {
    	this->hWnd = hWnd;
    	g_pD3D = NULL;
    	pDevice = NULL;
    	pMesh = NULL;
    	verBuff = NULL;
    	pointVerBuff = NULL;
    	inBuff = NULL;
    }
    
    //initialisation, create device
    HRESULT MyApp::Init()
    {
    	if( NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION))){
    		return E_FAIL;
    	}
    
    	D3DPRESENT_PARAMETERS d3dpp;
    	ZeroMemory( &d3dpp, sizeof(d3dpp) );
    	d3dpp.Windowed   = TRUE;
    	d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
    	d3dpp.EnableAutoDepthStencil = TRUE;
    	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
    
    	if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
    		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
    		&d3dpp, &pDevice ) ) ){
    		return E_FAIL;
    	}
    	D3DXVECTOR3 norm(0.5f,0.5f,0.5f); //define normal as middle of screen
    
    	//CLIENT STUFF HERE
    
    	//rectangle vertex buffer
    	MyVertexType vertArray[] =
    	{
    		{0,0,0,norm ,D3DCOLOR_XRGB(0,255,255)},
    		{0,240,0,norm ,D3DCOLOR_XRGB(0,255,255)},
    		{320,0,0,norm ,D3DCOLOR_XRGB(0,255,255)},
    		{320,240,0,norm ,D3DCOLOR_XRGB(0,255,255)},
    	};
    
    	//primitive points vertex buffer
    	MyVertexType pointListArray[] =
    	{
    		{10,10,0,norm ,D3DCOLOR_XRGB(255,255,255)},
    		{300,200,0,norm ,D3DCOLOR_XRGB(255,255,255)},
    		{280,195,0,norm ,D3DCOLOR_XRGB(255,255,255)},
    		{220,110,0,norm ,D3DCOLOR_XRGB(255,255,255)},
    		{115,25,0,norm ,D3DCOLOR_XRGB(255,255,255)},
    		{193,94,0,norm ,D3DCOLOR_XRGB(255,255,255)},
    	};
    
    	//index for rectangle
    	unsigned int indexArray[] =
    	{ 0,1,1,3,3,2,2,0 };
    
    	//create rectangle vertex buffer
    	if(FAILED(pDevice->CreateVertexBuffer(3*sizeof(MyVertexType),0,MY_VERTEX_TYPE,D3DPOOL_SYSTEMMEM,&verBuff,NULL)))
    	{
    		return E_FAIL;
    	}
    
    	//create point vertex buffer
    	if(FAILED(pDevice->CreateVertexBuffer(3*sizeof(MyVertexType),0,MY_VERTEX_TYPE,D3DPOOL_SYSTEMMEM,&pointVerBuff,NULL)))
    	{
    		return E_FAIL;
    	}
    
    	if( FAILED(pDevice->CreateIndexBuffer(8*sizeof(MyVertexType),0,D3DFMT_INDEX32,D3DPOOL_SYSTEMMEM,&inBuff,NULL)))
    	{
    		return E_FAIL;
    	}
    
    	void *lockPoint;
    	if( FAILED( verBuff->Lock( 0,sizeof(vertArray),(void**)&lockPoint,0 ) ) )
    	{
    		return E_FAIL;
    	}
    
    	memcpy( lockPoint,vertArray, sizeof(vertArray));
    	verBuff->Unlock();
    	//POINT
    	if( FAILED( pointVerBuff->Lock( 0,sizeof(pointListArray),(void**)&lockPoint,0 ) ) )
    	{
    		return E_FAIL;
    	}
    
    	memcpy( lockPoint,pointListArray, sizeof(pointListArray));
    	pointVerBuff->Unlock();
    	//FINISH
    	if( FAILED( inBuff->Lock( 0,sizeof(indexArray),(void**)&lockPoint,0 ) ) )
    	{
    		return E_FAIL;
    	}
    
    	memcpy( lockPoint,indexArray, sizeof(indexArray));
    	inBuff->Unlock();
    	return D3D_OK;
    }
    
    void MyApp::Render()
    {
    	D3DCOLOR background = 0x000000;
    
    	if (pDevice)
    	{
    		pDevice->Clear(0,0,(D3DCLEAR_TARGET| D3DCLEAR_ZBUFFER),background,1.0f,0);  //set background colour
    		pDevice->BeginScene();
    		pDevice->SetFVF(MY_VERTEX_TYPE);
    
    		pDevice->SetStreamSource(0,verBuff,0,sizeof(MyVertexType));
    		pDevice->SetIndices( inBuff );
    		pDevice->DrawIndexedPrimitive(D3DPT_LINELIST,0,0,4,0,4);
    
    		/*pDevice->SetStreamSource(0,pointVerBuff,0,sizeof(MyVertexType));
    		pDevice->SetFVF(MY_VERTEX_TYPE);
    		pDevice->DrawPrimitive(D3DPT_POINTLIST,0,6);*/
    		
    		//moves camera around
    		Transforms();
    		//what factors to render with
    		RenderStates();
    		pDevice->EndScene();
    		pDevice->Present(0,0,0,0);
    	}
    }
    
    void MyApp::RenderStates()
    {
    	pDevice->SetRenderState(D3DRS_CULLMODE,false );
    	pDevice->SetRenderState(D3DRS_LIGHTING,false );
    	pDevice->SetRenderState(D3DRS_AMBIENT, D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f));
    	pDevice->SetRenderState(D3DRS_SHADEMODE,3 );
    	pDevice->SetRenderState(D3DRS_SPECULARENABLE,false);
    	pDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
    }
    
    void MyApp::Transforms()
    {
    	D3DXMATRIX pMatrix;
    	D3DXMATRIX RotMatrix;
    	D3DXVECTOR3 lookat(0,120,0); //where the camera starts
    	D3DXVECTOR3 up (0,1,0);
    	D3DXVECTOR3 eye(0, 0, -500); //how far away the camera is
    
    	//D3DXMatrixRotationX( &RotMatrix,D3DXToRadian(90) );
    	D3DXMatrixRotationY( &pMatrix,timeGetTime()/1000.0F);
    	//pMatrix = RotMatrix * pMatrix; 
    	pDevice->SetTransform( D3DTS_WORLD, &pMatrix );
    
    	D3DXMatrixLookAtLH( &pMatrix,&eye,&lookat,&up);
    	pDevice->SetTransform( D3DTS_VIEW , &pMatrix );
    
    	D3DXMatrixPerspectiveFovLH( &pMatrix,D3DXToRadian(90),640/480,1.0f,1000.0f);
    	pDevice->SetTransform( D3DTS_PROJECTION , &pMatrix );
    }
    
    void MyApp::Shutdown()
    {
    	g_pD3D->Release();
    	pDevice->Release();
    	verBuff->Release();
    	pointVerBuff->Release();
    	//inBuff->Release();
    }
    The rectangle itself is turquoise, and the points are white, and they show up this way seperately just not together. The transform method just spins everything around a central axis.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    9
    anyone have any idea?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21