#include <stdio.h>
#include "OpenGLViewportRenderer.h"
#include <maya/MGlobal.h>
#include <maya/MString.h>
#include <maya/MRenderingInfo.h>
#include <maya/MFnCamera.h>
#include <maya/MAngle.h>
#include <maya/MPoint.h>
#include <maya/MVector.h>
#include <maya/MItDag.h>
#include <maya/MMatrix.h>
#include <maya/MDagPath.h>
#include <maya/MFnDagNode.h>
#include <maya/MFnMesh.h>
#include <maya/MBoundingBox.h>
#include <maya/MHardwareRenderer.h>
#include <stdio.h>
#include <maya/MIOStream.h>
#include <maya/MDrawTraversal.h>
#include <maya/MGeometryManager.h>
#include <maya/MGeometry.h>
#include <maya/MGeometryData.h>
#include <maya/MGeometryPrimitive.h>
#include <maya/MHwTextureManager.h>
#include <maya/MImageFileInfo.h>
#include <maya/MFnSingleIndexedComponent.h>
#if MAYA_API_VERSION > 800
#define _USE_MGL_FT_
#include <maya/MGLFunctionTable.h>
#else
#include <maya/MGL.h>
#endif
OpenGLViewportRendererHUD::OpenGLViewportRendererHUD()
: OpenGLViewportRenderer( "OpenGLViewportRendererHUD" )
{
fUIName.set( "Plugin OpenGL Renderer HUD");
}
unsigned int OpenGLViewportRendererHUD::overrideThenStandardExclusion() const
{
return ~(unsigned int)kExcludeManipulators;
}
OpenGLViewportRendererFullUI::OpenGLViewportRendererFullUI()
: OpenGLViewportRenderer( "OpenGLViewportRendererFullUI" )
{
fUIName.set( "Plugin OpenGL Renderer Full UI");
}
OpenGLViewportRenderer::OpenGLViewportRenderer(
const MString & name )
{
fUIName.set( "Plugin OpenGL Renderer");
m_Version = 2.0f;
}
OpenGLViewportRenderer::~OpenGLViewportRenderer()
{
uninitialize();
}
OpenGLViewportRenderer::initialize()
{
#if defined(_USE_MGL_FT)
Tboolean useMGLFT = false;
if (rend)
if (!gGLFT)
#endif
}
OpenGLViewportRenderer::uninitialize()
{
gGLFT = NULL;
}
{
{
renderToTarget( renderInfo );
}
}
bool
float version )
{
return (api == m_API);
}
bool
{
return (override == fRenderingOverride);
}
bool
OpenGLViewportRenderer::drawSurface(
const MDagPath &dagPath,
bool active,
bool templated )
{
bool drewSurface = false;
{
bool haveTexture = false;
int numUVsets = mesh.numUVSets();
if (numUVsets > 0)
{
mesh.getCurrentUVSetName( uvSetName );
MStatus status = mesh.getAssociatedUVSetTextures(uvSetName, textures);
{
haveTexture = true;
}
}
bool haveColors = false;
int numColors = mesh.numColorSets();
if (numColors > 0)
{
haveColors = true;
mesh.getCurrentColorSetName(colorSetName);
}
bool useNormals = false;
if (useNormals)
if (haveTexture)
if (haveColors)
bool testBinormal = false;
if (testBinormal)
bool testTangent= false;
if (testTangent)
#if defined(_TEST_OGL_RENDERER_COMPONENT_FILTER_)
#else
#endif
if (numPrims)
{
if (numElem)
{
unsigned int *idx = (
unsigned int *) prim.
data();
float * posPtr = (
float * )pos.
data();
bool haveData = idx && posPtr;
float * normPtr = NULL;
if (useNormals)
{
normPtr = (
float * )norm.
data();
}
float *uvPtr = NULL;
if (haveTexture)
{
uvPtr = (
float *)uvs.
data();
}
unsigned int numColorComponents = 4;
float *clrPtr = NULL;
if (haveColors)
{
clrPtr = (
float *)clrs.
data();
}
else if (testBinormal)
{
clrPtr = (
float *)binorm.
data();
numColorComponents = 3;
}
else if (testTangent)
{
clrPtr = (
float *)tang.
data();
numColorComponents = 3;
}
if (haveData)
{
drewSurface = true;
bool drawWire = false;
#if defined(_USE_MGL_FT_)
gGLFT->
glMatrixMode( MGL_MODELVIEW );
gGLFT->
glMultMatrixd( &(matrix.
matrix[0][0]) );
#else
::glMatrixMode( GL_MODELVIEW );
::glPushMatrix();
::glMultMatrixd( &(matrix.
matrix[0][0]) );
#endif
bool boundTexture = false;
if (uvPtr)
{
{
boundTexture = true;
#if defined(_USE_MGL_FT_)
gGLFT->
glTexParameteri(MGL_TEXTURE_2D, MGL_TEXTURE_WRAP_S, MGL_REPEAT);
gGLFT->glTexParameteri(MGL_TEXTURE_2D, MGL_TEXTURE_WRAP_T, MGL_REPEAT);
gGLFT->glTexParameteri(MGL_TEXTURE_2D, MGL_TEXTURE_MIN_FILTER, MGL_LINEAR);
gGLFT->glTexParameteri(MGL_TEXTURE_2D, MGL_TEXTURE_MAG_FILTER, MGL_LINEAR);
if (!clrPtr)
gGLFT->
glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
gGLFT->
glEnableClientState( MGL_TEXTURE_COORD_ARRAY );
gGLFT->
glTexCoordPointer( 2, MGL_FLOAT, 0, uvPtr );
#else
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if (!clrPtr)
glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glTexCoordPointer( 2, GL_FLOAT, 0, uvPtr );
#endif
drawWire = false;
}
}
if (clrPtr)
{
#if defined(_USE_MGL_FT_)
gGLFT->glEnableClientState(MGL_COLOR_ARRAY);
gGLFT->
glColorPointer( numColorComponents, MGL_FLOAT, 0, clrPtr );
#else
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer( numColorComponents, GL_FLOAT, 0, clrPtr );
#endif
}
#if defined(_USE_MGL_FT_)
gGLFT->glEnableClientState( MGL_VERTEX_ARRAY );
gGLFT->
glVertexPointer ( 3, MGL_FLOAT, 0, posPtr );
#else
glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer ( 3, GL_FLOAT, 0, posPtr );
#endif
if (normPtr)
{
#if defined(_USE_MGL_FT_)
gGLFT->glEnableClientState( MGL_NORMAL_ARRAY );
gGLFT->
glNormalPointer ( MGL_FLOAT, 0, normPtr );
#else
glEnableClientState( GL_NORMAL_ARRAY );
glNormalPointer ( GL_FLOAT, 0, normPtr );
#endif
drawWire = false;
}
if (drawWire)
{
#if defined(_USE_MGL_FT_)
gGLFT->
glPolygonMode(MGL_FRONT_AND_BACK, MGL_LINE);
gGLFT->
glDrawElements ( MGL_TRIANGLES, numElem, MGL_UNSIGNED_INT, idx );
gGLFT->glPolygonMode(MGL_FRONT_AND_BACK, MGL_FILL);
if (clrPtr)
{
gGLFT->
glDisableClientState(MGL_COLOR_ARRAY);
}
#else
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDrawElements ( GL_TRIANGLES, numElem, GL_UNSIGNED_INT, idx );
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
if (clrPtr)
{
glDisableClientState(GL_COLOR_ARRAY);
}
#endif
}
else
{
#if defined(_USE_MGL_FT_)
if (active)
{
gGLFT->
glEnable(MGL_POLYGON_OFFSET_FILL);
gGLFT->
glPolygonOffset( 0.95f, 1.0f );
}
gGLFT->glDrawElements ( MGL_TRIANGLES, numElem, MGL_UNSIGNED_INT, idx );
if (active)
gGLFT->
glDisable(MGL_POLYGON_OFFSET_FILL);
if (normPtr)
gGLFT->glDisableClientState( MGL_NORMAL_ARRAY );
if (boundTexture)
{
gGLFT->glDisableClientState( MGL_TEXTURE_COORD_ARRAY );
gGLFT->glDisable(MGL_TEXTURE_2D);
gGLFT->
glBindTexture(MGL_TEXTURE_2D, 0);
}
if (clrPtr)
{
gGLFT->glDisableClientState(MGL_COLOR_ARRAY);
}
if (active)
{
gGLFT->
glColor3f( 1.0f, 1.0f, 1.0f );
gGLFT->glPolygonMode(MGL_FRONT_AND_BACK, MGL_LINE);
gGLFT->glDrawElements ( MGL_TRIANGLES, numElem, MGL_UNSIGNED_INT, idx );
gGLFT->glPolygonMode(MGL_FRONT_AND_BACK, MGL_FILL);
}
#else
if (active)
{
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset( 0.95f, 1.0f );
}
glDrawElements ( GL_TRIANGLES, numElem, GL_UNSIGNED_INT, idx );
if (active)
glDisable(GL_POLYGON_OFFSET_FILL);
if (normPtr)
glDisableClientState( GL_NORMAL_ARRAY );
if (boundTexture)
{
glDisableClientState( GL_TEXTURE_COORD_ARRAY );
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
}
if (clrPtr)
{
glDisableClientState(GL_COLOR_ARRAY);
}
if (active)
{
::glColor3f( 1.0f, 1.0f, 1.0f );
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDrawElements ( GL_TRIANGLES, numElem, GL_UNSIGNED_INT, idx );
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
#endif
}
#if defined(_USE_MGL_FT_)
gGLFT->glDisableClientState( MGL_VERTEX_ARRAY );
#else
glDisableClientState( GL_VERTEX_ARRAY );
::glPopMatrix();
#endif
}
}
}
}
return drewSurface;
}
bool
OpenGLViewportRenderer::drawBounds(
const MDagPath &dagPath,
{
{
double bottomLeftFront[3] = { minPt.
x, minPt.
y, minPt.
z };
double topLeftFront[3] = { minPt.
x, maxPt.
y, minPt.
z };
double bottomRightFront[3] = { maxPt.
x, minPt.
y, minPt.
z };
double topRightFront[3] = { maxPt.
x, maxPt.
y, minPt.
z };
double bottomLeftBack[3] = { minPt.
x, minPt.
y, maxPt.
z };
double topLeftBack[3] = { minPt.
x, maxPt.
y, maxPt.
z };
double bottomRightBack[3] = { maxPt.
x, minPt.
y, maxPt.
z };
double topRightBack[3] = { maxPt.
x, maxPt.
y, maxPt.
z };
#if defined(_USE_MGL_FT_)
gGLFT->glMatrixMode( MGL_MODELVIEW );
gGLFT->glPushMatrix();
gGLFT->glMultMatrixd( &(matrix.
matrix[0][0]) );
gGLFT->
glBegin(MGL_LINE_STRIP);
gGLFT->
glVertex3dv( bottomLeftFront );
gGLFT->glVertex3dv( bottomLeftBack );
gGLFT->glVertex3dv( topLeftBack );
gGLFT->glVertex3dv( topLeftFront );
gGLFT->glVertex3dv( bottomLeftFront );
gGLFT->glVertex3dv( bottomRightFront );
gGLFT->glVertex3dv( bottomRightBack);
gGLFT->glVertex3dv( topRightBack );
gGLFT->glVertex3dv( topRightFront );
gGLFT->glVertex3dv( bottomRightFront );
gGLFT->glBegin(MGL_LINES);
gGLFT->glVertex3dv(bottomLeftBack);
gGLFT->glVertex3dv(bottomRightBack);
gGLFT->glVertex3dv(topLeftBack);
gGLFT->glVertex3dv(topRightBack);
gGLFT->glVertex3dv(topLeftFront);
gGLFT->glVertex3dv(topRightFront);
gGLFT->glEnd();
gGLFT->glPopMatrix();
#else
::glMatrixMode( GL_MODELVIEW );
::glPushMatrix();
::glMultMatrixd( &(matrix.
matrix[0][0]) );
::glBegin(GL_LINE_STRIP);
::glVertex3dv( bottomLeftFront );
::glVertex3dv( bottomLeftBack );
::glVertex3dv( topLeftBack );
::glVertex3dv( topLeftFront );
::glVertex3dv( bottomLeftFront );
::glVertex3dv( bottomRightFront );
::glVertex3dv( bottomRightBack);
::glVertex3dv( topRightBack );
::glVertex3dv( topRightFront );
::glVertex3dv( bottomRightFront );
::glEnd();
::glBegin(GL_LINES);
::glVertex3dv(bottomLeftBack);
::glVertex3dv(bottomRightBack);
::glVertex3dv(topLeftBack);
::glVertex3dv(topRightBack);
::glVertex3dv(topLeftFront);
::glVertex3dv(topRightFront);
::glEnd();
::glPopMatrix();
#endif
}
return true;
}
bool
OpenGLViewportRenderer::setupLighting()
{
return false;
}
{
{
bool prune = false;
#if defined(_DEBUG_TRAVERSAL_PRUNING)
#endif
{
)
{
#if defined(_DEBUG_TRAVERSAL_PRUNING)
printf(
"Prune path [ %s ]\n", pname.
asChar());
#endif
prune = true;
}
}
else
{
#if defined(_DEBUG_TRAVERSAL_PRUNING)
printf(
"Don't prune path [ %s ]\n", pname.
asChar());
#endif
}
return prune;
}
};
bool OpenGLViewportRenderer::renderToTarget(
const MRenderingInfo &renderInfo )
{
#if defined(_USE_MGL_FT_)
gGLFT->glDisable(MGL_LIGHTING);
gGLFT->glBegin(MGL_LINES);
gGLFT->glColor3f( 1.0f, 0.0f, 0.0f );
gGLFT->
glVertex3f( 0.0f, 0.0f, 0.0f );
gGLFT->glVertex3f( 3.0f, 0.0f, 0.0f );
gGLFT->glColor3f( 0.0f, 1.0f, 0.0f );
gGLFT->glVertex3f( 0.0f, 0.0f, 0.0f );
gGLFT->glVertex3f( 0.0f, 3.0f, 0.0f );
gGLFT->glColor3f( 0.0f, 0.0f, 1.0f );
gGLFT->glVertex3f( 0.0f, 0.0f, 0.0f );
gGLFT->glVertex3f( 0.0f, 0.0f, 3.0f );
gGLFT->glEnd();
gGLFT->glEnable(MGL_DEPTH_TEST);
#else
::glDisable(GL_LIGHTING);
::glBegin(GL_LINES);
::glColor3f( 1.0f, 0.0f, 0.0f );
::glVertex3f( 0.0f, 0.0f, 0.0f );
::glVertex3f( 3.0f, 0.0f, 0.0f );
::glColor3f( 0.0f, 1.0f, 0.0f );
::glVertex3f( 0.0f, 0.0f, 0.0f );
::glVertex3f( 0.0f, 3.0f, 0.0f );
::glColor3f( 0.0f, 0.0f, 1.0f );
::glVertex3f( 0.0f, 0.0f, 0.0f );
::glVertex3f( 0.0f, 0.0f, 3.0f );
::glEnd();
glEnable(GL_DEPTH_TEST);
#endif
bool useDrawTraversal = true;
if (useDrawTraversal)
{
#if defined(_DEBUG_TRAVERSAL_PRUNING)
printf("==========================\n");
#endif
{
bool pruneDuringTraversal = true;
if (pruneDuringTraversal)
{
trav = new MsurfaceDrawTraversal;
}
else
{
}
if (!trav)
{
printf("Warning: failed to create a traversal class !\n");
return true;
}
printf("Warning : Frustum is invalid !\n");
#if defined(_DEBUG_TRAVERSAL_PRUNING)
printf("There are %d items on the traversal list\n", numItems );
#endif
if (numItems)
{
setupLighting();
}
unsigned int i;
for (i=0; i<numItems; i++)
{
{
bool drawIt = false;
if (!pruneDuringTraversal)
continue;
#if defined(_DEBUG_TRAVERSAL_PRUNING)
printf(
"Draw path [%d][ %s ]\n", i, pname.
asChar());
#endif
bool active = false;
bool templated = false;
{
drawIt = true;
{
active = true;
#if defined(_USE_MGL_FT_)
gGLFT->glColor3f( 1.0f, 1.0f, 1.0f );
#else
::glColor3f( 1.0f, 1.0f, 1.0f );
#endif
}
{
#if defined(_USE_MGL_FT_)
gGLFT->glColor3f( 0.2f, 0.2f, 0.2f );
#else
::glColor3f( 0.2f, 0.2f, 0.2f );
#endif
templated = true;
}
else
{
#if defined(_USE_MGL_FT_)
gGLFT->glColor3f( 0.286f, 0.706f, 1.0f );
gGLFT->glColor3f( 0.486f, 0.306f, 1.0f );
else
gGLFT->glColor3f( 0.886f, 0.206f, 1.0f );
#else
::glColor3f( 0.286f, 0.706f, 1.0f );
::glColor3f( 0.486f, 0.306f, 1.0f );
else
::glColor3f( 0.886f, 0.206f, 1.0f );
#endif
}
}
{
drawIt = true;
#if defined(_USE_MGL_FT_)
gGLFT->glColor3f( 0.0f, 1.0f, 0.0f );
#else
::glColor3f( 0.0f, 1.0f, 0.0f );
#endif
}
if (drawIt)
{
if (!drawSurface( path, active, templated ))
{
drawBounds( path, box );
}
}
}
}
if (trav)
delete trav;
}
}
else
{
MItDag dagIterator( traversalType, filter, &status);
for ( ; !dagIterator.isDone(); dagIterator.next() )
{
status = dagIterator.getPath(dagPath);
if ( !status ) {
status.
perror(
"MItDag::getPath");
continue;
}
if ( !status ) {
status.
perror(
"MFnDagNode constructor");
continue;
}
#if defined(_USE_MGL_FT_)
gGLFT->glColor3f( 1.0f, 0.0f, 0.0f );
#else
::glColor3f( 1.0f, 0.0f, 0.0f );
#endif
drawBounds( dagPath, box );
}
}
return true;
}