This topic is now archived and is closed to further replies.



Recommended Posts

Does anyone know of, or has written, or perhaps can write, a simple example of creating, texturing, and displaying one simple point sprite without any extras? I really want to learn to use them so I can attempt a particle engine. I read over the msdn SDK example and I just get so lost because it has millions of calculations, variables, and extra more complicated code. I also looked over the tutorials in msdn but thier texturing example doesn''t deal with alpha values. Things i am fuzzy on are: what fvf to use how to combine texture for alpha with color in the program (big one) what coords are needed, a single vertex, a triangle, or a quad how to change the size or mass of the particle (point sprite) I am pretty lost aren''t I? Saving up money for some good direct3d books but still trying to learn what i can until that time. I made an attempt at a program that is supposed to display one pointsprite large enough for the viewer to see however nothing is displayed and I can''t figure out why. I will include the source in case I am lucky enough to find someone willing to go through it. Thanx, Christopher //--------------------------------------------------------------------- // GraphicSystem.cpp // // Christopher Pisz // 10/25/02 //--------------------------------------------------------------------- #include "GraphicSystem.h" LPDIRECT3D8 g_d3d =NULL; // D3D object LPDIRECT3DDEVICE8 g_device =NULL; // Our rendering device D3DXMATRIX g_matrix_world; // World matrix D3DXMATRIX g_matrix_view; // View matrix D3DXMATRIX g_matrix_proj; // Projection matrix LPDIRECT3DVERTEXBUFFER8 g_vertex_buffer =NULL; // Buffer to hold vertices LPDIRECT3DTEXTURE8 g_texture =NULL; // Single texture POINTVERTEX g_pointsprite; // One red point sprite //----------------------------------------------------------------------- // Initialize Direct3D //----------------------------------------------------------------------- HRESULT InitD3D(HWND hWnd) { D3DDISPLAYMODE mode; // Display Mode D3DPRESENT_PARAMETERS params; // Display Parameters // Create the D3D object, which is needed to create the // rendering device g_d3d=Direct3DCreate8(D3D_SDK_VERSION); if(!g_d3d) { MessageBox(NULL,"Failed to create D3D object", "InitD3D()",MB_OK | MB_ICONINFORMATION); return E_FAIL; } // Get the current desktop display mode if(FAILED(g_d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&mode))) { MessageBox(NULL,"Failed to get current display mode", "InitD3D()",MB_OK | MB_ICONINFORMATION); return E_FAIL; } // Set rendering device params ZeroMemory(¶ms,sizeof(params)); params.Windowed = TRUE; params.SwapEffect = D3DSWAPEFFECT_DISCARD; params.BackBufferFormat = mode.Format; params.EnableAutoDepthStencil = TRUE; params.AutoDepthStencilFormat = D3DFMT_D16; // Create the rendering device if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, ¶ms, &g_device))) { MessageBox(NULL,"Failed to create D3D device", "InitD3D()",MB_OK | MB_ICONINFORMATION); return E_FAIL; } // Turn on the zbuffer g_device->SetRenderState(D3DRS_ZENABLE,TRUE); // Enable alpha blending g_device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); g_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE); g_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); // Set the texture stage state g_device->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR ); g_device->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR ); g_device->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE ); g_device->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE ); // Enable point sprites g_device->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE); // Turn on ambient lighting g_device->SetRenderState(D3DRS_AMBIENT, 0xffffffff); // Set the point size g_device->SetRenderState(D3DRS_POINTSIZE, (DWORD)1.00f); return S_OK; } //---------------------------------------------------------------------- // Initialize Gemometry //---------------------------------------------------------------------- HRESULT InitGeometry() { BYTE* ptr =NULL; // Pointer to the vertex buffer memory // Initialize the pointsprite g_pointsprite.v =D3DXVECTOR3(0.0f, 0.0f, 0.0f); g_pointsprite.color =D3DCOLOR_RGBA(255,255,255,155); // Create a texture from a file based image if(FAILED(D3DXCreateTextureFromFile(g_device,"particle.bmp",&g_texture))) { MessageBox(NULL,"Failed to load texture", "InitGeometry()",MB_OK | MB_ICONINFORMATION); return E_FAIL; } // Create the vertex buffer if(FAILED(g_device->CreateVertexBuffer(1*sizeof(POINTVERTEX), 0,D3DFVF_POINTVERTEX,D3DPOOL_DEFAULT,&g_vertex_buffer))) { MessageBox(NULL,"Failed to Create Vertex Buffer", "InitVB()",MB_OK | MB_ICONINFORMATION); return E_FAIL; } // Fill the vertex buffer if(FAILED(g_vertex_buffer->Lock(0,sizeof(g_pointsprite), &ptr,0))) { MessageBox(NULL,"Failed to lock vertex buffer", "InitVB()",MB_OK | MB_ICONINFORMATION); return E_FAIL; } memcpy(ptr, &g_pointsprite ,sizeof(g_pointsprite)); g_vertex_buffer->Unlock(); return S_OK; } //---------------------------------------------------------------------- // Set Matrices //---------------------------------------------------------------------- HRESULT SetMatrices() { // Set up the projection matrix D3DXMatrixIdentity(&g_matrix_proj); D3DXMatrixPerspectiveFovLH(&g_matrix_proj,D3DX_PI/4, 1.0f, 1.0f, 100.0f); g_device->SetTransform(D3DTS_PROJECTION,&g_matrix_proj); // Set up our view matrix. D3DXMatrixIdentity(&g_matrix_view); D3DXMatrixLookAtLH(&g_matrix_view, &D3DXVECTOR3( 0.0f, 0.0f,-3.0f), &D3DXVECTOR3( 0.0f, 0.0f, 0.0f), &D3DXVECTOR3( 0.0f, 1.0f, 0.0f)); g_device->SetTransform(D3DTS_VIEW,&g_matrix_view); // Set world matrix D3DXMatrixIdentity(&g_matrix_world); g_device->SetTransform(D3DTS_WORLD,&g_matrix_world); return S_OK; } //---------------------------------------------------------------------- // Render the scene //---------------------------------------------------------------------- HRESULT Render() { // Check for valid device if(!g_device) return E_FAIL; // Clear the backbuffer g_device->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0); // Begin the scene g_device->BeginScene(); // Set up the matrices SetMatrices(); // Rendering of scene objects can happen here g_device->SetTexture(0, g_texture); g_device->SetStreamSource(0,g_vertex_buffer,sizeof(POINTVERTEX)); g_device->SetVertexShader(D3DFVF_POINTVERTEX); g_device->DrawPrimitive(D3DPT_POINTLIST,0,1); // End the scene g_device->EndScene(); // Present the backbuffer contents to the display g_device->Present(NULL,NULL,NULL,NULL); return S_OK; } //---------------------------------------------------------------------- // Clean-up Direct3D //---------------------------------------------------------------------- VOID CleanupD3D() { // Release texture if(g_texture) { g_texture->Release(); g_texture=NULL; } // Release the device object if(g_device) { g_device->Release(); g_device=NULL; } // Release the Direct3D COM object if(g_d3d) { g_d3d->Release(); g_d3d=NULL; } } I sure hope the source tags worked, they didn''t last time. Thanx, Christopher

Share this post

Link to post
Share on other sites