Archived

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

Helius

Making spheres in Viusal Basic

Recommended Posts

Hi! I have to make spheres in DirectX using Visual Basic. Before this i used a method in Visual C++ but now i have to port it to VB, but i can''t figure out how! here you can see my actual code in c++
  
bool CSphere::UpdateVertices()
{
	WORD* pIndices;
	SPHERE_CUSTOMVERTEX* pVertex; 
	WORD wVertexIndex = 0;
	int nCurrentRing;
	int nCurrentSegment;
	D3DXVECTOR3 vNormal;

	//Lock the vertex buffer

	if(FAILED(m_pVertexBuffer->Lock(0, 0, (BYTE**)&pVertex, 0)))
	{
		LogError("<li>CSphere: Unable to lock vertex buffer.");
		return false;
	}

	//Lock the index buffer 

	if(FAILED(m_pIndexBuffer->Lock(0, m_dwNumOfIndices, (BYTE**)&pIndices, 0)))
	{
		LogError("<li>CSphere: Unable to lock index buffer.");
		return false;
	}

	//Establish constants used in sphere generation

	FLOAT rDeltaRingAngle = (D3DX_PI / m_nRings);
	FLOAT rDeltaSegAngle = (2.0f * D3DX_PI / m_nSegments);

	//Generate the group of rings for the sphere

	for(nCurrentRing = 0; nCurrentRing < m_nRings + 1; nCurrentRing++)
	{
		FLOAT r0 = sinf(nCurrentRing * rDeltaRingAngle);
		FLOAT y0 = cosf(nCurrentRing * rDeltaRingAngle);

		//Generate the group of segments for the current ring

		for(nCurrentSegment = 0; nCurrentSegment < m_nSegments + 1; nCurrentSegment++)
		{
			FLOAT x0 = r0 * sinf(nCurrentSegment * rDeltaSegAngle);
			FLOAT z0 = r0 * cosf(nCurrentSegment * rDeltaSegAngle);

			vNormal.x = x0;
			vNormal.y = y0;
			vNormal.z = z0;
	
			D3DXVec3Normalize(&vNormal, &vNormal);

			//Add one vertex to the strip which makes up the sphere

			pVertex->x = x0;
			pVertex->y = y0;
			pVertex->z = z0;
			pVertex->nx = vNormal.x;
			pVertex->ny = vNormal.y;
			pVertex->nz = vNormal.z;
			pVertex->tu = 1.0f - ((FLOAT)nCurrentSegment / (FLOAT)m_nSegments);
			pVertex->tv = (FLOAT)nCurrentRing / (FLOAT)m_nRings;

			pVertex++;
			
			//Add two indices except for the last ring 

			if(nCurrentRing != m_nRings) 
			{
				*pIndices = wVertexIndex; 
				pIndices++;
				
				*pIndices = wVertexIndex + (WORD)(m_nSegments + 1); 
				pIndices++;
				
				wVertexIndex++; 
			}
		}
	}

	if(FAILED(m_pIndexBuffer->Unlock()))
	{
		LogError("<li>CSphere: Unable to unlock index buffer.");
		return false;
	}

	if(FAILED(m_pVertexBuffer->Unlock()))
	{
		LogError("<li>CSphere: Unable to unlock vertex buffer.");
		return false;
	}

	return true; 
}
  
Anyone can help?? Thanks in advance

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
really simple way:

where d3dx is a D3DX8 object, sphereMesh is a D3DXMesh object, and buffer is a D3DXBuffer object, and theDevice is a direct3d device:

set sphereMesh = d3dx.CreateSphere(theDevice, radius,slices,stacks,buff)

then to render:

sphereMesh.DrawSubset(0)

off the top of my head, but should work.

Share this post


Link to post
Share on other sites
Thanks, but i don''t wont to use that function because it''s difficult to map textures with it.
My problem is not how to create the sphere, my problem is the way to change those pointers and locks and unlocks to fit VB, and use this pair of functions:

D3DIndexBuffer8SetData()
D3DVertexBuffer8SetData()

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dx8_vb/directx_vb/dx8start_vb_81.asp


try that link - should have everything you need to know.

Share this post


Link to post
Share on other sites