当前位置:   article > 正文

鸿蒙原生应用元服务开发-WebGL网页图形库开发着色器绘制彩色三角形

鸿蒙原生应用元服务开发-WebGL网页图形库开发着色器绘制彩色三角形

着色器绘制彩色三角形
使用WebGL开发时,为保证界面图形显示效果,请使用真机运行。
此场景为使用WebGL绘制的彩色三角形图形(GPU绘制)。开发示例如下:
创建页面布局。index.hml示例如下:

  1. <div class="container">
  2. <canvas ref="canvas1" style="width : 400px; height : 200px; background-color : lightyellow;"></canvas>
  3. <button class="btn-button" onclick="BtnColorTriangle">BtnColorTriangle</button>
  4. </div>

1.设置页面样式。index.css示例如下:

  1. .container {
  2. flex-direction: column;
  3. justify-content: center;
  4. align-items: center;
  5. }
  6. .btn-button {
  7. margin: 1px;
  8. height: 40px;
  9. width: 220px;
  10. background-color: lightblue;
  11. font-size: 20px;
  12. text-color: blue;
  13. }

2.编辑JavaScript代码文件,增加彩色三角形绘制逻辑代码。index.js示例如下:

  1. // index.js
  2. // WebGL相关预定义
  3. var gl = {
  4. DEPTH_BUFFER_BIT: 0x00000100,
  5. STENCIL_BUFFER_BIT: 0x00000400,
  6. COLOR_BUFFER_BIT: 0x00004000,
  7. POINTS: 0x0000,
  8. LINES: 0x0001,
  9. LINE_LOOP: 0x0002,
  10. LINE_STRIP: 0x0003,
  11. TRIANGLES: 0x0004,
  12. TRIANGLE_STRIP: 0x0005,
  13. TRIANGLE_FAN: 0x0006,
  14. ZERO: 0,
  15. ONE: 1,
  16. SRC_COLOR: 0x0300,
  17. ONE_MINUS_SRC_COLOR: 0x0301,
  18. SRC_ALPHA: 0x0302,
  19. ONE_MINUS_SRC_ALPHA: 0x0303,
  20. DST_ALPHA: 0x0304,
  21. ONE_MINUS_DST_ALPHA: 0x0305,
  22. DST_COLOR: 0x0306,
  23. ONE_MINUS_DST_COLOR: 0x0307,
  24. SRC_ALPHA_SATURATE: 0x0308,
  25. FUNC_ADD: 0x8006,
  26. BLEND_EQUATION: 0x8009,
  27. BLEND_EQUATION_RGB: 0x8009,
  28. BLEND_EQUATION_ALPHA: 0x883D,
  29. FUNC_SUBTRACT: 0x800A,
  30. FUNC_REVERSE_SUBTRACT: 0x800B,
  31. BLEND_DST_RGB: 0x80C8,
  32. BLEND_SRC_RGB: 0x80C9,
  33. BLEND_DST_ALPHA: 0x80CA,
  34. BLEND_SRC_ALPHA: 0x80CB,
  35. CONSTANT_COLOR: 0x8001,
  36. ONE_MINUS_CONSTANT_COLOR: 0x8002,
  37. CONSTANT_ALPHA: 0x8003,
  38. ONE_MINUS_CONSTANT_ALPHA: 0x8004,
  39. BLEND_COLOR: 0x8005,
  40. ARRAY_BUFFER: 0x8892,
  41. ELEMENT_ARRAY_BUFFER: 0x8893,
  42. ARRAY_BUFFER_BINDING: 0x8894,
  43. ELEMENT_ARRAY_BUFFER_BINDING: 0x8895,
  44. STREAM_DRAW: 0x88E0,
  45. STATIC_DRAW: 0x88E4,
  46. DYNAMIC_DRAW: 0x88E8,
  47. BUFFER_SIZE: 0x8764,
  48. BUFFER_USAGE: 0x8765,
  49. CURRENT_VERTEX_ATTRIB: 0x8626,
  50. FRONT: 0x0404,
  51. BACK: 0x0405,
  52. FRONT_AND_BACK: 0x0408,
  53. CULL_FACE: 0x0B44,
  54. BLEND: 0x0BE2,
  55. DITHER: 0x0BD0,
  56. STENCIL_TEST: 0x0B90,
  57. DEPTH_TEST: 0x0B71,
  58. SCISSOR_TEST: 0x0C11,
  59. POLYGON_OFFSET_FILL: 0x8037,
  60. SAMPLE_ALPHA_TO_COVERAGE: 0x809E,
  61. SAMPLE_COVERAGE: 0x80A0,
  62. NO_ERROR: 0,
  63. INVALID_ENUM: 0x0500,
  64. INVALID_VALUE: 0x0501,
  65. INVALID_OPERATION: 0x0502,
  66. OUT_OF_MEMORY: 0x0505,
  67. CW: 0x0900,
  68. CCW: 0x0901,
  69. LINE_WIDTH: 0x0B21,
  70. ALIASED_POINT_SIZE_RANGE: 0x846D,
  71. ALIASED_LINE_WIDTH_RANGE: 0x846E,
  72. CULL_FACE_MODE: 0x0B45,
  73. FRONT_FACE: 0x0B46,
  74. DEPTH_RANGE: 0x0B70,
  75. DEPTH_WRITEMASK: 0x0B72,
  76. DEPTH_CLEAR_VALUE: 0x0B73,
  77. DEPTH_FUNC: 0x0B74,
  78. STENCIL_CLEAR_VALUE: 0x0B91,
  79. STENCIL_FUNC: 0x0B92,
  80. STENCIL_FAIL: 0x0B94,
  81. STENCIL_PASS_DEPTH_FAIL: 0x0B95,
  82. STENCIL_PASS_DEPTH_PASS: 0x0B96,
  83. STENCIL_REF: 0x0B97,
  84. STENCIL_VALUE_MASK: 0x0B93,
  85. STENCIL_WRITEMASK: 0x0B98,
  86. STENCIL_BACK_FUNC: 0x8800,
  87. STENCIL_BACK_FAIL: 0x8801,
  88. STENCIL_BACK_PASS_DEPTH_FAIL: 0x8802,
  89. STENCIL_BACK_PASS_DEPTH_PASS: 0x8803,
  90. STENCIL_BACK_REF: 0x8CA3,
  91. STENCIL_BACK_VALUE_MASK: 0x8CA4,
  92. STENCIL_BACK_WRITEMASK: 0x8CA5,
  93. VIEWPORT: 0x0BA2,
  94. SCISSOR_BOX: 0x0C10,
  95. COLOR_CLEAR_VALUE: 0x0C22,
  96. COLOR_WRITEMASK: 0x0C23,
  97. UNPACK_ALIGNMENT: 0x0CF5,
  98. PACK_ALIGNMENT: 0x0D05,
  99. MAX_TEXTURE_SIZE: 0x0D33,
  100. MAX_VIEWPORT_DIMS: 0x0D3A,
  101. SUBPIXEL_BITS: 0x0D50,
  102. RED_BITS: 0x0D52,
  103. GREEN_BITS: 0x0D53,
  104. BLUE_BITS: 0x0D54,
  105. ALPHA_BITS: 0x0D55,
  106. DEPTH_BITS: 0x0D56,
  107. STENCIL_BITS: 0x0D57,
  108. POLYGON_OFFSET_UNITS: 0x2A00,
  109. POLYGON_OFFSET_FACTOR: 0x8038,
  110. TEXTURE_BINDING_2D: 0x8069,
  111. SAMPLE_BUFFERS: 0x80A8,
  112. SAMPLES: 0x80A9,
  113. RGBA8: 0x8058,
  114. SAMPLE_COVERAGE_VALUE: 0x80AA,
  115. SAMPLE_COVERAGE_INVERT: 0x80AB,
  116. COMPRESSED_TEXTURE_FORMATS: 0x86A3,
  117. DONT_CARE: 0x1100,
  118. FASTEST: 0x1101,
  119. NICEST: 0x1102,
  120. GENERATE_MIPMAP_HINT: 0x8192,
  121. BYTE: 0x1400,
  122. UNSIGNED_BYTE: 0x1401,
  123. SHORT: 0x1402,
  124. UNSIGNED_SHORT: 0x1403,
  125. INT: 0x1404,
  126. UNSIGNED_INT: 0x1405,
  127. FLOAT: 0x1406,
  128. DEPTH_COMPONENT: 0x1902,
  129. ALPHA: 0x1906,
  130. RGB: 0x1907,
  131. RGBA: 0x1908,
  132. LUMINANCE: 0x1909,
  133. LUMINANCE_ALPHA: 0x190A,
  134. UNSIGNED_SHORT_4_4_4_4: 0x8033,
  135. UNSIGNED_SHORT_5_5_5_1: 0x8034,
  136. UNSIGNED_SHORT_5_6_5: 0x8363,
  137. FRAGMENT_SHADER: 0x8B30,
  138. VERTEX_SHADER: 0x8B31,
  139. MAX_VERTEX_ATTRIBS: 0x8869,
  140. MAX_VERTEX_UNIFORM_VECTORS: 0x8DFB,
  141. MAX_VARYING_VECTORS: 0x8DFC,
  142. MAX_COMBINED_TEXTURE_IMAGE_UNITS: 0x8B4D,
  143. MAX_VERTEX_TEXTURE_IMAGE_UNITS: 0x8B4C,
  144. MAX_TEXTURE_IMAGE_UNITS: 0x8872,
  145. MAX_FRAGMENT_UNIFORM_VECTORS: 0x8DFD,
  146. SHADER_TYPE: 0x8B4F,
  147. DELETE_STATUS: 0x8B80,
  148. LINK_STATUS: 0x8B82,
  149. VALIDATE_STATUS: 0x8B83,
  150. ATTACHED_SHADERS: 0x8B85,
  151. ACTIVE_UNIFORMS: 0x8B86,
  152. ACTIVE_ATTRIBUTES: 0x8B89,
  153. SHADING_LANGUAGE_VERSION: 0x8B8C,
  154. CURRENT_PROGRAM: 0x8B8D,
  155. NEVER: 0x0200,
  156. LESS: 0x0201,
  157. EQUAL: 0x0202,
  158. LEQUAL: 0x0203,
  159. GREATER: 0x0204,
  160. NOTEQUAL: 0x0205,
  161. GEQUAL: 0x0206,
  162. ALWAYS: 0x0207,
  163. KEEP: 0x1E00,
  164. REPLACE: 0x1E01,
  165. INCR: 0x1E02,
  166. DECR: 0x1E03,
  167. INVERT: 0x150A,
  168. INCR_WRAP: 0x8507,
  169. DECR_WRAP: 0x8508,
  170. VENDOR: 0x1F00,
  171. RENDERER: 0x1F01,
  172. VERSION: 0x1F02,
  173. NEAREST: 0x2600,
  174. LINEAR: 0x2601,
  175. NEAREST_MIPMAP_NEAREST: 0x2700,
  176. LINEAR_MIPMAP_NEAREST: 0x2701,
  177. NEAREST_MIPMAP_LINEAR: 0x2702,
  178. LINEAR_MIPMAP_LINEAR: 0x2703,
  179. TEXTURE_MAG_FILTER: 0x2800,
  180. TEXTURE_MIN_FILTER: 0x2801,
  181. TEXTURE_WRAP_S: 0x2802,
  182. TEXTURE_WRAP_T: 0x2803,
  183. TEXTURE_2D: 0x0DE1,
  184. TEXTURE: 0x1702,
  185. TEXTURE_CUBE_MAP: 0x8513,
  186. TEXTURE_BINDING_CUBE_MAP: 0x8514,
  187. TEXTURE_CUBE_MAP_POSITIVE_X: 0x8515,
  188. TEXTURE_CUBE_MAP_NEGATIVE_X: 0x8516,
  189. TEXTURE_CUBE_MAP_POSITIVE_Y: 0x8517,
  190. TEXTURE_CUBE_MAP_NEGATIVE_Y: 0x8518,
  191. TEXTURE_CUBE_MAP_POSITIVE_Z: 0x8519,
  192. TEXTURE_CUBE_MAP_NEGATIVE_Z: 0x851A,
  193. MAX_CUBE_MAP_TEXTURE_SIZE: 0x851C,
  194. TEXTURE0: 0x84C0,
  195. TEXTURE1: 0x84C1,
  196. TEXTURE2: 0x84C2,
  197. TEXTURE3: 0x84C3,
  198. TEXTURE4: 0x84C4,
  199. TEXTURE5: 0x84C5,
  200. TEXTURE6: 0x84C6,
  201. TEXTURE7: 0x84C7,
  202. TEXTURE8: 0x84C8,
  203. TEXTURE9: 0x84C9,
  204. TEXTURE10: 0x84CA,
  205. TEXTURE11: 0x84CB,
  206. TEXTURE12: 0x84CC,
  207. TEXTURE13: 0x84CD,
  208. TEXTURE14: 0x84CE,
  209. TEXTURE15: 0x84CF,
  210. TEXTURE16: 0x84D0,
  211. TEXTURE17: 0x84D1,
  212. TEXTURE18: 0x84D2,
  213. TEXTURE19: 0x84D3,
  214. TEXTURE20: 0x84D4,
  215. TEXTURE21: 0x84D5,
  216. TEXTURE22: 0x84D6,
  217. TEXTURE23: 0x84D7,
  218. TEXTURE24: 0x84D8,
  219. TEXTURE25: 0x84D9,
  220. TEXTURE26: 0x84DA,
  221. TEXTURE27: 0x84DB,
  222. TEXTURE28: 0x84DC,
  223. TEXTURE29: 0x84DD,
  224. TEXTURE30: 0x84DE,
  225. TEXTURE31: 0x84DF,
  226. ACTIVE_TEXTURE: 0x84E0,
  227. REPEAT: 0x2901,
  228. CLAMP_TO_EDGE: 0x812F,
  229. MIRRORED_REPEAT: 0x8370,
  230. FLOAT_VEC2: 0x8B50,
  231. FLOAT_VEC3: 0x8B51,
  232. FLOAT_VEC4: 0x8B52,
  233. INT_VEC2: 0x8B53,
  234. INT_VEC3: 0x8B54,
  235. INT_VEC4: 0x8B55,
  236. BOOL: 0x8B56,
  237. BOOL_VEC2: 0x8B57,
  238. BOOL_VEC3: 0x8B58,
  239. BOOL_VEC4: 0x8B59,
  240. FLOAT_MAT2: 0x8B5A,
  241. FLOAT_MAT3: 0x8B5B,
  242. FLOAT_MAT4: 0x8B5C,
  243. SAMPLER_2D: 0x8B5E,
  244. SAMPLER_CUBE: 0x8B60,
  245. VERTEX_ATTRIB_ARRAY_ENABLED: 0x8622,
  246. VERTEX_ATTRIB_ARRAY_SIZE: 0x8623,
  247. VERTEX_ATTRIB_ARRAY_STRIDE: 0x8624,
  248. VERTEX_ATTRIB_ARRAY_TYPE: 0x8625,
  249. VERTEX_ATTRIB_ARRAY_NORMALIZED: 0x886A,
  250. VERTEX_ATTRIB_ARRAY_POINTER: 0x8645,
  251. VERTEX_ATTRIB_ARRAY_BUFFER_BINDING: 0x889F,
  252. IMPLEMENTATION_COLOR_READ_TYPE: 0x8B9A,
  253. IMPLEMENTATION_COLOR_READ_FORMAT: 0x8B9B,
  254. COMPILE_STATUS: 0x8B81,
  255. LOW_FLOAT: 0x8DF0,
  256. MEDIUM_FLOAT: 0x8DF1,
  257. HIGH_FLOAT: 0x8DF2,
  258. LOW_INT: 0x8DF3,
  259. MEDIUM_INT: 0x8DF4,
  260. HIGH_INT: 0x8DF5,
  261. FRAMEBUFFER: 0x8D40,
  262. RENDERBUFFER: 0x8D41,
  263. RGBA4: 0x8056,
  264. RGB5_A1: 0x8057,
  265. RGB565: 0x8D62,
  266. DEPTH_COMPONENT16: 0x81A5,
  267. STENCIL_INDEX8: 0x8D48,
  268. DEPTH_STENCIL: 0x84F9,
  269. RENDERBUFFER_WIDTH: 0x8D42,
  270. RENDERBUFFER_HEIGHT: 0x8D43,
  271. RENDERBUFFER_INTERNAL_FORMAT: 0x8D44,
  272. RENDERBUFFER_RED_SIZE: 0x8D50,
  273. RENDERBUFFER_GREEN_SIZE: 0x8D51,
  274. RENDERBUFFER_BLUE_SIZE: 0x8D52,
  275. RENDERBUFFER_ALPHA_SIZE: 0x8D53,
  276. RENDERBUFFER_DEPTH_SIZE: 0x8D54,
  277. RENDERBUFFER_STENCIL_SIZE: 0x8D55,
  278. FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE: 0x8CD0,
  279. FRAMEBUFFER_ATTACHMENT_OBJECT_NAME: 0x8CD1,
  280. FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL: 0x8CD2,
  281. FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE: 0x8CD3,
  282. COLOR_ATTACHMENT0: 0x8CE0,
  283. DEPTH_ATTACHMENT: 0x8D00,
  284. STENCIL_ATTACHMENT: 0x8D20,
  285. DEPTH_STENCIL_ATTACHMENT: 0x821A,
  286. NONE: 0,
  287. FRAMEBUFFER_COMPLETE: 0x8CD5,
  288. FRAMEBUFFER_INCOMPLETE_ATTACHMENT: 0x8CD6,
  289. FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: 0x8CD7,
  290. FRAMEBUFFER_INCOMPLETE_DIMENSIONS: 0x8CD9,
  291. FRAMEBUFFER_UNSUPPORTED: 0x8CDD,
  292. FRAMEBUFFER_BINDING: 0x8CA6,
  293. RENDERBUFFER_BINDING: 0x8CA7,
  294. MAX_RENDERBUFFER_SIZE: 0x84E8,
  295. INVALID_FRAMEBUFFER_OPERATION: 0x0506,
  296. UNPACK_FLIP_Y_WEBGL: 0x9240,
  297. UNPACK_PREMULTIPLY_ALPHA_WEBGL: 0x9241,
  298. CONTEXT_LOST_WEBGL: 0x9242,
  299. UNPACK_COLORSPACE_CONVERSION_WEBGL: 0x9243,
  300. BROWSER_DEFAULT_WEBGL: 0x9244,
  301. TEXTURE_MAX_LOD: 0x813B,
  302. TEXTURE_BASE_LEVEL: 0x813C,
  303. TEXTURE_IMMUTABLE_FORMAT: 0x912F,
  304. UNIFORM_BLOCK_BINDING: 0x8A3F,
  305. UNIFORM_BLOCK_DATA_SIZE: 0x8A40,
  306. UNIFORM_BLOCK_ACTIVE_UNIFORMS: 0x8A42,
  307. UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES: 0x8A43,
  308. UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER: 0x8A44,
  309. UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER: 0x8A46,
  310. RED: 0x1903,
  311. PIXEL_UNPACK_BUFFER: 0x88EC,
  312. RGB8: 0x8051,
  313. R16F: 0x822D,
  314. COPY_WRITE_BUFFER: 0x8F37,
  315. TEXTURE_3D: 0x806F,
  316. COMPRESSED_R11_EAC: 0x9270,
  317. COPY_READ_BUFFER: 0x8F36,
  318. TRANSFORM_FEEDBACK_BUFFER: 0x8C8E,
  319. TRANSFORM_FEEDBACK_BUFFER_BINDING: 0x8C8F,
  320. TRANSFORM_FEEDBACK_BUFFER_SIZE: 0x8C85,
  321. TRANSFORM_FEEDBACK_BUFFER_START: 0x8C84,
  322. UNIFORM_BUFFER_BINDING: 0x8A28,
  323. UNIFORM_BUFFER_SIZE: 0x8A2A,
  324. UNIFORM_BUFFER_START: 0x8A29,
  325. DYNAMIC_READ: 0x88E9,
  326. READ_FRAMEBUFFER: 0x8CA8,
  327. COLOR_ATTACHMENT1: 0x8CE1,
  328. INTERLEAVED_ATTRIBS: 0x8C8C,
  329. UNIFORM_OFFSET: 0x8A3B,
  330. UNIFORM_TYPE: 0x8A37,
  331. UNIFORM_SIZE: 0x8A38,
  332. UNIFORM_BLOCK_INDEX: 0x8A3A,
  333. UNIFORM_ARRAY_STRIDE: 0x8A3C,
  334. UNIFORM_MATRIX_STRIDE: 0x8A3D,
  335. UNIFORM_IS_ROW_MAJOR: 0x8A3E,
  336. TEXTURE_MAX_ANISOTROPY_EXT: 0x84FE
  337. }
  338. // 顶点着色器程序
  339. var VSHADER_SOURCE =
  340. 'attribute vec4 a_Position;\n' + // attribute variable
  341. 'attribute vec4 a_Color;\n' +
  342. 'varying vec4 v_Color;\n' +
  343. 'void main() {\n' +
  344. ' gl_Position = a_Position;\n' + // Set the vertex coordinates of the point
  345. ' v_Color = a_Color;\n' +
  346. '}\n';
  347. // 片元着色器程序
  348. var FSHADER_SOURCE =
  349. 'precision mediump float;\n' +
  350. 'varying vec4 v_Color;\n' +
  351. 'void main() {\n' +
  352. ' gl_FragColor = v_Color;\n' +
  353. '}\n';
  354. function initVertexBuffers(gl) {
  355. // 顶点坐标和颜色
  356. var verticesColors = new Float32Array([
  357. 0.0, -0.5, 1.0, 0.0, 0.0,
  358. -0.5, -0.8, 0.0, 1.0, 0.0,
  359. 0.5, -0.8, 0.0, 0.0, 1.0,
  360. ]);
  361. var n = 3; // 点的个数
  362. var FSIZE = verticesColors.BYTES_PER_ELEMENT; //数组中每个元素的字节数
  363. // 创建缓冲区对象
  364. var vertexBuffer = gl.createBuffer();
  365. if (!vertexBuffer) {
  366. console.log('Failed to create the buffer object');
  367. return -1;
  368. }
  369. // 将缓冲区对象绑定到目标
  370. gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
  371. // 向缓冲区对象写入数据
  372. gl.bufferData(gl.ARRAY_BUFFER, verticesColors.buffer, gl.STATIC_DRAW);
  373. // 获取着色器中attribute变量a_Position的地址
  374. var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
  375. if (a_Position < 0) {
  376. console.log('Failed to get the storage location of a_Position');
  377. return -1;
  378. }
  379. // 将缓冲区对象分配给a_Position变量
  380. gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 5 * FSIZE, 0);
  381. // 连接a_Position变量与分配给它的缓冲区对象
  382. gl.enableVertexAttribArray(a_Position);
  383. // 获取着色器中attribute变量a_Color的地址
  384. var a_Color = gl.getAttribLocation(gl.program, 'a_Color');
  385. if (a_Color < 0) {
  386. console.log('Failed to get the storage location of a_Color');
  387. return -1;
  388. }
  389. // 将缓冲区对象分配给a_Color变量
  390. gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, FSIZE * 5, FSIZE * 2);
  391. // 连接a_Color变量与分配给它的缓冲区对象
  392. gl.enableVertexAttribArray(a_Color);
  393. // 解除绑定
  394. gl.bindBuffer(gl.ARRAY_BUFFER, null);
  395. return n;
  396. }
  397. /**
  398. * 创建并使能一个program对象
  399. * @param gl 表示 WebGL上下文对
  400. * @param vshader 表示顶点着色器
  401. * @param fshader 表示片段着色器
  402. * @return 如果WebGLProgram对象被创建并成功作为当前对象,则返回true;否则返回false
  403. */
  404. function initShaders(gl, vshader, fshader) {
  405. var program = createProgram(gl, vshader, fshader);
  406. console.log("======createProgram program: " + program);
  407. if (!program) {
  408. console.log('Failed to create program');
  409. return false;
  410. }
  411. gl.useProgram(program);
  412. gl.program = program;
  413. return true;
  414. }
  415. /**
  416. * 创建一个linked program对象
  417. * @param gl 表示 WebGL上下文对象
  418. * @param vshader 表示顶点着色器
  419. * @param fshader 表示片段着色器
  420. * @return 如果创建program成功,则返回创建的program;否则返回null
  421. */
  422. function createProgram(gl, vshader, fshader) {
  423. console.log("======createProgram start======");
  424. // 创建shader对象
  425. var vertexShader = loadShader(gl, gl.VERTEX_SHADER, vshader);
  426. console.log("======vertexShader: " + vertexShader);
  427. var fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fshader);
  428. if (!vertexShader || !fragmentShader) {
  429. return null;
  430. }
  431. // 创建program对象
  432. var program = gl.createProgram();
  433. console.log("======createProgram program: " + program);
  434. if (!program) {
  435. return null;
  436. }
  437. // 将着色器附加到对象
  438. gl.attachShader(program, vertexShader);
  439. gl.attachShader(program, fragmentShader);
  440. // 连接程序对象
  441. gl.linkProgram(program);
  442. // 检查连接对象的结果
  443. var linked = gl.getProgramParameter(program, 0x8B82);
  444. console.log("======getProgramParameter linked: " + linked);
  445. if (!linked) {
  446. var error = gl.getProgramInfoLog(program);
  447. console.log('Failed to link the program: ' + error);
  448. gl.deleteProgram(program);
  449. gl.deleteShader(fragmentShader);
  450. gl.deleteShader(vertexShader);
  451. return null;
  452. }
  453. return program;
  454. }
  455. /**
  456. * 创建一个shader对象
  457. * @param gl 表示 WebGL上下文对象
  458. * @param type 表示shader类型
  459. * @param source 表示shader的源码
  460. * @return 如果操作成功,返回创建的着色器对象;否则返回false
  461. */
  462. function loadShader(gl, type, source) {
  463. console.log("======into loadShader====");
  464. // 创建shader对象
  465. var shader = gl.createShader(type);
  466. if (shader == null) {
  467. console.log('Failed to create the shader.');
  468. return null;
  469. }
  470. // 设置shader program
  471. gl.shaderSource(shader, source);
  472. // 编译shader
  473. gl.compileShader(shader);
  474. // 检查shader编译结果
  475. var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
  476. if (!compiled) {
  477. var error = gl.getShaderInfoLog(shader);
  478. console.log('Failed to compile the shader: ' + error);
  479. gl.deleteShader(shader);
  480. return null;
  481. }
  482. return shader;
  483. }
  484. export default {
  485. data: {
  486. title: "DEMO BY TEAMOL",
  487. fit:"cover",
  488. fits: ["cover", "contain", "fill", "none", "scale-down"]
  489. }
  490. ,onInit() {
  491. this.title = this.$t('strings.world');
  492. }
  493. ,BtnColorTriangle() {
  494. // 获取canvas元素
  495. const el = this.$refs.canvas1;
  496. // 获取webgl上下文
  497. var gl = el.getContext('webgl');
  498. if (!gl) {
  499. console.log('Failed to get the rendering context for WebGL');
  500. return;
  501. }
  502. // 初始化着色器
  503. if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
  504. console.log('Failed to initialize shaders.');
  505. return;
  506. }
  507. // 设置顶点位置
  508. var n = initVertexBuffers(gl);
  509. if (n < 0) {
  510. console.log('Failed to set the positions of the vertices');
  511. return;
  512. }
  513. // 指定清空<canvas>的颜色
  514. gl.clearColor(0.0, 0.0, 0.0, 1.0);
  515. // 清空<canvas>
  516. gl.clear(gl.COLOR_BUFFER_BIT);
  517. // 绘制三角形
  518. gl.drawArrays(gl.TRIANGLES, 0, n);
  519. // 清buffer
  520. gl.flush();
  521. }
  522. }

3.点击按钮绘制彩色三角形的效果图
 

鸿蒙原生应用元服务开发-WebGL网页图形库开发着色器绘制彩色三角形-鸿蒙开发者社区


本文参考引用HarmonyOS官方开发文档,基于API9。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/233231
推荐阅读
相关标签
  

闽ICP备14008679号