From 3ca57d56ee3d3021e4a7885f27a905a2344b30e5 Mon Sep 17 00:00:00 2001 From: Kearwood Kip Gilbert Date: Sat, 29 Jul 2017 00:26:35 -0700 Subject: [PATCH] WIP organization of library --- kraken/KRFloat.h | 16 ---------- kraken/KRHelpers.cpp | 50 +++++++++++++++++++++++++++++++ kraken/KRHelpers.h | 29 ++++++++++++++++++ kraken/KRVector4.cpp | 8 +---- kraken/KRViewport.cpp | 6 ++-- kraken/KRViewport.h | 3 -- kraken/{ => public}/KRAABB.h | 4 ++- kraken/public/KRFloat.h | 39 ++++++++++++++++++++++++ kraken/{ => public}/KRMat4.h | 29 ++++-------------- kraken/{ => public}/KRTriangle3.h | 10 +++---- kraken/{ => public}/KRVector2.h | 12 ++------ kraken/{ => public}/KRVector3.h | 21 +++++-------- kraken/{ => public}/KRVector4.h | 15 ++++------ kraken/public/kraken.h | 12 ++++++++ 14 files changed, 162 insertions(+), 92 deletions(-) delete mode 100755 kraken/KRFloat.h create mode 100644 kraken/KRHelpers.cpp create mode 100644 kraken/KRHelpers.h rename kraken/{ => public}/KRAABB.h (96%) mode change 100755 => 100644 create mode 100644 kraken/public/KRFloat.h rename kraken/{ => public}/KRMat4.h (89%) mode change 100755 => 100644 rename kraken/{ => public}/KRTriangle3.h (95%) mode change 100755 => 100644 rename kraken/{ => public}/KRVector2.h (95%) mode change 100755 => 100644 rename kraken/{ => public}/KRVector3.h (90%) mode change 100755 => 100644 rename kraken/{ => public}/KRVector4.h (94%) mode change 100755 => 100644 create mode 100644 kraken/public/kraken.h diff --git a/kraken/KRFloat.h b/kraken/KRFloat.h deleted file mode 100755 index 91085e0..0000000 --- a/kraken/KRFloat.h +++ /dev/null @@ -1,16 +0,0 @@ -// -// KRFloat.h -// Kraken -// -// Created by Kearwood Gilbert on 2013-05-03. -// Copyright (c) 2013 Kearwood Software. All rights reserved. -// - -#ifndef KRFLOAT_H -#define KRFLOAT_H - -namespace KRFloat { - float SmoothStep(float a, float b, float t); -}; - -#endif /* defined(KRFLOAT_H) */ diff --git a/kraken/KRHelpers.cpp b/kraken/KRHelpers.cpp new file mode 100644 index 0000000..aa519fc --- /dev/null +++ b/kraken/KRHelpers.cpp @@ -0,0 +1,50 @@ +#include "KREngine-common.h" + +#include "KRHelpers.h" + +namespace kraken { + +void SetUniform(GLint location, const KRVector2 &v) +{ + if (location != -1) GLDEBUG(glUniform2f(location, v.x, v.y)); +} + +void SetUniform(GLint location, const KRVector3 &v) +{ + if (location != -1) GLDEBUG(glUniform3f(location, v.x, v.y, v.z)); +} + +void SetUniform(GLint location, const KRVector4 &v) +{ + if (location != -1) GLDEBUG(glUniform4f(location, v.x, v.y, v.z, v.w)); +} + +void SetUniform(GLint location, const KRMat4 &v) +{ + if (location != -1) GLDEBUG(glUniformMatrix4fv(location, 1, GL_FALSE, v.c)); +} + +void setXMLAttribute(const std::string &base_name, tinyxml2::XMLElement *e, const KRVector3 &value, const KRVector3 &default_value) +{ + // TODO - Increase number of digits after the decimal in floating point format (6 -> 12?) + // FINDME, TODO - This needs optimization... + if (value != default_value) { + e->SetAttribute((base_name + "_x").c_str(), value.x); + e->SetAttribute((base_name + "_y").c_str(), value.y); + e->SetAttribute((base_name + "_z").c_str(), value.z); + } +} + +const KRVector3 getXMLAttribute(const std::string &base_name, tinyxml2::XMLElement *e, const KRVector3 &default_value) +{ + KRVector3 value; + if (e->QueryFloatAttribute((base_name + "_x").c_str(), &value.x) == tinyxml2::XML_SUCCESS + && e->QueryFloatAttribute((base_name + "_y").c_str(), &value.y) == tinyxml2::XML_SUCCESS + && e->QueryFloatAttribute((base_name + "_z").c_str(), &value.z) == tinyxml2::XML_SUCCESS) { + return value; + } else { + return default_value; + } +} + +} // namespace kraken diff --git a/kraken/KRHelpers.h b/kraken/KRHelpers.h new file mode 100644 index 0000000..3dd9525 --- /dev/null +++ b/kraken/KRHelpers.h @@ -0,0 +1,29 @@ +#ifndef KRHELPERS_H +#define KRHELPERS_H + +#if defined(_WIN32) || defined(_WIN64) +#include +#include "../3rdparty/tinyxml2/tinyxml2.h" +#endif + +#include "KREngine-common.h" + +#define KRMIN(x,y) ((x) < (y) ? (x) : (y)) +#define KRMAX(x,y) ((x) > (y) ? (x) : (y)) +#define KRCLAMP(x, min, max) (KRMAX(KRMIN(x, max), min)) +#define KRALIGN(x) ((x + 3) & ~0x03) + +float const PI = 3.141592653589793f; +float const D2R = PI * 2 / 360; + +namespace kraken { + void SetUniform(GLint location, const KRVector2 &v); + void SetUniform(GLint location, const KRVector3 &v); + void SetUniform(GLint location, const KRVector4 &v); + void SetUniform(GLint location, const KRMat4 &v); + + void setXMLAttribute(const std::string &base_name, ::tinyxml2::XMLElement *e, const KRVector3 &value, const KRVector3 &default_value); + const KRVector3 getXMLAttribute(const std::string &base_name, ::tinyxml2::XMLElement *e, const KRVector3 &default_value); +} // namespace kraken + +#endif \ No newline at end of file diff --git a/kraken/KRVector4.cpp b/kraken/KRVector4.cpp index 09a9c5c..a5d8b61 100755 --- a/kraken/KRVector4.cpp +++ b/kraken/KRVector4.cpp @@ -29,8 +29,7 @@ // or implied, of Kearwood Gilbert. // -#include "KREngine-common.h" -#include "KRVector4.h" +#include "public/kraken.h" const KRVector4 KRVECTOR4_ZERO(0.0f, 0.0f, 0.0f, 0.0f); @@ -298,8 +297,3 @@ bool KRVector4::operator <(const KRVector4& b) const if(w != b.w) return w < b.w; return false; } - -void KRVector4::setUniform(GLint location) const -{ - if(location != -1) GLDEBUG(glUniform4f(location, x, y, z, w)); -} diff --git a/kraken/KRViewport.cpp b/kraken/KRViewport.cpp index 4a98772..a20a5c6 100755 --- a/kraken/KRViewport.cpp +++ b/kraken/KRViewport.cpp @@ -8,11 +8,9 @@ #define KRENGINE_SWAP_INT(x,y) {int t;t=x;x=y;y=t;} -#include "KRVector2.h" -#include "KRMat4.h" -#include "KRViewport.h" -#include "KRLight.h" +#include "KREngine-common.h" +#include "KRViewport.h" KRViewport::KRViewport() { diff --git a/kraken/KRViewport.h b/kraken/KRViewport.h index df516a0..29d46f5 100755 --- a/kraken/KRViewport.h +++ b/kraken/KRViewport.h @@ -10,9 +10,6 @@ #define KRENGINE_KRVIEWPORT_H #include "KREngine-common.h" -#include "KRVector2.h" -#include "KRMat4.h" -#include "KRAABB.h" class KRLight; diff --git a/kraken/KRAABB.h b/kraken/public/KRAABB.h old mode 100755 new mode 100644 similarity index 96% rename from kraken/KRAABB.h rename to kraken/public/KRAABB.h index 6a03c07..1fa5bd2 --- a/kraken/KRAABB.h +++ b/kraken/public/KRAABB.h @@ -11,10 +11,12 @@ #ifndef KRAABB_H #define KRAABB_H +#include // for hash<> + +#include "KRVector2.h" #include "KRVector3.h" class KRMat4; -class KRVector2; class KRAABB { public: diff --git a/kraken/public/KRFloat.h b/kraken/public/KRFloat.h new file mode 100644 index 0000000..1915533 --- /dev/null +++ b/kraken/public/KRFloat.h @@ -0,0 +1,39 @@ +// +// KRFloat.h +// Kraken +// +// Copyright 2017 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#ifndef KRFLOAT_H +#define KRFLOAT_H + +namespace KRFloat { + float SmoothStep(float a, float b, float t); +}; + +#endif /* defined(KRFLOAT_H) */ diff --git a/kraken/KRMat4.h b/kraken/public/KRMat4.h old mode 100755 new mode 100644 similarity index 89% rename from kraken/KRMat4.h rename to kraken/public/KRMat4.h index 8e7340e..ab802ce --- a/kraken/KRMat4.h +++ b/kraken/public/KRMat4.h @@ -1,8 +1,8 @@ // // KRMat4.h -// KREngine +// Kraken // -// Copyright 2012 Kearwood Gilbert. All rights reserved. +// Copyright 2017 Kearwood Gilbert. All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, are // permitted provided that the following conditions are met: @@ -33,21 +33,8 @@ #include "KRVector3.h" #include "KRVector4.h" -#include "KREngine-common.h" - -#ifndef KRMAT4_I -#define KRMAT4_I - - -#define EMPTY_MATRIX4 { 0.0, 0.0, 0.0, 0.0,\ -0.0, 0.0, 0.0, 0.0,\ -0.0, 0.0, 0.0, 0.0,\ -0.0, 0.0, 0.0, 0.0 } - -#define IDENTITY_MATRIX4 { 1.0, 0.0, 0.0, 0.0,\ -0.0, 1.0, 0.0, 0.0,\ -0.0, 0.0, 1.0, 0.0,\ -0.0, 0.0, 0.0, 1.0 } +#ifndef KRMAT4_H +#define KRMAT4_H typedef enum { X_AXIS, @@ -58,14 +45,10 @@ typedef enum { class KRQuaternion; class KRMat4 { - - public: float c[16]; // Matrix components, in column-major order - - // Default constructor - Creates an identity matrix KRMat4(); @@ -123,8 +106,6 @@ class KRMat4 { static KRMat4 Translation(const KRVector3 &v); static KRMat4 Rotation(const KRVector3 &v); static KRMat4 Scaling(const KRVector3 &v); - - void setUniform(GLint location) const; }; -#endif // KRMAT4_I \ No newline at end of file +#endif // KRMAT4_H \ No newline at end of file diff --git a/kraken/KRTriangle3.h b/kraken/public/KRTriangle3.h old mode 100755 new mode 100644 similarity index 95% rename from kraken/KRTriangle3.h rename to kraken/public/KRTriangle3.h index 25ade3e..de34fb5 --- a/kraken/KRTriangle3.h +++ b/kraken/public/KRTriangle3.h @@ -37,6 +37,8 @@ class KRTriangle3 { public: + KRVector3 vert[3]; + KRTriangle3(const KRTriangle3 &tri); KRTriangle3(const KRVector3 &v1, const KRVector3 &v2, const KRVector3 &v3); ~KRTriangle3(); @@ -46,18 +48,14 @@ public: bool operator ==(const KRTriangle3& b) const; bool operator !=(const KRTriangle3& b) const; KRTriangle3& operator =(const KRTriangle3& b); - KRVector3& operator[](unsigned i); - KRVector3 operator[](unsigned i) const; - + KRVector3& operator[](unsigned int i); + KRVector3 operator[](unsigned int i) const; bool rayCast(const KRVector3 &start, const KRVector3 &dir, KRVector3 &hit_point) const; bool sphereCast(const KRVector3 &start, const KRVector3 &dir, float radius, KRVector3 &hit_point, float &hit_distance) const; bool containsPoint(const KRVector3 &p) const; KRVector3 closestPointOnTriangle(const KRVector3 &p) const; -private: - - KRVector3 m_c[3]; }; #endif // KRTRIANGLE3_H diff --git a/kraken/KRVector2.h b/kraken/public/KRVector2.h old mode 100755 new mode 100644 similarity index 95% rename from kraken/KRVector2.h rename to kraken/public/KRVector2.h index 397833e..43b0d95 --- a/kraken/KRVector2.h +++ b/kraken/public/KRVector2.h @@ -1,8 +1,8 @@ // // KRVector2.h -// KREngine +// Kraken // -// Copyright 2012 Kearwood Gilbert. All rights reserved. +// Copyright 2017 Kearwood Gilbert. All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, are // permitted provided that the following conditions are met: @@ -32,7 +32,7 @@ #ifndef KRVECTOR2 #define KRVECTOR2 -#include "KREngine-common.h" +#include // for hash<> class KRVector2 { @@ -93,12 +93,6 @@ public: static KRVector2 Max(); static KRVector2 Zero(); static KRVector2 One(); - - void setUniform(GLint location) const; - -private: - - }; namespace std { diff --git a/kraken/KRVector3.h b/kraken/public/KRVector3.h old mode 100755 new mode 100644 similarity index 90% rename from kraken/KRVector3.h rename to kraken/public/KRVector3.h index 7c171ea..25880c4 --- a/kraken/KRVector3.h +++ b/kraken/public/KRVector3.h @@ -1,8 +1,8 @@ // // KRVector3.h -// KREngine +// Kraken // -// Copyright 2012 Kearwood Gilbert. All rights reserved. +// Copyright 2017 Kearwood Gilbert. All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, are // permitted provided that the following conditions are met: @@ -29,10 +29,10 @@ // or implied, of Kearwood Gilbert. // -#ifndef KRVECTOR3 -#define KRVECTOR3 +#ifndef KRVECTOR3_H +#define KRVECTOR3_H -#include "KREngine-common.h" +#include // for hash<> class KRVector2; class KRVector4; @@ -48,13 +48,13 @@ public: }; KRVector3(); - KRVector3(float X, float Y, float Z); + KRVector3(float X, float Y, float Z); KRVector3(float v); KRVector3(float *v); KRVector3(double *v); KRVector3(const KRVector3 &v); KRVector3(const KRVector4 &v); - ~KRVector3(); + ~KRVector3(); // KRVector2 swizzle getters KRVector2 xx() const; @@ -123,11 +123,6 @@ public: static KRVector3 Lerp(const KRVector3 &v1, const KRVector3 &v2, float d); static KRVector3 Slerp(const KRVector3 &v1, const KRVector3 &v2, float d); static void OrthoNormalize(KRVector3 &normal, KRVector3 &tangent); // Gram-Schmidt Orthonormalization - - void setUniform(GLint location) const; - - void setXMLAttribute(const std::string &base_name, tinyxml2::XMLElement *e, const KRVector3 &default_value); - void getXMLAttribute(const std::string &base_name, tinyxml2::XMLElement *e, const KRVector3 &default_value); }; namespace std { @@ -144,4 +139,4 @@ namespace std { }; }; -#endif \ No newline at end of file +#endif // KRVECTOR3_H diff --git a/kraken/KRVector4.h b/kraken/public/KRVector4.h old mode 100755 new mode 100644 similarity index 94% rename from kraken/KRVector4.h rename to kraken/public/KRVector4.h index 05ce192..08f0dc4 --- a/kraken/KRVector4.h +++ b/kraken/public/KRVector4.h @@ -1,8 +1,8 @@ // // KRVector4.h -// KREngine +// Kraken // -// Copyright 2012 Kearwood Gilbert. All rights reserved. +// Copyright 2017 Kearwood Gilbert. All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, are // permitted provided that the following conditions are met: @@ -32,7 +32,7 @@ #ifndef KRVECTOR4_H #define KRVECTOR4_H -#include "KREngine-common.h" +#include // for hash<> class KRVector3; @@ -47,12 +47,12 @@ public: }; KRVector4(); - KRVector4(float X, float Y, float Z, float W); + KRVector4(float X, float Y, float Z, float W); KRVector4(float v); KRVector4(float *v); KRVector4(const KRVector4 &v); KRVector4(const KRVector3 &v, float W); - ~KRVector4(); + ~KRVector4(); KRVector4& operator =(const KRVector4& b); @@ -83,7 +83,6 @@ public: void normalize(); static KRVector4 Normalize(const KRVector4 &v); - static float Dot(const KRVector4 &v1, const KRVector4 &v2); static KRVector4 Min(); @@ -99,8 +98,6 @@ public: static KRVector4 Lerp(const KRVector4 &v1, const KRVector4 &v2, float d); static KRVector4 Slerp(const KRVector4 &v1, const KRVector4 &v2, float d); static void OrthoNormalize(KRVector4 &normal, KRVector4 &tangent); // Gram-Schmidt Orthonormalization - - void setUniform(GLint location) const; }; -#endif \ No newline at end of file +#endif // KRVECTOR4_H diff --git a/kraken/public/kraken.h b/kraken/public/kraken.h new file mode 100644 index 0000000..2561c84 --- /dev/null +++ b/kraken/public/kraken.h @@ -0,0 +1,12 @@ +#ifndef KRAKEN_H +#define KRAKEN_H + +#include "KRFloat.h" +#include "KRVector2.h" +#include "KRVector3.h" +#include "KRVector4.h" +#include "KRMat4.h" +#include "KRAABB.h" +#include "KRTriangle3.h" + +#endif // KRAKEN_H