You are on page 1of 42

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

Lp trnh Opengl vi th vin AUX


(Ti liu tham kho ca trng i hc wakayama Nht)

Tc gi: Bi Minh Trng Email: truongtoba136@yahoo.co.jp Homepage: http://cct2.st.toba-cmt.ac.jp/~442/


I-Gii thiu v Opengl: Opengl l th vin lp trnh ho 3D, cc bn mun bit thm th xem ti trang ch opengl. Org.Bi vit ny dnh cho nhng bn bit opengl l g, v n cng l bi u tin cho vic hc ho vi opengl sau ny. II-Opengl trong Windows: Lp trnh opengl trong Windows bng Visual C, bn phi s dng ba th vin sau glaux.lib glu32.lib v opengl32.lib.Trong Visual C mun link ti cc th vin ny cc bn lm nh sau: trn menu(trnh n) chn Project sau chn setting ri cui cng trong tab link bn dnh tn 3 th vin trn vo(nh l c du cch gia cc tn ca th vin).Nu bn thch s dng phm tt th ch vic bm Alt+F7 th cng c kt qu nh trn.Mt iu cng rt quan trng l khi to mt project mi bn phi chn Window32 console application.T by gi bn c th sn sng vit m lnh ca mnh.(Nn nh l bn khng phi thm bt c ci g na v trong VC c y nhng ci ti cp trn). III-To mt ca s trong opengl: Di y l m ngun cho chng trnh u tin ca bn to mt ca s.Hy lu n vi tn g tu bn(v d hello.c nh truyn thng) 1-Chng trnh u tin ca bn: /*filename: hello.c*/ /*Chng trnh u tin to mt ca s trong opengl*/ #ifdef unix /*Phn ny dng xc nh mi trng lm vic ca bn*/ #include <GL/gl.h> /*N s xc nh bn bin dch chng trnh ny trn unix*/ #include aux.h /*hay Windows, vi lp trnh vin trn windows bn c */ #define CALLBACK /*th b phn bn trn i v ch ly phn in m*/ #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif int main(int argc, char *argv[]) { auxInitWindow(argv[0]); return 0; } Lnh auxInitWindow(string); c tc dng to mt ca s mi, string l tiu ca ca s , bn c th vit tiu nh th no l tu bn. Chng trnh ny sau khi bin dch th n mi ch hin ra mt ca s ri ng ngay, nu windows ca bn chy nhanh qu th bn s khng nhn thy chi ht Sau y chng ta s bt Window dng li chng 1 giy chng ta quan st.Cng vi m lnh trn bn ch cn thm mt dng lnh: sleep(s_giy_mun_xem x 1000);(tc l lnh ny bt window tm dng trong vng 1 phn nghn giy) 2-Theo di Window /*file name: hello1s.cpp*/ #ifdef unix 1

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

#include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif int main(int argc, char *argv[]) { auxInitWindow(argv[0]); /*dng lnh mi , window s hin trong vng 1 giy*/ Sleep(1000); /*dng lnh mi*/ return 0; } Trong phn source code m ngun ny nm trong file hello1s.cpp. 3-Xo mn hnh trong opengl Tip theo ti s gii thiu vi cc bn cch xo mn hnh trong opengl.Di y l m ngun: /*filename: clear.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif int main(int argc, char *argv[]) { auxInitWindow(argv[0]); /*Nhng dng lnh mi*/ glClearColor(1.0,1.0,1.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glFlush(); /*Nhng dng lnh mi*/ Sleep(1000); return 0; } Cc lnh glClearColor(), glClear(),glFush() l nhng lnh c bn ca Opengl.glClearColor() c nhim v chn mu xo window, bn d dng nhn ra l n c 4 tham s, 4 tham s l RGBA( red green blue alpha).Khng ging vi hm RGB() trong Win32 API , 4 tham s ny c gi tr trong khong 0.0f n 1.0f(kiu float).Ba tham s u l mu xanh l cy v xanh da tri, cn tham s th 4 l sng ti ca window.By gi hy thay i cc gi tr ca mu xem th!Hm glClear() mi thc s xo window, n c nhng hng s xc nh.C trng hp c nhng hm cha c chy n khi kt thc chng trnh, trnh trng hp ny hm glFlush()c gi, n s thc hin tt c cc hm cha c chy v kt thc chng trnh. 4-V hnh trong opengl 2

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

T trc n gi chng ta mi ch ni v cch to v xo ca s, by gi chng ta s thc hin v mt s hnh n gin: /*filename line.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif int main(int argc, char *argv[]) { auxInitWindow(argv[0]); glClearColor(1.0,1.0,1.0,0.0); glClear(GL_COLOR_BUFFER_BIT); /*nhng dng lnh mi*/ glBegin(GL_LINE_LOOP); glVertex2d(0.1,0.1); glVertex2d(0.9,0.1); glVertex2d(0.9,0.9); glVertex2d(0.1,0.9); /*nhng dng lnh mi*/ glEnd(); glFlush(); Sleep(1000); return 0; } Tt c cc hnh khi c v trong opengl u c nm gia hai dng lnh glBegin() v glEnd() (Hi ging vi pascal- bn no hc pascal th d hiu nh!).C th c nhiu cp dng lnh nh vy, tc l bn c th vit cc hm v khc nhau v dng cp cu lnh trn trong cc hm .Tham s ca glBegin() l GL_LINE_LOOP c ngha l n bo window v mt ng khp kn im u trng vi im cui. Di y l mt s hng s c bn: Hng s ngha GL_POINT V im GL_LIN V ng thng ni hai im GL_LINE_STRIP Tp hp ca nhng on c ni vi nhau GL_LINE_LOOP ng gp khc khp kn GL_TRIANGLES V hnh tam gic GL_QUADS V t gic GL_TRIANGLES_STRIP V mt tp hp cc tam gic lin nhau, chung mt cnh GL_QUAD_STRIP V mt tp hp cc t gic lin nhau, chung mt cnh GL_TRIANGLE_FAN V hnh qut Di y l bc tranh ton cnh v cc thng s ny.

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

Hm glVertex2d() xc nh im hai chiu.Bn nn bit mt s tin t cc hm ca opengl, cc hm dng th vin no s bt u bng tn ca th vin v d dng cc hm c bn ca opengl th thng l bt u vi gl, cc hm dng th vin glut th bt u vi glu cc hm dng th vin aux th bt u vi aux......Cc hm cng c hu t v d glVertex2d() l v im 2 chiu, glVertex3d() l v im 3 chiu,....dn dn hc cc bn s pht hin ra nhiu hn. 5-S dng mu v: Tip theo ti s hng dn cc bn cch s dng mu v v cch th hin n. Di y l m ngun: /*filename: color1.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif int main(int argc, char *argv[]) { auxInitDisplayMode(AUX_RGBA); /*hm mi*/ auxInitWindow(argv[0]); glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3d(1.0,0.0,0.0); /*hm mi*/ glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS); /*tham s mi*/ glVertex2d(0.1,0.1); 4

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

glVertex2d(0.9,0.1); glVertex2d(0.9,0.9); glVertex2d(0.1,0.9); glEnd(); glFlush(); Sleep(1000); return 0; } Hm auxInitDisplayMode() bo vi window rng chng ta chn cch hin th nhng g m chng ta sp v ti y, tham s ca n l AUX_RGBA chnh l mode RGBA m ti cp trn.Hm glColor3d() cho php chng ta chn mu v, tham s ca n l red green v blue nhng cc gi tr ny l kiu double nu bn mun dng kiu float th c hm glColor3f(), c hai kiu trn gi tr ca mu vn nm trong khong 0 n 1.Ch l chng trnh trn chng ta i tham s mi cho hm glBegin(), by gi n s v mt t gic, v trong chng trnh ny th l mt hnh vung. Trong phn ny ti mun trnh by vi cc bn mt k thut na, chng trnh trn ch cho chng ta nhn thy mt mu do chng ta t mt mu duy nht trc khi v. c th to nhiu mu n tng bn c th ci t i ci t li hm glColor3d() mi khi chng ta v mi. Di y l m ngun: /*filename: color2.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif int main(int argc, char *argv[]) { auxInitDisplayMode(AUX_RGBA); auxInitWindow(argv[0]); glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS); glColor3d(1.0,0.0,0.0); /*hm ny c chuyn xung y*/ glVertex2d(0.1,0.1); glColor3d(0.0,1.0,0.0); /*tham s mi cho hm*/ glVertex2d(0.9,0.1); glColor3d(0.0,0.0,1.0); /*tham s mi cho hm*/ glVertex2d(0.9,0.9); glColor3d(1.0,0.0,1.0); /*tham s mi cho hm*/ glVertex2d(0.1,0.9); glEnd(); glFlush(); Sleep(1000); return 0; } 5

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

Bin dch v chy th bn c mt hnh vung trng kh p mt, nhng hy tip tc hc, chng ta cn c th to nhiu hiu ng n tng hn nhiu. Ni thm cht na v cch s dng hm, vi cc hu t: v d vi hm glVertex*() v glColor*(), hay cc hm khc c du hoa th * th n c th c rt nhiu hu t.V n c cu to nh sau: ly v d hm glVertex*() C hm glVertex4dv(Gldouble x,Gldouble y,Gldouble z,Gldouble w) s 4 th hin rng hm c 4 tham s, ch d th hin rng tham s c gi tr double(ngoi ra n cn c th l float,int,short, unsigned int, unsigned short, unsigned char,char) ch v th hin rng n dng pointer.Cc bn ch cn hiu qua nh vy, sau ny chng ta s ni r hn. 6-Giao din ca ca s v qun l ca s: Vi nhng chng trnh ch cn v n gin th bn c th dng cc chng trnh trn, nhng vi cc chng trnh phc tp sau ny chng ta khng th vit nh th c na.Di y ti s trnh by vi cc bn cu trc ca chng trnh trong opengl. Trc ht l t kho CALLBACK, i vi cc bn lp trinh WIN API th c th hiu r c lnh ny, nhng c th ni n gin l khi s dng th vin AUX th ta phi dng t kho ny ch nh n.Cc chng trnh bn trn chng ta vit u dng lnh Sleep(1000) bt window dng li cho chng ta theo di, sp ti y chng ta s lm mt cch chuyn nghip hn l dng hm auxMailLoop() trong thn ca hm main() hm chnh ca chng trnh.Tham s ca hm ny l con tr tr n hm m chng ta v , hin th nhng g chng ta mun(trong chng trnh ny tham s chnh l hm draw()).iu g s xy ra nu ngi dng thay i kch c ca ca s? thc hin iu ny chng ta cng dng mt hm tng t nh hm auxMainLoop(), l hm auxReshapeFunc(), tham s ca n cng l con tr ch n hm m chng ta c th thay i thng s ca ca s, tham s ca n trong chng trnh ny l hm resize().Nu bn hc qua v ho my tnh th s d dng hiu v to trong ho, hm glLoadIdentity() c nhim v thit nh ma trn ca to l ma trn n v. M ngun di y s cho chng ta r hn: /*filename: interface.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif GLvoid CALLBACK draw(void){ /*ch bn c th khng cn ch void trong */ glClearColor(0.0,0.0,0.0,0.0); /*khi lp trnh vi VC, Glvoid l kiu */ glClear(GL_COLOR_BUFFER_BIT);/*hm trong opengl, n tng t */ glClearColor(0.0,0.0,0.0,0.0); /*nh void trong C hay C++*/ glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS); glColor3d(1.0,0.0,0.0); glVertex2d(0.1,0.1); glColor3d(0.0,1.0,0.0); glVertex2d(0.9,0.1); glColor3d(0.0,0.0,1.0); glVertex2d(0.9,0.9); 6

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

glColor3d(1.0,0.0,1.0); glVertex2d(0.1,0.9); glEnd(); glFlush(); } GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glLoadIdentity(); } int main(int argc, char *argv[]) { auxInitDisplayMode(AUX_RGBA); auxInitWindow(argv[0]); auxReshapeFunc(resize); auxMainLoop(draw); return 0; } 7-Quan st Khung nhn: Chng trnh trn, khi bn thay i kch c c lc bn khng nhn thy hnh vung m chng ta v na, ti sao li nh vy?Cu tr li nm trong chng trnh di y: /*filename: view.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif GLvoid CALLBACK draw(void){ glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS); glColor3d(1.0,0.0,0.0); glVertex2d(0.1,0.1); glColor3d(0.0,1.0,0.0); glVertex2d(0.9,0.1); glColor3d(0.0,0.0,1.0); glVertex2d(0.9,0.9); glColor3d(1.0,0.0,1.0); glVertex2d(0.1,0.9); glEnd(); glFlush(); 7

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

} GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glLoadIdentity(); glViewport(0,0,w,h); /*hm mi*/ glOrtho(-1.0,1.0,-1.0,1.0,0.0,1.0); /*hm mi*/ } int main(int argc, char *argv[]) { auxInitDisplayMode(AUX_RGBA); auxInitWindow(argv[0]); auxReshapeFunc(resize); auxMainLoop(draw); return 0; } Ti s gii thiu vi cc bn th no l Viewport.Viewport xc nh cng nhn cho chng ta, tc l phn khng gian trn ca s window ma ngi quan st c php quan st.N chnh l mt hnh ch nht.Hai tham s u tin ca hm ny xc nh to ca nh trn cng pha tay tri ca hnh ch nht, hai to d sau xc nh chiu rng v chiu cao ca hnh ch nht y.Vi cc tham s trn ta c th thy , chng trnh trn cho php ta quan st ton b mn hnh. Tip theo l kiu nhn glOrtho().Quan st hnh v di y:

Nh bn thy trn hnh, hm glOrtho(), xc lp mt ma trn cho php chng ta nhn theo kiu nh hnh v, y l hm tng qut: void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far); Tng ng vi chng trnh trn ca chng ta left l 1.0, right l 1.0, bottom l 1.0, top l 1.0, near l 0.0 v far l 1.0. Trong phn ny ti mun trnh by thm mt hm s na.Cc chng trnh trn u to ca s vi chiu di v rng xc inh, mun to mt ca s c kch c theo mun bn dng hm sau: auxInitPosition(), n c 4 thng s l to x, y ca nh trn bn tay tri ca ca s, chiu rng v chiu di ca ca s.Di y l m ngun: /*filename : size.cpp*/ #ifdef unix 8

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

#include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif GLvoid CALLBACK draw(void){ glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS); glColor3d(1.0,0.0,0.0); glVertex2d(0.1,0.1); glColor3d(0.0,1.0,0.0); glVertex2d(0.9,0.1); glColor3d(0.0,0.0,1.0); glVertex2d(0.9,0.9); glColor3d(1.0,0.0,1.0); glVertex2d(0.1,0.9); glEnd(); glFlush(); } GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glLoadIdentity(); glViewport(0,0,w/2,h/2); glOrtho(-1.0,1.0,-1.0,1.0,0.0,1.0); } int main(int argc, char *argv[]) { auxInitPosition(200,100,640,480); /*hm mi*/ auxInitDisplayMode(AUX_RGBA); auxInitWindow(argv[0]); auxReshapeFunc(resize); auxMainLoop(draw); return 0; } 8-Chut: Trong cc tr chi ta u thy s quan trng ca vic s dng chut, trong phn ny chng ta s xem xt lm th no chng trnh chng ta nhn ra chng ta ang bm tri chut, chng ta ang di chuyn chut. lm c iu ny chng ta s dng hm auxMouseFunc().Di y l m ngun ca chng trnh mouse1.cpp /*filename mouse1.cpp*/ 9

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

#ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #include"stdio.h" /*nu bn khng c dng ny th hm printf() khng thc hin*/ #endif GLvoid CALLBACK draw(void){ glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS); glColor3d(1.0,0.0,0.0); glVertex2d(0.1,0.1); glColor3d(0.0,1.0,0.0); glVertex2d(0.9,0.1); glColor3d(0.0,0.0,1.0); glVertex2d(0.9,0.9); glColor3d(1.0,0.0,1.0); glVertex2d(0.1,0.9); glEnd(); glFlush(); } GLvoid CALLBACK left(AUX_EVENTREC *event) { printf("%d,%d\n",event->data[AUX_MOUSEX],event->data[AUX_MOUSEY]); } GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glLoadIdentity(); glViewport(0,0,w/2,h/2); glOrtho(-1.0,1.0,-1.0,1.0,0.0,1.0); } int main(int argc, char *argv[]) { auxInitPosition(200,100,640,480); auxInitDisplayMode(AUX_RGBA); auxInitWindow(argv[0]); auxReshapeFunc(resize); /*hm mi*/ auxMouseFunc(AUX_LEFTBUTTON,AUX_MOUSEDOWN,left); /*hm mi*/ 10

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

auxMainLoop(draw); return 0; } Trong chng trnh trn , chng ta thy xut hin hm left() v hm auxMouseFunc().Hm auxMouseFunc() c gi n hm left(), n c ngha rng, khi chut c bm th s thc hin hm left().Trong tham s ca hm auxMouseFunc() c cc tham s sau: tham s u tin ni n phn no ca chut c tc ng, tham s th 2 ni n n c tc ng nh th no, v tham s cui cng mun ni tc ng ri th lm g.Trong hm left() tham s c dng con tr v c kiu l AUX_EVENTREC, n ly d liu v to x v y ca chut.Trong mt chng trnh khng phi l ch c mt hm auxMouseFunc() m bn c th dng bao nhiu tu thch, min l ng va chm nhau l c, trong phn m ngun ti c cho thm mt chng trnh v d v cch dng 2 ln hm auxMouseFunc()(trong file mouse2.cpp) Di y ti s trnh by mt chng trnh kh th v , m ngun ca n nh sau: /*filename connectlines.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #include"stdio.h" #endif GLvoid CALLBACK draw(void){ } GLvoid CALLBACK left(AUX_EVENTREC *event) { static int flag=0; static GLint x,y; if(flag){ glColor3d(0.0,0.0,0.0); glBegin(GL_LINE_STRIP); glVertex2i(x,y); glVertex2i(event->data[AUX_MOUSEX],event->data[AUX_MOUSEY]); glEnd(); glFlush(); } x=event->data[AUX_MOUSEX]; y=event->data[AUX_MOUSEY]; flag=1; } GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glLoadIdentity(); glViewport(0,0,w,h); 11

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

glOrtho(0.0,(GLdouble)w,(GLdouble)h,0.0,0.0,1.0);/* i thng s*/ glClearColor(1.0,1.0,1.0,0.0); /*chuyn v tr 2 hm ny*/ glClear(GL_COLOR_BUFFER_BIT); } int main(int argc, char *argv[]) { auxInitPosition(200,100,640,480); auxInitDisplayMode(AUX_RGBA); auxInitWindow(argv[0]); auxReshapeFunc(resize); auxMouseFunc(AUX_LEFTBUTTON,AUX_MOUSEDOWN,left); auxMainLoop(draw); return 0; } Thc ra chng trnh ny rt d hiu, c l khng phi trnh by g nhiu.N lu c im li v ni thnh mt ng gp khc.Nhc im ca chng trnh trn hn cc bn r khi bin dch n.N khng v li ca s ca bn khi ca s ca bn b che bi mt ca s khc, hay b minimize, tc l hnh m bn mun v khng c gi ti hm draw().V vy bn phi lu nhng im chn v v li chng trong hm draw().Di y l m ngun: /*filename connectlines1.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif #define MAXPOINTS 100 GLint point[MAXPOINTS][2]; int num=0; GLvoid CALLBACK draw(void) { int i; if(num>=2){ glClearColor(1.0,1.0,1.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3d(0.0,0.0,0.0); glBegin(GL_LINE_STRIP); for(i=0;i<num;i++) { glVertex2iv(point[i]); } glEnd(); glFlush(); } 12 /*s im ti a c th c chn*/ /*mng lu tr cc im */ /*s im chn n thi im hin ti*/

/*bn hy nh cu trc ny*/

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

} GLvoid CALLBACK left(AUX_EVENTREC *event) { if(num>=MAXPOINTS) return; point[num][0]=event->data[AUX_MOUSEX]; point[num][1]=event->data[AUX_MOUSEY]; num++; }

/*gii hn s im bn v */ /*lu tr to x ca chut*/ /*lu tr to y ca chut*/ /*tng s im sau mi ln bm*/

GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glLoadIdentity(); glViewport(0,0,w,h); glOrtho(0.0,(GLdouble)w,(GLdouble)h,0.0,0.0,1.0); } int main(int argc, char *argv[]) { auxInitPosition(200,100,640,480); auxInitDisplayMode(AUX_RGBA); auxInitWindow(argv[0]); auxReshapeFunc(resize); auxMouseFunc(AUX_LEFTBUTTON,AUX_MOUSEDOWN,left); auxMainLoop(draw); return 0; } By gi bn khng phi lo n vic ca s khng chu v li khi n b che mt.Mt iu cng ng ch trong chng trnh trn l chng ta s dng hm glVertex2iv() hm ny c tham s l thnh vin ca mng v thnh vin ca mng c cc gi tr x,y l s nguyn, ch i trong phn hu t ca hm trn biu hin cho gi tr nguyn cn ch v biu hin cho kiu pointer.Di y cung cp cho bn mt chng trnh c th v c c nhng ng gp khc v cc a gic.M ngun khng c g phc tp v ng bn y c, n ch l cch sp xp d liu v c thm mt hm right() m thc ra ti cp cc phn trn. /*filename connectlines2.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif #define MAXPOINTS 100 GLint point[MAXPOINTS][2]; int num=0; int flag=0; GLvoid CALLBACK draw(void) 13

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

{ int i; if(num>=2){ if(flag){ flag=0; i=num-2; glColor3d(0.0,0.0,0.0); glBegin(GL_LINE_STRIP); } else{ i=0; glClearColor(1.0,1.0,1.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3d(0.0,0.0,0.0); glBegin(GL_POLYGON); } for(;i<num;i++) { glVertex2iv(point[i]); } glEnd(); glFlush(); } } GLvoid CALLBACK left(AUX_EVENTREC *event) { if(num>=MAXPOINTS) return; point[num][0]=event->data[AUX_MOUSEX]; point[num][1]=event->data[AUX_MOUSEY]; num++; flag=1; } GLvoid CALLBACK right(AUX_EVENTREC *event) { draw(); } GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glLoadIdentity(); glViewport(0,0,w,h); glOrtho(0.0,(GLdouble)w,(GLdouble)h,0.0,0.0,1.0); glClearColor(1.0,1.0,1.0,0.0); glClear(GL_COLOR_BUFFER_BIT); } int main(int argc, char *argv[]) { 14

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

auxInitPosition(200,100,640,480); auxInitDisplayMode(AUX_RGBA); auxInitWindow(argv[0]); auxReshapeFunc(resize); auxMouseFunc(AUX_LEFTBUTTON,AUX_MOUSEDOWN,left); auxMouseFunc(AUX_RIGHTBUTTON,AUX_MOUSEDOWN,right); auxMainLoop(draw); return 0; } 8-Th hin to 3 chiu: n gi cc bn mi bit n to 2 chiu trong opengl, nu ch c vy th chng khc g trong lp trnh Window c.V vy trong phn ny chng ta s cng xem opengl v cc hnh 3 chiu nh th no. /*filename : rotated45.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif GLvoid CALLBACK draw(void) { glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glRotated(45,0.0,1.0,0.0); /*quay quanh trc OY 45 */ glBegin(GL_QUADS); glColor3d(1.0,0.0,0.0); glVertex2d(0.1,0.1); glColor3d(0.0,1.0,0.0); glVertex2d(0.9,0.1); glColor3d(0.0,0.0,1.0); glVertex2d(0.9,0.9); glColor3d(1.0,0.0,1.0); glVertex2d(0.1,0.9); glEnd(); glFlush(); } GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glLoadIdentity(); glViewport(0,0,w,h); glOrtho(-1.0,1.0,-1.0,1.0,0.0,1.0); } 15

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

int main(int argc, char *argv[]) { auxInitPosition(200,100,640,480); auxInitDisplayMode(AUX_RGBA); auxInitWindow(argv[0]); auxReshapeFunc(resize); auxMainLoop(draw); return 0; } Chng trnh ny khng c g c bit ngoi hm glRotated() , hm ny cho php chng ta quay hnh t gic ca chng ta quanh trc OY vi gc quay 45 .Tham s u tin ca n l gc s c quay, 3 tham s sau l tham s ca vector m hnh ca chng ta s quay vi gc quay trn.Bn nhn thy rng cc gi tr ca vector chng ta l : to x bng 0, to y bng 1, to z bng 0. Tc l vct ca chng ta thng ng theo trc OY, bn c th thay i cc thng s ca vector ny kim nghim hm ny xem !Cc gi tr ca cc thng s ny l kiu double.(Ch nu khng th cc thng s khc th bn s rt kh quan st hm ny hot ng ra sao ) Tip theo ti xin trnh by vi cc bn cch v mt hnh lp phng tht s bng opengl. /*filename cube1.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif GLdouble vertex[][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} }; int edge[][2]={ {0,1}, {1,2}, {2,3}, {0,3}, {4,5}, {5,6}, {6,7}, {7,4}, {0,4}, /*Khai bo d liu cho tm nh ca hnh lp phng*/

/*Khai bo cc cnh, m chng ta s s dng d liu*/ /*ca cc nh bn trn*/

16

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

{1,5}, {2,6}, {3,7} }; GLvoid CALLBACK draw(void) { int i; glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3d(1.0,1.0,1.0); glBegin(GL_LINES); for(i=0;i<12;i++){ glVertex3dv(vertex[edge[i][0]]); glVertex3dv(vertex[edge[i][1]]); } glEnd(); glFlush(); } GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glLoadIdentity(); glViewport(0,0,w,h); glOrtho(-2.0,2.0,-2.0,2.0,0.0,2.0); //gluPerspective(30.0,1.0,1.0,10.0); //gluLookAt(3.0,4.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); } int main(int argc, char *argv[]) { auxInitPosition(200,100,512,512); auxInitDisplayMode(AUX_RGBA); auxInitWindow(argv[0]); auxReshapeFunc(resize); auxMainLoop(draw); return 0; } Phn khai bo d liu, i vi cc bn chc tht d hiu, iu ng ni th nht l chng ta dng hm glVertex3dv() thay cho cc hm v nh 2 chiu trc y, hm ny nhn tham s l thnh vin ca mng, gi tr ca cc thnh vin phi l double, v n c to 3 chiu. Trc ht bin dch chng trnh trn, bn s cha thy g nu khng cho hai hm m ti nh du bn trn vo.Cc bn hy nh li cch quan st bng glOrtho() trc y v d dng nhn thy chng ta khng th nhn thy ton b hnh lp phng c m ch l mt hnh vung, v cch nhn bng glOrtho() ch cho ta nhn song song thi.Chnh v vy m chng ta phi chuyn qua cch quan st bng gluPerspective(), bn tham s ca n c trnh by nh sau:

/*hm mi*/ /*hm mi*/

17

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble near, GLdouble far); Tham s u tin l gc quan st trong mt phng XOZ, trong v d trn gc l 30, tham s th hai l t l gia w v h, n bng w/h, tham s th 3 v th 4 th hn cc bn quen khi quan st bng glOrtho().Nu ch c hm ny khng thi th chng ta vn cha quan st c hnh lp phng m chng ta v. quan st c chng ta phi dng thm hmgluLookAt() Hm ny c ti 9 tham s, nhng thc ra n nm trong 3 tham s chnh.Tham s u tin l v tr ca mt, cng c th coi l v tr ca camera(ch l trong to 3 chiu, nn v tr ca mt cha 3 to ), tham s th 2 l im nhn, v tham s th 3 gi l upvector, t ny khng bit dch ra ting vit ra sao.Upvector , hy tng tng bn ang theo di mt vt, upvector chnh l vector t tim bn ln nh u, nu thay i s liu cng tng t nh bn nghing u sang phi sang tri.Vy l 9 tham s r, by gi hy b lnh glOrtho()i v cho 2 lnh nh du vo, chng ta s quan st c hnh lp phng , m ngun nm trong file cube2.cpp 9.Animation(Hot cnh) Phn ny s gii thiu vi cc bn v cch to hot cnh trong opengl.Hot cnh lun lun c sc thu ht ngi lp trnh, n l mt phn quan trng trong lp trnh ho. Trc ht chng ta s xem xt hai hm auxIdleFunc() v auxMainLoop().Hm auxIdleFun() c nhim v gi cc hm trong khi khng nhn mt s kin (event) ca ngi dng, trong chng trnh di y, c th l n s v li window khi khng c event no.Cn hm auxMainLoop() ch v li window khi c mt s kin c th nh ngi dng di chuyn ca s, nt c bm, b ca s khc ln.... quan st c r rng chng ta cng phi dng n hm glMatrixMode().Khi thay i modeling v viewing th phi thay i ma trn ca n, bng cch dng hai thng s GL_MODELVIEW v GL_PROJECTION, v nu ch thay i trong lc khi to window th ta s khng thu c tc dng ca cc hm ny khi ca s b thay i, chnh v th m chng ta n trong hm resize(), v ma trn trn c lp i lp li nn chng ta hm glMatrixMode(GL_MODELVIEW) sau cng.Di y l m ngun: /*filename animation1.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif GLdouble vertex[][3]={ 18

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

{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} }; int edge[][2]={ {0,1}, {1,2}, {2,3}, {0,3}, {4,5}, {5,6}, {6,7}, {7,4}, {0,4}, {1,5}, {2,6}, {3,7} }; GLvoid CALLBACK none(void) { } GLvoid CALLBACK draw(void) { int i; static int r=0; glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); gluLookAt(3.0,4.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); glRotated((double)r,0.0,1.0,0.0); glColor3d(1.0,1.0,1.0); glBegin(GL_LINES); for(i=0;i<12;i++){ glVertex3dv(vertex[edge[i][0]]); glVertex3dv(vertex[edge[i][1]]); } glEnd(); glFlush(); if(++r>=360) r=0; } 19

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0,0,w,h); gluPerspective(30.0,1.0,1.0,10.0); glMatrixMode(GL_MODELVIEW); } int main(int argc, char *argv[]) { auxInitPosition(200,100,512,512); auxInitDisplayMode(AUX_RGBA); auxInitWindow(argv[0]); auxReshapeFunc(resize); auxIdleFunc(draw); auxMainLoop(none); return 0; } Trong chng trnh trn, mi ln hm draw()c gi th gi tr r c tng ln mt n v nu vt qu 360 th n s tr v 0.Chng ta phi thnh lp hm none() mc d n khng thc hin mt chc nng g, nhng hm auxMainLoop() cn mt hm gi n n nn ta to hm none.Tuy vy bn cng ch nhn thy nhp nhy ca hnh lp phng, c th quan st c hy bin dch m ngun ca chng trnh sau: /*filename animation2.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif GLdouble vertex[][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} }; int edge[][2]={ {0,1}, {1,2}, {2,3}, 20

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

{0,3}, {4,5}, {5,6}, {6,7}, {7,4}, {0,4}, {1,5}, {2,6}, {3,7} }; GLvoid CALLBACK none(void) { } GLvoid CALLBACK draw(void) { int i; static int r=0; glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); gluLookAt(3.0,4.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); glRotated((double)r,0.0,1.0,0.0); glColor3d(1.0,1.0,1.0); glBegin(GL_LINES); for(i=0;i<12;i++){ glVertex3dv(vertex[edge[i][0]]); glVertex3dv(vertex[edge[i][1]]); } glEnd(); auxSwapBuffers(); /*hm mi*/ if(++r>=360) r=0; } GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0,0,w,h); gluPerspective(30.0,1.0,1.0,10.0); glMatrixMode(GL_MODELVIEW); } int main(int argc, char *argv[]) { auxInitPosition(200,100,512,512); auxInitDisplayMode(AUX_RGBA|AUX_DOUBLE);/*thng s mi*/ auxInitWindow(argv[0]); 21

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

auxReshapeFunc(resize); auxIdleFunc(draw); auxMainLoop(none); return 0; } Trong hm draw() ti b i hm glFlush() v thay bng hm auxSwapBuffers(), s dng double buffer l mt k thut trnh hin tng nhp nhy mn hnh m cc bn mi lp trnh ho thng mc phi.K thut ny c m t nh sau: dng mt offbuffer, ri v ln sau mi a ln mn hnh, tng tng nu bn ct tng ch ri dn ln ngi xem thy th h s nhn thy bn dn tng ch mt, nhng nu bn dn ln ng sau t giy ri lt ngc li th h khng bit l n c dn tng ch mt(tng trng thi, ch ngi ta bit tha) dng c double buffers bn phi thm thng s AUX_DOUBLE trong hm auxInitDisplayMode().By gi bn c mt hnh lp phng chuyn ng mn mng quanh trc OY. Tht ra hnh lp phng m chng ta v ch l mt khung ca hnh lp phng thi, cn cc mt th chng ta cha v, v th m cc nt khut chng ta vn nhn thy, by gi chng ta s dng tham s GL_QUADS v hnh lp phng vi cc mt y v phn b khut s khng nhn thy c.M ngun: /*filename : animation3.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif GLdouble vertex[][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} }; int face[][4]={ {0,1,2,3}, {1,5,6,2}, {5,4,7,6}, {4,0,3,7}, {4,5,1,0}, {3,2,6,7} }; 22

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

GLvoid CALLBACK none(void) { } GLvoid CALLBACK draw(void) { int i,j; static int r=0; glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); gluLookAt(3.0,4.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); glRotated((double)r,0.0,1.0,0.0); glColor3d(1.0,1.0,1.0); glBegin(GL_QUADS); for(i=0;i<6;i++) for(j=0;j<4;j++){ glVertex3dv(vertex[face[i][j]]); } glEnd(); auxSwapBuffers(); if(++r>=360) r=0; } GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0,0,w,h); gluPerspective(30.0,1.0,1.0,10.0); glMatrixMode(GL_MODELVIEW); } int main(int argc, char *argv[]) { auxInitPosition(200,100,512,512); auxInitDisplayMode(AUX_RGBA|AUX_DOUBLE); auxInitWindow(argv[0]); auxReshapeFunc(resize); auxIdleFunc(draw); auxMainLoop(none); return 0; } Tip theo chng ta s thm mu vo hnh ca chng ta c sinh ng, bn ch cch sp xp d liu ca ti, rt nhiu nh lp trnh thch cch sp xp ny.M ngun: /*filename animation4.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" 23

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

#define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif GLdouble vertex[][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} }; int face[][4]={ {0,1,2,3}, {1,5,6,2}, {5,4,7,6}, {4,0,3,7}, {4,5,1,0}, {3,2,6,7} }; GLdouble color[][3]={ {1.0,0.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}, {1.0,1.0,0.0}, {1.0,0.0,1.0}, {0.0,1.0,1.0} }; GLvoid CALLBACK none(void) { } GLvoid CALLBACK draw(void) { int i,j; static int r=0; glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); gluLookAt(3.0,4.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); glRotated((double)r,0.0,1.0,0.0); 24

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

glBegin(GL_QUADS); for(i=0;i<6;i++){ glColor3dv(color[i]); for(j=0;j<4;j++){ glVertex3dv(vertex[face[i][j]]); } } glEnd(); auxSwapBuffers(); if(++r>=360) r=0; } GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0,0,w,h); gluPerspective(30.0,1.0,1.0,10.0); glMatrixMode(GL_MODELVIEW); } int main(int argc, char *argv[]) { auxInitPosition(200,100,512,512); auxInitDisplayMode(AUX_RGBA|AUX_DOUBLE); auxInitWindow(argv[0]); auxReshapeFunc(resize); auxIdleFunc(draw); auxMainLoop(none); return 0; } Tm thi dng phn animation y chng ta chuyn qua phn khc. 10-S dng Depth buffer test: Mi nghe tn thi cc bn cng c th hnh dung c cng vic chng ta sp lm l to chiu su v xa t khi quan st vt t im quan st.Trong phn ny chng ta s dng thm th vin na v th hy thm vo tiu file GL/glu.h(ci ny cng c sn trong VC bn khng phi download u c).Di y l m ngun /*filename depth1.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif #include<GL/glu.h> GLdouble vertex[][3]={ 25

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

{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} }; int face[][4]={ {0,1,2,3}, {1,5,6,2}, {5,4,7,6}, {4,0,3,7}, {4,5,1,0}, {3,2,6,7} }; GLdouble color[][3]={ {1.0,0.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}, {1.0,1.0,0.0}, {1.0,0.0,1.0}, {0.0,1.0,1.0} }; GLvoid CALLBACK none(void) { } GLvoid CALLBACK draw(void) { int i,j; static int r=0; glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);/*thng s mi*/ glLoadIdentity(); gluLookAt(3.0,4.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); glRotated((double)r,0.0,1.0,0.0); glEnable(GL_DEPTH_TEST); /*hm mi*/ glBegin(GL_QUADS); for(i=0;i<6;i++){ glColor3dv(color[i]); for(j=0;j<4;j++){ glVertex3dv(vertex[face[i][j]]); } 26

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

} glEnd(); glDisable(GL_DEPTH_TEST); auxSwapBuffers(); if(++r>=360) r=0; } GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0,0,w,h); gluPerspective(30.0,1.0,1.0,10.0); glMatrixMode(GL_MODELVIEW); } int main(int argc, char *argv[]) { auxInitPosition(200,100,512,512); auxInitDisplayMode(AUX_RGBA|AUX_DOUBLE|AUX_DEPTH);/*thng s mi*/ auxInitWindow(argv[0]); auxReshapeFunc(resize); auxIdleFunc(draw); auxMainLoop(none); return 0; } Trong chng trnh trn chng ta s dng tham s GL_DEPTH_BUFFER_BIT, mun xo buffer z th chng ta phi dng tham s ny(mc ch l ci t z buffer), hai hm glEnable() v glDisable() c tc dng khi ng buffer z(chiu su) v tt n i, c dng vi thng s GL_DEPTH_TEST.Trong hm auxInitDisplayMode() chng ta cng phi thm thng s AUX_DEPTH c th quan st c chiu su ca vt.Tuy vy chng trnh trn c mt s yu im, l vic lin quan n cc mt khut.Th bin dch m ngun di y v quan st s khc nhau: /*filename depth2.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif #include<GL/glu.h> GLdouble vertex[][3]={ {0.0,0.0,0.0}, {1.0,0.0,0.0}, 27 /*hm mi*/

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

{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} }; int face[][4]={ {0,1,2,3}, {1,5,6,2}, {5,4,7,6}, {4,0,3,7}, {4,5,1,0}, {3,2,6,7} }; GLdouble color[][3]={ {1.0,0.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}, {1.0,1.0,0.0}, {1.0,0.0,1.0}, {0.0,1.0,1.0} }; GLvoid CALLBACK none(void) { } GLvoid CALLBACK draw(void) { int i,j; static int r=0; glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt(3.0,4.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); glRotated((double)r,0.0,1.0,0.0); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glBegin(GL_QUADS); for(i=0;i<6;i++){ glColor3dv(color[i]); for(j=0;j<4;j++){ glVertex3dv(vertex[face[i][j]]); } } 28

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

glEnd(); glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); auxSwapBuffers(); if(++r>=360) r=0; } GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0,0,w,h); gluPerspective(30.0,1.0,1.0,10.0); glMatrixMode(GL_MODELVIEW); } int main(int argc, char *argv[]) { auxInitPosition(200,100,512,512); auxInitDisplayMode(AUX_RGBA|AUX_DOUBLE|AUX_DEPTH); auxInitWindow(argv[0]); auxReshapeFunc(resize); auxIdleFunc(draw); glCullFace(GL_BACK); auxMainLoop(none); return 0; } cc bn thy r s khc nhau ri ch?Chng trnh trc, cc cnh dng nh song song, trng khng ging mt vt 3 chiu cht no, v cc mt c chn th hin cha c lm chnh xc.Mun hiu r hn xin xem ti trang opengl.org. 10-S dng nh sng trong opengl. xc nh mt no c chiu sng v vi cng sng bao nhiu, ngi ta dng vc t php tuyn (normal vector).Trong chng trnh di y ti s gii thiu cch dng vc t ny.M ngun: /*filename: light1.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif #include<GL/glu.h> GLdouble vertex[][3]={ {0.0,0.0,0.0}, 29

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

{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} }; int face[][4]={ {0,1,2,3}, {1,5,6,2}, {5,4,7,6}, {4,0,3,7}, {4,5,1,0}, {3,2,6,7} }; GLdouble normal[][3]={ {0.0,0.0,-1.0}, {1.0,0.0,0.0}, {0.0,0.0,1.0}, {-1.0,0.0,0.0}, {0.0,-1.0,0.0}, {0.0,1.0,0.0} }; GLvoid CALLBACK none(void) { } GLvoid CALLBACK draw(void) { int i,j; static int r=0; glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt(3.0,4.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); glRotated((double)r,0.0,1.0,0.0); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); /*tham s mi*/ /*tham s mi*/

glBegin(GL_QUADS); for(i=0;i<6;i++){ glNormal3dv(normal[i]); /*hm mi*/ for(j=0;j<4;j++){ glVertex3dv(vertex[face[i][j]]); 30

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

} } glEnd(); glDisable(GL_LIGHT0); glDisable(GL_LIGHTING); glDisable(GL_DEPTH_TEST); auxSwapBuffers(); if(++r>=360) r=0; } GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0,0,w,h); gluPerspective(30.0,1.0,1.0,10.0); glMatrixMode(GL_MODELVIEW); } int main(int argc, char *argv[]) { auxInitPosition(200,100,512,512); auxInitDisplayMode(AUX_RGBA|AUX_DOUBLE|AUX_DEPTH); auxInitWindow(argv[0]); auxReshapeFunc(resize); auxIdleFunc(draw); glCullFace(GL_BACK); auxMainLoop(none); return 0; } Cc bn thy thng s mi trong hm glEnable() v glDisable(), cc thng s ny cho php chng ta s dng nh sng trong khi to vt.Opengl cung cp cho chng ta 8 ngun sng m chng trnh trn mi ch s dng mt ngun sng LIGHT0, hm glNormal3dv() thit lp vc t php tuyn cho mi mt chng ta v, v th khi vt th quay quanh trc th ta s thy sng ti thay i. Trong chng trnh light0.cpp trn chng ta khng s dng mu nn mu s c xc nh l mu mc nh nh cc bn thy.Tip theo y ti xin gii thiu cch s dng vt liu to vt vi cc mu sc n tng khc nhau.Chng ta khai bo d liu sau: GLfloat ambient[]={ 0.2,0.2,0.2,1.0}; GLfloat diffuse[]={ 0.8,0.0,0.0,1.0}; GLfloat specular[]={0.5,0.5,0.5,1.0}; GLfloat shininess=40.0; Trong cc thng s trn u c t theo h RGBA(nh cp cc phn trn), chnh v th m chng c 4 gi tr.Thng s ambient biu hin cho sng ca mi trng bn ngoi, diffuse l khuch tn, specular l phn x v shininess l bng( bng sng). 31 /*tham s mi*/ /*tham s mi*/

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

Cc thng s trn c hm glMaterial*() s dng to vt th ca chng ta.Hy quan st cch khai bo hm trn: glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,ambient); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,diffuse); glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specular); glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,shininess); Ch l lnh cui cng khng phi l fv nh bnh thng, v shininess ch l mt s. y l m ngun ca chng trnh. /*filename light1.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif #include<GL/glu.h> GLdouble vertex[][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} }; int face[][4]={ {0,1,2,3}, {1,5,6,2}, {5,4,7,6}, {4,0,3,7}, {4,5,1,0}, {3,2,6,7} }; GLdouble normal[][3]={ {0.0,0.0,-1.0}, {1.0,0.0,0.0}, {0.0,0.0,1.0}, {-1.0,0.0,0.0}, {0.0,-1.0,0.0}, {0.0,1.0,0.0} }; 32

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

GLfloat ambient[]={ 0.2,0.2,0.2,1.0}; GLfloat diffuse[]={ 0.8,0.0,0.0,1.0}; GLfloat specular[]={0.5,0.5,0.5,1.0}; GLfloat shininess=40.0; GLvoid CALLBACK none(void) { } GLvoid CALLBACK draw(void) { int i,j; static int r=0; glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt(3.0,4.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); glRotated((double)r,0.0,1.0,0.0); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,ambient); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,diffuse); glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specular); glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,shininess); glBegin(GL_QUADS); for(i=0;i<6;i++){ glNormal3dv(normal[i]); for(j=0;j<4;j++){ glVertex3dv(vertex[face[i][j]]); } } glEnd(); glDisable(GL_LIGHT0); glDisable(GL_LIGHTING); glDisable(GL_DEPTH_TEST); auxSwapBuffers(); if(++r>=360) r=0; } GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0,0,w,h); 33

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

gluPerspective(30.0,1.0,1.0,10.0); glMatrixMode(GL_MODELVIEW); } int main(int argc, char *argv[]) { auxInitPosition(200,100,512,512); auxInitDisplayMode(AUX_RGBA|AUX_DOUBLE|AUX_DEPTH); auxInitWindow(argv[0]); auxReshapeFunc(resize); auxIdleFunc(draw); glCullFace(GL_BACK); auxMainLoop(none); return 0; } 11-V nhiu vt-Dng Ma trn. Trong phn ny ti s gii thiu vi cc bn cch v c v chuyn nghip hn mt cht, c th t y bn s to c nhng iu m mnh mong mun. Trc ht hy to mt hm v hnh lp phng: void cube() { int i,j; glBegin(GL_QUADS); for(i=0;i<6;i++){ glNormal3dv(normal[i]); for(j=0;j<4;j++){ glVertex3dv(vertex[face[i][j]]); } } glEnd(); } V im nhn khng thay i nn khi c ca window thay i ta cng phi thay i theo. Di y l m ngun thay i khung nhn ca chng ta: GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0,0,w,h); gluPerspective(30.0,1.0,1.0,10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(3.0,4.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); } Ch l hai hm cui trong hm trn c chuyn t hm draw() sang.Nu thit t ma trn nh trn th khi hm glRotated() lm thay i v tr ca vt s lm cho window ca chng ta tr nn khng bnh thng.V vy trc khi dng hm glRotated() th chng ta phi lu ma trn vo ri kh thc hin xong hm ny ta li tr li ma trn th s bnh thng. lm 34

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

vic ny chng ta s dng 2 hm glPushMatrix() v glPopMatrix().M ngun c trnh by di y. /*filename matrix1.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif #include<GL/glu.h> GLdouble vertex[][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} }; int face[][4]={ {0,1,2,3}, {1,5,6,2}, {5,4,7,6}, {4,0,3,7}, {4,5,1,0}, {3,2,6,7} }; GLdouble normal[][3]={ {0.0,0.0,-1.0}, {1.0,0.0,0.0}, {0.0,0.0,1.0}, {-1.0,0.0,0.0}, {0.0,-1.0,0.0}, {0.0,1.0,0.0} }; void cube() { int i,j; glBegin(GL_QUADS); for(i=0;i<6;i++){ 35

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

glNormal3dv(normal[i]); for(j=0;j<4;j++){ glVertex3dv(vertex[face[i][j]]); } } glEnd(); } GLvoid CALLBACK none(void) { } GLvoid CALLBACK draw(void) { static int r=0; glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glPushMatrix(); glRotated((double)r,0.0,1.0,0.0); cube(); glPopMatrix(); glDisable(GL_LIGHT0); glDisable(GL_LIGHTING); glDisable(GL_DEPTH_TEST); auxSwapBuffers(); if(++r>=360) r=0; } GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0,0,w,h); gluPerspective(30.0,1.0,1.0,10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(3.0,4.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); } int main(int argc, char *argv[]) { auxInitPosition(200,100,512,512); auxInitDisplayMode(AUX_RGBA|AUX_DOUBLE|AUX_DEPTH); auxInitWindow(argv[0]); 36

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

auxReshapeFunc(resize); auxIdleFunc(draw); glCullFace(GL_BACK); auxMainLoop(none); return 0; } By gi ta s tm cch v hai hnh lp phng m khng mt cng v li tng mt ca hnh lp phng mi na. lm c iu ny bn dng hm glTranslated(), hm ny c 3 thng s, n c nhim v chuyn ta n v tr mi v(qua ba thng s ca n).Thc ra n nhn ma trn hin ti vi mt ma trn khc chuyn to cho chng ta, v th m chng ta s phi dng 2 ln cc hm glPushMatrix() v glPopMatrix().Hy nh l bao nhiu ln gi hm glPushMatrix() th cng phi tng y ln gi hm glPopmatrix().Di y l m ngun: /*filename matrix2.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif #include<GL/glu.h> GLdouble vertex[][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} }; int face[][4]={ {0,1,2,3}, {1,5,6,2}, {5,4,7,6}, {4,0,3,7}, {4,5,1,0}, {3,2,6,7} }; GLdouble normal[][3]={ {0.0,0.0,-1.0}, {1.0,0.0,0.0}, 37

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

{0.0,0.0,1.0}, {-1.0,0.0,0.0}, {0.0,-1.0,0.0}, {0.0,1.0,0.0} }; void cube() { int i,j; glBegin(GL_QUADS); for(i=0;i<6;i++){ glNormal3dv(normal[i]); for(j=0;j<4;j++){ glVertex3dv(vertex[face[i][j]]); } } glEnd(); } GLvoid CALLBACK none(void) { } GLvoid CALLBACK draw(void) { static int r=0; glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glPushMatrix(); glRotated((double)r,0.0,1.0,0.0); cube(); glPushMatrix(); glTranslated(1.0,1.0,1.0); cube(); glPopMatrix(); glPopMatrix(); glDisable(GL_LIGHT0); glDisable(GL_LIGHTING); glDisable(GL_DEPTH_TEST); auxSwapBuffers(); if(++r>=360) r=0; } 38

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0,0,w,h); gluPerspective(30.0,1.0,1.0,10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(3.0,4.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); } int main(int argc, char *argv[]) { auxInitPosition(200,100,512,512); auxInitDisplayMode(AUX_RGBA|AUX_DOUBLE|AUX_DEPTH); auxInitWindow(argv[0]); auxReshapeFunc(resize); auxIdleFunc(draw); glCullFace(GL_BACK); auxMainLoop(none); return 0; } Tip theo chng ta s cho hnh lp phng th hai quay vi vn tc khc, vn tc mi ca n l 2*r, lm c iu ny bn ch cn thm hm glRotated((double)(2*r),0.0,1.0,0.0); vo sau hm glTranslated(1.0,1.0,1.0); v trc hm cube() th hai.M ngun nm trong file matrix3.cpp. By gi hy cng t mu cho chng. /*filename matrix4.cpp*/ #ifdef unix #include <GL/gl.h> #include "aux.h" #define CALLBACK #else #include<windows.h> #include<GL/gl.h> #include<GL/glaux.h> #endif #include<GL/glu.h> GLdouble vertex[][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}, 39

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

{1.0,1.0,1.0}, {0.0,1.0,1.0} }; int face[][4]={ {0,1,2,3}, {1,5,6,2}, {5,4,7,6}, {4,0,3,7}, {4,5,1,0}, {3,2,6,7} }; GLdouble normal[][3]={ {0.0,0.0,-1.0}, {1.0,0.0,0.0}, {0.0,0.0,1.0}, {-1.0,0.0,0.0}, {0.0,-1.0,0.0}, {0.0,1.0,0.0} }; void cube() { int i,j; glBegin(GL_QUADS); for(i=0;i<6;i++){ glNormal3dv(normal[i]); for(j=0;j<4;j++){ glVertex3dv(vertex[face[i][j]]); } } glEnd(); } GLvoid CALLBACK none(void) { } GLvoid CALLBACK draw(void) { static int r=0; static GLfloat red[]={ 1.0,0.0,0.0,1.0}; static GLfloat blue[]={ 0.0,0.0,1.0,1.0}; glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); 40

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

glPushMatrix(); glRotated((double)r,0.0,1.0,0.0); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,red); cube(); glPushMatrix(); glTranslated(1.0,1.0,1.0); glRotated((double)(2*r),0.0,1.0,0.0); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,blue); cube(); glPopMatrix(); glPopMatrix(); glDisable(GL_LIGHT0); glDisable(GL_LIGHTING); glDisable(GL_DEPTH_TEST); auxSwapBuffers(); if(++r>=360) r=0; } GLvoid CALLBACK resize(GLsizei w,GLsizei h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0,0,w,h); gluPerspective(30.0,1.0,1.0,10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(3.0,4.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); } int main(int argc, char *argv[]) { auxInitPosition(200,100,512,512); auxInitDisplayMode(AUX_RGBA|AUX_DOUBLE|AUX_DEPTH); auxInitWindow(argv[0]); auxReshapeFunc(resize); auxIdleFunc(draw); glCullFace(GL_BACK); auxMainLoop(none); return 0; } n y th khng cn gii thch g cc bn cng hiu chng trnh ny. y l vi gi cho nhng bn thch tm ti, cc bn c th dng cc hm va cp v mt h mt tri vi cc hnh tinh, hay v mt cnh tay robt c ng chng hnNu c iu kin ti s cn tip tc vit tip nhng bi khc v opengl , c th l th vin glut v n rt mnh.Hi vng mt ngy no gp li. Nu bi vit ca ti c g sai th vui lng gi cho ti li nhn, ti s sa li n tht ng v tht hp.Mi cc bn gh qua trang web ca ti, trn c mt s 41

DienDanTinHoc Tutorial Room

Pending to be conterted to HTML

chng trnh kh th v nu bn no cn m ngun th mail cho ti, ti s sn lng gi cho.Thn i.

42

You might also like