I tried to set Render Target.
The Particles was seen By the Camera 1 but the particles was under
the sphere. I think it's a Zbuffer Prob.. It seem to be the particles are draw after the sphere.
I dont know how to Get the Camera Image Good like the Camera 0
The project attach to file with exe compiled and Screen of the probleme.
I think Mista Wilson Has Maybe a solution.. but I can't send it a message by forum..
[img]
Main.cpp
#include "DarkGDK.h"
#include "d3dfunc.h"
#include <math.h>
void ControlCamera ( int keyinput );
float camfactor=1.0;
float dt=0.3;
extern LPDIRECT3DDEVICE9 g_pd3dDevice;
extern LPDIRECT3DSURFACE9 originalRenderTarget;
extern LPDIRECT3DSURFACE9 destSurface;
// the main entry point for the application is this function
void DarkGDK ( void )
{
dbSetDisplayModeAntialias( 1280, 1024, 32, 0, 0, 0);
dbSetWindowPosition(0,0);
dbSetWindowOn ( ) ;
dbSyncOn ( );
dbSyncRate ( 0 );
dbBackdropOn ( );
dbColorBackdrop ( dbRGB ( 0, 0, 0 ) );
dbMakeCamera(1);
dbSetCameraToImage(1,1,512,512,0);
//Make Object for test Camera View
dbMakeObjectSphere(2,200);
//init DX Particles
initPointSprites();
dbPositionCamera(0,50,50,-100);
dbGetImagePointer(1)->GetSurfaceLevel(0, &destSurface);
while ( LoopGDK ( ) )
{
g_pd3dDevice->BeginScene();
g_pd3dDevice->GetRenderTarget(0, &originalRenderTarget);
g_pd3dDevice->SetRenderTarget(0, destSurface);
g_pd3dDevice->Clear( 0, NULL,D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
renderPointSprites( );
g_pd3dDevice->SetRenderTarget(0, originalRenderTarget);
g_pd3dDevice->EndScene();
//update Camera 1
dbPositionCamera(1, dbCameraPositionX( 0 ), dbCameraPositionY( 0 ), dbCameraPositionZ( 0 ) );
dbRotateCamera(1, dbCameraAngleX( 0 ), dbCameraAngleY( 0 ), dbCameraAngleZ( 0 ) );
if(dbSpaceKey())dbPasteImage(1,0,0);
dbSyncMask(1<<1);dbFastSync();
//dbSetCurrentCamera(0);
//Control camera
ControlCamera ( dbScanCode() );
dbText(10,10,"FPS:");dbText(60,10,dbStr(dbScreenFPS()));
dbText(10,30,"SpaceKey: Image Camera1");
//g_pd3dDevice->GetRenderTarget(0, &originalRenderTarget);
//renderPointSprites( );
dbSyncMask(1<<0);
dbSync();
g_pd3dDevice->BeginScene();
renderPointSprites( );
g_pd3dDevice->EndScene();
}
// and now everything is ready to return back to Windows
return;
}
void ControlCamera ( int keyinput )
{
static float cx=0.0 , cy=0.0 , ncx=0, ncy=0, ncr=0, ncf=0;
float cr=0 ,cf=0 ;
if ( keyinput==32 ) cr=-dt*camfactor;
if ( keyinput==30 ) cr=dt*camfactor;
if ( keyinput==17 ) cf=dt*camfactor;
if ( keyinput==31 ) cf=-dt*camfactor;
ncr=dbCurveValue(cr,ncr,5);
ncf=dbCurveValue(cf,ncf,5);
cx=cx+dbMouseMoveY()*0.2;
cy=cy+dbMouseMoveX()*0.2;
if (cx> 80) cx=80;
if (cx< -80) cx=-80;
ncx=dbCurveAngle(cx,ncx,2);
ncy=dbCurveAngle(cy,ncy,2);
dbMoveCamera(0,ncf);
dbRotateCamera( 0,dbWrapValue(ncy-90),0);
dbMoveCamera (0,ncr);
dbRotateCamera( 0,dbWrapValue(ncy+90),0);
dbRotateCamera( 0,ncx,ncy,0);
}
d3dfunc.cpp
#include "DarkGDK.h"
#include "d3d9.h"
#include "d3dx9.h"
//for 3d functions
LPDIRECT3DVERTEXBUFFER9 g_pVertexBuffer = NULL;
LPDIRECT3DTEXTURE9 g_pTexture = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
D3DVIEWPORT9 g_pViewPort;
LPDIRECT3DSURFACE9 destSurface;
LPDIRECT3DSURFACE9 originalRenderTarget;
struct Vertex
{
D3DXVECTOR3 posit;
D3DCOLOR color;
enum FVF
{
FVF_Flags = D3DFVF_XYZ|D3DFVF_DIFFUSE
};
};
struct Particle
{
D3DXVECTOR3 m_vCurPos;
D3DXVECTOR3 m_vCurVel;
D3DCOLOR m_vColor;
};
const int MAX_PARTICLES = 50000;
Particle g_particles[MAX_PARTICLES];
// Helper function to stuff a FLOAT into a DWORD argument
inline DWORD FtoDW( FLOAT f ) { return *((DWORD*)&f); }
float time;
//galaxy
float pi=4*atan(1.0);
int Rmax=200; // galaxy radius
float speed=0.002; // rotation speed
// stars follow elliptic orbits around the center
float eratio=.85; // ellipse ratio
float etwist=8.0/Rmax; // twisting factor (orbit axes depend on radius)
float angle[MAX_PARTICLES];
float radius[MAX_PARTICLES];
//-----------------------------------------------------------------------------
// Name: getRandomMinMax()
// Desc: Gets a random number between min/max boundaries
//-----------------------------------------------------------------------------
float getRandomMinMax( float fMin, float fMax )
{
float fRandNum = (float)rand () / RAND_MAX;
return fMin + (fMax - fMin) * fRandNum;
}
//-----------------------------------------------------------------------------
// Name: getRandomVector()
// Desc: Generates a random vector where X,Y, and Z components are between
// -1.0 and 1.0
//-----------------------------------------------------------------------------
D3DXVECTOR3 getRandomVector( void )
{
D3DXVECTOR3 vVector;
// Pick a random Z between -1.0f and 1.0f.
vVector.z = getRandomMinMax( -1.0f, 1.0f );
// Get radius of this circle
float radius = (float)sqrt(1 - vVector.z * vVector.z);
// Pick a random point on a circle.
float t = getRandomMinMax( -D3DX_PI, D3DX_PI );
// Compute matching X and Y for our Z.
vVector.x = (float)cosf(t) * radius;
vVector.y = (float)sinf(t) * radius;
return vVector;
}
//-----------------------------------------------------------------------------
// Name: initPointSprites()
// Desc:
//-----------------------------------------------------------------------------
void initPointSprites( void )
{
g_pd3dDevice = dbGetDirect3DDevice();
g_pd3dDevice->GetViewport( &g_pViewPort );
// Load up the point sprite's texture...
D3DXCreateTextureFromFile( g_pd3dDevice, "particle.bmp", &g_pTexture );
g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
// Create a vertex bufer which can be used with point sprites...
g_pd3dDevice->CreateVertexBuffer( MAX_PARTICLES * sizeof(Vertex),
D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY | D3DUSAGE_POINTS,
Vertex::FVF_Flags, D3DPOOL_DEFAULT,
&g_pVertexBuffer, NULL );
// If you want to know the max size that a point sprite can be set to,
// and whether or not you can change the size of point sprites in hardware
// by sending D3DFVF_PSIZE with the FVF, do this.
float fMaxPointSize = 0.0f;
bool bDeviceSupportsPSIZE = false;
D3DCAPS9 d3dCaps;
g_pd3dDevice->GetDeviceCaps( &d3dCaps );
fMaxPointSize = d3dCaps.MaxPointSize;
if( d3dCaps.FVFCaps & D3DFVFCAPS_PSIZE )
bDeviceSupportsPSIZE = true;
else
bDeviceSupportsPSIZE = false;
// Initialize our particles so they'll start at the origin with some
// random direction and color.
for( int i = 0; i < MAX_PARTICLES; ++i )
{
angle[i]= dbRnd(2*pi);
radius[i]=dbRnd(Rmax)+1.0;
float r=radius[i];
float a=angle[i]+speed; // increment angle
angle[i]=a;
float x = r*sin(a);
float y= r*eratio*cos(a);
float b=r*etwist;
float s=sin(b);
float c=cos(b);
g_particles[i].m_vCurPos = D3DXVECTOR3(s*x+c*y+dbRnd(200)/20.0,dbRnd(200)/20.0,c*x-s*y+dbRnd(200)/20.0);
g_particles[i].m_vCurVel = getRandomVector() * getRandomMinMax( 0.3f, 0.5f );
g_particles[i].m_vColor = D3DCOLOR_COLORVALUE( getRandomMinMax( 0.0f, 1.0f ),
getRandomMinMax( 0.0f, 1.0f ),
getRandomMinMax( 0.0f, 1.0f ),
1.0f );
}
}
void updatePointSprites( void )
{
//
// To repeat the sample automatically, keep track of the overall app time.
//
static double dStartAppTime = timeGetTime();
float fElpasedAppTime = (float)((timeGetTime() - dStartAppTime) * 0.001);
//
// To move the particles via their velocity, keep track of how much time
// has elapsed since last frame update...
//
static double dLastFrameTime = timeGetTime();
double dCurrenFrameTime = timeGetTime();
double dElpasedFrameTime = (float)((dCurrenFrameTime - dLastFrameTime) * 0.001);
dLastFrameTime = dCurrenFrameTime;
//
// Move each particle via its velocity and elapsed frame time.
//
for( int i = 0; i < MAX_PARTICLES; ++i )
g_particles[i].m_vCurPos += g_particles[i].m_vCurVel * (float)dElpasedFrameTime;
}
//-----------------------------------------------------------------------------
// Name: renderPointSprites()
// Desc:
//-----------------------------------------------------------------------------
void renderPointSprites( void )
{
//
// Setting D3DRS_ZWRITEENABLE to FALSE makes the Z-Buffer read-only, which
// helps remove graphical artifacts generated from rendering a list of
// particles that haven't been sorted by distance to the eye.
//
// Setting D3DRS_ALPHABLENDENABLE to TRUE allows particles, which overlap,
// to alpha blend with each other correctly.
//
g_pd3dDevice->SetTexture( 0, g_pTexture );
g_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );
g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
g_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );
//
// Set up the render states for using point sprites...
//
g_pd3dDevice->SetRenderState( D3DRS_POINTSPRITEENABLE, TRUE ); // Turn on point sprites
g_pd3dDevice->SetRenderState( D3DRS_POINTSCALEENABLE, TRUE ); // Allow sprites to be scaled with distance
g_pd3dDevice->SetRenderState( D3DRS_POINTSIZE, FtoDW(3.0) ); // Float value that specifies the size to use for point size computation in cases where point size is not specified for each vertex.
g_pd3dDevice->SetRenderState( D3DRS_POINTSIZE_MIN, FtoDW(1.0f) ); // Float value that specifies the minimum size of point primitives. Point primitives are clamped to this size during rendering.
g_pd3dDevice->SetRenderState( D3DRS_POINTSCALE_A, FtoDW(0.0f) ); // Default 1.0
g_pd3dDevice->SetRenderState( D3DRS_POINTSCALE_B, FtoDW(0.0f) ); // Default 0.0
g_pd3dDevice->SetRenderState( D3DRS_POINTSCALE_C, FtoDW(1.0f) ); // Default 0.0
//
// Lock the vertex buffer, and set up our point sprites in accordance with
// our particles that we're keeping track of in our application.
//
Vertex *pPointVertices;
g_pVertexBuffer->Lock( 0, MAX_PARTICLES * sizeof(Vertex),
(void**)&pPointVertices, D3DLOCK_DISCARD );
for( int i = 0; i < MAX_PARTICLES; ++i )
{
pPointVertices->posit = g_particles[i].m_vCurPos;
pPointVertices->color = g_particles[i].m_vColor;
pPointVertices++;
}
g_pVertexBuffer->Unlock();
//
// Render point sprites...
//
g_pd3dDevice->SetStreamSource( 0, g_pVertexBuffer, 0, sizeof(Vertex) );
g_pd3dDevice->SetFVF( Vertex::FVF_Flags );
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
g_pd3dDevice->DrawPrimitive( D3DPT_POINTLIST, 0, MAX_PARTICLES );
//
// Reset render states...
//
g_pd3dDevice->SetRenderState( D3DRS_POINTSPRITEENABLE, FALSE );
g_pd3dDevice->SetRenderState( D3DRS_POINTSCALEENABLE, FALSE );
g_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, TRUE );
g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
}
d3dfunc.h
void initPointSprites( void );
void renderPointSprites( void );
void updatePointSprites( void );