CodeForge QQ客服 CodeForge 400电话 客服电话 4006316121
首页 » 源代码 » Obj模型加载并显示 » OpenObjModel/include/ArcBall.h

OpenObjModel/include/ArcBall.h ( 文件浏览 )

文件源自:Obj模型加载并显示
  • 979395mmm 发布于2018-04-14
  • 浏览次数:0
  • 下载次数:0
  • 下载需 1 积分
  • 侵权举报
			////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////ArcBall.h///////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _ArcBall_h
#define _ArcBall_h

#include <stdlib.h>

// 仅在Debug模式下,启用断言
#ifdef _DEBUG
# include "assert.h"
#else
# define assert(x) {
 
}
#endif


//2维点
typedef union Tuple2f_t
{

	struct
	{

		GLfloat X, Y;
	
} s;

	GLfloat T[2];

} Tuple2fT;    

//3维点
typedef union Tuple3f_t
{

	struct
	{

		GLfloat X, Y, Z;
	
} s;

	GLfloat T[3];

} Tuple3fT;     

//4维点
typedef union Tuple4f_t
{

	struct
	{

		GLfloat X, Y, Z, W;
	
} s;

	GLfloat T[4];

} Tuple4fT;     


//3x3 矩阵
typedef union Matrix3f_t
{

	struct
	{

		//column major
		union {
 GLfloat M00; GLfloat XX; GLfloat SX; 
}; 
		union {
 GLfloat M10; GLfloat XY;             
}; 
		union {
 GLfloat M20; GLfloat XZ;             
}; 
		union {
 GLfloat M01; GLfloat YX;             
}; 
		union {
 GLfloat M11; GLfloat YY; GLfloat SY; 
}; 
		union {
 GLfloat M21; GLfloat YZ;             
}; 
		union {
 GLfloat M02; GLfloat ZX;             
}; 
		union {
 GLfloat M12; GLfloat ZY;             
}; 
		union {
 GLfloat M22; GLfloat ZZ; GLfloat SZ; 
}; 
	
} s;
	GLfloat M[9];

} Matrix3fT;   

//4x4 矩阵
typedef union Matrix4f_t
{

	struct
	{

		//column major
		union {
 GLfloat M00; GLfloat XX; GLfloat SX; 
}; 
		union {
 GLfloat M10; GLfloat XY;             
}; 
		union {
 GLfloat M20; GLfloat XZ;             
}; 
		union {
 GLfloat M30; GLfloat XW;             
}; 
		union {
 GLfloat M01; GLfloat YX;             
}; 
		union {
 GLfloat M11; GLfloat YY; GLfloat SY; 
}; 
		union {
 GLfloat M21; GLfloat YZ;             
}; 
		union {
 GLfloat M31; GLfloat YW;             
}; 
		union {
 GLfloat M02; GLfloat ZX;             
}; 
		union {
 GLfloat M12; GLfloat ZY;             
}; 
		union {
 GLfloat M22; GLfloat ZZ; GLfloat SZ; 
}; 
		union {
 GLfloat M32; GLfloat ZW;             
}; 
		union {
 GLfloat M03; GLfloat TX;             
}; 
		union {
 GLfloat M13; GLfloat TY;             
}; 
		union {
 GLfloat M23; GLfloat TZ;             
}; 
		union {
 GLfloat M33; GLfloat TW; GLfloat SW; 
}; 
	
} s;
	GLfloat M[16];

} Matrix4fT;   


//定义类型的别名
#define Point2fT    Tuple2fT  
#define Quat4fT     Tuple4fT  
#define Vector2fT   Tuple2fT  
#define Vector3fT   Tuple3fT  
#define FuncSqrt    sqrtf
# define Epsilon 1.0e-5

//2维点相加
inline
static void Point2fAdd(Point2fT* NewObj, const Tuple2fT* t1)
{

	assert(NewObj && t1);

	NewObj->s.X += t1->s.X;
	NewObj->s.Y += t1->s.Y;

}

//2 维点相减
inline
static void Point2fSub(Point2fT* NewObj, const Tuple2fT* t1)
{

	assert(NewObj && t1);

	NewObj->s.X -= t1->s.X;
	NewObj->s.Y -= t1->s.Y;

}

//3 维点矢积
inline
static void Vector3fCross(Vector3fT* NewObj, const Vector3fT* v1, const Vector3fT* v2)
{

	Vector3fT Result;

	assert(NewObj && v1 && v2);

	Result.s.X = (v1->s.Y * v2->s.Z) - (v1->s.Z * v2->s.Y);
	Result.s.Y = (v1->s.Z * v2->s.X) - (v1->s.X * v2->s.Z);
	Result.s.Z = (v1->s.X * v2->s.Y) - (v1->s.Y * v2->s.X);

	*NewObj = Result;

}

//3维点点积
inline
static GLfloat Vector3fDot(const Vector3fT* NewObj, const Vector3fT* v1)
{

	assert(NewObj && v1);

	return  (NewObj->s.X * v1->s.X) +
		(NewObj->s.Y * v1->s.Y) +
		(NewObj->s.Z * v1->s.Z);

}

//3维点的长度的平方
inline
static GLfloat Vector3fLengthSquared(const Vector3fT* NewObj)
{

	assert(NewObj);

	return  (NewObj->s.X * NewObj->s.X) +
		(NewObj->s.Y * NewObj->s.Y) +
		(NewObj->s.Z * NewObj->s.Z);

}

//3维点的长度
inline
static GLfloat Vector3fLength(const Vector3fT* NewObj)
{

	assert(NewObj);

	return FuncSqrt(Vector3fLengthSquared(NewObj));

}

//设置3x3矩阵为0 矩阵
inline
static void Matrix3fSetZero(Matrix3fT* NewObj)
{

	NewObj->s.M00 = NewObj->s.M01 = NewObj->s.M02 =
		NewObj->s.M10 = NewObj->s.M11 = NewObj->s.M12 =
		NewObj->s.M20 = NewObj->s.M21 = NewObj->s.M22 = 0.0f;

}

// 设置4x4矩阵为0矩阵
inline
static void Matrix4fSetZero(Matrix4fT* NewObj)
{

	NewObj->s.M00 = NewObj->s.M01 = NewObj->s.M02 =
		NewObj->s.M10 = NewObj->s.M11 = NewObj->s.M12 =
		NewObj->s.M20 = NewObj->s.M21 = NewObj->s.M22 =
		NewObj->s.M30 = NewObj->s.M31 = NewObj->s.M32 = 0.0f;

}

// 设置3x3矩阵为单位矩阵
inline
static void Matrix3fSetIdentity(Matrix3fT* NewObj)
{

	Matrix3fSetZero(NewObj);

	NewObj->s.M00 =
		NewObj->s.M11 =
		NewObj->s.M22 = 1.0f;

}

// 设置4x4矩阵为单位矩阵
inline
static void Matrix4fSetIdentity(Matrix4fT* NewObj)
{

	Matrix4fSetZero(NewObj);

	NewObj->s.M00 = 1.0f;
	NewObj->s.M11 = 1.0f;
	NewObj->s.M22 = 1.0f;
	NewObj->s.M33=1.0f;

}

//从四元数设置旋转矩阵
inline
static void Matrix3fSetRotationFromQuat4f(Matrix3fT* NewObj, const Quat4fT* q1)
{

	GLfloat n, s;
	GLfloat xs, ys, zs;
	GLfloat wx, wy, wz;
	GLfloat xx, xy, xz;
	GLfloat yy, yz, zz;

	assert(NewObj && q1);

	n = (q1->s.X * q1->s.X) + (q1->s.Y * q1->s.Y) + (q1->s.Z * q1->s.Z) + (q1->s.W * q1->s.W);
	s = (n > 0.0f) ? (2.0f / n) : 0.0f;

	xs = q1->s.X * s;  ys = q1->s.Y * s;  zs = q1->s.Z * s;
	wx = q1->s.W * xs; wy = q1->s.W * ys; wz = q1->s.W * zs;
	xx = q1->s.X * xs; xy = q1->s.X * ys; xz = q1->s.X * zs;
	yy = q1->s.Y * ys; yz = q1->s.Y * zs; zz = q1->s.Z * zs;

	NewObj->s.XX = 1.0f - (yy + zz); NewObj->s.YX =         xy - wz;  NewObj->s.ZX =         xz + wy;
	NewObj->s.XY =         xy + wz;  NewObj->s.YY = 1.0f - (xx + zz); NewObj->s.ZY =         yz - wx;
	NewObj->s.XZ =         xz - wy;  NewObj->s.YZ =         yz + wx;  NewObj->s.ZZ = 1.0f - (xx + yy);

}

//3x3 矩阵相乘
inline
static void Matrix3fMulMatrix3f(Matrix3fT* NewObj, const Matrix3fT* m1)
{

	Matrix3fT Result;

	assert(NewObj && m1);

	Result.s.M00 = (NewObj->s.M00 * m1->s.M00) + (NewObj->s.M01 * m1->s.M10) + (NewObj->s.M02 * m1->s.M20);
	Result.s.M01 = (NewObj->s.M00 * m1->s.M01) + (NewObj->s.M01 * m1->s.M11) + (NewObj->s.M02 * m1->s.M21);
	Result.s.M02 = (NewObj->s.M00 * m1->s.M02) + (NewObj->s.M01 * m1->s.M12) + (NewObj->s.M02 * m1->s.M22);

	Result.s.M10 = (NewObj->s.M10 * m1->s.M00) + (NewObj->s.M11 * m1->s.M10) + (NewObj->s.M12 * m1->s.M20);
	Result.s.M11 = (NewObj->s.M10 * m1->s.M01) + (NewObj->s.M11 * m1->s.M11) + (NewObj->s.M12 * m1->s.M21);
	Result.s.M12 = (NewObj->s.M10 * m1->s.M02) + (NewObj->s.M11 * m1->s.M12) + (NewObj->s.M12 * m1->s.M22);

	Result.s.M20 = (NewObj->s.M20 * m1->s.M00) + (NewObj->s.M21 * m1->s.M10) + (NewObj->s.M22 * m1->s.M20);
	Result.s.M21 = (NewObj->s.M20 * m1->s.M01) + (NewObj->s.M21 * m1->s.M11) + (NewObj->s.M22 * m1->s.M21);
	Result.s.M22 = (NewObj->s.M20 * m1->s.M02) + (NewObj->s.M21 * m1->s.M12) + (NewObj->s.M22 * m1->s.M22);

	*NewObj = Result;

}

//4x4 矩阵相乘
inline
static void Matrix4fSetRotationScaleFromMatrix4f(Matrix4fT* NewObj, const Matrix4fT* m1)
{

	assert(NewObj && m1);

	NewObj->s.XX = m1->s.XX; NewObj->s.YX = m1->s.YX; NewObj->s.ZX = m1->s.ZX;
	NewObj->s.XY = m1->s.XY; NewObj->s.YY = m1->s.YY; NewObj->s.ZY = m1->s.ZY;
	NewObj->s.XZ = m1->s.XZ; NewObj->s.YZ = m1->s.YZ; NewObj->s.ZZ = m1->s.ZZ;

}

//进行矩阵的奇异值分解,旋转矩阵被保存到rot3和 rot4中,返回矩阵的缩放因子
inline
static GLfloat Matrix4fSVD(const Matrix4fT* NewObj, Matrix3fT* rot3, Matrix4fT* rot4)
{

	GLfloat s, n;

	assert(NewObj);

	s = FuncSqrt(
		( (NewObj->s.XX * NewObj->s.XX) + (NewObj->s.XY * NewObj->s.XY) + (NewObj->s.XZ * NewObj->s.XZ) +
		(NewObj->s.YX * NewObj->s.YX) + (NewObj->s.YY * NewObj->s.YY) + (NewObj->s.YZ * NewObj->s.YZ) +
		(NewObj->s.ZX * NewObj->s.ZX) + (NewObj->s.ZY * NewObj->s.ZY) + (NewObj->s.ZZ * NewObj->s.ZZ) ) / 3.0f );

	if (rot3)  
	{

		rot3->s.XX = NewObj->s.XX; rot3->s.XY = NewObj->s.XY; rot3->s.XZ = NewObj->s.XZ;
		rot3->s.YX = NewObj-
...
...
(完整源码请下载查看)
			
...
展开> <收缩

下载源码到电脑,阅读使用更方便

1 积分

快速下载
还剩0行未阅读,继续阅读
云测速

源码文件列表

温馨提示: 点击源码文件名可预览文件内容哦 ^_^
...
名称 大小 修改日期
applicationhost.config82.95 kB2018-04-08 14:18
.suo41.50 kB2018-04-14 13:12
OpenObjModel.exe143.00 kB2018-04-14 13:08
OpenObjModel.ilk765.73 kB2018-04-14 13:08
OpenObjModel.pdb2.64 MB2018-04-14 13:08
ArcBall.h11.61 kB2018-04-14 13:11
char.h448.00 B2018-03-21 18:47
glew.h1.15 MB2018-04-10 17:20
glu.h15.99 kB2018-04-11 17:25
glut.h20.94 kB1998-08-18 16:24
objloader.h1.20 kB2018-04-13 11:43
stdafx.h1.03 kB2018-04-10 16:26
ArcBall.cpp4.59 kB2018-04-14 13:11
ArcBall.obj33.37 kB2018-04-14 13:08
GLRect.obj25.24 kB2018-04-13 10:55
main.obj398.39 kB2018-04-14 13:08
MousePoint.obj51.32 kB2018-04-13 10:56
MouseTri.obj148.68 kB2018-04-13 10:56
OpenObjModel.log204.00 B2018-04-14 13:08
CL.command.1.tlog7.30 kB2018-04-14 13:08
CL.read.1.tlog77.36 kB2018-04-14 13:08
CL.write.1.tlog3.79 kB2018-04-14 13:08
link.command.1.tlog3.23 kB2018-04-14 13:08
link.read.1.tlog3.13 kB2018-04-14 13:08
link.write.1.tlog458.00 B2018-04-14 13:08
OpenObjModel.lastbuildstate185.00 B2018-04-14 13:08
Render3Dbox.obj55.89 kB2018-04-13 11:14
vc140.idb923.00 kB2018-04-14 13:08
vc140.pdb524.00 kB2018-04-14 13:08
flamingo.obj263.26 kB2018-04-13 11:07
GLRect.cpp1.46 kB2018-04-13 10:45
main.cpp4.14 kB2018-04-14 13:11
MousePoint.cpp1.56 kB2018-04-13 10:49
MouseTri.cpp1.82 kB2018-04-13 10:49
OpenObjModel.vcxproj7.37 kB2018-04-14 13:06
OpenObjModel.vcxproj.filters1.01 kB2018-04-14 13:06
Render3Dbox.cpp2.59 kB2018-04-13 11:15
renderBall.cpp1.50 kB2018-04-13 10:49
shading.cpp1.68 kB2018-04-13 10:49
Simple.cpp387.00 B2018-04-13 10:49
vertexTest.cpp667.00 B2018-04-13 10:49
OpenObjModel.sln1.29 kB2018-04-14 13:12
OpenObjModel.VC.db54.55 MB2018-04-14 13:12
v140.00 B2018-04-13 10:36
OpenObjModel.tlog0.00 B2018-04-14 13:08
config0.00 B2018-04-13 11:06
OpenObjModel0.00 B2018-04-13 10:36
Debug0.00 B2018-04-14 13:08
.vs0.00 B2018-04-13 11:06
Debug0.00 B2018-04-14 13:06
include0.00 B2018-04-14 13:11
OpenObjModel0.00 B2018-04-14 13:11
OpenObjModel0.00 B2018-04-14 13:12
云测速

OpenObjModel/include/ArcBall.h (12.55 MB)

需要 1 积分
您持有 积分

CodeForge积分(原CF币)全新升级,功能更强大,使用更便捷,不仅可以用来下载海量源代码马上还可兑换精美小礼品了 了解更多

您的积分不足

支付宝优惠套餐快速获取 30 积分

订单支付完成后,积分将自动加入到您的账号。以下是优惠期的人民币价格,优惠期过后将恢复美元价格。

更多付款方式:网银PayPal

上传代码,免费获取

您本次下载所消耗的积分将转交上传作者。

同一源码,30天内重复下载,只扣除一次积分。

登录 CodeForge

还没有CodeForge账号? 立即注册
关注微博
联系客服

Switch to the English version?

Yes
CodeForge 英文版
No
CodeForge 中文版

完善个人资料,获价值¥30元积分奖励!

^_^"呃 ...

Sorry!这位大神很神秘,未开通博客呢,请浏览一下其他的吧
好的