Вы находитесь на странице: 1из 7

#include <GL/gl.h> #include <GL/glut.h> #include <stdio.h> #include <stdlib.h> #include<math.h> /*File name...*/ #define FILE_NAME "sphere_surf.

zfem" /*The three different operations the mouse can control for the canvas */ #define MOUSE_ROTATE_YX 0 #define MOUSE_TRANSLATE 1 #define MOUSE_ZOOM 2 /* The current mode the mouse is in */ int mouse_mode; /* The last position of the mouse since the last callback */ extern int m_last_x, m_last_y; /* Function declarations */ void myMouseButton(int button, int state, int x, int y); void myMouseMotion(int x, int y); /*Structure Declaration*/ struct points { double x; double y; double z; }pts[2080]; struct shape { int x; int y; int z; }shps[4160]; /* Function Declarations */ void myInit (int argc, char **argv); void myDisplay (void); void myReshape (int, int); void myKeyHandler (unsigned char, int, int); void resetCamera(void); int endCanvas(int status); /*void performanceTest(void);*/ void initLighting(void); void drawSphere(void); void drawtriangle(void); /* The current vermal object */ /*int vr_object,nt;*/ int nt; #define FALSE #define TRUE 0 1

/************************************************** *********** * Global Variables / Constants /* The canvas's width and height, in pixels */ int win_width = 500; int win_height = 500; /* The dimensions of the viewing frustum */ GLfloat fleft = -1.0; GLfloat fright = 1.0; GLfloat fbottom = -1.0; GLfloat ftop = 1.0; GLfloat zNear = -1.0; GLfloat zFar = -3.0; /* Global zoom factor. Modified by mouse movement Initially 1.0 */ GLfloat zoomFactor = 1.0; /* The current mode the mouse is in, based on button pressed */ int mouse_mode; /* The last position of the mouse since the last callback */ int m_last_x, m_last_y; /* Constants for #define X_AXIS #define Y_AXIS #define Z_AXIS specifying the 3 coordinate axes */ 0 1 2

/* Begin function definitions */ void myInit (int argc, char **argv) { /*Reading the file and Storing it in Structure*/ char str[15] ; FILE *f; int i=0,n=0,pindx = 0,tindx = 0,num_points; float values[100000]; f = fopen(FILE_NAME,"r"); while(!feof(f)) { fscanf(f,"%s",str); values[n++] = atof(str); } num_points = values[9]; for(i=10,pindx=0; pindx<num_points;pindx++) { pts[pindx].x = values[i++]; pts[pindx].y = values[i++]; pts[pindx].z = values[i++]; } i = i+3; nt = values[i++]; for(tindx=0; tindx<nt;tindx++) { shps[tindx].x = values[i++]; shps[tindx].y = values[i++];

shps[tindx].z = values[i++]; } /* Set up a black background */ glClearColor(0.0, 0.0, 0.0, 0.0); resetCamera(); } /* * The main drawing routine. Based on the current display mode, other * helper functions may be called. */ void myDisplay (void) { glEnable(GL_DEPTH_TEST); /* Use the Z - buffer for visibility */ glMatrixMode(GL_MODELVIEW); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); drawSphere(); drawtriangle(); glFlush(); glutSwapBuffers(); } void drawSphere(void) { int i=0; glPushMatrix(); glColor3f(0.1,0.7,0.5); /* sets the color */ glBegin(GL_POINTS); for(i=0;i<800;i++) { glVertex3f(pts[i].x,pts[i].y,pts[i].z); } glEnd(); glFlush(); return; } void drawtriangle(void) { int i=0,v1,v2,v3; glShadeModel(GL_SMOOTH); glBegin(GL_TRIANGLES); for(i=0;i<nt;i++) { v1 = shps[i].x; v2 = shps[i].y; v3 = shps[i].z; glColor3f(1.0,0,0); glVertex3f(pts[v1].x,pts[v1].y,pts[v1].z); glColor3f(0,1.0,0); glVertex3f(pts[v2].x,pts[v2].y,pts[v2].z); glColor3f(0,0,1.0); glVertex3f(pts[v3].x,pts[v3].y,pts[v3].z); } glEnd(); }

/* * Changes the size of the canvas's window, and will implicitly * the function bound by glutReshapeFunc(), which should be * Reshape(). */ void myResize (int x, int y) { glViewport(0,0,x,y); glutReshapeWindow(x, y); } /* Stretch the image to fit the reshaped window */ void myReshape (int x, int y) { glViewport(0,0,x,y); } /* * The rotation is specified in degrees about a certain axis of * the original model. * * AXIS should be either X_AXIS, Y_AXIS, or Z_AXIS. */ void rotateCamera(double deg, int axis) { double x, y, z; x = 0; y = 0; z = 0; if (axis == X_AXIS) { x = 1.0f; } else if (axis == Y_AXIS) { y = 1.0f; } else if (axis == Z_AXIS) { z = 1.0f; } glRotatef(deg, x, y, z); } /* * Changes the level of zooming by adjusting the dimenstions of the viewing * frustum. */ void zoomCamera(double delta) { zoomFactor += delta; if (zoomFactor <= 0.0) { /* The zoom factor should be positive */ zoomFactor = 0.001; } glMatrixMode(GL_PROJECTION); glLoadIdentity(); /*

* use of glFrustum finction */ glFrustum(fleft*zoomFactor, fright*zoomFactor, fbottom*zoomFactor, ftop*zoomFactor, -zNear, -zFar); } /* * Resets the viewing frustum and moves the drawing point to the center of * the frustum. */ void resetCamera() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(fleft, fright, fbottom, ftop, -zNear, -zFar); /* Set the drawing point at the center of the frustum */ glMatrixMode(GL_MODELVIEW ); glLoadIdentity( ); glTranslatef( (fleft + fright) / 2, (fbottom + ftop) / 2, (zNear + zFar) / 2); zoomFactor = 1.0; }

void myMouseButton(int button, int state, int x, int y) { if (state == GLUT_DOWN) { m_last_x = x; m_last_y = y; if (button == GLUT_LEFT_BUTTON) { mouse_mode = MOUSE_ROTATE_YX; } else if (button == GLUT_MIDDLE_BUTTON) { mouse_mode = MOUSE_ZOOM; } else if (button == GLUT_RIGHT_BUTTON) { mouse_mode = MOUSE_TRANSLATE; } } } void myMouseMotion(int x, int y) { double d_x, d_y; /* The change in x and y since the last callback */ d_x = x - m_last_x; d_y = y - m_last_y; m_last_x = x; m_last_y = y; if (mouse_mode == MOUSE_ROTATE_YX) { /* scaling factors */

d_x /= 2.0; d_y /= 2.0; glRotatef(d_x, 0.0, 1.0, 0.0); /* y-axis rotation */ glRotatef(-d_y, 1.0, 0.0, 0.0); /* x-axis rotation */ } else if (mouse_mode == MOUSE_ZOOM) { d_y /= 100.0; zoomFactor += d_y; if (zoomFactor <= 0.0) { /* The zoom factor should be positive */ zoomFactor = 0.001; } glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(fleft*zoomFactor, fright*zoomFactor, fbottom*zoomFactor, ftop*zoomFactor, -zNear, -zFar); }else if (mouse_mode == MOUSE_TRANSLATE) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); /* Set the drawing point at the center of the frustum */ glMatrixMode(GL_MODELVIEW ); glLoadIdentity( ); glTranslatef(d_x,d_y,d_x/d_y); zoomFactor = 1.0; } /* Redraw the screen */ glutPostRedisplay(); } int main(int argc, char **argv) { glutInit(&argc, argv); /* Set initial window size and screen offset */ glutInitWindowSize(win_width, win_height); glutInitWindowPosition(50, 50); /* Using: RGB, double buffering, z-buffer */ glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("Circle"); /* Set the function callbacks */ glutDisplayFunc(myDisplay); glutReshapeFunc(myReshape); glutMouseFunc(myMouseButton);

glutMotionFunc(myMouseMotion); /* User specific initialization */ myInit(argc, argv); glutMainLoop(); return 0; }

Вам также может понравиться