From 815283e25cd3550a8dc9ae00a705f1ed10352a8e Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Fri, 1 Apr 2016 14:26:42 +0300 Subject: [PATCH] #183 : fix OBJ loader --- examples/d3d/src/d3d.d | 10 ++++++++++ src/dlangui/graphics/scene/effect.d | 2 +- src/dlangui/graphics/scene/mesh.d | 30 ++++++++++++++++++++++++---- src/dlangui/graphics/scene/node.d | 4 ++++ src/dlangui/graphics/scene/scene3d.d | 2 ++ 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/examples/d3d/src/d3d.d b/examples/d3d/src/d3d.d index eaa31298..8fd38ce8 100644 --- a/examples/d3d/src/d3d.d +++ b/examples/d3d/src/d3d.d @@ -137,6 +137,12 @@ class UiWidget : VerticalLayout, CellVisitor { ObjModelImport importer; string src = loadTextResource("suzanne.obj"); importer.parse(src); + Log.d("suzanne mesh:", importer.mesh.dumpVertexes(20)); + Material suzanneMaterial = new Material(EffectId("colored.vert", "colored.frag", null), null); + Model suzanneDrawable = new Model(suzanneMaterial, importer.mesh); + Node3d suzanneNode = new Node3d("suzanne", suzanneDrawable); + //suzanneNode.translate(vec3(3, 4, 5)); + _scene.addChild(suzanneNode); _minerMesh = new Mesh(VertexFormat(VertexElementType.POSITION, VertexElementType.NORMAL, VertexElementType.COLOR, VertexElementType.TEXCOORD0)); @@ -164,6 +170,10 @@ class UiWidget : VerticalLayout, CellVisitor { Node3d minerNode = new Node3d("miner", minerDrawable); _scene.addChild(minerNode); + + //minerNode.visible = false; + //cubeNode.visible = false; + //CellVisitor visitor = new TestVisitor(); //Log.d("Testing cell visitor"); //long ts = currentTimeMillis; diff --git a/src/dlangui/graphics/scene/effect.d b/src/dlangui/graphics/scene/effect.d index f018b9f2..816f1d3d 100644 --- a/src/dlangui/graphics/scene/effect.d +++ b/src/dlangui/graphics/scene/effect.d @@ -94,7 +94,7 @@ class Effect : GLProgram { vertexLocation = getAttribLocation("vertex"); colAttrLocation = getAttribLocation("colAttr"); texCoordLocation = getAttribLocation("texCoord"); - return matrixLocation >= 0 && vertexLocation >= 0 && colAttrLocation >= 0 && texCoordLocation >= 0; + return matrixLocation >= 0 && vertexLocation >= 0; // && colAttrLocation >= 0 && texCoordLocation >= 0 } /// get location for vertex attribute diff --git a/src/dlangui/graphics/scene/mesh.d b/src/dlangui/graphics/scene/mesh.d index 79b1d820..0c10fb27 100644 --- a/src/dlangui/graphics/scene/mesh.d +++ b/src/dlangui/graphics/scene/mesh.d @@ -101,19 +101,19 @@ struct VertexElement { struct VertexFormat { private VertexElement[] _elements; private byte[VertexElementType.max + 1] _elementOffset = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]; - private int _vertexSize; + private int _vertexSize; // vertex size in floats /// make using element list this(inout VertexElement[] elems...) { _elements = elems.dup; foreach(elem; elems) { - _elementOffset[elem.type] = cast(byte)(_vertexSize / float.sizeof); - _vertexSize += elem.size * float.sizeof; + _elementOffset[elem.type] = cast(byte)_vertexSize; + _vertexSize += elem.size; } } /// init from vertex element types, using default sizes for types this(inout VertexElementType[] types...) { foreach(t; types) { - _elementOffset[t] = cast(byte)(_vertexSize / float.sizeof); + _elementOffset[t] = cast(byte)_vertexSize; VertexElement elem = VertexElement(t); _elements ~= elem; _vertexSize += elem.size; @@ -190,6 +190,16 @@ struct VertexFormat { return false; return true; } + string dump(float * data) { + import std.conv : to; + char[] buf; + int pos = 0; + foreach(VertexElement e; _elements) { + buf ~= data[pos .. pos + e.size].to!string; + pos += e.size; + } + return buf.dup; + } } struct IndexFragment { @@ -239,6 +249,18 @@ class Mesh : RefCountedObject { } } + string dumpVertexes(int maxCount = 30) { + char[] buf; + int count = 0; + for(int i = 0; i < _vertexData.length; i+= _vertexFormat.vertexFloats) { + buf ~= "\n"; + buf ~= _vertexFormat.dump(_vertexData.ptr + i); + if (++count >= maxCount) + break; + } + return buf.dup; + } + /// returns vertex count @property int vertexCount() const { return _vertexCount; } diff --git a/src/dlangui/graphics/scene/node.d b/src/dlangui/graphics/scene/node.d index a81c0952..40646f06 100644 --- a/src/dlangui/graphics/scene/node.d +++ b/src/dlangui/graphics/scene/node.d @@ -12,6 +12,7 @@ class Node3d : Transform { protected Node3d _parent; protected Scene3d _scene; protected string _id; + protected bool _visible = true; protected DrawableObjectRef _drawable; protected mat4 _worldMatrix; @@ -29,6 +30,9 @@ class Node3d : Transform { _drawable = drawable; } + @property bool visible() { return _visible; } + @property Node3d visible(bool v) { _visible = v; return this; } + /// drawable attached to node @property ref DrawableObjectRef drawable() { return _drawable; } diff --git a/src/dlangui/graphics/scene/scene3d.d b/src/dlangui/graphics/scene/scene3d.d index 586c10b3..1a0a57c5 100644 --- a/src/dlangui/graphics/scene/scene3d.d +++ b/src/dlangui/graphics/scene/scene3d.d @@ -67,6 +67,8 @@ class Scene3d : Node3d { /// depth-first recursive node traversion, stops if visitor returns true bool visit(Node3d node, bool delegate(Node3d node) visitor) { + if (!node.visible) + return false; bool res = visitor(node); if (res) return true;