#include <maya/MIOStream.h>
#include <math.h>
#include <stdlib.h>
#include "simpleEmitter.h"
#include <maya/MVectorArray.h>
#include <maya/MDoubleArray.h>
#include <maya/MIntArray.h>
#include <maya/MMatrix.h>
#include <maya/MArrayDataBuilder.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnUnitAttribute.h>
#include <maya/MFnVectorArrayData.h>
#include <maya/MFnDoubleArrayData.h>
#include <maya/MFnArrayAttrsData.h>
#include <maya/MFnMatrixData.h>
MTypeId simpleEmitter::id( 0x80014 );
 
simpleEmitter::simpleEmitter()
:   lastWorldPoint(0, 0, 0, 1)
{
}
simpleEmitter::~simpleEmitter()
{
}
void *simpleEmitter::creator()
{
    return new simpleEmitter;
}
MStatus simpleEmitter::initialize()
 
{
}
{
    
    
    if( !(plug == mOutput) )
    
    
    
    
    McheckErr(status, "ERROR in plug.logicalIndex.\n");
    
    
    
    McheckErr(status, "ERROR in hOutArray = block.outputArrayValue.\n");
    
    
    McheckErr(status, "ERROR in bOutArray = hOutArray.builder.\n");
    
    
    McheckErr(status, "ERROR in hOut = bOutArray.addElement.\n");
    
    
    
    
    McheckErr(status, "ERROR in fnOutput.create.\n");
    
    
    
    bool beenFull = isFullValue( multiIndex, block );
    if( beenFull )
    {
    }
    
    
    
    
    
    
    
    MPoint worldPos(0.0, 0.0, 0.0);
 
    status = getWorldPosition( worldPos );
    worldV[0] = worldPos[0];
    worldV[1] = worldPos[1];
    worldV[2] = worldPos[2];
    
    
    
    
    
    MTime cT = currentTimeValue( block );
 
    MTime sT = startTimeValue( multiIndex, block );
 
    MTime dT = deltaTimeValue( multiIndex, block );
 
    if( (cT <= sT) || (dT <= 0.0) )
    {
        
        
        
        
        
        
        
        
    }
    
    
    
    
    double rate = rateValue( block );
    MTime dtRate = deltaTimeValue( plugIndex, block );
 
    int intCount = (int)dblCount;
    emitCountPP.
append( intCount );
    
    
    double speed = speedValue( block );
    MVector dirV = directionVector( block );
 
    double inheritFactor = inheritFactorValue( multiIndex, block );
    
    
    
    
    
    MVector rotatedV = useRotation ( dirV );
 
    
    
    emit( inPosAry, inVelAry, emitCountPP,
            dt, speed, inheritFactor, rotatedV, fnOutPos, fnOutVel, fnOutTime );
    
    
}
void simpleEmitter::emit
    (
        double dt,                      
        double speed,                   
        double inheritFactor,           
    )
{
    
    
    int posLength = inPosAry.
length();
 
    int velLength = inVelAry.
length();
 
    int countLength = emitCountPP.
length();
 
    if( (posLength != velLength) || (posLength != countLength) )
        return;
    
    
    int index;
    int totalCount = 0;
    for( index = 0; index < countLength; index ++ )
        totalCount += emitCountPP[index];
    if( totalCount <= 0 )
        return;
    
    
    
    
    int emitCount;
    for( index = 0; index < posLength; index++ )
    {
        emitCount = emitCountPP[index];
        if( emitCount <= 0 )
            continue;
        sPos = inPosAry[index];
        sVel = inVelAry[index];
        prePos = sPos - sVel * dt;
        for( int i = 0; i < emitCount; i++ )
        {
            double alpha = ( (double)i + drand48() ) / (double)emitCount;
            newPos = (1 - alpha) * prePos + alpha * sPos;
            newVel = dirV * speed;
            newPos += newVel * ( dt * (1 - alpha) );
            newVel += sVel * inheritFactor;
            
            
        }
    }
}
{
    
    
    MObject worldMatrixAttr = fnThisNode.attribute( 
"worldMatrix" );
 
    
    
    
    MPlug matrixPlug( thisNode, worldMatrixAttr );
 
    matrixPlug = matrixPlug.elementByLogicalIndex( 0 );
    
    
    status = matrixPlug.getValue( matrixObject );
    if( !status )
    {
        status.
perror(
"simpleEmitter::getWorldPosition: get matrixObject");
        return( status );
    }
    if( !status )
    {
        status.
perror(
"simpleEmitter::getWorldPosition: get worldMatrixData");
        return( status );
    }
    if( !status )
    {
        status.
perror(
"simpleEmitter::getWorldPosition: get worldMatrix");
        return( status );
    }
    
    
    point[0] = worldMatrix( 3, 0 );
    point[1] = worldMatrix( 3, 1 );
    point[2] = worldMatrix( 3, 2 );
    return( status );
}
{
    
    
    MObject worldMatrixAttr = fnThisNode.attribute( 
"worldMatrix" );
 
    
    
    
    MPlug matrixPlug( thisNode, worldMatrixAttr );
 
    matrixPlug = matrixPlug.elementByLogicalIndex( 0 );
    McheckErr(status, "ERROR getting hWMatrix from dataBlock.\n");
    {
        point[0] = wMatrix(3, 0);
        point[1] = wMatrix(3, 1);
        point[2] = wMatrix(3, 2);
    }
    return( status );
}
{
    
    
    MObject worldMatrixAttr = fnThisNode.attribute( 
"worldMatrix" );
 
    
    
    
    MPlug matrixPlug( thisNode, worldMatrixAttr );
 
    matrixPlug = matrixPlug.elementByLogicalIndex( 0 );
    
    
    status = matrixPlug.getValue( matrixObject );
    if( !status )
    {
        status.
perror(
"simpleEmitter::getWorldPosition: get matrixObject");
        return ( direction );
    }
    if( !status )
    {
        status.
perror(
"simpleEmitter::getWorldPosition: get worldMatrixData");
        return( direction );
    }
    if( !status )
    {
        status.
perror(
"simpleEmitter::getWorldPosition: get worldMatrix");
        return( direction );
    }
    rotatedVector = direction * worldMatrix;
    return( rotatedVector );
}
#define TORUS_PI 3.14159265
#define TORUS_2PI 2*TORUS_PI
#define EDGES 30
#define SEGMENTS 20
{
    for (int j = 0; j < SEGMENTS; j++ )
    {
        glPushMatrix();
        glRotatef( GLfloat(360 * j / SEGMENTS), 0.0, 1.0, 0.0 );
        glTranslatef( 1.5, 0.0, 0.0 );
        for (int i = 0; i < EDGES; i++ )
        {
            glBegin(GL_LINE_STRIP);
            float p0 = float( TORUS_2PI * i / EDGES );
            float p1 = float( TORUS_2PI * (i+1) / EDGES );
            glVertex2f( cos(p0), sin(p0) );
            glVertex2f( cos(p1), sin(p1) );
            glEnd();
        }
        glPopMatrix();
    }
}
{
    MFnPlugin plugin(obj, PLUGIN_COMPANY, 
"3.0", 
"Any");
 
    status = plugin.registerNode( "simpleEmitter", simpleEmitter::id,
                            &simpleEmitter::creator, &simpleEmitter::initialize,
    if (!status) {
        status.
perror(
"registerNode");
        return status;
    }
    return status;
}
{
    status = plugin.deregisterNode( simpleEmitter::id );
    if (!status) {
        status.
perror(
"deregisterNode");
        return status;
    }
    return status;
}