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

#import "HS2DSquare3.h"


@implementation HS2DSquare3

- (id) init
{
	self = [super init];
	if (self != nil)
	{
		name = @"2D Haptic Square (Gravity Line)";
		
		scale = DEFAULT_SCALE;
		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
{
	glPushMatrix();
		glColor3f(0.0f,1.0f,0.0f);
		glScalef(scale,scale,1.0f);
		glLineWidth(2.0);
		glBegin(GL_LINE_LOOP);
			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)drawGL
{
	glPushMatrix();
		
		[self entityTransform];
		
		glTranslatef(0.0f,0.0f,-0.9f);
		
		// Draw Backing Surface
		[self drawSurface];
		
		// Draw Line
		[self drawLine];
		
	glPopMatrix();
}

- (void)drawHL
{
	glPushMatrix();
		
		[self entityTransform];
		
		glTranslatef(0.0f,0.0f,-0.9f);
		
		// Square Line
		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
