230 likes | 452 Views
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
E N D
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
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; }
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);}
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 (); }
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
Online references • Bluebook references (man pages) • Greenbook GLUT references (here) • Other sources on 486 home page • http://opengl.org/
Lab 1 • Play with triangle • Move it with mouse • Animate it
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.
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);}
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 (); }
glVertex primitives • GL_POINTS • GL_LINES • GL_LINE_STRIP • GL_LINE_LOOP • GL_POLYGON
openGL conventions • glVertex2i(100,50); • glVertex3d(100.0,50.0,60.0); • glColor3f, glColor4d
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.
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.)
GLUT callbacks • glutMouseFunc(myMouse) • glutMouseMoved(myMove) • glutKeyboardFunc(myKeyBoard) • glutIdleFunc(myidle) • Glut menu routines • Display, reshape
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 );
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);
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); }