opengl读取.obj三维模型,arcball实现鼠标点击实现模型变换放大,缩小,平移


opengl读取.obj三维模型(可自己创建放入.obj文件夹中),arcball实现鼠标点击实现模型变换,及键盘控制放大缩小。
资源截图
代码片段和文件信息
//#include “stdafx.h“
#define GLUT_DISABLE_ATEXIT_HACK 
#include         
#include                                     
#include “ArcBall.h“                                 

//轨迹球参数:
//直径                    2.0f
//半径                    1.0f
//半径平方                1.0f


void ArcBall_t::_mapToSphere(const Point2fT* NewPt Vector3fT* NewVec) const
{
Point2fT TempPt;
GLfloat length;

//复制到临时变量
TempPt = *NewPt;

//把长宽调整到[-1 ... 1]区间
TempPt.s.X = (TempPt.s.X * this->AdjustWidth) - 1.0f;
TempPt.s.Y = 1.0f - (TempPt.s.Y * this->AdjustHeight);

//计算长度的平方
length = (TempPt.s.X * TempPt.s.X) + (TempPt.s.Y * TempPt.s.Y);

//如果点映射到球的外面
if (length > 1.0f)
{
GLfloat norm;

//缩放到球上
norm = 1.0f / FuncSqrt(length);

//设置z坐标为0
NewVec->s.X = TempPt.s.X * norm;
NewVec->s.Y = TempPt.s.Y * norm;
NewVec->s.Z = 0.0f;
}
//如果在球内
else
{
//利用半径的平方为1求出z坐标
NewVec->s.X = TempPt.s.X;
NewVec->s.Y = TempPt.s.Y;
NewVec->s.Z = FuncSqrt(1.0f - length);
}
}

ArcBall_t::ArcBall_t(GLfloat NewWidth GLfloat NewHeight)
{
this->StVec.s.X = 0.0f;
this->StVec.s.Y = 0.0f;
this->StVec.s.Z = 0.0f;

this->EnVec.s.X = 0.0f;
this->EnVec.s.Y = 0.0f;
this->EnVec.s.Z = 0.0f;


Matrix4fSetIdentity(&Transform);
Matrix3fSetIdentity(&LastRot);
Matrix3fSetIdentity(&ThisRot);

this->isDragging = false;
this->isClicked = false;
this->isRClicked = false;
this->isZooming = false;
this->zoomRate = 1;
this->setBounds(NewWidth NewHeight);
}

void ArcBall_t::upstate()
{
if (!this->isZooming && this->isRClicked) {                    // 开始拖动
this->isZooming = true;                                        // 设置拖动为变量为true        
this->LastPt = this->MousePt;
this->lastZoomRate = this->zoomRate;
}
else if (this->isZooming) {//正在拖动
if (this->isRClicked) {                //拖动        
Point2fSub(&this->MousePt &this->LastPt);
this->zoomRate = this->lastZoomRate + this->MousePt.s.X * this->AdjustWidth * 2;
}
else {                                            //停止拖动
this->isZooming = false;
}
}
else if (!this->isDragging && this->isClicked) {                                               // 如果没有拖动
this->isDragging = true;                                        // 设置拖动为变量为true
this->LastRot = this->ThisRot;
this->click(&this->MousePt);
}
else if (this->isDragging) {
if (this->isClicked) {                                            //如果按住拖动
Quat4fT     ThisQuat;

this->drag(&this->MousePt &ThisQuat);                        // 更新轨迹球的变量
Matrix3fSetRotationFromQuat4f(&this->ThisRot &ThisQuat);       // 计算旋转量
Matrix3fMulMatrix3f(&this->ThisRot &this->LastRot);
Matrix4fSetRotationFromMatrix3f(&this->Transform &this->ThisRot);
}
else                                                        // 如果放开鼠标,设置拖动为false
this->isDragging = false;
}
}

//按下鼠标记录当前对应的轨迹球的位置
void    ArcBall_t::click(co

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.     27648  2018-03-29 14:12  MyArcball.vsMyArcballv14.suo

     文件     230912  2018-03-29 14:11  MyArcballDebugMyArcball.exe

     文件     943380  2018-03-29 14:11  MyArcballDebugMyArcball.ilk

     文件    1560576  2018-03-29 14:11  MyArcballDebugMyArcball.pdb

     文件       3901  2018-03-28 23:25  MyArcballMyArcballArcball.cpp

     文件      11211  2018-03-28 15:41  MyArcballMyArcballArcBall.h

     文件      34441  2018-03-29 14:07  MyArcballMyArcballDebugArcball.obj

     文件     823261  2018-03-29 14:07  MyArcballMyArcballDebugloadmodel.obj

     文件       1011  2018-03-29 14:12  MyArcballMyArcballDebugMyArcball.log

     文件     249722  2018-03-29 14:12  MyArcballMyArcballDebugMyArcball.obj

     文件       2070  2018-03-29 14:12  MyArcballMyArcballDebugMyArcball.tlogCL.command.1.tlog

     文件      55458  2018-03-29 14:12  MyArcballMyArcballDebugMyArcball.tlogCL.read.1.tlog

     文件       1596  2018-03-29 14:12  MyArcballMyArcballDebugMyArcball.tlogCL.write.1.tlog

     文件       1920  2018-03-29 14:11  MyArcballMyArcballDebugMyArcball.tloglink.command.1.tlog

     文件       4572  2018-03-29 14:11  MyArcballMyArcballDebugMyArcball.tloglink.read.1.tlog

     文件        776  2018-03-29 14:11  MyArcballMyArcballDebugMyArcball.tloglink.write.1.tlog

     文件        205  2018-03-29 14:11  MyArcballMyArcballDebugMyArcball.tlogMyArcball.lastbuildstate

     文件     863232  2018-03-29 14:12  MyArcballMyArcballDebugvc140.idb

     文件     577536  2018-03-29 14:12  MyArcballMyArcballDebugvc140.pdb

     文件      10611  2018-03-29 14:05  MyArcballMyArcballloadmodel.cpp

     文件       1107  2018-03-29 14:05  MyArcballMyArcballloadmodel.h

     文件       6293  2018-03-29 14:12  MyArcballMyArcballMyArcball.cpp

     文件       7846  2018-03-29 14:09  MyArcballMyArcballMyArcball.vcxproj

     文件       1345  2018-03-29 14:09  MyArcballMyArcballMyArcball.vcxproj.filters

     文件      66707  2017-04-18 19:10  MyArcballMyArcballobjird.obj

     文件    6656387  2018-03-27 23:19  MyArcballMyArcballobjcolor-tomota.obj

     文件      65024  2018-03-28 23:55  MyArcballMyArcballobj
ock.obj

     文件     394444  2017-04-18 19:10  MyArcballMyArcballobj
ubby.obj

     文件      17844  2017-04-18 19:10  MyArcballMyArcballobj orus.obj

     文件    6659036  2018-03-27 22:26  MyArcballMyArcballobj ri-tomato.obj

............此处省略15个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)