#ifdef _CH_
#pragma package <opencv>
#endif

#ifndef _EiC
#include <GL/freeglut.h>
#include <GL/gl.h>
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <ctype.h>
#endif

int height;
int width;
int step;
int channels;
int lo         = 175;
int hi         = 255;
int match      = 0;
int X, Y, X1, Y1       = 0;
int count      =0;
float rot = 0;
int sens       = 160;

int init = 0;

CvCapture *capture = 0; 
IplImage *image;
IplImage *image0;
int thresh = 50;
CvMemStorage* storage = 0;
GLenum format;
GLuint imageID;



#define IsRGB(s) ((s[0] == 'R') && (s[1] == 'G') && (s[2] == 'B'))
#define IsBGR(s) ((s[0] == 'B') && (s[1] == 'G') && (s[2] == 'R'))

#ifndef GL_CLAMP_TO_BORDER
#define GL_CLAMP_TO_BORDER 0x812D
#endif
#define GL_MIRROR_CLAMP_EXT               0x8742


void glutReshape(int width,int height)
{
    int winWidth  = 512; 
    int winHeight = 512;
    glViewport(0,0,width,height);

    // set projection matrix
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0f,(GLfloat)width/(GLfloat)height,0.1f,200.0f);

    // switch back to modelview matrix
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
	
void draw()
{
    //GLenum format = IsBGR(image->channelSeq) ? GL_BGR_EXT : GL_RGBA;
    image = 0;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(-0.5f, -0.5f, -1.0f);
    IplImage *frame = 0;
    frame = cvQueryFrame( capture );
    image = cvCreateImage(cvSize(512, 512), 8, 3); 
    cvResize(frame, image); // have to scale to power of two. will work out padding to avoid scale distortion later.
    image->origin = frame->origin;
    glRasterPos2f(0.f, 0.f); 
    glDrawPixels(image->width, image->height, GL_BGR, GL_UNSIGNED_BYTE, image->imageData);    

    /*
    glRotatef(45, 0, 1, 0); // rotate the matrix to rotate the video texture.
    glBegin(GL_QUADS);
        glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
        glTexCoord2f(.5, 0); glVertex3f(.5, 0, 0);
        glTexCoord2f(.5, .5); glVertex3f(.5, .5, 0);
        glTexCoord2f(0, .5); glVertex3f(0, .5, 0);
    glEnd();
    */
    glutSwapBuffers();// swap the draw buffer
    cvReleaseImage(&image); // clean up used images
    glFlush();
    count += 1;
    printf("this is count %i \n", count);
}

void exitGL(int returnCode)
{				
    exit(returnCode);
}

void glutKeyboard(unsigned char key,int x,int y)
{
    if (key == 27) exitGL(0);
}

int main( int argc, char** argv )
{
    capture = cvCaptureFromCAM(0);
    //capture = cvCaptureFromAVI("mona-cone.avi"); // non-live video textures work too (will crash on EOF though..)
    glutInit(&argc,argv);
    glutInitWindowPosition(0,0);
    glutInitWindowSize(512,512);
    //glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
    glutCreateWindow("foo");
    glutReshapeFunc(glutReshape);
    glutKeyboardFunc(glutKeyboard);
    glutDisplayFunc(draw);
    glutIdleFunc(draw);
    glutMainLoop();
    return 0;
}

