1 / 29

Hwk1- A Tutorial of OPENGL ver2.0

Hwk1- A Tutorial of OPENGL ver2.0. We will introduce…. Draw a window by OpenGL Draw one/many polygons by OpenGL Rotate/Translate Matrix Push/ PopMatrix. Homework1. Please draw a walking robot!. TAs will offer…. Ex1a:Rotate/Translate/Push/ PopMatrix Ex1b:draw polygon/Keyboard function.

sierra
Download Presentation

Hwk1- A Tutorial of OPENGL ver2.0

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. Hwk1-A Tutorial of OPENGLver2.0

  2. We will introduce… • Draw a window by OpenGL • Draw one/many polygons by OpenGL • Rotate/Translate Matrix • Push/PopMatrix

  3. Homework1 • Please draw a walking robot!

  4. TAs will offer… • Ex1a:Rotate/Translate/Push/PopMatrix • Ex1b:draw polygon/Keyboard function

  5. First open Ex1b… • A basic OpenGLmain functionlooks like: int main(intargc, char** argv){ glutInit(&argc, argv); glutInitWindowSize(800, 800); glutInitWindowPosition(0, 0); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("Homework_1b"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutSpecialFunc(specialkey); glutMainLoop(); return 0; }

  6. What is in the Main function? • Windows size/starting position/name. • Display mode(how many color channels? …etc) • Tell the system “which function” handle the operations glutInitWindowSize(800, 800); glutInitWindowPosition(0, 0); glutCreateWindow("Homework_1b"); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutSpecialFunc(specialkey);

  7. void glutInit(int *argcp, char **argv); • Initializing the GLUT library • Should be called before any other GLUT funcitons • http://www.opengl.org/resources/libraries/glut/spec3/node10.html • void glutInitDisplayMode(unsigned int mode); • Specify a display mode for windows created. • GLUT_RGB / GLUT_RGBA / GLUT_INDEX • GLUT_SINGLE / GLUT_DOUBLE • GLUT_DEPTH / GLUT_STENCIL / GLUT_ACCUM • http://www.opengl.org/resources/libraries/glut/spec3/node12.html

  8. What is Display? • void glutDisplayFunc(void (*func)(void)); • Automatically called when the windows needs redraw (focus on/in, …etc) • draw function handler • call glutPostRedisplay() if you need to redraw immediately • http://www.opengl.org/resources/libraries/glut/spec3/node46.html

  9. What is in the Display function? • The draw part looks: glPushMatrix(); glTranslatef(-400+movement[0][0], movement[0][1], movement[0][2]); glColor3f(0.0, 0.0, 1.0); glBegin(GL_QUADS); glVertex3f(-100.0, 200.0, 250.0); glVertex3f(-100.0, -200.0, 250.0); glVertex3f(100.0, -200.0, 250.0); glVertex3f(100.0, 200.0, 250.0); glEnd(); glPopMatrix();

  10. Indicate the color(R,G,B) • From glBegin() to glEnd(): • Indicate every points’ position/color(if need)/normal,…etc glColor3f(0.0, 0.0, 1.0); glBegin(GL_QUADS); glVertex3f(-100.0, 200.0, 250.0); glVertex3f(-100.0, -200.0, 250.0); glVertex3f(100.0, -200.0, 250.0); glVertex3f(100.0, 200.0, 250.0); glEnd();

  11. Next 1a…

  12. Adding • glutTimerFunc(TimerFunction); • glRotatef(45, 0.0,1.0,0.0); • glTranslatef(0,0,500); • drawCooridinate() is another function just draw the cooridinates

  13. Basic prior: • Everything in OpenGL is represented as a matrix operation. • glMatrixMode(): Decide which matrix is now operated. glTranslatef( 0, 0, -1 );

  14. glTranslate{fd}( TYPE x,TYPEy,TYPE z ); • Multiplies current matrix by a matrix that moves an object by x,y,z • http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/glfunc03_9a05.asp glTranslatef( 0, 0, -1 );

  15. glRotate{fd}( TYPR angle,TYPEx,TYPRy,TYPE z ); • Multiplies current matrix by a matrix that rotates an object in a counterclockwise direction about the ray from origin to (x,y,z) with angle as the degrees. • http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/glfunc03_21d1.asp glRotatef( 45.0, 0, 0, 1);

  16. void glPushMatrix(); • Push current matrix into matrix stack. • Void glPopMatrix(); • Pop matrix from matrix stack • These stack operations of matrix is very useful for constructing a hierarchical structure. • http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/glfunc03_246w.asp

  17. To more details • Step by step

  18. drawCooridinate(); glutSolidTeapot(100.0); //glRotatef(45, 0.0,1.0,0.0); //glTranslatef(0,0,500); //drawCooridinate(); // glutSolidTeapot(50.0); // glTranslatef(0,0,-1000); // glutSolidTeapot(50.0); Current Matrix

  19. drawCooridinate(); glutSolidTeapot(100.0); glRotatef(45, 0.0,1.0,0.0); glTranslatef(0,0,500); drawCooridinate(); // glutSolidTeapot(50.0); // glTranslatef(0,0,-1000); // glutSolidTeapot(50.0); Current Matrix

  20. drawCooridinate(); glutSolidTeapot(100.0); glRotatef(45, 0.0,1.0,0.0); glTranslatef(0,0,500); drawCooridinate(); glutSolidTeapot(50.0); // glTranslatef(0,0,-1000); // glutSolidTeapot(50.0); Current Matrix

  21. … glTranslatef(0,0,-1000); glutSolidTeapot(50.0); Current Matrix

  22. Question • If I want to “back” to the initial matrix, I can… • 1.glTranslatef(0,0, 1000); • glTranslatef(0,0, -500); • glRotatef(-45, 0.0,1.0,0.0); • Work, but stupid! • 1a.glTranslatef(0,0,500); • glRotatef(-45,0.0,1.0,0.0); …. Not good Current Matrix

  23. 2. Adding push/popMatrix as follows: glPushMatrix(); drawCooridinate(); glutSolidTeapot(100.0); glRotatef(45, 0.0,1.0,0.0); glTranslatef(0,0,500); drawCooridinate(); glutSolidTeapot(50.0); glTranslatef(0,0,-1000); glutSolidTeapot(50.0); glPopMatrix();

  24. glPushMatrix(); glRotatef(theta,0.0,1.0,0.0); ….. glPopMatrix(); glTranslatef(0,200,0); GLUquadricObj *quadObj=gluNewQuadric(); gluQuadricDrawStyle(quadObj,GLU_SILHOUETTE); gluSphere(quadObj,50,20,20); glTranslatef(0,-400,0); gluQuadricDrawStyle(quadObj,GLU_FILL); glColor3f(1.0,0.0,0.0); glScalef(10.0,1.0,1.0); gluSphere(quadObj,50,20,20);

  25. Hw1b void keyboard(unsigned char key, int x, int y){ switch(key){ case '2': //type 2, move forward (axis Z) movement[selected][2]+=10.0; break; case '8': //type 8, move backward(axis Z) movement[selected][2]-=10.0; break; case '4': //type 4, move left(axis X) movement[selected][0]-=10.0; break; case '6': //type 6, move wight(axis X) movement[selected][0]+=10.0; …….. …….. case 27: exit(0); break; }

  26. Hwk1 Requirement • Please draw a robot which can walk! • At least , robot have body(sphere or cube or etc.) with 2 feet, each has more than 2 part. • Users can control each joints’ rotation. • The robot can walk (by user changing the rotation degree of joints) • The robot can walk around. • Bonus: Design a special movement with rotation / translate

  27. Reference • NeHe Productionshttp://nehe.gamedev.net/

More Related