Professional Documents
Culture Documents
(L Thnh Sch)
Ni dung: Ci t v cu hnh OpenGL, Visual Studio trn Windows. Bin dch, thc thi, v tm hiu cu trc mt chng trnh ha dng OpenGL vi OpenGL Utility Toolkit (GLUT) v Microsoft Foundation Class (MFC). Vit chng trnh nh v cc i tng hnh hc 2D (im, ng thng, a gic, hnh trn, v.v ) v 3D (hnh hp, a in, v.v) n gin. thc hin tt phn thc hnh cho mn Computer Graphics, sinh vin cn c trong tay ti liu, th vin lp trnh ha, v cc chng trnh mu cho mi bui thc hnh. Ti liu: 1. Ti liu hng dn ny. 2. OpengGL reference book (blue book). 3. GLUT reference book. Th vin lp trnh ha: 1. Opengl core library(GL): a. Header filer: GL/gl.h b. Static link library: Opengl32.lib c. Dynamic link library: Opengl32.dll 2. Opengl utility library (GLU): a. Header filer: GL/glu.h b. Static link library: glu32.lib c. Dynamic link library: glu32.dll 3. Opengl utility toolkit(GLUT): a. Header filer: GL/glut.h b. Static link library: glut32.lib c. Dynamic link library: glut32.dll 4. Chng trnh mu: For GLUT: 1. D n mu: 2. D n 1: v hnh vung tnh 3. D n 2: v hnh vung ng 4. D n 3: v t din tnh 5. D n 4: v t din ng For MFC: 1. D n mu: 2. D n 1: v hnh vung tnh 3. D n 2: v hnh vung ng 4. D n 3: v t din tnh 5. D n 4: v t din ng Mc lc: I. Kin trc OpenGL trn Windows: ..........................................................................................3 II. Cu hnh mi trng pht trin tch hp (IDE): ..............................................................4 II.1 Cu hnh cho Opengl ............................................................................................... 4
Trang 1/20
II.2 Cu hnh cho GLUT ................................................................................................. 5 II.3 D n mu dng GLUT:........................................................................................... 5 II.4 D n mu dng MFC: ............................................................................................ 6 III. Tm hiu cu trc chng trnh ha: ..........................................................................7 III.1 Dng GLUT: .......................................................................................................... 7 1. D n 1: V hnh vung tnh ..........................................................................................7 2. D n 2: V hnh vung ng ........................................................................................8 3. D n 3: V a in .....................................................................................................10 4. D n 4: V a in, thay i v tr nhn.......................................................................12 III.2 Dng MFC:.......................................................................................................... 14 1. D n 1: V hnh vung tnh ........................................................................................14 2. D n 2: V hnh vung ng ......................................................................................16 3. D n 3: V t din tnh...............................................................................................18 IV. Vit chng ha n gin:.........................................................................................20
Trang 2/20
Hnh 1: Kin trc h tr vic thc thi chng trnh ha trn Windows
ng v pha ngi lp trnh, to ra chng trnh ha ni trn program.exe, h phi tri qua son tho (edit), bin dch (compile), lin kt (link), sau l chy th v sa li. Nu dng ngn ng C/C++ th phi cn n cc header file v th vin lin kt library. C th l cc tp tin nh sau: Header files: gl.h, glu.h Library: opengl32.lib, glu32.lib Nu ci t mi trng pht trin tch hp Visual Studio (VS) th cc tp tin ni trn nm trong cc th mc ch ra trong hnh 2 sau y. Cc tp tin trn cng c th c download min ph t Internet.
Trang 3/20
glu32.lib
Hnh 2: V tr v mi quan h gia cc tp tin trong qu trnh pht trin ng dng ha. Khi lp trnh (dng C/C++), ngi lp trnh phi thc hin cc bc sau: Include tp tin gl.h v glu.h pha trc bt k tham chiu no n cc hm, hng, etc ca Opengl, ngc li s c li bin dch. Ch r v tr ca cc header file v library, ngc li s c li bin dch. Ch r khi lin kt th dng opengl32.lib v glu32.lib, ngc li s c li lin kt.
Trang 4/20
b. Trn h thng cy thuc tnh, tm n Linker\Input\Additional Dependencies. B sung opengl32.lib; glu32.lib vo danh mc cc tp tin th vin dng n ca d n.
int main(int argc, char** argv){ printf("Add your own code to this program"); printf("\n"); getchar(); }
Trang 5/20
Trc khi ca s vng nhn hin th, hm OnSize c gi mt ln. Sau , c mi khi ngi dng thay i kch thc ca s chnh th OnSize c gi. Vi lp trnh OpenGL: OnSize l ni thch hp nht thit lp Viewport. Vic thit lp php chiu (projection) hay thng s camera cng c th t y. d) WM_TIMER OnTimer(UINT_PTR nIDEvent): OnTimer ch c nu nh trc hm SetTimer c gi. Hm SetTimer c th c gi OnCreate hay trong bt k hm x l s kin khc, nh khi ngi dng nhn chut hay phm no th gi SetTimer. Xem Code mu trong cc d n sau bit cch gi SetTimer. Hm SetTimer to ra mt Timer m n s c kch gi sau mi T miliseconds. Nh vy, khi thi gian T tri qua th OnTimer s c gi. V c th to ra nhiu Timer cng lc, nn khi OnTimer c gi, m s ca Timer cng c th c bi ngi lp trnh. Sau khi dng Timer, nn gi hm KillTimer xa b Timer. e) WM_PAINT OnPaint(UINT_PTR nIDEvent): OnPaint l hm quan trng vi lp trnh OpenGL. V tt c cc tc v v ca OpenGL u c c trong hm ny. V chc nng, hm ny tng t hm Callback c truyn vo glutDisplayFunc khi dng GLUT. OnPaint s c gi mi khi vng nhn cn v li, c th l: i) khi vng nhn va mi hin th ln u, 2i) vng nhn c nhn thy tr li sau khi b che khut, 3i) khi ngi lp trnh yu cu v li bi nhng hm nh InvalidateRect.
main(,,): im bt u chng trnh mydisplay(): hm v n gin, c ng k vi hm glutDisplayFunc ca GLUT. Tn hm ny c th c thay i bt k, nhng phi theo quy tc t tn mt danh hiu trong C. - glutCreateWindow: ca GLUT, cho php to mt ca s vi tiu l thng s . - glutDisplayFunc: dng hm ny ng k cc hm v (dng OpengGL) vi GLUT. - glutMainLoop: khi ng mt vng lp x l s kin ca GLUT. Bn trong mydisplay: 1. Xa color frame buffer: glClear 2. Xc lp mu v hin ti: glColor3f 3. V mt polygon: glBegin(GL_POLYGON) 4. Xut nh ra mn hnh: glFlush Cc cu hnh mc nhin (default): Viewer (camera): dng php chiu trc giao, vi volume l hnh hp vung trng tm vi Polygon ni trn, mi chiu c kch thc l 2 n v (t -1 n 1). Chiu sng: khng dng S color buffer: 1 (Single Buffer) -
2. D n 2: V hnh vung ng
1. Trong d n mu GlutTemplate, thay template.cpp bi tp tin demo2.cpp 2. Son demo2.cpp nh Hnh 5 3. Bin dch v thc thi
Trang 8/20
#include <GL/glut.h> #include <math.h> #include <stdio.h> GLfloat angle; #define DEG2RAD (3.14159f/180.0f) void mydisplay(){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f, 0.0f, 0.0f); GLfloat cx, cy; cx = 0.5*cos(DEG2RAD*angle); cy = 0.5*sin(DEG2RAD*angle); glBegin(GL_POLYGON); glVertex2f(cx glVertex2f(cx glVertex2f(cx glVertex2f(cx glEnd(); glFlush(); glutSwapBuffers();
+ +
cy cy cy cy
+ + -
} void processTimer(int value){ angle += (GLfloat)value; if(angle > 360) angle = angle - 360.0f; glutTimerFunc(100, processTimer, 10); glutPostRedisplay(); } int main(int argc, char** argv){ angle = 0.0f; glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutCreateWindow("simple"); glutDisplayFunc(mydisplay); glutTimerFunc(100, processTimer, 10); glutMainLoop(); }
Hnh 5: Chng trnh mu vi GLUT Cu trc chng trnh: - main(,,): - mydisplay(): - glutCreateWindow: ... - glutDisplayFunc: - glutMainLoop:.... - glutInitDisplayMode: Ci t ch hin th, c th: GLUT_DOUBLE: S dng hai buffer. Mt gi l front buffer, dng hin th nh ra mn hnh, nhng khi v th khng v trn buffer ny. Buffer cn li c gi l Back Buffer, dng v m khng hin th, khi v xong th hon v Back thanh Front v ngc li. GLUT_RGB: nh mu, nh dng RGB - processTimer: c gi bi GLUT khi thi gian ng k vi GLUT (bi hm glutTimerFunc) ti hn.
ha My tnh: Bi Thc hnh S 1 Trang 9/20
glutTimerFunc: Dng hm ny ng k hm x Timer vi GLUT. Timer hay dng to animation. Hm x l Timer trong GLUT c dng: void processFunc(int value), tn hm c th t khc, value l gi tr ca tham s th 3 c truyn vo hm glutTimerFunc. Bn trong mydisplay: 1. V hnh vung m tm ca n c cp nht theo thi gian, bin angle. Bin angle c thay i trong hm processTimer. 2. Hon v hai buffer (font buffer v back buffer) Bn trong processTimer: 1. Thay i bin angle (tm vc global, n tc ng n cc hm khc na). 2. Ci t Timer cho ln n. 3. Yu cu v li mn hnh: glutPostRedisplay. -
3. D n 3: V a in
1. Trong d n mu GlutTemplate, thay template.cpp bi tp tin demo3.cpp 2. Son demo3.cpp nh sau:
#include <GL/glut.h> #define min(a,b) ((a)<(b)?(a):(b)) float tetra_vertices[][3] = { {0.0, 0.0, 1.0}, {0.0, 0.942809, -0.33333}, {-0.816497, -0.471405, -0.33333}, {0.816497, -0.471405, -0.33333} }; void mydisplay(){ //setup viewwer - camera parameter: //i.e., location (eye), //direction (from eye to the reference point), and orientation (up vector) glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt( 1.5, //eyeX 1.5, //eyeY 1.5, //eyeX 0.0, //reference point X 0.0, //reference point Y 0.0, //reference point Z //up vector X 0.0, 1.0, //up vector Y 0.0 //up vector Z ); //clear screen glClear(GL_COLOR_BUFFER_BIT); //the order of the vertices in a triangle is important! glBegin(GL_TRIANGLES); //Face 1: defined by vertices: 0, 2, 1; colored: red glColor3f(1.0f, 0.0f, 0.0f); glVertex3fv(tetra_vertices[0]); glVertex3fv(tetra_vertices[2]); glVertex3fv(tetra_vertices[1]); //Face 2: defined by vertices: 0, 1, 3; colored: red glColor3f(0.0f, 1.0f, 0.0f);
Trang 10/20
glVertex3fv(tetra_vertices[0]); glVertex3fv(tetra_vertices[1]); glVertex3fv(tetra_vertices[3]); //Face 3: defined by vertices: 0, 3, 2; colored: red glColor3f(0.0f, 0.0f, 1.0f); glVertex3fv(tetra_vertices[0]); glVertex3fv(tetra_vertices[3]); glVertex3fv(tetra_vertices[2]); //Face 4: defined by vertices: 3, 1, 2; colored: red glColor3f(1.0f, 0.0f, 0.0f); glVertex3fv(tetra_vertices[3]); glVertex3fv(tetra_vertices[1]); glVertex3fv(tetra_vertices[2]); glEnd(); glFlush(); glutSwapBuffers(); } void reshape(int width, int height){ //setup viewport int size = min(width, height); glViewport(0, 0, size, size); } void initOpenGL(){ //setup projection type //glFrustrum: define viewing volume glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum( -1.0, //left 1.0, //right -1.0, //bottom 1.0, //top //near 2.0, 10.0 //far ); //Default MatrixMode is MODELVIEW glMatrixMode(GL_MODELVIEW); //setup background color, or clear color glClearColor(0.1f, 0.7f, 0.7f, 1.0f); } int main(int argc, char** argv){ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutCreateWindow("Drawing a Tetrahedron"); initOpenGL(); glutDisplayFunc(mydisplay); glutReshapeFunc(reshape); glutMainLoop(); }
3. Bin dch v thc thi Cu trc chng trnh: 1. main(): im bt u chng trnh 2. initOpenGL(): xc lp cc thng s ca Opengl dng v sau. - Tn hm ny do programmer t.
ha My tnh: Bi Thc hnh S 1 Trang 11/20
- L ni thch hp xc nh : a) Xc nh php chiu: bng cch gi glFurstrum, glOrtho, gluOrtho2D, gluPerspective b) Xc nh mu mc nh cho foreground v background: bng cch gi glColor3f (cho foreground) v glClearColor (cho background). 2. mydisplay(): hm v chnh, cc tc v v ca OpenGL c t trong hm ny. - c t Viewer, nh li trong Computer Graphics cn c t 3 i tng quan trng: Object, Viewer (Camera), v Light Source. Bng cch gi hm gluLookAt(), ngi lp trnh c th t t: v tr, phng nhn (ni t camera n mt im tham chiu reference point, v hng ln ca camera) - V cc mt ca t din bi cc tam gic, dng GL_TRIANGLES, t mu cc mt l Red, Green, Blue, v Black. Ch , th t cc nh khi t t tam gic, v n to ra mt m v dng. 3. reshape(int width, int height): - Hm ny c ng k l lm callback vi glutReshapeFunc(). Do , n s c gi ln u khi xc lp kch thc ca s. Sau , khi ca s hin th hm ny cng s c gi mi khi ngi dng thay i kch thc ca s, v d dng chut ko bin ca ca s. - Hm reshape l ni thch hp nht xc nh viewport. 4. Bin ton cc tetra_vertices, nh ngha ta 4 nh ca hnh t din.
Trang 12/20
//clear screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //the order of the vertices in a triangle is important! glBegin(GL_TRIANGLES); //Face 1: defined by vertices: 0, 2, 1; colored: red glColor3f(1.0f, 0.0f, 0.0f); glVertex3fv(tetra_vertices[0]); glVertex3fv(tetra_vertices[2]); glVertex3fv(tetra_vertices[1]); //Face 2: defined by vertices: 0, 1, 3; colored: red glColor3f(0.0f, 1.0f, 0.0f); glVertex3fv(tetra_vertices[0]); glVertex3fv(tetra_vertices[1]); glVertex3fv(tetra_vertices[3]); //Face 3: defined by vertices: 0, 3, 2; colored: red glColor3f(0.0f, 0.0f, 1.0f); glVertex3fv(tetra_vertices[0]); glVertex3fv(tetra_vertices[3]); glVertex3fv(tetra_vertices[2]); //Face 4: defined by vertices: 3, 1, 2; colored: red glColor3f(1.0f, 0.0f, 0.0f); glVertex3fv(tetra_vertices[3]); glVertex3fv(tetra_vertices[1]); glVertex3fv(tetra_vertices[2]); glEnd(); glFlush(); glutSwapBuffers(); } void reshape(int width, int height){ //setup viewport int size = min(width, height); glViewport(0, 0, size, size); } void processTimer(int value){ angle += (GLfloat)value; if(angle > 360.0f) angle -= 360.0f; glutTimerFunc(100, processTimer, value); glutPostRedisplay(); } void initOpenGL (){ //setup projection type //glFrustrum: define viewing volume glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum( -2.0, //left 2.0, //right -2.0, //bottom 2.0, //top 2.0, //near 10.0 //far ); //Default MatrixMode is MODELVIEW glMatrixMode(GL_MODELVIEW);
Trang 13/20
glEnable(GL_DEPTH_TEST); } int main(int argc, char** argv){ angle = 0.0f; glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("Drawing a Tetrahedron"); initOpenGL(); glutDisplayFunc(mydisplay); glutReshapeFunc(reshape); glutTimerFunc(100, processTimer, 10); glutMainLoop(); }
3. Bin dch v thc thi. Cu trc chng trnh: Nh d n 3, ch thay i: a. Tham s ha v tr camera theo bin angle. b. S dng DEPTH_BUFFER
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glEnable(GL_DEPTH_TEST);
Ci t OpenGL: 2. B sung hm SetupOpenGL. 3. B sung bin thnh vin m_hRC kiu HGLRC vo lp MFCTemplateView 4. Son hm SetupOpenGL nh sau:
//Declare Pixel Format static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA,
Trang 14/20
32, // bit depth 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, // z-buffer depth 0, 0, 0, 0, 0, 0, 0, }; // Get device context only once. HDC hdc = GetDC()->m_hDC; // Set Pixel format. int nPixelFormat = ChoosePixelFormat(hdc, &pfd); SetPixelFormat(hdc, nPixelFormat, &pfd); // Create the OpenGL Rendering Context. m_hRC = wglCreateContext(hdc); wglMakeCurrent(hdc, m_hRC);
5. B sung hm x l cho s kin WM_CREATE, Visual Studio sinh ra OnCreate(param). 6. Son hm OnCreate gi SetupOpenGL nh sau:
if (CView::OnCreate(lpCreateStruct) == -1) return -1; // Add only the following code. // The previous code is generated automatically by IDE SetupOpenGL(); return 0;
Thit lp viewport: 7. B sung hm x l cho s kin WM_SIZE, Visual Studio sinh ra OnSize(.,.). 8. Son hm OnSize nh sau:
CView::OnSize(nType, cx, cy); // Add only the following codes. // The previous codes is generated automatically by IDE //Setting up viewport CRect rect; GetClientRect(rect); int size = min(rect.Width(), rect.Height()); glViewport(0, 0, size, size);
Thc hin v mt hnh a gic n gin: 9. B sung hm x l cho thng ip WM_PAINT, Visual Studio sinh ra OnPaint() 10. Son hm OnPaint nh sau:
CPaintDC dc(this); // device context for painting // Add only the following codes. // The previous codes is generated automatically by IDE glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f, 0.0f, 0.0f); glBegin(GL_POLYGON); glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5);
Trang 15/20
glVertex2f(0.5, -0.5); glEnd(); glFlush(); SwapBuffers(dc.m_ps.hdc); // Swap the front and the back buffer to show the created graphics
Gii phng ti nguyn: 11. B sung hm RemoveOpenGL(), kiu void, vo lp MFCTemplateView 12. Son tho hm RemoveOpenGL nh sau:
//Delete Rendering Context if(wglGetCurrentContext() != NULL) wglMakeCurrent(NULL,NULL); if(m_hRC != NULL) { wglDeleteContext(m_hRC); m_hRC = NULL; }
13. B sung hm x l s kin WM_DESTROY, Visual Studio sinh ra OnDestroy(). 14. Son hm OnDestroy nh sau:
CView::OnDestroy(); //Remove openGL RemoveOpenGL();
Bin dch v thc thi: 15. Bin dch v thc thi. Bi tp m rng t D n 1: 1) Thay on m bc 9. dng glVertex2fv thay v glVertex2f. 2) bc 9., dng glVertex3f hay glVertex3fv v cc plolygon mt phng XY, XZ, v YZ. Nhng polygon nn c ta x, y, v z u nm trong on [-1,1]. Nhng polygon no trong s cc polygon trn nhn thy c, v sao? 3) bc 9., thay v dng GL_POLYGON, hy dng cc hng khc (xem BlueBook) v im, on thng, tam gic, chui tam gic,
2. D n 2: V hnh vung ng
Ni dung cng vic: B sung Header files cho OpenGL. Ci t OpenGL. Thit lp viewport. Tham s ha a gic D n 1 tm ca n thay i theo thi gian. Ci t v s dng Timer. Gii phng ti nguyn. Bin dch v thc thi. Cc bc thc hin: S dng li d n MFCTemplate, i.e., copy sang mt folder khc ri thay i m nh sau B sung Header files cho OpenGL. Ci t OpenGL.
Trang 16/20
Thit lp viewport. 1. Thc hin cc bc 1.9. trong D n 1. Tham s ha a gic D n 1 tm ca n thay i theo thi gian. 2. B sung bin thnh vin m_angle vo lp MFCTemplateView.
float m_angle;
3. B nh ngha hng DEG2RAD i t Degree sang Radian, b sung vo tp tin CPP ca lp MFCTemplateView.
#define DEG2RAD (3.14159f/180.0f)
4. B sung bin thnh vin m_angle vo lp MFCTemplateView. 5. B sung hm x l cho thng ip WM_PAINT, Visual Studio sinh ra OnPaint() 6. Son hm OnPaint nh sau:
CPaintDC dc(this); // device context for painting // Add only the following codes. // The previous codes is generated automatically by IDE glColor3f(1.0f, 0.0f, 0.0f); GLfloat cx, cy; cx = 0.5*cos(DEG2RAD*m_angle); cy = 0.5*sin(DEG2RAD*m_angle); glBegin(GL_POLYGON); glVertex2f(cx glVertex2f(cx glVertex2f(cx glVertex2f(cx glEnd(); glFlush();
+ +
cy cy cy cy
+ + -
SwapBuffers(dc.m_ps.hdc); // Swap the front and the back buffer to show the created graphics
9. B sung hm x l s kin WM_TIMER. Visual Studio sinh ra hm OnTimer(param). 10. Son hm OnTimer: Cc dng lnh sau y s thay i tham s m_angle v yu cu v li mn hnh.
CView::OnTimer(nIDEvent); //RePaint the window m_angle += 10.0f; if(m_angle > 360) m_angle = m_angle - 360.0f; InvalidateRect(NULL, false);
Trang 17/20
Gii phng ti nguyn: 11. B sung hm RemoveOpenGL(), kiu void, vo lp MFCTemplateView 12. Son tho hm RemoveOpenGL nh sau:
//Delete Rendering Context if(wglGetCurrentContext() != NULL) wglMakeCurrent(NULL,NULL); if(m_hRC != NULL) { wglDeleteContext(m_hRC); m_hRC = NULL; }
13. B sung hm x l s kin WM_DESTROY, Visual Studio sinh ra OnDestroy(). 14. Son hm OnDestroy nh sau:
CView::OnDestroy(); //Remove openGL RemoveOpenGL(); //Remove timer if(m_hTimer > 0){ KillTimer(m_hTimer); m_hTimer = -1; }
Bin dch v thc thi: 15. Bin dch v thc thi. Bi tp m rng t D n 2: 1) Thay i ni dung hm OnPaint hnh vung xoay quanh mt tm ca n. Hng dn: - Ta 1 nh ca n tnh theo tham s m_angle. - 3 Ta cn li tnh tng i t ta u: theo phng trnh Q = P + v*t. Q l ta cn tnh, P l ta bit trc, v l vector ch phng PQ v l vector n v, v t l chiu di cnh hnh vung.
3. D n 3: V t din tnh
Ni dung cng vic: B sung Header files cho OpenGL. Ci t OpenGL. Thit lp viewport. Khi ng OpenGL. V t din (tetrahydron) vi cc ta c bit trc. Gii phng ti nguyn. Bin dch v thc thi. Cc bc thc hin: S dng li d n MFCTemplate, i.e., copy sang mt folder khc ri thay i m nh sau B sung Header files cho OpenGL.
Trang 18/20
Ci t OpenGL. Thit lp viewport. 1. Thc hin cc bc 1.9. trong D n 1. Khi ng OpenGL: 2. B sung hm thnh vin InitOpenGL() vo lp MFCTemplateView
void InitOpenGL(void);
3. Son InitOpenGL():
//setup projection type //glOrtho: define viewing volume glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho( -1.0, //left //right 1.0, -1.0, //bottom 1.0, //top -2.0, //near //far 2.0 ); //Default MatrixMode is MODELVIEW glMatrixMode(GL_MODELVIEW); //setup background color, or clear color glClearColor(0.1f, 0.7f, 0.7f, 1.0f); //Enable using depth info. to remove hidden surface glEnable(GL_DEPTH_TEST);
4. Gi hm InitOpenGL() t hm OnCreate, to bc 1.
//Initialize OpenGL InitOpenGL();
V t din (tetrahydron) vi cc ta c bit trc. 5. B sung bin thnh vin m_tetra_vertices ca lp MFCTemplateView. - Trong file *.H ca MFCTemplateView:
static const float m_tetra_vertices[][3];
6. B sung hm x l cho thng ip WM_PAINT, Visual Studio sinh ra OnPaint() 7. Son hm OnPaint nh sau:
CPaintDC dc(this); // device context for painting // Add only the following codes. // The previous codes is generated automatically by IDE //clear the frame buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //the order of the vertices in a triangle is important! glBegin(GL_TRIANGLES); //Face 1: defined by vertices: 0, 2, 1; colored: red
Trang 19/20
glColor3f(1.0f, 0.0f, 0.0f); glVertex3fv(m_tetra_vertices[0]); glVertex3fv(m_tetra_vertices[2]); glVertex3fv(m_tetra_vertices[1]); //Face 2: defined by vertices: 0, 1, 3; colored: green glColor3f(0.0f, 1.0f, 0.0f); glVertex3fv(m_tetra_vertices[0]); glVertex3fv(m_tetra_vertices[1]); glVertex3fv(m_tetra_vertices[3]); //Face 3: defined by vertices: 0, 3, 2; colored: blue glColor3f(0.0f, 0.0f, 1.0f); glVertex3fv(m_tetra_vertices[0]); glVertex3fv(m_tetra_vertices[3]); glVertex3fv(m_tetra_vertices[2]); //Face 4: defined by vertices: 3, 1, 2; colored: x glColor3f(0.0f, 0.5f, 0.9f); glVertex3fv(m_tetra_vertices[3]); glVertex3fv(m_tetra_vertices[1]); glVertex3fv(m_tetra_vertices[2]); glEnd(); glFlush(); SwapBuffers(dc.m_ps.hdc); // Swap the front and the back buffer to show the created graphics
Gii phng ti nguyn: 8. Thc hin cc bc 11. 14. t D n 1: Bin dch v thc thi: 9. Bin dch v thc thi. Bi tp m rng t D n 3: 1) bc 3., thay i gi tr near ca hm glOrtho t -2, -1.5, -1, -0.5, 0, 0.5, 1.0 Bin dch v chy li nhn thy s khc nhau. 2) Thay i mu ca cc mt trong t din, mu bt k. 3) Th xa dng glEnable(GL_DEPTH_TEST) InitOpenGL. Sau , bin dch v chy li. 4) Th xa tr GL_DEPTH_BUFFER_BIT OnPaint. Sau , bin dch v chy li.