Beispiel #1
0
void Combined()
{
    // Begin rendering the Sobel Filter along the X-axis
    gRenderTarget.begin(&gBlurTexture2);
    gRenderTarget.clear(D3DCOLOR_XRGB(255, 255, 255));

    // Select unfiltered render target to the left side of the screen
    gDiffTex.select();
    g3D->renderSAQ(0, 0, 256, 256, "GuassianBlurVert");

    gRenderTarget.end(); // Finish rendering to the shadow map

    // Begin rendering the Sobel Filter along the Y-axis
    gRenderTarget.begin(&gBlurTexture1);

    gRenderTarget.clear(D3DCOLOR_XRGB(255, 255, 255));

    // Select unfiltered render target to the left side of the screen
    gBlurTexture2.select();
    g3D->renderSAQ(0, 0, 256, 256, "GuassianBlurHorz");

    gRenderTarget.end(); // Finish rendering to the shadow map

    ////// Sobel

    // Begin rendering the Sobel Filter along the X-axis
    gRenderTarget.begin(&gBlurTexture2);
    gRenderTarget.clear(D3DCOLOR_XRGB(255, 255, 255));

    // Select unfiltered render target to the left side of the screen
    gBlurTexture1.select();
    g3D->renderSAQ(0, 0, 256, 256, "SobelFilter");

    gRenderTarget.end(); // Finish rendering to the shadow map
}
void CTextureBundle::createResource()
{
	// reload all objects
	TResourceMap::iterator it;
	for( it = mResourceMap.begin(); it != mResourceMap.end(); ++it ) {
		CD3DTexture& res = *it->second;
		assert( res.isNull() );
		CD3DTexture* n = tryLoadResourceById( it->first );
		assert( n );
		res.setObject( n->getObject() );
		delete n;
		assert( !res.isNull() );
	}
}
Beispiel #3
0
void RenderScene()
{
    g3D->begin(); // Begin the scene
    g3D->clear(D3DCOLOR_XRGB(0,0,0)); // Clear the frame buffer color, z-buffer, and stencil buffer

    // Select unfiltered render target to the left side of the screen
    gDiffTex.select();
    g3D->renderSAQ(0,0,256,256);

    gBlurTexture2.select();
    g3D->renderSAQ((float)kWinWid-256,0,256,256);

    g3D->end();
}
Beispiel #4
0
void RenderScene()
{
	g3D->begin(); // Begin the scene
	g3D->clear(D3DCOLOR_XRGB(0,0,25)); // Clear the frame buffer color, z-buffer, and stencil buffer
			
	D3DXMATRIX wMat; // world matrix for each object we want to render
	
	// Select the shadow map as the active texture
	gShadowMap.select();
	
	// Translate and draw the ground
	D3DXMatrixTranslation(&wMat, kGroundPos.x, kGroundPos.y, kGroundPos.z);
	g3D->setWorldMatrix(&wMat);
	g3D->render("PerPixelLightingWithShadowMap", gGround);
			
	// Translate and draw the box
	D3DXMatrixTranslation(&wMat, kBoxPos.x, kBoxPos.y, kBoxPos.z);
	g3D->setWorldMatrix(&wMat);
	g3D->render("PerPixelLightingWithShadowMap", gBox);
	
	// Translate and draw the sphere		
	D3DXMatrixTranslation(&wMat, kSherePos.x, kSherePos.y, kSherePos.z);
	g3D->setWorldMatrix(&wMat);
	g3D->render("PerPixelLightingWithShadowMap", gSphere);
	
	// Draw the tiny sphere representing the light
	D3DXMatrixIdentity(&wMat);
	D3DXMatrixTranslation(&wMat, gLightPos.x, gLightPos.y, gLightPos.z);
	g3D->setWorldMatrix(&wMat);
	g3D->render(gLight);
	
	g3D->end();
}
Beispiel #5
0
void CRendererSoftware::RenderImpl(CD3DTexture& target, CRect& sourceRect, CPoint(&destPoints)[4], uint32_t flags)
{
  // if creation failed
  if (!m_outputShader)
    return;

  CRenderBuffer* buf = m_renderBuffers[m_iBufferIndex];

  // 1. convert yuv to rgb
  m_sw_scale_ctx = sws_getCachedContext(m_sw_scale_ctx,
    buf->GetWidth(), buf->GetHeight(), buf->av_format,
    buf->GetWidth(), buf->GetHeight(), AV_PIX_FMT_BGRA,
    SWS_FAST_BILINEAR, nullptr, nullptr, nullptr);

  if (!m_sw_scale_ctx)
    return;

  sws_setColorspaceDetails(m_sw_scale_ctx,
    sws_getCoefficients(buf->color_space), buf->full_range,
    sws_getCoefficients(AVCOL_SPC_BT709),  buf->full_range,
    0, 1 << 16, 1 << 16);

  uint8_t* src[YuvImage::MAX_PLANES];
  int srcStride[YuvImage::MAX_PLANES];
  buf->GetDataPlanes(src, srcStride);

  D3D11_MAPPED_SUBRESOURCE mapping;
  if (target.LockRect(0, &mapping, D3D11_MAP_WRITE_DISCARD))
  {
    uint8_t *dst[] = { static_cast<uint8_t*>(mapping.pData), nullptr, nullptr };
    int dstStride[] = { static_cast<int>(mapping.RowPitch), 0, 0 };

    sws_scale(m_sw_scale_ctx, src, srcStride, 0, std::min(target.GetHeight(), buf->GetHeight()), dst, dstStride);

    if (!target.UnlockRect(0))
      CLog::LogF(LOGERROR, "failed to unlock swtarget texture.");
  }
  else
    CLog::LogF(LOGERROR, "failed to lock swtarget texture into memory.");

  // rotate initial rect
  ReorderDrawPoints(CRect(destPoints[0], destPoints[2]), destPoints);
}
Beispiel #6
0
bool CD3DEffect::SetTexture(LPCSTR handle, CD3DTexture &texture)
{
  if (m_effect)
  {
    ID3DX11EffectShaderResourceVariable* var = m_effect->GetVariableByName(handle)->AsShaderResource();
    if (var->IsValid())
      return SUCCEEDED(var->SetResource(texture.GetShaderResource()));
  }
  return false;
}
Beispiel #7
0
void RenderSobelFilter()
{
    // Begin rendering the Sobel Filter along the X-axis
    gRenderTarget.begin(&gBlurTexture2);
    gRenderTarget.clear(D3DCOLOR_XRGB(255, 255, 255));

    // Select unfiltered render target to the left side of the screen
    gDiffTex.select();
    g3D->renderSAQ(0, 0, 256, 256, "SobelFilter");

    gRenderTarget.end(); // Finish rendering to the shadow map
}
Beispiel #8
0
CBaseTexture* CGUIFontTTFDX::ReallocTexture(unsigned int& newHeight)
{
  assert(newHeight != 0);
  assert(m_textureWidth != 0);
  if(m_textureHeight == 0)
  {
    delete m_texture;
    m_texture = NULL;
    delete m_speedupTexture;
    m_speedupTexture = NULL;
  }
  m_staticCache.Flush();
  m_dynamicCache.Flush();

  CDXTexture* pNewTexture = new CDXTexture(m_textureWidth, newHeight, XB_FMT_A8);
  CD3DTexture* newSpeedupTexture = new CD3DTexture();
  if (!newSpeedupTexture->Create(m_textureWidth, newHeight, 1, D3D11_USAGE_DEFAULT, DXGI_FORMAT_R8_UNORM))
  {
    SAFE_DELETE(newSpeedupTexture);
    SAFE_DELETE(pNewTexture);
    return NULL;
  }

  ID3D11DeviceContext* pContext = g_Windowing.GetImmediateContext();

  // There might be data to copy from the previous texture
  if (newSpeedupTexture && m_speedupTexture)
  {
    CD3D11_BOX rect(0, 0, 0, m_textureWidth, m_textureHeight, 1);
    pContext->CopySubresourceRegion(newSpeedupTexture->Get(), 0, 0, 0, 0, m_speedupTexture->Get(), 0, &rect);
  }

  SAFE_DELETE(m_texture);
  SAFE_DELETE(m_speedupTexture);
  m_textureHeight = newHeight;
  m_textureScaleY = 1.0f / m_textureHeight;
  m_speedupTexture = newSpeedupTexture;

  return pNewTexture;
}
Beispiel #9
0
bool CD3DEffect::SetTexture(D3DXHANDLE handle, CD3DTexture &texture)
{
/*
	参数:
		1、

	返回:
		1、

	说明:
		1、
*/
	if (m_effect)
		return (D3D_OK == m_effect->SetTexture(handle, texture.Get()));
	return false;
}
Beispiel #10
0
CBaseTexture* CGUIFontTTFDX::ReallocTexture(unsigned int& newHeight)
{
  assert(newHeight != 0);
  assert(m_textureWidth != 0);
  if(m_textureHeight == 0)
  {
    delete m_texture;
    m_texture = NULL;
    delete m_speedupTexture;
    m_speedupTexture = NULL;
  }
  m_staticCache.Flush();
  m_dynamicCache.Flush();

  CDXTexture* pNewTexture = new CDXTexture(m_textureWidth, newHeight, XB_FMT_A8);
  pNewTexture->CreateTextureObject();
  LPDIRECT3DTEXTURE9 newTexture = pNewTexture->GetTextureObject();

  if (newTexture == NULL)
  {
    CLog::Log(LOGERROR, __FUNCTION__" - failed to create the new texture h=%d w=%d", m_textureWidth, newHeight);
    SAFE_DELETE(pNewTexture);
    return NULL;
  }

  // Use a speedup texture in system memory when main texture in default pool+dynamic
  // Otherwise the texture would have to be copied from vid mem to sys mem, which is too slow for subs while playing video.
  CD3DTexture* newSpeedupTexture = NULL;
  if (g_Windowing.DefaultD3DPool() == D3DPOOL_DEFAULT && g_Windowing.DefaultD3DUsage() == D3DUSAGE_DYNAMIC)
  {
    newSpeedupTexture = new CD3DTexture();

    if (!newSpeedupTexture->Create(m_textureWidth, newHeight, 1, 0, D3DFMT_A8, D3DPOOL_SYSTEMMEM))
    {
      SAFE_DELETE(newSpeedupTexture);
      SAFE_DELETE(pNewTexture);
      return NULL;
    }
  }

  LPDIRECT3DSURFACE9 pSource, pTarget;
  // There might be data to copy from the previous texture
  if ((newSpeedupTexture && m_speedupTexture) || (newTexture && m_texture))
  {
    if (m_speedupTexture && newSpeedupTexture)
    {
      m_speedupTexture->GetSurfaceLevel(0, &pSource);
      newSpeedupTexture->GetSurfaceLevel(0, &pTarget);
    }
    else
    {
      ((CDXTexture *)m_texture)->GetTextureObject()->GetSurfaceLevel(0, &pSource);
      newTexture->GetSurfaceLevel(0, &pTarget);
    }

    D3DLOCKED_RECT srclr, dstlr;
    if(FAILED(pSource->LockRect( &srclr, NULL, 0 ))
    || FAILED(pTarget->LockRect( &dstlr, NULL, 0 )))
    {
      CLog::Log(LOGERROR, __FUNCTION__" - failed to lock surfaces");
      SAFE_DELETE(newSpeedupTexture);
      SAFE_DELETE(pNewTexture);
      pSource->Release();
      pTarget->Release();
      return NULL;
    }

    unsigned char *dst = (unsigned char *)dstlr.pBits;
    unsigned char *src = (unsigned char *)srclr.pBits;
    unsigned int dstPitch = dstlr.Pitch;
    unsigned int srcPitch = srclr.Pitch;
    unsigned int minPitch = std::min(srcPitch, dstPitch);

    if (srcPitch == dstPitch)
    {
      memcpy(dst, src, srcPitch * m_textureHeight);
    }
    else
    {
      for (unsigned int y = 0; y < m_textureHeight; y++)
      {
        memcpy(dst, src, minPitch);
        src += srcPitch;
        dst += dstPitch;
      }
    }
    pSource->UnlockRect();
    pTarget->UnlockRect();

    pSource->Release();
    pTarget->Release();
  }

  // Upload from speedup texture to main texture
  if (newSpeedupTexture && m_speedupTexture)
  {
    LPDIRECT3DSURFACE9 pSource, pTarget;
    newSpeedupTexture->GetSurfaceLevel(0, &pSource);
    newTexture->GetSurfaceLevel(0, &pTarget);
    const RECT rect = { 0, 0, m_textureWidth, m_textureHeight };
    const POINT point = { 0, 0 };

    HRESULT hr = g_Windowing.Get3DDevice()->UpdateSurface(pSource, &rect, pTarget, &point);
    SAFE_RELEASE(pSource);
    SAFE_RELEASE(pTarget);

    if (FAILED(hr))
    {
      CLog::Log(LOGERROR, __FUNCTION__": Failed to upload from sysmem to vidmem (0x%08X)", hr);
      SAFE_DELETE(newSpeedupTexture);
      SAFE_DELETE(pNewTexture);
      return NULL;
    }
  }

  SAFE_DELETE(m_texture);
  SAFE_DELETE(m_speedupTexture);
  m_textureHeight = newHeight;
  m_textureScaleY = 1.0f / m_textureHeight;
  m_speedupTexture = newSpeedupTexture;

  return pNewTexture;
}
Beispiel #11
0
void CWinRenderer::RenderHW(DWORD flags, CD3DTexture* target)
{
  CRenderBuffer& buf = m_renderBuffers[m_iYV12RenderBuffer];
  if ( buf.format != BUFFER_FMT_D3D11_BYPASS
    && buf.format != BUFFER_FMT_D3D11_NV12
    && buf.format != BUFFER_FMT_D3D11_P010
    && buf.format != BUFFER_FMT_D3D11_P016)
    return;

  if (!buf.loaded)
    return;

  int past = 0;
  int future = 0;

  CRenderBuffer* views[8];
  memset(views, 0, 8 * sizeof(CRenderBuffer*));
  views[2] = &buf;

  // set future frames
  while (future < 2)
  {
    bool found = false;
    for (int i = 0; i < m_NumYV12Buffers; i++)
    {
      if (m_renderBuffers[i].frameIdx == buf.frameIdx + (future*2 + 2))
      {
        // a future frame may not be loaded yet
        if (m_renderBuffers[i].loaded || m_renderBuffers[i].UploadBuffer())
        {
          views[1 - future++] = &m_renderBuffers[i];
          found = true;
          break;
        }
      }
    }
    if (!found)
      break;
  }

  // set past frames
  while (past < 4)
  {
    bool found = false;
    for (int i = 0; i < m_NumYV12Buffers; i++)
    {
      if (m_renderBuffers[i].frameIdx == buf.frameIdx - (past*2 + 2))
      {
        if (m_renderBuffers[i].loaded)
        {
          views[3 + past++] = &m_renderBuffers[i];
          found = true;
          break;
        }
      }
    }
    if (!found)
      break;
  }

  CRect destRect;
  switch (m_renderOrientation)
  {
  case 90:
    destRect = CRect(m_rotatedDestCoords[3], m_rotatedDestCoords[1]);
    break;
  case 180:
    destRect = m_destRect;
    break;
  case 270:
    destRect = CRect(m_rotatedDestCoords[1], m_rotatedDestCoords[3]);
    break;
  default:
    destRect = m_bUseHQScaler ? m_sourceRect : CServiceBroker::GetWinSystem()->GetGfxContext().StereoCorrection(m_destRect);
    break;
  }

  CRect src = m_sourceRect, dst = destRect;
  CRect targetRect = CRect(0.0f, 0.0f,
                       static_cast<float>(m_IntermediateTarget.GetWidth()),
                       static_cast<float>(m_IntermediateTarget.GetHeight()));

  if (target != DX::Windowing()->GetBackBuffer())
  {
    // rendering capture
    targetRect.x2 = static_cast<float>(target->GetWidth());
    targetRect.y2 = static_cast<float>(target->GetHeight());
  }
  CWIN32Util::CropSource(src, dst, targetRect, m_renderOrientation);

  m_processor->Render(src, dst, m_IntermediateTarget.Get(), views, flags, buf.frameIdx, m_renderOrientation,
                      m_videoSettings.m_Contrast, m_videoSettings.m_Brightness);

  if (!m_bUseHQScaler)
  {
    if ( CServiceBroker::GetWinSystem()->GetGfxContext().GetStereoMode() == RENDER_STEREO_MODE_SPLIT_HORIZONTAL
      || CServiceBroker::GetWinSystem()->GetGfxContext().GetStereoMode() == RENDER_STEREO_MODE_SPLIT_VERTICAL)
    {
      CD3DTexture *backBuffer = DX::Windowing()->GetBackBuffer();
      CD3D11_VIEWPORT bbSize(0.f, 0.f, static_cast<float>(backBuffer->GetWidth()), static_cast<float>(backBuffer->GetHeight()));
      DX::DeviceResources::Get()->GetD3DContext()->RSSetViewports(1, &bbSize);
    }

    // render frame
    m_outputShader->SetDisplayMetadata(buf.hasDisplayMetadata, buf.displayMetadata, buf.hasLightMetadata, buf.lightMetadata);
    m_outputShader->SetToneMapParam(m_videoSettings.m_ToneMapParam);
    m_outputShader->Render(m_IntermediateTarget, m_destWidth, m_destHeight, dst, dst, target);
    DX::Windowing()->RestoreViewPort();
  }
}
Beispiel #12
0
// WinProc
LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
	const float kMoveAmt = 0.05f; // Amount to move light by

	switch(message)
    {
		case WM_SYSKEYDOWN:

			// Toggle on ALT + ENTER				
			if(wparam == VK_RETURN && (lparam & (1 << 29)))
			{
				// Shut down are render texture and render target
				gShadowMap.deinit();
				gRenderTarget.deinit();
				
				g3D->toggleFullScreen(); // Toggle!
				
				// Reinitialize the render texture and target
				gShadowMap.createRenderTexture(512, 512, D3DFMT_R32F);
				gRenderTarget.init(512, 512, D3DFMT_R32F, D3DFMT_D24S8);
				
				g3D->setViewMatrix(kEyePos, CPos(0.0f, 0.0f, 0.0f)); // Reset the view of our scene
					return 0;
			}

			break; // Allow other system keys to be handled by DefWindowProc()
    
		case WM_KEYDOWN:
					
			switch(wparam)
			{
				case VK_LEFT: // Move the light along the -X axis
					gLightPos.x -= kMoveAmt;
						break;
						
				case VK_RIGHT: // Move the light along the +X axis
					gLightPos.x += kMoveAmt;
						break;
						
				case VK_UP: // Move the light along the +Z axis
					gLightPos.z += kMoveAmt;
						break;
						
				case VK_DOWN: // Move the light along the -Z axis
					gLightPos.z -= kMoveAmt;
						break;
					
				case VK_ESCAPE:
					SendMessage(hwnd, WM_CLOSE, 0, 0);
						break;
			}
							
			return 0;

		case WM_DESTROY:
		{
			PostQuitMessage(0);
				return 0;
		}
    }

    return DefWindowProc(hwnd, message, wparam, lparam);
}
Beispiel #13
0
// Our Win32 main
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow)
{
    HWND hwnd = NULL; // Handle to our window
	MSG msg = {0};  
    WNDCLASSEX wndclassex = {0};

	// Init fields we care about
	wndclassex.cbSize = sizeof(WNDCLASSEX); // Always set to size of WNDCLASSEX
    wndclassex.style = CS_HREDRAW | CS_VREDRAW;
    wndclassex.lpfnWndProc = WinProc;
    wndclassex.hInstance = hinstance;
	wndclassex.lpszClassName = kClassName;
	wndclassex.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW),
											IMAGE_CURSOR, 0, 0, LR_SHARED);
    
    RegisterClassEx(&wndclassex); // Register the WNDCLASSEX

	RECT rect = { 0, 0, kWinWid, kWinHgt }; // Desired window client rect
	
	DWORD winStyleEx = WS_EX_CLIENTEDGE;
	DWORD winStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME;

	// Adjust window rect so it gives us our desired client rect when we 
	// create the window
	AdjustWindowRectEx(&rect, winStyle, false, winStyleEx);

	// Create the window
    hwnd = CreateWindowEx(winStyleEx,
					      kClassName,
						  "www.GameTutorials.com -- Shadow Mapping (HLSL)",
						  winStyle,
						  CW_USEDEFAULT,
						  CW_USEDEFAULT,
						  rect.right - rect.left,
						  rect.bottom - rect.top,
						  NULL,
						  NULL,
						  hinstance,
						  NULL);
						  
	// Get the client rect and make sure our client is the size we want
	GetClientRect(hwnd, &rect);
	assert(rect.right == kWinWid && rect.bottom == kWinHgt);

	// Init our global 3D object
	if(g3D->init(hwnd) == false)
		return EXIT_FAILURE; // There's been an error, lets get out of this joint
	
	if(!CreateSphere(0.05f, D3DCOLOR_XRGB(255,255,0), &gLight))
		return false; // Couldn't create the sphere... Something very bad happened...
		
	if(!CreateSphere(0.75f, D3DCOLOR_XRGB(0,128,64), &gSphere))
		return false; // Couldn't create the sphere... Something very bad happened...
		
	if(!CreateBox(1.0f, 2.0f, 1.0f, D3DCOLOR_XRGB(0, 128, 64), &gBox))
		return false; // Couldn't create the box... This isn't good!
	
	if(!CreateBox(16.0f, 0.05f, 16.0f, D3DCOLOR_XRGB(225, 225, 255), &gGround))
		return false; // Couldn't create the ground... Time to bail
				
	if(!gShadowMap.createRenderTexture(512, 512, D3DFMT_R32F))
		return false; // Couldn't create a shadow map texture.  Your video card 
					 // probably doesn't support the D3DFMT_R32F format.  You will need a beefier card to
					// run this tutorial
					 
	if(!gRenderTarget.init(512, 512, D3DFMT_R32F, D3DFMT_D24S8))
		return false; // Couldn't create a shadow map texture.  Your video card doesn't support render
					 // targets or it probably doesn't support the D3DFMT_R32F format.  You will need
					// a beefier card to run this tutorial

	// Set up our projection matrix once because it will not change
	g3D->setProjMatrix(DEG2RAD(60), 1.0f, 2048.0f);
	
	// We set up our view matrix once because it will never change
	g3D->setViewMatrix(kEyePos, CPos(0.0f, 0.0f, 0.0f));
	
	D3DXMATRIXA16 projMat;
	
	// Create texture projection matrix
	D3DXMatrixPerspectiveFovLH(&projMat, DEG2RAD(60), 1.0f, 0.1f, 100.0f);
	
	// Set data in our shader that will never change
	g3D->mShader->SetFloatArray("gLightProjMat", &projMat._11, 16);	
	
	g3D->mShader->SetFloatArray("gEyePos", &kEyePos.x, 3); // Set the camera's eye position
	g3D->mShader->SetFloatArray("gDiffuse", &kDiffuse.r, 3); // Lights diffuse color
	g3D->mShader->SetFloatArray("gSpecular", &kSpecular.r, 3); // Lights specular color
	g3D->mShader->SetFloatArray("gAmbient", &kAmbient.r, 3); // Lights ambient color

	// Show and update the window
	ShowWindow(hwnd, ishow);
	UpdateWindow(hwnd);
	
    while(1) // While the app is alive
	{
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // If the OS has a message for us
		{
			if(msg.message == WM_QUIT)
				break;
			
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else if(LockFrameRate()) // Else, if it's time to draw
		{
			static int angle = 0; // Stores the amount rotated in degrees
			D3DXMATRIX tMat, rMat; // This will hold translation and rotation matrices
			D3DXMATRIX wMat; // This will hold the world matrix of objects we want rendered
			D3DXMATRIX shadowMat; // The matrix for projecting the shadow to a plane
			
			// The light is going to potentially move, so we need to
			// set it's position every frame 
			g3D->mShader->SetFloatArray("gLightPos", &gLightPos.x, 3);
			
			// Render the shadow map
			RenderShadowMap();
		
			// Render the ground, box and sphere with shadows
			RenderScene();
		}
		else
			Sleep(1); // Otherwise, give the OS some time to process other programs
	
	} // end of while(1)

	gLight->Release(); // Free up the light
	gSphere->Release(); // Free up the sphere
	gBox->Release(); // Free up the box
	gGround->Release(); // Free up the ground
	
	g3D->deinit(); // Free up the D3D object
	UnregisterClass(kClassName,hinstance); // Free up WNDCLASSEX
	    return EXIT_SUCCESS; // Application was a success
}
CGUIFont* CFontBundle::loadResourceById( const CResourceId& id, const CResourceId& fullName )
{
    // open file
    FILE* f = fopen( fullName.getUniqueName().c_str(), "rb" );
    if( !f ) {
        return NULL;
    }
    assert( f );

    // read magic
    char magic[4];
    fread( &magic, 1, 4, f );
    if( magic[0]!='D' || magic[1]!='F' || magic[2]!='N' || magic[3]!='T' ) {
        std::string msg = "file isn't valid font file! '" + id.getUniqueName() + "'";
        CConsole::CON_ERROR.write( msg );
        THROW_ERROR( msg );
    }
    // letter count
    int letterCount;
    fread( &letterCount, 1, 4, f );
    // first letter
    int firstLetter;
    fread( &firstLetter, 1, 4, f );
    // max width
    unsigned short maxWidth;
    fread( &maxWidth, 1, 2, f );
    // max height
    unsigned short maxHeight;
    fread( &maxHeight, 1, 2, f );

    // load texture - with same name
    CD3DTexture* texture = RGET_TEX(id);
    assert( texture );
    D3DSURFACE_DESC desc;
    texture->getObject()->GetLevelDesc( 0, &desc );
    float halftexX = 0.5f / desc.Width;
    float halftexY = 0.5f / desc.Height;

    // create font
    CGUIFont* font = new CGUIFont( letterCount, firstLetter, maxWidth, maxHeight, *texture );
    assert( font );

    // read letter infos
    for( int i = 0; i < letterCount; ++i ) {
        CGUIFont::SLetter& l = font->getLetterByNumber( i );
        fread( &l.u0, 1, 4, f );
        fread( &l.v0, 1, 4, f );
        fread( &l.u1, 1, 4, f );
        fread( &l.v1, 1, 4, f );
        l.u0 += halftexX;
        l.v0 += halftexY;
        l.u1 += halftexX;
        l.v1 += halftexY;
        unsigned short v;
        fread( &v, 1, 2, f );
        l.width = v;
        fread( &v, 1, 2, f );
        l.height = v;
    }

    // close file
    fclose( f );

    CONSOLE.write( "font loaded '" + id.getUniqueName() + "'" );

    return font;
}
Beispiel #15
0
// Our Win32 main
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow)
{
    HWND hwnd = NULL; // Handle to our window
    MSG msg = {0};
    WNDCLASSEX wndclassex = {0};

    // Init fields we care about
    wndclassex.cbSize = sizeof(WNDCLASSEX); // Always set to size of WNDCLASSEX
    wndclassex.style = CS_HREDRAW | CS_VREDRAW;
    wndclassex.lpfnWndProc = WinProc;
    wndclassex.hInstance = hinstance;
    wndclassex.lpszClassName = kClassName;
    wndclassex.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW),
                                            IMAGE_CURSOR, 0, 0, LR_SHARED);

    RegisterClassEx(&wndclassex); // Register the WNDCLASSEX

    RECT rect = { 0, 0, kWinWid, kWinHgt }; // Desired window client rect

    DWORD winStyleEx = WS_EX_CLIENTEDGE;
    DWORD winStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME;

    // Adjust window rect so it gives us our desired client rect when we
    // create the window
    AdjustWindowRectEx(&rect, winStyle, false, winStyleEx);

    // Create the window
    hwnd = CreateWindowEx(winStyleEx,
                          kClassName,
                          "www.GameTutorials.com -- Render Targets",
                          winStyle,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          rect.right - rect.left,
                          rect.bottom - rect.top,
                          NULL,
                          NULL,
                          hinstance,
                          NULL);

    // Get the client rect and make sure our client is the size we want
    GetClientRect(hwnd, &rect);
    assert(rect.right == kWinWid && rect.bottom == kWinHgt);



    // Init our global 3D object
    if(g3D->init(hwnd) == false)
        return EXIT_FAILURE; // There's been an error, lets get out of this joint

    if(!gBlurTexture1.createRenderTexture(256, 256, D3DFMT_A8R8G8B8))
        return false; // Couldn't create a off screen texture to render to

    if(!gBlurTexture2.createRenderTexture(256, 256, D3DFMT_A8R8G8B8))
        return false; // Couldn't create a off screen texture to render to

    if(!gDiffTex.load("texture.jpg"))
        return false;

    if(!gRenderTarget.init(256, 256, D3DFMT_A8R8G8B8, D3DFMT_D24S8))
        return false; // Couldn't create a render texture... Does your video card support this?

    Make3x3GuassianDistribution();

    // Set up our projection matrix once because it will not change
    g3D->setProjMatrix(DEG2RAD(60), 1.0f, 2048.0f);

    // We set up our view matrix once because it will never change
    g3D->setViewMatrix(kEyePos, CPos(0.0f, 0.0f, 0.0f));

    g3D->mShader->SetFloatArray("gEyePos", &kEyePos.x, 3); // Set the camera's eye position
    g3D->mShader->SetFloatArray("gBlurWeights", gBlurWeights, 9); // Gaussian blur weights

    // Show and update the window
    ShowWindow(hwnd, ishow);
    UpdateWindow(hwnd);

    while(1) // While the app is alive
    {
        if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // If the OS has a message for us
        {
            if(msg.message == WM_QUIT)
                break;

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else if(LockFrameRate()) // Else, if it's time to draw
        {
            // Render the off screen texture (created directly above) to another
            // off screen texture using a Gaussian blur
            if(gToggle)
                Combined();
            else
                RenderSobelFilter();

            // Render the two textures to the screen
            RenderScene();
        }
        else
            Sleep(1); // Otherwise, give the OS some time to process other programs

    } // end of while(1)

    g3D->deinit(); // Free up the D3D object
    UnregisterClass(kClassName,hinstance); // Free up WNDCLASSEX
    return EXIT_SUCCESS; // Application was a success
}
Beispiel #16
0
// Main window program
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow)
{
	HWND hwnd = NULL;
	MSG msg = {0};
	WNDCLASSEX wndclassex = {0};

	// Init fields we care about
	wndclassex.cbSize = sizeof(WNDCLASSEX); // Always set to size of WNDCLASSEX
	wndclassex.style = CS_HREDRAW | CS_VREDRAW;
	wndclassex.lpfnWndProc = WinProc;
	wndclassex.hInstance = hinstance;
	wndclassex.lpszClassName = kClassName;
	wndclassex.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW),
											IMAGE_CURSOR, 0, 0, LR_SHARED);

	RegisterClassEx(&wndclassex); // Register the WNDCLASSEX

	RECT rect = { 0, 0, kWinWid, kWinHgt }; // Desired window client rect

	DWORD winStyleEx = WS_EX_CLIENTEDGE;
	DWORD winStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME |
					 WS_CLIPCHILDREN | WS_CLIPSIBLINGS;

	// Adjust window rect so it gives us our desired client rect when we 
	// create the window
	AdjustWindowRectEx(&rect, winStyle, false, winStyleEx);
	
	// Create the window
    hwnd = CreateWindowEx(winStyleEx, // Window extended style
					      kClassName,
						  "www.GameTutorials.com -- D3D Vertex Buffers",
						  winStyle, // Window style
						  CW_USEDEFAULT,
						  CW_USEDEFAULT,
						  rect.right - rect.left,
						  rect.bottom - rect.top,
						  NULL,
						  NULL,
						  hinstance,
						  NULL);
						  
	// Get the client rect and make sure our client is the size we want
	GetClientRect(hwnd, &rect);
	assert(rect.right == kWinWid && rect.bottom == kWinHgt);

	// Init our global 3D object
	if(g3D->init(hwnd) == false)
		return EXIT_FAILURE; // There's been an error, lets get out of this joint
		
	if(gTexture.load("Texture.jpg") == false)
		return EXIT_FAILURE; // There's been an error, lets get out of this joint
		
	// Create and set the vertex buffer in our CD3DMesh object
	if(gQuad.setVertexData(gQuadVertexData, kMaxVerts) == false)
		return EXIT_FAILURE; // This is bad!  The tutorial is officially broken

	// Set the perspective matrix once because it will never change
	g3D->setProjMatrix(DEG2RAD(60), (float)rect.right / (float)rect.bottom, 1.0f, 8192.0f);
	
	// We set up our view matrix once because it will never change
	g3D->setViewMatrix(CPos(0.0f, 0.0f, -5.0f), CPos(0.0f, 0.0f, 0.0f));

	ShowWindow(hwnd, ishow);
	UpdateWindow(hwnd);

    while(1) // While the app is running
	{
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Handle messages from the OS
		{
			if(msg.message == WM_QUIT)
				break;
			
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else if(LockFrameRate()) // If it is time to draw, do so
		{
			g3D->begin(); // Begin the scene
			g3D->clear(); // Clear the Z-buffer
			
			gTexture.select(); // Select the texture to be used
			
			// Render the textured quad
			gQuad.render();
			
			g3D->end(); // End the scene
		}
		else
			Sleep(1); // Give the OS a slice of time to process other things
	
	} // end of while(1)

	g3D->deinit(); // Free up CD3DObj
	UnregisterClass(kClassName, hinstance); // Free up WNDCLASSEX
	    return EXIT_SUCCESS; // Application was a success
}