public void Apply(ref Matrix4 world) { var mat = ScopedObject.Find <Material>(); if (mat == null) { throw new InvalidOperationException("There is no active material."); } var shader = mat.Shader; Matrix4 wvp; Matrix4.Mult(ref _view, ref _projection, out wvp); Matrix4.Mult(ref world, ref wvp, out wvp); Matrix4 wv; Matrix4.Mult(ref world, ref _view, out wv); Matrix4 nw = wv; nw.M41 = nw.M42 = nw.M43 = 0f; nw.Invert(); nw.Transpose(); shader.Uniform("World", ref world); shader.Uniform("WorldView", ref wv); shader.Uniform("WorldViewProjection", ref wvp); shader.Uniform("NormalMatrix", ref nw); }
public virtual void Draw(Vector3 pos, float scaleX = 1f, float scaleY = 1f, float rotation = 0f) { var cam = ScopedObject.Find <Camera> (); if (cam == null) { throw new InvalidOperationException("There is no active camera."); } Matrix4 world; Matrix4.CreateTranslation(ref pos, out world); if (scaleX != 1f || scaleY != 1f) { Matrix4 tmp = Matrix4.Scale(scaleX, scaleY, 1f); Matrix4.Mult(ref tmp, ref world, out world); } if (rotation != 0f) { Matrix4 tmp; Matrix4.CreateFromAxisAngle(Vector3.UnitZ, rotation, out tmp); Matrix4.Mult(ref tmp, ref world, out world); } this.Draw(ref world); }
protected override void OnDraw(ref Matrix4 world) { var cam = ScopedObject.Find <Camera> (); if (cam == null) { throw new InvalidOperationException("There is no active camera."); } var mat = ScopedObject.Find <Material>(); if (mat == null) { using (_material.Begin()) { cam.Apply(ref world); using (_vbuffer.Begin()) { _ibuffer.Draw(_ioffset, _icount); } } } else { cam.Apply(ref world); using (_vbuffer.Begin()) { _ibuffer.Draw(_ioffset, _icount); } } }
protected override void OnBegin() { var mat = ScopedObject.Find <Material>(); if (mat == null) { throw new InvalidOperationException("There is no active material."); } if (ScopedObject.Find <VertexBuffer>() != null) { throw new InvalidOperationException("there is already an active vertex buffer."); } #if __DESKTOP__ GL.BindVertexArray(_vao); #endif var stride = _format.Stride * sizeof(float); GL.BindBuffer(BufferTarget.ArrayBuffer, _handle); foreach (var el in _format.Elements) { var loc = mat.Shader.Attribute(el.Name); if (loc >= 0) { GL.EnableVertexAttribArray(loc); GL.VertexAttribPointer(loc, el.Size, VertexAttribPointerType.Float, false, stride, (IntPtr)(el.Offset * sizeof(float))); } } }
public void Save(string path) { if (ScopedObject.Find <FrameBuffer>() != this) { throw new InvalidOperationException("FBO must be active to save its contents."); } using (var stream = Assets.ResolveUserStream(path, FileMode.Create, FileAccess.Write)) { Save(stream); } }
protected override void OnBegin() { if (ScopedObject.Find <Material>() != null) { throw new InvalidOperationException("There is already an active material."); } if (_shader == null) { throw new InvalidOperationException("The material has no shader."); } GL.UseProgram(_shader.Handle); }
public void Draw(ref Matrix4 world) { IBatchable batchable; Batch batch; if ((batchable = this as IBatchable) != null && (batch = ScopedObject.Find <Batch> ()) != null) { batch.Draw(batchable, ref world); } else { this.OnDraw(ref world); } }
public void Save(Stream stream) { if (ScopedObject.Find <FrameBuffer>() != this) { throw new InvalidOperationException("FBO must be active to save its contents."); } var img = new byte[_size.Width * _size.Height * 4]; GL.PixelStore(PixelStoreParameter.PackAlignment, 4); GL.ReadPixels(0, 0, _size.Width, _size.Height, PixelFormat.Rgba, PixelType.UnsignedByte, img); var buf = PngLoader.Encode(img, Size); stream.Write(buf, 0, buf.Length); }
protected override void OnEnd() { var mat = ScopedObject.Find <Material>(); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); if (mat != null) { foreach (var el in _format.Elements) { var loc = mat.Shader.Attribute(el.Name); if (loc >= 0) { GL.DisableVertexAttribArray(loc); } } } }
protected override void OnEnd() { var cam = ScopedObject.Find <Camera>(); if (cam == null) { throw new InvalidOperationException("There is no active camera."); } if (_vbuffer == null || _ibuffer == null) { return; } _vbuffer.Commit(); _ibuffer.Commit(); using (_material.Begin()) { cam.Apply(ref Matrix4.Identity); using (_vbuffer.Begin()) { _ibuffer.Draw(0, _iidx); } } }
public void Draw(ref Matrix4 world) { var cam = ScopedObject.Find <Camera>(); if (cam == null) { throw new InvalidOperationException("There is no active camera."); } var lights = ScopedObject.Find <Lighting>(); if (_bones != null) { if (_boneSet == null) { _boneSet = new HashSet <Node>(_bones.Select(b => b.Node)); } var identity = Matrix4.Identity; foreach (var node in _node.Parent.Children) { if (node != _node) { this.PrepareBoneTransforms(node, ref identity); } } if (_node.Children != null) { foreach (var child in _node.Children) { this.PrepareBoneTransforms(child, ref identity); } } } var mat = ScopedObject.Find <Material>(); if (mat == null) { using (_material.Begin()) { cam.Apply(ref world); if (lights != null) { lights.Apply(ref world); } if (_bones != null && _bones.Length > 0) { _material.Shader.Uniform("Bones[0]", _boneTransforms); } using (_vbuffer.Begin()) { _ibuffer.Draw(); } } } else { cam.Apply(ref world); if (lights != null) { lights.Apply(ref world); } if (_bones != null && _bones.Length > 0) { mat.Shader.Uniform("Bones[0]", _boneTransforms); } using (_vbuffer.Begin()) { _ibuffer.Draw(); } } }
public Scope(ScopedObject obj) { _obj = obj; }
public void Apply(ref Matrix4 world) { var mat = ScopedObject.Find <Material>(); if (mat == null) { throw new InvalidOperationException("A material must be in scope to use lighting."); } var cam = ScopedObject.Find <Camera>(); if (cam == null) { throw new InvalidOperationException("A camera must be in scope to use lighting."); } var shader = mat.Shader; if (shader.MaxNumLights == 0) { throw new InvalidOperationException("The selected shader does not support lights."); } if (shader.MaxNumLights < _lights.Length) { throw new InvalidOperationException("Too many lights for the selected shader."); } var view = cam.View; var viewRot = view; viewRot.Invert(); viewRot.Transpose(); var locType = shader.Uniform("LightType[0]"); var locVector = shader.Uniform("LightVector[0]"); var locAmbient = shader.Uniform("LightAmbient[0]"); var locDiffuse = shader.Uniform("LightDiffuse[0]"); var locSpecular = shader.Uniform("LightSpecular[0]"); var locAtten = shader.Uniform("LightAttenuation[0]"); for (var i = 0; i < _lights.Length; i++) { var light = _lights[i]; if (locType >= 0) { GL.Uniform1(locType + i, (int)light.Type); } if (locVector >= 0) { var v = light.Vector; if (light.Type == LightType.Directional) { Vector3.Transform(ref v, ref viewRot, out v); v.Normalize(); } else if (light.Type == LightType.Point) { Vector3.Transform(ref v, ref view, out v); } GL.Uniform3(locVector + i, v); } if (locAmbient >= 0) { GL.Uniform3(locAmbient + i, light.Ambient); } if (locDiffuse >= 0) { GL.Uniform3(locDiffuse + i, light.Diffuse); } if (locSpecular >= 0) { GL.Uniform3(locSpecular + i, light.Specular); } if (locAtten >= 0) { GL.Uniform3(locAtten + i, light.Attenuation); } } }