//
//  HS2DCircle2.m
//  OffScreenHapticRenderer
//
//  Created by Luke Pullella on 4/04/06.
//  Copyright 2006 Luke Pullella. All rights reserved.
//

#import "HS2DCircle2.h"


@implementation HS2DCircle2

- (id) init
{
	self = [super init];
	if (self != nil)
	{
		name = @"2D Haptic Circle (Groove)";
		scale = DEFAULT_SCALE;
		baseScale = DEFAULT_BASE_SCALE;
		ridgeWidth = DEFAULT_RIDGE_WIDTH;
		ridgeHeight = DEFAULT_RIDGE_HEIGHT;
		slices = DEFAULT_SLICES;
		
		hlShape = hlGenShapes(1);
	}
	return self;
}

- (void) dealloc
{
	hlDeleteShapes(hlShape,1);
	[super dealloc];
}

/* Modified because there is no need for this function to handle scaleing */
- (void)entityTransform
{
	glRotatef(rotX,1,0,0);
	glRotatef(rotY,0,1,0);
	glRotatef(rotZ,0,0,1);
}

- (void)drawGL
{
	glPushMatrix();
		
		[self entityTransform];
		
		float DEG2RAD = 3.14159/180;
		int i;
		
		glTranslatef(0.0f,0.0f,0.1f);

		/**** Draw Shape ****/
		// Base
		glColor3f(1.0f,0.0f,0.0f);
		glNormal3f(0.0f,0.0f,1.0f);
		for(i = 0; i < slices; i++)
		{
			float degInRad1 = i*(360.0f/slices)*DEG2RAD;
			float degInRad2 = (i+1)*(360.0f/slices)*DEG2RAD;
			float scaleIn = scale+(ridgeWidth/2.0f);
			float scaleOut = baseScale;
			
			glBegin(GL_TRIANGLES);
				glVertex3f(scaleOut*cos(degInRad1),scaleOut*sin(degInRad1),-1.0f);
				glVertex3f(scaleOut*cos(degInRad2),scaleOut*sin(degInRad2),-1.0f);
				glVertex3f(scaleIn*cos(degInRad2),scaleIn*sin(degInRad2),-1.0f);
				glVertex3f(scaleOut*cos(degInRad1),scaleOut*sin(degInRad1),-1.0f);
				glVertex3f(scaleIn*cos(degInRad2),scaleIn*sin(degInRad2),-1.0f);
				glVertex3f(scaleIn*cos(degInRad1),scaleIn*sin(degInRad1),-1.0f);
			glEnd();
		}
		
		// Inner Circle
		glBegin(GL_POLYGON);
			glColor3f(1.0f,0.0f,0.0f);
			glNormal3f(0.0f,0.0f,1.0f);
			for(i = 0; i < slices; i++)
			{
				float degInRad = i*(360.0f/slices)*DEG2RAD;
				float scaleOut = scale-(ridgeWidth/2);
				glVertex3f(scaleOut*cos(degInRad),scaleOut*sin(degInRad),-1.0f);
			}
		glEnd();
		
		// Groove
		glBegin(GL_QUADS);
			for(i = 0; i < slices; i++)
			{
				float degInRad1 = i*(360.0f/slices)*DEG2RAD;
				float degInRad2 = (i+1)*(360.0f/slices)*DEG2RAD;
				float scaleIn = scale-(ridgeWidth/2.0f);
				float scaleOut = scale;
				
				// Inner Slope
				glColor3f(0.0f,0.0f,1.0f);
				glNormal3f((cos(degInRad1)+cos(degInRad2))/2,(sin(degInRad1)+sin(degInRad2))/2,90*tan((ridgeWidth/2)/ridgeHeight));
				glVertex3f(scaleOut*cos(degInRad2),scaleOut*sin(degInRad2),(-1.0f - ridgeHeight));
				glVertex3f(scaleIn*cos(degInRad2),scaleIn*sin(degInRad2),-1.0f);
				glVertex3f(scaleIn*cos(degInRad1),scaleIn*sin(degInRad1),-1.0f);
				glVertex3f(scaleOut*cos(degInRad1),scaleOut*sin(degInRad1),(-1.0f - ridgeHeight));
				
				scaleIn = scale;
				scaleOut = scale+(ridgeWidth/2);
				
				// Outer Slope
				glColor3f(0.0f,1.0f,0.0f);
				glNormal3f((cos(degInRad1)+cos(degInRad2))/2,(sin(degInRad1)+sin(degInRad2))/2,90*tan(ridgeHeight/(ridgeWidth/2)));
				glVertex3f(scaleOut*cos(degInRad2),scaleOut*sin(degInRad2),-1.0f);
				glVertex3f(scaleIn*cos(degInRad2),scaleIn*sin(degInRad2),(-1.0f - ridgeHeight));
				glVertex3f(scaleIn*cos(degInRad1),scaleIn*sin(degInRad1),(-1.0f - ridgeHeight));
				glVertex3f(scaleOut*cos(degInRad1),scaleOut*sin(degInRad1),-1.0f);
			}
		glEnd();
		
	glPopMatrix();
}

- (void)drawHL
{
	hlBeginShape(HL_SHAPE_FEEDBACK_BUFFER,hlShape);
	
		[self drawGL];
	
	hlEndShape();
}

@end
