Drawing a sphere: Matrices 2nd Ed

Started by
0 comments, last by nedemon 18 years, 7 months ago
Greetings, gentlemen. I've got another problem with a view matrix. I figured the code for sphere, but i'm unable to view it. Actually, i don't even know if this code outputs anything, im just trying to get an algorithm for outputting the Sphere and since here i tried to use normals it's difficult to view anything. So i would be thankful for any information on how to output the inside of the vertex buffer.

HRESULT InitGeometry()
{
	const FLOAT drho = D3DX_PI/49.0f;
	const FLOAT dtheta = 2.0f*D3DX_PI/49.0f;
	int i,j, k = 0;
	FLOAT rho, theta;
	CUSTOMVERTEX g_Vertices[5000];
	MyCVector tv1, tv2, tv3, nV = {0,0,0};
    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 5000*sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
    {
        return E_FAIL;
    }
    VOID* pVertices;
    if( FAILED( g_pVB->Lock( 0, 5000*sizeof(CUSTOMVERTEX), (void**)&pVertices, 0 )))
        return E_FAIL;
	
	for(i = 0; i < 48; i++)
	{
		rho = i*drho;
	for(j = 0; j < 49; j++)
	{
		theta = j*dtheta;
	tv1.x = sinf(theta)*sinf(rho);
	tv1.y = cosf(theta)*sinf(rho);
	tv1.z = cosf(rho);
	
	tv2.x = -sinf(theta)*sinf(rho+drho);
	tv2.y = cosf(theta)*sinf(rho+drho);
	tv2.z = cosf(rho+drho);

	tv3.x = -sinf((j+1)*dtheta)*sinf(rho);
	tv3.y = cosf((j+1)*dtheta)*sinf(rho);
	tv3.z = cosf(rho);
	CalcNormals(tv1, tv2, tv3, nV);
	if(!i)
	{
		g_Vertices[k].x = tv1.x;
		g_Vertices[k].y = tv1.y;
		g_Vertices[k].z = tv1.z;
		g_Vertices[k].nX = nV.x;
		g_Vertices[k].nY = nV.y;
		g_Vertices[k].nZ = nV.z;
		g_Vertices[k].color = D3DCOLOR_XRGB(0, 0, 255);
		g_Vertices[k].u = j/49.0f;
		g_Vertices[k].v = i/49.0f;
		k++;
		g_Vertices[k].x = tv2.x;
		g_Vertices[k].y = tv2.y;
		g_Vertices[k].z = tv2.z;
		g_Vertices[k].nX = nV.x;
		g_Vertices[k].nY = nV.y;
		g_Vertices[k].nZ = nV.z;
		g_Vertices[k].color = D3DCOLOR_XRGB(0, 0, 255);
		g_Vertices[k].u = j/49.0f;
		g_Vertices[k].v = (i+1)/49.0f;
		k++;
	}
	g_Vertices[k].x = tv3.x;
	g_Vertices[k].y = tv3.y;
	g_Vertices[k].z = tv3.z;
	g_Vertices[k].nX = nV.x;
	g_Vertices[k].nY = nV.y;
	g_Vertices[k].nZ = nV.z;
	g_Vertices[k].color = D3DCOLOR_XRGB(0, 0, 255);
	g_Vertices[k].u = (j+1)/49.0f;
	g_Vertices[k].v = i/49.0f;
	k++;
tv1.x = -sinf((j+1)*dtheta)*sinf((i+1)*drho);
tv1.y = cosf((j+1)*dtheta)*sinf((i+1)*drho);
tv1.z = cosf((i+1)*drho);
tv2.x = -sinf(theta)*sinf((i+1)*drho+drho);
tv2.y = cosf(theta)*sinf((i+1)*drho+drho);
tv2.z = cosf((i+1)*drho + drho);
tv3.x = -sinf((j+1)*dtheta)*sinf(i*drho+drho);
tv3.y = cosf((j+1)*dtheta)*sinf(i*drho+drho);
tv3.z = cosf(i+drho+drho);
CalcNormals(tv1, tv2, tv3, nV);
g_Vertices[k].x = tv1.x;
g_Vertices[k].y = tv1.y;
g_Vertices[k].z = tv1.z;	
g_Vertices[k].nX = nV.x;
g_Vertices[k].nY = nV.y;
g_Vertices[k].nZ = nV.z;
g_Vertices[k].color = D3DCOLOR_XRGB(0, 0, 255); 
g_Vertices[k].u = (j+1)/49.0f;
g_Vertices[k].v = (i+1)/49.0f;
k++;
}
}
    memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
    g_pVB->Unlock();

    return S_OK;
}
VOID Render()
{
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );

    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {
       
        SetupMatrices();
        g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
        g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
        g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 5000 - 2);

        g_pd3dDevice->EndScene();
    }

    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}



Advertisement
I almost figured it, but now i need to set the world matrix, guess. And if i change the coordinates to XYZRHW, the sphere appears, it just that i can't move it in the middle of the screen. And of course i can't even make it appear if i got XYZ|NORMAL. Thanx.

//-----------------------------------------------------------------------------#include <Windows.h>#include <mmsystem.h>#include <d3dx9.h>#include <math.h>#include <fstream.h>ofstream out("out.txt");//-----------------------------------------------------------------------------// Global variables//-----------------------------------------------------------------------------LPDIRECT3D9             g_pD3D       = NULL;LPDIRECT3DDEVICE9       g_pd3dDevice = NULL;LPDIRECT3DVERTEXBUFFER9 g_pVB        = NULL;struct CUSTOMVERTEX{    FLOAT x, y, z;	FLOAT u,v,nX,nY,nZ;    DWORD color;};/*struct CUSTOMVERTEX{    FLOAT x, y, z, rhw;    DWORD color;};*/struct MyCVector{	FLOAT x,y,z;};//#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_NORMAL|D3DFVF_TEX1)//#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)void CalcNormals(const MyCVector v1,const MyCVector v2,				 const MyCVector v3, MyCVector eV){	FLOAT nx, ny, nz, ss;	nx = (v1.x + v2.x + v3.x)/3;	ny = (v1.y + v2.y + v3.y)/3;	nz = (v1.z + v2.z + v3.z)/3;	ss = sqrtf(nx*nx+ny*ny+nz*nz);	eV.x = nx/ss; eV.y = ny/ss; eV.z = nz/ss; 	return;}//-----------------------------------------------------------------------------// Name: InitD3D()// Desc: Initializes Direct3D//-----------------------------------------------------------------------------HRESULT InitD3D( HWND hWnd ){    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )        return E_FAIL;    D3DPRESENT_PARAMETERS d3dpp;    ZeroMemory( &d3dpp, sizeof(d3dpp) );    d3dpp.Windowed = TRUE;    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;    // Create the D3DDevice    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,                                      &d3dpp, &g_pd3dDevice ) ) )    {        return E_FAIL;    }    // Turn off culling, so we see the front and back of the triangle    g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );    // Turn off D3D lighting, since we are providing our own vertex colors//    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );    return S_OK;}//-----------------------------------------------------------------------------// Name: InitGeometry()// Desc: Creates the scene geometry//-----------------------------------------------------------------------------HRESULT InitGeometry(){	const FLOAT drho = D3DX_PI/49.0f;	const FLOAT dtheta = 2.0f*D3DX_PI/49.0f;	int i,j, k = 0;	FLOAT rho, theta;	CUSTOMVERTEX g_Vertices[5000];	MyCVector tv1, tv2, tv3, nV = {0,0,0};    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 5000*sizeof(CUSTOMVERTEX),                                                  0, D3DFVF_CUSTOMVERTEX,                                                  D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )    {        return E_FAIL;    }    VOID* pVertices;    if( FAILED( g_pVB->Lock( 0, 5000*sizeof(CUSTOMVERTEX), (void**)&pVertices, 0 )))        return E_FAIL;		for(i = 0; i < 48; i++)	{		rho = i*drho;	for(j = 0; j < 49; j++)	{		theta = j*dtheta;	tv1.x = sinf(theta)*sinf(rho);	tv1.y = cosf(theta)*sinf(rho);	tv1.z = cosf(rho);		tv2.x = -sinf(theta)*sinf(rho+drho);	tv2.y = cosf(theta)*sinf(rho+drho);	tv2.z = cosf(rho+drho);	tv3.x = -sinf((j+1)*dtheta)*sinf(rho);	tv3.y = cosf((j+1)*dtheta)*sinf(rho);	tv3.z = cosf(rho);	CalcNormals(tv1, tv2, tv3, nV);	if(!i)	{		out<<(g_Vertices[k].x = tv1.x*10)<<endl;		out<<(g_Vertices[k].y = tv1.y*10)<<endl;		out<<(g_Vertices[k].z = tv1.z*10)<<endl;//		g_Vertices[k].rhw = 1.0f;		g_Vertices[k].nX = nV.x;		g_Vertices[k].nY = nV.y;		g_Vertices[k].nZ = nV.z;		g_Vertices[k].color = D3DCOLOR_XRGB(0, 0, 255);		g_Vertices[k].u = j/49.0f;		g_Vertices[k].v = i/49.0f;		k++;		out<<(g_Vertices[k].x = tv2.x*10)<<endl;		out<<(g_Vertices[k].y = tv2.y*10)<<endl;		out<<(g_Vertices[k].z = tv2.z*10)<<endl;//		g_Vertices[k].rhw = 1.0f;		g_Vertices[k].nX = nV.x;		g_Vertices[k].nY = nV.y;		g_Vertices[k].nZ = nV.z;		g_Vertices[k].color = D3DCOLOR_XRGB(0, 0, 255);		g_Vertices[k].u = j/49.0f;		g_Vertices[k].v = (i+1)/49.0f;		k++;	}	g_Vertices[k].x = tv3.x*10;	g_Vertices[k].y = tv3.y*10;	g_Vertices[k].z = tv3.z*10;//	g_Vertices[k].rhw = 1.0f;	g_Vertices[k].nX = nV.x;	g_Vertices[k].nY = nV.y;	g_Vertices[k].nZ = nV.z;	g_Vertices[k].color = D3DCOLOR_XRGB(0, 0, 255);	g_Vertices[k].u = (j+1)/49.0f;	g_Vertices[k].v = i/49.0f;	k++;tv1.x = -sinf((j+1)*dtheta)*sinf((i+1)*drho);tv1.y = cosf((j+1)*dtheta)*sinf((i+1)*drho);tv1.z = cosf((i+1)*drho);tv2.x = -sinf(theta)*sinf((i+1)*drho+drho);tv2.y = cosf(theta)*sinf((i+1)*drho+drho);tv2.z = cosf((i+1)*drho + drho);tv3.x = -sinf((j+1)*dtheta)*sinf(i*drho+drho);tv3.y = cosf((j+1)*dtheta)*sinf(i*drho+drho);tv3.z = cosf(i+drho+drho);CalcNormals(tv1, tv2, tv3, nV);g_Vertices[k].x = tv1.x*10;g_Vertices[k].y = tv1.y*10;g_Vertices[k].z = tv1.z*10;//g_Vertices[k].rhw = 1.0f;g_Vertices[k].nX = nV.x;g_Vertices[k].nY = nV.y;g_Vertices[k].nZ = nV.z;g_Vertices[k].color = D3DCOLOR_XRGB(0, 0, 255); g_Vertices[k].u = (j+1)/49.0f;g_Vertices[k].v = (i+1)/49.0f;k++;}}    memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );    g_pVB->Unlock();    return S_OK;}//-----------------------------------------------------------------------------// Name: Cleanup()// Desc: Releases all previously initialized objects//-----------------------------------------------------------------------------VOID Cleanup(){    if( g_pVB != NULL )        g_pVB->Release();    if( g_pd3dDevice != NULL )        g_pd3dDevice->Release();    if( g_pD3D != NULL )        g_pD3D->Release();}//-----------------------------------------------------------------------------// Name: SetupMatrices()// Desc: Sets up the world, view, and projection transform matrices.//-----------------------------------------------------------------------------VOID SetupMatrices(){    D3DXMATRIXA16 matWorld;    matWorld(0,0)		g_pd3dDevice->SetTransform( D3DTS_VIEW, &matWorld );	D3DXVECTOR3 vEyePt( 0.0f, 0.0f,-2.50f );    D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );    D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );    D3DXMATRIXA16 matView;    D3DXMatrixLookAtRH( &matView, &vEyePt, &vLookatPt, &vUpVec );    g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );	//	D3DXMATRIXA16 matProj;	FLOAT h = (cosf(1.0f/2.0f)/sinf(1.0f/2.0f));	FLOAT w = 1 * h;	FLOAT Q = 5.0f / (5.0f - 1.0f);	ZeroMemory(matProj, sizeof(matProj));	matProj(0,0) = 1*h;	matProj(1,1) = h;	matProj(2,2) = Q;	matProj(2,3) = 1.0f;	matProj(3,2) = -Q;    g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);}//-----------------------------------------------------------------------------// Name: Render()// Desc: Draws the scene//-----------------------------------------------------------------------------VOID Render(){    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )    {               SetupMatrices();        g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );        g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );        g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 5000 - 2);        g_pd3dDevice->EndScene();    }    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );}//-----------------------------------------------------------------------------// Name: MsgProc()// Desc: The window's message handler//-----------------------------------------------------------------------------LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){    switch( msg )    {        case WM_DESTROY:            Cleanup();            PostQuitMessage( 0 );            return 0;    }    return DefWindowProc( hWnd, msg, wParam, lParam );}//-----------------------------------------------------------------------------// Name: WinMain()// Desc: The application's entry point//-----------------------------------------------------------------------------INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ){    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,                      "D3D", NULL };    RegisterClassEx( &wc );    HWND hWnd = CreateWindow( "D3D", "D_3_D",                              WS_OVERLAPPEDWINDOW, 100, 100, 256, 256,                              GetDesktopWindow(), NULL, wc.hInstance, NULL );    if( SUCCEEDED( InitD3D( hWnd ) ) )    {        if( SUCCEEDED( InitGeometry() ) )        {            ShowWindow( hWnd, SW_SHOWDEFAULT );            UpdateWindow( hWnd );            MSG msg;            ZeroMemory( &msg, sizeof(msg) );            while( msg.message!=WM_QUIT )            {                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )                {                    TranslateMessage( &msg );                    DispatchMessage( &msg );                }                else                    Render();            }        }    }    UnregisterClass( "D3D", wc.hInstance );    return 0;}

This topic is closed to new replies.

Advertisement