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

#import "HS2DCircle3.h"


@implementation HS2DCircle3

- (id) init
{
	self = [super init];
	if (self != nil)
	{
		name = @"2D Haptic Circle (Gravity)";
		scale = DEFAULT_SCALE;
		height = DEFAULT_HEIGHT;
		slices = DEFAULT_SLICES;
		
		snapDistance = DEFAULT_SNAP_DISTANCE;
		
		hlShapeSurface = hlGenShapes(2);
		hlShapeLine = hlShapeSurface + 1;
	}
	return self;
}

- (void) dealloc
{
	hlDeleteShapes(hlShapeSurface,2);
	[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)drawSurface
{
	glPushMatrix();
		glBegin(GL_QUADS);
			glColor3f(1.0f,0.0f,0.0f);
			glVertex3f(1.0f,-1.0f,0.0f);
			glVertex3f(1.0f,1.0f,0.0f);
			glVertex3f(-1.0f,1.0f,0.0f);
			glVertex3f(-1.0f,-1.0f,0.0f);
		glEnd();
	glPopMatrix();
}

- (void)drawLine
{
	float DEG2RAD = 3.14159/180;
	int i;
	glBegin(GL_LINE_LOOP);
		glColor3f(0.0f,1.0f,0.0f);
		for(i = 0; i < slices; i++)
		{
			float degInRad = i*(360.0f/slices)*DEG2RAD;
			glVertex3f(scale*cos(degInRad),scale*sin(degInRad), 0.0f);
		}
	glEnd();
}

- (void)drawGL
{
	glPushMatrix();
		
		[self entityTransform];
		
		glTranslatef(0.0f,0.0f,-0.9f);
		
		// Base
		[self drawSurface];
		
		// Cicle Outline
		[self drawLine];
		
	glPopMatrix();
}

- (void)drawHL
{
	glPushMatrix();
	
		[self entityTransform];
		
		glTranslatef(0.0f,0.0f,-0.9f);
		
		// Circle Outine
		hlTouchModel(HL_CONSTRAINT);
		hlTouchModelf(HL_SNAP_DISTANCE, snapDistance);
		hlBeginShape(HL_SHAPE_FEEDBACK_BUFFER,hlShapeLine);
		
			[self drawLine];
		
		hlEndShape();
	
		// Flat Surface
		hlTouchModel(HL_CONTACT);
		hlBeginShape(HL_SHAPE_FEEDBACK_BUFFER,hlShapeSurface);
		
			[self drawSurface];
	
		hlEndShape();
		
	glPopMatrix();
}

@end
