02 OpenGL and GLUT

boringtarpΛογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 3 χρόνια και 8 μήνες)

262 εμφανίσεις

 O Op pe en nG GL L iis s a a s so offt tw wa ar re e A AP PI I t to o g gr ra ap ph hiic cs s h ha ar rd dw wa ar re e. .
 d de es siig gn ne ed d a as s a a s st tr re ea aml mliin ne ed d, , h ha ar rd dw wa ar re e-independent interface
t to o b be e iimp mplle eme men nt te ed d o on n ma man ny y d diiffffe er re en nt t h ha ar rd dw wa ar re e p plla at tffo or rms ms
 I In nt tu uiit tiiv ve e, , p pr ro oc ce ed du ur ra all iin nt te er rffa ac ce e w wiit th h C binding
 N No o w wiin nd do ow wiin ng g c co omma mman nd ds s ! !
 No high-lle ev ve ell c co omma mman nd ds s ffo or r d de es sc cr riib biin ng g mo mod de ells s o off 3D objects
 T Th he e O Op pe en nG GL L U Ut tiilliit ty y L Liib br ra ar ry y ( (G GL LU U) ) p pr ro ov viid de es s ma man ny y o off t th he e
mo mod de elliin ng g ffe ea at tu ur re es s, , s su uc ch h a as s q qu ua ad dr riic c s su ur rffa ac ce es s a an nd d N NU UR RB BS S
curves and surfaces
2 S Siilliic co on n G Gr ra ap ph hiic cs s ( (S SG GI I) ) r re ev vo ollu ut tiio on niiz ze ed d t th he e
g gr ra ap ph hiic cs s w wo or rk ks st ta at tiio on n b by y iim mp plle em me en nt tiin ng g
t th he e p piip pe elliin ne e iin n h ha ar rd dw wa ar re e ( (1982)
  T T T To o o o a a a ac c c cc c c ce e e es s s ss s s s t t t th h h he e e e s s s sy y y ys s s st t t te e e em m m m, , , , a a a ap p p pp p p plllliiiic c c ca a a at t t tiiiio o o on n n n
p pr ro og gr ra am mm me er rs s u us se ed d a a lliib br ra ar ry y c ca alllle ed d G GL L
 Wi Wit th h G GL L, , iit t w wa as s r re ella at tiiv ve elly y s siim mp plle e t to o
program 3D D iin nt te er ra ac ct tiiv ve e applications
3T Th he e s su uc cc ce es ss s o off G GL L lle ea ad d t to o O Op pe en nG GL L ( (1992),
a platform-iin nd de ep pe en nd de en nt t A AP PI I t th ha at t w wa as s
 Easy to use
 C C C Cllllo o o os s s se e e e e e e en n n no o o ou u u ug g g gh h h h t t t to o o o t t t th h h he e e e h h h ha a a ar r r rd d d dw w w wa a a ar r r re e e e t t t to o o o g g g ge e e et t t t
e ex xc ce elllle en nt t p pe er rffo or rm ma an nc ce e
 Focus on rendering
 O Om miit tt te ed d w wiin nd do ow wiin ng g a an nd d iin np pu ut t t to o a av vo oiid d
w wiin nd do ow w s sy ys st te em m d de ep pe en nd de en nc ciie es s
4 C Co on nt tr ro olllle ed d b by y a an n A Ar rc ch hiit te ec ct tu ur ra all R Re ev viie ew w
Board (ARB)
 M Me em mb be er rs s iin nc cllu ud de e S SG GI I, , M Miic cr ro os so offt t, , Nvidia, HP,
3DLabs, IBM,…….
 R R R Re e e elllla a a at t t tiiiiv v v ve e e elllly y y y s s s st t t ta a a ab b b blllle e e e ( ( ( (p p p pr r r re e e es s s se e e en n n nt t t t v v v ve e e er r r rs s s siiiio o o on n n n 3 3. .2 2) )
 E Ev vo ollu ut tiio on n r re efflle ec ct ts s n ne ew w h ha ar rd dw wa ar re e c ca ap pa ab biilliit tiie es s
 3D D t te ex xt tu ur re e m ma ap pp piin ng g a an nd d t te ex xt tu ur re e o ob bjje ec ct ts s
 V Ve er rt te ex x a an nd d ffr ra ag gm me en nt t p pr ro og gr ra am ms s
 A Allllo ow ws s ffo or r p plla at tffo or rm m s sp pe ec ciiffiic c ffe ea at tu ur re es s t th hr ro ou ug gh h
extensions
5 OpenGL core library (GL)
 opengl32 on Windows
 GL on most Unix/Linux s sy ys st te ems ms ( (libGL.a)
 O Op pe en nG GL L U Ut tiilliit ty y L Liib br ra ar ry y ( (G GL LU U) )
 C Co on nt ta aiin ns s s se ev ve er ra all r ro ou ut tiin ne es s u u u us s s siiiin n n ng g g g llllo o o ow w w we e e er r r r- -lle ev ve ell O Op pe en nG GL L
c co omma mman nd ds s t to o p pe er rffo or rm m s su uc ch h t ta as sk ks s as
 setting up matrices for s sp pe ec ciifi fic c viewing orientations and projections
 performing polygon t te es ss se elllla at tiio on n
 rendering surfaces
 Links with window system
 GLX for X window s sy ys st te ems ms, , A AG GL L ffo or r O OS S X X, , WGL for Windows
 Cross-p plla at tffo or rm m G GU UI I lliib br ra ar riie es s: : G GL LU UT T, , S SDL DL, , F FL LT TK K, , Q QT T, , … …
6 O Op pe en nG GL L iis s iin nd de ep pe en nd de en nt t o off a an ny y s sp pe ec ciiffiic c
window system
 O Op pe en nG GL L c ca an n b be e u us se ed d w wiit th h any different
window systems
 X X w wiin nd do ow ws s ( (G GL LX X) )
 MFC
 …
 GLUT (O Op pe en nG GL L U Ut tiilliit ty y T To oo ollk kiit t) provides a
p po or rt ta ab blle e A AP PI I ffo or r c cr re ea at tiin ng g w wiin nd do ow w a an nd d
iin nt te er ra ac ct tiin ng g w wiit th h I I/ /O O d de ev viic ce es s
7 O Op pe en nG GL L U Ut tiilliit ty y T To oo ollk kiit t ( (G GL LU UT T) )
 P Pr ro ov viid de es s ffu un nc ct tiio on na alliit ty y c co om mm mo on n t to o a allll w wiin nd do ow w
systems
 Open a window
 G G G Ge e e et t t t iiiin n n np p p pu u u ut t t t ffffr r r ro o o om m m m m m m mo o o ou u u us s s se e e e a a a an n n nd d d d k k k ke e e ey y y yb b b bo o o oa a a ar r r rd d d d
 Menus
 Event-driven
 C Co od de e iis s p po or rt ta ab blle e b bu ut t G GL LU UT T lla ac ck ks s t th he e
ffu un nc ct tiio on na alliit ty y o off a a g go oo od d t to oo ollk kiit t ffo or r a a s sp pe ec ciiffiic c
platform
 No slide bars, b bu ut tt to on ns s, , ffiille e b br ro ow ws se er rs s, , … …
8ap app pl li ic cat ati ion on p pr rogr ogram am
OpenGL Motif
G GL LU UT T
w wi id dg ge et t o or r s si im mi il la ar r
GLX, AGL
GLU
or WGL
OpenGL
X, Win32, Mac O/S
s sof oft tw war are e an and d/ /or or h har ard dw war are e
9Immediate Mode
geometry
P Pe er r V Ve er rt te ex x pipeline
Polynomial
O Op pe er ra at ti io on ns s & &
Evaluator
P Pr ri im mi it ti iv ve e
A As ss se em mb bl ly y
Display Per Fragment Frame
R Ra as st te er ri iz za at ti io on n
CPU
L Li is st t O Op pe er ra at ti io on ns s B Bu uf ff fe er r
T Te ex xt tu ur re e
M Me em mo or ry y
Pixel
Operations
10 GL State Variables c ca an n b be e s se et t a an nd d q qu ue er riie ed d b by y O Op pe en nG GL L. .
R Re em ma aiin ns s u un nc ch ha an ng ge ed d u un nt tiill t th he e n ne ex xt t c ch ha an ng ge e. .
 P Pr ro ojje ec ct tiio on n a an nd d v viie ew wiin ng g m ma at tr riix x
 C Co ollo or r a an nd d m ma at te er riia all p pr ro op pe er rt tiie es s
 Lights and shading
 L Liin ne e a an nd d p po olly yg go on n d dr ra aw wiin ng g m mo od de es s
 … …
 O Op pe en nG GL L ffu un nc ct tiio on ns s a ar re e o off t tw wo o t ty yp pe es s
 Primitive generating
 Can cause output (if p pr riim miit tiiv ve e iis s visible)
 H Ho ow w v ve er rt tiic ce es s a ar re e p pr ro oc ce es ss se ed d a an nd d a ap pp pe ea ar ra an nc ce e o off p pr riim miit tiiv ve e a ar re e
controlled by the state
 State changing
 Transformation functions
 Attribute functions
11 Functions have prefix gl a an nd d iin niit tiia all c ca ap piit ta all lle et tt te er rs s ffo or r e ea ac ch h
word
 glClearColor(), glEnable(()),, glPushMatrix() …
 glu for GLU functions
 gluLookAt(), gglluuPPeerrssppeeccttiivvee() …
  C Co on ns st ta an nt ts s b be eg giin n w wiit th h GL_ GL_, , , , u u u us s s se e e e a a a allllllll c c c ca a a ap p p piiiit t t ta a a allll lllle e e et t t tt t t te e e er r r rs s s s
 GGLL__CCOOLLOORR__BBUUFFFFEERR__BBIITT,, GGLL__PPRROOJJEECCTTIIOONN,, GGLL__MMOODDEELLVVIIEEWW …
 E Ex xt tr ra a lle et tt te er rs s iin n s so om me e c co om mm ma an nd ds s iin nd diic ca at te e t th he e number and
type of variables
 glColor3f(), glVertex3ff(()) …
 OpenGL data types
 GLfloat, GLdouble, GLint,, GLenum, …
 U Un nd de er rlly yiin ng g s st to or ra ag ge e m mo od de e iis s t th he e s sa am me e
 E Ea as sy y t to o c cr re ea at te e o ov ve er rllo oa ad de ed d ffu un nc ct tiio on ns s iin n C C+ ++ + b bu ut t iis ss su ue e iis s
efficiency
12f func unct ti ion on na nam me e
dimensions
glVertex3f(x,y,z x,y,z)
x,y,z are floats floats
belongs to GL library
glVertex3fv(p)
p is a pointer to an array
13 M Mo os st t c co on ns st ta an nt ts s a ar re e d de effiin ne ed d iin n t th he e iin nc cllu ud de e ffiille es s
gl.h, glu.h and gglluutt..hh
 Note #include <GL/glut.h glut.h> should automatically
include the others
 E Ex xa am mp plle es s
 glBegin(GL_POLYGON)
 glClear((GGLL__CCOOLLOORR__BBUUFFFFEERR__BBIITT))
 iin nc cllu ud de e ffiille es s a alls so o d de effiin ne e O Op pe en nG GL L d da at ta a t ty yp pe es s: :
GLfloat, GLdouble, ,… …. .
14 D De ev ve ello op pe ed d b by y M Ma ar rk k Kilgard
 H Hiid de es s t th he e c co om mp plle ex xiit tiie es s o off d diiffffe er riin ng g w wiin nd do ow w
system APIs
 D D D De e e effffa a a au u u ullllt t t t u u u us s s se e e er r r r iiiin n n nt t t te e e er r r rffffa a a ac c c ce e e e ffffo o o or r r r c c c clllla a a as s s ss s s s p p p pr r r ro o o ojjjje e e ec c c ct t t ts s s s
 G Gllu ut t r ro ou ut tiin ne es s h ha av ve e p pr re effiix x glut
 glutCreateWindow(()) ……
 H Ha as s v ve er ry y lliim miit te ed d G GU UI I iin nt te er rffa ac ce e
 Glui iis s t th he e C C+ ++ + e ex xt te en ns siio on n o off glut and
p pr ro ov viid de es s m mo or re e G GU UI Is s
15 Initialization: glutInit() processes (and removes)
command-line a ar rg gu um me en nt ts s t th ha at t m ma ay y b be e o off iin nt te er re es st t t to o g gllu ut t a an nd d t th he e
w wiin nd do ow w s sy ys st te em m a an nd d d do oe es s g ge en ne er ra all iin niit tiia alliiz za at tiio on n o off G Gllu ut t a an nd d O Op pe en nG GL L
 M Mu us st t b be e c ca alllle ed d b be effo or re e a an ny y o ot th he er r g gllu ut t r ro ou ut tiin ne es s
 Display Mode: T Th he e n ne ex xt t p pr ro oc ce ed du ur re e, , glutInitDisplayMode(),
p pe er rffo or rm ms s iin niit tiia alliiz za at tiio on ns s iin nffo or rm miin ng g O Op pe en nG GL L h ho ow w t to o s se et t u up p t th he e ffr ra am me e
buffer.
Display Mode M Me ea an niin ng g
 GLUT_RGB Use R RG GB B c co ollo or rs s
 GLUT_RGBA Use R RG GB B p pllu us s a allp ph ha a ( (ffo or r t tr ra an ns sp pa ar re en nc cy y) )
 GLUT_INDEX Use iin nd de ex xe ed d c co ollo or rs s ( (n no ot t r re ec co om mm me en nd de ed d) )
 GLUT_DOUBLE U Us se e d do ou ub blle e b bu uffffe er riin ng g ( (r re ec co om mm me en nd de ed d) )
 GLUT_SINGLE U Us se e s siin ng glle e b bu uffffe er riin ng g ( (n no ot t r re ec co om mm me en nd de ed d) )
 GLUT_DEPTH Use d de ep pt th h- -b bu uffffe er r (for hidden surface removal.)
16 Window Setup
 glutInitWindowSize(int int width, int height)
 glutInitWindowPos glutInitWindowPosition ition(int x, int y)
 glutCreateWindow glutCreateWindow((((char* char* char* char* title) title) title) title)
17G Ge en ne er ra at te e a a s sq qu ua ar re e o on n a a s so olliid d b ba ac ck kg gr ro ou un nd d
18#include <GL/glut.h>
void mydisplay(){
glClear((GGLL__CCOOLLOORR__BBUUFFFFEERR__BBIITT);
glBegin((GGLL__PPOOLLYYGGOONN));;
glVertex2f(-0.5,, -0.5);
glVertex2f(-0.5,, 0.5);
glVertex2f(0.5, 00.5);
glVertex2f(0.5, --0.5);
glEnd();
ggllFFlluusshh(());;
}
int main(int argc, , char** char** argv){
glutInit(&argc, argv argv);
glutInitD glutInitDis ispla playM yMode ode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize((500,500);
glutInitW glutInitWin indow dowPo Posit sitio ionn(0,0);
glutCreateWindow("s ("sim impl ple") e"); ;
glutDisplayFunc(myd mydis ispl play ay);
init();
glutMainLoop();
}
19includes gl.h
#include <GL/glut.h>
int main(int argc, char** aarrggvv)
{
glutInit(&argc,argv);
glutInitDisplayMode((GGLLUUTT__SSIINNGGLLEE||GGLLUUTT__RRGGBB));;
gglluuttIInniittWWiinnddoowwSSiizzee((550000,,555500000000));;
glutInitWindowPosition(0,00);
glutCreateWindow((""ssiimmppllee""));;
define window properties
glutDisplayFunc(mydisplay));;
display callback
init();
s se et t O Ope penG nGL L s st ta at te e
glutMainLoop();
}
e ent nte er r e eve vent nt l loop oop
20black clear color
void init()
opaque window
{
glClearColor (0.0, 0.0, 0.0,, 1.0);
ggllCCoolloorr33ff((11..00,, 11..00,, 11..00));;
f fi il ll l/ /dr dra aw w w wi it th w h whi hit te e
glMatrixMode ((GGLL__PPRROOJJEECCTTIIOONN));;
glLoadIdentity ();
glOrtho(-1.0, 1.0, -1.0, 1.00, -1.0, 1.0);
}
viewing volume
21 V Viir rt tu ua alllly y a allll iin nt te er ra ac ct tiiv ve e g gr ra ap ph hiic cs s p pr ro og gr ra am ms s a ar re e e ev ve en n
driven
 G GL LU UT T u us se es s c ca allllb ba ac ck ks s t to o h ha an nd dlle e e ev ve en nt ts s
 W Wiin nd do ow ws s s sy ys st te em m iin nv vo ok ke es s a a p pa ar rt tiic cu ulla ar r p pr ro oc ce ed du ur re e w wh he en n a an n
e e e ev v v ve e e en n n nt t t t o o o offff p p p pa a a ar r r rt t t tiiiic c c cu u u ulllla a a ar r r r t t t ty y y yp p p pe e e e o o o oc c c cc c c cu u u ur r r rs s s s. . . .
 M MO OS ST T I IM MP PO OR RT TA AN NT T: : d diis sp plla ay y e ev ve en nt t
 S Siig gn na alle ed d w wh he en n w wiin nd do ow w ffiir rs st t d diis sp plla ay ys s a an nd d w wh he en ne ev ve er r
p po or rt tiio on ns s o off t th he e w wiin nd do ow w r re ev ve ea alls s ffr ro om m b bllo oc ck kiin ng g w wiin nd do ow w
 glutDisplayFunc(void glutDisplayFunc(void (* (*func)(void)) registers the
display callback function
 Running the program: gglluuttMMaaiinnLLoooopp(())
 M Ma aiin n e ev ve en nt t llo oo op p. . N Ne ev ve er r e ex xiit t( () )
22 glutReshapeFunc(v (void oid (* (*func)(int w, int h))
iin nd diic ca at te es s w wh ha at t a ac ct tiio on n s sh ho ou ulld d b be e t ta ak ke en n w wh he en n t th he e w wiin nd do ow w iis s r re es siiz ze ed d. .
 glutKeyboardFunc((void void (* (*func)(unsigned char key,
int x, int y)) and
glutMouseFunc(void (*func func)(int button, int
state, state, int int x, x, int int y)) y)) a allllo ow w y yo ou u t to o lliin nk k a a k ke ey yb bo oa ar rd d k ke ey y o or r a a
m m m mo o o ou u u us s s se e e e b b b bu u u ut t t tt t t to o o on n n n w w w wiiiit t t th h h h a a a a r r r ro o o ou u u ut t t tiiiin n n ne e e e t t t th h h ha a a at t t t''''s s s s iiiin n n nv v v vo o o ok k k ke e e ed d d d w w w wh h h he e e en n n n t t t th h h he e e e k k k ke e e ey y y y o o o or r r r m m m mo o o ou u u us s s se e e e
button is pressed or released.
 glutMotionFunc(void (*func func)(int x, int y))
r re eg giis st te er rs s a a r ro ou ut tiin ne e t to o c ca allll b ba ac ck k w wh he en n t th he e m mo ou us se e iis s m mo ov ve ed d w wh hiille e a a
mouse button is also pressed.
 glutIdleFunc(void (*func func)(void)) registers a function
t th ha at t''s s t to o b be e e ex xe ec cu ut te ed d iiff n no o o ot th he er r e ev ve en nt ts s a ar re e p pe en nd diin ng g - for example,
w wh he en n t th he e e ev ve en nt t llo oo op p w wo ou ulld d o ot th he er rw wiis se e b be e iid dlle e
23 Lab: set u up p a a p pr ro ojje ec ct t w wiit th h O Op pe en nG GL L
 Create a w wiin nd do ow w a ap pp plliic ca at tiio on n u us siin ng g O Op pe en nG GL L
template
 A Ad dd d o op pe en ng gll3 32 2. . . .lllliiiib b b b, , , , g g g gllllu u u ut t t t3 32 2. .lliib b, , g gllu u3 32 2. .lliib b t to o
p pr ro ojje ec ct t s se et tt tiin ng gs s ( (u un nd de er r lliin nk k t ta ab b) )
24 W We e h ha av ve e lle ea ar rn ne ed d h ho ow w t to o c cr re ea at te e a a w wiin nd do ow w
 S St te ep ps s iin n t th he e d diis sp plla ay y ffu un nc ct tiio on n
 Clear the window
 Set drawing attributes
 S S S Se e e en n n nd d d d d d d dr r r ra a a aw w w wiiiin n n ng g g g c c c co o o om m m mm m m ma a a an n n nd d d ds s s s
 Call glFlush( () ) ( (s siin ng glle e b bu uffffe er r) ) o or r glutSwapBuffers()
(double buffers)
 s se ee e t th he e d diiffffe er re en nc ce e o on n d dr ra aw wiin ng g w wh hiille e a an niim ma at tiio on n iis s o on n
 O Op pe en nG GL L c co oo or rd diin na at te e s sy ys st te em m h ha as s d diiffffe er re en nt t o or riig giin n
ffr ro om m t th he e w wiin nd do ow w s sy ys st te em m
 U Us se es s llo ow we er r lle efft t c co or rn ne er r iin ns st te ea ad d o off u up pp pe er r lle efft t c co or rn ne er r a as s
origin
25 glClear((GGLL__CCOOLLOORR__BBUUFFFFEERR__BBIITT))
 c clle ea ar rs s t th he e ffr ra am me e b bu uffffe er r b by y o ov ve er rw wr riit tiin ng g iit t w wiit th h
t th he e b ba ac ck kg gr ro ou un nd d c co ollo or r. .
 B B B Ba a a ac c c ck k k kg g g gr r r ro o o ou u u un n n nd d d d c c c co o o ollllo o o or r r r iiiis s s s a a a a s s s st t t ta a a at t t te e e e s s s se e e et t t t b b b by y y y
glClearColor(GGLLffllooaatt r, GLfloat g,
GGLLffllooaatt b, GLfloat a)
 Setting Setting it once it once is is enough enough and and it it can can be put be put
in init()
26 glColor3f(GLfloat rr,, GLfloat g, GLfloat b)
sets the drawing color
 glColor3d(), glColor3ui() can also be used
 R Re eme memb mbe er r O Op pe en nG GL L iis s a a s st ta at te e ma mac ch hiin ne e
 O On nc ce e s se et t, , t th he e a at tt tr riib bu ut te e a ap pp plliie es s t to o a allll s su ub bs se eq qu ue en nt t d de effiin ne ed d
o o o ob b b bjjjje e e ec c c ct t t ts s s s u u u un n n nt t t tiiiillll iiiit t t t iiiis s s s s s s se e e et t t t t t t to o o o s s s so o o ome me me me o o o ot t t th h h he e e er r r r v v v va a a allllu u u ue e e e
 glColor3fv() t ta ak ke es s a a fflla at t a ar rr ra ay y a as s iin np pu ut t
 T Th he er re e a ar re e m mo or re e d dr ra aw wiin ng g a at tt tr riib bu ut te es s t th ha an n c co ollo or r
 Point size: glPointSize()
 Line width: glLinewidth(())
 Dash or dotted line: ggllLLiinneeSSttiippppllee()
 Polygon pattern: ggllPPoollyyggoonnSSttiippppllee()
 …
27 Simple Objects glRectf()
 Complex Objects
 Use construct glBegin(mode))
and glEnd() and a list of
vertices in between
 ggllBBeeggiinn((mmooddee))
glVertex(v0);
glVertex(v1);
...
glEnd();
 Some other commands
can also be used between
glBegin() and glEnd(), e.g.
glColor3f().
 Example29 Orthographic View
 glOrtho(left, (left, right, right, bottom, bottom, top, top, front, front, ba back)
 Specifies the coordinates of 3 3D region to be projected into the
image space.
 A An ny y d dr ra aw wiin ng g o ou ut ts siid de e t th he e r re eg giio on n w wiillll b be e a au ut to om ma at tiic ca alllly y c clliip pp pe ed d
a aw wa ay y. .
z=0
30 Do not have t to o u us se e the entire window for
the image: ggllVViieewwppoorrtt(x,y,w,h)
 V Va allu ue es s iin n p piix xe ells s ( (s sc cr re ee en n c co oo or rd diin na at te es s) )
31Aspect Ratio: Height/Width
If the aspect ratio of the window
is different from that of the viewport, , t the he pi pic ct tur ure e w wi il ll l be be distorted.
32 Start with a triangle
 C C C Co o o on n n nn n n ne e e ec c c ct t t t b b b biiiis s s se e e ec c c ct t t to o o or r r rs s s s o o o offff s s s siiiid d d de e e es s s s a a a an n n nd d d d r r r re e e em m m mo o o ov v v ve e e e c c c ce e e en n n nt t t tr r r ra a a allll
triangle
 Repeat
33 Five subdivisions
34#include <GL/glut.h>
/* initial triangle */
GLfloat v[3][2]={ {-1.0, , -0.58},
{1.0, , -0.58},
{{00..00, , , , 11..15 15}}; }};
int n; /* /* number number of recursive of recursive steps steps */ */
void triangle( GLfloat *a, *a, GLfloat *b, GLfloat *c)
/* /* display display one one triangle triangle */ */
{
glVertex2fv(a);
glVertex2fv(b);
glVertex2fv(c);
}
35void divide_triangle(GLfloat **aa,, GLfloat *b, GLfloat *c, int m)
{
/* ttrriiaannggllee ssuubbddiivviissiioonn uussiinngg vveerrtteexx nnuummbbeerrss **//
point2 v0, v1, v2;
int j;
if(m>0)
{{
for(j=0; j<2; j++) v0[[jj]]==((aa[[jj]]++bb[[jj]]))//2;
for(j=0; j<2; j++) v1[[jj]]==((aa[[jj]]++cc[[jj]]))//2;
for(j=0; j<2; j++) v2[[jj]]==((bb[[jj]]++cc[[jj]]))//2;
divide_triangle(a, v0,, vv1, m-1);
divide_triangle(c, v1,, vv2, m-1);
divide_triangle(b, v2,, vv0, m-1);
}
else(triangle(a,b,c));
/* ddrraaww ttrriiaannggllee aatt eenndd ooff rreeccuurrssiioonn **//
}
36void display()
{
glClear(GL_COLOR_BUFFER (GL_COLOR_BUFFER_BIT) _BIT);;
glBegin(GL_TRIANGLES); (GL_TRIANGLES);
divide_triangle divide_triangle(v[ (v[00], ], ], ], v[ v[ v[ v[11], ], v[ v[22], ], n); n);
glEnd();
glFlush();
}
 By having the glBegin and glEnd glEnd in the display callback rather
than in the function triangle triangle and using GL_TRIANGLES rather
than GL_POLYGON in glBegin, , w we e c ca allll glBegin and glEnd only
o on nc ce e ffo or r t th he e e en nt tiir re e g ga as sk ke et t r ra at th he er r t th ha an n o on nc ce e ffo or r e ea ac ch h t tr riia an ng glle e
37a af ft te er r 5 5 i i i it t t te e e er r r ra a a at t t ti i i io o o on n n ns s s s
38void tetrahedron( int mm))
{
glColor3f(1.0,0.0,0..0);
divide_triangle(v[0]],, vv[[1], v[2], m);
glColor3f(0.0,1.0,0..0);
ddiivviiddee__ttrriiaannggllee((vv[[33]]]],,,, vvvv[[[[22]],, vv[[11]],, mm));;
glColor3f(0.0,0.0,1..0);
divide_triangle(v[0]],, vv[[3], v[1], m);
glColor3f(0.0,0.0,0..0);
divide_triangle(v[0]],, vv[[2], v[3], m);
}
39