Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
    
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. –  JustSid Jun 10 '11 at 6:49
    
That's a funny question actually. And people making tutorials should be avare of this :). What version of DX are you using? –  Notabene Jun 10 '11 at 7:01

3 Answers 3

up vote 8 down vote accepted

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!

share|improve this answer
    
Gotta upvote this- nice and comprehensive. –  DeadMG Jun 10 '11 at 14:22

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

share|improve this answer

Depends if you want to use OpenGL or Direct3D.

For D3D the SDK has all the gory details.

share|improve this answer
4  
Hi welcome to Gamedev. Please read questions better. HLSL means directx, he also specified directx in tags. –  Notabene Jun 10 '11 at 7:50
    
AFAIK, you cannot use HLSL with OpenGL. It is DirectX only. –  The Communist Duck Jun 10 '11 at 20:46
    
Fair enough. But reading the DX SDK would give him the answer. –  PowerApp101 Jun 11 '11 at 7:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.