/*
* Computer Graphics: Andrew Hanson, Indiana University
* GLUT Graphics Exercise Template
*
cc -o template.linux template.c -lglut -lGLU -lGL -lm
*
*
*/
#define shift = 9;
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
/* Local Graphics Definitions. Also in /l/b481/include, /l/b581/include */
#include "defs.h"
#include "dectEdge.c"
/* double y0,y1; math.h conflict */
/***************************************************************/
/******************** MY GLOBAL VARIABLES **********************/
/***************************************************************/
int mouseButtons = 0;
int MouseModifiers = 0;
int X=0, Y=0, dX=0, dY=0;
int main_window;
int main_height, main_width;
float mat[16];
struct POINT /*the position of the mouse*/
{
float x;
float y;
}start={-1, -1}, current={-1,-1},center, ocenter, inticenter,off={0,0};
int myDrawingMode=1;
int rad=200;
int inside = GL_FALSE; int drag = GL_FALSE;
float iflag;
float offsetx, offsety;
float polygonx[50], polygony[50];
float opolygonx[50], opolygony[50];
int i=0;
float M[3][3];
float Tx, Ty;
int j;
int k=0;
float px[50],py[50],pz[50],h[50],d[50],D,t;
int m=0;
pointq Q;
int temp = 0, vtemp = 0; int in = GL_FALSE;
float xTrans = 0;
float yTrans = 0;
float xTransInit = 0;
float yTransInit = 0;
float xScale = 0;
float yScale = 0;
float xScaleInit = 0;
float yScaleInit = 0;
float xRotAng = 0;
float yRotAng = 0;
float xRotAngInit = 0;
float yRotAngInit = 0;
int scale = 1;
float N[3][3], M[3][3], L[3][3];
/***************************************************************/
/*********************** MOUSE CALLBACKS ***********************/
/***************************************************************/
/* Mouse Button UP/DOWN or DOWN/UP transition only */
void
mouse(int button, int state, int x, int y)
{
X = x;
Y = main_height - y - 1; /* Invert to right-handed system */
button = (1 << button);
MouseModifiers = glutGetModifiers();
/* bitmask with GLUT_ACTIVE_SHIFT, GLUT_ACTIVE_CTRL, GLUT_ACTIVE_ALT */
if (state == GLUT_DOWN)
{
mouseButtons |= button;
start.x = X; start.y = Y;
// Xp= X; Yp= Y;
current.x = start.x;current.y = start.y;
switch (myDrawingMode) {
case 1:
drag = GL_TRUE;
ocenter.x = center.x; ocenter.y = center.y;
break;
case 2: drag = GL_TRUE;
break;
case 3:
switch(mouseButtons )
{
case 0x1: /* Left Mouse Button */
fprintf(stderr,"Left+shift\n");
xTransInit = current.x - start.x;
yTransInit = current.y - start.y;
break;
case 0x2: /* Middle Mouse Button */
xScaleInit = current.x - start.x;
yScaleInit = current.y - start.y;
break;
case 0x4: /* Right Mouse Button */
xRotAngInit = current.x - start.x;
yRotAngInit = current.y - start.y;
break;
}
break;
}
}
else
{
mouseButtons &= (~(button));
current.x = X; current.y = Y;
switch (myDrawingMode)
{
case 1:
drag = GL_FALSE;
break;
case 2: drag = GL_FALSE;
if (MouseModifiers != GLUT_ACTIVE_SHIFT){
polygonx[i] = current.x;
polygony[i] = current.y;i++;
}
break;
case 3:
break;
}
}
// fprintf(stderr,"Comment this out: mouse buttons = [%d], modifiers = [%d] \n state = %s, mouse at (%d,%d)\n", mouseButtons, MouseModifiers, state == GLUT_UP? "Just Up":"Just Down",x,y);
glutPostRedisplay();
}
/* Mouse motion WHILE BUTTON DOWN */
/* COMPLAINS if you call glutGetModifiers() here!! */
void
motion(int x, int y)
{
//fprintf(stderr,"Comment this out: mouse at (%d,%d)\nmouse buttons = [%d], modifiers = [%d]\n", x,y, mouseButtons, MouseModifiers);
X = x;
Y = main_height - y - 1; /* Invert to right-handed system */
current.x = X; current.y = Y;
if (MouseModifiers == GLUT_ACTIVE_SHIFT)
{
// SHIFT is down
switch(mouseButtons )
{
case 0x1: /* Left Mouse Button */
fprintf(stderr,"Left+shift\n");
xTrans += (current.x - start.x - xTransInit) / scale;
yTrans += (current.y - start.y - yTransInit) / scale;
xTransInit = current.x - start.x;
yTransInit = current.y - start.y;
break;
case 0x2: /* Middle Mouse Button */
fprintf(stderr,"middle+shift\n");
xScale += (current.x - start.x - xScaleInit) / (100* scale);
yScale += (current.y - start.y - yScaleInit) / (100* scale);
xScaleInit = current.x - start.x;
yScaleInit = current.y - start.y;
break;
case 0x4: /* Right Mouse Button */
fprintf(stderr,"right+shift\n");
xRotAng += (current.x - start.x - xRotAngInit) / (10* scale);
yRotAng += (current.y - start.y - yRotAngInit) / (10* scale);
xRotAngInit = current.x - start.x;
yRotAngInit = current.y - start.y;
break;
}
}
glutPostRedisplay();
}
/* Mouse motion WHILE ALL BUTTONS UP */
/* COMPLAINS if you call glutGetModifiers() here!! */
void
passive(int x, int y)
{
X = x;
Y = main_height - y - 1;
current.x = X; current.y = Y;
//drag = GL_FALSE;
switch (myDrawingMode) {
case 1:
//fprintf(stderr,"inside%f\n", ((current.x-center.x)*(current.x-center.x) + (current.y-center.y)*(current.y-center.y)) - rad*rad);
iflag = ((current.x-center.x)*(current.x-center.x) + (current.y-center.y)*(current.y-center.y)) - rad*rad;
if (iflag < 0)
{
inside = GL_TRUE;
}
else
{
inside = GL_FALSE;
}
break;
case 2:
for (j=0; j<i; j++)
{
if (j+1 == i){Q = dectEdge(polygonx[j], polygony[j], polygonx[0], polygony[0], current.x, current.y);
}else{
Q = dectEdge(polygonx[j], polygony[j], polygonx[j+1], polygony[j+1], current.x, current.y);}
px[j] = Q.x; py[j] = Q.y; h[j] = Q.h;printf("x=%lf y=%lf h=%lf point%d\n",px[j],py[j],h[j], j);
}
j = i;
while(j > 0)
{
j--;
if (h[temp] > h[j]){temp = j;}
}printf("nearest x=%lf y=%lf h=%lf point:%d\n",px[temp],py[temp],h[temp], temp);
if (temp+1 == i){t =