Upload
stian
View
61
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Chapter 12 Interactive Graphics. Chih-Kuo Yeh. Interactive Graphics. Picking. Direct Manipulation Demo. Object Picking by Ray Tracing. Object 2. Object 1. p2. p1. COP Center of Projection. Window Coordinates. OpenGL Geometric Pipline. Vertex Coordinates x, y, z, w. - PowerPoint PPT Presentation
Citation preview
Chapter 12
Interactive Graphics
Chih-Kuo Yeh
Interactive Graphics
Picking
Direct Manipulation Demo
Object Picking by Ray Tracing
p1
COPCenter of Projection
p2
WindowCoordinates
Object 1Object 2
OpenGL Geometric Pipline
ModelviewTransformation
Vertex Coordinatesx, y, z, w
Object Coordinates
Eye Coordinates
Clip Coordinates
Device Coordinates
Window Coordinates x,y
ProjectionTransformation
PerspectiveDivision
ViewportTransformation
Modelview Matrix
Projection Matrix
Part of OpenGL State
ViewportTransformation
OpenGL solution
SelectionFeedback
ApplicationModel
ApplicationProgram
GraphicsSystem
OutputDevices
InputDevices
API
Function Callsor Protocol
Data
Selection
planets.cpp
The Basic Steps
glSelectBuffer(…)
ProcessSelection
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glMatrixMode(GL_PROJECTION)
glPopMatrix()
Change render mode
The Basic Steps
glRenderMode(GL_SELECT )
glRenderMode(GL_RENDER )
RenderScene()
gluPickMatrix(…)
Change render mode
The Basic Steps
glInitNames()
glPushName()
glLoadName(…)
RenderScene
Draw your object 1
glLoadName(…)
Draw your object 2
Selection Mode
GLint glRenderMode(GLenum mode);Mode = 1. GL_RENDER (the default)2. GL_SELECT3. GL_FEEDBAC
glRenderMode()
GL_RENDER
GL_SELECT
1
zmin
zmax
Yellow Cube
glRenderMode(GL_RENDER) returns the number of hits while in SELECT mode
Picking
void gluPickMatrix(GLdouble x, GLdouble y, GLdouble width, GLdouble height, GLint viewport[4]);
glGetIntegerv(GL_VIEWPORT, viewport);
gluPickMatrix(xPos, viewport[3] – yPos + viewport[1], 2,2, viewport);
Picking in OpenGL
y
xz
z
x
y
Plane z= -1
Plane z= +1
DOP
z
x
y
Perspective View Volume Canonical View Volume
Projection
TransformPick MatrixTransform
Screen Coordinates
The Hit Record
Each hit record consists of four items, in order.1. The number of names on the name stack when
the hit occurred.2. Both the minimum and maximum window-
coordinate z values 3. The contents of the name stack at the time of
the hit, with the bottommost element first.
Selection Buffer
planets.cpp
void RenderScene(void){
// Clear the window with current clearing colorglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Save the matrix state and do the rotationsglMatrixMode(GL_MODELVIEW);glPushMatrix();
// Translate the whole scene out and into viewglTranslatef(0.0f, 0.0f, -300.0f);
// Initialize the names stackglInitNames();glPushName(0);
// Name and draw the SunglColor3f(1.0f, 1.0f, 0.0f);glLoadName(SUN);DrawSphere(15.0f);
// Draw MercuryglColor3f(0.5f, 0.0f, 0.0f);glPushMatrix();glTranslatef(24.0f, 0.0f, 0.0f);glLoadName(MERCURY);DrawSphere(2.0f);glPopMatrix();
// Draw VenusglColor3f(0.5f, 0.5f, 1.0f);glPushMatrix();glTranslatef(60.0f, 0.0f, 0.0f);glLoadName(VENUS);DrawSphere(4.0f);glPopMatrix();
// Draw the EarthglColor3f(0.0f, 0.0f, 1.0f);glPushMatrix();glTranslatef(100.0f,0.0f,0.0f);glLoadName(EARTH);DrawSphere(8.0f);glPopMatrix();
// Draw MarsglColor3f(1.0f, 0.0f, 0.0f);glPushMatrix();glTranslatef(150.0f, 0.0f, 0.0f);glLoadName(MARS);DrawSphere(4.0f);glPopMatrix();
// Restore the matrix stateglPopMatrix(); // Modelview matrix
glutSwapBuffers();}
void MouseCallback(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)ProcessSelection(x, y);
}
ProcessSelection(int xPos, int yPos)
void ProcessSelection(int xPos, int yPos){
GLfloat fAspect;
// Space for selection bufferstatic GLuint selectBuff[BUFFER_LENGTH];
// Hit counter and viewport storageGLint hits, viewport[4];
// Setup selection bufferglSelectBuffer(BUFFER_LENGTH, selectBuff);
// Get the viewportglGetIntegerv(GL_VIEWPORT, viewport);
// Switch to projection and save the matrixglMatrixMode(GL_PROJECTION);glPushMatrix();
// Change render modeglRenderMode(GL_SELECT);
glLoadIdentity();gluPickMatrix(xPos, viewport[3] - yPos + viewport[1], 2,2, viewport);
// Apply perspective matrix fAspect = (float)viewport[2] / (float)viewport[3];gluPerspective(45.0f, fAspect, 1.0, 425.0);
// Draw the sceneRenderScene();
// Collect the hitshits = glRenderMode(GL_RENDER);
GLuint nErr = glGetError();// If a single hit occurred, display the info.if(hits == 1)
ProcessPlanet(selectBuff[3]);else
glutSetWindowTitle("Nothing was clicked on!");
// Restore the projection matrixglMatrixMode(GL_PROJECTION);glPopMatrix();
// Go back to modelview for normal renderingglMatrixMode(GL_MODELVIEW);
Hierarchical Picking
Hierarchical Picking
Moons.cpp
Selection Buffer
glSelectBuffer( int size, int *pBuffer );
Set up Pick Buffer
Selection Buffer: user specified integer array
Name Stack
stack pointer
Set up Pick Buffer
Initialize Name Stack
glRenderMode( GL_SELECT );glInitNames();
Pick Volume
gluPickMatrix( x, y, 5.0, 5.0, viewport );
Initialize Name Stack
Set up Pick Buffer
Specify a Pick Volume
Fill Selection Buffer
glPushName(100);1 0 0 100
Specify a Pick Volume
Initialize Name Stack
Set up Pick Buffer
Draw Object with IDs
100
glDrawRedRect();
Fill Selection Buffer
glPushName(200);1 0 0 100 2 0 0 100 200
Specify a Pick Volume
Draw Object with IDs
Initialize Name Stack
Set up Pick Buffer
200100
glDrawGreenRect();
Fill Selection Buffer
1 0 0 100 2 0 0 100 200
100200300
Specify a Pick Volume
Draw Object with IDs
Initialize Name Stack
Set up Pick Buffer
glPushName(300);
glDrawBlueRect();
Post Processing
Do anything you want !!!
Specify a Pick Volume
Draw Object with IDs
Initialize Name Stack
Set up Pick Buffer
Post Processing
gRenderMode( GL_RENDER );
Feedback
select.cpp
The Basic Steps
glSelectBuffer(…)
ProcessSelection
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glMatrixMode(GL_PROJECTION)
glPopMatrix()
Change render mode
The Basic Steps
glRenderMode(GL_SELECT )
glRenderMode(GL_RENDER )
RenderScene()
gluPickMatrix(…)
Change render mode
The Basic Steps
glInitNames()
glPushName()
RenderScene
glLoadName(…)
Draw your object 1
glPassThrough(…)
glLoadName(…)
glPassThrough(…)
The Basic Steps
glRenderMode(GL_FEEDBACK )
glRenderMode(GL_RENDER )
RenderScene()
glFeedbackBuffer(…)
MakeSelection
Parse the feedback buffer
The Feedback Buffer
void glFeedbackBuffer(GLsizei size, GLenum type, GLfloat *buffer);
Feedback Buffer Tokens
Feedback Buffer Memory
GL_3D type
Feedback Buffer Memory
[0] GL_PASS_THROUGH_TOKEN[1] User defined name[2] GL_POLYGON_TOKEN[3] Number of vertices[4] x[5] y See MakeSelection(int nChoice) for detail