From: chsieh Date: Mon, 18 Apr 2011 06:01:34 +0000 (-0700) Subject: Redoing everything. Nuke first. X-Git-Tag: box2d-testbed~25 X-Git-Url: http://git.less.ly:3516/?a=commitdiff_plain;h=f1c3686e0bce6354e1bab136b58e3fbfb0269767;p=tanks-ios.git Redoing everything. Nuke first. --- diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 90c1515..0000000 --- a/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -# ignore built files -build/* -Littlest.xcodeproj/*.pbxuser -Littlest.xcodeproj/*.mode1v3 diff --git a/Classes/Foundation/Common/Assert.h b/Classes/Foundation/Common/Assert.h deleted file mode 100755 index bf465fa..0000000 --- a/Classes/Foundation/Common/Assert.h +++ /dev/null @@ -1,69 +0,0 @@ -#pragma once - -#if defined(DEBUG) - -#include -#include -#include -#include -#include -#include - -#include "GlobalDefines.h" - -#if defined(__WINDOWS__) - - #define Assert(expression) if (!(expression)) __debugbreak(); - -#elif defined(__iOS__) - - // Returns true if the current process is being debugged (either - // running under the debugger or has a debugger attached post facto). - static bool AmIBeingDebugged(void) - { - int junk; - int mib[4]; - struct kinfo_proc info; - size_t size; - - // Initialize the flags so that, if sysctl fails for some bizarre - // reason, we get a predictable result. - info.kp_proc.p_flag = 0; - - // Initialize mib, which tells sysctl the info we want, in this case - // we're looking for information about a specific process ID. - mib[0] = CTL_KERN; - mib[1] = KERN_PROC; - mib[2] = KERN_PROC_PID; - mib[3] = getpid(); - - // Call sysctl. - size = sizeof(info); - junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0); - assert(junk == 0); - - // We're being debugged if the P_TRACED flag is set. - return ( (info.kp_proc.p_flag & P_TRACED) != 0 ); - } - - // http://iphone.m20.nl/wp/?p=1#more-1 - //#define DEBUGSTOP(signal) __asm__ __volatile__ ("mov r0, %0\nmov r1, %1\nmov r12, #37\nswi 128\n" : : "r" (getpid ()), "r" (signal) : "r12", "r0", "r1", "cc"); - //#define DEBUGGER do { int trapSignal = AmIBeingDebugged () ? SIGINT : SIGSTOP; DEBUGSTOP(trapSignal); if (trapSignal == SIGSTOP) { DEBUGSTOP (SIGINT); } } while (false); - - // Graciously copied from http://cocoawithlove.com/2008/03/break-into-debugger.html - #define DebugBreak() if (AmIBeingDebugged()) { DebugBreak(); } - - #define Assert(expression) if (!(expression)) raise(SIGTRAP); - -#endif - -#define AssertMessage(expression, message, ...) \ - if (!(expression)) Printf(message, ##__VA_ARGS__) - -#else - -// Note that the expression is still evaluated, since we declared this as an inlined function, rather than a macro. -slInline void Assert(bool expression) {} -slInline void AssertMsg(bool expression, char* msg, ...) {} - -#endif diff --git a/Classes/Foundation/Common/Base.h b/Classes/Foundation/Common/Base.h deleted file mode 100755 index 4cf66ae..0000000 --- a/Classes/Foundation/Common/Base.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -namespace Foundation -{ - class CBase - { - public: - CBase() {} - ~CBase() {} - }; -} diff --git a/Classes/Foundation/Common/GlobalDefines.h b/Classes/Foundation/Common/GlobalDefines.h deleted file mode 100755 index 14712b2..0000000 --- a/Classes/Foundation/Common/GlobalDefines.h +++ /dev/null @@ -1,152 +0,0 @@ -#pragma once - -#include "Foundation/Common/GlobalTypes.h" - -//---------------------------------------------------------------------------------------- -// OS and CPU architecture defines -#if defined(_WINDOWS) - #define __WINDOWS__ - #define __X86__ -#elif defined(__iOS__) - #include - #if TARGET_IPHONE_SIMULATOR - #define __X86__ - #elif TARGET_OS_IPHONE - #define __ARM__ - #else - #error undefined architecture! - #endif -#endif - -//---------------------------------------------------------------------------------------- -// configuration defines -#if defined(_DEBUG) && !defined(DEBUG) - #define DEBUG -#endif - -#if defined(_NDEBUG) && !defined(RELEASE) - #define RELEASE -#endif - -//---------------------------------------------------------------------------------------- -// some type defines that may or may not be defined already? -#if !defined(NULL) - #define NULL 0 -#endif - -#if !defined(FALSE) - #define FALSE 0 -#endif - -#if !defined(TRUE) - #define TRUE 1 -#endif - -//---------------------------------------------------------------------------------------- -// cache information -#if !defined(CACHE_LINE_SIZE) - #if defined(__X86__) - #define CACHE_LINE_SIZE 128 // normally equals to 2 lines for intel - #elif defined(__ARM__) - #define CACHE_LINE_SIZE 32 - #else - #define CACHE_LINE_SIZE 32 - #endif -#endif - -//---------------------------------------------------------------------------------------- -// force inline on non-debug, might make code explode -#if defined(DEBUG) - #define slInline inline -#else - #define slInline __forceinline -#endif - -// compiler determined inline -#define clInline inline - -#define slRestrict __restrict - -//---------------------------------------------------------------------------------------- -// alignment macros -#if defined(__WINDOWS__) -#define MEM_ALIGN(N) __declspec(align(N)) -#elif defined(__iOS__) -#define MEM_ALIGN(N) __attribute__((aligned (N))) -#endif - -#define IS_POWER_OF_TWO(x) ( ((x) & -(x)) == (x) ) - -//#pragma warning(disable:4146) // =( -#define ALIGN_UP(x, ALIGNMENT) ( ((x) + (ALIGNMENT) - 1) & -(ALIGNMENT) ) -#define ALIGN_DOWN(x, ALIGNMENT) ( (x) & -(ALIGNMENT) ) - -#define POWER_OF_TWO_MOD(x, N) ( (x) & ((N) - 1) ) - -#define ALIGN_CACHE ( ALIGN( CACHE_LINE_SIZE ) ) - -//---------------------------------------------------------------------------------------- -// cache macros -#if defined(__X86__) -// uhh, nothing? -#elif defined(__ARM__) -#define DCBT(x) __asm__("pld %[input]" : : [input] "r" ((x))) -#else -#error Not implemented yet! -#endif - -//---------------------------------------------------------------------------------------- -// string and memory functions -#if defined(__WINDOWS__) - #pragma intrinsic(memcpy) - #pragma intrinsic(memset) - #pragma intrinsic(strcmp) - #pragma intrinsic(strcpy) - #pragma intrinsic(strlen) - #pragma intrinsic(strcat) -#endif - -//---------------------------------------------------------------------------------------- -// color constants...this probably needs to consider endianness? -#define NULL_COLOR 0x00000000 -#define BLACK_COLOR 0x000000ff -#define RED_COLOR 0xff0000ff -#define GREEN_COLOR 0x00ff00ff -#define BLUE_COLOR 0x0000ffff -#define WHITE_COLOR 0xffffffff - -//---------------------------------------------------------------------------------------- -// some intrinsic/asm/fast stuff stuffs -- maybe this should be moved to another file -#define RightMostEnabledBit(x) ((x) & -(x)) - - -#if defined(__ARM__) - -slInline uint8_t LZCount(uint64_t x) -{ - uint8_t result; - __asm__("clz %[result] %[input]" : [result] "=r" (result) : [input] "r" (x)); - return result; -} - -#else - -slInline uint8_t LZCount(uint64_t x) -{ - uint8_t leading_zero_count = 0; - uint8_t next_shift = 32; - uint64_t copy = x; - while (next_shift != 0) - { - bool non_zero = copy >= (0x1ULL << next_shift); - uint8_t actual_shift = (uint8_t)non_zero * next_shift; - leading_zero_count += actual_shift; - copy >>= actual_shift; - next_shift >>= 1; - } - leading_zero_count += (copy == 0x1ULL); - - return leading_zero_count; -} - -#endif diff --git a/Classes/Foundation/Common/GlobalInclude.h b/Classes/Foundation/Common/GlobalInclude.h deleted file mode 100755 index 83fe092..0000000 --- a/Classes/Foundation/Common/GlobalInclude.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include -#include "GlobalDefines.h" -#include "GlobalTypes.h" -#include "Assert.h" -#include "Print.h" diff --git a/Classes/Foundation/Common/GlobalTypes.h b/Classes/Foundation/Common/GlobalTypes.h deleted file mode 100755 index a2d92f0..0000000 --- a/Classes/Foundation/Common/GlobalTypes.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#if defined(uint32_t) -#undef uint32_t -#endif - -typedef signed char int8_t; -typedef unsigned char uint8_t; -typedef short int16_t; -typedef unsigned short uint16_t; -typedef int int32_t; -typedef unsigned int uint32_t; -typedef long long int64_t; -typedef unsigned long long uint64_t; -typedef uint32_t color32_t; - -union IntFloat -{ - int8_t m_Int8[4]; - uint8_t m_UInt8[4]; - int16_t m_Int16[2]; - uint16_t m_UInt16[2]; - int32_t m_Int32; - uint32_t m_UInt32; - float m_Float; -}; - -union LongDouble -{ - int8_t m_Int8[8]; - uint8_t m_UInt8[8]; - int16_t m_Int16[4]; - uint16_t m_UInt16[4]; - int32_t m_Int32[2]; - uint32_t m_UInt32[2]; - int64_t m_Int64; - uint64_t m_UInt64; - float m_Float[2]; - double m_Double; -}; diff --git a/Classes/Foundation/Common/Print.h b/Classes/Foundation/Common/Print.h deleted file mode 100755 index 48c0609..0000000 --- a/Classes/Foundation/Common/Print.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include "GlobalDefines.h" -#include - -#if defined(DEBUG) - #define Printf(message, ...) printf(message, ##__VA_ARGS__) -#else - #define Printf(message, ...) {} -#endif diff --git a/Classes/Foundation/Common/Singleton.h b/Classes/Foundation/Common/Singleton.h deleted file mode 100755 index bc1ae03..0000000 --- a/Classes/Foundation/Common/Singleton.h +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once - -#include "Foundation/Common/GlobalInclude.h" - -//---------------------------------------------------------------------------------------- -namespace Foundation -{ - -template -class Singleton -{ -private: - static T* m_Instance; - -protected: - Singleton() { m_Instance = NULL; } - -public: - static T* Init() - { - Assert( m_Instance == NULL); - m_Instance = new (16) T(); - return m_Instance; - } - - slInline static T* GetInstance() - { - return m_Instance; - } - - static void DestroyInstance() - { - if ( m_Instance ) - { - delete m_Instance; - } - } -}; - -template T* Singleton::m_Instance = NULL; - -} diff --git a/Classes/Foundation/Containers/BitEncoder.h b/Classes/Foundation/Containers/BitEncoder.h deleted file mode 100755 index 102e7fc..0000000 --- a/Classes/Foundation/Containers/BitEncoder.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include "Foundation/Common/GlobalInclude.h" - -#define SET_BITS(x, new_val, shift, mask) ((x) = (((x) & ~(mask)) | (((new_val) << (shift)) & (mask)))) -#define ZERO_BITS(x, mask) ((x) &= ~(mask)) -#define OR_BITS (x, new_val, shift, mask) ((x) |= (((new_val) << (shift)) & (mask))) -#define AND_BITS(x, new_val, shift, mask) ((x) &= (((new_val) << (shift)) & (mask))) -#define GET_BITS(x, mask) ((x) & (mask)) -#define GET_BITS_RIGHT(x, shift, mask) (((x) & (mask)) >> (shift)) diff --git a/Classes/Foundation/Containers/LocklessRingBuffer.cpp b/Classes/Foundation/Containers/LocklessRingBuffer.cpp deleted file mode 100755 index 0a9f9d6..0000000 --- a/Classes/Foundation/Containers/LocklessRingBuffer.cpp +++ /dev/null @@ -1,159 +0,0 @@ -#include "LocklessRingBuffer.h" -#include "Foundation/Synchronization/MemorySync.h" - -//---------------------------------------------------------------------------------------- -void LocklessRingBuffer::Init(void* buffer, uint32_t buffer_size) -{ - m_Base = (uint8_t*)buffer; - m_BufferSize = buffer_size; - m_WriteOffset = 0; - m_ReadOffset = 0; -} - -//---------------------------------------------------------------------------------------- -void LocklessRingBuffer::Destroy() -{ - m_Base = NULL; - m_BufferSize = 0; - m_WriteOffset = 0; - m_ReadOffset = 0; -} - -//---------------------------------------------------------------------------------------- -bool LocklessRingBuffer::Write(void* entry, uint32_t entry_size) -{ - Assert(entry_size < m_BufferSize); - - uint32_t new_write_offset = m_WriteOffset; - uint32_t remaining_space = m_WriteOffset < m_ReadOffset ? m_ReadOffset - m_WriteOffset - 1 : m_ReadOffset + m_BufferSize - m_WriteOffset; - if (remaining_space < entry_size) - { - return false; - } - - bool write_will_end_lower = new_write_offset + entry_size >= m_BufferSize; - - uint32_t distance_to_top = (entry_size > m_BufferSize - new_write_offset) ? (m_BufferSize - new_write_offset) : entry_size; - memcpy(m_Base + new_write_offset, entry, distance_to_top); - new_write_offset += distance_to_top; - - if (write_will_end_lower) - { - uint32_t remainder = entry_size - distance_to_top; - memcpy(m_Base, (uint8_t*)entry + distance_to_top, remainder); - new_write_offset = remainder; - } - - ReadWriteSync(); - - m_WriteOffset = new_write_offset; - return true; -} - -//---------------------------------------------------------------------------------------- -bool LocklessRingBuffer::Write(void* entries[], uint32_t entry_sizes[], uint32_t entry_count) -{ - uint32_t total_size = 0; - for (uint32_t i = 0; i < entry_count; i++) - { - total_size += entry_sizes[i]; - } - - Assert(total_size < m_BufferSize); - - uint32_t new_write_offset = m_WriteOffset; - uint32_t remaining_space = m_WriteOffset < m_ReadOffset ? m_ReadOffset - m_WriteOffset - 1 : m_ReadOffset + m_BufferSize - m_WriteOffset; - if (remaining_space < total_size) - { - return false; - } - - for (uint32_t i = 0; i < entry_count; i++) - { - bool write_will_end_lower = new_write_offset + entry_sizes[i] >= m_BufferSize; - uint32_t distance_to_top = (entry_sizes[i] > m_BufferSize - new_write_offset) ? (m_BufferSize - new_write_offset) : entry_sizes[i]; - memcpy(m_Base + new_write_offset, entries[i], distance_to_top); - new_write_offset += distance_to_top; - - if (write_will_end_lower) - { - uint32_t remainder = entry_sizes[i] - distance_to_top; - memcpy(m_Base, (uint8_t*)entries[i] + distance_to_top, remainder); - new_write_offset = remainder; - } - } - - ReadWriteSync(); - - m_WriteOffset = new_write_offset; - return true; -} - -//---------------------------------------------------------------------------------------- -bool LocklessRingBuffer::Peek(void* read_buffer, uint32_t read_size) -{ - uint32_t read_remaining = m_WriteOffset < m_ReadOffset ? m_BufferSize - m_ReadOffset + m_WriteOffset : m_WriteOffset - m_ReadOffset; - if (read_remaining < read_size) - { - return false; - } - - uint32_t read_to_top = m_BufferSize - m_ReadOffset <= read_size ? m_BufferSize - m_ReadOffset : read_size; - memcpy(read_buffer, m_Base + m_ReadOffset, read_to_top); - if (m_BufferSize - m_ReadOffset <= read_size) - { - memcpy((uint8_t*)read_buffer + read_to_top, m_Base, read_remaining - read_to_top); - } - - return true; -} - -//---------------------------------------------------------------------------------------- -bool LocklessRingBuffer::Read(void* read_buffer, uint32_t read_size) -{ - uint32_t read_remaining = m_WriteOffset < m_ReadOffset ? m_BufferSize - m_ReadOffset + m_WriteOffset : m_WriteOffset - m_ReadOffset; - if (read_remaining < read_size) - { - return false; - } - - uint32_t read_to_top = m_BufferSize - m_ReadOffset <= read_size ? m_BufferSize - m_ReadOffset : read_size; - memcpy(read_buffer, m_Base + m_ReadOffset, read_to_top); - uint32_t new_read_offset = m_ReadOffset + read_to_top; - - if (m_BufferSize - m_ReadOffset <= read_size) - { - memcpy((uint8_t*)read_buffer + read_to_top, m_Base, read_remaining - read_to_top); - new_read_offset = read_remaining - read_to_top; - } - - ReadWriteSync(); - - m_ReadOffset = new_read_offset; - - return true; -} - -//---------------------------------------------------------------------------------------- -bool LocklessRingBuffer::ReadDiscard(uint32_t read_size) -{ - uint32_t read_remaining = m_WriteOffset < m_ReadOffset ? m_BufferSize - m_ReadOffset + m_WriteOffset : m_WriteOffset - m_ReadOffset; - if (read_remaining < read_size) - { - return false; - } - - uint32_t read_to_top = m_BufferSize - m_ReadOffset <= read_size ? m_BufferSize - m_ReadOffset : read_size; - uint32_t new_read_offset = m_ReadOffset + read_to_top; - - if (m_BufferSize - m_ReadOffset <= read_size) - { - new_read_offset = read_remaining - read_to_top; - } - - ReadWriteSync(); - - m_ReadOffset = new_read_offset; - - return true; -} diff --git a/Classes/Foundation/Containers/LocklessRingBuffer.h b/Classes/Foundation/Containers/LocklessRingBuffer.h deleted file mode 100755 index e098a36..0000000 --- a/Classes/Foundation/Containers/LocklessRingBuffer.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include "Foundation/Common/GlobalInclude.h" - -class LocklessRingBuffer -{ -private: - uint8_t* m_Base; - uint32_t m_BufferSize; - uint32_t m_WriteOffset; - uint32_t m_ReadOffset; - -public: - void Init(void* buffer, uint32_t buffer_size); - void Destroy(); - - bool Write(void* entry, uint32_t entry_size); - bool Write(void* entries[], uint32_t entry_sizes[], uint32_t entry_count); - - bool Peek(void* read_buffer, uint32_t read_size); - bool Read(void* read_buffer, uint32_t read_size); - bool ReadDiscard(uint32_t read_size); -}; diff --git a/Classes/Foundation/GraphicsServices/Geometry/BasicPrimitives.h b/Classes/Foundation/GraphicsServices/Geometry/BasicPrimitives.h deleted file mode 100644 index ba50ba9..0000000 --- a/Classes/Foundation/GraphicsServices/Geometry/BasicPrimitives.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include "Foundation/Common/GlobalInclude.h" -#include "Foundation/Math/MathInclude.h" - -//#include -#include - -//---------------------------------------------------------------------------------------- -struct Basic2DPrimitive -{ -protected: - Basic2DPrimitive(); // Only the children classes can call this. - ~Basic2DPrimitive(); - - GLuint mVertexBuffer; - uint16_t mX, mY; -}; - -//---------------------------------------------------------------------------------------- -struct Rectangle : public Basic2DPrimitive -{ -public: - GLuint mWidth, mHeight; - - Rectangle( uint16_t width, uint16_t height, uint16_t anchorX, uint16_t anchorY ); -}; diff --git a/Classes/Foundation/GraphicsServices/Geometry/BasicPrimitives.mm b/Classes/Foundation/GraphicsServices/Geometry/BasicPrimitives.mm deleted file mode 100644 index 4cc9da1..0000000 --- a/Classes/Foundation/GraphicsServices/Geometry/BasicPrimitives.mm +++ /dev/null @@ -1,26 +0,0 @@ -#include "BasicPrimitives.h" - -//---------------------------------------------------------------------------------------- -Basic2DPrimitive::Basic2DPrimitive() : mVertexBuffer(0), mX(0), mY(0) -{ - glGenBuffers( 1, &mVertexBuffer ); -} - -//---------------------------------------------------------------------------------------- -Basic2DPrimitive::~Basic2DPrimitive() -{ - glDeleteBuffers( 1, &mVertexBuffer ); -} - -//---------------------------------------------------------------------------------------- -Rectangle::Rectangle( uint16_t width, uint16_t height, uint16_t anchorX, uint16_t anchorY ) : Basic2DPrimitive() -{ - Assert( mVertexBuffer ); - glBindBuffer( GL_ARRAY_BUFFER, mVertexBuffer ); - - mWidth = width; - mHeight = height; - - uint16_t values[4 * 2] = {-anchorX, height + anchorY, -anchorX, }; - glBufferData( GL_ARRAY_BUFFER, 4 * 2 * sizeof(uint16_t), values, GL_STATIC_DRAW ); -} diff --git a/Classes/Foundation/GraphicsServices/OpenGLServices.h b/Classes/Foundation/GraphicsServices/OpenGLServices.h deleted file mode 100644 index deadd57..0000000 --- a/Classes/Foundation/GraphicsServices/OpenGLServices.h +++ /dev/null @@ -1,58 +0,0 @@ -#pragma once - -/***************************************************************************************** - * This is a wrapper class around all OpenGL API calls in iOS. This completely abstracts - * away all mixes between the OS-specific calls in Objective-C and the C-style inteface of - * OpenGL. - * - * Since iPhone/iPad will be single/dual cores for the near-term future, we'll basically - * only allow a single rendering context to exist at any given point in time. Therefore, - * we should never need to set context other than during startup or shutdown. - *****************************************************************************************/ - -//#include -//#include -#include -#include - -#include "Foundation/Common/GlobalInclude.h" - -//---------------------------------------------------------------------------------------- -// Some forward declarations -@class EAGLContext; -struct RenderTarget; - -//---------------------------------------------------------------------------------------- -// Some typedefs for handles. May want to move this to a separate file. -typedef GLuint TextureHandle; - -//---------------------------------------------------------------------------------------- -// Complete C(++) OpenGL wrapper. We will use this to wrap -class OpenGLServices -{ -public: - enum - { - kMaxTextureSize = 512 * 512 * 4, - }; - - OpenGLServices(); - - bool init( EAGLContext* mainContext ); - void destroy(); - - bool initScratchMem(); - void destroyScratchMem(); - - bool setRenderTarget( RenderTarget& renderTarget ); - bool present( RenderTarget& renderTarget ); - - TextureHandle loadPNGTexture( const char* fileName ); - void destroyTexture( TextureHandle texture ); - -private: - EAGLContext* mGLContext; - void* mScratchMem; - - void setContext( EAGLContext* newContext ); -}; diff --git a/Classes/Foundation/GraphicsServices/OpenGLServices.mm b/Classes/Foundation/GraphicsServices/OpenGLServices.mm deleted file mode 100644 index 9c1f7a2..0000000 --- a/Classes/Foundation/GraphicsServices/OpenGLServices.mm +++ /dev/null @@ -1,160 +0,0 @@ -#import -#import - -#include "OpenGLServices.h" -#include "RenderTarget.h" - -//---------------------------------------------------------------------------------------- -OpenGLServices::OpenGLServices() : mGLContext(NULL), mScratchMem(NULL) -{ -} - -//---------------------------------------------------------------------------------------- -bool OpenGLServices::init( EAGLContext* mainContext ) -{ - if ( mGLContext ) - { - return false; - } - - mGLContext = [mainContext retain]; - Assert( [EAGLContext setCurrentContext:mGLContext] ); - - initScratchMem(); - - // Enable some common states and set their parameters. - glEnable( GL_TEXTURE_2D ); - glEnable( GL_BLEND ); - // We will premultiply alpha into the color channels of all textures (so no alpha animation). - // This is basically the A over B alpha scheme. - glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA ); - glEnable( GL_DEPTH_TEST ); - glDepthFunc( GL_LEQUAL ); - glFrontFace( GL_CCW ); - glCullFace( GL_BACK ); - - // Disable unused stuff. - glDisable( GL_STENCIL_TEST ); - glDisable( GL_SCISSOR_TEST ); - glDisable( GL_DITHER ); - - return true; -} - -//---------------------------------------------------------------------------------------- -void OpenGLServices::destroy() -{ - if ( !mGLContext ) - { - return; - } - - destroyScratchMem(); - [mGLContext release]; - [EAGLContext setCurrentContext:nil]; -} - -//---------------------------------------------------------------------------------------- -bool OpenGLServices::initScratchMem() -{ - if (!mScratchMem) - { - mScratchMem = malloc( kMaxTextureSize ); - return mScratchMem != NULL; - } - return true; -} - -//---------------------------------------------------------------------------------------- -void OpenGLServices::destroyScratchMem() -{ - if (mScratchMem) - { - free( mScratchMem ); - mScratchMem = NULL; - } -} - -//---------------------------------------------------------------------------------------- -bool OpenGLServices::setRenderTarget( RenderTarget& renderTarget ) -{ - if (!renderTarget.getFramebuffer() || !glIsFramebuffer( renderTarget.getFramebuffer() )) - { - return false; - } - - glBindFramebuffer( GL_FRAMEBUFFER, renderTarget.getFramebuffer() ); - return true; -} - -//---------------------------------------------------------------------------------------- -bool OpenGLServices::present( RenderTarget& renderTarget ) -{ - GLuint colorBuffer = renderTarget.getColorBuffer(); - if (!colorBuffer || !glIsRenderbuffer( colorBuffer )) - { - return false; - } - - glBindRenderbuffer( GL_RENDERBUFFER, colorBuffer ); - return [renderTarget.getContext() presentRenderbuffer:GL_RENDERBUFFER]; -} - -//---------------------------------------------------------------------------------------- -TextureHandle OpenGLServices::loadPNGTexture( const char* fileName ) -{ - GLuint textureHandle; - glGenTextures( 1, &textureHandle ); - if (!textureHandle) - { - return textureHandle; - } - - glBindTexture( GL_TEXTURE_2D, textureHandle ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); - - // http://iphonedevelopment.blogspot.com/2009/05/opengl-es-from-ground-up-part-6_25.html - NSString* nsFileName = [NSString stringWithUTF8String:fileName]; - NSString* nsPath = [[NSBundle mainBundle] pathForResource:nsFileName ofType:@"png"]; - NSData* fileData = [[NSData alloc] initWithContentsOfFile:nsPath]; - UIImage* image = [[UIImage alloc] initWithData:fileData]; - AssertMessage( image != nil, "Image failed to load!\n" ); - - GLuint width = CGImageGetWidth( image.CGImage ); - GLuint height = CGImageGetHeight( image.CGImage ); - CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); - Assert( width * height * 4 <= kMaxTextureSize ); - CGContextRef context = CGBitmapContextCreate( mScratchMem, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big ); - CGColorSpaceRelease( colorSpace ); - CGContextClearRect( context, CGRectMake( 0, 0, width, height ) ); - CGContextTranslateCTM( context, 0, 0 ); - CGContextDrawImage( context, CGRectMake( 0, 0, width, height ), image.CGImage ); - - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, mScratchMem ); - glGenerateMipmap( GL_TEXTURE_2D ); - - CGContextRelease(context); - [image release]; - [fileData release]; - - return textureHandle; -} - -//---------------------------------------------------------------------------------------- -void OpenGLServices::destroyTexture( TextureHandle texture ) -{ - glDeleteTextures( 1, &texture ); -} - -//---------------------------------------------------------------------------------------- -void OpenGLServices::setContext( EAGLContext* newContext ) -{ - if (mGLContext != newContext) - { - mGLContext = newContext; - [EAGLContext setCurrentContext:newContext]; - } -} diff --git a/Classes/Foundation/GraphicsServices/RenderTarget.h b/Classes/Foundation/GraphicsServices/RenderTarget.h deleted file mode 100644 index aad5d43..0000000 --- a/Classes/Foundation/GraphicsServices/RenderTarget.h +++ /dev/null @@ -1,65 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "Foundation/Common/GlobalInclude.h" - -@class EAGLContext; -@class CAEAGLLayer; - -//---------------------------------------------------------------------------------------- -struct RenderTargetInitParams -{ - RenderTargetInitParams(); - - EAGLContext* mContext; - CAEAGLLayer* mCALayer; - - // Width and height of color vs. depth buffers have to be the same. We don't resize in iOS. - int32_t mBufferWidth; - int32_t mBufferHeight; - - GLenum mColorFormat; - GLenum mDepthStencilFormat; - - uint32_t mFlags; -}; - -//---------------------------------------------------------------------------------------- -class RenderTarget -{ -public: - enum RenderTargetFlags - { - kSystemFramebuffer = 0x1 << 0, - }; - - const static uint32_t MAX_RENDER_BUFFERS = 2; - - RenderTarget(); - ~RenderTarget(); - - bool init( RenderTargetInitParams& params ); - void destroy(); - - EAGLContext* getContext() const { return mContext; } - uint32_t getWidth() const { return mRenderBufferWidth; } - uint32_t getHeight() const { return mRenderBufferHeight; } - GLuint getFramebuffer() const { return mFramebuffer; } - GLuint getColorBuffer() const { return mColorBuffer; } - -protected: - EAGLContext* mContext; - GLuint mFramebuffer; - - GLuint mColorBuffer; - GLuint mDepthStencilBuffer; - - int32_t mRenderBufferWidth; - int32_t mRenderBufferHeight; - - uint32_t mFlags; -}; diff --git a/Classes/Foundation/GraphicsServices/RenderTarget.mm b/Classes/Foundation/GraphicsServices/RenderTarget.mm deleted file mode 100644 index 659f7d6..0000000 --- a/Classes/Foundation/GraphicsServices/RenderTarget.mm +++ /dev/null @@ -1,150 +0,0 @@ -#include "RenderTarget.h" - -#import - -//---------------------------------------------------------------------------------------- -RenderTargetInitParams::RenderTargetInitParams() : mContext(NULL), - mCALayer(NULL), - mBufferWidth(0), - mBufferHeight(0), - mColorFormat(GL_NONE), - mDepthStencilFormat(GL_NONE), - mFlags(0) -{ -} - -//---------------------------------------------------------------------------------------- -RenderTarget::RenderTarget() : mContext(NULL), - mFramebuffer(0), - mColorBuffer(0), - mDepthStencilBuffer(0), - mRenderBufferWidth(0), - mRenderBufferHeight(0), - mFlags(0) -{ -}; - -//---------------------------------------------------------------------------------------- -RenderTarget::~RenderTarget() -{ - if ( mFramebuffer ) - { - AssertMessage( !mFramebuffer, "RenderTarget has not been destroyed yet!" ); - destroy(); - } - else - { - mContext = NULL; - mFramebuffer = 0; - mColorBuffer = 0; - mDepthStencilBuffer = 0; - mRenderBufferWidth = 0; - mRenderBufferHeight = 0; - mFlags = 0; - } -}; - -//---------------------------------------------------------------------------------------- -bool RenderTarget::init( RenderTargetInitParams& params ) -{ - if (mFramebuffer) - { - AssertMessage( 0, "RenderTarget already initialized.\n" ); - return false; - } - - mContext = params.mContext; - AssertMessage( mContext, "Invalid context provided.\n" ); - - // Set the context first. - [EAGLContext setCurrentContext:mContext]; - - mRenderBufferWidth = params.mBufferWidth; - mRenderBufferHeight = params.mBufferHeight; - - if (params.mFlags & kSystemFramebuffer) - { - [mContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:params.mCALayer]; - glGetRenderbufferParameteriv( GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &mRenderBufferWidth ); - glGetRenderbufferParameteriv( GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &mRenderBufferHeight ); - } - else - { - if (params.mColorFormat != GL_NONE) - { - glGenRenderbuffers( 1, &mColorBuffer ); - Assert( mColorBuffer ); - glBindRenderbuffer( GL_RENDERBUFFER, mColorBuffer ); - glRenderbufferStorage( GL_RENDERBUFFER, params.mColorFormat, mRenderBufferWidth, mRenderBufferHeight ); - } - } - - if (params.mDepthStencilFormat != GL_NONE) - { - // If a depth/stencil buffer is needed, we'll create one with the same dimensions as the color buffer. - glGenRenderbuffers( 1, &mDepthStencilBuffer ); - Assert( mDepthStencilBuffer ); - glBindRenderbuffer( GL_RENDERBUFFER, mDepthStencilBuffer ); - glRenderbufferStorage( GL_RENDERBUFFER, params.mDepthStencilFormat, mRenderBufferWidth, mRenderBufferHeight ); - } - - if (mColorBuffer || mDepthStencilBuffer) - { - // Create framebuffer object. - glGenFramebuffers( 1, &mFramebuffer ); - glBindFramebuffer( GL_FRAMEBUFFER, mFramebuffer ); - - if (mColorBuffer) - { - glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, mColorBuffer ); - } - - if (mDepthStencilBuffer) - { - glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mDepthStencilBuffer ); - } - - AssertMessage( glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE, "Failed to make complete framebuffer object: %x", glCheckFramebufferStatus(GL_FRAMEBUFFER) ); - } - else - { - return false; - } - - - return true; -} - -//---------------------------------------------------------------------------------------- -void RenderTarget::destroy() -{ - if (!mContext) - { - return; - } - - [EAGLContext setCurrentContext:mContext]; - - if (mColorBuffer && glIsRenderbuffer(mColorBuffer)) - { - glDeleteRenderbuffers( 1, &mColorBuffer ); - } - - if (mDepthStencilBuffer && glIsRenderbuffer(mDepthStencilBuffer)) - { - glDeleteRenderbuffers( 1, &mDepthStencilBuffer ); - } - - if (mFramebuffer) - { - glDeleteFramebuffers( 1, &mFramebuffer ); - } - - mContext = NULL; - mFramebuffer = 0; - mColorBuffer = 0; - mDepthStencilBuffer = 0; - mRenderBufferWidth = 0; - mRenderBufferHeight = 0; - mFlags = 0; -} diff --git a/Classes/Foundation/Hash/DJB2.h b/Classes/Foundation/Hash/DJB2.h deleted file mode 100755 index 31b863f..0000000 --- a/Classes/Foundation/Hash/DJB2.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include "Foundation/Common/GlobalInclude.h" - -//---------------------------------------------------------------------------------------- -// djb2 string hash function -static slInline uint32_t StrHash(const char* str) -{ - uint32_t hash = 5381; // seed value - int32_t c = *str; - - if(!str) - { - Assert(false); - return 0; - } - - while ( (*str != 0) ) - { - hash = ((hash << 5) + hash) + c; /* hash * 33 + c == ((hash * 32) + hash) + c */ - c = *(str++); - } - - return hash; -} \ No newline at end of file diff --git a/Classes/Foundation/Math/MathDefines.h b/Classes/Foundation/Math/MathDefines.h deleted file mode 100755 index b9bbdaa..0000000 --- a/Classes/Foundation/Math/MathDefines.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#define kEpsilon 1e-6 -#define kSqrtEpsilon 1e-3 - -#define kPi 3.1415926535897932384626433832795 -#define kPi_2 1.5707963267948966192313216916398 -#define kPi_3 1.0471975511965977461542144610932 -#define kPi_4 0.78539816339744830961566084581988 -#define k2Pi_3 2.0943951023931954923084289221863 -#define k3Pi_4 2.3561944901923449288469825374596 -#define k2Pi 6.283185307179586476925286766559 - -#define kE 2.7182818284590452353602874713527 diff --git a/Classes/Foundation/Math/MathInclude.h b/Classes/Foundation/Math/MathInclude.h deleted file mode 100755 index 718190a..0000000 --- a/Classes/Foundation/Math/MathInclude.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -#include "Foundation/Math/MathDefines.h" -#include "Foundation/Math/MathOperations.h" -#include "Foundation/Math/MathTypes.h" -#include "Foundation/Math/Vector.h" -#include "Foundation/Math/Quaternion.h" -#include "Foundation/Math/Matrix.h" diff --git a/Classes/Foundation/Math/MathOperations.h b/Classes/Foundation/Math/MathOperations.h deleted file mode 100755 index af49e6f..0000000 --- a/Classes/Foundation/Math/MathOperations.h +++ /dev/null @@ -1,75 +0,0 @@ -#pragma once - -#include -#include "Foundation/Common/GlobalInclude.h" - -#if defined(__WINDOWS__) - -#pragma intrinsic(sin) -#pragma intrinsic(cos) -#pragma intrinsic(tan) -#pragma intrinsic(abs) -#pragma intrinsic(fabs) -#pragma intrinsic(asin) -#pragma intrinsic(acos) -#pragma intrinsic(atan) -#pragma intrinsic(atan2) -#pragma intrinsic(exp) -#pragma intrinsic(sqrt) -#pragma intrinsic(log) -#pragma intrinsic(log10) - -#endif - -#define Sinf(x) sin(x) -#define Cosf(x) cos(x) -#define Tanf(x) tan(x) -#define Sqrtf(x) sqrt(x) - -//---------------------------------------------------------------------------------------- -#define Min2(x, y) ( (x) <= (y) ? (x) : (y) ) -#define Max2(x, y) ( (x) >= (y) ? (x) : (y) ) -#define Min3(x, y, z) ( (x) <= (y) ? ((x) <= (z) ? (x) : (z)) : ((y) <= (z) ? (y) : (z)) ) -#define Max3(x, y, z) ( (x) >= (y) ? ((x) >= (z) ? (x) : (z)) : ((y) >= (z) ? (y) : (z)) ) - -//---------------------------------------------------------------------------------------- -slInline float Absf(const float& f) -{ - IntFloat int_float; - int_float.m_Float = f; - int_float.m_Int32 &= 0x7fffffff; - return int_float.m_Float; -} - -//---------------------------------------------------------------------------------------- -slInline bool EpsilonEquals(const float& a, const float& b, const float& epsilon) -{ - return Absf(a - b) <= epsilon; -} - -//---------------------------------------------------------------------------------------- -slInline uint16_t ConvertFloatToHalf( unsigned int f ) -{ - unsigned int s = f & 0x80000000; - signed int e = ((f & 0x7f800000) >> 23) - (127 - 15); - if (e < 0) return 0; - else if (e > 31) - { - e = 31; - } - unsigned int fo = f & 0x7fffff; - return (uint16_t)((s >> 16) | ((e << 10) & 0x7c00) | (fo >> 13)); -} - -//---------------------------------------------------------------------------------------- -slInline float ConvertHalfToFloat( unsigned short h ) -{ - unsigned int s = h & 0x8000; - unsigned int e = ((h & 0x7c00) >> 10) - 15 + 127; - unsigned int f = h & 0x3ff; - - IntFloat int_float; - int_float.m_UInt32 = ((s << 16) | ((e << 23) & 0x7f800000) | (f << 13)); - - return int_float.m_Float; -} diff --git a/Classes/Foundation/Math/MathTypes.h b/Classes/Foundation/Math/MathTypes.h deleted file mode 100755 index 379bf16..0000000 --- a/Classes/Foundation/Math/MathTypes.h +++ /dev/null @@ -1,363 +0,0 @@ -#pragma once - -#include "Foundation/Common/GlobalInclude.h" -#include "Foundation/Math/MathOperations.h" - -//---------------------------------------------------------------------------------------- -struct Vec2 -{ - float x; - float y; - - slInline explicit Vec2() {} - slInline explicit Vec2(const float xy) {x = xy; y = xy;} - slInline explicit Vec2(const float ix, const float iy) {x = ix; y = iy;} - - slInline Vec2(const Vec2& v) {x = v.x; y = v.y;} // copy constructor - - slInline float* AsFloatArray() {return &x;} - slInline const float* AsFloatArray() const {return &x;} - - slInline float GetComponent(int index) const {return AsFloatArray()[index];} -}; - - -//---------------------------------------------------------------------------------------- -struct Vec3 -{ - float x; - float y; - float z; - - slInline explicit Vec3() {} - slInline explicit Vec3(const float xyz) {x = xyz; y = xyz; z = xyz;} - slInline explicit Vec3(const float ix, const float iy, const float iz) {x = ix; y = iy; z = iz;} - - slInline explicit Vec3(const Vec2& v, const float iz) {x = v.x; y = v.y; z = iz;} - - slInline Vec3(const Vec3& v) {x = v.x; y = v.y; z = v.z;} // copy constructor - - slInline Vec2 AsVec2() const {return Vec2(x, y);} - - slInline float* AsFloatArray() {return &x;} - slInline const float* AsFloatArray() const {return &x;} - - slInline float GetComponent(int index) const {return AsFloatArray()[index];} -}; - - -//---------------------------------------------------------------------------------------- -struct Vec4 -{ - float x; - float y; - float z; - float w; - - slInline explicit Vec4() {} - slInline explicit Vec4(const float xyzw) {x = xyzw; y = xyzw; z = xyzw; w = xyzw;} - slInline explicit Vec4(const float xyz, const float iw) {x = xyz; y = xyz; z = xyz; w = iw;} - slInline explicit Vec4(const float ix, const float iy, const float iz, const float iw) {x = ix; y = iy; z = iz; w = iw;} - - slInline explicit Vec4(const Vec2& u, const Vec2& v) {x = u.x; y = u.y; z = v.x; w = v.y;} - slInline explicit Vec4(const Vec2& v, const float iz, const float iw) {x = v.x; y = v.y; z = iz; w = iw;} - - slInline explicit Vec4(const Vec3& v, const float iw) {x = v.x; y = v.y; z = v.z; w = iw;} - - slInline Vec4(const Vec4& v) {x = v.x; y = v.y; z = v.z; w = v.w;} // copy constructor - - slInline Vec2 AsVec2() const {return Vec2(x, y);} - slInline Vec3 AsVec3() const {return Vec3(x, y, z);} - - slInline float* AsFloatArray() {return &x;} - slInline const float* AsFloatArray() const {return &x;} - - slInline float GetComponent(int index) const {return AsFloatArray()[index];} -}; - - -//---------------------------------------------------------------------------------------- -struct Quaternion -{ - float i; - float j; - float k; - float s; - - slInline explicit Quaternion() {} - explicit Quaternion(const float angle, const Vec3& normalized_axis); - explicit Quaternion(const float angle, const float x, const float y, const float z); - - slInline Quaternion(const Quaternion& q) {i = q.i; j = q.j; k = q.k; s = q.s;} - - Vec4 AsVec4(); - slInline float* AsFloatArray() {return &i;} - - Vec3 GetImaginary(); - slInline float GetReal() {return s;} -}; - -slInline Quaternion::Quaternion(const float angle, const Vec3& normalized_axis) -{ - Assert( EpsilonEquals(normalized_axis.x * normalized_axis.x + normalized_axis.y * normalized_axis.y + normalized_axis.z * normalized_axis.z, 1.0f, kSqrtEpsilon) ); - float half_angle = angle * 0.5f; - float sin_half_angle = (float)Sinf(half_angle); - i = normalized_axis.x * sin_half_angle; - j = normalized_axis.y * sin_half_angle; - k = normalized_axis.z * sin_half_angle; - s = (float)Cosf(half_angle); -} - -slInline Quaternion::Quaternion(const float angle, const float x, const float y, const float z) -{ - Assert( EpsilonEquals(x * x + y * y + z * z, 1.0f, kSqrtEpsilon) ); - float half_angle = angle * 0.5f; - float sin_half_angle = (float)Sinf(half_angle); - i = x * sin_half_angle; - j = y * sin_half_angle; - k = z * sin_half_angle; - s = (float)Cosf(half_angle); -} - -Vec4 Quaternion::AsVec4() -{ - Vec4 r; - r.x = i; - r.y = j; - r.z = k; - r.w = s; - return r; -} - -Vec3 Quaternion::GetImaginary() -{ - Vec3 r; - r.x = i; - r.y = j; - r.z = k; - return r; -} - - -//---------------------------------------------------------------------------------------- -struct Mat3 -{ - Vec3 v[3]; - - slInline explicit Mat3() {} - explicit Mat3(const Vec3& v0, const Vec3& v1, const Vec3& v2); - explicit Mat3(const float v00, const float v01, const float v02, - const float v10, const float v11, const float v12, - const float v20, const float v21, const float v22); - Mat3(const Mat3& m); // copy constructor - - float* AsFloatArray() {return &v[0].x;} -}; - -slInline Mat3::Mat3(const Vec3& v0, const Vec3& v1, const Vec3& v2) -{ - v[0] = v0; - v[1] = v1; - v[2] = v2; -} - -slInline Mat3::Mat3(const float v00, const float v01, const float v02, const float v10, const float v11, const float v12, const float v20, const float v21, const float v22) -{ - v[0].x = v00; v[0].y = v01; v[0].z = v02; - v[1].x = v10; v[1].y = v11; v[1].z = v12; - v[2].x = v20; v[2].y = v21; v[2].z = v22; -} - -slInline Mat3::Mat3(const Mat3& m) -{ - v[0] = m.v[0]; - v[1] = m.v[1]; - v[2] = m.v[2]; -} - - -//---------------------------------------------------------------------------------------- -struct Mat4 -{ - Vec4 v[4]; - - slInline explicit Mat4() {} - explicit Mat4(const Vec4& v0, const Vec4& v1, const Vec4& v2, const Vec4& v3); - explicit Mat4(const float v00, const float v01, const float v02, const float v03, - const float v10, const float v11, const float v12, const float v13, - const float v20, const float v21, const float v22, const float v23, - const float v30, const float v31, const float v32, const float v33); - explicit Mat4(const Mat3& m); - Mat4(const Mat4& m); // copy constructor - - float* AsFloatArray() {return &v[0].x;} -}; - -slInline Mat4::Mat4(const Vec4& v0, const Vec4& v1, const Vec4& v2, const Vec4& v3) -{ - v[0] = v0; - v[1] = v1; - v[2] = v2; - v[3] = v3; -} - -slInline Mat4::Mat4(const float v00, const float v01, const float v02, const float v03, const float v10, const float v11, const float v12, const float v13, const float v20, const float v21, const float v22, const float v23, const float v30, const float v31, const float v32, const float v33) -{ - v[0].x = v00; v[0].y = v01; v[0].z = v02; v[0].w = v03; - v[1].x = v10; v[1].y = v11; v[1].z = v12; v[1].w = v13; - v[2].x = v20; v[2].y = v21; v[2].z = v22; v[2].w = v23; - v[3].x = v30; v[3].y = v31; v[3].z = v32; v[3].w = v33; -} - -slInline Mat4::Mat4(const Mat3& m) -{ - v[0].x = m.v[0].x; v[0].y = m.v[0].y; v[0].z = m.v[0].z; v[0].w = 0.0f; - v[1].x = m.v[1].x; v[1].y = m.v[1].y; v[1].z = m.v[1].z; v[1].w = 0.0f; - v[2].x = m.v[2].x; v[2].y = m.v[2].y; v[2].z = m.v[2].z; v[2].w = 0.0f; - v[3].x = 0.0f; v[3].y = 0.0f; v[3].z = 0.0f; v[3].w = 1.0f; -} - -slInline Mat4::Mat4(const Mat4& m) -{ - v[0] = m.v[0]; - v[1] = m.v[1]; - v[2] = m.v[2]; - v[3] = m.v[3]; -} - - -//---------------------------------------------------------------------------------------- -typedef MEM_ALIGN(8) Vec2 AlignedVec2; -typedef MEM_ALIGN(16) Vec3 AlignedVec3; -typedef MEM_ALIGN(16) Vec4 AlignedVec4; - -typedef MEM_ALIGN(64) Mat3 AlignedMat3; -typedef MEM_ALIGN(64) Mat4 AlignedMat4; - - -//---------------------------------------------------------------------------------------- -// SIMD Structures -//---------------------------------------------------------------------------------------- -struct Vec2SOA -{ - uint32_t m_Count; - uint32_t m_MaxCount; - - float* m_X; - float* m_Y; - - slInline explicit Vec2SOA() {m_Count = 0; m_MaxCount = 0; m_X = NULL; m_Y = NULL;} - bool InitVec2SOA(float* buffer, uint32_t buffer_size); - - void GetVec2AtIndex(Vec2& v, uint32_t index) {Assert(index < m_Count); v.x = m_X[index]; v.y = m_Y[index];} -}; - -slInline bool Vec2SOA::InitVec2SOA(float* buffer, uint32_t max_count) -{ - m_Count = 0; - - uint64_t buffer_addr = (uint64_t)buffer; - uint64_t buffer_addr_aligned = ALIGN_UP(buffer_addr, 16); - uint64_t buffer_end = (uint64_t)(buffer + max_count); - - if (buffer_end - buffer_addr_aligned < 20) - { - m_MaxCount = 0; - m_X = NULL; - m_Y = NULL; - - return false; - } - - uint32_t remainder = (uint32_t)POWER_OF_TWO_MOD(buffer_end, 16) / 4; - uint32_t slot_count = (uint32_t)(buffer_end - buffer_addr_aligned) / 32; - - m_MaxCount = slot_count * 4 + (uint32_t)(remainder * (slot_count & 0x1)); - - m_X = (float*)buffer_addr_aligned; - m_Y = m_X + ALIGN_UP(m_MaxCount, 4); -} - - -//---------------------------------------------------------------------------------------- -struct Vec3SOA : public Vec2SOA -{ - float* m_Z; - - slInline explicit Vec3SOA() {m_Count = 0; m_MaxCount = 0; m_X = NULL; m_Y = NULL; m_Z = NULL;} - bool InitVec3SOA(float* buffer, uint32_t buffer_size); - - void GetVec3AtIndex(Vec3& v, uint32_t index) {Assert(index < m_Count); v.x = m_X[index]; v.y = m_Y[index]; v.z = m_Z[index];} -}; - -slInline bool Vec3SOA::InitVec3SOA(float* buffer, uint32_t max_count) -{ - m_Count = 0; - - uint64_t buffer_addr = (uint64_t)buffer; - uint64_t buffer_addr_aligned = ALIGN_UP(buffer_addr, 16); - uint64_t buffer_end = (uint64_t)(buffer + max_count); - - if (buffer_end - buffer_addr_aligned < 36) - { - m_MaxCount = 0; - m_X = NULL; - m_Y = NULL; - m_Z = NULL; - - return false; - } - - uint32_t remainder = (uint32_t)POWER_OF_TWO_MOD(buffer_end, 16) / 4; - uint32_t slot_count = (uint32_t)(buffer_end - buffer_addr_aligned) / 48; - - m_MaxCount = slot_count * 4 + (uint32_t)(remainder * (slot_count % 3 == 2)); - - m_X = (float*)buffer_addr_aligned; - uint32_t aligned_count = ALIGN_UP(m_MaxCount, 4); - m_Y = m_X + aligned_count; - m_Z = m_Y + aligned_count; -} - - -//---------------------------------------------------------------------------------------- -struct Vec4SOA : public Vec3SOA -{ - float* m_W; - - slInline explicit Vec4SOA() {m_Count = 0; m_MaxCount = 0; m_X = NULL; m_Y = NULL; m_Z = NULL; m_W = NULL;} - bool InitVec4SOA(float* buffer, uint32_t buffer_size); - - void GetVec4AtIndex(Vec4& v, uint32_t index) {Assert(index < m_Count); v.x = m_X[index]; v.y = m_Y[index]; v.z = m_Z[index]; v.w = m_W[index];} -}; - -slInline bool Vec4SOA::InitVec4SOA(float* buffer, uint32_t max_count) -{ - m_Count = 0; - - uint64_t buffer_addr = (uint64_t)buffer; - uint64_t buffer_addr_aligned = ALIGN_UP(buffer_addr, 16); - uint64_t buffer_end = (uint64_t)(buffer + max_count); - - if (buffer_end - buffer_addr_aligned < 52) - { - m_MaxCount = 0; - m_X = NULL; - m_Y = NULL; - m_Z = NULL; - m_W = NULL; - - return false; - } - - uint32_t remainder = (uint32_t)POWER_OF_TWO_MOD(buffer_end, 16) / 4; - uint32_t slot_count = (uint32_t)(buffer_end - buffer_addr_aligned) / 64; - - m_MaxCount = slot_count * 4 + (uint32_t)(remainder * ((slot_count & 0x3) == 4)); - - m_X = (float*)buffer_addr_aligned; - uint32_t aligned_count = ALIGN_UP(m_MaxCount, 4); - m_Y = m_X + aligned_count; - m_Z = m_Y + aligned_count; - m_W = m_Z + aligned_count; -} diff --git a/Classes/Foundation/Math/Matrix.h b/Classes/Foundation/Math/Matrix.h deleted file mode 100755 index 40e2a70..0000000 --- a/Classes/Foundation/Math/Matrix.h +++ /dev/null @@ -1,660 +0,0 @@ -#pragma once - -#include "Foundation/Common/GlobalInclude.h" -#include "Foundation/Math/MathOperations.h" -#include "Foundation/Math/MathTypes.h" -#include "Foundation/Math/Vector.h" - -//---------------------------------------------------------------------------------------- -// Matrix are expressed in post-multiply row-major format. -// That is, matrix multiplication follows the form v = x * A. -// A local-to-world transform with basis a, b, c, expressed in world space, will result -// in a matrix that looks like: -// _ _ -// | a | -// | b | -// | c | -// - - -// -// Therefore, translate t will naturally follow below c, in the illustration above. -// With this, we can stuff the matrix directly into OpenGL and not have to do swizzling. -//---------------------------------------------------------------------------------------- - -//---------------------------------------------------------------------------------------- -// Mat3 -//---------------------------------------------------------------------------------------- -void SetMat3(Mat3& r, const Mat3& a); -void SetMat3(Mat3& r, const Vec3& v0, const Vec3& v1, const Vec3& v2); - -void ScaleMat3(Mat3& r, const float s, const Mat3& a); -Mat3 ScaleMat3(const float s, const Mat3& a); - -float DeterminantOfMat3(const Mat3& a); - -void TransposeMat3(Mat3& r, const Mat3& a); -Mat3 TransposeMat3(const Mat3& a); - -void InvertAffineMat3(Mat3& r, const Mat3& a); -Mat3 InvertAffineMat3(const Mat3& a); - -void InvertGeneralMat3(Mat3& r, const Mat3& a); -Mat3 InvertGeneralMat3(const Mat3& a); - -void MulMat3(Mat3& r, const Mat3& a, const Mat3& b); -Mat3 MulMat3(const Mat3& a, const Mat3& b); - -void MulMat3ByTransposedMat3(Mat3& r, const Mat3& a, const Mat3& b); -Mat3 MulMat3ByTransposedMat3(const Mat3& a, const Mat3& b); - -void MulVec3ByMat3(Vec3& r, const Vec3& v, const Mat3& a); -Vec3 MulVec3ByMat3(const Vec3& v, const Mat3& a); - -//---------------------------------------------------------------------------------------- -slInline void SetMat3(Mat3& r, const Mat3& a) -{ - r.v[0] = a.v[0]; - r.v[1] = a.v[1]; - r.v[2] = a.v[2]; -} - -//---------------------------------------------------------------------------------------- -slInline void SetMat3(Mat3& r, const Vec3& v0, const Vec3& v1, const Vec3& v2) -{ - r.v[0] = v0; - r.v[1] = v1; - r.v[2] = v2; -} - -//---------------------------------------------------------------------------------------- -slInline void ScaleMat3(Mat3& r, const float s, const Mat3& a) -{ - ScaleVec3(r.v[0], s, a.v[0]); - ScaleVec3(r.v[1], s, a.v[1]); - ScaleVec3(r.v[2], s, a.v[2]); -} - -//---------------------------------------------------------------------------------------- -slInline Mat3 ScaleMat3(const float s, const Mat3& a) -{ - Mat3 r; - ScaleVec3(r.v[0], s, a.v[0]); - ScaleVec3(r.v[1], s, a.v[1]); - ScaleVec3(r.v[2], s, a.v[2]); - return r; -} - -//---------------------------------------------------------------------------------------- -slInline float DeterminantOfMat3(const Mat3& a) -{ - return a.v[0].x * (a.v[1].y * a.v[2].z - a.v[1].z * a.v[2].y) - + a.v[0].y * (a.v[1].z * a.v[2].x - a.v[1].x * a.v[2].z) - + a.v[0].z * (a.v[1].x * a.v[2].y - a.v[1].y * a.v[2].x); -} - -//---------------------------------------------------------------------------------------- -slInline void TransposeMat3(Mat3& r, const Mat3& a) -{ - // a might be the same as r - r.v[0].x = a.v[0].x; - r.v[1].y = a.v[1].y; - r.v[2].z = a.v[2].z; - - float temp; - temp = r.v[0].y; - r.v[0].y = r.v[1].x; - r.v[1].x = temp; - - temp = r.v[0].z; - r.v[0].z = r.v[2].x; - r.v[2].x = temp; - - temp = r.v[1].z; - r.v[1].z = r.v[2].y; - r.v[2].y = temp; -} - -//---------------------------------------------------------------------------------------- -slInline Mat3 TransposeMat3(const Mat3& a) -{ - Mat3 r; - r.v[0].x = a.v[0].x; r.v[0].y = a.v[1].x; r.v[0].z = a.v[2].x; - r.v[1].x = a.v[0].y; r.v[1].y = a.v[1].y; r.v[1].z = a.v[2].y; - r.v[2].x = a.v[0].z; r.v[2].y = a.v[1].z; r.v[2].z = a.v[2].z; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void InvertAffineMat3(Mat3& r, const Mat3& a) -{ - TransposeMat3(r, a); -} - -//---------------------------------------------------------------------------------------- -slInline Mat3 InvertAffineMat3(const Mat3& a) -{ - return TransposeMat3(a); -} - -//---------------------------------------------------------------------------------------- -slInline void InvertGeneralMat3(Mat3& r, const Mat3& a) -{ - float inv_determinant = 1.0f / DeterminantOfMat3(a); - - Mat3 a_T; - TransposeMat3(a_T, a); - CrossVec3(r.v[0], a_T.v[1], a_T.v[2]); - CrossVec3(r.v[1], a_T.v[2], a_T.v[0]); - CrossVec3(r.v[2], a_T.v[0], a_T.v[1]); - - ScaleMat3(r, inv_determinant, r); -} - -//---------------------------------------------------------------------------------------- -slInline Mat3 InvertGeneralMat3(const Mat3& a) -{ - float inv_determinant = 1.0f / DeterminantOfMat3(a); - - Mat3 r; - Mat3 a_T = TransposeMat3(a); - CrossVec3(r.v[0], a_T.v[1], a_T.v[2]); - CrossVec3(r.v[1], a_T.v[2], a_T.v[0]); - CrossVec3(r.v[2], a_T.v[0], a_T.v[1]); - - return ScaleMat3(inv_determinant, r); -} - -//---------------------------------------------------------------------------------------- -slInline void MulMat3(Mat3& r, const Mat3& a, const Mat3& b) -{ - Mat3 temp; - - temp.v[0].x = DotVec3(a.v[0], b.v[0].x, b.v[1].x, b.v[2].x); - temp.v[0].y = DotVec3(a.v[0], b.v[0].y, b.v[1].y, b.v[2].y); - temp.v[0].z = DotVec3(a.v[0], b.v[0].z, b.v[1].z, b.v[2].z); - - temp.v[1].x = DotVec3(a.v[1], b.v[0].x, b.v[1].x, b.v[2].x); - temp.v[1].y = DotVec3(a.v[1], b.v[0].y, b.v[1].y, b.v[2].y); - temp.v[1].z = DotVec3(a.v[1], b.v[0].z, b.v[1].z, b.v[2].z); - - temp.v[2].x = DotVec3(a.v[2], b.v[0].x, b.v[1].x, b.v[2].x); - temp.v[2].y = DotVec3(a.v[2], b.v[0].y, b.v[1].y, b.v[2].y); - temp.v[2].z = DotVec3(a.v[2], b.v[0].z, b.v[1].z, b.v[2].z); - - SetMat3(r, temp); -} - -//---------------------------------------------------------------------------------------- -slInline Mat3 MulMat3(const Mat3& a, const Mat3& b) -{ - Mat3 r; - - r.v[0].x = DotVec3(a.v[0], b.v[0].x, b.v[1].x, b.v[2].x); - r.v[0].y = DotVec3(a.v[0], b.v[0].y, b.v[1].y, b.v[2].y); - r.v[0].z = DotVec3(a.v[0], b.v[0].z, b.v[1].z, b.v[2].z); - - r.v[1].x = DotVec3(a.v[1], b.v[0].x, b.v[1].x, b.v[2].x); - r.v[1].y = DotVec3(a.v[1], b.v[0].y, b.v[1].y, b.v[2].y); - r.v[1].z = DotVec3(a.v[1], b.v[0].z, b.v[1].z, b.v[2].z); - - r.v[2].x = DotVec3(a.v[2], b.v[0].x, b.v[1].x, b.v[2].x); - r.v[2].y = DotVec3(a.v[2], b.v[0].y, b.v[1].y, b.v[2].y); - r.v[2].z = DotVec3(a.v[2], b.v[0].z, b.v[1].z, b.v[2].z); - - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void MulMat3ByTransposedMat3(Mat3& r, const Mat3& a, const Mat3& b) -{ - Mat3 temp; - - temp.v[0].x = DotVec3(a.v[0], b.v[0]); - temp.v[0].y = DotVec3(a.v[0], b.v[1]); - temp.v[0].z = DotVec3(a.v[0], b.v[2]); - - temp.v[1].x = DotVec3(a.v[1], b.v[0]); - temp.v[1].y = DotVec3(a.v[1], b.v[1]); - temp.v[1].z = DotVec3(a.v[1], b.v[2]); - - temp.v[2].x = DotVec3(a.v[2], b.v[0]); - temp.v[2].y = DotVec3(a.v[2], b.v[1]); - temp.v[2].z = DotVec3(a.v[2], b.v[2]); - - SetMat3(r, temp); -} - -//---------------------------------------------------------------------------------------- -slInline Mat3 MulMat3ByTransposedMat3(const Mat3& a, const Mat3& b) -{ - Mat3 r; - - r.v[0].x = DotVec3(a.v[0], b.v[0]); - r.v[0].y = DotVec3(a.v[0], b.v[1]); - r.v[0].z = DotVec3(a.v[0], b.v[2]); - - r.v[1].x = DotVec3(a.v[1], b.v[0]); - r.v[1].y = DotVec3(a.v[1], b.v[1]); - r.v[1].z = DotVec3(a.v[1], b.v[2]); - - r.v[2].x = DotVec3(a.v[2], b.v[0]); - r.v[2].y = DotVec3(a.v[2], b.v[1]); - r.v[2].z = DotVec3(a.v[2], b.v[2]); - - return r; -} - -//---------------------------------------------------------------------------------------- -// do NOT pass in components of a as the r vector -slInline void MulVec3ByMat3(Vec3& r, const Vec3& v, const Mat3& a) -{ - float x = v.x * a.v[0].x + v.y * a.v[1].x + v.z * a.v[2].x; - float y = v.x * a.v[0].y + v.y * a.v[1].y + v.z * a.v[2].y; - float z = v.x * a.v[0].z + v.y * a.v[1].z + v.z * a.v[2].z; - SetVec3(r, x, y, z); -} - -//---------------------------------------------------------------------------------------- -slInline Vec3 MulVec3ByMat3(const Vec3& v, const Mat3& a) -{ - Vec3 r; - r.x = v.x * a.v[0].x + v.y * a.v[1].x + v.z * a.v[2].x; - r.y = v.x * a.v[0].y + v.y * a.v[1].y + v.z * a.v[2].y; - r.z = v.x * a.v[0].z + v.y * a.v[1].z + v.z * a.v[2].z; - return r; -} - - - -//---------------------------------------------------------------------------------------- -// Mat4 -//---------------------------------------------------------------------------------------- -void SetMat4(Mat4& r, const Mat4& a); -void SetMat4(Mat4& r, const Mat3& a); -void SetMat4(Mat4& r, const Mat3& a, const Vec3& position); - -void ScaleMat4(Mat4& r, const float s, const Mat4& a); -Mat4 ScaleMat4(const float s, const Mat4& a); - -float DeterminantOfMat4(const Mat4& a); - -void TransposeMat4(Mat4& r, const Mat4& a); -Mat4 TransposeMat4(const Mat4& a); - -void InvertAffineMat4(Mat4& r, const Mat4& a); -Mat4 InvertAffineMat4(const Mat4& a); - -void InvertGeneralMat4(Mat4& r, const Mat4& a); -Mat4 InvertGeneralMat4(const Mat4& a); - -void MulMat4(Mat4& r, const Mat4& a, const Mat4& b); -Mat4 MulMat4(const Mat4& a, const Mat4& b); - -void MulMat4ByTransposedMat4(Mat4& r, const Mat4& a, const Mat4& t); -Mat4 MulMat4ByTransposedMat4(const Mat4& a, Mat4& t); - -void MulVec4ByMat4(Vec4& r, const Vec4& v, const Mat4& a); -Vec4 MulVec4ByMat4(const Vec4& v, const Mat4& a); - -void MulVec3ByMat4(Vec4& r, const Vec3& v, const float w, const Mat4& a); -Vec4 MulVec3ByMat4(const Vec3& v, const float w, const Mat4& a); - -void MulVec4ByTransposedMat4(Vec4& r, const Vec4& v, const Mat4& t); -Vec4 MulVec4ByTransposedMat4(const Vec4& v, const Mat4& t); - -void MulVec3ByTransposedMat4(Vec4& r, const Vec3& v, const float w, const Mat4& t); -Vec4 MulVec3ByTransposedMat4(const Vec3& v, const float w, const Mat4& t); - -//---------------------------------------------------------------------------------------- -slInline void SetMat4(Mat4& r, const Mat4& a) -{ - r.v[0] = a.v[0]; - r.v[1] = a.v[1]; - r.v[2] = a.v[2]; - r.v[3] = a.v[3]; -} - -//---------------------------------------------------------------------------------------- -slInline void SetMat4(Mat4& r, const Mat3& a) -{ - SetVec4(r.v[0], a.v[0], 0.0f); - SetVec4(r.v[1], a.v[1], 0.0f); - SetVec4(r.v[2], a.v[2], 0.0f); - SetVec4(r.v[3], 0.0f, 0.0f, 0.0f, 1.0f); -} - -//---------------------------------------------------------------------------------------- -slInline void SetMat4(Mat4& r, const Mat3& a, const Vec3& position) -{ - SetVec4(r.v[0], a.v[0], 0.0f); - SetVec4(r.v[1], a.v[1], 0.0f); - SetVec4(r.v[2], a.v[2], 0.0f); - SetVec4(r.v[3], position, 1.0f); -} - -//---------------------------------------------------------------------------------------- -slInline void ScaleMat4(Mat4& r, const float s, const Mat4& a) -{ - ScaleVec4(r.v[0], s, a.v[0]); - ScaleVec4(r.v[1], s, a.v[1]); - ScaleVec4(r.v[2], s, a.v[2]); - ScaleVec4(r.v[3], s, a.v[3]); -} - -//---------------------------------------------------------------------------------------- -slInline Mat4 ScaleMat4(const float s, const Mat4& a) -{ - Mat4 r; - ScaleVec4(r.v[0], s, a.v[0]); - ScaleVec4(r.v[1], s, a.v[1]); - ScaleVec4(r.v[2], s, a.v[2]); - ScaleVec4(r.v[3], s, a.v[3]); - return r; -} - -//---------------------------------------------------------------------------------------- -slInline float DeterminantOfMat4(const Mat4& a) -{ - // is this correct? - Vec4 cross_product; - CrossVec4(cross_product, a.v[1], a.v[2], a.v[3]); - return DotVec4(a.v[0], cross_product); -} - -//---------------------------------------------------------------------------------------- -slInline void TransposeMat4(Mat4& r, const Mat4& a) -{ - // a might be the same as r - r.v[0].x = a.v[0].x; - r.v[1].y = a.v[1].y; - r.v[2].z = a.v[2].z; - r.v[3].w = a.v[3].w; - - float temp; - temp = r.v[0].y; - r.v[0].y = r.v[1].x; - r.v[1].x = temp; - - temp = r.v[0].z; - r.v[0].z = r.v[2].x; - r.v[2].x = temp; - - temp = r.v[0].w; - r.v[0].w = r.v[3].x; - r.v[3].x = temp; - - temp = r.v[1].z; - r.v[1].z = r.v[2].y; - r.v[2].y = temp; - - temp = r.v[1].w; - r.v[1].w = r.v[3].y; - r.v[3].y = temp; - - temp = r.v[2].w; - r.v[2].w = r.v[3].z; - r.v[3].z = temp; -} - -//---------------------------------------------------------------------------------------- -slInline Mat4 TransposeMat4(const Mat4& a) -{ - Mat4 r; - r.v[0].x = a.v[0].x; r.v[0].y = a.v[1].x; r.v[0].z = a.v[2].x; r.v[0].w = a.v[3].x; - r.v[1].x = a.v[0].y; r.v[1].y = a.v[1].y; r.v[1].z = a.v[2].y; r.v[1].w = a.v[3].y; - r.v[2].x = a.v[0].z; r.v[2].y = a.v[1].z; r.v[2].z = a.v[2].z; r.v[2].w = a.v[3].z; - r.v[3].x = a.v[0].w; r.v[3].y = a.v[1].w; r.v[3].z = a.v[2].w; r.v[3].w = a.v[3].w; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void InvertAffineMat4(Mat4& r, const Mat4& a) -{ - // this may not be correct... - Mat3 a3_T(a.v[0].AsVec3(), a.v[1].AsVec3(), a.v[2].AsVec3()); - InvertAffineMat3(a3_T, a3_T); - - Vec3 transpose; - MulVec3ByMat3(transpose, a.v[3].AsVec3(), a3_T); - - SetMat4(r, a3_T, transpose); -} - -//---------------------------------------------------------------------------------------- -slInline Mat4 InvertAffineMat4(const Mat4& a) -{ - Mat4 r; - - // this may not be correct... - Mat3 a3_T(a.v[0].AsVec3(), a.v[1].AsVec3(), a.v[2].AsVec3()); - InvertAffineMat3(a3_T, a3_T); - - Vec3 transpose; - MulVec3ByMat3(transpose, a.v[3].AsVec3(), a3_T); - - SetMat4(r, a3_T, transpose); - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void InvertGeneralMat4(Mat4& r, const Mat4& a) -{ - // does this work? - float inv_determinant = 1.0f / DeterminantOfMat4(a); - - Mat4 a_T; - TransposeMat4(a_T, a); - CrossVec4(r.v[0], a_T.v[1], a_T.v[2], a_T.v[3]); - CrossVec4(r.v[1], a_T.v[2], a_T.v[3], a_T.v[0]); - CrossVec4(r.v[2], a_T.v[3], a_T.v[0], a_T.v[1]); - CrossVec4(r.v[3], a_T.v[0], a_T.v[1], a_T.v[2]); - - ScaleMat4(r, inv_determinant, r); -} - -//---------------------------------------------------------------------------------------- -slInline Mat4 InvertGeneralMat4(const Mat4& a) -{ - // does this work? - Mat4 r; - float inv_determinant = 1.0f / DeterminantOfMat4(a); - - Mat4 a_T; - TransposeMat4(a_T, a); - r.v[0] = CrossVec4(a_T.v[1], a_T.v[2], a_T.v[3]); - r.v[1] = CrossVec4(a_T.v[2], a_T.v[3], a_T.v[0]); - r.v[2] = CrossVec4(a_T.v[3], a_T.v[0], a_T.v[1]); - r.v[3] = CrossVec4(a_T.v[0], a_T.v[1], a_T.v[2]); - - return ScaleMat4(inv_determinant, r); -} - -//---------------------------------------------------------------------------------------- -slInline void MulMat4(Mat4& r, const Mat4& a, const Mat4& b) -{ - Mat4 temp; - - temp.v[0].x = DotVec4(a.v[0], b.v[0].x, b.v[1].x, b.v[2].x, b.v[3].x); - temp.v[0].y = DotVec4(a.v[0], b.v[0].y, b.v[1].y, b.v[2].y, b.v[3].y); - temp.v[0].z = DotVec4(a.v[0], b.v[0].z, b.v[1].z, b.v[2].z, b.v[3].z); - temp.v[0].w = DotVec4(a.v[0], b.v[0].w, b.v[1].w, b.v[2].w, b.v[3].w); - - temp.v[1].x = DotVec4(a.v[1], b.v[0].x, b.v[1].x, b.v[2].x, b.v[3].x); - temp.v[1].y = DotVec4(a.v[1], b.v[0].y, b.v[1].y, b.v[2].y, b.v[3].y); - temp.v[1].z = DotVec4(a.v[1], b.v[0].z, b.v[1].z, b.v[2].z, b.v[3].z); - temp.v[1].w = DotVec4(a.v[1], b.v[0].w, b.v[1].w, b.v[2].w, b.v[3].w); - - temp.v[2].x = DotVec4(a.v[2], b.v[0].x, b.v[1].x, b.v[2].x, b.v[3].x); - temp.v[2].y = DotVec4(a.v[2], b.v[0].y, b.v[1].y, b.v[2].y, b.v[3].y); - temp.v[2].z = DotVec4(a.v[2], b.v[0].z, b.v[1].z, b.v[2].z, b.v[3].z); - temp.v[2].w = DotVec4(a.v[2], b.v[0].w, b.v[1].w, b.v[2].w, b.v[3].w); - - temp.v[3].x = DotVec4(a.v[3], b.v[0].x, b.v[1].x, b.v[2].x, b.v[3].x); - temp.v[3].y = DotVec4(a.v[3], b.v[0].y, b.v[1].y, b.v[2].y, b.v[3].y); - temp.v[3].z = DotVec4(a.v[3], b.v[0].z, b.v[1].z, b.v[2].z, b.v[3].z); - temp.v[3].w = DotVec4(a.v[3], b.v[0].w, b.v[1].w, b.v[2].w, b.v[3].w); - - SetMat4(r, temp); -} - -//---------------------------------------------------------------------------------------- -slInline Mat4 MulMat4(const Mat4& a, const Mat4& b) -{ - Mat4 r; - - r.v[0].x = DotVec4(a.v[0], b.v[0].x, b.v[1].x, b.v[2].x, b.v[3].x); - r.v[0].y = DotVec4(a.v[0], b.v[0].y, b.v[1].y, b.v[2].y, b.v[3].y); - r.v[0].z = DotVec4(a.v[0], b.v[0].z, b.v[1].z, b.v[2].z, b.v[3].z); - r.v[0].w = DotVec4(a.v[0], b.v[0].w, b.v[1].w, b.v[2].w, b.v[3].w); - - r.v[1].x = DotVec4(a.v[1], b.v[0].x, b.v[1].x, b.v[2].x, b.v[3].x); - r.v[1].y = DotVec4(a.v[1], b.v[0].y, b.v[1].y, b.v[2].y, b.v[3].y); - r.v[1].z = DotVec4(a.v[1], b.v[0].z, b.v[1].z, b.v[2].z, b.v[3].z); - r.v[1].w = DotVec4(a.v[1], b.v[0].w, b.v[1].w, b.v[2].w, b.v[3].w); - - r.v[2].x = DotVec4(a.v[2], b.v[0].x, b.v[1].x, b.v[2].x, b.v[3].x); - r.v[2].y = DotVec4(a.v[2], b.v[0].y, b.v[1].y, b.v[2].y, b.v[3].y); - r.v[2].z = DotVec4(a.v[2], b.v[0].z, b.v[1].z, b.v[2].z, b.v[3].z); - r.v[2].w = DotVec4(a.v[2], b.v[0].w, b.v[1].w, b.v[2].w, b.v[3].w); - - r.v[3].x = DotVec4(a.v[3], b.v[0].x, b.v[1].x, b.v[2].x, b.v[3].x); - r.v[3].y = DotVec4(a.v[3], b.v[0].y, b.v[1].y, b.v[2].y, b.v[3].y); - r.v[3].z = DotVec4(a.v[3], b.v[0].z, b.v[1].z, b.v[2].z, b.v[3].z); - r.v[3].w = DotVec4(a.v[3], b.v[0].w, b.v[1].w, b.v[2].w, b.v[3].w); - - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void MulMat4ByTransposedMat4(Mat4& r, const Mat4& a, const Mat4& t) -{ - Mat4 temp; - - temp.v[0].x = DotVec4(a.v[0], t.v[0]); - temp.v[0].y = DotVec4(a.v[0], t.v[1]); - temp.v[0].z = DotVec4(a.v[0], t.v[2]); - temp.v[0].w = DotVec4(a.v[0], t.v[3]); - - temp.v[1].x = DotVec4(a.v[1], t.v[0]); - temp.v[1].y = DotVec4(a.v[1], t.v[1]); - temp.v[1].z = DotVec4(a.v[1], t.v[2]); - temp.v[1].w = DotVec4(a.v[1], t.v[3]); - - temp.v[2].x = DotVec4(a.v[2], t.v[0]); - temp.v[2].y = DotVec4(a.v[2], t.v[1]); - temp.v[2].z = DotVec4(a.v[2], t.v[2]); - temp.v[2].w = DotVec4(a.v[2], t.v[3]); - - temp.v[3].x = DotVec4(a.v[3], t.v[0]); - temp.v[3].y = DotVec4(a.v[3], t.v[1]); - temp.v[3].z = DotVec4(a.v[3], t.v[2]); - temp.v[3].w = DotVec4(a.v[3], t.v[3]); - - SetMat4(r, temp); -} - -//---------------------------------------------------------------------------------------- -slInline Mat4 MulMat4ByTransposedMat4(const Mat4& a, const Mat4& t) -{ - Mat4 r; - - r.v[0].x = DotVec4(a.v[0], t.v[0]); - r.v[0].y = DotVec4(a.v[0], t.v[1]); - r.v[0].z = DotVec4(a.v[0], t.v[2]); - r.v[0].w = DotVec4(a.v[0], t.v[3]); - - r.v[1].x = DotVec4(a.v[1], t.v[0]); - r.v[1].y = DotVec4(a.v[1], t.v[1]); - r.v[1].z = DotVec4(a.v[1], t.v[2]); - r.v[1].w = DotVec4(a.v[1], t.v[3]); - - r.v[2].x = DotVec4(a.v[2], t.v[0]); - r.v[2].y = DotVec4(a.v[2], t.v[1]); - r.v[2].z = DotVec4(a.v[2], t.v[2]); - r.v[2].w = DotVec4(a.v[2], t.v[3]); - - r.v[3].x = DotVec4(a.v[3], t.v[0]); - r.v[3].y = DotVec4(a.v[3], t.v[1]); - r.v[3].z = DotVec4(a.v[3], t.v[2]); - r.v[3].w = DotVec4(a.v[3], t.v[3]); - - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void MulVec4ByMat4(Vec4& r, const Vec4& v, const Mat4& a) -{ - Vec4 result; - ScaleVec4(result, v.x, a.v[0]); - ScaleAddVec4(result, v.y, a.v[1], result); - ScaleAddVec4(result, v.z, a.v[2], result); - ScaleAddVec4(result, v.w, a.v[3], result); - SetVec4(r, result); -} - -//---------------------------------------------------------------------------------------- -slInline Vec4 MulVec4ByMat4(const Vec4& v, const Mat4& a) -{ - Vec4 r; - ScaleVec4(r, v.x, a.v[0]); - ScaleAddVec4(r, v.y, a.v[1], r); - ScaleAddVec4(r, v.z, a.v[2], r); - ScaleAddVec4(r, v.w, a.v[3], r); - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void MulVec3ByMat4(Vec4& r, const Vec3& v, const float iw, const Mat4& a) -{ - Vec4 result; - ScaleVec4(result, v.x, a.v[0]); - ScaleAddVec4(result, v.y, a.v[1], result); - ScaleAddVec4(result, v.z, a.v[2], result); - ScaleAddVec4(result, iw, a.v[3], result); - SetVec4(r, result); -} - -//---------------------------------------------------------------------------------------- -slInline Vec4 MulVec3ByMat4(const Vec3& v, const float iw, const Mat4& a) -{ - Vec4 r; - ScaleVec4(r, v.x, a.v[0]); - ScaleAddVec4(r, v.y, a.v[1], r); - ScaleAddVec4(r, v.z, a.v[2], r); - ScaleAddVec4(r, iw, a.v[3], r); - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void MulVec4ByTransposedMat4(Vec4& r, const Vec4& v, const Mat4& t) -{ - float x = DotVec4(v, t.v[0]); - float y = DotVec4(v, t.v[1]); - float z = DotVec4(v, t.v[2]); - float w = DotVec4(v, t.v[3]); - SetVec4(r, x, y, z, w); -} - -//---------------------------------------------------------------------------------------- -slInline Vec4 MulVec4ByTransposedMat4(const Vec4& v, const Mat4& t) -{ - Vec4 r; - r.x = DotVec4(v, t.v[0]); - r.y = DotVec4(v, t.v[1]); - r.z = DotVec4(v, t.v[2]); - r.w = DotVec4(v, t.v[3]); - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void MulVec3ByTransposedMat4(Vec4& r, const Vec3& v, const float w, const Mat4& t) -{ - Vec4 v_new(v, w); - MulVec4ByTransposedMat4(r, v_new, t); -} - -//---------------------------------------------------------------------------------------- -slInline Vec4 MulVec3ByTransposedMat4(const Vec3& v, const float w, const Mat4& t) -{ - Vec4 v_as_v4(v, w); - return MulVec4ByTransposedMat4(v_as_v4, t); -} diff --git a/Classes/Foundation/Math/Quaternion.h b/Classes/Foundation/Math/Quaternion.h deleted file mode 100755 index d9cce5d..0000000 --- a/Classes/Foundation/Math/Quaternion.h +++ /dev/null @@ -1,100 +0,0 @@ -#pragma once - -#include "Foundation/Common/GlobalInclude.h" -#include "Foundation/Math/MathTypes.h" -#include "Foundation/Math/Vector.h" - -//---------------------------------------------------------------------------------------- -// Quaternion -//---------------------------------------------------------------------------------------- -void SetQuaternion(Quaternion& r, const float angle, const float x, const float y, const float z); -void SetQuaternion(Quaternion& r, const float angle, const Vec3& rotation_axis); -void ScaleQuaternion(Quaternion& r, const float s, const Quaternion& q); -void MulQuaternion(Quaternion& r, const Quaternion& p, const Quaternion& q); -void ConjugateOfQuaternion(Quaternion& r, const Quaternion& q); -float NormOfQuaternion(const Quaternion& q); -float NormSquaredOfQuaternion(const Quaternion& q); -void NormalizeQuaternion(Quaternion& r, Quaternion& q); -void InverseQuaterion(Quaternion& r, const Quaternion& q); - -//---------------------------------------------------------------------------------------- -void SetQuaternion(Quaternion& r, const float angle, const float x, const float y, const float z) -{ - Assert( EpsilonEquals(x*x + y*y + z*z, 0.0f, kSqrtEpsilon) ); - - float half_angle = angle * 0.5f; - float sin_half_angle = (float)Sinf(half_angle); - - r.i = x * sin_half_angle; - r.j = y * sin_half_angle; - r.k = z * sin_half_angle; - r.s = (float)Cosf(half_angle); -} - -//---------------------------------------------------------------------------------------- -void SetQuaternion(Quaternion& r, const float angle, const Vec3& rotation_axis) -{ - Assert( EpsilonEquals(rotation_axis.x * rotation_axis.x + rotation_axis.y * rotation_axis.y + rotation_axis.z * rotation_axis.z, 0.0f, kSqrtEpsilon) ); - - float half_angle = angle * 0.5f; - float sin_half_angle = (float)Sinf(half_angle); - - r.i = rotation_axis.x * sin_half_angle; - r.j = rotation_axis.y * sin_half_angle; - r.k = rotation_axis.z * sin_half_angle; - r.s = (float)Cosf(half_angle); -} - -//---------------------------------------------------------------------------------------- -void ScaleQuaternion(Quaternion& r, const float s, const Quaternion& q) -{ - r.i = q.i * s; - r.j = q.j * s; - r.k = q.k * s; - r.s = q.s * s; -} - -//---------------------------------------------------------------------------------------- -void MulQuaternion(Quaternion& r, const Quaternion& p, const Quaternion& q) -{ - r.i = p.s * q.i + p.i * q.s + p.j * q.k - p.k * q.j; - r.j = p.s * q.j + p.j * q.s + p.k * q.i - p.i * q.k; - r.k = p.s * q.k + p.k * q.s + p.i * q.j - p.j * q.i; - r.s = p.s * q.s - p.i * q.i - p.j * q.j - p.k * q.k; -} - -//---------------------------------------------------------------------------------------- -void ConjugateOfQuaternion(Quaternion& r, const Quaternion& q) -{ - r.i = -q.i; - r.j = -q.j; - r.k = -q.k; - r.s = q.s; -} - -//---------------------------------------------------------------------------------------- -float NormOfQuaternion(const Quaternion& q) -{ - return (float)Sqrtf( q.i * q.i + q.j * q.j + q.k * q.k + q.s * q.s); -} - -//---------------------------------------------------------------------------------------- -float NormSquaredOfQuaternion(const Quaternion& q) -{ - return q.i * q.i + q.j * q.j + q.k * q.k + q.s * q.s; -} - -//---------------------------------------------------------------------------------------- -void NormalizeQuaternion(Quaternion& r, Quaternion& q) -{ - float norm_scale = 1.0f / NormOfQuaternion( q ); - ScaleQuaternion( r, norm_scale, q ); -} - -//---------------------------------------------------------------------------------------- -void InverseQuaterion(Quaternion& r, const Quaternion& q) -{ - ConjugateOfQuaternion( r, q ); - float inv_norm_squared = 1.0f / NormSquaredOfQuaternion( q ); - ScaleQuaternion( r, inv_norm_squared, q ); -} diff --git a/Classes/Foundation/Math/Vector.h b/Classes/Foundation/Math/Vector.h deleted file mode 100755 index 91cc817..0000000 --- a/Classes/Foundation/Math/Vector.h +++ /dev/null @@ -1,1176 +0,0 @@ -#pragma once - -#include "Foundation/Common/GlobalInclude.h" -#include "Foundation/Math/MathTypes.h" -#include "Foundation/Math/MathOperations.h" - -//---------------------------------------------------------------------------------------- -// Vec2 -//---------------------------------------------------------------------------------------- -void SetVec2(Vec2& r, const Vec2& v); -void SetVec2(Vec2& r, const float x, const float y); -void SetVec2(Vec2& r, const float xy); -void AbsVec2(Vec2& r, const Vec2& v); -float MinComponentVec2(const Vec2& v); -float MaxComponentVec2(const Vec2& v); -uint32_t MinIndexVec2(const Vec2& v); -uint32_t MaxIndexVec2(const Vec2& v); -float DotVec2(const Vec2& a, const Vec2& b); -float DotVec2(const Vec2& a, const float x, const float y); - -void PerpendicularVec2(Vec2& r, const Vec2& v); -Vec2 PerpendicularVec2(const Vec2& v); - -void AddVec2ByScalar(Vec2& r, const float s, const Vec2& v); -Vec2 AddVec2ByScalar(const float s, const Vec2& v); - -void AddVec2(Vec2& r, const Vec2& a, const Vec2& b); -Vec2 AddVec2(const Vec2& a, const Vec2& b); - -void AddVec2(Vec2& r, const Vec2& a, const float x, const float y); -Vec2 AddVec2(const Vec2& a, const float x, const float y); - -void SubVec2(Vec2& r, const Vec2& a, const Vec2& b); -Vec2 SubVec2(const Vec2& a, const Vec2& b); - -void SubVec2(Vec2& r, const Vec2& a, const float x, const float y); -Vec2 SubVec2(const Vec2& a, const float x, const float y); - -void ScaleVec2(Vec2& r, const float s, const Vec2& v); -Vec2 ScaleVec2(const float s, const Vec2& v); - -void MulVec2(Vec2& r, const Vec2& a, const Vec2& b); -Vec2 MulVec2(const Vec2& a, const Vec2& b); - -void MulVec2(Vec2& r, const Vec2& a, const float x, const float y); -Vec2 MulVec2(const Vec2& a, const float x, const float y); - -void DivVec2(Vec2& r, const Vec2& a, const Vec2& b); -Vec2 DivVec2(const Vec2& a, const Vec2& b); - -void DivVec2(Vec2& r, const Vec2& a, const float x, const float y); -Vec2 DivVec2(const Vec2& a, const float x, const float y); - -void ScaleAddVec2(Vec2& r, const float s, const Vec2& v_scale, const Vec2& v_add); -Vec2 ScaleAddVec2(const float s, const Vec2& v_scale, const Vec2& v_add); - -void NormalizeVec2(Vec2& r, const Vec2& a); -Vec2 NormalizeVec2(const Vec2& a); - -float LengthOfVec2(const Vec2& a); -float LengthSquaredOfVec2(const Vec2& a); - -//---------------------------------------------------------------------------------------- -slInline void SetVec2(Vec2& r, const Vec2& v) -{ - r.x = v.x; - r.y = v.y; -} - -//---------------------------------------------------------------------------------------- -slInline void SetVec2(Vec2& r, const float x, const float y) -{ - r.x = x; - r.y = y; -} - -//---------------------------------------------------------------------------------------- -slInline void SetVec2(Vec2& r, const float xy) -{ - r.x = xy; - r.y = xy; -} - -//---------------------------------------------------------------------------------------- -void AbsVec2(Vec2& r, const Vec2& v) -{ - r.x = Absf(v.x); - r.y = Absf(v.y); -} - -//---------------------------------------------------------------------------------------- -float MinComponentVec2(const Vec2& v) -{ - return v.x <= v.y ? v.x : v.y; -} - -//---------------------------------------------------------------------------------------- -float MaxComponentVec2(const Vec2& v) -{ - return v.x >= v.y ? v.x : v.y; -} - -//---------------------------------------------------------------------------------------- -uint32_t MinIndexVec2(const Vec2& v) -{ - return v.x <= v.y ? 0 : 1; -} - -//---------------------------------------------------------------------------------------- -uint32_t MaxIndexVec2(const Vec2& v) -{ - return v.x >= v.y ? 0 : 1; -} - -//---------------------------------------------------------------------------------------- -slInline float DotVec2(const Vec2& a, const Vec2& b) -{ - return a.x * b.x + a.y * b.y; -} - -//---------------------------------------------------------------------------------------- -slInline float DotVec2(const Vec2& a, const float x, const float y) -{ - return a.x * x + a.y * y; -} - -//---------------------------------------------------------------------------------------- -slInline void PerpendicularVec2(Vec2& r, const Vec2& v) -{ - r.x = -v.y; - r.y = v.x; -} - -//---------------------------------------------------------------------------------------- -slInline Vec2 PerpendicularVec2(const Vec2& v) -{ - Vec2 r; - r.x = -v.y; - r.y = v.x; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void AddVec2ByScalar(Vec2& r, const float s, Vec2& v) -{ - r.x = s + v.x; - r.y = s + v.y; -} - -//---------------------------------------------------------------------------------------- -slInline Vec2 AddVec2ByScalar(const float s, const Vec2& v) -{ - Vec2 r; - r.x = s + v.x; - r.y = s + v.y; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void AddVec2(Vec2& r, const Vec2& a, const Vec2& b) -{ - r.x = a.x + b.x; - r.y = a.y + b.y; -} - -//---------------------------------------------------------------------------------------- -slInline Vec2 AddVec2(const Vec2& a, const Vec2& b) -{ - Vec2 r; - r.x = a.x + b.x; - r.y = a.y + b.y; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void AddVec2(Vec2& r, const Vec2& a, const float x, const float y) -{ - r.x = a.x + x; - r.y = a.y + y; -} - -//---------------------------------------------------------------------------------------- -slInline Vec2 AddVec2(const Vec2& a, const float x, const float y) -{ - Vec2 r; - r.x = a.x + x; - r.y = a.y + y; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void SubVec2(Vec2& r, const Vec2& a, const Vec2& b) -{ - r.x = a.x - b.x; - r.y = a.y - b.y; -} - -//---------------------------------------------------------------------------------------- -slInline Vec2 SubVec2(const Vec2& a, const Vec2& b) -{ - Vec2 r; - r.x = a.x - b.x; - r.y = a.y - b.y; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void SubVec2(Vec2& r, const Vec2& a, const float x, const float y) -{ - r.x = a.x - x; - r.y = a.y - y; -} - -//---------------------------------------------------------------------------------------- -slInline Vec2 SubVec2(const Vec2& a, const float x, const float y) -{ - Vec2 r; - r.x = a.x - x; - r.y = a.y - y; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void ScaleVec2(Vec2& r, const float s, const Vec2& v) -{ - r.x = s * v.x; - r.y = s * v.y; -} - -//---------------------------------------------------------------------------------------- -slInline Vec2 ScaleVec2(const float s, const Vec2& v) -{ - Vec2 r; - r.x = s * v.x; - r.y = s * v.y; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void MulVec2(Vec2& r, const Vec2& a, const Vec2& b) -{ - r.x = a.x * b.x; - r.y = a.y * b.y; -} - -//---------------------------------------------------------------------------------------- -slInline Vec2 MulVec2(const Vec2& a, const Vec2& b) -{ - Vec2 r; - r.x = a.x * b.x; - r.y = a.y * b.y; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void MulVec2(Vec2& r, const Vec2& a, const float x, const float y) -{ - r.x = a.x * x; - r.y = a.y * y; -} - -//---------------------------------------------------------------------------------------- -slInline Vec2 MulVec2(const Vec2& a, const float x, const float y) -{ - Vec2 r; - r.x = a.x * x; - r.y = a.y * y; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void DivVec2(Vec2& r, const Vec2& a, const Vec2& b) -{ - r.x = a.x / b.x; - r.y = a.y / b.y; -} - -//---------------------------------------------------------------------------------------- -slInline Vec2 DivVec2(const Vec2& a, const Vec2& b) -{ - Vec2 r; - r.x = a.x / b.x; - r.y = a.y / b.y; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void DivVec2(Vec2& r, const Vec2& a, const float x, const float y) -{ - r.x = a.x / x; - r.y = a.y / y; -} - -//---------------------------------------------------------------------------------------- -slInline Vec2 DivVec2(const Vec2& a, const float x, const float y) -{ - Vec2 r; - r.x = a.x / x; - r.y = a.y / y; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void ScaleAddVec2(Vec2& r, const float s, const Vec2& v_scale, const Vec2& v_add) -{ - r.x = v_scale.x * s + v_add.x; - r.y = v_scale.y * s + v_add.y; -} - -//---------------------------------------------------------------------------------------- -slInline Vec2 ScaleAddVec2(const float s, const Vec2& v_scale, const Vec2& v_add) -{ - Vec2 r; - r.x = v_scale.x * s + v_add.x; - r.y = v_scale.y * s + v_add.y; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void NormalizeVec2(Vec2& r, const Vec2& a) -{ - float scale = 1.0f / LengthOfVec2(a); - ScaleVec2( r, scale, a ); -} - -//---------------------------------------------------------------------------------------- -slInline Vec2 NormalizeVec2(const Vec2& a) -{ - float scale = 1.0f / LengthOfVec2(a); - return ScaleVec2( scale, a ); -} - -//---------------------------------------------------------------------------------------- -slInline float LengthOfVec2(const Vec2& a) -{ - return (float)Sqrtf( DotVec2(a, a) ); -} - -//---------------------------------------------------------------------------------------- -slInline float LengthSquaredOfVec2(const Vec2& a) -{ - return DotVec2(a, a); -} - - - -//---------------------------------------------------------------------------------------- -// Vec3 -//---------------------------------------------------------------------------------------- -void SetVec3(Vec3& r, const Vec3& v); -void SetVec3(Vec3& r, const Vec2& v, const float z); -void SetVec3(Vec3& r, const float xyz); -void SetVec3(Vec3& r, const float x, const float y, const float z); - -void AbsVec3(Vec3& r, const Vec3& v); -Vec3 AbsVec3(const Vec3& v); - -float MinComponentVec3(const Vec3& v); -float MaxComponentVec3(const Vec3& v); -uint32_t MinIndexVec3(const Vec3& v); -uint32_t MaxIndexVec3(const Vec3& v); -float DotVec3(const Vec3& a, const Vec3& b); -float DotVec3(const Vec3& a, const float x, const float y, const float z); - -void CrossVec3(Vec3& r, const Vec3& a, const Vec3& b); -Vec3 CrossVec3(const Vec3& a, const Vec3& b); - -void AddVec3ByScalar(Vec3& r, const float s, Vec3& v); -Vec3 AddVec3ByScalar(const float s, Vec3& v); - -void AddVec3(Vec3& r, const Vec3& a, const Vec3& b); -Vec3 AddVec3(const Vec3& a, const Vec3& b); - -void AddVec3(Vec3& r, const Vec3& a, const float x, const float y, const float z); -Vec3 AddVec3(const Vec3& a, const float x, const float y, const float z); - -void SubVec3(Vec3& r, const Vec3& a, const Vec3& b); -Vec3 SubVec3(const Vec3& a, const Vec3& b); - -void SubVec3(Vec3& r, const Vec3& a, const float x, const float y, const float z); -Vec3 SubVec3(const Vec3& a, const float x, const float y, const float z); - -void ScaleVec3(Vec3& r, const float s, const Vec3& v); -Vec3 ScaleVec3(const float s, const Vec3& v); - -void MulVec3(Vec3& r, const Vec3& a, const Vec3& b); -Vec3 MulVec3(const Vec3& a, const Vec3& b); - -void MulVec3(Vec3& r, const Vec3& a, const float x, const float y, const float z); -Vec3 MulVec3(const Vec3& a, const float x, const float y, const float z); - -void DivVec3(Vec3& r, const Vec3& a, const Vec3& b); -Vec3 DivVec3(const Vec3& a, const Vec3& b); - -void DivVec3(Vec3& r, const Vec3& a, const float x, const float y, const float z); -Vec3 DivVec3(const Vec3& a, const float x, const float y, const float z); - -void ScaleAddVec3(Vec3& r, const float s, const Vec3& v_scale, const Vec3& v_add); -Vec3 ScaleAddVec3(const float s, const Vec3& v_scale, const Vec3& v_add); - -void NormalizeVec3(Vec3& r, const Vec3& a); -Vec3 NormalizeVec3(const Vec3& a); - -float LengthOfVec3(const Vec3& a); -float LengthSquaredVec3(const Vec3& a); - -//---------------------------------------------------------------------------------------- -slInline void SetVec3(Vec3& r, const Vec3& v) -{ - r.x = v.x; - r.y = v.y; - r.z = v.z; -} - -//---------------------------------------------------------------------------------------- -slInline void SetVec3(Vec3& r, const Vec2& v, const float z) -{ - r.x = v.x; - r.y = v.y; - r.z = z; -} - -//---------------------------------------------------------------------------------------- -slInline void SetVec3(Vec3& r, const float xyz) -{ - r.x = xyz; - r.y = xyz; - r.z = xyz; -} - -//---------------------------------------------------------------------------------------- -slInline void SetVec3(Vec3& r, const float x, const float y, const float z) -{ - r.x = x; - r.y = y; - r.z = z; -} - -//---------------------------------------------------------------------------------------- -slInline void AbsVec3(Vec3& r, const Vec3& v) -{ - r.x = Absf(v.x); - r.y = Absf(v.y); - r.z = Absf(v.z); -} - -//---------------------------------------------------------------------------------------- -slInline Vec3 AbsVec3(const Vec3& v) -{ - Vec3 r; - r.x = Absf(v.x); - r.y = Absf(v.y); - r.z = Absf(v.z); - return r; -} - -//---------------------------------------------------------------------------------------- -slInline float MinComponentVec3(const Vec3& v) -{ - float xy = v.x <= v.y ? v.x : v.y; - return xy <= v.z ? xy : v.z; -} - -//---------------------------------------------------------------------------------------- -slInline float MaxComponentVec3(const Vec3& v) -{ - float xy = v.x >= v.y ? v.x : v.y; - return xy >= v.z ? xy : v.z; -} - -//---------------------------------------------------------------------------------------- -slInline uint32_t MinIndexVec3(const Vec3& v) -{ - const float* v_array = &v.x; - uint32_t xy = v.x <= v.y ? 0 : 1; - return v_array[xy] <= v.z ? xy : 2; -} - -//---------------------------------------------------------------------------------------- -slInline uint32_t MaxIndexVec3(const Vec3& v) -{ - const float* v_array = &v.x; - uint32_t xy = v.x >= v.y ? 0 : 1; - return v_array[xy] >= v.z ? xy : 2; -} - -//---------------------------------------------------------------------------------------- -slInline float DotVec3(const Vec3& a, const Vec3& b) -{ - return a.x * b.x + a.y * b.y + a.z * b.z; -} - -//---------------------------------------------------------------------------------------- -slInline float DotVec3(const Vec3& a, const float x, const float y, const float z) -{ - return a.x * x + a.y * y + a.z * z; -} - -//---------------------------------------------------------------------------------------- -slInline void CrossVec3(Vec3& r, const Vec3& a, const Vec3& b) -{ - float x = a.y * b.z - b.y * a.z; - float y = a.z * b.x - b.z * a.x; - float z = a.x * b.y - b.x * a.y; - r.x = x; - r.y = y; - r.z = z; -} - -//---------------------------------------------------------------------------------------- -slInline Vec3 CrossVec3(const Vec3& a, const Vec3& b) -{ - Vec3 r; - r.x = a.y * b.z - b.y * a.z; - r.y = a.z * b.x - b.z * a.x; - r.z = a.x * b.y - b.x * a.y; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void AddVec3ByScalar(Vec3& r, const float s, Vec3& v) -{ - r.x = s + v.x; - r.y = s + v.y; - r.z = s + v.z; -} - -//---------------------------------------------------------------------------------------- -slInline Vec3 AddVec3ByScalar(const float s, Vec3& v) -{ - Vec3 r; - r.x = s + v.x; - r.y = s + v.y; - r.z = s + v.z; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void AddVec3(Vec3& r, const Vec3& a, const Vec3& b) -{ - r.x = a.x + b.x; - r.y = a.y + b.y; - r.z = a.z + b.z; -} - -//---------------------------------------------------------------------------------------- -slInline Vec3 AddVec3(const Vec3& a, const Vec3& b) -{ - Vec3 r; - r.x = a.x + b.x; - r.y = a.y + b.y; - r.z = a.z + b.z; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void AddVec3(Vec3& r, const Vec3& a, const float x, const float y, const float z) -{ - r.x = a.x + x; - r.y = a.y + y; - r.z = a.z + z; -} - -//---------------------------------------------------------------------------------------- -slInline Vec3 AddVec3(const Vec3& a, const float x, const float y, const float z) -{ - Vec3 r; - r.x = a.x + x; - r.y = a.y + y; - r.z = a.z + z; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void SubVec3(Vec3& r, const Vec3& a, const Vec3& b) -{ - r.x = a.x - b.x; - r.y = a.y - b.y; - r.z = a.z - b.z; -} - -//---------------------------------------------------------------------------------------- -slInline Vec3 SubVec3(const Vec3& a, const Vec3& b) -{ - Vec3 r; - r.x = a.x - b.x; - r.y = a.y - b.y; - r.z = a.z - b.z; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline Vec3 SubVec3(const Vec3& a, const float x, const float y, const float z) -{ - Vec3 r; - r.x = a.x - x; - r.y = a.y - y; - r.z = a.z - z; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void ScaleVec3(Vec3& r, const float s, const Vec3& v) -{ - r.x = s * v.x; - r.y = s * v.y; - r.z = s * v.z; -} - -//---------------------------------------------------------------------------------------- -slInline Vec3 ScaleVec3(const float s, const Vec3& v) -{ - Vec3 r; - r.x = s * v.x; - r.y = s * v.y; - r.z = s * v.z; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void MulVec3(Vec3& r, const Vec3& a, const Vec3& b) -{ - r.x = a.x * b.x; - r.y = a.y * b.y; - r.z = a.z * b.z; -} - -//---------------------------------------------------------------------------------------- -slInline Vec3 MulVec3(const Vec3& a, const Vec3& b) -{ - Vec3 r; - r.x = a.x * b.x; - r.y = a.y * b.y; - r.z = a.z * b.z; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void MulVec3(Vec3& r, const Vec3& a, const float x, const float y, const float z) -{ - r.x = a.x * x; - r.y = a.y * y; - r.z = a.z * z; -} - -//---------------------------------------------------------------------------------------- -slInline Vec3 MulVec3(const Vec3& a, const float x, const float y, const float z) -{ - Vec3 r; - r.x = a.x * x; - r.y = a.y * y; - r.z = a.z * z; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void DivVec3(Vec3& r, const Vec3& a, const Vec3& b) -{ - r.x = a.x / b.x; - r.y = a.y / b.y; - r.z = a.z / b.z; -} - -//---------------------------------------------------------------------------------------- -slInline Vec3 DivVec3(const Vec3& a, const Vec3& b) -{ - Vec3 r; - r.x = a.x / b.x; - r.y = a.y / b.y; - r.z = a.z / b.z; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void DivVec3(Vec3& r, const Vec3& a, const float x, const float y, const float z) -{ - r.x = a.x / x; - r.y = a.y / y; - r.z = a.z / z; -} - -//---------------------------------------------------------------------------------------- -slInline Vec3 DivVec3(const Vec3& a, const float x, const float y, const float z) -{ - Vec3 r; - r.x = a.x / x; - r.y = a.y / y; - r.z = a.z / z; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void ScaleAddVec3(Vec3& r, const float s, const Vec3& v_scale, const Vec3& v_add) -{ - r.x = s * v_scale.x + v_add.x; - r.y = s * v_scale.y + v_add.y; - r.z = s * v_scale.z + v_add.z; -} - -//---------------------------------------------------------------------------------------- -slInline Vec3 ScaleAddVec3(const float s, const Vec3& v_scale, const Vec3& v_add) -{ - Vec3 r; - r.x = s * v_scale.x + v_add.x; - r.y = s * v_scale.y + v_add.y; - r.z = s * v_scale.z + v_add.z; - return r; -} - -//---------------------------------------------------------------------------------------- -slInline void NormalizeVec3(Vec3& r, const Vec3& a) -{ - float scale = 1.0f / LengthOfVec3( a ); - ScaleVec3( r, scale, a ); -} - -//---------------------------------------------------------------------------------------- -slInline Vec3 NormalizeVec3(const Vec3& a) -{ - float scale = 1.0f / LengthOfVec3( a ); - return ScaleVec3( scale, a ); -} - -//---------------------------------------------------------------------------------------- -slInline float LengthOfVec3(const Vec3& a) -{ - return (float)Sqrtf( DotVec3(a, a) ); -} - -//---------------------------------------------------------------------------------------- -slInline float LengthSquaredOfVec3(const Vec3& a) -{ - return DotVec3( a, a ); -} - - - -//---------------------------------------------------------------------------------------- -// Vec4 -//---------------------------------------------------------------------------------------- -void SetVec4(Vec4& r, const Vec4& v); -void SetVec4(Vec4& r, const Vec2& a, const Vec2& b); -void SetVec4(Vec4& r, const Vec3& v, const float w); -void SetVec4(Vec4& r, const float xyzw); -void SetVec4(Vec4& r, const float x, const float y, const float z, const float w); - -void AbsVec4(Vec4& r, const Vec4& v); -Vec4 AbsVec4(const Vec4& v); - -float MinComponentVec4(const Vec4& v); -float MaxComponentVec4(const Vec4& v); -uint32_t MinIndexVec4(const Vec4& v); -uint32_t MaxIndexVec4(const Vec4& v); -float DotVec4(const Vec4& a, const Vec4& b); -float DotVec4(const Vec4& a, const float x, const float y, const float z, const float w); - -void CrossVec4(Vec4& r, const Vec4& a, const Vec4& b, const Vec4& c); -Vec4 CrossVec4(const Vec4& a, const Vec4& b, const Vec4& c); - -void AddVec4ByScalar(Vec4& r, const float s, Vec4& v); -Vec4 AddVec4ByScalar(const float s, Vec4& v); - -void AddVec4(Vec4& r, const Vec4& a, const Vec4& b); -Vec4 AddVec4(const Vec4& a, const Vec4& b); - -void AddVec4(Vec4& r, const Vec4& a, const float x, const float y, const float z, const float w); -Vec4 AddVec4(const Vec4& a, const float x, const float y, const float z, const float w); - -void SubVec4(Vec4& r, const Vec4& a, const Vec4& b); -Vec4 SubVec4(const Vec4& a, const Vec4& b); - -void SubVec4(Vec4& r, const Vec4& a, const float x, const float y, const float z, const float w); -Vec4 SubVec4(const Vec4& a, const float x, const float y, const float z, const float w); - -void ScaleVec4(Vec4& r, const float s, const Vec4& v); -Vec4 ScaleVec4(const float s, const Vec4& v); - -void MulVec4(Vec4& r, const Vec4& a, const Vec4& b); -Vec4 MulVec4(const Vec4& a, const Vec4& b); - -void MulVec4(Vec4& r, const Vec4& a, const float x, const float y, const float z, const float w); -Vec4 MulVec4(const Vec4& a, const float x, const float y, const float z, const float w); - -void DivVec4(Vec4& r, const Vec4& a, const Vec4& b); -Vec4 DivVec4(const Vec4& a, const Vec4& b); - -void DivVec4(Vec4& r, const Vec4& a, const float x, const float y, const float z, const float w); -Vec4 DivVec4(const Vec4& a, const float x, const float y, const float z, const float w); - -void ScaleAddVec4(Vec4& r, const float s, const Vec4& v_scale, const Vec4& v_add); -Vec4 ScaleAddVec4(const float s, const Vec4& v_scale, const Vec4& v_add); - -void NormalizeVec4(Vec4& r, const Vec4& a); -Vec4 NormalizeVec4(const Vec4& a); - -float LengthOfVec4(const Vec4& a); -float LengthSquaredVec4(const Vec4& a); - -//---------------------------------------------------------------------------------------- -slInline void SetVec4(Vec4& r, const Vec4& v) -{ - r.x = v.x; - r.y = v.y; - r.z = v.z; - r.w = v.w; -} - -//---------------------------------------------------------------------------------------- -slInline void SetVec4(Vec4& r, const Vec2& a, const Vec2& b) -{ - r.x = a.x; - r.y = a.y; - r.z = b.x; - r.w = b.y; -} - -//---------------------------------------------------------------------------------------- -slInline void SetVec4(Vec4& r, const Vec3& v, const float w) -{ - r.x = v.x; - r.y = v.y; - r.z = v.z; - r.w = w; -} - -//---------------------------------------------------------------------------------------- -slInline void SetVec4(Vec4& r, const float xyzw) -{ - r.x = xyzw; - r.y = xyzw; - r.z = xyzw; - r.w = xyzw; -} - -//---------------------------------------------------------------------------------------- -slInline void SetVec4(Vec4& r, const float x, const float y, const float z, const float w) -{ - r.x = x; - r.y = y; - r.z = z; - r.w = w; -} - -//---------------------------------------------------------------------------------------- -slInline void AbsVec4(Vec4& r, const Vec4& v) -{ - r.x = Absf(v.x); - r.y = Absf(v.y); - r.z = Absf(v.z); - r.w = Absf(v.w); -} - -//---------------------------------------------------------------------------------------- -slInline Vec4 AbsVec4(const Vec4& v) -{ - Vec4 r; - r.x = Absf(v.x); - r.y = Absf(v.y); - r.z = Absf(v.z); - r.w = Absf(v.w); - return r; -} - -//---------------------------------------------------------------------------------------- -slInline float MinComponentVec4(const Vec4& v) -{ - float xy = v.x <= v.y ? v.x : v.y; - float zw = v.z <= v.w ? v.z : v.w; - return xy <= zw ? xy : zw; -} - -//---------------------------------------------------------------------------------------- -slInline float MaxComponentVec4(const Vec4& v) -{ - float xy = v.x >= v.y ? v.x : v.y; - float zw = v.z >= v.w ? v.z : v.w; - return xy >= zw ? xy : zw; -} - -//--------------------------------------