Bài thực hành số 3: Lập trình với DirectX

21 42 0
Bài thực hành số 3: Lập trình với DirectX

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Bài thực hành số 3: Lập trình với DirectX Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt Trong thực hành số làm quen với bước lập trình với DirectX: khởi tạo thiết bị, vẽ hình, load liệu từ file Chúng ta tiếp tục tìm hiểu thêm chức DirectX hỗ trợ 1.1 Tạo ứng dụng _1: Khởi động Visual Studio C# 2005 Express _2: Tạo dự án với kiểu dự án Window Application ngơn ngữ lập trình C# z Trong menu File chọn New Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt z Trong khung Project type chọn Visual C# z Trong khung bên phải chọn Windows Application z Nhập tên cho ứng dụng: ThucHanhDirectX02 nhấn OK Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt _3: Lúc hình xuất cửa sổ trống: - Chuyển sang cửa sổ mã nguồn: tìm file Program.cs khung bên phải Nháy đúp nhấn chuột phải chọn View code Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt - Khung soạn thảo mã nguồn hiển thị phía bên trái _4 Để sử dụng tiện ích thư viện DirectX phai thiết lập tham chiếu đến thư viện - Chọn mục References khung bên phải Nhấn chuột phải chọn Add references Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt - Trong hộp thoại Add reference chọn hai thư viện DirectX DirectX3D - Trong khung soạn thảo mã nguồn bổ sung hai câu lệnh để sử dụng hai thư viện vừa tham chiếu: using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; _5 Lúc biên dịch chương trình thị form trống Để thay đổi tiêu đề kích thước form tìm phương thức InitializeComponent() thay đổi lại kích thước form (500,500), tiêu đề “Thuc hanh DirectX02” - Mở mã nguồn form: Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt - Thay đổi phương thức InitializeComponent(): private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(500, 500); this.Text = "Thuc hanh DirectX02"; this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); } - Khai báo đối tượng Device đầu form private Device device; _6 Xây dựng phương thức khởi tạo thiết bị thiết lập vị trí Camera //phương thức khởi tạo đối tượng device public void InitializeDevice() { PresentParameters presentParams = new PresentParameters(); presentParams.Windowed = true; presentParams.SwapEffect = SwapEffect.Discard; presentParams.AutoDepthStencilFormat = DepthFormat.D16; presentParams.EnableAutoDepthStencil = true; device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt device.RenderState.Lighting = false; } //phương thức thiết lập vị trí Camera public void SetUpCamera() { device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 0.3f, 500f); device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 30), new Vector3(0, 0, 0), new Vector3(0, 1, 0)); } - Bổ sung phương thức OnPaint() vào lớp Form: protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkSlateBlue, 1.0f, 0); device.BeginScene(); device.EndScene(); device.Present(); this.Invalidate(); } - Thay đổi phương thức main() Program.cs sau: static void Main() { using (Form1 our_directx_form = new Form1()) { our_directx_form.InitializeDevice(); our_directx_form.SetUpCamera(); Application.Run(our_directx_form); } } - Thực thi chương trình thu sau: Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt Tạo bề mặt _1 Trong hai thực hành giới thiệu cách xây dựng đối tượng thơng qua việc ghép hình tam giác tô màu cho chúng DirectX hỗ trợ phương thức hiệu để tạo nên khung cảnh cách tải file ảnh đặt chúng vào hình tam giác _2 Trong phần thực vẽ tam giác đơn giản gắn bề mặt cho - Tạo tham chiếu đến thư viện: Microsoft.DirectX.Direct3DX - Để vẽ tam giác cần khai báo vertices đặt mảng Bổ sung khai báo sau vào đầu form: private CustomVertex.PositionTextured[] vertices; - Tiếp theo xây dựng phương thức VertexDeclarartion() để khởi tạo cho vertex này: public void VertexDeclaration() { vertices = new CustomVertex.PositionTextured[3]; vertices[0].Position = new Vector3(10f, 10f, 0f); vertices[0].Tu = 0; vertices[0].Tv = 0; vertices[1].Position = new Vector3(-10f, -10f, 0f); vertices[1].Tu = 1; vertices[1].Tv = 1; } vertices[2].Position = new Vector3(10f, -10f, 0f); vertices[2].Tu = 0; vertices[2].Tv = 1; - Bổ sung lời gọi phương thức VertexDeclarartion() vào hàm main(): our_directx_form.VertexDeclaration(); _3 Như đến lúc có liệu Vertex, để hiển thị điểm cần thay đổi hàm OnPaint(): - Bổ sung câu lệnh sau vào hai lệnh BeginScene() EndScene(): Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt device.VertexFormat = CustomVertex.PositionTextured.Format; device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices); - Thực thi chương trình ta hình tam giác màu trắng: _4 Bổ sung bề mặt cho tam giác: - Trước hết định nghĩa biến: biến chứa giá trị bề mặt, biến chứa thông tin vật liệu, bổ sung khai báo sau vào đầu lớp: private Texture texture; private Material material; - Xây dựng phương thức LoadTexturesAndMaterials() để thiết lập bề mặt cho thiết bị: public void LoadTexturesAndMaterials() { material = new Material(); material.Diffuse = Color.White; material.Specular = Color.LightGray; material.SpecularSharpness = 15.0F; Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt device.Material = material; texture = TextureLoader.FromFile(device, "testtexture.bmp"); } • Trong đoạn lệnh trên, trước hết khởi tạo đối tượng material thiết lập thuộc tính cho đối tượng - Gắn bề mặt cho đối tượng vẽ device cách bổ sung vào hàm OnPaint() trước vẽ tam giác: device.SetTexture(0,texture); - Bề mặt vật liệu load từ file ảnh bimap gắn cho đối tượng device File ảnh bề mặt phải đặt thư mục với file thực thi chương trình - Bổ sung vào hàm main() lời gọi: our_directx_form.LoadTexturesAndMaterials(); - Thực thi chương trình thu được: Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt Tạo sơ đồ phân tầng _1 Trong phần trước load bề mặt lên tam giác, phần tạo số lượng hình ảnh nhiều - Để tạo chữ nhật gồm ảnh ghép (3*3) lại ảnh trung tâm rỗng cần ảnh, tức 16 tam giác Thêm khai báo sau vào đầu lớp: private int[,] int_Floorplan; private int WIDTH; private int HEIGHT; private int differentbuildings = 5; private CustomVertex.PositionNormalTextured[] verticesarray; ArrayList verticeslist = new ArrayList(); - Xây dựng phương thức vẽ nền: private void LoadFloorplan() { WIDTH = 3; HEIGHT = 3; int_Floorplan = new int[,] { {0,0,0}, {0,1,0}, {0,0,0}, }; } - Trong giá trị nghĩa load bề mặt vào vị trí giá tri để trống Thêm lời gọi đến phương thức hàm main(): our_directx_form.LoadFloorplan(); - Tiếp theo cập nhật cho phương thức VertexDeclaration() để load bề mặt từ file ảnh tự động tạo vertices tương ứng, file ảnh bao gồm nhiều loại bề mặt khác nhau, phần bên trái sử dụng để tạo nền, phần để tạo tường cho tòa nhà khác Thay đổi tên file ảnh phương thức: LoadTexturesAndMaterials() - Trước hết định nghĩa số lượng bề mặt đọc từ file ảnh load Thêm khai báo vào phương thức VertexDeclaration(): Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt float imagesintexture = + differentbuildings * 2; - Tiếp theo phương thức đọc liệu mảng int_Floorplan tạo bề mặt gặp giá trị 0: for (int x = 0; x < WIDTH; x++) { for (int y = 0; y < HEIGHT; y++) { if (int_Floorplan[x, y] == 0) { verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0), new Vector3(0, 0, 1), 1f / imagesintexture, 1)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y, 0), new Vector3(0, 0, 1), 0, 1)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y + 1, 0), new Vector3(0, 0, 1), 0, 0)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y + 1, 0), new Vector3(0, 0, 1), 0, 0)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y + 1, 0), new Vector3(0, 0, 1), 1f / imagesintexture, 0)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0), new Vector3(0, 0, 1), 1f / imagesintexture, 1)); } } } verticesarray = (CustomVertex.PositionNormalTextured[])verticeslist.ToArray (typeof(CustomVertex.PositionNormalTextured)); - Kết thúc phương thức VertexDeclaration() load liệu từ file vào đối tượng texture: scenerytexture = TextureLoader.FromFile(device, "texturemap.jpg"); - Cập nhật hàm OnPaint(): device.VertexFormat = CustomVertex.PositionNormalTextured.Format; device.SetTexture(0, scenerytexture); Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt device.DrawUserPrimitives(PrimitiveType.TriangleList, verticeslist.Count / 3, verticesarray); - Điều chỉnh lại vị trí camera: device.Transform.View = Matrix.LookAtLH(new Vector3(3, -2, 5), new Vector3(2, 1, 0), new Vector3(0, 0, 1)); - Thực thi chương trình được: Khoa CNTT – ĐHBK Hà nơi hunglt@it-hut.edu.vn Y: hung_lt Vẽ tịa nhà _1 Trong file bề mặt hỗ trợ loại nhà khác cần khai báo mảng chứa chiều cao tòa nhà đầu lớp: private int[] buildingheights = new int[] {0,10,1,3,2,5}; - Tiếp theo xử lý liệu mảng int_Floorplan, phần trước dùng hai giá trị để thể bỏ trống, phần giá trị giữ nguyên giá trị thay giá trị ngẫu nhiên từ đến thể loại nhà khác - Thêm đoạn lệnh sau vào cuối phương thức LoadFloorPlan(): Random random = new Random(); for (int x=0; x< WIDTH; x++){ for (int y=0; y< HEIGHT; y++) { if (int_Floorplan[x,y] == 1) { int_Floorplan[x, y] = random.Next(differentbuildings) + 1; } } } _2 Thay đổi phương thức VertexDeclaration() để vẽ tòa nhà load bề mặt tự động: public void VertexDeclaration() { float imagesintexture = + differentbuildings * 2; for (int x = 0; x < WIDTH; x++) { for (int y = 0; y < HEIGHT; y++) { int currentbuilding = int_Floorplan[x, y]; verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, buildingheights[currentbuilding]), new Vector3(0, 0, 1), (currentbuilding*2+1)/imagesintexture, 1)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y, buildingheights[currentbuilding]), new Vector3(0, 0, 1), currentbuilding * / imagesintexture, 1)); verticeslist.Add(new Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt CustomVertex.PositionNormalTextured(new Vector3(x + 1, y + 1, buildingheights[currentbuilding]), new Vector3(0, 0, 1), currentbuilding * / imagesintexture, 0)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y + 1, buildingheights[currentbuilding]), new Vector3(0, 0, 1), currentbuilding * / imagesintexture, 0)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y + 1, buildingheights[currentbuilding]), new Vector3(0, 0, 1), (currentbuilding * + 1) / imagesintexture, 0)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, buildingheights[currentbuilding]), new Vector3(0, 0, 1), (currentbuilding * + 1) / imagesintexture, 1)); if (y > 0) { if (int_Floorplan[x, y - 1] != int_Floorplan[x, y]) { if (int_Floorplan[x, y-1] > 0) { currentbuilding = int_Floorplan[x, y-1]; verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0f), new Vector3(0, 1, 0), (currentbuilding * - 1) / imagesintexture, 1)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y, buildingheights[currentbuilding]), new Vector3(0, 1, 0), currentbuilding * / imagesintexture, 0)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y, 0f), new Vector3(0, 1, 0), currentbuilding * / imagesintexture, 1)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y, buildingheights[currentbuilding]), new Vector3(0, 1, 0), currentbuilding * / imagesintexture, 0)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0f), new Vector3(0, 1, 0), (currentbuilding * - 1) / imagesintexture, 1)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, buildingheights[currentbuilding]), new Vector3(0, 1, 0), Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt (currentbuilding * - 1) / imagesintexture, 0)); } if (int_Floorplan[x,y] > 0) { currentbuilding = int_Floorplan[x, y]; verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0f), new Vector3(0, -1, 0), currentbuilding * / imagesintexture, 1)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y, 0f), new Vector3(0, -1, 0), (currentbuilding * - 1) / imagesintexture, 1)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y, buildingheights[currentbuilding]), new Vector3(0, -1, 0), (currentbuilding * - 1) / imagesintexture, 0)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0f), new Vector3(0, -1, 0), currentbuilding * / imagesintexture, 1)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y, buildingheights[currentbuilding]), new Vector3(0, -1, 0), (currentbuilding * - 1) / imagesintexture, 0)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, buildingheights[currentbuilding]), new Vector3(0, -1, 0), currentbuilding * / imagesintexture, 0)); } } } if (x > 0) { if (int_Floorplan[x - 1, y] != int_Floorplan[x, y]) { if (int_Floorplan[x-1, y] > 0) { currentbuilding = int_Floorplan[x1, y]; verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0f), new Vector3(1, 0, 0), currentbuilding * / imagesintexture, 1)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y + 1, 0f), new Vector3(1, 0, 0), (currentbuilding * - 1) / Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt imagesintexture, 1)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y + 1, buildingheights[currentbuilding]), new Vector3(1, 0, 0), (currentbuilding * - 1) / imagesintexture, 0)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y + 1, buildingheights[currentbuilding]), new Vector3(1, 0, 0), (currentbuilding * - 1) / imagesintexture, 0)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, buildingheights[currentbuilding]), new Vector3(1, 0, 0), currentbuilding * / imagesintexture, 0)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0f), new Vector3(1, 0, 0), currentbuilding * / imagesintexture, 1)); } if (int_Floorplan[x,y] > 0) { currentbuilding = int_Floorplan[x, y]; verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0f), new Vector3(-1, 0, 0), (currentbuilding * - 1) / imagesintexture, 1)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, buildingheights[currentbuilding]), new Vector3(-1, 0, 0), (currentbuilding * - 1) / imagesintexture, 0)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y + 1, 0f), new Vector3(-1, 0, 0), currentbuilding * / imagesintexture, 1)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, buildingheights[currentbuilding]), new Vector3(-1, (currentbuilding * - 1) / imagesintexture, 0)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, buildingheights[currentbuilding]), new Vector3(-1, currentbuilding * / imagesintexture, 0)); verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, 0f), new Vector3(-1, 0, 0), currentbuilding * / imagesintexture, 1)); } } Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt y, 0, 0), y + 1, 0, 0), y + 1, } } } verticesarray = (CustomVertex.PositionNormalTextured[])verticeslist.ToArray (typeof(CustomVertex.PositionNormalTextured)); } - Thay đổi kích thước nội dung mảng int_Floorplan: WIDTH = 20; HEIGHT = 15; int_Floorplan = new int[,] { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,1,1,0,0,0,1,1,0,0,1,0,1}, {1,0,0,1,1,0,0,0,1,0,0,0,1,0,1}, {1,0,0,0,1,1,0,1,1,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,1,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,1,1,0,0,0,1,0,0,0,0,0,0,1}, {1,0,1,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,1,0,0,0,1,0,0,0,0,1}, {1,0,1,0,0,0,0,0,0,1,0,0,0,0,1}, {1,0,1,1,0,0,0,0,1,1,0,0,0,1,1}, {1,0,0,0,0,0,0,0,1,1,0,0,0,1,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, }; - Điều chỉnh lại vị trí Camera: device.Transform.View = Matrix.LookAtLH(new Vector3(20, 5, 13), new Vector3(8, 7, 0), new Vector3(0, 0, 1)); - Thực thi chương trình: Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt Khoa CNTT – ĐHBK Hà nôi hunglt@it-hut.edu.vn Y: hung_lt ... Lúc biên dịch chương trình th? ?? form trống Để thay đổi tiêu đề kích th? ?ớc form tìm phương th? ??c InitializeComponent() thay đổi lại kích th? ?ớc form (500,500), tiêu đề “Thuc hanh DirectX02” - Mở mã... hunglt@it-hut.edu.vn Y: hung_lt - Khung soạn th? ??o mã nguồn hiển th? ?? phía bên trái _4 Để sử dụng tiện ích th? ? viện DirectX phai thiết lập tham chiếu đến th? ? viện - Chọn mục References khung bên phải... device.RenderState.Lighting = false; } //phương th? ??c thiết lập vị trí Camera public void SetUpCamera() { device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 0.3f, 500f);

Ngày đăng: 08/09/2020, 18:22

Hình ảnh liên quan

_3: Lúc này trên màn hình xuất hiện một cửa sổ trống: - Bài thực hành số 3: Lập trình với DirectX

3.

Lúc này trên màn hình xuất hiện một cửa sổ trống: Xem tại trang 4 của tài liệu.
- Th ực thi chương trình ta được một hình tam giác màu trắng: - Bài thực hành số 3: Lập trình với DirectX

h.

ực thi chương trình ta được một hình tam giác màu trắng: Xem tại trang 11 của tài liệu.

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan