shaders/shader_template/orshader_template_shader.cxx

shaders/shader_template/orshader_template_shader.cxx
/***************************************************************************************
Autodesk(R) Open Reality(R) Samples
(C) 2009 Autodesk, Inc. and/or its licensors
All rights reserved.
AUTODESK SOFTWARE LICENSE AGREEMENT
Autodesk, Inc. licenses this Software to you only upon the condition that
you accept all of the terms contained in the Software License Agreement ("Agreement")
that is embedded in or that is delivered with this Software. By selecting
the "I ACCEPT" button at the end of the Agreement or by copying, installing,
uploading, accessing or using all or any portion of the Software you agree
to enter into the Agreement. A contract is then formed between Autodesk and
either you personally, if you acquire the Software for yourself, or the company
or other legal entity for which you are acquiring the software.
AUTODESK, INC., MAKES NO WARRANTY, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
PURPOSE REGARDING THESE MATERIALS, AND MAKES SUCH MATERIALS AVAILABLE SOLELY ON AN
"AS-IS" BASIS.
IN NO EVENT SHALL AUTODESK, INC., BE LIABLE TO ANYONE FOR SPECIAL, COLLATERAL,
INCIDENTAL, OR CONSEQUENTIAL DAMAGES IN CONNECTION WITH OR ARISING OUT OF PURCHASE
OR USE OF THESE MATERIALS. THE SOLE AND EXCLUSIVE LIABILITY TO AUTODESK, INC.,
REGARDLESS OF THE FORM OF ACTION, SHALL NOT EXCEED THE PURCHASE PRICE OF THE
MATERIALS DESCRIBED HEREIN.
Autodesk, Inc., reserves the right to revise and improve its products as it sees fit.
Autodesk and Open Reality are registered trademarks or trademarks of Autodesk, Inc.,
in the U.S.A. and/or other countries. All other brand names, product names, or
trademarks belong to their respective holders.
GOVERNMENT USE
Use, duplication, or disclosure by the U.S. Government is subject to restrictions as
set forth in FAR 12.212 (Commercial Computer Software-Restricted Rights) and
DFAR 227.7202 (Rights in Technical Data and Computer Software), as applicable.
Manufacturer is Autodesk, Inc., 10 Duke Street, Montreal, Quebec, Canada, H3C 2L7.
***************************************************************************************/
#ifndef ORSDK_DLL
#define ORSDK_DLL K_DLLEXPORT
#endif
// Class declaration
#include "orshader_template_shader.h"
#include <math.h>
//--- Registration defines
#define ORSHADER_TEMPLATE__CLASS ORSHADER_TEMPLATE__CLASSNAME
#define ORSHADER_TEMPLATE__DESC "OR - Shader Template Description"
//--- FiLMBOX Registration & Implementation.
FBShaderImplementation( ORSHADER_TEMPLATE__CLASS );
FBRegisterShader ( ORSHADER_TEMPLATE__DESCSTR,
ORSHADER_TEMPLATE__CLASS,
ORSHADER_TEMPLATE__DESCSTR,
ORSHADER_TEMPLATE__DESC,
FB_DEFAULT_SDK_ICON ); // Icon filename (default=Open Reality icon)
/************************************************
* Specific Constructor. Construct custom shader from an FBMaterial object.
************************************************/
ORShader_Template::ORShader_Template(FBMaterial *pMaterial):FBShader(pMaterial->Name)
{
mMaterial = pMaterial;
}
/************************************************
* FiLMBOX Constructor.
************************************************/
bool ORShader_Template::FBCreate()
{
mRenderFrameId = 0;
return true;
}
/************************************************
* FiLMBOX Destructor.
************************************************/
void ORShader_Template::FBDestroy()
{
}
/************************************************
* Shader functions.
************************************************/
bool ORShader_Template::ShaderNeedBeginRender()
{
return true;
}
void ORShader_Template::ShaderBeginRender( FBRenderOptions* pRenderOptions, FBShaderModelInfo* pShaderModelInfo )
{
int lRenderFrameId = pRenderOptions->GetRenderFrameId();
if( mRenderFrameId == 0 || mRenderFrameId != lRenderFrameId ) {
mRenderFrameId = lRenderFrameId;
// Do setup for the current rendering pass.
}
}
void ORShader_Template::ShadeModel( FBRenderOptions* pRenderOptions, FBShaderModelInfo* pShaderModelInfo, FBRenderingPass pPass )
{
/***************************************************************************************
* Make sure everything in OpenGL is the same on exit as when it entered this function! *
***************************************************************************************/
if( pShaderModelInfo != NULL )
{
float lTemp[4];
FBVertex lVertex;
glEnable( GL_LIGHTING);
lTemp[0] = 0.6f;
lTemp[1] = 0.9f;
lTemp[2] = 0.67f;
lTemp[3] = 1.0f;
glMaterialfv( GL_FRONT, GL_AMBIENT, lTemp );
FBModelVertexData* lModelVertexData = pShaderModelInfo->GetFBModel()->ModelVertexData;
if (lModelVertexData)
{
//Get number of region mapped by different materials.
const int lSubRegionCount = lModelVertexData->GetSubRegionCount();
if (lSubRegionCount)
{
//Set up vertex buffer object (VBO) or vertex array
lModelVertexData->EnableOGLVertexData();
for (int lSubRegionIndex = 0; lSubRegionIndex < lSubRegionCount; lSubRegionIndex++)
{
// Setup material, texture, shader, parameters here.
/*
FBMaterial* lMaterial = lModelVertexData->GetSubRegionMaterial(lSubRegionIndex);
*/
lModelVertexData->DrawSubRegion(lSubRegionIndex);
//Cleanup material, texture, shader, parameters here
}
lModelVertexData->DisableOGLVertexData();
}
}
glDisable( GL_LIGHTING);
// MB could use GPU for skinning deformation, and thus all deformation result reside in GPU only.
bool lDisplayNormal = pShaderModelInfo->GetFBModel()->IsDeformable == false;
if( lDisplayNormal )
{
// All the nurbs/patch surface are tessellated
FBMesh* lTessellatedMesh = pShaderModelInfo->GetFBModel()->TessellatedMesh;
if (lTessellatedMesh->NormalMappingMode == kFBGeometryMapping_BY_CONTROL_POINT)
{
int lPosCount = lTessellatedMesh->VertexCount();
float* lPositionArray = (float*)lTessellatedMesh->GetVertexes();
int lNormalCount = 0;
float* lNormalArray = (float*)lTessellatedMesh->GetNormalsDirectArray(lNormalCount);
assert(lNormalCount == lPosCount);
const float lNormalLentgh = 1.02f;
float lx, ly, lz, lLen;
glDisable( GL_LIGHTING );
glColor3ub( 0, 255, 0 );
glBegin( GL_LINES );
while( lPosCount )
{
glVertex3fv( lPositionArray );
lx = lNormalArray[0];
ly = lNormalArray[1];
lz = lNormalArray[2];
lLen = sqrt( lx*lx + ly*ly + lz*lz ) /lNormalLentgh;
lx /= lLen;
ly /= lLen;
lz /= lLen;
glVertex3f( lPositionArray[0] + ( lx ), lPositionArray[1] + ( ly ), lPositionArray[2] + ( lz ) );
lPositionArray +=4;
lNormalArray +=4;
lPosCount --;
}
glEnd();
}
}// Display Normal
}
}