All pastes #734334 Raw Edit

k4f

public text v1 · immutable
·published 2007-10-12 16:44 UTC
rendered paste body
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>

#define MAXLINE 2412

GLint width = 500;
GLint height = 500;
GLint count;
FILE *fp;
float   target[MAXLINE][3];		//Contains the Vertices (X,Y,Z Coordinates)

// Transformation variables
GLfloat xPos,      // X position of center of object
		yPos,      // Y position of center of object
		xRot,	   // Angle of rotation on the x axis
		yRot,	   // Angle of rotation on the y axis
		zRot,      // Angle of rotation on the z axis
		scale;     // Scaling factor for object

void init_window(int argc, char** argv)		
{
    glutInit(&argc,argv);				/* Initialize GLUT */
	glutInitDisplayMode (GLUT_RGB);		/* Specify display mode */
	glutInitWindowSize(width, height);	/* Set window size */
	glutInitWindowPosition(0,0);		/* Set window position */
	glutCreateWindow("HW#3 Part A");	/* Create Window */
}


void prompt_open_file()
{
   int flag = 1;
   char file_name[256];

   while(flag)
   {
      printf("\nEnter file name : ");
      scanf("%s", file_name);
      if ((fp = fopen(file_name, "r")) == NULL)
		printf("\n%s File not found!\n", file_name);
      else
		flag = 0;
   } 
}

void read_data()
{
   char buffer[50];
   int x, y, z;

   while ((fgets(buffer, 50, fp)) != NULL)
   {
      if (buffer[0] == 'j' || buffer[0] == 'J')
      {
		target[count][0] = -9999;
		count++;
      }
      else
      {
		sscanf(buffer, "%d %d %d", &x, &y,&z);
		target[count][0] = x;
		target[count][1] = y;
		target[count][2] = z;
		count++;
      }
   }
}

void change_scale()
{
   int i;

   for(i = 0; i < count + 1; i++)
   {
      if (target[i][0] == -9999)
      ;
      else
      {
		target[i][0] = (((((float)target[i][0]) + 2026) * width ) / 4052);
		target[i][1] = (((float)abs(target[i][1] - 2026) * height) / 4052);
		target[i][2] = (((((float)target[i][0]) + 2026) * 1 ) / 4052);
      }
   }
}

void read_file()
{
	prompt_open_file();
	read_data();
	change_scale();
}

void other_init()		
{
	glClearColor(1.0, 1.0, 1.0, 1.0);	/* Set background color */
	glMatrixMode(GL_PROJECTION);		/* Modify Projection Matrix */
	glLoadIdentity();					/* Set to identity matrix */
	/* Orthographic viewing volume */
	glOrtho(0.0, width, height, 0.0, -1.0, 1.0);
	count = 0;
	read_file();
}

void display(void)
{
	int i, flag;
	
	glClear(GL_COLOR_BUFFER_BIT);

	// Translates the object by the values of xPos and yPos
	glTranslatef(xPos, yPos, 0.0f);

	// Rotates xRot degrees about the X axis
	glRotatef(xRot, 1.0f, 0.0f, 0.0f);

	// Rotates zRot degrees about the Z axis
	glRotatef(zRot, 0.0f, 0.0f, 1.0f);

	// Scales by the scaling factor along the x and y axis
	glScalef(scale, scale, 1.0f);

	glColor3f(0.0, 0.0, 1.0);
	glPushMatrix();

	for (i = 0; i < count; i++)
	{
		if (target[i][0] == -9999)
		{
			flag = 0;
		}
		else
		{
			if (flag == 1)
			{
				glBegin(GL_LINES);
					glVertex3f(target[i - 1][0], target[i - 1][1], target[i-1][2]);
					glVertex3f(target[i][0], target[i][1], target[i][2]);
				glEnd();
			}
			else	flag = 1;
		}
	}

	xPos = 0.0f;
	yPos = 0.0f;
	zRot = 0.0f;
	xRot = 0.0f;
	scale = 1.0f;

	glFlush();		
}

void keyboard(unsigned char key, int x, int y) {
	// The WASD keys will be used for the movement of the square.
	// These are used instead of arrow keys because they would require
	// the assignment of the glutSpecialFunc callback method.
	switch (key) {
		// Move up
		case 'u': case 'U':
			yPos += -10.0f;
			break;

		// Move down
		case 'd': case 'D':
			yPos += 10.0f;
			break;

		// Move square left
		case 'l': case 'L':
			xPos += -10.0f;
			break;

		// Move square right
		case 'r': case 'R':
			xPos += 10.0f;
			break;

		// Decreate the scaling factor
		case '-': case '_':
			scale += -0.1f;  break;

		// Increase the scaling factor
		case '+': case '=':
			scale += 0.1f;   break;

		// Rotate the square clockwise along the Z axis
		case 'z':
			zRot += -15.0f;  break;

		// Rotate the square counter-clockwise along the Z axis
		case 'Z':
			zRot += 15.0f;   break;

		// Rotate the square clockwise along the X axis
		case 'x':
			xRot += -15.0f;  break;

		// Rotate the square counter-clockwise along the X axis
		case 'X':
			xRot += 15.0f;   break;
	}

	// Mark the current window as needing to be redisplayed.
	glutPostRedisplay();
}

void main(int argc, char** argv)
{
    init_window(argc,argv);		/* Initialize window */
    other_init();				/* Initialize other parameter */

	/* Redisplay callback event handling */
    glutDisplayFunc(display); 

	// Set the keyboard callback function
	glutKeyboardFunc(keyboard);

	// We initialize the transformation variables to their initial states.
	xPos = 0.0f;
	yPos = 0.0f;
	zRot = 0.0f;
	scale = 1.0f;

    glutMainLoop();				/* Main event loop */
}