More on Drawable Objects, Hierarchical Objects Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, January.

Slides:



Advertisements
Similar presentations
PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
Advertisements

Using GLU/GLUT Objects GLU/GLUT provides very simple object primitives glutWireCube glutWireCone gluCylinder glutWireTeapot.
1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 20 Object Oriented Theory II.
. Virtual Classes & Polymorphism. Example (revisited) u We want to implement a graphics system u We plan to have lists of shape. Each shape should be.
Polymorphism From now on we will use g++!. Example (revisited) Goal: Graphics package Handle drawing of different shapes Maintain list of shapes.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
1 Inheritance and Polymorphism Inheritance (Continued) Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Course Overview, Introduction to CG Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Friday, September 5, 2003.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Mouse-Based Viewing & Navigation Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, November 3, 2003.
Polygon Lists & 3-D File Formats Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Monday, February 18, 2002.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Object Oriented Programming Philosophy. Part 1 -- Basic Understanding & Encapsulation.
Inheritance. Recall the plant that we defined earlier… class Plant { public: Plant( double theHeight ) : hasLeaves( true ), height (theHeight) { } Plant(
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction Structures are somewhat like an array in that.
CS 481 Preview, Some Lighting Details Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, November 19, 2003.
CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.
Data Structures for Scenes, The Basics of Scene Graphs Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Friday,
Implementing Scene Graphs, CSG Trees Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Monday, January 26, 2004.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Homogeneous Form, Introduction to 3-D Graphics Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, October 20,
Shadows via Projection Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, November 5, 2003.
Lecture 21 Multiple Inheritance. What is Multiple Inheritance? We defined inheritance earlier in the semester as a relationship between classes. If class.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Intro. to Advanced Lighting, Basic Ray Tracing Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Monday, April.
More on Environment Mapping Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, December 10, 2003.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Build-A-Button Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, October 8, 2003.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
1 CS161 Introduction to Computer Science Topic #9.
More on GLUT Programming Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, September 15, 2003.
Advanced Viewing Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Friday, October 31, 2003.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Introduction to OOP CPS235: Introduction.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
The const Keyword Extreme Encapsulation. Humble Beginnings There are often cases in coding where it is helpful to use a const variable in a method or.
Solving Quadratic Equations by Factoring. Zero Product Property For any real numbers a and b, if the product ab = 0, then either a = 0, b = 0, or both.
Object Oriented Programming Elhanan Borenstein Lecture #7.
1 Linked Lists Assignment What about assignment? –Suppose you have linked lists: List lst1, lst2; lst1.push_front( 35 ); lst1.push_front( 18 ); lst2.push_front(
More on Ray Tracing Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, April 14, 2004.
Some Notes on 3-D Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Friday, October 24, 2003.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Module 11: Polymorhism #1 2000/01Scientific Computing in OOCourse code 3C59 Module 11: Polymorphism and virtual methods In this module we will cover Polymorphism.
CS 2130 Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing Warning: The precedence table given for the Wff grammar is in error.
Introduction to 3-D Viewing Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, October 27, 2003.
Visit for more Learning Resources
Stenciling Effects Glenn G. Chappell
CMSC 202 Polymorphism.
Introduction to the Mouse
Packages and Interfaces
Projection in 3-D Glenn G. Chappell
Display Lists & Text Glenn G. Chappell
More on Widgets, Misc. Topics
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Review of Previous Lesson
Presentation transcript:

More on Drawable Objects, Hierarchical Objects Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, January 21, 2004

21 Jan 2004CS 481/6812 Review: Drawable Objects [1/4] Now we begin looking at how to represent a scene internally. It will be convenient to be able to deal with drawable objects independent of their characteristics. There are many ways to do this; we discuss the usual OO solution, as implemented in C++. It is reasonable to represent different kinds of drawable objects with different data types. Deriving all of these from a common base class (“ Drawable ”?) allows us to draw them without knowing what type they are.

21 Jan 2004CS 481/6813 Review: Drawable Objects [2/4] Here is one way to write our base class: // class Drawable // Abstract base class for drawable objects class Drawable { public: virtual ~Drawable() {} virtual void draw() const = 0; }; That’s all! The destructor is a C++ detail: a base class should have a virtual destructor. Note that Drawable is an abstract class (due to the “ = 0 ”); we cannot declare objects of type Drawable.

21 Jan 2004CS 481/6814 Review: Drawable Objects [3/4] To declare a drawable object type, do something like this: class Cokebottle : public Drawable { public: virtual ~Cokebottle() {} virtual void draw() const; Cokebottle():Drawable(),iscokeit(false) {} void cokeisit() { iscokeit = true; } private: bool iscokeit; // true if Coke is it. }; void Cokebottle::draw() const { Draw a cokebottle here. }

21 Jan 2004CS 481/6815 Review: Drawable Objects [4/4] To use objects polymorphically, refer to them via base-class pointers or references: void draw_this(const Drawable & obj) { obj.draw(); // Calls the proper virtual function. } Function draw_this can take a parameter of any type derived from Drawable. The above code would not work correctly if the object were passed by value. Now we can do this: Cokebottle c; draw_this(c);

21 Jan 2004CS 481/6816 More on Drawable Objects: Inheritance and Containers [1/3] Since we can deal with all drawable objects the same way, we can stick all of our objects into a container (array, vector, etc.), and iterate through that container to draw the scene. However, the following will get us into trouble. std::vector scene; Why? Hint: There are two big problems here.

21 Jan 2004CS 481/6817 More on Drawable Objects: Inheritance and Containers [2/3] std::vector scene; First, this will not compile. Since Drawable is an abstract class, we cannot create objects of type Drawable. But, second, even if we make Drawable a concrete class, this is a problem. A Cokebottle is probably bigger than a Drawable. So we cannot store a Cokebottle in the space meant for a Drawable. The problem resulting from trying to store an object of a derived class in a base-class variable is called slicing.

21 Jan 2004CS 481/6818 More on Drawable Objects: Inheritance and Containers [3/3] Solution: Use base-class pointers. Be sure that objects are delete ’d properly! std::vector scene; To add to the scene: scene.push_back(new Cokebottle); To draw the entire scene: std::vector ::const_iterator it; for (it = scene.begin(); it != scene.end(); ++it) (*it)->draw();

21 Jan 2004CS 481/6819 Hierarchical Objects: Overview Suppose we wish to draw a moving object with moving parts. This is called a hierarchical object. See face.cpp for an example. Two questions: How can we handle this conveniently with our graphics API? What sorts of data structures are appropriate for storing such an object? We begin with the first question.

21 Jan 2004CS 481/68110 Hierarchical Objects: Introduction We think of an object with moving parts as a hierarchy. At the top of the hierarchy is the object as a whole. At the next lower level in the hierarchy are the moving parts (for example, the eyes in face.cpp ). Moving parts can contain moving parts; these are at an even lower level in the hierarchy. We use stack operations to handle the transformations involved in drawing hierarchical objects.

21 Jan 2004CS 481/68111 Hierarchical Objects: Transformations [1/4] We want a moving object to have a moving part. We should be able to move the object as a whole. We should also be able to move the part as a part of the object. Thus, one transformation is applied to the object as a whole, but two transformations are applied to the moving part. The transformation for the moving part needs to be done before the transformation of the object as a whole (right?); therefore it comes later in the code (right?).

21 Jan 2004CS 481/68112 Hierarchical Objects: Transformations [2/4] Pseudocode: glPushMatrix(); Set up transformation for whole object Draw non-moving parts glPushMatrix(); Multiply current matrix by transformation for moving part Draw moving part glPopMatrix(); This is the general form of the code to draw a hierarchical object. What are the push/pop really good for? See the next slide …

21 Jan 2004CS 481/68113 Hierarchical Objects: Transformations [3/4] What if a hierarchical object has more than one moving part? glPushMatrix(); Set up transformation for whole object Draw non-moving parts glPushMatrix(); Multiply current matrix by transformation for moving part 1 Draw moving part 1 glPopMatrix(); glPushMatrix(); Multiply current matrix by transformation for moving part 2 Draw moving part 2 glPopMatrix(); It is convenient to make some of the pieces above into separate functions. Then follow this rule: If a function changes a matrix, then it also restores it to its prior value. This is essentially the form of our example (discussed shortly).

21 Jan 2004CS 481/68114 Hierarchical Objects: Transformations [4/4] What if a hierarchical object has more than two levels? glPushMatrix(); Set up transformation for whole object Draw non-moving parts glPushMatrix(); Multiply current matrix by transformation for moving part 1 Draw moving part 1 glPushMatrix(); Multiply current matrix by transformation for sub-part 1 of moving part 1 Draw sub-part 1 of moving part 1 Etc …

21 Jan 2004CS 481/68115 Hierarchical Objects: Example ( face.cpp ) [1/4] void display() { glClear(GL_COLOR_BUFFER_BIT); // Draw face glPushMatrix(); glTranslated(face_move, 0.0, 0.0); glRotated(face_angle, 0,0,1); glScaled(face_scale, face_scale, face_scale); draw_face(); glPopMatrix(); This is the beginning of the display routine. It sets up the transformation for the face, then calls draw_face to do the drawing.

21 Jan 2004CS 481/68116 Hierarchical Objects: Example ( face.cpp ) [2/4] Here is a portion of the code for function draw_face: // Draw head glColor3d(0.8, 0.6, 0.4); glCallList(disk_list); // Draw left eye (on viewer's right) glPushMatrix(); glTranslated( 0.4, 0.3, 0.0); glScaled(0.2, 0.2, 1.0); draw_eye(); glPopMatrix(); // Draw right eye (on viewer's left) glPushMatrix(); glTranslated(-0.4, 0.3, 0.0); glScaled(0.2, 0.2, 1.0); draw_eye(); glPopMatrix(); Because we handle transformations properly, we can use the same function to draw both eyes.

21 Jan 2004CS 481/68117 Hierarchical Objects: Example ( face.cpp ) [3/4] Each function can be written to draw its part within a square of side 2, centered at the origin (x & y go from –1 to 1). Then we set up the transformation to put the part in the proper place before we call the function that draws it. The function that does the drawing uses the transformation it is given, modifies it if necessary, but always restores it to the original value. void draw_eye() { // Draw white of eye glPushMatrix(); glScaled(1.0, 0.4, 1.0); glColor3d(1.0, 1.0, 1.0); glCallList(disk_list); glPopMatrix();

21 Jan 2004CS 481/68118 Hierarchical Objects: Example ( face.cpp ) [4/4] Comments Each function is written to draw the appropriate part (and all sub-parts) in some kind of “standard position”. Again, sub-part transformations come before the main transformations, which means they are later in the code, which means they can go in the function that draws the sub-part (right?). This is all very convenient and easy to use, once you wrap your mind around it. We are very clear about expectations for matrix mode, etc., when entering and leaving each function. The initial set-up is done when the reshape function is first called. After that, whenever a function finishes, we are careful to leave things the way they were when it started.