1 / 23

OpenGL basics

OpenGL basics. OpenGL pipeline. Using the pipeline. Pipeline details. OpenGL libraries. gl - the basic OpenGL functions glVertex2i(0, 0); glu - utility routines for common higher level operations (cubes, spheres) glut - a system independent library of window and mouse functions

minya
Download Presentation

OpenGL basics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OpenGL basics

  2. OpenGL pipeline

  3. Using the pipeline

  4. Pipeline details

  5. OpenGL libraries • gl - the basic OpenGL functions glVertex2i(0, 0); • glu - utility routines for common higher level operations (cubes, spheres) • glut - a system independent library of window and mouse functions • wgl (wiggle) - native Windows API • glx - native X API

  6. Example: simple.c

  7. Main routine Initialize GLUT, create window Register event callback routines Start main event loop int main(int argc, char **argv) { glutInit(&argc, argv); glutCreateWindow("single triangle"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0; }

  8. Reshape function Called when canvas is resized/moved Sets up 3d to 2d geometry Void reshape(int w, int h){ glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, w, 0, h, -1, 1); glScalef(1, -1, 1); glTranslatef(0, -h, 0);}

  9. Display function void display(void) { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_TRIANGLES); glColor3f(0.0, 0.0, 1.0); /* blue */ glVertex2i(0, 0); glColor3f(0.0, 1.0, 0.0); /* green */ glVertex2i(200, 200); glColor3f(1.0, 0.0, 0.0); /* red */ glVertex2i(20, 200); glEnd(); glFlush (); }

  10. Compiling/debugging • cc -o simple simple.c -lglut -lGLU -lGL -lXmu -lXext -lX11 -lm • Printf and scanf can be used for text I/O simultaneously with graphics

  11. Online references • Bluebook references (man pages) • Greenbook GLUT references (here) • Other sources on 486 home page • http://opengl.org/

  12. Lab 1 • Play with triangle • Move it with mouse • Animate it

  13. Part A. Play with the triangle • a. Undo the glScalef and glTranslatef so the Y goes up, not down. • b. Make the triangle equilateral. • c. Use glScalef to shrink the triangle by 50%. • d. Use the glTranslate to move the triangle to the center of the window. • e. Make the triangle scale with the window resize by making the coordinates in glVertex2i relative to w and h, not absolute (you need globals!) • f. Make the simple triangle a blend of orange, purple and grey, not red, blue, green. • g. Make the triangle a pentagon (use GL_POLYGON) with any combination of colors you’d like. • h. Turn off glClear and see what happens. • i. Try the glBegin with GL_POINTS, GL_LINES, GL_LINE_LOOP, and others.

  14. Void reshape(int w, int h){ glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, w, 0, h, -1, 1); glScalef(1, -1, 1); glTranslatef(0, -h, 0);}

  15. void display(void) { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_TRIANGLES); glColor3f(0.0, 0.0, 1.0); /* blue */ glVertex2i(0, 0); glColor3f(0.0, 1.0, 0.0); /* green */ glVertex2i(200, 200); glColor3f(1.0, 0.0, 0.0); /* red */ glVertex2i(20, 200); glEnd(); glFlush (); }

  16. glVertex primitives • GL_POINTS • GL_LINES • GL_LINE_STRIP • GL_LINE_LOOP • GL_POLYGON

  17. openGL conventions • glVertex2i(100,50); • glVertex3d(100.0,50.0,60.0); • glColor3f, glColor4d

  18. Part B. Move with the mouse. • a. Add a callback routine for the move w/ button down so it sets the fan center to a new position. Use global variables for the fan position. Do a glutPostRedisplay to update the screen. • b. Modify the glTranslate call to move the fan to that location. Email me the source.

  19. Part C. Make it a rotating fan. • a. Make the triangle a fan by making four triangles centered in the window. • b. Use the idle even to advance a global variable Angle use Angle in a glRotatef call in the display callback to make the fan rotate. You will need to use glTranslatef to bring the fan to the center of the window (try translating to x = w/2, y = h/2. Set the increment of the angle to a small value so the fan doesn’t rotate too fast. • c. Use double buffering to make it go smoothly. • d. Add a menu to turn the fan on and off (and, if you wish, slow it down and speed it up.)

  20. GLUT callbacks • glutMouseFunc(myMouse) • glutMouseMoved(myMove) • glutKeyboardFunc(myKeyBoard) • glutIdleFunc(myidle) • Glut menu routines • Display, reshape

  21. General pattern • Define callback routine • Any name, fixed parameters • void zKeyBrd(unsigned char key, int x, int y) • Register callback • glutKeyboardFunc(keyboard); • Remove registration • glutKeyboardFunc( (void *)NULL );

  22. Using globals • On user event set a global variable • In reshape and display use the value void ClickDrag(int x, int y) { printf("(%d, %d)\n", x, y); xcenter = x; ycenter = y; glutPostRedisplay(); } glutMotionFunc(ClickDrag);

  23. Menus void HandleMenu(int mode) { switch (mode) { case 1: glutIdleFunc(myidle); glutPostRedisplay(); break; case 2: glutIdleFunc((void *)NULL); glutPostRedisplay(); break; case 3: exit(0); } } void initMenus ( void ) { glutCreateMenu(HandleMenu); glutAddMenuEntry("Start",1); glutAddMenuEntry("Stop",2); glutAddMenuEntry("Exit",3); glutAttachMenu(GLUT_RIGHT_BUTTON); }

More Related