#define WIN32_LEAN_AND_MEAN
#define STRICT

#include <windows.h>
#include <windowsx.h>
#include <winuser.h>
#include <winsock2.h>
#include <commctrl.h>
#include <commdlg.h>
#include <mmsystem.h>
#include <commctrl.h>
#include <shlwapi.h>

#include <io.h>
#include <conio.h>
#include <commctrl.h>
#include <shlwapi.h>

#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <time.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <math.h>

#define _USE_BETTLEJUICE_

#include "beetlejuice.h"


#define L -.5       /* For x: left side     */
#define R  .5       /* For x: right side    */
#define D -.5       /* For y: down side     */
#define U  .5       /* For y: upper side    */
#define F  .5       /* For z: far side      */
#define N -.5       /* For z: near side     */

/* UnitCube(): define a cube in the graphics environment */
void UnitCube(FRtShaderHandle _hShader, FRtMatrix _objToWorld, int _id)
{
unsigned char szBuf[256];

    static FRtPoint Cube[6][4] = {
        { {L,D,F},  {L,D,N},  {R,D,N},  {R,D,F} },   /* Bottom face  */
        { {L,D,F},  {L,U,F},  {L,U,N},  {L,D,N} },   /* Left face    */
        { {R,U,N},  {L,U,N},  {L,U,F},  {R,U,F} },   /* Top  face    */
        { {R,U,N},  {R,U,F},  {R,D,F},  {R,D,N} },   /* Right face   */
        { {R,D,F},  {R,U,F},  {L,U,F},  {L,D,F} },   /* Far face     */
        { {L,U,N},  {R,U,N},  {R,D,N},  {L,D,N} }    /* Near face    */
    };
    int i;
FRiAttributeBegin();
    for( i = 0; i < 6; i++)  
	{
	sprintf((char *)szBuf, "cubeface%d%d" , _id, i);

	FRiAttribute(FRI_ATT_IDENTIFIER, szBuf, FRI_NULL);

	FRiSurfaceInstance((FRtShaderHandle)_hShader);

	FRiTransform(_objToWorld);

    FRiPolygon( (FRtInt) 4, FRI_P, (FRtPointer) Cube[i], FRI_NULL);    

	FRiAttributeEnd();
	}
}


BOOL Light()
{
	FRtMatrix objToWorld;
	objToWorld[0][0]=0.177665f; objToWorld[0][1]=0.984091f; objToWorld[0][2]=0.000000f; objToWorld[0][3]=0.000000f;
	objToWorld[1][0]=-0.824751f; objToWorld[1][1]=0.148898f; objToWorld[1][2]=0.545542f; objToWorld[1][3]=0.000000f;
	objToWorld[2][0]=0.536863f; objToWorld[2][1]=-0.096924f; objToWorld[2][2]=0.838084f; objToWorld[2][3]=0.000000f;
	objToWorld[3][0]=1505.846924f; objToWorld[3][1]=414.878082f; objToWorld[3][2]=1539.483032f; objToWorld[3][3]=1.000000f;


	FRtColor   lightcolor,shadowcolor;
	FRtFloat   intensity,coneAngle,coneDelta,decayRadius,attenStart,attenEnd,nearAttenStart,nearAttenEnd;
	FRtFloat   contrast,diffSoften,shadMult,atmosOpacity,atmosColAmt,shadowTraceBias,shadowMapBias,shadowMapSmpRange,aspect;
	FRtInt     decayType,shape,shadowMapSize;
	FRtBoolean useAtten,useNearAtten,affectDiffuse,affectSpecular,ambientOnly,overshoot,ltAfctShad,atmosShadows,shadowMapAbs;




	FRiAttributeBegin();

		FRiAttribute(FRI_ATT_IDENTIFIER, "Light", FRI_NULL);

		FRiTransform(objToWorld);

		lightcolor[0]=0.921569; lightcolor[1]=0.956863; lightcolor[2]=1.000000;
		shadowcolor[0]=0.000000; shadowcolor[1]=0.000000; shadowcolor[2]=0.000000;
		intensity=1.000000;
		coneAngle=952.000000;
		coneDelta=2.000000;
		decayRadius=0.000000;
		attenStart=80.000000;
		attenEnd=320.000000;
		nearAttenStart=0.000000;
		nearAttenEnd=0.000000;
		contrast=0.000000;
		diffSoften=0.000000;
		shadMult=1.000000;
		atmosOpacity=1.000000;
		atmosColAmt=1.000000;
		shadowTraceBias=0.000000;
		shadowMapBias=4.000000;
		aspect=1.000000;
		decayType=0;
		shape=1;
		shadowMapSize=256;
		useAtten=false;
		useNearAtten=false;
		affectDiffuse=true;
		affectSpecular=true;
		ambientOnly=false;
		overshoot=false;
		ltAfctShad=true;
		atmosShadows=false;
		shadowMapAbs=false;


		FRiLightSource(FRI_DISTANTLIGHT,
		               FRI_INTENSITY,              &intensity,
		               FRI_LIGHTCOLOR,              lightcolor,
		               FRI_CONEANGLE,              &coneAngle,
		               FRI_CONEDELTAANGLE,         &coneDelta,
		               FRI_DECAY,                  &decayType,
		               FRI_DECAYSTART,             &decayRadius,
		               FRI_USEATTENUATION,         &useAtten,
		               FRI_ATTENUATIONSTART,       &attenStart,
		               FRI_ATTENUATIONEND,         &attenEnd,
		               FRI_USENEARATTENUATION,     &useNearAtten,
		               FRI_NEARATTENUATIONSTART,   &nearAttenStart,
		               FRI_NEARATTENUATIONEND,     &nearAttenEnd,
		               FRI_CONTRAST,               &contrast,
		               FRI_DIFFSOFTEN,             &diffSoften,
		               FRI_AFFECTDIFFUSE,          &affectDiffuse,
		               FRI_AFFECTSPECULAR,         &affectSpecular,
		               FRI_AMBIENTONLY,            &ambientOnly,
		               FRI_OVERSHOOT,              &overshoot,
		               FRI_LIGHTSHAPE,             &shape,
		               FRI_LIGHTSHAPE_ASPECT,      &aspect,
		               FRI_SHADOWDENSITY,          &shadMult,
		               FRI_SHADOWCOLOR,             shadowcolor,
		               FRI_LIGHTAFFECTSHADOWCOLOR, &ltAfctShad,
		               FRI_ATMOSSHADOWS,           &atmosShadows,
		               FRI_ATMOSSHADOWS_OPACITY,   &atmosOpacity,
		               FRI_ATMOSSHADOWS_COLORAMT,  &atmosColAmt,
		               FRI_SHADOWTYPE,              "raytraceshadows",
		               FRI_RAYTRACESHADOWS_BIAS,   &shadowTraceBias,
		               FRI_SHADOWMAPS_BIAS,        &shadowMapBias,
		               FRI_SHADOWMAPS_SMPRANGE,    &shadowMapSmpRange,
		               FRI_SHADOWMAPS_SIZE,        &shadowMapSize,
		               FRI_SHADOWMAPS_ABSBIAS,     &shadowMapAbs,
		               FRI_NULL);


	FRiAttributeEnd();

	return TRUE;
}

BOOL Box()
{

	FRtPoint verts[8];
	FRtInt faces[36];
	FRtUInt smgroups[12];

	FRtMatrix objToWorld;
	objToWorld[0][0]=1.210000f; objToWorld[0][1]=-0.000000f; objToWorld[0][2]=0.000000f; objToWorld[0][3]=0.000000f;
	objToWorld[1][0]=-0.000000f; objToWorld[1][1]=1.525500f; objToWorld[1][2]=0.000000f; objToWorld[1][3]=0.000000f;
	objToWorld[2][0]=-0.000000f; objToWorld[2][1]=0.000000f; objToWorld[2][2]=1.000000f; objToWorld[2][3]=0.000000f;
	objToWorld[3][0]=-1.529449f; objToWorld[3][1]=-8.839781f; objToWorld[3][2]=-59.913006f; objToWorld[3][3]=1.000000f;


	verts[0][0]=-154.696136f; verts[0][1]=-95.395950f; verts[0][2]=0.000000f;
	verts[1][0]=154.696136f; verts[1][1]=-95.395950f; verts[1][2]=0.000000f;
	verts[2][0]=-154.696136f; verts[2][1]=95.395950f; verts[2][2]=0.000000f;
	verts[3][0]=154.696136f; verts[3][1]=95.395950f; verts[3][2]=0.000000f;
	verts[4][0]=-154.696136f; verts[4][1]=-95.395950f; verts[4][2]=120.810310f;
	verts[5][0]=154.696136f; verts[5][1]=-95.395950f; verts[5][2]=120.810310f;
	verts[6][0]=-154.696136f; verts[6][1]=95.395950f; verts[6][2]=120.810310f;
	verts[7][0]=154.696136f; verts[7][1]=95.395950f; verts[7][2]=120.810310f;

	faces[0]=2; faces[1]=0; faces[2]=3; smgroups[0]=2;
	faces[3]=1; faces[4]=3; faces[5]=0; smgroups[1]=2;
	faces[6]=5; faces[7]=4; faces[8]=7; smgroups[2]=4;
	faces[9]=6; faces[10]=7; faces[11]=4; smgroups[3]=4;
	faces[12]=1; faces[13]=0; faces[14]=5; smgroups[4]=8;
	faces[15]=4; faces[16]=5; faces[17]=0; smgroups[5]=8;
	faces[18]=3; faces[19]=1; faces[20]=7; smgroups[6]=16;
	faces[21]=5; faces[22]=7; faces[23]=1; smgroups[7]=16;
	faces[24]=2; faces[25]=3; faces[26]=6; smgroups[8]=32;
	faces[27]=7; faces[28]=6; faces[29]=3; smgroups[9]=32;
	faces[30]=0; faces[31]=2; faces[32]=4; smgroups[10]=64;
	faces[33]=6; faces[34]=4; faces[35]=2; smgroups[11]=64;

	FRiAttributeBegin();

		FRiAttribute(FRI_ATT_IDENTIFIER, "Box01", FRI_NULL);

		FRtFloat selfIllum=0.0f;
		FRtColor white; white[0]=white[1]=white[2]=1.0f;

		FRiSurface("standard", "color" , &white, FRI_KSI, &selfIllum, FRI_NULL);

		FRiTransform(objToWorld);
		FRiPointsTriangles(12, 8, faces, FRI_P, &verts, FRI_SMGROUP, &smgroups, FRI_NULL);

	FRiAttributeEnd();

	return TRUE;
}

void go()
{
FRtFloat		i_amb = 0.3f;
FRtFloat		i_dist = 1.0f;

FRtPoint		from = {-0.1f, 0.5f, -1.0f};
FRtPoint		to = {0.0, 0.0, 0.0};

FRtColor gray,red,yellow;
gray[0]  =0.85f; gray[1]  =0.85f; gray[2]  =0.85f;
red[0]   =0.85f; red[1]   =0.0f;  red[2]   =0.0f;
yellow[0]=1.0f;  yellow[1]=1.0f;  yellow[2]=0.0f;

FRtShaderHandle grayShader   = FRiSurface("standard", FRI_COLOR, gray,   FRI_NULL);
FRtShaderHandle redShader    = FRiSurface("standard", FRI_COLOR, red,    FRI_NULL);
FRtShaderHandle yellowShader = FRiSurface("standard", FRI_COLOR, yellow, FRI_NULL);

FRtMatrix objToWorld;

objToWorld[0][0]=1.000000f; objToWorld[0][1]=0.000000f; objToWorld[0][2]=-0.000000f; objToWorld[0][3]=0.000000f;
objToWorld[1][0]=0.000000f; objToWorld[1][1]=1.000000f; objToWorld[1][2]=-0.000000f; objToWorld[1][3]=0.000000f;
objToWorld[2][0]=-0.000000f; objToWorld[2][1]=-0.000000f; objToWorld[2][2]=1.000000f; objToWorld[2][3]=0.000000f;
objToWorld[3][0]=0.000005f; objToWorld[3][1]=0.000005f; objToWorld[3][2]=-0.000000f; objToWorld[3][3]=1.000000f;

FRtFloat selfIllum=1.0f;
FRtColor white; white[0]=white[1]=white[2]=1.0f;

FRtShaderHandle whiteShader = FRiSurface("standard", FRI_COLOR, &white, FRI_KSI, &selfIllum, FRI_NULL);

objToWorld[3][0]=0.000005f; 
objToWorld[3][1]=0.000005f; 
objToWorld[3][2]=1.000000f;


UnitCube(whiteShader, objToWorld, 0);

objToWorld[3][0]=-2.000005f; 
objToWorld[3][1]=0.000005f; 
objToWorld[3][2]=1.000000f;

UnitCube(redShader, objToWorld, 1);

objToWorld[3][0]=-2.000005f; 
objToWorld[3][1]=-2.000005f; 
objToWorld[3][2]=1.000000f;

UnitCube(redShader, objToWorld, 2);

objToWorld[3][0]=0.000005f; 
objToWorld[3][1]=-2.000005f; 
objToWorld[3][2]=1.000000f;

UnitCube(redShader, objToWorld, 3);


objToWorld[3][0]=2.000005f; 
objToWorld[3][1]=-2.000005f; 
objToWorld[3][2]=1.000000f;

UnitCube(redShader, objToWorld, 4);

objToWorld[3][0]=-2.000005f; 
objToWorld[3][1]=2.000005f; 
objToWorld[3][2]=1.000000f;

UnitCube(redShader, objToWorld, 5);

objToWorld[3][0]=2.000005f; 
objToWorld[3][1]=2.000005f; 
objToWorld[3][2]=1.000000f;

UnitCube(redShader, objToWorld, 6);

objToWorld[3][0]=0.000005f; 
objToWorld[3][1]=2.000005f; 
objToWorld[3][2]=1.000000f;

UnitCube(redShader, objToWorld, 7);

objToWorld[3][0]=2.000005f; 
objToWorld[3][1]=0.000005f; 
objToWorld[3][2]=1.000000f;

UnitCube(redShader, objToWorld, 8);

Box();

Light();
}



// EOF