Re: Problems in Colorizing Surfaces of 4 Triangles & 1 Quad of A Pyram

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Phil Taylor (philipt_at_private-citizen-sorta.com)
Date: 07/04/04


Date: Sat, 3 Jul 2004 17:23:22 -0700

not only is your geometry borked, but you havent set any of the state to use
vertex coloring, neither renderstates nor texture stage states.

here is your data in a source file that can be used in place of the simplest
auto-generated DX9 wizard project:
//--------------------------------------------------------------------------

---
// File: DirectX9Pyramid.cpp
//
// Desc: DirectX window application created by the DirectX AppWizard
//--------------------------------------------------------------------------
---
#define STRICT
#include <windows.h>
#include <commctrl.h>
#include <commdlg.h>
#include <basetsd.h>
#include <math.h>
#include <stdio.h>
#include <d3dx9.h>
#include <dxerr9.h>
#include <tchar.h>
#include "DXUtil.h"
#include "D3DEnumeration.h"
#include "D3DSettings.h"
#include "D3DApp.h"
#include "D3DFont.h"
#include "D3DFile.h"
#include "D3DUtil.h"
#include "resource.h"
#include "DirectX9Pyramid.h"
//--------------------------------------------------------------------------
---
// Global access to the app (needed for the global WndProc())
//--------------------------------------------------------------------------
---
CMyD3DApplication* g_pApp = NULL;
HINSTANCE g_hInst = NULL;
//--------------------------------------------------------------------------
---
// Name: WinMain()
// Desc: Entry point to the program. Initializes everything, and goes into a
// message-processing loop. Idle time is used to render the scene.
//--------------------------------------------------------------------------
---
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
CMyD3DApplication d3dApp;
g_pApp = &d3dApp;
g_hInst = hInst;
InitCommonControls();
if( FAILED( d3dApp.Create( hInst ) ) )
return 0;
return d3dApp.Run();
}
//from usenet
LPDIRECT3DVERTEXBUFFER9 vertexBuffer;
// camera variables
D3DXMATRIX matView; // the view matrix
D3DXMATRIX matProj; // the projection matrix
D3DXVECTOR3 cameraPosition; // the position of the camera
D3DXVECTOR3 cameraLook; // where the camera is pointing
// A structure for our custom vertex type
struct CUSTOMVERTEX
{
FLOAT x, y, z; // The untransformed, 3D position for the vertex
DWORD color;
};
// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
bool createPyramid(LPDIRECT3DDEVICE9 pd3dDevice)
{
// Initialize five vertices for rendering a pyramid
CUSTOMVERTEX g_Vertices[] =
{
// Triangle #1
{ 0.0f, 0.0f, -64.0f, D3DCOLOR_ARGB(0,255,0,0)},//red
{ 64.0f, 64.0f, 0.0f, D3DCOLOR_ARGB(0,255,0,0)},
{ 64.0f, -64.0f, 0.0f, D3DCOLOR_ARGB(0,255,0,0)},
// Triangle #2
{ 0.0f, 0.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,0)},//black
{ 64.0f, -64.0f, 0.0f, D3DCOLOR_ARGB(0,0,0,0)},
{-64.0f, -64.0f, 0.0f, D3DCOLOR_ARGB(0,0,0,0)},
// Triangle #3
{ 0.0f, 0.0f, -64.0f, D3DCOLOR_ARGB(0,255,0,0)},//red
{-64.0f, -64.0f, 0.0f, D3DCOLOR_ARGB(0,255,0,0)},
{-64.0f, 64.0f, 0.0f, D3DCOLOR_ARGB(0,255,0,0)},
// Triangle #4
{ 0.0f, 0.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,0)},//black
{-64.0f, 64.0f, 0.0f, D3DCOLOR_ARGB(0,0,0,0)},
{ 64.0f, 64.0f, 0.0f, D3DCOLOR_ARGB(0,0,0,0)},
// Quad #1
{ 64.0f, 64.0f, 0.0f, D3DCOLOR_ARGB(0,0,255,0)},//green
{ 64.0f, -64.0f, 0.0f, D3DCOLOR_ARGB(0,0,255,0)},
{ -64.0f, -64.0f, 0.0f, D3DCOLOR_ARGB(0,0,255,0)},
{ 64.0f, 64.0f, 0.0f, D3DCOLOR_ARGB(0,0,255,0)},
{ -64.0f, -64.0f, 0.0f, D3DCOLOR_ARGB(0,0,255,0)},
{ -64.0f, 64.0f, 0.0f, D3DCOLOR_ARGB(0,0,255,0)},
};
// Create the vertex buffer.
HRESULT hr;
hr = pd3dDevice->CreateVertexBuffer(sizeof(g_Vertices) *
sizeof(CUSTOMVERTEX),
0,
D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT,
&vertexBuffer,
NULL );
if FAILED (hr)
return false;
// prepare to copy the vertices into the vertex buffer
VOID* pVertices;
// lock the vertex buffer
hr = vertexBuffer->Lock(0, sizeof(g_Vertices), (void**)&pVertices, 0);
// check to make sure the vertex buffer can be locked
if FAILED (hr)
return false;
// copy the vertices into the buffer
memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
// unlock the vertex buffer
vertexBuffer->Unlock();
return true;
}
void cleanupPyramid(void)
{
if( vertexBuffer != NULL)
{
vertexBuffer->Release();
vertexBuffer = NULL;
}
}
void restorePyramid(LPDIRECT3DDEVICE9 pd3dDevice)
{
pd3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(255, 255, 255));
pd3dDevice->SetRenderState(D3DRS_COLORVERTEX, TRUE);
// pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
pd3dDevice->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_FLAT); // Flat shading
for one color for each triangle
// pd3dDevice->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_GOURAUD);
pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG2 );
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
createPyramid(pd3dDevice);
}
/*************************************************************************
* createCamera
* creates a virtual camera
*************************************************************************/
void createCamera(LPDIRECT3DDEVICE9 pd3dDevice,float nearClip, float
farClip)
{
//Here we specify the field of view, aspect ration and near and far clipping
planes.
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 640/480, nearClip, farClip);
pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}
/*************************************************************************
* moveCamera
* moves the camera to a position specified by the vector passed as a
* parameter
*************************************************************************/
void moveCamera(D3DXVECTOR3 vec)
{
cameraPosition = vec;
}
/*************************************************************************
* pointCamera
* points the camera a location specified by the passed vector
*************************************************************************/
void pointCamera(LPDIRECT3DDEVICE9 pd3dDevice,D3DXVECTOR3 vec)
{
cameraLook = vec;
D3DXMatrixLookAtLH(&matView, &cameraPosition, //Camera Position
&cameraLook, //Look At Position
&D3DXVECTOR3(0.0f, 1.0f, 0.0f)); //Up Direction
pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
}
void setCameraTransform(LPDIRECT3DDEVICE9 pd3dDevice)
{
createCamera(pd3dDevice,1.0f, 500.0f); // near clip plane, far clip plane
moveCamera(D3DXVECTOR3(0.0f, 0.0f, -450.0f));
pointCamera(pd3dDevice,D3DXVECTOR3(0.0f, 0.0f, 0.0f));
}
void setPyramidTransform(LPDIRECT3DDEVICE9 pd3dDevice)
{
D3DXMATRIX meshMat, meshScale, meshRotate;
// set the rotation
D3DXMatrixRotationY(&meshRotate, D3DXToRadian(0));
// set the scaling
D3DXMatrixScaling(&meshScale, 1.0f, 1.0f, 1.0f);
// multiple the scaling and rotation matrices to create the meshMat matrix
D3DXMatrixMultiply(&meshMat, &meshScale, &meshRotate);
// transform the object in world space
pd3dDevice->SetTransform(D3DTS_WORLD, &meshMat);
}
void renderPyramid(LPDIRECT3DDEVICE9 pd3dDevice)
{
pd3dDevice->SetStreamSource( 0, vertexBuffer, 0, sizeof(CUSTOMVERTEX) );
pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 3, 1 );
pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 6, 1 );
pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 9, 1 );
pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 12, 2 );
}
//--------------------------------------------------------------------------
---
// Name: CMyD3DApplication()
// Desc: Application constructor. Paired with ~CMyD3DApplication()
// Member variables should be initialized to a known state here.
// The application window has not yet been created and no Direct3D device
// has been created, so any initialization that depends on a window or
// Direct3D should be deferred to a later stage.
//--------------------------------------------------------------------------
---
CMyD3DApplication::CMyD3DApplication()
{
m_dwCreationWidth = 500;
m_dwCreationHeight = 375;
m_strWindowTitle = TEXT( "DirectX9Pyramid" );
m_d3dEnumeration.AppUsesDepthBuffer = TRUE;
m_bStartFullscreen = false;
m_bShowCursorWhenFullscreen = false;
// Create a D3D font using d3dfont.cpp
m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
m_bLoadingApp = TRUE;
m_pD3DXMesh = NULL;
ZeroMemory( &m_UserInput, sizeof(m_UserInput) );
m_fWorldRotX = 0.0f;
m_fWorldRotY = 0.0f;
}
//--------------------------------------------------------------------------
---
// Name: ~CMyD3DApplication()
// Desc: Application destructor. Paired with CMyD3DApplication()
//--------------------------------------------------------------------------
---
CMyD3DApplication::~CMyD3DApplication()
{
}
//--------------------------------------------------------------------------
---
// Name: OneTimeSceneInit()
// Desc: Paired with FinalCleanup().
// The window has been created and the IDirect3D9 interface has been
// created, but the device has not been created yet. Here you can
// perform application-related initialization and cleanup that does
// not depend on a device.
//--------------------------------------------------------------------------
---
HRESULT CMyD3DApplication::OneTimeSceneInit()
{
// TODO: perform one time initialization
// Drawing loading status message until app finishes loading
SendMessage( m_hWnd, WM_PAINT, 0, 0 );
m_bLoadingApp = FALSE;
return S_OK;
}
//--------------------------------------------------------------------------
---
// Name: ConfirmDevice()
// Desc: Called during device initialization, this code checks the display
device
// for some minimum set of capabilities
//--------------------------------------------------------------------------
---
HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps, DWORD dwBehavior,
D3DFORMAT Format )
{
UNREFERENCED_PARAMETER( Format );
UNREFERENCED_PARAMETER( dwBehavior );
UNREFERENCED_PARAMETER( pCaps );
BOOL bCapsAcceptable;
// TODO: Perform checks to see if these display caps are acceptable.
bCapsAcceptable = TRUE;
if( bCapsAcceptable )
return S_OK;
else
return E_FAIL;
}
//--------------------------------------------------------------------------
---
// Name: InitDeviceObjects()
// Desc: Paired with DeleteDeviceObjects()
// The device has been created. Resources that are not lost on
// Reset() can be created here -- resources in D3DPOOL_MANAGED,
// D3DPOOL_SCRATCH, or D3DPOOL_SYSTEMMEM. Image surfaces created via
// CreateOffScreenPlainSurface are never lost and can be created here.
Vertex
// shaders and pixel shaders can also be created here as they are not
// lost on Reset().
//--------------------------------------------------------------------------
---
HRESULT CMyD3DApplication::InitDeviceObjects()
{
// TODO: create device objects
HRESULT hr;
// Init the font
hr = m_pFont->InitDeviceObjects( m_pd3dDevice );
if( FAILED( hr ) )
return DXTRACE_ERR( "m_pFont->InitDeviceObjects", hr );
// Create a teapot mesh using D3DX
// if( FAILED( hr = D3DXCreateTeapot( m_pd3dDevice, &m_pD3DXMesh, NULL ) ) )
// return DXTRACE_ERR( "D3DXCreateTeapot", hr );
return S_OK;
}
//--------------------------------------------------------------------------
---
// Name: RestoreDeviceObjects()
// Desc: Paired with InvalidateDeviceObjects()
// The device exists, but may have just been Reset(). Resources in
// D3DPOOL_DEFAULT and any other device state that persists during
// rendering should be set here. Render states, matrices, textures,
// etc., that don't change during rendering can be set once here to
// avoid redundant state setting during Render() or FrameMove().
//--------------------------------------------------------------------------
---
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
// TODO: setup render states
// Setup a material
D3DMATERIAL9 mtrl;
D3DUtil_InitMaterial( mtrl, 1.0f, 0.0f, 0.0f );
m_pd3dDevice->SetMaterial( &mtrl );
/*
// Set up the textures
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
*/
// Set miscellaneous render states
m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
// m_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x000F0F0F );
//from usenet
restorePyramid(m_pd3dDevice);
// Set the world matrix
D3DXMATRIX matIdentity;
D3DXMatrixIdentity( &matIdentity );
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matIdentity );
// Set up our view matrix. A view matrix can be defined given an eye point,
// a point to lookat, and a direction for which way is up. Here, we set the
// eye five units back along the z-axis and up three units, look at the
// origin, and define "up" to be in the y-direction.
D3DXMATRIX matView;
D3DXVECTOR3 vFromPt = D3DXVECTOR3( 0.0f, 0.0f, -450.0f );
D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXMatrixLookAtLH( &matView, &vFromPt, &vLookatPt, &vUpVec );
m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
// Set the projection matrix
D3DXMATRIX matProj;
FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 500.0f );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
/*
// Set up lighting states
D3DLIGHT9 light;
D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, -1.0f, -1.0f, 2.0f );
m_pd3dDevice->SetLight( 0, &light );
m_pd3dDevice->LightEnable( 0, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
*/
// Restore the font
m_pFont->RestoreDeviceObjects();
return S_OK;
}
//--------------------------------------------------------------------------
---
// Name: FrameMove()
// Desc: Called once per frame, the call is the entry point for animating
// the scene.
//--------------------------------------------------------------------------
---
HRESULT CMyD3DApplication::FrameMove()
{
// TODO: update world
// Update user input state
UpdateInput( &m_UserInput );
// Update the world state according to user input
D3DXMATRIX matWorld;
D3DXMATRIX matRotY;
D3DXMATRIX matRotX;
if( m_UserInput.bRotateLeft && !m_UserInput.bRotateRight )
m_fWorldRotY += m_fElapsedTime;
else if( m_UserInput.bRotateRight && !m_UserInput.bRotateLeft )
m_fWorldRotY -= m_fElapsedTime;
static D3DXVECTOR3 vec(0.0f, 0.0f, -450.0f);
if( m_UserInput.bRotateUp && !m_UserInput.bRotateDown )
{
m_fWorldRotX += m_fElapsedTime;
vec.z++;
moveCamera(vec);
pointCamera(m_pd3dDevice,vec);
}
else if( m_UserInput.bRotateDown && !m_UserInput.bRotateUp )
{
m_fWorldRotX -= m_fElapsedTime;
vec.z--;
moveCamera(vec);
pointCamera(m_pd3dDevice,vec);
}
D3DXMatrixRotationX( &matRotX, m_fWorldRotX );
D3DXMatrixRotationY( &matRotY, m_fWorldRotY );
D3DXMatrixMultiply( &matWorld, &matRotX, &matRotY );
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
return S_OK;
}
//--------------------------------------------------------------------------
---
// Name: UpdateInput()
// Desc: Update the user input. Called once per frame
//--------------------------------------------------------------------------
---
void CMyD3DApplication::UpdateInput( UserInput* pUserInput )
{
pUserInput->bRotateUp = ( m_bActive && (GetAsyncKeyState( VK_UP ) & 0x8000)
== 0x8000 );
pUserInput->bRotateDown = ( m_bActive && (GetAsyncKeyState( VK_DOWN ) &
0x8000) == 0x8000 );
pUserInput->bRotateLeft = ( m_bActive && (GetAsyncKeyState( VK_LEFT ) &
0x8000) == 0x8000 );
pUserInput->bRotateRight = ( m_bActive && (GetAsyncKeyState( VK_RIGHT ) &
0x8000) == 0x8000 );
}
//--------------------------------------------------------------------------
---
// Name: Render()
// Desc: Called once per frame, the call is the entry point for 3d
// rendering. This function sets up render states, clears the
// viewport, and renders the scene.
//--------------------------------------------------------------------------
---
HRESULT CMyD3DApplication::Render()
{
// Clear the viewport
m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
0x000000ff, 1.0f, 0L );
// Begin the scene
if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
{
// TODO: render world
// from usenet
setCameraTransform(m_pd3dDevice);
setPyramidTransform(m_pd3dDevice);
m_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
renderPyramid(m_pd3dDevice);
m_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
renderPyramid(m_pd3dDevice);
// Render the teapot mesh
// m_pD3DXMesh->DrawSubset(0);
// Render stats and help text
RenderText();
// End the scene.
m_pd3dDevice->EndScene();
}
return S_OK;
}
//--------------------------------------------------------------------------
---
// Name: RenderText()
// Desc: Renders stats and help text to the scene.
//--------------------------------------------------------------------------
---
HRESULT CMyD3DApplication::RenderText()
{
D3DCOLOR fontColor = D3DCOLOR_ARGB(255,255,255,0);
TCHAR szMsg[MAX_PATH] = TEXT("");
// Output display stats
FLOAT fNextLine = 40.0f;
lstrcpy( szMsg, m_strDeviceStats );
fNextLine -= 20.0f;
m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, m_strFrameStats );
fNextLine -= 20.0f;
m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
// Output statistics & help
fNextLine = (FLOAT) m_d3dsdBackBuffer.Height;
wsprintf( szMsg, TEXT("Arrow keys: Up=%d Down=%d Left=%d Right=%d"),
m_UserInput.bRotateUp, m_UserInput.bRotateDown, m_UserInput.bRotateLeft,
m_UserInput.bRotateRight );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("Use arrow keys to rotate object") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("Press 'F2' to configure display") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
return S_OK;
}
//--------------------------------------------------------------------------
---
// Name: MsgProc()
// Desc: Overrrides the main WndProc, so the sample can do custom message
// handling (e.g. processing mouse, keyboard, or menu commands).
//--------------------------------------------------------------------------
---
LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg, WPARAM wParam,
LPARAM lParam )
{
switch( msg )
{
case WM_PAINT:
{
if( m_bLoadingApp )
{
// Draw on the window tell the user that the app is loading
// TODO: change as needed
HDC hDC = GetDC( hWnd );
TCHAR strMsg[MAX_PATH];
wsprintf( strMsg, TEXT("Loading... Please wait") );
RECT rct;
GetClientRect( hWnd, &rct );
DrawText( hDC, strMsg, -1, &rct, DT_CENTER|DT_VCENTER|DT_SINGLELINE );
ReleaseDC( hWnd, hDC );
}
break;
}
}
return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
}
//--------------------------------------------------------------------------
---
// Name: InvalidateDeviceObjects()
// Desc: Invalidates device objects. Paired with RestoreDeviceObjects()
//--------------------------------------------------------------------------
---
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
// TODO: Cleanup any objects created in RestoreDeviceObjects()
m_pFont->InvalidateDeviceObjects();
return S_OK;
}
//--------------------------------------------------------------------------
---
// Name: DeleteDeviceObjects()
// Desc: Paired with InitDeviceObjects()
// Called when the app is exiting, or the device is being changed,
// this function deletes any device dependent objects.
//--------------------------------------------------------------------------
---
HRESULT CMyD3DApplication::DeleteDeviceObjects()
{
// TODO: Cleanup any objects created in InitDeviceObjects()
m_pFont->DeleteDeviceObjects();
// SAFE_RELEASE( m_pD3DXMesh );
//from usenet
cleanupPyramid();
return S_OK;
}
//--------------------------------------------------------------------------
---
// Name: FinalCleanup()
// Desc: Paired with OneTimeSceneInit()
// Called before the app exits, this function gives the app the chance
// to cleanup after itself.
//--------------------------------------------------------------------------
---
HRESULT CMyD3DApplication::FinalCleanup()
{
// TODO: Perform any final cleanup needed
// Cleanup D3D font
SAFE_DELETE( m_pFont );
return S_OK;
}
"Kryst(ian)" <kryst_NOSPAM@o2.pl> wrote in message
news:ucsS1aFYEHA.996@TK2MSFTNGP12.phx.gbl...
> problem 2 is obvious:
> you have triangle list and you try to render it as a strip. You DO have 2
> triangles on screen, but second one is on wrong vertices.
> you shold use trianglelist for geomrtry you have declared.
> Perhaps problem 1 is caused by the same reason.
>
> Kryst(ian)
>
> "SHC" <SHC@discussions.microsoft.com> wrote in message
> news:83A9AE64-A2B4-49DC-A0AA-5F21AB9D3D4F@microsoft.com...
> > Hi all,
> > I got a pyramid (4 triangles of faces and 1 quad of base) and tried to
use
> flat shading "Device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT);" to
> colorize the surfaces of 4 triangles and 1 quad of the pyramid-see the
> attached
> > source code. The project works without any errors. But I have 2 weird
> problems. Problem#1: I can not get the first and second triangles in black
> and red respectively as set in the code.  Problem#2: I just a red triangle
> in the quad,
> > not the whole quad in color by using
> >             pd3dDevice->DrawPrimitive( D3DPT_LINESTRIP, 12, 4 );
> >         or
> >             pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  12, 2 );
> > Please help and tell me what wrong in my coding and how to correct the
> problem.
> > Thanks in advance,
> > SHC
> > ///////////--- 4t1q-Pyramid.cpp---//////////////////
> > #include <windows.h>
> > #include <d3d9.h>
> > #include <d3dx9tex.h>
> > // #include <dxguid.h>  //added 12 June 2004
> > #include <dxerr9.h>  //added 12 June 2004
> >
> > HINSTANCE hInst; // holds the instance for this app
> > HWND wndHandle; // global window handle
> >
> > LPDIRECT3D9             pD3D;
> > LPDIRECT3DDEVICE9       pd3dDevice;
> > LPDIRECT3DVERTEXBUFFER9 vertexBuffer;
> >
> > // camera variables
> > D3DXMATRIX matView; // the view matrix
> > D3DXMATRIX matProj; // the projection matrix
> > D3DXVECTOR3 cameraPosition; // the position of the camera
> > D3DXVECTOR3 cameraLook; // where the camera is pointing
> >
> > // A structure for our custom vertex type
> > struct CUSTOMVERTEX
> > {
> > FLOAT x, y, z;  // The untransformed, 3D position for the vertex
> > DWORD color;
> > };
> >
> > // Our custom FVF, which describes our custom vertex structure
> > #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
> >
> > ////////////////////////////////////////////// forward declarations
> > bool    initWindow(HINSTANCE hInstance);
> > LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
> >
> > bool initDirect3D(HWND hwnd);
> > void shutdownDirect3D(void);
> > bool createPyramid(void);
> >
> > void createCamera(float nearClip, float farClip);
> > void moveCamera(D3DXVECTOR3 vec);
> > void pointCamera(D3DXVECTOR3 vec);
> >
> > #define SCREEN_WIDTH  640
> > #define SCREEN_HEIGHT 480
> >
> > int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR
> lpCmdLine, int nCmdShow)
> > {
> > // call our function to init and create our window
> > if (!initWindow(hInstance))
> > {
> > MessageBox(NULL, "Unable to create window", "ERROR", MB_OK);
> > return false;
> > }
> >
> > if (!initDirect3D(wndHandle))
> > {
> > MessageBox(NULL, "Unable to init Direct3D", "ERROR", MB_OK);
> > return false;
> > }
> >
> > if (!createPyramid())
> > {
> > MessageBox(NULL, "Pyramid could not be created", "ERROR", MB_OK);
> > return false;
> > }
> >
> > createCamera(1.0f, 500.0f); // near clip plane, far clip plane
> > moveCamera(D3DXVECTOR3(0.0f, 0.0f, -450.0f));
> > pointCamera(D3DXVECTOR3(0.0f, 0.0f, 0.0f));
> >
> > D3DXMATRIX meshMat, meshScale, meshRotate;
> > // set the rotation
> > D3DXMatrixRotationY(&meshRotate, D3DXToRadian(0));
> > // set the scaling
> > D3DXMatrixScaling(&meshScale, 1.0f, 1.0f, 1.0f);
> > // multiple the scaling and rotation matrices to create the meshMat
matrix
> > D3DXMatrixMultiply(&meshMat, &meshScale, &meshRotate);
> >
> > // transform the object in world space
> > pd3dDevice->SetTransform(D3DTS_WORLD, &meshMat);
> >
> > // Main message loop:
> > // Enter the message loop
> >     MSG msg;
> >     ZeroMemory( &msg, sizeof(msg) );
> >     while( msg.message!=WM_QUIT )
> >     {
> > // check for messages
> > if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
> >         {
> > TranslateMessage( &msg );
> >             DispatchMessage( &msg );
> >         }
> > // this is called when no messages are pending
> > else
> > {
> > // Clear the backbuffer to a black color
> > pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,255),
> 1.0f, 0 );
> >
> > pd3dDevice->BeginScene();
> >
> > pd3dDevice->SetStreamSource( 0, vertexBuffer, 0, sizeof(CUSTOMVERTEX) );
> >     pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
> >
> > pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  0, 1 );
> > pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  3, 1 );
> > pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  6, 1 );
> > pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  9, 1 );
> >  // pd3dDevice->DrawPrimitive( D3DPT_LINESTRIP, 12, 4 );
> >             pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  12, 2 );
> >  //         pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  15, 1 );
> >
> > pd3dDevice->EndScene();
> >
> > // Present the backbuffer contents to the display
> > pd3dDevice->Present( NULL, NULL, NULL, NULL );
> > }
> >     }
> >
> > // release and shutdown Direct3D
> > shutdownDirect3D();
> >
> > return (int) msg.wParam;
> > }
> >
> > bool initWindow(HINSTANCE hInstance)
> > {
> > WNDCLASSEX wcex;
> >
> > wcex.cbSize = sizeof(WNDCLASSEX);
> > wcex.style = CS_HREDRAW | CS_VREDRAW;
> > wcex.lpfnWndProc = (WNDPROC)WndProc;
> > wcex.cbClsExtra = 0;
> > wcex.cbWndExtra = 0;
> > wcex.hInstance = hInstance;
> > wcex.hIcon = 0;
> > wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
> > wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
> > wcex.lpszMenuName = NULL;
> > wcex.lpszClassName = "DirectXExample";
> > wcex.hIconSm = 0;
> > RegisterClassEx(&wcex);
> >
> > wndHandle = CreateWindow("DirectXExample",
> > "DirectXExample",
> > WS_OVERLAPPEDWINDOW,
> > CW_USEDEFAULT,
> > CW_USEDEFAULT,
> > SCREEN_WIDTH,
> > SCREEN_HEIGHT,
> > NULL,
> > NULL,
> > hInstance,
> > NULL);
> >    if (!wndHandle)
> >       return false;
> >
> >    ShowWindow(wndHandle, SW_SHOW);
> >    UpdateWindow(wndHandle);
> >
> >    return true;
> > }
> >
> > LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM
> lParam)
> > {
> > switch (message)
> > {
> > case WM_DESTROY:
> > PostQuitMessage(0);
> > break;
> > }
> > return DefWindowProc(hWnd, message, wParam, lParam);
> > }
> >
> > bool createPyramid(void)
> > {
> > // Initialize five vertices for rendering a pyramid
> > CUSTOMVERTEX g_Vertices[] =
> > {
> >        // Triangle #1
> > {  0.0f,   0.0f,  -64.0f, D3DCOLOR_ARGB(255,255,0,0)},
> > { 64.0f,  64.0f,    0.0f, D3DCOLOR_ARGB(255,255,0,0)},
> > { 64.0f, -64.0f,    0.0f, D3DCOLOR_ARGB(255,255,0,0)},
> >
> > // Triangle #2
> > {  0.0f,  0.0f,   -64.0f, D3DCOLOR_ARGB(0,255,0,0)},
> > { 64.0f, -64.0f,    0.0f, D3DCOLOR_ARGB(0,255,0,0)},
> > {-64.0f, -64.0f,    0.0f, D3DCOLOR_ARGB(0,255,0,0)},
> >
> > // Triangle #3
> > {  0.0f,   0.0f,  -64.0f, D3DCOLOR_ARGB(0,0,255,0)},
> > {-64.0f, -64.0f,    0.0f, D3DCOLOR_ARGB(0,0,255,0)},
> > {-64.0f,  64.0f,    0.0f, D3DCOLOR_ARGB(0,0,255,0)},
> >
> > // Triangle #4
> > {  0.0f,   0.0f,  -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
> > {-64.0f,  64.0f,    0.0f, D3DCOLOR_ARGB(0,0,0,255)},
> > { 64.0f,  64.0f,    0.0f, D3DCOLOR_ARGB(0,0,0,255)},
> >
> > // Quad #1
> > {  64.0f,  64.0f,   0.0f, D3DCOLOR_ARGB(0,0,0,0)},
> > {  64.0f, -64.0f,   0.0f, D3DCOLOR_ARGB(0,255,0,0)},
> > { -64.0f, -64.0f,   0.0f, D3DCOLOR_ARGB(0,0,255,0)},
> > { -64.0f,  64.0f,   0.0f, D3DCOLOR_ARGB(0,0,0,255)},
> >     {  64.0f,  64.0f,   0.0f, D3DCOLOR_ARGB(0,0,255,0)}, //Repeat
> >         { -64.0f, -64.0f,   0.0f, D3DCOLOR_ARGB(0,0,0,0)}, //Repeat
> > };
> >
> > // Create the vertex buffer.
> >     HRESULT hr;
> >
> > hr = pd3dDevice->CreateVertexBuffer(sizeof(g_Vertices) *
> sizeof(CUSTOMVERTEX),
> >       0,
> > D3DFVF_CUSTOMVERTEX,
> > D3DPOOL_DEFAULT,
> > &vertexBuffer,
> > NULL );
> > if FAILED (hr)
> > return false;
> >
> > // prepare to copy the vertices into the vertex buffer
> > VOID* pVertices;
> > // lock the vertex buffer
> > hr = vertexBuffer->Lock(0, sizeof(g_Vertices), (void**)&pVertices, 0);
> >
> > // check to make sure the vertex buffer can be locked
> > if FAILED (hr)
> > return false;
> >
> > // copy the vertices into the buffer
> >     memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
> >
> > // unlock the vertex buffer
> > vertexBuffer->Unlock();
> >
> > return true;
> > }
> > bool initDirect3D(HWND hwnd)
> > {
> > if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
> > return false;
> >
> > D3DPRESENT_PARAMETERS d3dpp;
> >     ZeroMemory( &d3dpp, sizeof(d3dpp) );
> >     d3dpp.Windowed = TRUE;
> >     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
> >     d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
> > d3dpp.BackBufferCount  = 1;
> > d3dpp.BackBufferHeight = SCREEN_HEIGHT;
> > d3dpp.BackBufferWidth  = SCREEN_WIDTH;
> > d3dpp.hDeviceWindow    = hwnd;
> >
> >     if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
> hwnd,
> >
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
> >                                       &d3dpp, &pd3dDevice ) ) )
> >     return false;
> >
> > // pd3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(255, 255,
> 255));
> > // pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
> >     pd3dDevice->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_FLAT); // Flat
> shading for one color for each triangle
> > pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
> >
> > return true;
> > }
> >
> > void shutdownDirect3D(void)
> > {
> >     if( pd3dDevice != NULL)
> > {
> >         pd3dDevice->Release();
> > pd3dDevice = NULL;
> > }
> >     if( pD3D != NULL)
> > {
> >         pD3D->Release();
> > pD3D = NULL;
> > }
> > }
> >
> >
/*************************************************************************
> > * createCamera
> > * creates a virtual camera
> >
*************************************************************************/
> > void createCamera(float nearClip, float farClip)
> > {
> > //Here we specify the field of view, aspect ration and near and far
> clipping planes.
> >     D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 640/480, nearClip,
> farClip);
> >     pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
> >
> > }
> >
> >
/*************************************************************************
> > * moveCamera
> > * moves the camera to a position specified by the vector passed as a
> > * parameter
> >
*************************************************************************/
> > void moveCamera(D3DXVECTOR3 vec)
> > {
> > cameraPosition = vec;
> > }
> >
> >
/*************************************************************************
> > * pointCamera
> > * points the camera a location specified by the passed vector
> >
*************************************************************************/
> > void pointCamera(D3DXVECTOR3 vec)
> > {
> > cameraLook = vec;
> >
> > D3DXMatrixLookAtLH(&matView, &cameraPosition, //Camera Position
> >                                  &cameraLook, //Look At Position
> >                                  &D3DXVECTOR3(0.0f, 1.0f, 0.0f)); //Up
> Direction
> >
> > pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
> > }
>
>


Relevant Pages

  • SwapChains and MultiSample dont work together
    ... I modified Tutorial2 sample from Manged DirectX SDK April 2006 ... VertexBuffer vertexBuffer = null; ... void form2_FormClosed ... // While the form is still valid, render and process messages ...
    (microsoft.public.win32.programmer.directx.managed)
  • Re: d3dxcreatebox and translation
    ... // Render the scene using the D3D9 device ... void CALLBACK OnD3D9FrameRender(IDirect3DDevice9* pd3dDevice, double fTime, ... // Clear the render target and the zbuffer ...
    (microsoft.public.win32.programmer.directx.graphics)
  • Possible state problems with my particle system
    ... The render states are changed in the ... class ScreenEntityParticleSystem: public ScreenEntityBase ... void ScreenEntityParticleSystem::OutputInformation ... float ScreenEntityParticleSystem::GetReleaseInterval ...
    (microsoft.public.win32.programmer.directx.graphics)
  • Pyramid: error C2065: g_pd3dDevice : undeclared identifier
    ... bool initWindow; ... void createCamera; ... void pointCamera(D3DXVECTOR3 vec); ... // Name: render() ...
    (microsoft.public.win32.programmer.directx.graphics)
  • Re: Problems Decoding Video Captured By Digital Camera
    ... Problems Decoding Video Captured By Digital Camera>, ... I took screenshots as I was going along doing all of the above. ... under "Codec Installation Package for Windows Media ... Launch it and do a File | Render Media ...
    (rec.video.desktop)