Stage3d learning notes (2): Draw a triangle using GPU

Source: Internet
Author: User

We need to use the agalminicycler code class provided by Adobe, which can be downloaded from the Internet;

For more information about agal, see the following article:

Agal introduction series (Part 1)
Agal introduction series (Part 2)
Agal introduction series (part 3)

The final result is as follows:

Directly go to the code. Please refer to the related notes:

1 package 2 {3 Import COM. adobe. utils. agalminiassembler; 4 5 import flash. display. sprite; 6 Import flash. display. stage3d; 7 Import flash. display3d. context3d; 8 Import flash. display3d. context3dprofile; 9 Import flash. display3d. context3drendermode; 10 Import flash. display3d. context3dvertexbufferformat; 11 import flash. display3d. indexbuffer3d; 12 Import flash. display3d. program3d; 13 Import flash. display3d. vertexbuffer3d; 14 Import flash. events. errorevent; 15 Import flash. events. event; 16 17 [SWF (width = 800, Height = 600, framerate = 60)] 18 public class drawtriangle extends sprite 19 {20 // 3D scene object 21 private VaR _ stage3d: stage3d; 22 // 3D context rendering object 23 private VaR _ context3d: context3d; 24 25 // vertex buffer data 26 private VaR _ vertexbuffer: vertexbuffer3d; 27 // index buffer data 28 private VaR _ indexbuffer: indexbuffer3d; 29 30 // The coloring object 31 private VaR _ program3d: program3d; 32 33 public function drawtriangle () 34 {35 addeventlistener (event. added_to_stage, addedtostagehandler); 36} 37 38 private function addedtostagehandler (Event: Event): void 39 {40 removeeventlistener (event. added_to_stage, addedtostagehandler); 41 42 // 3D scene exists, usually four 3D scene objects 43 If (stage. stage3ds. length> 0) 44 {45 // use the lowest 3D scene 46 _ stage3d = stage. stage3ds [0]; 47 // request 3D context rendering object 48 _ stage3d. addeventlistener (errorevent. error, stage3derrorhandler); 49 _ stage3d. addeventlistener (event. context3d_create, context3dcreatehandler); 50 _ stage3d. requestcontext3d (context3drendermode. auto, context3dprofile. baseline); 51} 52} 53 54 private function stage3derrorhandler (Event: errorevent): void 55 {56 trace ("context3d object request failed:", event. text); 57} 58 59 private function context3dcreatehandler (Event: Event): void 60 {61 initcontext3d (); 62 initbuffer (); 63 initprogram (); 64 65 // rendering each frame 66 addeventlistener (event. enter_frame, render); 67} 68 69 private function initcontext3d (): void 70 {71 // get 3D rendering object 72 _ context3d = _ stage3d. context3d; 73 // adjust the 3D stage position 74 _ stage3d. X = 50; 75 _ stage3d. y = 50; 76 // set the backend buffer 77 _ context3d. configurebackbuffer (700,500, 2); 78} 79 80 private function initbuffer (): void 81 {82 // vertex data 83 var vertexdata: vector. <number> = vector. <number> (84 [85 // X, Y, Z, R, G, B 86 0, 0, 0, 1, 0, 0, 87 1, 1, 0, 0, 1, 0, 88 1, 0, 0, 0, 0, 1 89]); 90 91 // create a vertex buffer object, the parameter sets the number of groups of data and the number of data in each group. 92 _ vertexbuffer = _ context3d. createvertexbuffer (vertexdata. length/6, 6); 93 // upload vertex data to the GPU. The parameter sets the number of sets of data to be uploaded and the number of sets of data to be uploaded. 94 _ vertexbuffer. uploadfromvector (vertexdata, 0, vertexdata. length/6); 95 96 // index data 97 var indexdata: vector. <uint> = vector. <uint> (98 [99 0, 1, 2100]); 101 102 // create an index buffer object. Each index corresponds to a set of data corresponding to the vertex data, 103 // every three indexes form a triangle that will be drawn. The parameter specifies the index length of 104 _ indexbuffer = _ context3d. createindexbuffer (indexdata. length); 105 // upload the index data to the GPU. The parameter sets the number of data records uploaded and uploaded starting from 106 _ indexbuffer. uploadfromvector (indexdata, 0, 3); 107} 108 109 private function initprogram (): void110 {111 // Vertex coloring code, run the code 112 var vertexarr: array = 113 [114 // op indicates the position output register before each uploaded vertex, no matter how many operations are performed on the vertex, the final result 115 must be assigned to him. Here we do not run the operation. The value is 116 "mov op, va0 ", 117 // the data required by the fragment shader should be transferred here through v0, because the fragment shader does not 118 // can directly read data of va0 and va1 119 "mov v0, va1 "120]; 121 122 // fragment shader code. Each pixel that can be displayed will execute this Code 123 var fragmentarr: array = 124 [125 // OC indicates the color output register. Each vertex's color data must be assigned a value of 126 "mov oC, V0" 127]; 128 129 // use the compiler provided by Adobe to compile the code as binary data that can be used by the program. 130 var aggreger: agalminiassembler = new agalminiassembler (); 131 _ program3d = aggreger. assemble2 (_ context3d, 1, vertexarr. join ("\ n"), fragmentarr. join ("\ n"); 132} 133 134 private function render (Event: Event): void135 {136 // clear the drawn 3D image 137 _ context3d. clear (0, 0, 0); 138 // specifies the data segment represented by va0 of the Shadow code. Based on a set of vertex data, we can see 139 _ context3d. setvertexbufferat (0, _ vertexbuffer, 0, context3dvertexbufferformat. float_3); 140 // specifies the data segment represented by va1 of the Shadow code. Based on a set of vertex data, we can see 141 _ context3d. setvertexbufferat (1, _ vertexbuffer, 3, context3dvertexbufferformat. float_3); 142 // specify the currently used color object 143 _ context3d. setprogram (_ program3d); 144 // draw all triangles 145 _ context3d using Vertex index data. drawtriangles (_ indexbuffer); 146 // display the background buffered image to screen 147 _ context3d. present (); 148} 149} 150}

Stage3d learning notes (2): Draw a triangle using GPU

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.