From 84c9dc4e4e6eb0d592d4d8fa97b42befd12a3079 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Tue, 29 Mar 2016 16:37:58 +0300 Subject: [PATCH] fixes for #183 --- src/dlangui/graphics/gldrawbuf.d | 5 ++++- src/dlangui/graphics/glsupport.d | 6 +++--- src/dlangui/graphics/scene/effect.d | 5 +++-- src/dlangui/graphics/scene/material.d | 23 +++++++++++++++++++++-- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/dlangui/graphics/gldrawbuf.d b/src/dlangui/graphics/gldrawbuf.d index 9c46e632..0a9d8894 100644 --- a/src/dlangui/graphics/gldrawbuf.d +++ b/src/dlangui/graphics/gldrawbuf.d @@ -27,6 +27,9 @@ import dlangui.core.logger; private import dlangui.graphics.glsupport; private import std.algorithm; +/// Reference counted GLTexture object +alias TextureRef = Ref!GLTexture; + interface GLConfigCallback { void saveConfiguration(); void restoreConfiguration(); @@ -792,7 +795,7 @@ public: } /// GL Texture object from image -static class GLTexture { +static class GLTexture : RefCountedObject { protected string _resourceId; protected int _dx; protected int _dy; diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index 4506993e..74b6e9ca 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -25,9 +25,7 @@ static if (ENABLE_OPENGL): public import dlangui.core.math3d; import dlangui.graphics.scene.mesh; import dlangui.core.logger; -//import derelict.opengl3.gl3; import derelict.opengl3.gl; -//import derelict.opengl3.types; import dlangui.core.types; import std.conv; import std.string; @@ -115,7 +113,6 @@ string glerrorToString(in GLenum err) pure nothrow { class GLProgram : GraphicsEffect { - import derelict.opengl3.types; @property abstract string vertexSource(); @property abstract string fragmentSource(); protected GLuint program; @@ -990,6 +987,8 @@ enum GLObjectTypes { Buffer, VertexArray, Texture, Framebuffer }; * Note: on construction it binds itself to the target, and it binds 0 to target on destruction. * All methods (except ctor, dtor, bind(), unbind() and setup()) does not perform binding. */ + + class GLObject(GLObjectTypes type, GLuint target = 0) { immutable GLuint ID; //alias ID this; // good, but it confuses destroy() @@ -1061,6 +1060,7 @@ class GLObject(GLObjectTypes type, GLuint target = 0) { } } } + alias VAO = GLObject!(GLObjectTypes.VertexArray); alias VBO = GLObject!(GLObjectTypes.Buffer, GL_ARRAY_BUFFER); alias Tex2D = GLObject!(GLObjectTypes.Texture, GL_TEXTURE_2D); diff --git a/src/dlangui/graphics/scene/effect.d b/src/dlangui/graphics/scene/effect.d index e29a87cc..806f07b2 100644 --- a/src/dlangui/graphics/scene/effect.d +++ b/src/dlangui/graphics/scene/effect.d @@ -62,7 +62,7 @@ class Effect : GLProgram { } ~this() { - _instance.onObjectDestroyed(_id); + EffectCache.instance.onObjectDestroyed(_id); } protected void init() { @@ -151,6 +151,8 @@ class Effect : GLProgram { class EffectCache { private Effect[EffectId] _map; + private static __gshared EffectCache _instance; + /// returns effect cache singleton instance static @property EffectCache instance() { if (!_instance) @@ -179,4 +181,3 @@ class EffectCache { } } -private __gshared EffectCache _instance; diff --git a/src/dlangui/graphics/scene/material.d b/src/dlangui/graphics/scene/material.d index 8c22d7fc..ee44c3a3 100644 --- a/src/dlangui/graphics/scene/material.d +++ b/src/dlangui/graphics/scene/material.d @@ -13,11 +13,30 @@ alias MaterialRef = Ref!Material; class Material : RefCountedObject { protected EffectRef _effect; - protected GLTexture _texture; + protected TextureRef _texture; + // TODO: more material properties @property EffectRef effect() { return _effect; } - @property Material effect(EffectRef e) { + /// set as effect instance + @property Material effect(EffectRef e) { _effect = e; return this; } + /// set as effect id + @property Material effect(EffectId effectId) { + _effect = EffectCache.instance.get(effectId); + return this; + } + + @property TextureRef texture() { return _texture; } + /// set texture + @property Material texture(TextureRef e) { + _texture = e; + return this; + } + /// set texture from resourceId + @property Material texture(string resourceId) { + _texture = GLTextureCache.instance.get(resourceId); + return this; + } }