2015年8月6日木曜日

WegGL Trial with Dartlang draw rect


四角形を表示するサンプル




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import 'dart:html' as html;
import 'dart:web_gl' as webgl;
import 'dart:typed_data' as data;

void main() {
  var canvas = new html.CanvasElement(width: 500, height: 500);
  html.document.body.append(canvas);

  webgl.RenderingContext GL = canvas.getContext3d();
  GL.clearColor(0.6, 0.2, 0.2, 1.0);
  GL.clear(webgl.RenderingContext.COLOR_BUFFER_BIT);

  // setup shader
  webgl.Shader vertexShader = loadShader(GL, webgl.RenderingContext.VERTEX_SHADER, 
          "attribute vec3 vp;\n" +
          "void main() {\n" +
          "  gl_Position = vec4(vp, 1.0);\n" +
          "}\n");

  webgl.Shader fragmentShader = loadShader(GL, webgl.RenderingContext.FRAGMENT_SHADER, 
          "precision mediump float;\n" +
          "void main() {\n" +
          " gl_FragColor = vec4(0.0,1.0,0.0,1.0);\n" +
          "}\n");

  webgl.Program shaderProgram = GL.createProgram();
  GL.attachShader(shaderProgram, fragmentShader);
  GL.attachShader(shaderProgram, vertexShader);
  GL.linkProgram(shaderProgram);
  GL.useProgram(shaderProgram);

  if (false == GL.getProgramParameter(shaderProgram, webgl.RenderingContext.LINK_STATUS)) {
    String message = "alert: Failed to linked shader";
    throw new Exception("${message}\n");
  }

  //
  // setup
  // leftup (x, y, z), leftdown, rightup, rightdown
  data.TypedData rectData = new data.Float32List.fromList([-0.8, 0.8, 0.0, -0.8, -0.8, 0.0, 0.8, 0.8, 0.0, 0.8, -0.8, 0.0]);
  data.TypedData rectDataIndex = new data.Uint16List.fromList([0, 1, 2, 1, 3, 2]);

  webgl.Buffer rectBuffer = GL.createBuffer();
  GL.bindBuffer(webgl.RenderingContext.ARRAY_BUFFER, rectBuffer);
  GL.bufferData(webgl.ARRAY_BUFFER, rectData, webgl.RenderingContext.STATIC_DRAW);

  webgl.Buffer rectIndexBuffer = GL.createBuffer();
  GL.bindBuffer(webgl.ELEMENT_ARRAY_BUFFER, rectIndexBuffer);
  GL.bufferDataTyped(webgl.RenderingContext.ELEMENT_ARRAY_BUFFER, rectDataIndex, webgl.RenderingContext.STATIC_DRAW);

  //
  // draw
  int locationVertexPosition = GL.getAttribLocation(shaderProgram, "vp");
  GL.vertexAttribPointer(locationVertexPosition, 3, webgl.RenderingContext.FLOAT, false, 0, 0);
  GL.enableVertexAttribArray(locationVertexPosition);
  GL.drawElements(webgl.RenderingContext.TRIANGLES, 6, webgl.RenderingContext.UNSIGNED_SHORT, 0);
}

webgl.Shader loadShader(webgl.RenderingContext context, int type, var src) {
  webgl.Shader shader = context.createShader(type);
  context.shaderSource(shader, src);
  context.compileShader(shader);
  if (false == context.getShaderParameter(shader, webgl.RenderingContext.COMPILE_STATUS)) {
    String message = "Error compiling shader ${context.getShaderInfoLog(shader)}";
    context.deleteShader(shader);
    throw new Exception("${message}\n");
  }
  return shader;
}

- source
https://github.com/kyorohiro/dart_hetimagl/tree/master/web

-------
Kyorohiro work

kyorohiro.strikingly.com

0 件のコメント:

コメントを投稿