4
\$\begingroup\$

I've been looking everywhere and all I can find are tutorials on writing the shaders. None of them showed me how to incorporate them into my scene.

So essentially:

Given an hlsl shader, if I were to have a function called drawTexturedQuad() and I wanted the shader to be applied to the result, how exactly could I do this?

Thanks

\$\endgroup\$
2
  • \$\begingroup\$ OpenGL dev here, but your method sound more like a FFP function that can't be combined with a shader. You have to draw the quad with texture yourself in the shader. \$\endgroup\$
    – JustSid
    Commented Jun 10, 2011 at 6:49
  • \$\begingroup\$ That's a funny question actually. And people making tutorials should be avare of this :). What version of DX are you using? \$\endgroup\$
    – Notabene
    Commented Jun 10, 2011 at 7:01

3 Answers 3

9
\$\begingroup\$

There are three steps:

  1. Load effect and set the technique
  2. Provide data to the effect
  3. Render

1) Load effect and set the technique

  // Declaration of your effect variable
  LPD3DXEFFECT  mDSEGeometryStage; 
  ...
  initEffects()
  {
  //With this method you load your effect file
  HR(**D3DXCreateEffectFromFile**( d3ddev, "./DeferredEffect_MaterialsStage.fx", 0, 0, D3DXSHADER_DEBUG, 0, &mDSEGeometryStage, &errors);) 
        // Some Error checking   
    if( errors )
    {
        MessageBoxA(0, (char *)errors->GetBufferPointer(), 0, 0);
        errors->Release();
    }       
    // Retrieve a Technique from your effects handler
    D3DXHANDLE mhTech = mDSEGeometryStage->**GetTechniqueByName**("MaterialsTech");
            // Set the technique to be used 
    HR(mDSEGeometryStage->SetTechnique(mhTech));
  }

2) Provide data to the effect:

renderGeometryStage()
{
    // Effect values handlers (String parameter is the name of a variable in your .fx file)
D3DXHANDLE mhView       = mDSEGeometryStage->GetParameterByName(0, "gView");            
D3DXHANDLE mhProjection = mDSEGeometryStage->GetParameterByName(0, "gProjection");      
D3DXHANDLE mhNearClip = mDSEGeometryStage->GetParameterByName(0, "gNearClip");
D3DXHANDLE mhFarClip = mDSEGeometryStage->GetParameterByName(0, "gFarClip");

    // Set effect values indicating the handler and the data as paramters
HR(mDSEGeometryStage->SetMatrix(mhProjection, &(camera->getProjectionMatrix())));
HR(mDSEGeometryStage->SetMatrix(mhView, &(camera->getViewMatrix())));       
HR(mDSEGeometryStage->SetFloat(mhFarClip, camera->getFarClip()));

    ...

3) Render: As said in a previous answer

ID3DXEffect provides Begin() and BeginPass() methods.

So, in the same rendering method you can start to render by calling the device BeginScene() method and inside call the Begin or BeginPass methods of your effect.

    ...

// Begin Scene  
HR(d3ddev->BeginScene());
{
                ...     

    // Clear targets surface
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET |D3DCLEAR_ZBUFFER, 
        D3DCOLOR_ARGB(0, 0,0,0), 1.0f, 0);    

    // Begin Effect passes.
    UINT numPasses = 0;
    HR(mDSEGeometryStage->Begin(&numPasses, 0))
    {
       // PLACE HERE YOUR RENDERING STUFF 
       // like:  drawTexturedQuad();    
    }
    // End Effect passes
    HR(mDSEGeometryStage->End());
}   
// End scene rendering
HR(d3ddev->EndScene());    
}// End of renderGeometryStage method

Hope it helps, but your question is not about a chunk of code that you can copy/paste, its more about "basic" concepts. So for this kind of things I really recommend you to follow some tutorial because you will understand all the underlying concepts better. Cheers!

\$\endgroup\$
1
  • \$\begingroup\$ Gotta upvote this- nice and comprehensive. \$\endgroup\$
    – DeadMG
    Commented Jun 10, 2011 at 14:22
1
\$\begingroup\$

ID3DXEffect provides Begin() and BeginPass() methods. All you have to do is call DrawQuad() appropriately between them and their equivalent End() and EndPass() methods.

\$\endgroup\$
-2
\$\begingroup\$

Depends if you want to use OpenGL or Direct3D.

For D3D the SDK has all the gory details.

\$\endgroup\$
3
  • 4
    \$\begingroup\$ Hi welcome to Gamedev. Please read questions better. HLSL means directx, he also specified directx in tags. \$\endgroup\$
    – Notabene
    Commented Jun 10, 2011 at 7:50
  • \$\begingroup\$ AFAIK, you cannot use HLSL with OpenGL. It is DirectX only. \$\endgroup\$ Commented Jun 10, 2011 at 20:46
  • \$\begingroup\$ Fair enough. But reading the DX SDK would give him the answer. \$\endgroup\$ Commented Jun 11, 2011 at 7:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .