400 likes | 537 Views
Introduction to OpenGL & HW1 Announcement. 劉軒銘 , 網媒所 碩二 ICG 2012 Fall. Introduction to OpenGL & HW1 Announcement. Introduction to openGL Coordinate system and transformations in openGL A simple sample Requirements of HW1 Others. 劉軒銘 , 網媒所 碩二 ICG 2012 Fall.
E N D
Introduction to OpenGL & HW1 Announcement 劉軒銘,網媒所碩二 ICG 2012 Fall
Introduction to OpenGL & HW1 Announcement • Introduction to openGL • Coordinate system and transformations in openGL • A simple sample • Requirements of HW1 • Others 劉軒銘,網媒所碩二 ICG 2012 Fall
Introduction to openGL • A Graphics rendering API introduced in 1992 by Silicon Graphics Inc • Now managed by Khronos Group • Provide the API functions to access graphics hardware directly • Cross-platform
Introduction to openGL • GLU (OpenGL Utility Library) • Part of OpenGL • Use the prefix of glu(ex: gluLookAt()) • GLUT (OpenGL Utility Toolkit) • Not officially part of OpenGL • hide the complexities of differing window system APIs. • Use the prefix of glut(ex:glutDisplayFunc())
Coordinate system World coordinates object coordinates
Coordinate system World coordinates Camera coordinates
Coordinate system World coordinates Clip coordinates Camera coordinates
Coordinate system Clip coordinates Camera coordinates World coordinates Model-view transformation Projection transformation Vertices Vertices • There are two matrix stacks. • ModelView matrix (GL_MODELVIEW) • Projection matrix (GL_PROJECTION) CTM
Coordinate system Clip coordinates Camera coordinates World coordinates Model-view transformation Projection transformation Vertices Vertices • 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….
openGL as state machine • Put a value into various states, then it will remain in effect until being changed. • e.g. glColor*() • Many state variables are enabled or disabled with glEnable(), glDisable() • e.g. glEnable(GL_LIGHT0)
openGL as state machine glBegin(GL_LINES); RGB glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0, 1.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0, 1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glEnd(); (-1,1,0) (1,1,0) (-1,-1,0) (1,-1,0)
openGL as state machine Current Transformation matrix (CTM) glLoadIdentity(); glTranslatef( distance,0, 0); glRotatef(angleX, 1.0, 0.0, 0.0); glRotatef(angleY, 0.0, 1.0, 0.0); glBegin(GL_QUADS); glVertex3f(-7, 7, 7); … glEnd(); (-1,1,0) (1,1,0) (-1,-1,0) (1,-1,0)
Matrix in OpenGL • Mantain matrix stack • glPushMatrix() : used to save current stack • glPopMatrix() : used to restore previous stack x glRotatef glPopMatrix() glPushMatirx()
Coordinate system glBegin(TYPE) glVertex3f(x,y,z) ……. glEnd() World coordinates
Transformations : Model-View glMatrixMode(GL_MODELVIEW) //Affine Transformation glRotatef(angle,direction) glTranslatef(displacement) glScalef(scale coefficients) glBegin(TYPE) glVertex3f(x,y,z) …………………. glEnd() World coordinates Camera coordinates V’<- [R][T][S] V
Transformations : Model-View glMatrixMode(GL_MODELVIEW) gluLookAt({eye},{look at},{{up}) //Affine Transformation glBegin(TYPE) glVertex3f(v0) glEnd() World coordinates Camera coordinates
Transformations : Projection glMatrixMode(GL_PROJECTION) //projection Transformation glOrtho(clipping volume) gluPerspective(fov,ciippingVolume) glFrustrum(clipping volume) glMatrixMode(GL_MODELVIEW) gluLookAt //Affine Transformation glBegin(TYPE) glVertex3f(v0) ……….. glEnd() World coordinates Image coordinates Camera coordinates
Notice!! Each affine and projective transformation is implemented as a matrix, the order of function calls plays an important role. Matrix multiplication Code: glMatrixMode(GL_PROJECTION) gluperspective gluperspective glRotatef glScalef glTranslatef glMatrixMode(GL_MODELVIEW) glRotatef = V gluLookAt glVertex3f glTranslatef glScalef gluLookAt glBegin glVertex3f …… glEnd
A Simple Example #include <GL/glut.h> int main(intargc, char** argv) { glutInit(&argc, argv); glutInitWindowSize(600, 800); glutInitWindowPosition(500, 500); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutCreateWindow("ICG simple"); glutReshapeFunc(GL_reshape); glutDisplayFunc(GL_display); glutMainLoop(); return 0; }
A Simple Example void GL_display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glBegin( GL_LINES ); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0, 1.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0, 1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glEnd(); glutSwapBuffers(); }
A Simple Example void GL_display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glBegin( GL_LINES ); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0, 1.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0, 1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glEnd(); glutSwapBuffers(); }
A Simple Example void GL_display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glBegin( GL_LINES ); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0, 1.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0, 1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glEnd(); glutSwapBuffers(); }
A Simple Example void GL_display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glBegin( GL_LINES ); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0, 1.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0, 1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glEnd(); glutSwapBuffers(); }
A Simple Example void GL_display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glBegin( GL_LINES ); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0, 1.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0, 1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glEnd(); glutSwapBuffers(); }
A Simple Example 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); }
A Simple Example 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); }
A Simple Example 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); }
A Simple Example #include <GL\glut.h> void GL_reshape(GLsizei w, GLsizei h); void GL_display(); int main(intargc, char** argv) { glutInit(&argc, argv); glutInitWindowSize(600, 800); glutInitWindowPosition(500, 500); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutCreateWindow("ICG simple"); glutReshapeFunc(GL_reshape); glutDisplayFunc(GL_display); glutMainLoop(); return 0; } 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); } void GL_display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glBegin( GL_LINES ); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0, 1.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0, 1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glEnd(); glutSwapBuffers(); }
Requirements of HW1 Due : 10/24 You are required to do the following in homework #1: (6 points ) 1. Load a 3D model file of TRI format. Draw it in wireframe mode and view it in perspective view.(2 pts) 2. Implement basic transformations such as rotation, translation, scaling and shearing.(3 pts) 3. Object rotation around x , y and z-axis.(4 pts) ** You have to make these transformation matrix by yourself in problem 2 and 3 ** glTranslate , glRotate, glScale are not allowed 4. Clipping implementation. Try to show the difference between clipping and non-clipping. (5 pts) ** glOrtho, gluPerspectiveand glFrustumare not allowed in problem 4 5. Bonus. Any kind of effort you made more than requirements above.(6 pts)
Notice!! How does openGL set its clipping volume for window?? y Using homogeneous coordinates clipping box clipping box is enclosed by: x = 1,x=-1,y=1,y=-1,z=1,z=-1 x screen at z = 0 z x y nonhomogeneous coordinate: , w =/= 1 z w
Notice!! How does openGL set its clipping volume for window?? y Using homogeneous coordinates clipping box clipping box is enclosed by: x = 1,x=-1,y=1,y=-1,z=1,z=-1 x screen at z = 0 z x/w y/w homogeneous coordinate: z/w 1
Notice!! How does openGL set its clipping volume for window?? Using homogeneous image coordinates clipping box You’d better set your own homogeneous image coordinates clipping box different from openGL’s. ※Here the homogeneous image coordinates clipping volume is different from the One described in textbook , which is refer to camera coordinates clipping volume.
Others Glut window functions: glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(800, 600); glutInitWindowPosition(100, 100); glutCreateWindow(“Name"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMouseFunc(mouse); glutMainLoop();
Others Interactive setting: void keyboard(unsigned char key, int x, int y){ switch(key){ ……… case 'a': glClearColor(1., 1., 1., 1.); startPosX -= 10; glutPostRedisplay(); break; } }
Others Interactive setting: void mouse(int button, int state, int x, int y){ static float preX, preY; switch(state){ case GLUT_DOWN: preX = x; preY = y; glutPostRedisplay(); break; ………………. } }
Others Setting up openGL(visual C++): 1.download glut-3.7.6-bin.zip or similar - GLUT for Win32 dll, lib and header file - there. 2.Make a directory called "GL" under C:\Program Files\Microsoft Visual Studio 9.0\VC\include or similar 3.Copy the header files into C:\programme\microsoft visual studio\vc98\include\GL4.Copy all *.lib files into C:\programme\microsoft visual studio\vc98\Lib 5.Copy all .dll files into C:\Windows\System326.In the Menu of VC++ go through to -> project -> settings -> Link and add (do not remove the others!) the following libraries to the Object/libary modules line:glut32.lib glu32.lib opengl32.lib glaux.lib
Reference 1.Introduction to OpenGL PPT, Presented by Chung-Lin WenICG 2006 Fall 2.Interactive Computer Graphics – A Top-Down Approach Using openGL