Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
OpenGL Introduction and HW1 Guide Speaker: Yu Tu CSIE M2 This guide is talking about… 1.Part of computer graphics 2.OpenGL introduction 3.Sample codes discussion : line.cpp, robot.c, dance.c Part of computer graphics Computer Graphics Pipeline Model in Graphics - object observation Model in Graphics - coordinate system World coordinate Image coordinate Camera coordinate OpenGL Introduction What is OpenGL • A Graphics rendering API introduced in 1992 by Silicon Graphics Inc • Produces high-quality color images composed of geometric and image primitives • Cross-platform Related APIs Application program GLUT AGL,WGL,GLX X Window, Windows, Mac OSX GLU GL Software and/or hardware OpenGL Utility Toolkit (GLUT) • Visual Studio 2010/2008, Visual C++ 2010/2008 • http://www.xmission.com/~nate/glut.html • Add glut32.dll to • Add glut32.lib to • Add glut.h to C:\Windows\SysWOW64 (System32) C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib (9.0) C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\GL (9.0) HelloWorld #include <GL/glut.h> void GL_display(); void GL_reshape(GLsizei w, GLsizei h); void main(void) { glutCreateWindow(“HelloWorld"); glutDisplayFunc(GL_display); glutReshapeFunc(GL_reshape); glutMainLoop(); } HelloWorld void GL_display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glBegin( GL_LINES ); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 0.0f); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0, 1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 0.0f); glEnd(); glFlush(); } Primitives HelloWorld void GL_display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glBegin( GL_LINES ); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 0.0f); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0, 1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 0.0f); glEnd(); glFlush(); } HelloWorld void GL_reshape(GLsizei w, GLsizei h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-2.0f, 2.0f, -2.0f, 2.0f, -2.0f, 2.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } Now that you have done your first OpenGL Program, let’s talk about something advanced. Matrix in OpenGL • • There are two matrix stacks. • ModelView matrix (GL_MODELVIEW) • Projection matrix (GL_PROJECTION) When we call functions of transformation, we should change to the appropriate matrix stack first. • • glMatrixMode(GL_MODELVIEW); • //now we are in modelview matrix stack! • //do modelview transformation here….. glMatrixMode(GL_PROJECTION); • //now we are in projection matrix stack! • //do projection transformation here…. Vertex ModelView Projection Projection Matrix • Orthographic Projection glOrtho( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far ) gluOrtho2D( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top) Projection Matrix Perspective Projection gluPerspective( GLdouble fovy, GLdouble aspect, GLdouble near, GLdouble far ); glFrustum( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far ); ModelView Matrix • Modeling Transformation • • Perform rotate, translate, scale and combinations of these transformations to the object. Viewing Transformation • To positioning and aiming the camera Modeling Transformations glTranslatef(x, y, z) Multiplies current matrix by a matrix that moves an object by x,y,z glTranslatef( 0, 0, -1) Modeling Transformations glRotatef(angle, x, y, 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 glRotatef( 45.0, 0, 0, 1) Modeling Transformations glScalef (x, y, z) Multiplies current matrix by a matrix that scales an object along axes. glScalef( 2.0, -0.5, 1) Viewing Transformations gluLookAt (eyex, eyey, eyez, atx, aty, atz, upx, upy, upz ); Matrix Order Code: glMatrixMode(GL_ PROJECTION) gluPerspective glMatrixMode(GL_ MODELVIEW) gluLookAt glRotatef glTranslatef glScalef glBegin glVertex3f glEnd Matrix Stack: Matrix Multiplication glVertex3f glScalef gluPerspective gluLookAt glRotatef glTranslatef glRotatef gluLookAt gluPerspective glTranslatef glScalef glVertex3f = Vi Matrix Order The order of transformations is critical. glRotatef(45.0, 0,0,1 ); glTranslatef( 1,0,0 ); drawObject(); glTranslatef( 1,0,0 ); glRotatef(45.0, 0,0,1 ); drawObject(); Interactive Setting and Display Glut Window functions: glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(800, 600); glutInitWindowPosition(100, 100); glutCreateWindow(“Name"); glutDisplayFunc(display); glutReshapeFunc(reshape); Interactive setting functions: glutKeyboardFunc(keyboard); glutMouseFunc(mouse); glutMotionFunc(motion); Wake up for The most important topic. Mantain matrix stack • glPushMatrix() : save the current matrix • glPopMatrix() : restore the saved matrix x glScalef glPushMatirx() glPopMatrix() Heirarchy Modeling Body head Left hand Finger 1 Right hand Finger 2 Left leg Righ t leg Heirarchy Modeling Matrix Stack: Current Matrix Stack Heirarchy Modeling Push Matrix Stack: Current Matrix Stack glPushMatrix(); Heirarchy Modeling Push glTranslatef(); Matrix Stack: x Current Matrix Stack Heirarchy Modeling Push Matrix Stack: Current Matrix Stack DrawBody(); Heirarchy Modeling Push Matrix Stack: Current Matrix Stack glPushMatrix(); Push Heirarchy Modeling Push Matrix Stack: glRotatef(); Push x Current Matrix Stack Heirarchy Modeling Push Matrix Stack: glTranslatef(); Push x Current Matrix Stack Heirarchy Modeling Push Matrix Stack: Current Matrix Stack DrawHead(); Push Heirarchy Modeling Push Matrix Stack: glPopMatrix(); Push Pop Current Matrix Current Matrix Stack Heirarchy Modeling Push Matrix Stack: glPushMatrix(); Push Pop Current Matrix Stack Push Heirarchy Modeling Push Matrix Stack: glRotatef(); Push Pop x Current Matrix Stack Push Heirarchy Modeling Push Matrix Stack: DrawHand(); Push Pop Current Matrix Stack Push Heirarchy Modeling Push Matrix Stack: glPushMatrix(); Push Pop Current Matrix Stack Push Push Heirarchy Modeling Push Matrix Stack: glTranslatef(); Push Pop Current Matrix Stack x Push Push Heirarchy Modeling Push Matrix Stack: DrawFinger(); Push Pop Current Matrix Stack Push Push Heirarchy Modeling Push Matrix Stack: glPopMatrix(); Push Pop Push Push Pop Current Matrix Current Matrix Stack Heirarchy Modeling Push Body Pop Push Push Push Left hand head Pop Right hand Pop Pop Push Push Finger 1 Finger 2 Pop Pop Push Push Pop Pop Left leg Righ t leg Contratulations ! You are well prepared for our homework now! Time for Sample Codes Robot.c glPushMatrix(); glTranslatef (-1.0, 0.0, 0.0); glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix(); glScalef (2.0, 0.4, 1.0); glutWireCube (1.0); glPopMatrix(); glPushMatrix(); glTranslatef (1.0, 0.0, 0.0); glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix(); glScalef (2.0, 0.4, 1.0); glutWireCube (1.0); glPopMatrix(); glPopMatrix(); glPopMatrix(); Dance.c • • • • • • • • • • • BOOL Open_CD(void) BOOL Stop_CD(void) BOOL Close_CD(void) BOOL Play_CD(int bTrack) int GetUnit(FILE* input) void GetMovie(void) void PutIntoFile(FILE* input, int item) void SetNull(FILE* input) void SetRecord() void ZoomChange() void ColorChange(int mode) • • • • • • • • • void init(void) void reshape (int w, int h) void baseroom(void) void ManSet() void WomanSet() void display(void) void keyboard (unsigned char key, int x, int y) int OnTimer(int value) int main(int argc, char** argv) Dance.c • • • • • • • • • • • BOOL Open_CD(void) Class Music{ BOOL Stop_CD(void) … BOOL Close_CD(void) . BOOL}; Play_CD(int bTrack) • • int GetUnit(FILE* input) void GetMovie(void) Class Script{ void PutIntoFile(FILE* input, … int item) }; .input) void SetNull(FILE* void SetRecord() • Class Animation{ void ZoomChange() void ColorChange(int … }; mode) • • • • • • void ManSet() Class Component{ … }; void WomanSet() Class Actor{ … }; void init(void) void reshape (int w, int h) void baseroom(void) void display(void) void keyboard (unsigned char key, int x, int y) int OnTimer(int value) int main(int argc, char** argv) Q&A