Anda di halaman 1dari 25

ComputerGraphics Computer Graphics Lecture5: SimpleDataStructuresforGraphics Simple Data Structures for Graphics

Asst.Prof.Dr.Edizaykol Asst.Prof.Dr. Ediz aykol

Id tif Identifyauserdefinedobjectonthedisplay d fi d bj t th di l Inprinciple,itshouldbesimplebecausethemouse givesthepositionandweshouldbeableto h d h ld b bl determinetowhichobject(s)apositioncorresponds Practicaldifficulties
Pipelinearchitectureisfeedforward,hardtogofromscreen backtoworld b k ld Complicatedbyscreenbeing2D,worldis3D How close do we have to come to object to say we selected Howclosedowehavetocometoobjecttosayweselected it?

UsingRegionsoftheScreen Using Regions of the Screen

M Manyapplicationsuseasimplerectangular li i i l l arrangementofthescreen
Example: paint/CAD program Example:paint/CADprogram
tools drawingarea menus

Easiertolookatmousepositionanddeterminewhich areaofscreenitisinthanusingselectionmodepicking

RectangularMap Rectangular Map

ImmediateandRetainedModes Immediate and Retained Modes

RecallthatinastandardOpenGLprogram,oncean objectisrenderedthereisnomemoryofitandto redisplayit,wemustreexecutethecodeforit di l i h d f i
Knownasimmediatemodegraphics Canbeespeciallyslowiftheobjectsarecomplexandmustbe b ll l f h b l d b sentoveranetwork

Alternative is define objects and keep them in some Alternativeisdefineobjectsandkeeptheminsome formthatcanberedisplayedeasily

Retained mode graphics Retainedmodegraphics AccomplishedinOpenGLviadisplaylists

DisplayLists Display Lists

Mustdefine(name,create) ( , ) Addcontents Close

Inclientserverenvironment,displaylistis placedonserver
Can be redisplayed without sending primitives Canberedisplayedwithoutsendingprimitives overnetworkeachtime

DisplayListFunctions Display List Functions

Creatingadisplaylist i di l li
GLuint id; void init() { id = glGenLists( 1 ); glNewList( id, GL COMPILE ); GL_COMPILE /* other OpenGL routines */ glEndList(); }


void display() { glCallList( id ); }

HierarchyandDisplayLists Hierarchy and Display Lists

Consider model of a car
- Create display list for chassis p y - Create display list for wheel
g glNewList( CAR, GL_COMPILE ); ( , glCallList( CHASSIS ); glTranslatef( ); g glCallList( WHEEL ); glTranslatef( ); glCallList( WHEEL ); glEndList();

DataStructuresforObjects Data Structures for Objects

Si l d t t t Simple datastructuresforbuilding f b ildi polygonalmodels
Vertexlists Edge lists Edgelists


RepresentingaMesh Representing a Mesh

C id Consideramesh h
v6 e1 v1 e6 e8 v8 e11 e7 v2 v7 e12 e2 e9 e10 v3 v5 e3 v4 e4


Thereare8nodesand12edges 5interiorpolygons 6interior(shared)edges Each vertex has a location vi = (xi , yi , zi) Eachvertexhasalocationv

SimpleRepresentation Simple Representation

D fi Defineeachpolygonbythegeometriclocationsofits h l b h i l i fi vertices L d t O LeadstoOpenGLcodesuchas GL d h
glBegin(GL_POLYGON); glVertex3f(x1, y1, glVertex3f(x1 y1 z1); glVertex3f(x6, y6, z6); glVertex3f(x7, y7, z7); glEnd();

Considermovingavertextoanewlocation Mustsearchforalloccurrences

InwardandOutwardFacingPolygons Inward and Outward Facing Polygons

Theorder{v1, v6, v7} and{v6, v7, v1} areequivalentinthat thesamepolygonwillberenderedbyOpenGLbuttheorder {v1, v7, v6} is different isdifferent Thefirsttwodescribeoutwardly facing polygons Usetherighthandrule = counterclockwiseencirclement counter clockwise encirclement ofoutwardpointingnormal O OpenGLcantreatinwardand GL t ti d d outwardfacingpolygonsdifferently

GeometryvsTopology Geometry vs Topology

Generallyitisagoodideatolookfordata p g y structuresthatseparatethegeometryfrom thetopology
Geometry: locations of the vertices locationsofthevertices Topology: organizationoftheverticesandedges Example:apolygonisanorderedlistofverticeswith anedgeconnectingsuccessivepairsofvertices andthelasttothefirst Topologyisthesame evenifgeometrychanges

VertexLists Vertex Lists

P t th Putthegeometryinanarray t i Usepointersfromtheverticesintothisarray Introduceapolygonlist x1 y1 z 1 v1 x2 y2 z 2 v7 P1 v6 P2 x3 y3 z 3 P3 x4 y4 z 4 P4 P5 x5 y5 z5. v8 v5 x6 y6 z 6 v6 x7 y7 z 7 topology p gy x8 y8 z 8 geometry

SharedEdges Shared Edges

Vertexlistswilldrawfilledpolygonscorrectlybutifwe drawthepolygonbyitsedges,sharededgesaredrawn twice i


EdgeList Edge List

Everyedgehastwovertices Every edge has two vertices e1 e2 e3 e4 e5 e6 e7 e8 e9 v1 v6 e2 v5 e3

x1 y1 z 1 x2 y2 z 2 x3 y3 z 3 x4 y4 z 4 x5 y5 z5. x6 y6 z 6 x7 y7 z 7 x8 y8 z 8

v6 e1 v1 e6

e8 v8 e11 e7 v2 v7 e12

e9 e10 v3



Note polygons are not represented t t d

ModelingaCube Modeling a Cube

Model a color cube for rotating cube program Define global arrays for vertices and colors

GLfloat vertices[][3] = { {-1.0,-1.0,-1.0}, {1.0,-1.0,-1.0}, {1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0}, {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}}; GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0}, {1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}, {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}};

Drawingapolygonfromalistof indices
Drawaquadrilateralfromalistofindicesintothearray vertices andusecolorcorrespondingtofirstindex
void polygon(int a, int b, int c , int d) { glColor3fv(colors[a]); glBegin(GL_POLYGON); glVertex3fv(vertices[a]); glVertex3fv(vertices[b]); glVertex3fv(vertices[c]); glVertex3fv(vertices[d]); glEnd();

Drawcubefromfaces Draw cube from faces

void colorcube( ) id l b ( { polygon(0,3,2,1); polygon(2,3,7,6); polygon(0,4,7,3); p yg ( , , , ) polygon(1,2,6,5); polygon(4,5,6,7); polygon(0,1,5,4); }
5 6

4 0

Note that vertices are ordered so that we obtain correct outward facing normals

Theweaknessofourapproachisthatweare h k f hi h buildingthemodelintheapplicationand mustdomanyfunctioncallstodrawthecube Drawingacubebyitsfacesinthemost g y straightforwardwayrequires
6 glBegin, 6 glEnd 6glBegin,6glEnd 6glColor 24 glVertex 24glVertex

VertexArrays Vertex Arrays

OpenGLprovidesafacilitycalledvertexarrays that allowsustostorearraydataintheimplementation Sixtypesofarrayssupported
Vertices Colors Colorindices Normals Texturecoordinates Edgeflags Ed fl
Wewillneedonlycolors andvertices

glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL COLOR ARRAY); glEnableClientState(GL_VERTEX_ARRAY);

Identify location of arrays Identifylocationofarrays

glVertexPointer(3, GL_FLOAT, 0, vertices);
dataarray 3darrays storedasfloats datacontiguous

glColorPointer(3, GL_FLOAT, 0, colors);

Mappingindicestofaces Mapping indices to faces

5 6 2 4 0 3 7

GLubyte cubeIndices[24] = {0 3 2 1 2 3 7 6 {0,3,2,1, 2,3,7,6, 0,4,7,3, 1,2,6,5, 4,5,6,7, 0,1,5,4};

Eachsuccessivefourindicesdescribeafaceof thecube DrawthroughglDrawElements() which replacesallglVertex andglColor callsinthe l ll lV t d lC l ll i th displaycallback

Drawingthecube Drawing the cube

what to draw number of indices

for(i=0; i<6; i++) glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, &cubeIndices[4*i]); , [ ])

format of index data start of index data

glDrawElements(GL_QUADS, 24, _ GL_UNSIGNED_BYTE, cubeIndices);
Draws cube with 1 function call!!