1

我喜欢 Dart,所以一直在写一个小型的 WebGL 游戏。我有一个Shaders非常基础的课程:

class Shaders {
  static final int FRAGMENT = 0;
  static final int VERTEX = 1;

  HashMap<String, webGL.UniformLocation> _uniforms;

  webGL.Program           _program;
  webGL.Shader            _vertShader;
  webGL.Shader            _fragShader;
  webGL.RenderingContext  _gl;


  Shaders(this._gl) {
    _uniforms = new HashMap();
    _program = _gl.createProgram();
    _vertShader = _gl.createShader(webGL.VERTEX_SHADER);
    _fragShader = _gl.createShader(webGL.FRAGMENT_SHADER);
  }

  Future<String> loadSource(String filename) {
    return HttpRequest.getString(filename);
  }

  void compileSource(int type, String data) {
    if (type == VERTEX) {
      _gl.shaderSource(_vertShader, data);
      _gl.compileShader(_vertShader);
      _gl.attachShader(_program, _vertShader);

      if (!_gl.getShaderParameter(_vertShader, webGL.COMPILE_STATUS))
          print("Vertex shader: ${_gl.getShaderInfoLog(_vertShader)}");

    } else if (type == FRAGMENT) {
      _gl.shaderSource(_fragShader, data);
      _gl.compileShader(_fragShader);
      _gl.attachShader(_program, _fragShader);

      if (!_gl.getShaderParameter(_fragShader, webGL.COMPILE_STATUS))
          print("Vertex shader: ${_gl.getShaderInfoLog(_fragShader)}");
    }
  }

  void setUniformMatrix(String name, var mat) {
    //if we dont have this uniform get it and store for later
    if (!_uniforms.containsKey(name)) {
      var loc = _gl.getUniformLocation(_program, name);
      _uniforms[name] = loc;
    }

    _gl.uniformMatrix4fv(_uniforms[name], false, mat.data());
  }

  void bindAttribute(int index, String name) {
    _gl.bindAttribLocation(_program, index, name);
  }

  void useShader() {
    _gl.linkProgram(_program);
    _gl.useProgram(_program);
  }
}

我还有一个RenderComponent类,当它们应该是可绘制的时,我将它们提供给实体。它看起来像这样(这些类没有完成):

abstract class Component {
  static final int RENDER = 1;
  int _type;

  Component(this._type);  
  int get type => _type;
}


class RenderComponent extends Component {
  BufferObject _vbo;
  Shaders _shader;

  RenderComponent() : super(Component.RENDER);

  set shader(Shaders s) => _shader = s;
  set buffer(BufferObject v) => _vbo = v;

  Shaders get shader           => _shader;
  BufferObject get buffer      => _vbo;
}

当我创建一个对象RenderComponent并设置shader如下:

var s = new Shaders(pass in gl context);
var r = new RenderComponent();

r.shader = s;

我收到此错误消息:

打破例外:“着色器”类型不是“s”的“着色器”类型的子类型。

谷歌没有想出任何对我有意义的东西。这是我遇到过的最无益的信息,我无法摆脱它。有谁知道是什么原因造成的?

4

0 回答 0