vat_the_3d_va_cac_phep_bien_doi

7
Bài hướng dn thc hành - Thư vin OpenGL Lab 2 : Vcác đối tượng 3D và các phép biến đổi GV HDTH : Dương Nguyn Thch Tho - [email protected] 1. Ví dmu : Bước 1 : Cài đặt thư vin OpenGL glut vào máy theo hướng dn Lab1 Bước 2 : To project dng console application trong môi trường VC++ 6.0 Bước 3 : đon code sau vào file .cpp #include <GL/glut.h> #include <stdlib.h> GLfloat rtri; // Angle For The Triangle ( NEW ) GLfloat rquad; // Angle For The Quad ( NEW ) void init(void) { glShadeModel(GL_SMOOTH); // Enable Smooth Shading (7) glClearColor(0.0f, 0.0f, 0.0f, 0.5f) // Black Background (8) glClearDepth(1.0f); // Depth Buffer Setup glEnable(GL_DEPTH_TEST) // Enables Depth Testing glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations } void displayPyramid(void) { glLoadIdentity(); // Reset The Current Modelview Matrix (1) glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0 (2) glRotatef(rtri,0.0f,1.0f,0.0f); // Rotate The Triangle On The Y axis ( NEW ) (3) glBegin(GL_TRIANGLES); // Start Drawing A Triangle (4) glColor3f(1.0f,0.0f,0.0f); // Red (5) glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Front) (6) glColor3f(0.0f,1.0f,0.0f); // Green glVertex3f(-1.0f,-1.0f, 1.0f); // Left Of Triangle (Front) glColor3f(0.0f,0.0f,1.0f); // Blue

Transcript of vat_the_3d_va_cac_phep_bien_doi

Bài hướng dẫn thực hành - Thư viện OpenGL

Lab 2 : Vẽ các đối tượng 3D và các phép biến đổi GV HDTH : Dương Nguyễn Thạch Thảo - [email protected]

1. Ví dụ mẫu :

Bước 1 : Cài đặt thư viện OpenGL glut vào máy theo hướng dẫn Lab1

Bước 2 : Tạo project dạng console application trong môi trường VC++ 6.0

Bước 3 : Gõ đoạn code sau vào file .cpp

#include <GL/glut.h> #include <stdlib.h> GLfloat rtri; // Angle For The Triangle ( NEW ) GLfloat rquad; // Angle For The Quad ( NEW ) void init(void) { glShadeModel(GL_SMOOTH); // Enable Smooth Shading (7) glClearColor(0.0f, 0.0f, 0.0f, 0.5f) // Black Background (8) glClearDepth(1.0f); // Depth Buffer Setup glEnable(GL_DEPTH_TEST) // Enables Depth Testing glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations } void displayPyramid(void) { glLoadIdentity(); // Reset The Current Modelview Matrix (1) glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0 (2) glRotatef(rtri,0.0f,1.0f,0.0f); // Rotate The Triangle On The Y axis ( NEW ) (3) glBegin(GL_TRIANGLES); // Start Drawing A Triangle (4) glColor3f(1.0f,0.0f,0.0f); // Red (5) glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Front) (6) glColor3f(0.0f,1.0f,0.0f); // Green glVertex3f(-1.0f,-1.0f, 1.0f); // Left Of Triangle (Front) glColor3f(0.0f,0.0f,1.0f); // Blue

glVertex3f( 1.0f,-1.0f, 1.0f); // Right Of Triangle (Front) glColor3f(1.0f,0.0f,0.0f); // Red glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Right) glColor3f(0.0f,0.0f,1.0f); // Blue glVertex3f( 1.0f,-1.0f, 1.0f); // Left Of Triangle (Right) glColor3f(0.0f,1.0f,0.0f); // Green glVertex3f( 1.0f,-1.0f, -1.0f); // Right Of Triangle (Right) glColor3f(1.0f,0.0f,0.0f); // Red glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Back) glColor3f(0.0f,1.0f,0.0f); // Green glVertex3f( 1.0f,-1.0f, -1.0f); // Left Of Triangle (Back) glColor3f(0.0f,0.0f,1.0f); // Blue glVertex3f(-1.0f,-1.0f, -1.0f); // Right Of Triangle (Back) glColor3f(1.0f,0.0f,0.0f); // Red glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Left) glColor3f(0.0f,0.0f,1.0f); // Blue glVertex3f(-1.0f,-1.0f,-1.0f); // Left Of Triangle (Left) glColor3f(0.0f,1.0f,0.0f); // Green glVertex3f(-1.0f,-1.0f, 1.0f); // Right Of Triangle (Left) glEnd(); (4) // Done Drawing The Pyramid } void displayCube() { glLoadIdentity(); // Reset The Current Modelview Matrix glTranslatef(1.5f,0.0f,-7.0f); // Move Right 1.5 Units And Into The Screen 7.0 glRotatef(rquad,1.0f,1.0f,1.0f); // Rotate The Quad On The X axis ( NEW ) glBegin(GL_QUADS); // Draw A Quad glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top) glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top) glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top) glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top) glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom) glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom) glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom) glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom) glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front) glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front) glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front) glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)

glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right Of The Quad (Back) glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left Of The Quad (Back) glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom Left Of The Quad (Back) glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right Of The Quad (Back) glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left) glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left) glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left) glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left) glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right) glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right) glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right) glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right) glEnd(); // Done Drawing The Quad } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer glPushMatrix(); //(9) displayPyramid(); displayCube(); glPopMatrix();//(10) glutSwapBuffers();//(11) } void reshape (int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); } void keyboard (unsigned char key, int x, int y)

{ switch (key) { case 'p': rtri +=0.2f; glutPostRedisplay(); //(12) break; case 'c': rquad-=0.15f; glutPostRedisplay(); break; case 27: //ESC exit(0); break; default: break; } } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); //(13) glutReshapeFunc(reshape); //(14) glutKeyboardFunc(keyboard);//(15) glutMainLoop(); return 0; }

Bước 4 : Build project sẽ có được hình như sau

- Khi người dùng nhấn phím ‘p’ : hình chóp xoay quanh trục của nó - Khi người dùng nhấn phím ‘p’ : khối lập phương xoay quanh đường chéo của nó.

2. Giải thích source code :

void displayPyramid(void) (1) : glLoadIdentity() : Hàm reset lại trạng thái vẽ về vị trí khởi đầu (2) : glTranslatef(-1.5f,0.0f,-6.0f) : Dịch chuyển từ vị trí hiện hành ( trong trường hợp

này là Gốc tọa độ ) theo vecto (-1.5f,0.0f,-6.0f); (3) : glRotatef(rquad,1.0f,1.0f,1.0f) : Chọn phép xoay quanh vector tạo bởi gốc tọa

độ và điểm (1.0f,1.0f,1.0f) với góc quay rquad. (4) : glBegin(GL_TRIANGLES); glEnd() : Các mô hình được vẽ trong cặp lệnh

glBegin(), glEnd(). GL_TRIANGLES là chế độ vẽ theo các hình tam giác được tạo bởi các đỉnh được liệt kê trong cặp glBegin() , glEnd(). (Tham khảo thêm các chế độ có thể dùng cho glBegin trong sách the red book, trang 37, Fig 2.6)

(5) glColor3f(1.0f,0.0f,0.0f); Chọn màu đỏ để vẽ (6) glVertex3f( 0.0f, 1.0f, 0.0f); // Vẽ đỉnh đầu tiên

Như vậy mỗi đỉnh của hình chóp được vẽ với các màu khác nhau nên ta có sự biến đổi và hòa trộn màu trên mỗi bề mặt hình chóp. void displayCube() : Tương tự hàm displayPyramid(). Mỗi mặt của khối lập phương được vẽ một màu. void init(void)

(7) glShadeModel(GL_SMOOTH); Chọn chế độ màu smooth. Chính nhờ chế độ này mà ta có hình chóp như vậy. glShadeModel có 2 chế độ GL_SMOOTH và GL_FLAT. ( Thử thay giá trị GL_GLAT xem kết quả thế nào?)

(8) glClearColor(0.0f, 0.0f, 0.0f, 0.5f) : Chọn màu đen làm màu nền void display(void)

(9) glPushMatrix(); Lấy Trạng thái hiện hành và tiếp tục thực hiện thao tác vẽ (10) glPopMatrix(); Lưu trạng thái hiện hành lại (11) glutSwapBuffers(); Chuyển tất cả những thong tin vẽ lên buffer ra màn

hình void keyboard (unsigned char key, int x, int y)

(12) glutPostRedisplay(); Vẽ lại ( gọi lại hàm display )

int main(int argc, char** argv) (13) glutDisplayFunc(display); Chọn hàm display cho hàm display của thư viện

glut (14) glutReshapeFunc(reshape); // chọn hàm reshape cho hàm reshape của

glut (15) glutKeyboardFunc(keyboard);// chọn hàm keyboard cho hàm keyboard

của glut

Owner
Text Box
c
Owner
Text Box

3. C

Phép dị

glTransl

glTransl

Dịch chu

Phép xo

glRotatefglRotated

Phép xoaangle Phép co glScalef(

Các phép

ịch chuyển

atef(GLfloa

ated(GLdo

uyển gốc tọ

oay

f(GLfloat and(GLdouble

ay có trục tạ

giãn ( GLfloat x,

biển đổi

n :

at x, GLfloa

uble x, GLd

ọa độ để vẽ

ngle, GLfloae angle, GLd

ạo bởi vecto

GLfloat y, G

3D

t y, GLfloat

double y, G

ẽ đối tượng

at x, GLfloatdouble x, G

or e tọa bởi

GLfloat z);

t z);

GLdouble z)

đến vị trí (x

t y, GLfloat zLdouble y,

gốc tọa độ

;

x,y,z)

z); GLdouble z

và điểm có

z);

tọa độ (x,y,,z), với góc quay

glScaled( GLdouble x, GLdouble y, GLdouble z);

4. Bài tập rèn luyện 1) Nâng cấp chương trình lên thành hình chóp và khối lập phương tự xoay theo thời gian

mà ko có tín hiệu từ bàn phím. 2) Vẽ thêm hình cầu ( tham khảo hàm glSolidSphere ) 3) Thay đổi cho phép hình chóp và hình cầu quay quanh một cạnh nào đó của nó.