OpenGL Picking and Quaternions Project Miscellaneous Topics.

Slides:



Advertisements
Similar presentations
Today Composing transformations 3D Transformations
Advertisements

Coordinate System.
Better Interactive Programs
CS123 | INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © 1/16 Deferred Lighting Deferred Lighting – 11/18/2014.
©Zachary Wartell, UNCC9/28/ :30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights.
3D Game Programming Geometric Transformations
Using GLU/GLUT Objects GLU/GLUT provides very simple object primitives glutWireCube glutWireCone gluCylinder glutWireTeapot.
Ray tracing. New Concepts The recursive ray tracing algorithm Generating eye rays Non Real-time rendering.
Understanding the graphics pipeline Lecture 2 Original Slides by: Suresh Venkatasubramanian Updates by Joseph Kider.
Graphics Pipeline.
2 COEN Computer Graphics I Evening’s Goals n Discuss the fundamentals of lighting in computer graphics n Analyze OpenGL’s lighting model n Show.
Computer Graphics(Fall 2003) COMS 4160, Lecture 7: OpenGL 3 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
OPENGL Return of the Survival Guide. Buffers (0,0) OpenGL holds the buffers in a coordinate system such that the origin is the lower left corner.
CS 4363/6353 BASIC RENDERING. THE GRAPHICS PIPELINE OVERVIEW Vertex Processing Coordinate transformations Compute color for each vertex Clipping and Primitive.
University of Sulaimani - School of Science - Computer Dept.
Computer Graphic Creator: Mohsen Asghari Session 2 Fall 2014.
3D Graphics Rendering and Terrain Modeling
When click!! (void) glRenderMode (GL_SELECT); glInitNames(); glPushName(0); glMatrixMode (GL_PROJECTION); glPushMatrix (); glLoadIdentity (); gluPickMatrix.
Chapter 12 Interactive Graphics Chih-Kuo Yeh. Direct Manipulation Demo.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Better Interactive Programs Ed Angel Professor of Computer Science, Electrical and Computer.
OpenGL (II). How to Draw a 3-D object on Screen?
Informationsteknologi Tuesday, November 6, 2007Computer Graphics - Class 41 Today’s class Input and interaction.
3D Rendering with JOGL Introduction to Java OpenGL Graphic Library By Ricardo Veguilla
Introduction to 3D Graphics John E. Laird. Basic Issues u Given a internal model of a 3D world, with textures and light sources how do you project it.
Hierarchical Transformations Hierarchical Models Scene Graphs
10/5/04© University of Wisconsin, CS559 Fall 2004 Last Time Compositing Painterly Rendering Intro to 3D Graphics Homework 3 due Oct 12 in class.
Picking. What is picking? Selecting an object on the screen What does this require? –Get Mouse Location –Compute what objects are rendered at the position.
1 Perception, Illusion and VR HNRS 299, Spring 2008 Lecture 19 Other Graphics Considerations Review.
19/4/ :32 Graphics II Syllabus Selection and Picking Session 1.
Mouse-Based Viewing & Navigation Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, November 3, 2003.
Technology and Historical Overview. Introduction to 3d Computer Graphics  3D computer graphics is the science, study, and method of projecting a mathematical.
Programmable Pipelines. Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
CSE 381 – Advanced Game Programming Basic 3D Graphics
CSC461 Lecture 11: Interactive Programs Contents and Objectives Picking Writing modes – XOR/Copy Rubberbanding Display list.
Open GL Programming Speaker: 彭任右 Date: 2005/10/3.
Programmable Pipelines. 2 Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
Image Synthesis Rabie A. Ramadan, PhD 2. 2 Java OpenGL Using JOGL: Using JOGL: Wiki: You can download JOGL from.
Buffers Textures and more Rendering Paul Taylor & Barry La Trobe University 2009.
Week 5 - Wednesday.  What did we talk about last time?  Project 2  Normal transforms  Euler angles  Quaternions.
Korea University Korea University Computer Graphics Laboratory Computer Graphics Laboratory Jung Lee, Chapter 13.
CSE 470: Computer Graphics. 10/15/ Defining a Vertex A 2D vertex: glVertex2f(GLfloat x, GLfloat y); 2D vertexfloating pointopenGL parameter type.
Homogeneous Form, Introduction to 3-D Graphics Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, October 20,
Computing & Information Sciences Kansas State University Lecture 20 of 42CIS 636/736: (Introduction to) Computer Graphics Lecture 21 of 42 William H. Hsu.
CAP4730: Computational Structures in Computer Graphics 3D Transformations.
1 Better Interactive Programs. 2 Objectives Learn to build more sophisticated interactive programs using ­Picking Select objects from the display Three.
Computer Graphics Bing-Yu Chen National Taiwan University.
Programmable Pipelines Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University.
OpenGL Selection. Three Selection Methods Color coding (OpenGL) Selection mode (OpenGL) Selection ray (generic)
OpenGL The Viewing Pipeline: Definition: a series of operations that are applied to the OpenGL matrices, in order to create a 2D representation from 3D.
Image Synthesis Rabie A. Ramadan, PhD 4. 2 Review Questions Q1: What are the two principal tasks required to create an image of a three-dimensional scene?
Advanced Computer Graphics Spring 2014 K. H. Ko School of Mechatronics Gwangju Institute of Science and Technology.
Advanced Viewing Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Friday, October 31, 2003.
Viewing and Transformation. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture.
Review on Graphics Basics. Outline Polygon rendering pipeline Affine transformations Projective transformations Lighting and shading From vertices to.
What are shaders? In the field of computer graphics, a shader is a computer program that runs on the graphics processing unit(GPU) and is used to do shading.
Selection Mode, Introduction to Widgets Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, October 6, 2003.
Introduction to OpenGL (INF 250) Veronika Solteszova et al., UiB Dept. of Informatics,
OpenGL LAB III.
- Introduction - Graphics Pipeline
Better Interactive Programs
3D Transformations Source & Courtesy: University of Wisconsin,
Modeling 101 For the moment assume that all geometry consists of points, lines and faces Line: A segment between two endpoints Face: A planar area bounded.
3D Graphics Rendering PPT By Ricardo Veguilla.
CS451Real-time Rendering Pipeline
Projection in 3-D Glenn G. Chappell
Better Interactive Programs
UMBC Graphics for Games
Chapter VII Rasterizer
Computer Graphics Practical Lesson 8
Computer Graphics 3Practical Lesson
Presentation transcript:

OpenGL Picking and Quaternions Project Miscellaneous Topics

Agenda GL_SELECT picking review Alternative picking methods  Based on the colour buffer  Based on the depth buffer Quaternions Bonus topics  Basic skybox  Texture-mapped text overview  Basic height-mapped terrain

Picking: GL_SELECT Concept:  the scene is rendered in a simplified way  objects have IDs  IDs of objects drawn inside the picking (view) volume are returned in a hit record Uses of IDs:  Each object has a unique name  At the time when an object is drawn, the name stack represents the object’s position in the hierarchical scene tree

GL_SELECT: Start a Pick glSelectBuffer(512, selectBuf); // buffer glRenderMode(GL_SELECT); // mode glInitNames(); // name stack // pick matrix glMatrixMode(GL_PROJECTION); glLoadIdentity(); GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); gluPickMatrix((GLdouble) screen_x, (GLdouble) (viewport[3] - screen_y), 2.0, 2.0, viewport); if ( // 3D picking) // setup standard perspective else // setup ortho perspective

GL_SELECT: Process Hits glFlush(); GLint hits = glRenderMode(GL_RENDER); pick_result result; int offset_to_cur_record = 0; int cur_stack_len; int min_depth = 0xFFFFFFFF; int cur_depth; for (int h = 0; h < hits; ++h) { cur_stack_len = *(selectBuf + offset_to_cur_record); cur_depth = *(selectBuf + offset_to_cur_record + 1);

GL_SELECT: Process Hits (Cont’d) if (cur_depth < min_depth) { min_depth = cur_depth; result.names.clear(); int i; for (i = 0; i < cur_stack_len; ++i) { if (i > 2) break; result.names.push_back( *(selectBuf + i offset_to_cur_record) ); } result.depth = (double) min_depth / 0xFFFFFFFF; } offset_to_cur_record += cur_stack_len + 3; }

GL_SELECT: Hierarchical IDs // in scene::draw(…) glPushMatrix(); // here: apply transforms // here: draw the current node for ( // children) { glPushName(i); // call draw(…) for the children glPopName(); } glPopMatrix(); } Notes:  It is also necessary to have a way to query the scene for a particular node, given a sequence of names  This is easy to do since each name is the number of the branch taken on the path from the root to the query node  In the code on the left, the root is not picked

Problems with GL_SELECT GL_SELECT (outrageous) slow down  ATI: (useful info)  NVIDIA: (evidence of the problem and forum drama only)  OpenGL 3.0: GL_SELECT deprecated? Xiachunyi host abuse hack  Insert empty glBegin(GL_LINES); glEnd(); after glPushName(…); Additional Technical Problem:  GL_SELECT does not respect per fragment operations; therefore incorrectly picks transparent objects.

Alternative: Colour-based Picking Most similar to GL_SELECT:  Instead of the special GL_SELECT render mode, render each object in a unique flat colour  Get the colour of the pixel under the cursor  Query for the object given the colour Need to make sure that the specified unique colours are actually drawn. Render to the back buffer (and don’t swap), so that the user can’t see the special rendering operation. No perspective changes are necessary.

Colour-based Picking: Colours From OpenGL FAQ:  Obtain the number of bits that can be used for each channel using glGetIntegerv;  Turn off features that affect colour and use GL_FLAT shading model;  Use glColor3ui() to set the colour;  Details at or.htm#0050 ; or.htm#0050

Colour-based Picking: Reading the Colours using glReadPixels: void glReadPixels(  GLint x,  GLint y,  GLsizei width,  GLsizei height,  GLenum format,  GLenum type,  GLvoid *pixels) ; Notes:  x, y are in r.t. to the lower left corner of the screen  width, length are the size of the area to be read  format is GL_RGB  type is GL_UNSIGNED_BYTE – the data type of the pixel data  pixels is the location to save the data Colour Picking Example: index.php?color1 index.php?color1

Alternative: Depth-based Picking Proposed here: depth-buffer/ depth-buffer/ Basic idea: RenderSceneForPick() { modify the view frustum to just cover the picked pixel; scissor out everything except the picked pixel; for each visible object { clear depth buffer; render object; read depth buffer at pick location; if (depth value != clear value) { add object to list of picked objects; } } sort list of picked objects near to far; }

Depth-based Picking: Performance Considerations Scissoring everything out except the one pixel:  Makes drawing objects and clearing the buffers faster  All vertices are processed but only a few of the fragments Using the smallest possible view frustum:  Implies the need for culling with bounding volumes, etc.

Picking Methods Comparison MethodProsConsWill buy again? GL_SELECT Easy to implement Can’t deal with alpha test Hell, no Colour-based Easy to implement The above + possible trickyness with colours Yes Depth-based Handles anything Multiple readPixels(); Culling? Maybe CPU intersections Fancy data structures? No GPU => no per- fragment operations Maybe not

Quaternions Practical Introduction

Motivation Quaternions as representation of rotations  vs. Matrices: easier to construct use fewer numbers (4 vs. 9) require fewer multiplications  Make interpolation for smooth rotations easier Rotations are specified using unit quaternions

Basic Quaternion Math A quaternion is like a complex number with 3 complex components: q = r + a*i + b*j +c*k; i^2 = -1 and so on i*j = -j*i = k Addition and Multiplication: follow basic algebra rules Magnitude: obvious generalization of complex number magnitude Inverse: 1/(abs(q)^2)*(r - a*i - b*j - c*k)

Rotation Using Quaternions Given axis and angle: Quaternion(const Vector3D &axis, const double angle) { double t = angle / 180 * M_PI; Vector3D u = (1 / axis.length()) * axis; real = cos(t / 2); complex = sin(t / 2) * u; } Rotating a point represented by a vector (very informal…): Vector3D operator* (const Vector3D &v) const { Quaternion P(0, v); return (*this * (P * this->conjugate())).complex; } Converting back to a rotation matrix 

A Note about Interpolation Interpolation between two quaternions representing rotations has to produce intermediate unit quaternions This makes spherical interpolation a straightforward way to do it However, there are some complications de12.html de12.html

Example: Arcball thing Vector3D track = track_vec(mouse.old_x - track_center_x(), (sts.video.resolution_h - mouse.old_y) - track_center_y()).cross(track_vec(mouse.new_x - track_center_x(), (sts.video.resolution_h - mouse.new_y) - track_center_y())); double len = track.length(); if (len < ) return; track.normalize(); objh->rot = Quaternion(track, len / M_PI * 180) * objh->rot; objh->need_recalc = true;

Example: Camera 4 “advance” functions: void advance_right(float sec) { Vector3D delta (sec * speed, 0, 0); pos += rotation * delta; } 4 “move” functions: void move_right() { anim_on = true; dir = 0; } void tick(float sec) { if (anim_on) switch(dir) { case 0: advance_right(sec); break; case 1: advance_forward(sec); break; case 2: advance_left(sec); break; case 3: advance_back(sec); break; default: break; } }

Example: Camera (Cont’d) inline void look_h (int pix) { look_h(-pix * act.pix_to_deg); } inline void look_v (int pix) { look_v(-pix * act.pix_to_deg); } void look_h (double deg) { rotation = Quaternion(Vector3D(0, 1, 0), deg) * rotation; } void look_v (double deg) { rotation = rotation * Quaternion(Vector3D(1, 0, 0), deg); }

Example: Camera: Cont’d void apply_to_world() { glMatrixMode(GL_MODELVIEW); glMultTransposeMatrixd((const GLdouble *) rotation.conjugate().asMatrix().begin()); glTranslated(-1*pos[0], -1*pos[1], -1*pos[2]); }

Bonus: Basic Skybox // save old camera position Point3D oldp = cam.get_position(); Quaternion oldr = cam.get_rotation(); // move camera to skybox cam.set_to(oldr, sky_pos); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); cam.apply_to_world(); glPushAttrib(GL_ENABLE_BIT); glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glDisable(GL_BLEND); // here: draw the sky glPopAttrib(); glPopMatrix(); // restore camera cam.set_to(oldr, oldp); Note: texture mapping a sphere in a straightforward way will produce a lot of distortion;

Bonus: Texture-mapped text Based on: =17 =17 Create a texture made up of equally-spaced white letters on transparent background Generate the display lists like in the Red Book, one for each letter Each list makes a small textured rectangle and uses the appropriate texture coordinates Use glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

Bonus: Texture-mapped Text (Cont’d) glDepthFunc(GL_LEQUAL); glBlendEquation(GL_FUNC_ADD); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); // here set up the font’s colour with glMaterial while (*txt) { // here call the list for the letter glTranslated(cur_width, 0, 0); ++txt; } glPopMatrix(); glDisable(GL_BLEND); glDepthFunc(GL_LESS);

Bonus: Height-mapped Terrain Based on particle deposition described at: p3?particle p3?particle Implementation details:  Better to use triangles (triangle strip)  Each vertex is adjacent to 6 others (4 close and 2 farther away)  Smoothing the normals by averaging the 6 neighbours helps  Improvement suggestion: experiment with depositing “large” particles, to create the terrain faster and avoid having to scale the heights afterward

Height-mapped Terrain Pictures