Lecture07 OpenGL Interaction
Lecture07 OpenGL Interaction
OpenGL
Input and Interaction
Todays lecture
•What is OpenGL?
•How does it work?
– Primitives: Points, vertices, lines, polygons
– Input and interaction
•Code examples
• Industry Standard
• Stable
– Well controlled specification
– Backward compatibility
• Reliable and portable
– Consistent visual display results
OpenGL advantages
• Evolving
– Extension mechanism
(http://www.opengl.org/registry/)
• Scalable
– Run on systems ranging from cell phones and
PDA:s to PCs, workstations, and
supercomputers
• Documentation
– Numerous books, web material, and sample
code is readily available
– http://www.opengl.org/documentation/red_book/
State machine
http://www.opengl.org
Primitives in OpenGL
glBegin(GL_LINES);
glVertex2f(-0.5,-0.5); // 1
glVertex2f( 0.5,-0.5); // 2
glVertex2f( 0.5, 0.5); // 3
glVertex2f(-0.5, 0.5); // 4
glEnd();
GL_LINE_STRIP
glBegin(GL_LINE_STRIP);
glVertex2f(-0.5,-0.5); // 1
glVertex2f( 0.5,-0.5); // 2
glVertex2f( 0.5, 0.5); // 3
glVertex2f(-0.5, 0.5); // 4
glEnd();
GL_LINE_LOOP
glBegin(GL_LINE_LOOP);
glVertex2f(-0.5,-0.5); // 1
glVertex2f( 0.5,-0.5); // 2
glVertex2f( 0.5, 0.5); // 3
glVertex2f(-0.5, 0.5); // 4
glEnd();
GL_TRIANGLES
glBegin(GL_TRIANGLES);
glVertex3f(-0.5,-0.5,0.0); // 1
glVertex3f(0.5,-0.5,0.0); // 2
glVertex3f(0.25, 0.5,0.0); // 3
glVertex3f(-0.5, 1.25, 0.0); // 4
glVertex3f(0.5, 1.25, 0.0); // 5
glVertex3f(0.25, 0.75, 0.0); // 6
glEnd();
GL_TRIANGLE_STRIP
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(-0.5,-0.5,0.0); // 1
glVertex3f(0.5,-0.5,0.0); // 2
glVertex3f(0.25, 0.5,0.0); // 3
glVertex3f(0.5, 0.75, 0.0); // 4
glEnd();
Front and back rendering
// culling
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
Color
glColor3f(1.0,0.0,0.0); // red
glBegin(GL_TRIANGLES);
glVertex3f(…);
…
glEnd();
Color per vertex
glBegin(GL_TRIANGLES);
glColor3f(…);
glVertex3f(…);
glColor3f(…);
glVertex3f(…);
…
glEnd();
A very simple program (Primitives.cpp)
#include <GL/glut.h>
24
Primitives.cpp
void initGL() {
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0 );
glMatrixMode ( GL_MODELVIEW );
}
void draw() {
float point2[2] = {0.5, 0.75};
glClear( GL_COLOR_BUFFER_BIT );
glColor3f( 1.0, 1.0, 1.0 );
glBegin( GL_POLYGON );
glVertex2f( 0.25, 0.25 );
glVertex2f( 0.75, 0.5 );
glVertex2fv( point2 );
glEnd();
glFlush();
}
26
Primitives.cpp
27
Primitives.cpp
Coding time!
28
Input and interaction
30
Callback functions
31
Keyboard interaction
32
Keyboard interaction
//keyboard callback
void keyboard(unsigned char key, int x, int y) {
switch(key){
case 'r':
r=1.0; g=0.0; b=0.0;
break;
case 'g':
r=0.0; g=1.0; b=0.0;
break;
}
glutPostRedisplay();
}
33
In the display function
void draw() {
...
//draw a polygon
glColor3f( r, g, b );
glBegin( GL_POLYGON );
glVertex2f( 0.25, 0.25 );
glVertex2f( 0.75, 0.5 );
glVertex2fv( point2 );
glEnd();
...
}
34
Keyboard interaction
35
Mouse interaction
glutMouseFunc(mouse);
glutPassiveMotionFunc(passiveMotion);
glutMotionFunc(motion);
...
}
36
Mouse interaction
// mouse callback
void mouse(int button, int state, int x, int y) {
if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ){
printf(“Left mouse button pressed“);
}
}
37
Mouse interaction
38
In the display function
void draw() {
...
//draw a point
glColor3f( 0.8, 0.8, 0.2 );
glPointSize(10.0);
glBegin( GL_POINTS );
glVertex2f( pos_x, pos_y );
glEnd();
...
}
39
Mouse interaction
Coding time!
40
Idle tasks
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutPassiveMotionFunc(motion);
glutIdleFunc(idle);
...
}
41
Idle tasks
// idle callback
void idle() {
t+=1;
glutPostRedisplay();
}
42
Display function
void draw() {
...
glColor3f( 0.5+0.5*sin(0.001*t),
0.3,
0.5+0.5*cos(0.001*t));
//draw a polygon
glBegin( GL_POLYGON );
glVertex2f( 0.25, 0.25 );
...
glEnd();
...
} Coding time!
43
OpenGL in GUI applications
44
GLUI
(http://www.cs.unc.edu/~rademach/glui/)
• Multi-platform
(Windows, OS X, Linux)
• Open Source
• Large feature set
• OpenGL supported
through wxGLCanvas
class.
Qt (http://qt.nokia.com/)
• Multi-platform (Windows,
OS X, Linux)
• Open Source (since 2008)
• Large feature set
• OpenGL support.
Questions so far…?
Interaction with more degrees
of freedom
Haptic interfaces provice multiple degrees of freedom
and force feedback. Enables exciting interaction
possibilities…
Cybergrasp