示例#1
0
文件: app.cpp 项目: Alriightyman/RTS
HRESULT APPLICATION::Render()
{
    // Clear the viewport
    m_pDevice->Clear( 0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0L );

    // Begin the scene 
    if(SUCCEEDED(m_pDevice->BeginScene()))
    {
		if(m_wireframe)m_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);	
		else m_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);

		m_terrain.Render();

		RECT r[] = {{10, 10, 0, 0}, {10, 30, 0, 0}, {10, 50, 0, 0}, {400, 10, 0, 0}, {600, 10, 0, 0}};
		m_pFont->DrawText(NULL, "W: Toggle Wireframe", -1, &r[0], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
		m_pFont->DrawText(NULL, "+/-: Zoom In/Out", -1, &r[1], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
		m_pFont->DrawText(NULL, "SPACE: Randomize Map", -1, &r[2], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
		m_pFont->DrawText(NULL, "F5: Save Terrain", -1, &r[3], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
		m_pFont->DrawText(NULL, "F6: Load Terrain", -1, &r[4], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);

        // End the scene.
		m_pDevice->EndScene();
		m_pDevice->Present(0, 0, 0, 0);
    }

	return S_OK;
}
示例#2
0
文件: app.cpp 项目: Alriightyman/RTS
HRESULT APPLICATION::Render()
{
    // Clear the viewport
    m_pDevice->Clear( 0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0L );

    // Begin the scene 
    if(SUCCEEDED(m_pDevice->BeginScene()))
    {
		if(m_wireframe)m_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);	
		else m_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);

		//Render either of the meshes
		if(m_activeMesh == 0)
			m_mesh1.Render();
		else m_mesh2.Render();

		RECT r[] = {{10, 10, 0, 0}, {10, 30, 0, 0}};
		m_pFont->DrawText(NULL, "Space: Next Object", -1, &r[0], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
		m_pFont->DrawText(NULL, "W: Toggle Wireframe", -1, &r[1], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);

        // End the scene.
		m_pDevice->EndScene();
		m_pDevice->Present(0, 0, 0, 0);
    }

	return S_OK;
}
示例#3
0
void UIRender::Render()
{
	// 窗口最小化不进行render
	HWND hWnd = UIDXInit::GetSingleton()->GetBindHWnd();
	if( hWnd!=0 )
	{
		if( IsIconic( hWnd )!=0 ) 
			return;
	}
		
	// 清除缓存表面
	IDirect3DDevice9* pIDevice = UIDXInit::GetSingleton()->_pIDevice;
	pIDevice->Clear( 0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER| D3DCLEAR_STENCIL, 0xffffffff, 1.0f, 0 );
	
	pIDevice->BeginScene();

	// 顶层容器绘制子窗口
	UIWinShell::_uiTopContainer.Draw();

	// 动画主动绘制
	UIAnimateManage::GetSingleton()->DrawAutoList();

	static UI3DCubeTest cube;
	cube.Draw();

	pIDevice->EndScene();

	// Swap the back and front buffers.
	HRESULT hr = pIDevice->Present( 0, 0, 0, 0 );
}
示例#4
0
///////////////////////////////////////////////////////////////////////////////
// Draw the scene.
// 
void RenderWindow::Draw()
{
    IDirect3DDevice9* device = m_Render.GetD3DDevice();
    if ( !device )
    {
        return;
    }

    if ( m_IsDeviceLost )
    {
        if ( device->TestCooperativeLevel() == D3DERR_DEVICENOTRESET )
        {
            m_Render.Reset();
            m_IsDeviceLost = device->TestCooperativeLevel() != D3D_OK;
        }
    }

    if ( m_IsDeviceLost )
    {
        return;
    }

    // If we have a scene with a mesh, draw it
    if ( !RenderScene() )
    {
        // No scene or no mesh, just clear the screen
        device->BeginScene();
        device->SetRenderTarget( 0, m_Render.GetBackBuffer() );
        device->SetDepthStencilSurface( m_Render.GetDepthBuffer() );
        device->Clear( 0, 0, D3DCLEAR_TARGET | D3DCLEAR_STENCIL | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0xFF, 0xB8, 0xB8, 0xB8), 1.0f, 0 );
        device->EndScene();
    }

    m_IsDeviceLost = ( m_Render.Display( GetHwnd() ) == D3DERR_DEVICELOST );
}
示例#5
0
void
sge::d3d9::devicefuncs::begin_scene(
	IDirect3DDevice9 &_device
)
{
	if(
		_device.BeginScene()
		!= D3D_OK
	)
		throw sge::renderer::exception(
			FCPPT_TEXT("BeginScene() failed!")
		);
}
示例#6
0
			void _draw(const DWORD time_interval)
			{
				//清除背景
				_device->Clear(0,0,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER ,0xffffffff,1.0f,0);
				//繪製場景
				_device->BeginScene();
				{
					draw(time_interval);
				}
				_device->EndScene();

				//交換緩衝區到 屏幕
				_device->Present(0,0,0,0);
			}
示例#7
0
bool MythRenderD3D9::Begin(void)
{
    D3D9Locker locker(this);
    IDirect3DDevice9* dev = locker.Acquire();
    if (!dev)
        return false;

    HRESULT hr = dev->BeginScene();
    if (FAILED(hr))
    {
        VERBOSE(VB_IMPORTANT, D3DERR + "BeginScene() failed.");
        return false;
    }
    return true;
}
示例#8
0
bool MythRenderD3D9::Begin(void)
{
    D3D9Locker locker(this);
    IDirect3DDevice9* dev = locker.Acquire();
    if (!dev)
        return false;

    HRESULT hr = dev->BeginScene();
    if (FAILED(hr))
    {
        LOG(VB_GENERAL, LOG_ERR, D3DLOC + "BeginScene() failed.");
        return false;
    }
    return true;
}
示例#9
0
文件: main.cpp 项目: snarf95/Snake
void Render()
{
	IDirect3DDevice9 *dev = GetDevice();
	if (dev)
	{
		dev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(50, 50, 200), 1.0f, 0);
		dev->BeginScene();

		for (int i = 0; i < length; ++i)
		{
			int value = segments[i];
			int x = value % size;
			int y = value / size;

			if (map[value])
			{
				D3DXMATRIX translation;
				D3DXMatrixTranslation(&translation, -1 + (x * width), -1 + (y * height), 0);
				dev->SetTransform(D3DTS_WORLD, &translation);

				dev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
			}
		}


		{
			int value = target;
			int x = value % size;
			int y = value / size;

			D3DXMATRIX translation;
			D3DXMatrixTranslation(&translation, -1 + (x * width), -1 + (y * height), 0);
			dev->SetTransform(D3DTS_WORLD, &translation);

			dev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
		}

		dev->EndScene();
		dev->Present(NULL, NULL, NULL, NULL);
	}
	else
	{
		Sleep(500);
		Beep(440, 100);
	}
}
示例#10
0
文件: app.cpp 项目: Alriightyman/RTS
HRESULT APPLICATION::Render()
{
    // Clear the viewport
    m_pDevice->Clear( 0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0L );

    // Begin the scene 
    if(SUCCEEDED(m_pDevice->BeginScene()))
    {
		if(m_pHeightMap != NULL)m_pHeightMap->Render();

		RECT r = {630, 10, 800, 30};
		m_pFont->DrawText(NULL, "Space: Change Image", -1, &r, DT_LEFT | DT_NOCLIP | DT_TOP, 0xffffffff);

        // End the scene.
		m_pDevice->EndScene();
		m_pDevice->Present(0, 0, 0, 0);
    }

	return S_OK;
}
示例#11
0
文件: app.cpp 项目: Alriightyman/RTS
HRESULT APPLICATION::Render()
{
    // Clear the viewport
    m_pDevice->Clear(0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0L );

    // Begin the scene 
    if(SUCCEEDED(m_pDevice->BeginScene()))
    {
		//Set camera
		D3DXMATRIX view, proj;
		D3DXMatrixLookAtLH(&view, &D3DXVECTOR3(0.0f, 10.0f, -50.0f), &D3DXVECTOR3(0.0f, 3.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
		D3DXMatrixOrthoLH(&proj, 10.0f, 9.0f, 0.1f, 1000.0f);
		m_pDevice->SetTransform(D3DTS_VIEW, &view);
		m_pDevice->SetTransform(D3DTS_PROJECTION, &proj);

		if(m_wireframe)m_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);	
		else m_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);

		m_farmer1.Render();
		m_farmer2.RenderProgressive();

		//Number of polygons
		char number[50];
		std::string text = itoa(m_farmer2.GetNumProgressiveFaces(), number, 10);
		text += " polygons (UP/DOWN Arrow)";
		
		RECT r[] = {{170, 520, 0, 0}, {530, 520, 0, 0}, {470, 540, 0, 0}, {130, 540, 0, 0}};
		m_pFont->DrawText(NULL, "Original", -1, &r[0], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
		m_pFont->DrawText(NULL, "Progressive Mesh", -1, &r[1], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
		m_pFont->DrawText(NULL, text.c_str(), -1, &r[2], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
		m_pFont->DrawText(NULL, "(W)ireframe On/Off", -1, &r[3], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);		

        // End the scene.
		m_pDevice->EndScene();
		m_pDevice->Present(0, 0, 0, 0);
    }

	return S_OK;
}
示例#12
0
fResult f2dGraphics2DImpl::Begin()
{
	fResult tRet = f2dGraphicsImpl::Begin();
	if(tRet != FCYERR_OK)
		return tRet;

	if(!(m_pIB && m_pVB))
		return FCYERR_INTERNALERR;

	m_pParent->SubmitVD(NULL);
	setColorBlendType(m_ColorBlendType);

	IDirect3DDevice9* pDev = (IDirect3DDevice9*)m_pParent->GetHandle();
	pDev->SetFVF(FVF);
	pDev->SetStreamSource(0, m_pVB, 0, sizeof(f2dGraphics2DVertex));
	pDev->SetIndices(m_pIB);

	if(FAILED(pDev->BeginScene()))
	{
		m_bInRender = false;
		return FCYERR_INTERNALERR;
	}
	else
	{
		// 准备缓冲区
		m_pVB->Lock(0, m_VBMaxCount*sizeof(f2dGraphics2DVertex), (void**)&m_pVBData, D3DLOCK_DISCARD);
		m_pIB->Lock(0, m_IBMaxCount*sizeof(fuShort), (void**)&m_pIBData, D3DLOCK_DISCARD);

		m_VBUsedCount = 0;
		m_IBUsedCount = 0;
		m_VBAlloced = 0;
		m_IBAlloced = 0;

		return FCYERR_OK;
	}
}
示例#13
0
文件: app.cpp 项目: Alriightyman/RTS
HRESULT APPLICATION::Render()
{
    // Clear the viewport
    m_pDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);

	//Set camera
	D3DXMATRIX view, proj, world, identity;
	D3DXMatrixLookAtLH(&view, &D3DXVECTOR3(15.0f, 30.0f, -40.0f), &D3DXVECTOR3(0.0f, 11.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
	D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI * 0.3f, 1.33333f, 0.01f, 1000.0f);
	D3DXMatrixIdentity(&identity);

	m_pDevice->SetTransform(D3DTS_WORLD, &identity);
	m_pDevice->SetTransform(D3DTS_VIEW, &view);
	m_pDevice->SetTransform(D3DTS_PROJECTION, &proj);

    // Begin the scene 
    if(SUCCEEDED(m_pDevice->BeginScene()))
	{
		if(m_buildPrc < 1.0f)
		{
			m_build1.Render(m_buildPrc);

			//Progressbar
			m_pDevice->Clear(1, &SetRect(10, 560, 790, 590), D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff000000, 1.0f, 0);
			m_pDevice->Clear(1, &SetRect(12, 562, 788, 588), D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
			m_pDevice->Clear(1, &SetRect(12, 562, (int)(12 + 774 * m_buildPrc), 588), D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff00ff00, 1.0f, 0);
		}
		else m_build2.Render();		

		// End the scene.
		m_pDevice->EndScene();
		m_pDevice->Present(0, 0, 0, 0);
    }

	return S_OK;
}
示例#14
0
 void GPUContextDX9::beginScene()
 {
   HRESULT result = _device->BeginScene();
   GPUAssert( !FAILED(result), "BeginScene failed" );
 }
int main(int argc, char *argv[])
{
	int ret = 0;
	try {
		// initialize directx 
		IDirect3D9 *d3d = Direct3DCreate9(D3D_SDK_VERSION);
		if (!d3d)
			throw std::string("This application requires DirectX 9");

		// create a window
		HWND hwnd = CreateWindowEx(0, "static", "GNU Rocket Example",
		    WS_POPUP | WS_VISIBLE, 0, 0, width, height, 0, 0,
		    GetModuleHandle(0), 0);

		// create the device
		IDirect3DDevice9 *device = NULL;
		static D3DPRESENT_PARAMETERS present_parameters = {width,
		    height, D3DFMT_X8R8G8B8, 3, D3DMULTISAMPLE_NONE, 0,
		    D3DSWAPEFFECT_DISCARD, 0, WINDOWED, 1, D3DFMT_D24S8,
		    0, WINDOWED ? 0 : D3DPRESENT_RATE_DEFAULT, 0
		};
		if (FAILED(d3d->CreateDevice(D3DADAPTER_DEFAULT,
		    D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING,
		    &present_parameters, &device)))
			throw std::string("Failed to create device");

		// init BASS
		int soundDevice = 1;
		if (!BASS_Init(soundDevice, 44100, 0, hwnd, 0))
			throw std::string("Failed to init bass");

		// load tune
		HSTREAM stream = BASS_StreamCreateFile(false, "tune.ogg", 0, 0,
		    BASS_MP3_SETPOS | (!soundDevice ? BASS_STREAM_DECODE : 0));
		if (!stream)
			throw std::string("Failed to open tune");

		// let's just assume 150 BPM (this holds true for the included tune)
		float bpm = 150.0f;

		// setup timer and construct sync-device
		BassTimer timer(stream, bpm, 8);
		std::auto_ptr<sync::Device> syncDevice = std::auto_ptr<sync::Device>(
		    sync::createDevice("sync", timer));
		if (!syncDevice.get())
			throw std::string("Failed to connect to host?");

		// get tracks
		sync::Track &clearRTrack = syncDevice->getTrack("clear.r");
		sync::Track &clearGTrack = syncDevice->getTrack("clear.g");
		sync::Track &clearBTrack = syncDevice->getTrack("clear.b");

		sync::Track &camRotTrack  = syncDevice->getTrack("cam.rot");
		sync::Track &camDistTrack = syncDevice->getTrack("cam.dist");

		LPD3DXMESH cubeMesh = NULL;
		if (FAILED(D3DXCreateBox(device, 1.0f, 1.0f, 1.0f, &cubeMesh, NULL)))
			return -1;

		// let's roll!
		BASS_Start();
		timer.play();

		bool done = false;
		while (!done) {
			float row = float(timer.getRow());
			if (!syncDevice->update(row))
				done = true;

			// setup clear color
			D3DXCOLOR clearColor(clearRTrack.getValue(row), clearGTrack.getValue(row), clearBTrack.getValue(row), 0.0);

			// paint the window
			device->BeginScene();
			device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, clearColor, 1.0f, 0);

/*			D3DXMATRIX world;
			device->SetTransform(D3DTS_WORLD, &world); */

			float rot = camRotTrack.getValue(row);
			float dist = camDistTrack.getValue(row);
			D3DXVECTOR3 eye(sin(rot) * dist, 0, cos(rot) * dist);
			D3DXVECTOR3 at(0, 0, 0);
			D3DXVECTOR3 up(0, 1, 0);
			D3DXMATRIX view;
			D3DXMatrixLookAtLH(&view, &(eye + at), &at, &up);
			device->SetTransform(D3DTS_WORLD, &view);

			D3DXMATRIX proj;
			D3DXMatrixPerspectiveFovLH(&proj, D3DXToRadian(60), 4.0f / 3, 0.1f, 1000.f);
			device->SetTransform(D3DTS_PROJECTION, &proj);

			cubeMesh->DrawSubset(0);

			device->EndScene();
			device->Present(0, 0, 0, 0);

			BASS_Update(0); // decrease the chance of missing vsync
			MSG msg;
			while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
				TranslateMessage(&msg);
				DispatchMessage(&msg);

				if (WM_QUIT == msg.message) done = true;
				if ((WM_KEYDOWN == msg.message) && (VK_ESCAPE == LOWORD(msg.wParam))) done = true;
			}
		}

		BASS_StreamFree(stream);
		BASS_Free();

		device->Release();
		d3d->Release();
		DestroyWindow(hwnd);
	} catch (const std::exception &e) {
#ifdef _CONSOLE
		fprintf(stderr, "*** error: %s\n", e.what());
#else
		MessageBox(NULL, e.what(), NULL, MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
#endif
		ret = -1;
	} catch (const std::string &str) {
#ifdef _CONSOLE
		fprintf(stderr, "*** error: %s\n", str.c_str());
#else
		MessageBox(NULL, e.what(), NULL, MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
#endif
		ret = -1;
	}

	return ret;
}
示例#16
0
文件: app.cpp 项目: Alriightyman/RTS
HRESULT APPLICATION::Render()
{
	//Setup the viewport
	D3DVIEWPORT9 v, v2;
	v.Width = 800;
	v.Height = 600;
	v.X = 0;
	v.Y = 0;
	v.MaxZ = 1.0f;
	v.MinZ = 0.0f;
	m_pDevice->SetViewport(&v);
	v2 = v;

    // Clear the viewport
    m_pDevice->Clear(0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff4444ff, 1.0f, 0L );

    // Begin the scene 
    if(SUCCEEDED(m_pDevice->BeginScene()))
    {
		//Render big city
		m_city.Render(&m_camera);

		RECT r[] = {{10, 10, 0, 0}, {10, 30, 0, 0}, {10, 50, 0, 0}};
		m_pFont->DrawText(NULL, "Mouse Wheel: Change Camera Radius", -1, &r[0], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
		m_pFont->DrawText(NULL, "Arrows: Change Camera Angle", -1, &r[1], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
		
		//Render city overview, set viewport
		v.X = 550;
		v.Y = 20;
		v.Width = 230;
		v.Height = 230;
		m_pDevice->SetViewport(&v);
		m_pDevice->Clear(0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0L );

		//Setup camera view to orthogonal view looking down on city
		D3DXMATRIX viewTop, projectionTop;
		D3DXMatrixLookAtLH(&viewTop, &(m_city.GetCenter() + D3DXVECTOR3(0.0f, 100.0f, 0.0f)), &m_city.GetCenter(), &D3DXVECTOR3(0,0,1));
		D3DXMatrixOrthoLH(&projectionTop, m_city.m_size.x * TILE_SIZE, m_city.m_size.y * TILE_SIZE, 0.1f, 1000.0f);
		m_pDevice->SetTransform(D3DTS_VIEW, &viewTop);
		m_pDevice->SetTransform(D3DTS_PROJECTION, &projectionTop);
		
		//Draw city blocks that are in view
		m_city.Render(NULL);

		//Restore viewport
		m_pDevice->SetViewport(&v2);

		//Draw line around smaller window
		D3DXVECTOR2 outline[] = {D3DXVECTOR2(550, 20), D3DXVECTOR2(779, 20), D3DXVECTOR2(779, 249), D3DXVECTOR2(550, 249), D3DXVECTOR2(550, 20)};
		m_pLine->SetWidth(3.0f);
		m_pLine->Begin();
		m_pLine->Draw(outline, 5, 0xff000000);
		m_pLine->End();

		//Draw mouse
		m_mouse.Paint();

        // End the scene.
		m_pDevice->EndScene();
		m_pDevice->Present(0, 0, 0, 0);
    }

	return S_OK;
}
示例#17
0
文件: app.cpp 项目: Alriightyman/RTS
HRESULT APPLICATION::Render()
{
    // Clear the viewport
    m_pDevice->Clear(0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0L );
	
    // Begin the scene 
    if(SUCCEEDED(m_pDevice->BeginScene()))
    {
		for(int i=0;i<(int)m_objects.size();i++)
			m_objects[i].Render();
		
		//Find intersecting object closest to the camera
		D3DXMATRIX identity;
		D3DXMatrixIdentity(&identity);

		int object = -1;
		float bestDist = 100000.0f;
		for(int i=0;i<(int)m_objects.size() - 1;i++)
		{
			float dist = -1.0f; 
			
			//Do intersection tests
			if(m_intersectType == 0)
			{
				m_pDevice->SetTransform(D3DTS_WORLD, &m_objects[i].m_meshInstance.GetWorldMatrix());
				RAY ray = m_mouse.GetRay();
				dist = ray.Intersect(m_objects[i].m_meshInstance);
			}
			else if(m_intersectType == 1)
			{				
				m_pDevice->SetTransform(D3DTS_WORLD, &identity);
				RAY ray = m_mouse.GetRay();
				dist = ray.Intersect(m_objects[i].m_BBox);
			}
			else if(m_intersectType == 2)
			{
				m_pDevice->SetTransform(D3DTS_WORLD, &identity);
				RAY ray = m_mouse.GetRay();
				dist = ray.Intersect(m_objects[i].m_BSphere);
			}

			m_objects[i].RenderBoundingVolume(m_intersectType);

			if(dist >= 0.0f && dist < bestDist)
			{
				object = i;
				bestDist = dist;
			}
		}

		//Write m_mouse text
		if(object != -1)
		{
			RECT mr[] = {{m_mouse.x + 2, m_mouse.y + 24, 0, 0}, {m_mouse.x, m_mouse.y + 22, 0, 0}};
			m_pFontMouse->DrawText(NULL, m_objects[object].m_name.c_str(), -1, &mr[0], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
			m_pFontMouse->DrawText(NULL, m_objects[object].m_name.c_str(), -1, &mr[1], DT_LEFT| DT_TOP | DT_NOCLIP, 0xffff0000);
		}

		RECT r[] = {{10, 10, 0, 0}, {10, 30, 0, 0}, {10, 50, 0, 0}};
		m_pFont->DrawText(NULL, "Mouse Wheel: Change Camera Radius", -1, &r[0], DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
		m_pFont->DrawText(NULL, "Arrows: Change Camera Angle", -1, &r[1], DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
		m_pFont->DrawText(NULL, "Space: Change Bounding Volume", -1, &r[2], DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);

		RECT rc = {500, 10, 0, 0};
		if(m_intersectType == 0)
			m_pFont->DrawText(NULL, "Mesh Intersection Test", -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
		else if(m_intersectType == 1)
			m_pFont->DrawText(NULL, "Box Intersection Test", -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
		else if(m_intersectType == 2)
			m_pFont->DrawText(NULL, "Sphere Intersection Test", -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
		
		//Draw m_mouse
		m_mouse.Paint();

        // End the scene.
		m_pDevice->EndScene();
		m_pDevice->Present(0, 0, 0, 0);
    }

	return S_OK;
}
示例#18
0
文件: Box.cpp 项目: dejw/bit-graphics
int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int){
	Application app("Okienko");

	// Inicjalizacja Direct3D
	IDirect3D9* d3d = Direct3DCreate9(D3D_SDK_VERSION);

	// Parametry urzadzenia
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = true;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferHeight = app.get_height();
	d3dpp.BackBufferWidth = app.get_width();
	d3dpp.EnableAutoDepthStencil = true;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;

	// Tworzenie urzadzenia
	IDirect3DDevice9* dev;
	d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, app.window_handle(),
		D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &dev);

	app.init_font(dev, "Courier New");

	dev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);	// Brak obcinania scian
    dev->SetRenderState(D3DRS_LIGHTING, false);			// Brak swiatla

	// Inicjalizacja kamery
//	D3DXVECTOR3 eye(0, 5, -10);
    D3DXVECTOR3 target(0, 0, 0);
    D3DXVECTOR3 up(0, 1, 0);
    D3DXMATRIXA16 view;

    D3DXMATRIX mProjection;
	D3DXMatrixPerspectiveFovLH(&mProjection, D3DX_PI * 0.5f,  app.get_width()/(float)app.get_height(), 1, 50);
    dev->SetTransform(D3DTS_PROJECTION, &mProjection);

	// Model pudelka
	Vertex box[] = {
	//	  X   Y   Z  Color
		{ 1,  1,  1, D3DCOLOR_XRGB(255, 255, 255)},
		{-1,  1,  1, D3DCOLOR_XRGB(0, 255, 255)},
		{-1,  1, -1, D3DCOLOR_XRGB(0, 255, 0)},
		{ 1,  1, -1, D3DCOLOR_XRGB(255, 255, 0)},

		{ 1, -1,  1, D3DCOLOR_XRGB(255, 0, 255)},
		{-1, -1,  1, D3DCOLOR_XRGB(0, 0, 255)},
		{-1, -1, -1, D3DCOLOR_XRGB(0, 0, 0)},
		{ 1, -1, -1, D3DCOLOR_XRGB(255, 0, 0)},

		box[0], box[4], box[1], box[5], box[2], box[6], box[3], box[7], box[0], box[4]
	};

	int box_size = (16 + 2) * sizeof(Vertex);

	// Tworzenie bufora wierzcholkow
	IDirect3DVertexBuffer9* box_buffer;
	dev->CreateVertexBuffer(box_size, 0, Vertex_Format, D3DPOOL_MANAGED, &box_buffer, NULL);

	VOID* pVoid;
	box_buffer->Lock(0, box_size, (void**)&pVoid, 0);
	memcpy(pVoid, box, box_size);
	box_buffer->Unlock();

	dev->SetFVF(Vertex_Format);
	dev->SetStreamSource(0, box_buffer, 0, sizeof(Vertex));

	float radius = 3;
	while(app.running()){
		float alfa = app.get_alfa();
		float beta = app.get_beta();
		// Aktualizujemy kamere
		D3DXVECTOR3 eye(
			radius * cos(alfa) * sin(beta),
			radius * cos(beta),
			radius * sin(alfa) * sin(beta)
		);

		D3DXMatrixLookAtLH(&view, &eye, &target, &up);
		dev->SetTransform(D3DTS_VIEW, &view);

		// Rysujemy pudeleczko
		dev->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
		dev->BeginScene();

		for(int i = 0; i < 2; i++) dev->DrawPrimitive(D3DPT_TRIANGLEFAN, i*4, 2);
		dev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 8, 8);

		app.print(10, 10, "Mysz X = %d", app.get_mouse_x());
		app.print(10, 24, "Mysz Y = %d", app.get_mouse_y());

		dev->EndScene();
		dev->Present(NULL, NULL, NULL, NULL);
	}

	// Zwalniamy zasoby
	box_buffer->Release();
	dev->Release();
	d3d->Release();
	return 0;
}