   Getting Vertex Buffer for SetStreamSource()
  Hi, I'm trying to use the SetStreamSource() function but I am encountering a problem. To get the vertext buffer from my mesh I am using:m_mesh->GetVertexBuffer(m_VertexBuffer);However, for this function to return the value to the parameter my m_VertexBuffer must be of type LPDIRECT3DVERTEXBUFFER9*. This is causing a problem when I try to call the SetStreamSource function like so:d3dDevice->SetStreamSource( 0, m_VertexBuffer, 0, m_mesh->GetNumBytesPerVertex())as the vertex buffer that needs to be passed has to be a IDirect3DVertexBuffer9 * so it is throwing up an error.Anybody any ideas please????Thanks   
Quote:Original post by DOrmisherHi, I'm trying to use the SetStreamSource() function but I am encountering a problem. To get the vertext buffer from my mesh I am using:m_mesh->GetVertexBuffer(m_VertexBuffer);However, for this function to return the value to the parameter my m_VertexBuffer must be of type LPDIRECT3DVERTEXBUFFER9*. This is causing a problem when I try to call the SetStreamSource function like so:d3dDevice->SetStreamSource( 0, m_VertexBuffer, 0, m_mesh->GetNumBytesPerVertex())as the vertex buffer that needs to be passed has to be a IDirect3DVertexBuffer9 * so it is throwing up an error.Anybody any ideas please????ThanksAlmost all D3D/D3DX Get*() calls take a pointer to a pointer as a parameter, and they fill in that pointer. So they return a pointer indirectly. You want to do this:LPDIRECT3DVERTEXBUFFER9 pVB;  // Declare a pointer to a vertex bufferHRESULT hResult = m_mesh->GetVertexBuffer(&pVB); // Set pVB to the correct pointerif(SUCCEEDED(hResult)){   d3dDevice->SetStreamSource( 0, pVB, 0, etc); // Use the vertex buffer   // Call Draw[Indexed]Primitive, etc here   pVB->Release(); // Always Release() a pointer from a Get*() call.}Firstly, you don't want to allocate a vertex buffer for D3DX to return, you just need a pointer that D3DX will set for you.Secondly, you must always Release() an interface returned from a D3D/D3DX Get*() call once you're finished with it, because the call will increase the reference count on the interface (As I believe the docs say).Thirdly, ALWAYS check the return value for any function that returns a pointer or other data that you rely on. Without the if(SUCCEEDED(hResult)) line in the above snippet, your app would crash because you're using a bad pointer.   ;  Cheers dude its got rid of the errors, still not rendering the model though! This is the code for my model Render() function, does it look ok? In my window when I run my program all I can see is grey and a black object that flickers around when I move the camera.LPDIRECT3DVERTEXBUFFER9 VertexBuffer;HRESULT hr = m_mesh->GetVertexBuffer(&VertexBuffer); // Set pVB to the correct pointerif(SUCCEEDED(hr)){d3dDevice->SetStreamSource( 0, VertexBuffer, 0, m_mesh->GetNumBytesPerVertex()/*m_VertexSize*/ );}d3dDevice->SetFVF( m_mesh->GetFVF() );// Now tell DirectX the index buffer to useLPDIRECT3DINDEXBUFFER9  IndexBuffer;hr = m_mesh->GetIndexBuffer( &IndexBuffer );if(SUCCEEDED(hr)){d3dDevice->SetIndices( IndexBuffer );}// Draw the primitives from the vertex buffer - a triangle listd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,  // Primitive type - usually tri-list or strip 0,    // Offset to add to all indices (0 in simple cases) 0,    // Minimum index used (allows for optimisation)  m_mesh->GetNumVertices(),// Range of vertices refered to, effectively =   //maximum index - minimum index + 1 0,    // Position to start at in index buffer m_mesh->GetNumFaces() );  // Number of primitives to render (triangles)VertexBuffer->Release();IndexBuffer->Release();;  You need all of that code inside the if(SUCCEEDED(hr)) statement, otherwise you could end up calling Release() on an invalid pointer.As for the actual problem, is there a reason you're trying to draw the mesh manually instead of with DrawSubset?Are you clearing the depth buffer every frame? Not clearing it is the usual cause for something flickering on screen. Any output from the Debug runtimes?   ;  The only reason I wasn't using draw subset was because I was getting the flickering black model when I was trying to use it so I thought I'd try it another way.  Is it still possible to use shaders when using the DrawSubset method?  For example should the following code work correctly?// Set vertex and pixel shader to render basic transformed, single colour objectsSetVertexShader( Engine->VS_PixelLighting, Engine->m_d3dDevice );SetPixelShader( Engine->PS_PixelLighting, Engine->m_d3dDevice );// Pass camera matrices to the vertex shader, these are the same for all modelsEngine->VS_PixelLightingConsts->SetMatrix( Engine->m_d3dDevice, "ViewProjMatrix", &viewProjMatrix );// Calculate world matrix for first model (floor) and send to vertex shaderfloor1->CalculateMatrix();worldMatrix = floor1->GetWorldMatrix();Engine->VS_PixelLightingConsts->SetMatrix( Engine->m_d3dDevice, "WorldMatrix", &worldMatrix );// Set the plain colour to use in the pixel shaderEngine->PS_PixelLightingConsts->SetFloatArray( Engine->m_d3dDevice, "Colour", (FLOAT*)&Black, 4 );floor1->Render(Engine->m_d3dDevice);...and then in the the model render method is the following:for (DWORD i=0; i<m_numMaterials; i++){   // Set the material and texture for this subset  gD3dDevice->SetMaterial(&meshMaterials);  gD3dDevice->SetTexture(0,meshTextures);// Draw the mesh subset  m_mesh->DrawSubset( i );}Sorry I keep asking lots of stuff but I'm pretty new to DirectX!   ;  Yup, that should work fine. Internally, DrawSubset() just calls DrawPrimitive like you do manually.Some other causes of flickering are having a corrupt projection matrix (The near clip plane must be non-zero or your projection matrix gets NaNs in it), or having the near and far planes poorly set. Your near clip plane should be as far as possible, definitely higher than 0.01, and preferably more like 1.0 or 0.1. Your far clip plane should also be "reasonable". Anything over 10000.0 is too far, expecially if you're using a 16-bit depth buffer format.   ;  and p.s. yeah i am clearing the z buffer!   ;  Yeah my near an far clip are normal just like you said, probably something else pretty minor i'll just have to keep having a mess around thanks anyway!!   ;  One other thing that could potentially cause problems is if your FOV is a bizarre value (E.g. if it's in degrees, D3D expects radians).Can you explain what you mean by the model flickers? Is it animating or moving at all, or is it just static? If you put a breakpoint on your Present() call, does it flicker every frame or something? Can we get a screenshot (Upload to imageshack or something, and then post here with HTML)?   