// Program to demonstrate how to open a window size and position it properly // Animated version of basicwindow.c // Left mouse button fires the cannonball again // Q or q or ESC quits the program // R or C determines whether to use a circle or a rectangle // This variation by Mr. Latimer #include #include #define CIRCLE 1 #define RECTANGLE 2 int wide=300, high=300; // Global Variables for Length and Width int winX=40, winY=40; // X and Y positions for window double xPos; double yPos; double radius; double MinX=-2.0; double MaxX=2.0; double MinY=-2.0; double MaxY=2.0; double MinZ=-1.0; double MaxZ=1.0; double RADIUS=0.1; int DRAWCIRCLE; // Draw text on the screen using pre-defined Bitmap character fonts // Passed arguments include location in 3-space, pointer to string, // pointer to font void bitmap_output(int x, int y, int z, char *string, void *font) { int len, i; glRasterPos3f(x, y, 0); // Locate Raster Position in 3-space len = (int) strlen(string); // Find length of string for (i = 0; i < len; i++) // Loop through plotting all characters { // in font style glutBitmapCharacter(font, string[i]); } } void processMenuEvents(int option) { switch (option) { case CIRCLE: DRAWCIRCLE = 1; break; case RECTANGLE: DRAWCIRCLE = 0; break; } } void createGLUTMenus() { int menu; //create the menu and //tell glut that "processMenuEvents" will //handle the events menu = glutCreateMenu(processMenuEvents); //add entries to our menu glutAddMenuEntry("Circle",CIRCLE); glutAddMenuEntry("Rectangle",RECTANGLE); // attach the menu to the right button glutAttachMenu(GLUT_RIGHT_BUTTON); } void DrawMyStuff() // The Drawing Routine { // double x,y; GLUquadricObj *p; glPushMatrix(); p = gluNewQuadric(); glColor3f(1.0, 1.0, 0.0); // Set drawing color to yellow // yPos = 4* xPos - 4* xPos * xPos ; // Calculate y value of parabola glTranslatef(xPos,yPos,0.0); gluQuadricDrawStyle(p, GLU_FILL);//Use GLU_SILHOUETTE for a circle outline // gluQuadricDrawStyle(p, GLU_SILHOUETTE);//Use GLU_SILHOUETTE for a circle outline gluDisk(p,0,radius,20,1); //gluDisk(p, innerRadius, outerRadius, // slices, rings); glPopMatrix(); // if (xPos <= 1) xPos += .001; if (radius <= 2) radius += .001; } void DrawMyStuffRectangle() { glColor3f(1.0, 1.0, 0.0); yPos = 4* xPos - 4* xPos * xPos ; // Calculate y value of parabola glRectf(xPos, yPos, xPos+.4, yPos+.4); if (xPos <= 1) xPos += .001; } void DrawAxis() { glColor3f(0.0,0.0,0.0); glBegin(GL_LINES); glVertex2f(MinX, 0); glVertex2f(MaxX, 0); glVertex2f(0 , MinY); glVertex2f(0 , MaxY); glEnd(); glFlush(); } void display(void) { /* clear all pixels */ glClear(GL_COLOR_BUFFER_BIT); DrawAxis(); glPushMatrix(); // New frame of reference glTranslatef(0.5, -1.0, 0); // Move to new location glColor3f(0.0,1.0,0.1); bitmap_output(0,0,0, "It Works!", GLUT_BITMAP_TIMES_ROMAN_24); glPopMatrix(); // Return to old reference if (DRAWCIRCLE == 1) DrawMyStuff(); // Call my Drawing Routine else DrawMyStuffRectangle(); glutSwapBuffers(); glFlush(); // Force writing all pending OpenGL actions to the screen. // if (xPos <=1) // glutPostRedisplay(); if (radius <= 2) glutPostRedisplay(); } void keyboard(unsigned char key, int x, int y) { printf("Key pressed= %c %d\n", key, key); if (key == 'q' || key == 'Q' || key == (unsigned char) 27) exit(0); // This should work: key == '\27' if (key == 'r' || key == 'R') DRAWCIRCLE = 0; if (key == 'c' || key == 'C') DRAWCIRCLE = 1; } void mouse(int button, int state, int x, int y) { if (state == GLUT_DOWN && button == GLUT_LEFT_BUTTON) { xPos = -1.0; glutPostRedisplay(); } } void myinit(void) { // xPos = -1.0; xPos = 0.0; yPos = 0.0; radius = .01; DRAWCIRCLE = 1; /* 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(MinX, MaxX, MinY, MaxY, MinZ, MaxZ); // Set window dimensions // Parameters: glOrtho(Left, Right, Bottom, Top, Front, Back) DrawAxis(); } /* 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); 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); glutMouseFunc(mouse); glutDisplayFunc(display); // Call my display routine createGLUTMenus(); glutMainLoop(); // Fall into glut's main event loop return 0; } /* * Adapted for use in Supercomputer Applications * at TJHSST from the OpenGL Tutor by Mahesh Kumar * Parts of this program might have been adopted from or inspired by * sample programs in * "OpenGL Programming Guide", * Copyright (c) 1993-1997, Silicon Graphics, Inc. * "Interactive Computer Graphics" * Copyright (c) 1997, Addison-Wesley, * and/or those written by Dr. Sumanta Guha guha@cs.uwm.edu * and/or those written by Dr. Ichiro Suzuki suzuki@cs.uwm.edu */