GLART week 7: Navigation

Assigment: keep working on your assignment from last week. Reading: Redbook pages 150-154: at the end of Chapter 3 (Viewing) the section titled "Reversing or Mimicking Transformations" Examples: GLART_7_modelviewer_new.java (Model Viewer with triangle picking) Some sample Models: glart2010_models.zip NOTES: ================================================================= To interact with a scene we need to be able to convert the scene, or "world space" coordinates into screen coordinates, and vice-versa. I have wrapped much of the code to do this into JOApp functions that are demonstrated in the example GLART_7_modelviewer_new.java: JOApp.getWorldCoordsAtScreen(int x, int y) converts a screen x,y coordinate into a world space position (at Z=0) JOApp.project(float x, float y, float z, float[] resultf) calls gluProject() to convert a point in your 3D world to a point on the screen JOApp.unProject(float x, float y, float z, float[] resultf) calls gluUnProject() to convert a point on the screen to a point in the 3D world And in JOMesh.java: JOMesh.getTriangle() compares the vertex screen positions to the mouse position to see if any triangles are under the mouse.if it finds more than one triangle at the mouse position, it picks the one that is closer to the viewpoint. JOMesh.projectVerts(GL_Mesh obj, float[][] modelMatrix, float[][] projectionMatrix, int[] viewport) calls gluProject() on every vertex in the mesh, then stores the resulting screen coordinates into the GL_Vertex.posS (GL_Vertex.pos holds the world coordinate of the vert, posS holds the screen coordinate). Here are the OpenGL functions that are called by the functions above. glu.gluProject() "Project" a point (in the 3D world space) onto the screen. As the name implies, projecting is like projecting a movie onto a screen The original 3D scene that was filmed is converted into a flat image on the screen. The 3D "world space" is converted into a "screen space". OpenGL does projection internally every time it renders a frame We can use gluProject() to manually repeat the projection math so that we can find where a point in our world space ends up on the screen. gluProject needs the current matrices: modelView -- defines how the scene has been rotated, translated or scaled projection -- defines the perspective of the scene viewport -- defines where the rendered image is placed on the screen Returns the screen space position of the point. Ie. for an 800x600 window, the x value can be between 0-800 the y value between 0-600 and the z value will be between 0-1 The Z is used only to say which objects in the scene are closer to the viewpoint. glu.gluUnProject() Converts a screen x,y point back to a world space 3D coordinate. The exact opposite of project, gluUnProject() can be used to place an object in the scene at the mouse position. You could use this function to "drag" an object in the scene using the mouse. Needs the current matrices: modelView -- defines how the scene has been rotated, translated or scaled projection -- defines the perspective of the scene viewport -- defines where the rendered image is placed on the screen Because the screen is flat, we have only x,y coordinates to work with We need to supply a Z screen depth value for unproject to work with. We can pick a convenient position, such as the origin, gluProject() that point to the screen, then record the screen Z depth of that point. We then pass that Z depth value into gluUnProject() as our Z value, to place our object at Z = 0 in the world space. glGetFloat() Retrieves values from OpenGL, such as the current matrices. Pass in parameters to say what matrix you want to retrieve: GL_MODELVIEW_MATRIX GL_PROJECTION_MATRIX GL_VIEWPORT