English Ver.
Download
Report
Transcript English Ver.
Hwk1A 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…
1. Ex1a:Rotate/Translate/Push/PopMatrix
2. Ex1b:draw polygon/Keyboard function
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;
}
What is in the Main function?
glutInitWindowSize(800, 800);
glutInitWindowPosition(0, 0);
glutCreateWindow("Homework_1b");
glutInitDisplayMode(GLUT_DOUBLE
| GLUT_RGB | GLUT_DEPTH);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialkey);
• Windows
size/starting
position/name.
• Display mode(how
many color
channels? …etc)
• Tell the system
“which function”
handle the operations
• 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.ht
ml
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
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();
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();
• Indicate the
color(R,G,B)
• From glBegin() to
glEnd():
• Indicate every
points’
position/color(if
need)/normal,…et
c
Next 1a…
Adding
• glutTimerFunc(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,z
▫
http://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 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
To more details
• Step 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 Matrix
//
//
//
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
//
//
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
…
…
glTranslatef(0,0,-1000);
glutSolidTeapot(50.0);
Current Matrix
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);
Current Matrix
▫ Work, but stupid!
▫ 1a.glTranslatef(0,0,500);
▫
glRotatef(-45,0.0,1.0,0.0); …. Not good
• 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();
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);
Hw1b
void keyboard(unsigned char key, int x, int y){
switch(key){
case '2':
case '8':
case '4':
case '6':
//type 2, move forward (axis Z)
movement[selected][2]+=10.0;
break;
//type 8, move backward(axis Z)
movement[selected][2]-=10.0;
break;
//type 4, move left(axis X)
movement[selected][0]-=10.0;
break;
//type 6, move wight(axis X)
movement[selected][0]+=10.0;
……..
……..
case 27:
exit(0);
break;
}
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
Reference
• NeHe Productions http://nehe.gamedev.net/