Jump to content
  • Advertisement
Sign in to follow this  
brekehan

HLSL - Get Viewport

This topic is 3411 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a way to get the viewport dimensions in HLSL? I know there is a way to set states for things like depth, alpha, etc. And that doesn't directly show up in the reference docs, so I am wondering if I can get the viewport values too? If I can, I could move all the these calculations that happen in my render method to the shader and add techniques for rendering 2D UI style, or billboarded. (which is my hope)
//-------------------------------------------------------------------
void Image2D::Render()
{
   // Get the original matrices
   D3DXMATRIX origProjection;
   D3DXMATRIX origView;

   m_effectManager.GetViewMatrix(origView);
   m_effectManager.GetProjectionMatrix(origProjection);

   // Create new matrices for rendering 2D
   D3DXMATRIX     newProjection;	
	D3DXMATRIX     newView;
   D3DXMATRIX     world;
   UINT           numViewports = 1;
   D3D10_VIEWPORT viewport;

   m_device.RSGetViewports(&numViewports, &viewport);

   D3DXMatrixOrthoLH(&newProjection, static_cast<float>(viewport.Width), static_cast<float>(viewport.Height), 0.0f, 1.0f);
   D3DXMatrixIdentity(&newView);

   m_effectManager.SetProjectionMatrix(newProjection);
   m_effectManager.SetViewMatrix(newView);

   // Set the world matrix and material
   Effect *    effect    = NULL;
   Technique * technique = NULL;
   Pass *      pass      = NULL;

   try
   {
      effect    = &(m_effectManager.GetChildEffect("image"));
      technique = &(effect->GetTechnique("RenderWithAlpha"));
      pass      = &(technique->GetPass(0));

      effect->SetWorldMatrix(m_world);
      effect->SetMaterial(*m_material);
   }
   catch(BaseException & e)
   {
      throw e;
   }

   // Bind the input layout
   m_device.IASetInputLayout(m_inputLayout);
   m_device.IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);

   // Bind the vertex buffers
   std::vector<ID3D10Buffer *> vertexBuffers;
   vertexBuffers.push_back( m_quadBuffers[0]->GetD3DBuffer() );
   vertexBuffers.push_back( m_quadBuffers[1]->GetD3DBuffer() );

   std::vector<unsigned> offsets;
   offsets.push_back(0);
   offsets.push_back(0);

   m_device.IASetVertexBuffers(0,
                               static_cast<UINT>(vertexBuffers.size()),
                               &vertexBuffers[0], 
                               &m_strides[0], 
                               &offsets[0]);

   // Calculate the frame to be rendered
   CalculateAnimationFrame();
   unsigned startVertex = (m_currentFrameIndex) * 4;

   // Apply the pass
   pass->Apply();

   // Draw
   m_device.Draw(4, startVertex);

   // Restore the original matrices
   m_effectManager.SetProjectionMatrix(origProjection);
   m_effectManager.SetViewMatrix(origView);
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!