UfnCod3r
دوشنبه 19 فروردین 1392, 10:56 صبح
سلام من می خوام Quaternion درو محور World بچرخه . لوکال نباشه .
این رو نوشتم .
inline void worldRotate(flt angleX, flt angleY, flt angleZ)
{
XQuat q ;
XQuat inv;
q.setFromEuler(angleX, angleY, angleZ);
this->getInverse(&inv);
*this = (*this) * inv * q * (*this);
}
مدل ها درست می چرخن .
ولی دوربین دور محور خودش می چرخه نمی دونم چشه .:متعجب:
اینم کد:::لبخند:
#include <stdio.h>
#include <Windows.h>
#include <gl\GL.h>
//SSE vec,quat,mat4x4
#include "..\XSSE\XVec3.h"
#include "..\XSSE\XMath.h"
#include "..\XSSE\XVec4.h"
#include "..\XSSE\XQuat.h"
#include "..\XSSE\XMat4x4.h"
XMat4x4 gMatProj;
XMat4x4 gMatView;
XVec3 gCameraPos(0,4,4);
XQuat gCameraRotation(0,0,0,1);
struct XStaticMesh
{
XVec3 worldPos;
XQuat rotation;
void render()
{
XMat4x4 matrix = XMat4x4::CreateTranslation(worldPos)*XMat4x4::Crea teRotation(rotation);
glLoadMatrixf((gMatView*matrix).m);
glColor3f(1,1,0.8);
//DrawMode();
//draw axis ----
glDisable(GL_LIGHTING);
glCallList(gCallListwAxis);
glEnable(GL_LIGHTING);
}
};
XStaticMesh gModel1;
XStaticMesh gModel2;
int main(int argc, char** argv)
{
XMath_Init();
XSSE_Init();
XCreateWnd(640, 480, "Test1");
XGLInit();
gModel1.worldPos.setZero();
gModel1.rotation.setIdentity();
gModel2.worldPos.setOne();
gModel2.rotation.setFromAngleAxisY(90.0f);
while (gLoop)
{
//input update ======================================
MSG msg;
memcpy(gKeysPre, gkeysCur, 255);
while (PeekMessage(&msg, gHWND, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//proj matrix
gMatProj = XMat4x4::CreatePerspective(45.0f, 1.3f, 0.0001f, 1000.0f);
//view matrix
gMatView = XMat4x4::CreateTranslation(gCameraPos) * XMat4x4::CreateRotation(gCameraRotation);
gMatView.invert();
//load proj matrix
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(gMatProj.m);
//load view matrix
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(gMatView.m);
//draw gride
glDisable(GL_LIGHTING);
glCallList(gCallListGrid);
glEnable(GL_LIGHTING);
//rotate model
if(XKeyDown('T'))
gModel1.rotation.worldRotate(3.0f,0,0);
if(XKeyDown('Y'))
gModel1.rotation.worldRotate(0.0f,3,0);
if(XKeyDown('U'))
gModel1.rotation.worldRotate(0.0f,0,3);
if(XKeyDown('E'))
gCameraPos.y++;
else if(XKeyDown('Q'))
gCameraPos.y--;
//move camera --------------------
if(XKeyDown('D'))
gCameraPos += gCameraRotation * XVec3::RIGHT;
else if(XKeyDown('A'))
gCameraPos -= gCameraRotation * XVec3::RIGHT;
if(XKeyDown('W'))
gCameraPos -= gCameraRotation * XVec3::FORWARD;
else if(XKeyDown('S'))
gCameraPos += gCameraRotation * XVec3::FORWARD;
//rotate camera -------------------
if(XKeyDown(VK_RIGHT))
gCameraRotation.worldRotate(0,-2,0);
else if(XKeyDown(VK_LEFT))
gCameraRotation.worldRotate(0,2,0);
if(XKeyDown(VK_UP))
gCameraRotation.worldRotate(-2,0,0);
if(XKeyDown(VK_DOWN))
gCameraRotation.worldRotate(2,0,0);
gModel1.render();
gModel2.render();
SwapBuffers(gHDC);
Sleep(32);
//exith with ESC
if(XKeyRelese(VK_ESCAPE))
gLoop = false;
}
return 1;
}
این رو نوشتم .
inline void worldRotate(flt angleX, flt angleY, flt angleZ)
{
XQuat q ;
XQuat inv;
q.setFromEuler(angleX, angleY, angleZ);
this->getInverse(&inv);
*this = (*this) * inv * q * (*this);
}
مدل ها درست می چرخن .
ولی دوربین دور محور خودش می چرخه نمی دونم چشه .:متعجب:
اینم کد:::لبخند:
#include <stdio.h>
#include <Windows.h>
#include <gl\GL.h>
//SSE vec,quat,mat4x4
#include "..\XSSE\XVec3.h"
#include "..\XSSE\XMath.h"
#include "..\XSSE\XVec4.h"
#include "..\XSSE\XQuat.h"
#include "..\XSSE\XMat4x4.h"
XMat4x4 gMatProj;
XMat4x4 gMatView;
XVec3 gCameraPos(0,4,4);
XQuat gCameraRotation(0,0,0,1);
struct XStaticMesh
{
XVec3 worldPos;
XQuat rotation;
void render()
{
XMat4x4 matrix = XMat4x4::CreateTranslation(worldPos)*XMat4x4::Crea teRotation(rotation);
glLoadMatrixf((gMatView*matrix).m);
glColor3f(1,1,0.8);
//DrawMode();
//draw axis ----
glDisable(GL_LIGHTING);
glCallList(gCallListwAxis);
glEnable(GL_LIGHTING);
}
};
XStaticMesh gModel1;
XStaticMesh gModel2;
int main(int argc, char** argv)
{
XMath_Init();
XSSE_Init();
XCreateWnd(640, 480, "Test1");
XGLInit();
gModel1.worldPos.setZero();
gModel1.rotation.setIdentity();
gModel2.worldPos.setOne();
gModel2.rotation.setFromAngleAxisY(90.0f);
while (gLoop)
{
//input update ======================================
MSG msg;
memcpy(gKeysPre, gkeysCur, 255);
while (PeekMessage(&msg, gHWND, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//proj matrix
gMatProj = XMat4x4::CreatePerspective(45.0f, 1.3f, 0.0001f, 1000.0f);
//view matrix
gMatView = XMat4x4::CreateTranslation(gCameraPos) * XMat4x4::CreateRotation(gCameraRotation);
gMatView.invert();
//load proj matrix
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(gMatProj.m);
//load view matrix
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(gMatView.m);
//draw gride
glDisable(GL_LIGHTING);
glCallList(gCallListGrid);
glEnable(GL_LIGHTING);
//rotate model
if(XKeyDown('T'))
gModel1.rotation.worldRotate(3.0f,0,0);
if(XKeyDown('Y'))
gModel1.rotation.worldRotate(0.0f,3,0);
if(XKeyDown('U'))
gModel1.rotation.worldRotate(0.0f,0,3);
if(XKeyDown('E'))
gCameraPos.y++;
else if(XKeyDown('Q'))
gCameraPos.y--;
//move camera --------------------
if(XKeyDown('D'))
gCameraPos += gCameraRotation * XVec3::RIGHT;
else if(XKeyDown('A'))
gCameraPos -= gCameraRotation * XVec3::RIGHT;
if(XKeyDown('W'))
gCameraPos -= gCameraRotation * XVec3::FORWARD;
else if(XKeyDown('S'))
gCameraPos += gCameraRotation * XVec3::FORWARD;
//rotate camera -------------------
if(XKeyDown(VK_RIGHT))
gCameraRotation.worldRotate(0,-2,0);
else if(XKeyDown(VK_LEFT))
gCameraRotation.worldRotate(0,2,0);
if(XKeyDown(VK_UP))
gCameraRotation.worldRotate(-2,0,0);
if(XKeyDown(VK_DOWN))
gCameraRotation.worldRotate(2,0,0);
gModel1.render();
gModel2.render();
SwapBuffers(gHDC);
Sleep(32);
//exith with ESC
if(XKeyRelese(VK_ESCAPE))
gLoop = false;
}
return 1;
}