OpenGL笔记——轨迹球实现资料

阅读更多

OpenGL 轨迹球问题

03-28

我写了一个轨迹球的类, 第一次拖动还可以, 以后在拖动, 物体就走形了, 不知道有没有逻辑错误,高手帮看看吧谢谢了rnrnarcball.hrn#pragma oncern#include "stdafx.h"rn//定义二元组rntypedef union Tuple2fTrnrn struct rn rn float x,y;rn s;rn float T[2];rnTuple2fT;rn//定义三元组rntypedef union Tuple3fTrnrn struct rn rn float x,y,z;rn s;rn float T[3];rnTuple3fT;rn//定义四元组rntypedef union Tuple4fTrnrn struct rn rn float x,y,z,w;rn s;rn float T[4];rnTuple4fT;rn//定义3*3矩阵rntypedef union Matrix3fTrnrn struct rn rn //column majorrn union GLfloat M00; GLfloat XX; GLfloat SX; ; rn union GLfloat M10; GLfloat XY; ; rn union GLfloat M20; GLfloat XZ; ; rn union GLfloat M01; GLfloat YX; ; rn union GLfloat M11; GLfloat YY; GLfloat SY; ; rn union GLfloat M21; GLfloat YZ; ; rn union GLfloat M02; GLfloat ZX; ; rn union GLfloat M12; GLfloat ZY; ; rn union GLfloat M22; GLfloat ZZ; GLfloat SZ; ; rn s;rn GLfloat M[9];rnMatrix3fT;rn//定义4*4矩阵rntypedef union Matrix4fTrnrn struct rn rn union GLfloat M00; GLfloat XX; GLfloat SX; ; rn union GLfloat M10; GLfloat XY; ; rn union GLfloat M20; GLfloat XZ; ; rn union GLfloat M30; GLfloat XW; ; rn union GLfloat M01; GLfloat YX; ; rn union GLfloat M11; GLfloat YY; GLfloat SY; ; rn union GLfloat M21; GLfloat YZ; ; rn union GLfloat M31; GLfloat YW; ; rn union GLfloat M02; GLfloat ZX; ; rn union GLfloat M12; GLfloat ZY; ; rn union GLfloat M22; GLfloat ZZ; GLfloat SZ; ; rn union GLfloat M32; GLfloat ZW; ; rn union GLfloat M03; GLfloat TX; ; rn union GLfloat M13; GLfloat TY; ; rn union GLfloat M23; GLfloat TZ; ; rn union GLfloat M33; GLfloat TW; GLfloat SW; ; rn s;rn GLfloat M[16];rnMatrix4fT;rnrn#define Point2fT Tuple2fT //二维点 rn#define Point3fT Tuple3fT //三维点rn#define Vector2fT Tuple2fT //二维向量rn#define Vector3fT Tuple3fT //三维向量rn#define Quat4fT Tuple4fT //四元数,用来保存旋转轴, w=cos(a/2)为旋转角度参数,a为向量夹角rn//三维向量内积rninline static float Vector3fDot(Vector3fT *V1, Vector3fT *V2)rnrn return(V1->s.x*V2->s.x+V1->s.y*V2->s.y+V1->s.z*V2->s.z);rnrn//三维向量外积rninline static Vector3fT Vector3fCross(Vector3fT *V1, Vector3fT *V2)rnrn Vector3fT V;rn V.s.x=V1->s.y*V2->s.z-V1->s.z*V2->s.y;rn V.s.y=V1->s.z*V2->s.x-V1->s.x*V2->s.z;rn V.s.z=V1->s.x*V2->s.y-V1->s.y*V2->s.x;rn return V;rnrn//三维向量求模rninline static float Vector3Model(Vector3fT *V)rnrn return (sqrt(V->s.x*V->s.x+V->s.y*V->s.y+V->s.z*V->s.z));rnrnrn//三阶矩阵清零rninline static int Matrix3SetZero(Matrix3fT *Matrix)rnrn Matrix->s.M00=Matrix->s.M01=Matrix->s.M02=rn Matrix->s.M10=Matrix->s.M11=Matrix->s.M12=rn Matrix->s.M20=Matrix->s.M21=Matrix->s.M22=0.0f;rn return TRUE;rnrn//三阶矩阵设置成单位阵rninline static int Matrix3SetIdentity(Matrix3fT *Matrix)rnrn Matrix3SetZero(Matrix);rn Matrix->s.M00=Matrix->s.M11=Matrix->s.M22=1.0f;rn return TRUE;rnrn//四阶矩阵置零rninline static int Matrix4SetZero(Matrix4fT *Matrix)rnrn Matrix->s.M00=Matrix->s.M01=Matrix->s.M02=Matrix->s.M03=rn Matrix->s.M10=Matrix->s.M11=Matrix->s.M12=Matrix->s.M13=rn Matrix->s.M20=Matrix->s.M21=Matrix->s.M22=Matrix->s.M23=rn Matrix->s.M30=Matrix->s.M31=Matrix->s.M32=Matrix->s.M33=0.0f;rn return TRUE;rnrn//四阶矩阵设置成单位阵rninline static int Matrix4SetIdentity(Matrix4fT *Matrix)rnrn Matrix4SetZero(Matrix);rn Matrix->s.M00=Matrix->s.M11=Matrix->s.M22=Matrix->s.M33=1.0f;rn return TRUE;rnrn//3阶矩阵相乘 Matrix=M1*M2rninline static int Matrix3Mul(Matrix3fT *M1,Matrix3fT *M2, Matrix3fT *Matrix)rnrn Matrix3SetZero(Matrix);rn Matrix->s.M00=M1->s.M00*M2->s.M00+M1->s.M01*M2->s.M10+M1->s.M02*M2->s.M20;rn Matrix->s.M01=M1->s.M00*M2->s.M01+M1->s.M01*M2->s.M11+M1->s.M02*M2->s.M21;rn Matrix->s.M02=M1->s.M00*M2->s.M02+M1->s.M01*M2->s.M12+M1->s.M02*M2->s.M22;rnrn Matrix->s.M10=M1->s.M10*M2->s.M00+M1->s.M11*M2->s.M10+M1->s.M12*M2->s.M20;rn Matrix->s.M11=M1->s.M10*M2->s.M01+M1->s.M11*M2->s.M11+M1->s.M12*M2->s.M21;rn Matrix->s.M12=M1->s.M10*M2->s.M02+M1->s.M11*M2->s.M12+M1->s.M12*M2->s.M22;rnrn Matrix->s.M20=M1->s.M20*M2->s.M00+M1->s.M21*M2->s.M10+M1->s.M22*M2->s.M20;rn Matrix->s.M21=M1->s.M20*M2->s.M01+M1->s.M21*M2->s.M11+M1->s.M22*M2->s.M21;rn Matrix->s.M22=M1->s.M20*M2->s.M02+M1->s.M21*M2->s.M12+M1->s.M22*M2->s.M22;rn return TRUE;rnrn//由旋转4元数得到3维旋转矩阵rninline static int Quat4ToMatrix3Rot(Quat4fT *Rot, Matrix3fT *Matrix3Rot)rnrn Matrix3Rot->s.M00=1-2*(Rot->s.y*Rot->s.y+Rot->s.z*Rot->s.z);rn Matrix3Rot->s.M01=2*(Rot->s.x*Rot->s.y-Rot->s.w*Rot->s.z);rn Matrix3Rot->s.M02=2*(Rot->s.w*Rot->s.y+Rot->s.x*Rot->s.z);rn Matrix3Rot->s.M10=2*(Rot->s.x*Rot->s.y+Rot->s.w*Rot->s.z);rn Matrix3Rot->s.M11=1-2*(Rot->s.x*Rot->s.x+Rot->s.z*Rot->s.z);rn Matrix3Rot->s.M12=2*(Rot->s.y*Rot->s.z-Rot->s.w*Rot->s.x);rn Matrix3Rot->s.M20=2*(Rot->s.x*Rot->s.z-Rot->s.w*Rot->s.y);rn Matrix3Rot->s.M21=2*(Rot->s.y*Rot->s.z+Rot->s.w*Rot->s.x);rn Matrix3Rot->s.M22=1-2*(Rot->s.x*Rot->s.x+Rot->s.y*Rot->s.y);rn return TRUE;rnrn//由3*3旋转矩阵得到用于OpenGL的4*4旋转矩阵rninline static int Matrix3RotToMatrix4Rot(Matrix3fT *M1,Matrix4fT *M2)rnrn M2->s.M00=M1->s.M00; M2->s.M01=M1->s.M01; M2->s.M02=M1->s.M02;rn M2->s.M10=M1->s.M10; M2->s.M11=M1->s.M11; M2->s.M12=M1->s.M12;rn M2->s.M20=M1->s.M20; M2->s.M21=M1->s.M21; M2->s.M22=M1->s.M22;rn M2->s.M30=0.0f; M2->s.M31=0.0f; M2->s.M32=0.0f; rn M2->s.M03=0.0f; M2->s.M13=0.0f; M2->s.M23=0.0f; M2->s.M33=1.0f;rn return TRUE;rnrnrnrnrnrnclass ArcBallrnrnpublic:rn ArcBall(void);rnpublic:rn ~ArcBall(void);rnprotected:rn // 保存鼠标点击起始向量rn Vector3fT ClickPoint;rn // 保存鼠标拖动向量rn Vector3fT DragPoint;rnpublic:rn int MousePointMappingToSphere(Point2fT* MousePoint, Vector3fT* MouseVector);rnpublic:rn int MouseClick(Point2fT* MousePoint);rnpublic:rn int MouseDrag(Point2fT* MousePoint, Quat4fT* Rot);rnpublic:rn int Update(int ID, Point2fT *MousePoint);rn;rnrn=======================================================rn

没有更多推荐了,返回首页