29
Hwk1- A Tutorial of OPENGL ver2.0

Hwk1- A Tutorial of OPENGL ver2.0

  • Upload
    sierra

  • View
    73

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

OPENGL

Hwk1-A Tutorial of OPENGLver2.0

We will introduceDraw a window by OpenGLDraw one/many polygons by OpenGLRotate/Translate MatrixPush/PopMatrixHomework1Please draw a walking robot!

TAs will offerEx1a:Rotate/Translate/Push/PopMatrixEx1b:draw polygon/Keyboard functionFirst open Ex1bA 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;}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 operationsglutInitWindowSize(800, 800);glutInitWindowPosition(0, 0);glutCreateWindow("Homework_1b");glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);glutDisplayFunc(display);glutReshapeFunc(reshape);glutKeyboardFunc(keyboard);glutSpecialFunc(specialkey);

void glutInit(int *argcp, char **argv); Initializing the GLUT libraryShould be called before any other GLUT funcitonshttp://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_INDEXGLUT_SINGLE / GLUT_DOUBLEGLUT_DEPTH / GLUT_STENCIL / GLUT_ACCUMhttp://www.opengl.org/resources/libraries/glut/spec3/node12.htmlWhat is Display?void glutDisplayFunc(void (*func)(void)); Automatically called when the windows needs redraw (focus on/in, etc)draw function handlercall glutPostRedisplay() if you need to redraw immediatelyhttp://www.opengl.org/resources/libraries/glut/spec3/node46.htmlWhat 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();Indicate the color(R,G,B)From glBegin() to glEnd():Indicate every points position/color(if need)/normal,etcglColor3f(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();

Next 1a

AddingglutTimerFunc(TimerFunction);glRotatef(45, 0.0,1.0,0.0);glTranslatef(0,0,500);

drawCooridinate() is another function just draw the cooridinates

Basic prior:Everything in OpenGL is represented as a matrix operation.glMatrixMode(): Decide which matrix is now operated.

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

glTranslatef( 0, 0, -1 );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);void glPushMatrix();Push current matrix into matrix stack.Void glPopMatrix();Pop matrix from matrix stackThese 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

To more detailsStep by step

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

glTranslatef(0,0,-1000);glutSolidTeapot(50.0);

Current MatrixQuestionIf I want to back to the initial matrix, I can1. 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 Matrix2. 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();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);

Hw1bvoid 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;}Hwk1 RequirementPlease 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 / translateReferenceNeHe Productions http://nehe.gamedev.net/