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

#import "HS2DSquare2.h"


@implementation HS2DSquare2

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

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

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

- (void)drawGL
{
	glPushMatrix();
	
		glNormal3f(0,0,1);
		
		[self entityTransform];
		
		/**** POINTS ****/
	
		// Outer
		GLfloat vfOBL[3] = { -1.0f, 1.0f, 0.0f };
		GLfloat vfOBR[3] = { 1.0f, 1.0f, 0.0f };
		GLfloat vfOFR[3] = { 1.0f, -1.0f, 0.0f };
		GLfloat vfOFL[3] = { -1.0f, -1.0f, 0.0f };
		
		// Inner (Outer of Ridge)
		GLfloat vfIOBL[3] = { -(scale+(ridgeWidth/2.0f)), (scale+(ridgeWidth/2.0f)), 0.0f };
		GLfloat vfIOBR[3] = { (scale+(ridgeWidth/2.0f)), (scale+(ridgeWidth/2.0f)), 0.0f };
		GLfloat vfIOFR[3] = { (scale+(ridgeWidth/2.0f)), -(scale+(ridgeWidth/2.0f)), 0.0f };
		GLfloat vfIOFL[3] = { -(scale+(ridgeWidth/2.0f)), -(scale+(ridgeWidth/2.0f)), 0.0f };
		
		// Inner (Middle of Ridge)
		GLfloat vfIMBL[3] = { -scale, scale, -ridgeHeight };
		GLfloat vfIMBR[3] = { scale, scale, -ridgeHeight };
		GLfloat vfIMFR[3] = { scale, -scale, -ridgeHeight };
		GLfloat vfIMFL[3] = { -scale, -scale, -ridgeHeight };
		
		// Inner (Inner of Ridge)
		GLfloat vfIIBL[3] = { -(scale-(ridgeWidth/2.0f)), (scale-(ridgeWidth/2.0f)), 0.0f };
		GLfloat vfIIBR[3] = { (scale-(ridgeWidth/2.0f)), (scale-(ridgeWidth/2.0f)), 0.0f };
		GLfloat vfIIFR[3] = { (scale-(ridgeWidth/2.0f)), -(scale-(ridgeWidth/2.0f)), 0.0f };
		GLfloat vfIIFL[3] = { -(scale-(ridgeWidth/2.0f)), -(scale-(ridgeWidth/2.0f)), 0.0f };
		
		/**** Draw Shape ****/
		glTranslatef(0.0f,0.0f,-0.9f);
		
		glBegin(GL_QUADS);
			// Outside first
			glColor3f(1.0f,0.0f,0.0f);
			
			glVertex3fv(vfIOBL);
			glVertex3fv(vfIOBR);
			glVertex3fv(vfOBR);
			glVertex3fv(vfOBL);
			
			glVertex3fv(vfIOBR);
			glVertex3fv(vfIOFR);
			glVertex3fv(vfOFR);
			glVertex3fv(vfOBR);
			
			glVertex3fv(vfIOFR);
			glVertex3fv(vfIOFL);
			glVertex3fv(vfOFL);
			glVertex3fv(vfOFR);
			
			glVertex3fv(vfIOFL);
			glVertex3fv(vfIOBL);
			glVertex3fv(vfOBL);
			glVertex3fv(vfOFL);
			
			// Inner Second
			glVertex3fv(vfIIFL);
			glVertex3fv(vfIIFR);
			glVertex3fv(vfIIBR);
			glVertex3fv(vfIIBL);
			
			// Outer ridge slope Third
			glColor3f(0.0f,1.0f,0.0f);
			
			glVertex3fv(vfIMBL);
			glVertex3fv(vfIMBR);
			glVertex3fv(vfIOBR);
			glVertex3fv(vfIOBL);
			
			glVertex3fv(vfIMBR);
			glVertex3fv(vfIMFR);
			glVertex3fv(vfIOFR);
			glVertex3fv(vfIOBR);
			
			glVertex3fv(vfIMFR);
			glVertex3fv(vfIMFL);
			glVertex3fv(vfIOFL);
			glVertex3fv(vfIOFR);
			
			glVertex3fv(vfIMFL);
			glVertex3fv(vfIMBL);
			glVertex3fv(vfIOBL);
			glVertex3fv(vfIOFL);
			
			// Inner ridge slope Fourth
			glColor3f(0.0f,0.0f,1.0f);
			
			glVertex3fv(vfIIBL);
			glVertex3fv(vfIIBR);
			glVertex3fv(vfIMBR);
			glVertex3fv(vfIMBL);
			
			glVertex3fv(vfIIBR);
			glVertex3fv(vfIIFR);
			glVertex3fv(vfIMFR);
			glVertex3fv(vfIMBR);
			
			glVertex3fv(vfIIFR);
			glVertex3fv(vfIIFL);
			glVertex3fv(vfIMFL);
			glVertex3fv(vfIMFR);
			
			glVertex3fv(vfIIFL);
			glVertex3fv(vfIIBL);
			glVertex3fv(vfIMBL);
			glVertex3fv(vfIMFL);
			
			
		glEnd();
		
	glPopMatrix();
}

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

@end
