goat3d

changeset 76:9785847d52d4

bounding boxes calculation (untested) and automatic camera placement in goatview
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 08 May 2014 13:43:45 +0300
parents 76dea247f75c
children 4b653386a154
files goat3d.vcxproj goat3d.vcxproj.filters goatview/goatview.vcxproj goatview/src/goatview.cc goatview/src/goatview.h goatview/src/main.cc src/goat3d.cc src/goat3d.h src/mesh.cc src/mesh.h src/node.cc
diffstat 11 files changed, 72 insertions(+), 14 deletions(-) [+]
line diff
     1.1 --- a/goat3d.vcxproj	Thu May 08 00:50:16 2014 +0300
     1.2 +++ b/goat3d.vcxproj	Thu May 08 13:43:45 2014 +0300
     1.3 @@ -31,6 +31,7 @@
     1.4      <ClInclude Include="libs\openctm\openctm.h" />
     1.5      <ClInclude Include="libs\openctm\openctmpp.h" />
     1.6      <ClInclude Include="libs\tinyxml2\tinyxml2.h" />
     1.7 +    <ClInclude Include="src\aabox.h" />
     1.8      <ClInclude Include="src\camera.h" />
     1.9      <ClInclude Include="src\chunk.h" />
    1.10      <ClInclude Include="src\goat3d.h" />
    1.11 @@ -55,6 +56,7 @@
    1.12      <ClCompile Include="libs\openctm\openctm.c" />
    1.13      <ClCompile Include="libs\openctm\stream.c" />
    1.14      <ClCompile Include="libs\tinyxml2\tinyxml2.cpp" />
    1.15 +    <ClCompile Include="src\aabox.cc" />
    1.16      <ClCompile Include="src\camera.cc" />
    1.17      <ClCompile Include="src\chunk.cc" />
    1.18      <ClCompile Include="src\goat3d.cc" />
     2.1 --- a/goat3d.vcxproj.filters	Thu May 08 00:50:16 2014 +0300
     2.2 +++ b/goat3d.vcxproj.filters	Thu May 08 13:43:45 2014 +0300
     2.3 @@ -3,7 +3,7 @@
     2.4    <ItemGroup>
     2.5      <Filter Include="src">
     2.6        <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
     2.7 -      <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
     2.8 +      <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx;h</Extensions>
     2.9      </Filter>
    2.10      <Filter Include="libs">
    2.11        <UniqueIdentifier>{bbf568eb-077e-4cb6-8607-e6a016ab7360}</UniqueIdentifier>
    2.12 @@ -88,6 +88,9 @@
    2.13      <ClInclude Include="libs\openctm\liblzma\Types.h">
    2.14        <Filter>libs\openctm\liblzma</Filter>
    2.15      </ClInclude>
    2.16 +    <ClInclude Include="src\aabox.h">
    2.17 +      <Filter>src</Filter>
    2.18 +    </ClInclude>
    2.19    </ItemGroup>
    2.20    <ItemGroup>
    2.21      <ClCompile Include="src\goat3d.cc">
    2.22 @@ -165,5 +168,8 @@
    2.23      <ClCompile Include="src\goat3d_read.cc">
    2.24        <Filter>src</Filter>
    2.25      </ClCompile>
    2.26 +    <ClCompile Include="src\aabox.cc">
    2.27 +      <Filter>src</Filter>
    2.28 +    </ClCompile>
    2.29    </ItemGroup>
    2.30  </Project>
    2.31 \ No newline at end of file
     3.1 --- a/goatview/goatview.vcxproj	Thu May 08 00:50:16 2014 +0300
     3.2 +++ b/goatview/goatview.vcxproj	Thu May 08 13:43:45 2014 +0300
     3.3 @@ -98,11 +98,12 @@
     3.4        <WarningLevel>Level3</WarningLevel>
     3.5        <Optimization>Disabled</Optimization>
     3.6        <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
     3.7 +      <AdditionalIncludeDirectories>$(SolutionDir)\src</AdditionalIncludeDirectories>
     3.8      </ClCompile>
     3.9      <Link>
    3.10        <SubSystem>Console</SubSystem>
    3.11        <GenerateDebugInformation>true</GenerateDebugInformation>
    3.12 -      <AdditionalDependencies>goat3d-x64.lib;qtmaind.lib;Qt5Cored.lib;Qt5Widgetsd.lib;Qt5OpenGLd.lib;opengl32.lib;libvmath-x64-dbg.lib;libanim-x64-dbg.lib;pthreadVC2_x64.lib;%(AdditionalDependencies)</AdditionalDependencies>
    3.13 +      <AdditionalDependencies>goat3d-x64.lib;qtmaind.lib;Qt5Cored.lib;Qt5Widgetsd.lib;Qt5OpenGLd.lib;opengl32.lib;glu32.lib;libvmath-x64-dbg.lib;libanim-x64-dbg.lib;pthreadVC2_x64.lib;%(AdditionalDependencies)</AdditionalDependencies>
    3.14        <AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
    3.15      </Link>
    3.16      <CustomBuildStep>
    3.17 @@ -148,13 +149,14 @@
    3.18        <FunctionLevelLinking>true</FunctionLevelLinking>
    3.19        <IntrinsicFunctions>true</IntrinsicFunctions>
    3.20        <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    3.21 +      <AdditionalIncludeDirectories>$(SolutionDir)\src</AdditionalIncludeDirectories>
    3.22      </ClCompile>
    3.23      <Link>
    3.24        <SubSystem>Console</SubSystem>
    3.25        <GenerateDebugInformation>true</GenerateDebugInformation>
    3.26        <EnableCOMDATFolding>true</EnableCOMDATFolding>
    3.27        <OptimizeReferences>true</OptimizeReferences>
    3.28 -      <AdditionalDependencies>goat3d-x64.lib;qtmain.lib;Qt5Widgets.lib;Qt5OpenGL.lib;opengl32.lib;libvmath-x64.lib;libanim-x64.lib;pthreadVC2_x64.lib;%(AdditionalDependencies)</AdditionalDependencies>
    3.29 +      <AdditionalDependencies>goat3d-x64.lib;qtmain.lib;Qt5Widgets.lib;Qt5OpenGL.lib;opengl32.lib;glu32.lib;libvmath-x64.lib;libanim-x64.lib;pthreadVC2_x64.lib;%(AdditionalDependencies)</AdditionalDependencies>
    3.30        <AdditionalLibraryDirectories>$(OutDir)</AdditionalLibraryDirectories>
    3.31      </Link>
    3.32    </ItemDefinitionGroup>
     4.1 --- a/goatview/src/goatview.cc	Thu May 08 00:50:16 2014 +0300
     4.2 +++ b/goatview/src/goatview.cc	Thu May 08 13:43:45 2014 +0300
     4.3 @@ -1,4 +1,6 @@
     4.4 +#include <QtOpenGL/QtOpenGL>
     4.5  #include <GL/glu.h>
     4.6 +#include <vmath/vmath.h>
     4.7  #include "goatview.h"
     4.8  #include "goat3d.h"
     4.9  
    4.10 @@ -7,7 +9,25 @@
    4.11  
    4.12  static long anim_time;
    4.13  static float cam_theta, cam_phi, cam_dist = 8;
    4.14 +static float fov = 60.0;
    4.15  
    4.16 +bool load_scene(const char *fname)
    4.17 +{
    4.18 +	if(scene) {
    4.19 +		goat3d_free(scene);
    4.20 +	}
    4.21 +	if(!(scene = goat3d_create()) || goat3d_load(scene, fname) == -1) {
    4.22 +		return false;
    4.23 +	}
    4.24 +
    4.25 +	float bmin[3], bmax[3];
    4.26 +	goat3d_get_bounds(scene, bmin, bmax);
    4.27 +	float bsize = (Vector3(bmax[0], bmax[1], bmax[2]) - Vector3(bmin[0], bmin[1], bmin[2])).length();
    4.28 +	cam_dist = bsize / tan(DEG_TO_RAD(fov) / 2.0) + bsize;
    4.29 +
    4.30 +	printf("bounds size: %f, cam_dist: %f\n", bsize, cam_dist);
    4.31 +	return true;
    4.32 +}
    4.33  
    4.34  GoatView::GoatView()
    4.35  {
    4.36 @@ -101,12 +121,8 @@
    4.37  		return;
    4.38  	}
    4.39  
    4.40 -	if(scene) {
    4.41 -		goat3d_free(scene);
    4.42 -	}
    4.43 -
    4.44  	statusBar()->showMessage("opening scene file");
    4.45 -	if(!(scene = goat3d_create()) || goat3d_load(scene, fname.c_str()) == -1) {
    4.46 +	if(!load_scene(fname.c_str())) {
    4.47  		statusBar()->showMessage("failed to load scene file");
    4.48  	}
    4.49  }
    4.50 @@ -207,6 +223,4 @@
    4.51  	for(int i=0; i<num_child; i++) {
    4.52  		draw_node(goat3d_get_node_child(node, i));
    4.53  	}
    4.54 -}
    4.55 -
    4.56 -
    4.57 +}
    4.58 \ No newline at end of file
     5.1 --- a/goatview/src/goatview.h	Thu May 08 00:50:16 2014 +0300
     5.2 +++ b/goatview/src/goatview.h	Thu May 08 13:43:45 2014 +0300
     5.3 @@ -9,6 +9,8 @@
     5.4  extern goat3d *scene;
     5.5  extern QSettings *settings;
     5.6  
     5.7 +bool load_scene(const char *fname);
     5.8 +
     5.9  class GoatView : public QMainWindow {
    5.10  	Q_OBJECT
    5.11  private:
     6.1 --- a/goatview/src/main.cc	Thu May 08 00:50:16 2014 +0300
     6.2 +++ b/goatview/src/main.cc	Thu May 08 13:43:45 2014 +0300
     6.3 @@ -25,7 +25,7 @@
     6.4  		}
     6.5  		std::string fname = args.at(0).toStdString();
     6.6  		printf("loading scene file: %s ...\n", fname.c_str());
     6.7 -		if(!(scene = goat3d_create()) || goat3d_load(scene, fname.c_str())) {
     6.8 +		if(!load_scene(fname.c_str())) {
     6.9  			fprintf(stderr, "failed to load scene: %s\n", fname.c_str());
    6.10  			return 1;
    6.11  		}
     7.1 --- a/src/goat3d.cc	Thu May 08 00:50:16 2014 +0300
     7.2 +++ b/src/goat3d.cc	Thu May 08 13:43:45 2014 +0300
     7.3 @@ -253,6 +253,15 @@
     7.4  	return &g->scn->get_ambient().x;
     7.5  }
     7.6  
     7.7 +GOAT3DAPI void goat3d_get_bounds(const struct goat3d *g, float *bmin, float *bmax)
     7.8 +{
     7.9 +	AABox bbox = g->scn->get_bounds();
    7.10 +	for(int i=0; i<3; i++) {
    7.11 +		bmin[i] = bbox.bmin[i];
    7.12 +		bmax[i] = bbox.bmax[i];
    7.13 +	}
    7.14 +}
    7.15 +
    7.16  // ---- materials ----
    7.17  GOAT3DAPI void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl)
    7.18  {
     8.1 --- a/src/goat3d.h	Thu May 08 00:50:16 2014 +0300
     8.2 +++ b/src/goat3d.h	Thu May 08 13:43:45 2014 +0300
     8.3 @@ -123,6 +123,8 @@
     8.4  GOAT3DAPI void goat3d_set_ambient3f(struct goat3d *g, float ar, float ag, float ab);
     8.5  GOAT3DAPI const float *goat3d_get_ambient(const struct goat3d *g);
     8.6  
     8.7 +GOAT3DAPI void goat3d_get_bounds(const struct goat3d *g, float *bmin, float *bmax);
     8.8 +
     8.9  /* materials */
    8.10  GOAT3DAPI void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl);
    8.11  GOAT3DAPI int goat3d_get_mtl_count(struct goat3d *g);
     9.1 --- a/src/mesh.cc	Thu May 08 00:50:16 2014 +0300
     9.2 +++ b/src/mesh.cc	Thu May 08 13:43:45 2014 +0300
     9.3 @@ -228,3 +228,22 @@
     9.4  {
     9.5  	return material;
     9.6  }
     9.7 +
     9.8 +AABox Mesh::get_bounds(const Matrix4x4 &xform) const
     9.9 +{
    9.10 +	AABox bbox;
    9.11 +
    9.12 +	for(size_t i=0; i<vertices.size(); i++) {
    9.13 +		Vector3 v = vertices[i].transformed(xform);
    9.14 +
    9.15 +		for(int j=0; j<3; j++) {
    9.16 +			if(v[j] < bbox.bmin[j]) {
    9.17 +				bbox.bmin[j] = v[j];
    9.18 +			}
    9.19 +			if(v[j] > bbox.bmax[j]) {
    9.20 +				bbox.bmax[j] = v[j];
    9.21 +			}
    9.22 +		}
    9.23 +	}
    9.24 +	return bbox;
    9.25 +}
    9.26 \ No newline at end of file
    10.1 --- a/src/mesh.h	Thu May 08 00:50:16 2014 +0300
    10.2 +++ b/src/mesh.h	Thu May 08 13:43:45 2014 +0300
    10.3 @@ -61,7 +61,7 @@
    10.4  	Material *get_material();
    10.5  	const Material *get_material() const;
    10.6  
    10.7 -	AABox get_bounds() const;
    10.8 +	AABox get_bounds(const Matrix4x4 &xform) const;
    10.9  };
   10.10  
   10.11  }	// namespace g3dimpl
    11.1 --- a/src/node.cc	Thu May 08 00:50:16 2014 +0300
    11.2 +++ b/src/node.cc	Thu May 08 13:43:45 2014 +0300
    11.3 @@ -47,7 +47,9 @@
    11.4  const AABox &Node::get_bounds() const
    11.5  {
    11.6  	if(!bbox_valid) {
    11.7 -		bbox = obj ? obj->get_bounds() : AABox();
    11.8 +		Matrix4x4 xform;
    11.9 +		get_xform(0, &xform);
   11.10 +		bbox = obj ? obj->get_bounds(xform) : AABox();
   11.11  
   11.12  		for(int i=0; i<get_children_count(); i++) {
   11.13  			bbox = aabox_union(bbox, ((Node*)get_child(i))->get_bounds());