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

#import "HS2DSquare1.h"


@implementation HS2DSquare1

- (id) init
{
	self = [super init];
	if (self != nil)
	{
		name = @"2D Haptic Square (Ridge)";
		face = glGenLists(1);
		scale = DEFAULT_SCALE;
		height = DEFAULT_HEIGHT;
		
		hlShape = hlGenShapes(1);
		
		/* Create a sqaure face that will be used throughout the drawing of this shape */
		glNewList(face, GL_COMPILE);
			glBegin(GL_QUADS);
				glNormal3f(0.0f,0.0f,1.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();
		glEndList();
	}
	return self;
}

- (void) dealloc
{
	glDeleteLists(face,1);
	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];
		
		// Background
		glColor3f(1.0f,0.0f,0.0f);
		glPushMatrix();
			glTranslatef(0.0f,0.0f,-1.0f);
			glCallList(face);
		glPopMatrix();

		// Top of square
		glColor3f(0.0f,1.0f,0.0f);
		glPushMatrix();
			glTranslatef(0.0f,0.0f,(-1.0f+height));
			glScalef(scale,scale,1.0f);
			glCallList(face);
		glPopMatrix();

		// Front edge
		glColor3f(0.0f,0.0f,1.0f);
		glPushMatrix();
			glTranslatef(0.0f,scale,(-1.0f+(height/2.0f)));
			glRotatef(90.0f,-1.0f,0.0f,0.0f);
			glScalef(scale,(height/2.0f),1.0f);
			glCallList(face);
		glPopMatrix();
		
		// Back edge
		glColor3f(0.0f,0.0f,1.0f);
		glPushMatrix();
			glTranslatef(0.0f,-scale,(-1.0f+(height/2.0f)));
			glRotatef(90.0f,1.0f,0.0f,0.0f);
			glScalef(scale,(height/2.0f),1.0f);
			glCallList(face);
		glPopMatrix();
		
		// Left edge
		glColor3f(0.0f,0.0f,1.0f);
		glPushMatrix();
			glTranslatef(-scale,0.0f,(-1.0f+(height/2.0f)));
			glRotatef(90.0f,0.0f,-1.0f,0.0f);
			glScalef((height/2.0f),scale,1.0f);
			glCallList(face);
		glPopMatrix();
		
		// Right edge
		glColor3f(0.0f,0.0f,1.0f);
		glPushMatrix();
			glTranslatef(scale,0.0f,(-1.0f+(height/2.0f)));
			glRotatef(90.0f,0.0f,1.0f,0.0f);
			glScalef((height/2.0f),scale,1.0f);
			glCallList(face);
		glPopMatrix();
		
	glPopMatrix();
}

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

@end
