Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Hwk1- A Tutorial of OPENGL ver2.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… 1.Ex1a:Rotate/Translate/Push/PopMatrix 2.Ex1b:draw polygon/Keyboard function

5 First open Ex1b… A basic OpenGL main function looks like: int main(int argc, 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.htmlhttp://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.ht ml

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.h tml

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,…et c 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

12 Next 1a…

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

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

15 glTranslate{fd}( TYPE x,TYPE y,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 );

16 glRotate{fd}( TYPR angle,TYPE x,TYPR y,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);

17 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

18 To more details Step by step

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 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

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

23 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

24 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();

25 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);

26

27 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; }

28 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

29 Reference NeHe Productions http://nehe.gamedev.net/http://nehe.gamedev.net/


Download ppt "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."

Similar presentations


Ads by Google