180 likes | 301 Views
Introduction to OpenGL. Monil Shah Laboratory for Product and Process Design , Department of Chemical Engineering, University of Illinois, Chicago, IL 60607, U.S.A. Motivation. Graphics rendering API (application programming interface)
E N D
Introduction to OpenGL Monil Shah Laboratory for Product and Process Design, Department of Chemical Engineering, University of Illinois, Chicago, IL 60607, U.S.A.
Motivation • Graphics rendering API (application programming interface) • high-quality color images composed of geometric and image primitives • window system independent • operating system independent • What can it do for us??? • GLU (OpenGL Utility Library), GLUT (OpenGL Utility Toolkit) • portable windowing API • Geometric primitives : points, lines and polygons • Rendering depends on state : colors, materials, light sources, etc.
Application Structure • Configure and open window • Initialize OpenGL state • Register input callback functions • render • resize • input: keyboard, mouse, etc. • Enter event processing loop
Include the header files #include <GL/gl.h> #include <GL/glut.h> • Display function void display (void) { glClearColor (0.0,0.0,0.0,1.0); //clear the color of the window glClear (GL_COLOR_BUFFER_BIT); //Clear the Color Buffer (more buffers later on) glLoadIdentity(); //load the Identity Matrix gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); //set the view glFlush(); //flush it all to the screen, submit for hardware execution } • Main • int main (int argc, char **argv) { • glutInit (&argc, argv); //initialize the program. • glutInitDisplayMode (GLUT_SINGLE); //set up a basic display buffer (only singular for now) • glutInitWindowSize (500, 500); //set the width and height of the window • glutInitWindowPosition (100, 100); //set the position of the window • glutCreateWindow ("A basic OpenGL Window"); //set the caption for the window • glutDisplayFunc (display); //call the display function to draw our world • glutMainLoop (); //initialize the OpenGL loop cycle • return 0; • }
glPushMatrix(); glMaterialfv(GL_FRONT,GL_DIFFUSE,color); glutSolidSphere(1,20,20); Or glutWireSphere(1,20,20); radius : for sphere slices : The number of subdivisions around the Z axis (similar to lines of longitude). stacks :The number of subdivisions along the Z axis (similar to lines of latitude). glPopMatrix(); glutSwapBuffers();
Lighting effect • //Specifies a symbolic value representing a shading technique. • glShadeModel(GL_SMOOTH); • /*If enabled, do depth comparisons and update the depth buffer. glEnable(GL_DEPTH_TEST); • /*If enabled, use the current lighting parame-parameters to compute the vertex color or index. • Otherwise, simply associate the current color index with each vertex.*/ • glEnable(GL_LIGHTING); • // lighting effect • GLfloat lightZeroPosition[] = {10.,4.,10.,1.}; • GLfloat lightZeroColor[] = {0.8,0.8,0.8,1.0}; • glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition); • glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor); • glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1); • glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05); • glEnable(GL_LIGHT0);
The Ortho function glOrtho (-alpha , alpha , -alpha , alpha, -alpha , alpha);
Opening and reading the file struct point3D { GLdouble x,y,z; }; point3D inputGrid[1000]; int i = 0; int count; int j ; int initGrid() { //define a string const int MAX = 200; char str[MAX]; // read file ifstream infile("D:\\pyramidgrid.txt");
while(!infile.eof()) { // reading line by line infile.getline(str,MAX); //if (str != NULL) //{cout << endl <<str;} int j = 0 ; char * pch; pch = strtok (str," "); // removing space and converting ascii to float, double while (pch != NULL) { if (j == 0) { // loading values into an array inputGrid [i].x = atof (pch); }
if (j == 1) { inputGrid [i].y = atof (pch); } if (j == 2) { inputGrid [i].z = atof(pch); } //cout<< endl<< pch<< endl; pch = strtok (NULL, " "); j++; } i++; }