#183 : lights, initial version

This commit is contained in:
Vadim Lopatin 2016-04-01 15:26:21 +03:00
parent 815283e25c
commit 634dfc195c
7 changed files with 52 additions and 4 deletions

View File

@ -284,6 +284,7 @@
<Compile Include="src\dlangui\graphics\scene\camera.d" /> <Compile Include="src\dlangui\graphics\scene\camera.d" />
<Compile Include="src\dlangui\graphics\scene\drawableobject.d" /> <Compile Include="src\dlangui\graphics\scene\drawableobject.d" />
<Compile Include="src\dlangui\graphics\scene\effect.d" /> <Compile Include="src\dlangui\graphics\scene\effect.d" />
<Compile Include="src\dlangui\graphics\scene\light.d" />
<Compile Include="src\dlangui\graphics\scene\material.d" /> <Compile Include="src\dlangui\graphics\scene\material.d" />
<Compile Include="src\dlangui\graphics\scene\mesh.d" /> <Compile Include="src\dlangui\graphics\scene\mesh.d" />
<Compile Include="src\dlangui\graphics\scene\model.d" /> <Compile Include="src\dlangui\graphics\scene\model.d" />

View File

@ -143,6 +143,7 @@
<Compile Include="src\dlangui\graphics\scene\camera.d" /> <Compile Include="src\dlangui\graphics\scene\camera.d" />
<Compile Include="src\dlangui\graphics\scene\drawableobject.d" /> <Compile Include="src\dlangui\graphics\scene\drawableobject.d" />
<Compile Include="src\dlangui\graphics\scene\effect.d" /> <Compile Include="src\dlangui\graphics\scene\effect.d" />
<Compile Include="src\dlangui\graphics\scene\light.d" />
<Compile Include="src\dlangui\graphics\scene\material.d" /> <Compile Include="src\dlangui\graphics\scene\material.d" />
<Compile Include="src\dlangui\graphics\scene\mesh.d" /> <Compile Include="src\dlangui\graphics\scene\mesh.d" />
<Compile Include="src\dlangui\graphics\scene\model.d" /> <Compile Include="src\dlangui\graphics\scene\model.d" />

View File

@ -775,6 +775,7 @@
<File path="src\dlangui\graphics\scene\camera.d" /> <File path="src\dlangui\graphics\scene\camera.d" />
<File path="src\dlangui\graphics\scene\drawableobject.d" /> <File path="src\dlangui\graphics\scene\drawableobject.d" />
<File path="src\dlangui\graphics\scene\effect.d" /> <File path="src\dlangui\graphics\scene\effect.d" />
<File path="src\dlangui\graphics\scene\light.d" />
<File path="src\dlangui\graphics\scene\material.d" /> <File path="src\dlangui\graphics\scene\material.d" />
<File path="src\dlangui\graphics\scene\mesh.d" /> <File path="src\dlangui\graphics\scene\mesh.d" />
<File path="src\dlangui\graphics\scene\model.d" /> <File path="src\dlangui\graphics\scene\model.d" />

View File

@ -138,7 +138,7 @@ class UiWidget : VerticalLayout, CellVisitor {
string src = loadTextResource("suzanne.obj"); string src = loadTextResource("suzanne.obj");
importer.parse(src); importer.parse(src);
Log.d("suzanne mesh:", importer.mesh.dumpVertexes(20)); Log.d("suzanne mesh:", importer.mesh.dumpVertexes(20));
Material suzanneMaterial = new Material(EffectId("colored.vert", "colored.frag", null), null); Material suzanneMaterial = new Material(EffectId("colored.vert", "colored.frag", null), "DIRECTIONAL_LIGHT_COUNT 1");
Model suzanneDrawable = new Model(suzanneMaterial, importer.mesh); Model suzanneDrawable = new Model(suzanneMaterial, importer.mesh);
Node3d suzanneNode = new Node3d("suzanne", suzanneDrawable); Node3d suzanneNode = new Node3d("suzanne", suzanneDrawable);
//suzanneNode.translate(vec3(3, 4, 5)); //suzanneNode.translate(vec3(3, 4, 5));

View File

@ -16,6 +16,7 @@ alias EffectRef = Ref!Effect;
class Effect : GLProgram { class Effect : GLProgram {
EffectId _id; EffectId _id;
string[string] _defs; string[string] _defs;
string _defText;
@property ref const(EffectId) id() const { return _id; } @property ref const(EffectId) id() const { return _id; }
this(EffectId id) { this(EffectId id) {
@ -35,13 +36,21 @@ class Effect : GLProgram {
// parse defs // parse defs
import std.array : split; import std.array : split;
string[] defs = _id.definitions.split(";"); string[] defs = _id.definitions.split(";");
char[] buf;
foreach(def; defs) { foreach(def; defs) {
assert(def.length > 0); assert(def.length > 0);
string[] items = def.split(" "); string[] items = def.split(" ");
if (items.length > 0) { if (items.length > 0) {
_defs[items[0]] = items.length > 1 ? items[1] : ""; string value = items.length > 1 ? items[1] : "";
_defs[items[0]] = value;
buf ~= "#define ";
buf ~= items[0];
buf ~= " ";
buf ~= value;
buf ~= "\n";
} }
} }
_defText = buf.dup;
// compile shaders // compile shaders
if (!check()) { if (!check()) {
Log.e("Failed to compile shaders ", _id.vertexShaderName, " ", _id.fragmentShaderName, " ", _id.definitions); Log.e("Failed to compile shaders ", _id.vertexShaderName, " ", _id.fragmentShaderName, " ", _id.definitions);
@ -50,8 +59,8 @@ class Effect : GLProgram {
} }
protected string preProcessSource(string src) { protected string preProcessSource(string src) {
// TODO: preprocess source code // prepend definitions
return src; return _defText ~ src;
} }
protected string loadVertexSource(string resourceId) { protected string loadVertexSource(string resourceId) {

View File

@ -0,0 +1,31 @@
module dlangui.graphics.scene.light;
import dlangui.core.math3d;
import dlangui.core.types;
enum LightType : ubyte {
directional,
point,
spot
}
/// Reference counted Light object
alias LightRef = Ref!Light;
class Light : RefCountedObject {
protected vec3 _color;
protected this(vec3 color) {}
@property vec3 color() { return _color; }
@property Light color(vec3 c) { _color = c; return this; }
@property LightType type() { return LightType.directional; }
/// create new directional light
static Light createDirectional(vec3 color) {
return new DirectionalLight(color);
}
}
protected class DirectionalLight : Light {
protected this(vec3 color) {
super(color);
}
}

View File

@ -6,6 +6,7 @@ import dlangui.graphics.scene.transform;
import dlangui.core.collections; import dlangui.core.collections;
import dlangui.graphics.scene.scene3d; import dlangui.graphics.scene.scene3d;
import dlangui.graphics.scene.drawableobject; import dlangui.graphics.scene.drawableobject;
import dlangui.graphics.scene.light;
/// 3D scene node /// 3D scene node
class Node3d : Transform { class Node3d : Transform {
@ -14,6 +15,7 @@ class Node3d : Transform {
protected string _id; protected string _id;
protected bool _visible = true; protected bool _visible = true;
protected DrawableObjectRef _drawable; protected DrawableObjectRef _drawable;
protected LightRef _light;
protected mat4 _worldMatrix; protected mat4 _worldMatrix;
@ -36,6 +38,9 @@ class Node3d : Transform {
/// drawable attached to node /// drawable attached to node
@property ref DrawableObjectRef drawable() { return _drawable; } @property ref DrawableObjectRef drawable() { return _drawable; }
/// light attached to node
@property ref LightRef light() { return _light; }
/// returns scene for node /// returns scene for node
@property Scene3d scene() { @property Scene3d scene() {
if (_scene) if (_scene)