1 / 43

Chapter 12 Interactive Graphics

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.

stian
Download Presentation

Chapter 12 Interactive Graphics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 12 Interactive Graphics • Chih-Kuo Yeh

  2. Interactive Graphics

  3. Picking Direct Manipulation Demo

  4. Object Picking by Ray Tracing Object 2 Object 1 p2 p1 COP Center of Projection Window Coordinates

  5. OpenGL Geometric Pipline Vertex Coordinates x, y, z, w Object Coordinates Modelview Transformation Modelview Matrix Eye Coordinates Projection Transformation Projection Matrix Clip Coordinates Perspective Division Device Coordinates Viewport Transformation Viewport Transformation Part of OpenGL State Window Coordinates x,y

  6. OpenGL solution • Selection • Feedback API Output Devices Application Program Application Model Graphics System Input Devices Function Calls or Protocol Data

  7. Selection

  8. planets.cpp

  9. The Basic Steps ProcessSelection glSelectBuffer(…) glMatrixMode(GL_PROJECTION) glPushMatrix() Change render mode glMatrixMode(GL_PROJECTION) glPopMatrix()

  10. The Basic Steps Change render mode glRenderMode(GL_SELECT ) gluPickMatrix(…) RenderScene() glRenderMode(GL_RENDER )

  11. The Basic Steps RenderScene glInitNames() glPushName() glLoadName(…) Draw your object 1 glLoadName(…) Draw your object 2

  12. Selection Mode • GLintglRenderMode(GLenum mode); • Mode = • GL_RENDER (the default) • GL_SELECT • GL_FEEDBAC GL_RENDER 1 zmin glRenderMode() GL_SELECT zmax Yellow Cube glRenderMode(GL_RENDER) returns the number of hits while in SELECT mode

  13. 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);

  14. Picking in OpenGL Perspective View Volume Canonical View Volume Projection Transform Pick Matrix Transform Screen Coordinates

  15. The Hit Record • Each hit record consists of four items, in order. • The number of names on the name stack when the hit occurred. • Both the minimum and maximum window-coordinate z values • The contents of the name stack at the time of the hit, with the bottommost element first.

  16. Selection Buffer

  17. planets.cpp void RenderScene(void) { // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Save the matrix state and do the rotations glMatrixMode(GL_MODELVIEW); glPushMatrix(); // Translate the whole scene out and into view glTranslatef(0.0f, 0.0f, -300.0f); // Initialize the names stack glInitNames(); glPushName(0); // Name and draw the Sun glColor3f(1.0f, 1.0f, 0.0f); glLoadName(SUN); DrawSphere(15.0f); // Draw Mercury glColor3f(0.5f, 0.0f, 0.0f); glPushMatrix(); glTranslatef(24.0f, 0.0f, 0.0f); glLoadName(MERCURY); DrawSphere(2.0f); glPopMatrix(); // Draw Venus glColor3f(0.5f, 0.5f, 1.0f); glPushMatrix(); glTranslatef(60.0f, 0.0f, 0.0f); glLoadName(VENUS); DrawSphere(4.0f); glPopMatrix(); // Draw the Earth glColor3f(0.0f, 0.0f, 1.0f); glPushMatrix(); glTranslatef(100.0f,0.0f,0.0f); glLoadName(EARTH); DrawSphere(8.0f); glPopMatrix(); // Draw Mars glColor3f(1.0f, 0.0f, 0.0f); glPushMatrix(); glTranslatef(150.0f, 0.0f, 0.0f); glLoadName(MARS); DrawSphere(4.0f); glPopMatrix(); // Restore the matrix state glPopMatrix(); // Modelview matrix glutSwapBuffers(); }

  18. void MouseCallback(int button, int state, int x, int y) { if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) ProcessSelection(x, y); }

  19. ProcessSelection(int xPos, int yPos) void ProcessSelection(int xPos, int yPos) { GLfloat fAspect; // Space for selection buffer static GLuint selectBuff[BUFFER_LENGTH]; // Hit counter and viewport storage GLint hits, viewport[4]; // Setup selection buffer glSelectBuffer(BUFFER_LENGTH, selectBuff);

  20. // Get the viewport glGetIntegerv(GL_VIEWPORT, viewport); // Switch to projection and save the matrix glMatrixMode(GL_PROJECTION); glPushMatrix(); // Change render mode glRenderMode(GL_SELECT);

  21. 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 scene RenderScene(); // Collect the hits hits = glRenderMode(GL_RENDER);

  22. 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 matrix glMatrixMode(GL_PROJECTION); glPopMatrix(); // Go back to modelview for normal rendering glMatrixMode(GL_MODELVIEW);

  23. Hierarchical Picking

  24. Hierarchical Picking

  25. Moons.cpp

  26. Selection Buffer Set up Pick Buffer Selection Buffer: user specified integer array glSelectBuffer( int size, int *pBuffer );

  27. Initialize Name Stack stack pointer Name Stack Set up Pick Buffer glRenderMode( GL_SELECT ); glInitNames();

  28. Specify a Pick Volume Pick Volume Set up Pick Buffer Initialize Name Stack gluPickMatrix( x, y, 5.0, 5.0, viewport );

  29. Draw Object with IDs 1 0 0 100 Fill Selection Buffer Set up Pick Buffer Initialize Name Stack Specify a Pick Volume glPushName(100); glDrawRedRect();

  30. 1 0 0 100 2 0 0 100 200 Fill Selection Buffer Set up Pick Buffer Initialize Name Stack Specify a Pick Volume Draw Object with IDs glPushName(200); glDrawGreenRect();

  31. 1 0 0 100 2 0 0 100 200 Fill Selection Buffer Set up Pick Buffer Initialize Name Stack Specify a Pick Volume Draw Object with IDs glPushName(300); glDrawBlueRect();

  32. Post Processing Post Processing Set up Pick Buffer Initialize Name Stack Specify a Pick Volume Draw Object with IDs gRenderMode( GL_RENDER ); Do anything you want !!!

  33. Feedback

  34. select.cpp

  35. The Basic Steps ProcessSelection glSelectBuffer(…) glMatrixMode(GL_PROJECTION) glPushMatrix() Change render mode glMatrixMode(GL_PROJECTION) glPopMatrix() MakeSelection

  36. The Basic Steps Change render mode glRenderMode(GL_SELECT ) gluPickMatrix(…) RenderScene() glRenderMode(GL_RENDER )

  37. The Basic Steps RenderScene glInitNames() glPushName() glLoadName(…) glLoadName(…) glPassThrough(…) glPassThrough(…) Draw your object 2 Draw your object 1

  38. The Basic Steps MakeSelection glFeedbackBuffer(…) glRenderMode(GL_FEEDBACK ) RenderScene() glRenderMode(GL_RENDER ) Parse the feedback buffer

  39. The Feedback Buffer void glFeedbackBuffer(GLsizei size, GLenum type, GLfloat *buffer);

  40. Feedback Buffer Tokens

  41. Feedback Buffer Memory GL_3D type

  42. 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

More Related