// Program to demonstrate how to open a window size and position it properly #include #include #include int wide=300, high=300; // Global Variables for Length and Width int winX=40, winY=40; // X and Y positions for window double radians, angle, velocity; double dx, dy; double vx, vy; double time; double dt = 0.05; double a = 9.8; double timestart=-10.0; double timestop=10.0; double x=0.0,y=0.0; double clock=0.0; double distanceup, distancefall; //const double PI = 3.14159; void drawAxis(); void drawAxis () { glColor3f(1.0, 0.0, 1.0); glBegin(GL_LINES); glVertex3f(0.0, -50.0, 0.0); glVertex3f(0.0, 400.0, 0.0); glVertex3f(-50.0, 0.0, 0.0); glVertex3f(800.0, 0.0, 0.0); glEnd(); } void DrawGraph() // The Drawing Routine { glColor3f(1.0, 1.0, 0.0); // Set drawing color to yellow glBegin( GL_POINTS ); // Start drawing POINTS clock=clock+dt; x = x + dx; distanceup = distanceup + dy; distancefall = 0.5* a * clock*clock; y = distanceup - distancefall; // Calculate y value of parabola printf("x=%f, y=%f, t=%f, dx=%f, dy=%f, vx=%f,vy=%f\n", x,y,time,dx,dy,vx,vy); glVertex3f(x, y, 0.0); // Plot point at (x,y) z=0 in 2D glEnd(); // End drawing points } void display(void) { /* clear all pixels */ glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.8900, 0.1500, 0.2100); //alizarin_crimson see glRectf(500.0,25.0,550.0,0.0); // http://stevehollasch.com/cgindex/color/colors.txt drawAxis(); DrawGraph(); // Call my Drawing Routine glFlush(); // Force writing all pending OpenGL actions to the screen. time = time + dt; glutSwapBuffers(); if (x >800) dx = dx*-1; if (time <=timestop && y >=0 ) glutPostRedisplay(); } void keyboard(unsigned char key, int x, int y) { if (key == (unsigned char) 27) //Press 'Esc' key, with mouse on window, exit(0); // to exit } void myinit(void) { /* select clearing (background) color */ glClearColor(0.5, 0.5, 0.5, 0.0); // These RGB values make gray /* initialize viewing values */ glMatrixMode(GL_PROJECTION); // Select Matrix Mode glLoadIdentity(); // Provide Base Matrix glOrtho(-50.0, 1000.0, -50.0, 500.0, -1.0, 1.0); // Set window dimensions glPointSize(5.0); } /* Declare initial window size, position, and display mode */ int main(int argc, char* argv[]) { printf("Creating window %d pixels wide and %d pixels high", wide, high); printf(" at (%d,%d).\n", winX, winY); printf("Enter angle in degrees: "); scanf("%lf", &angle); radians = angle*M_PI/180.0; printf("Enter velocity in meters per sec (gravity acc = 9.8 m/sec^2): "); scanf("%lf", &velocity); vx = velocity*cos(radians); vy = velocity*sin(radians); dx = vx*dt; dy = vy*dt; distanceup=0.0; time = timestart; clock=0; glutInit(&argc, argv); // Call glut's initialization routine glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); // Set Single Buffer, RGB Color glutInitWindowSize(wide, high); // Initialize window size glutInitWindowPosition(winX,winY); // Position window glutCreateWindow("My Window"); // Actually create window with title myinit(); // Call my initialization routine glutKeyboardFunc(keyboard); glutDisplayFunc(display); // Call my display routine glutMainLoop(); // Fall into glut's main event loop return 0; }