簡體   English   中英

圍繞相機opengl c ++旋轉/設置旋轉中心

[英]Rotate around camera opengl c++ / Set center for rotation

Opengl始終繞(0,0,0)旋轉。 但是我需要繞着鏡頭旋轉。 我知道在GL中沒有相機, 但是如果我將其轉換回(0,0,-6.0) ,它將繞(0,0,0)旋轉,而不是繞(0,0,-6.0)旋轉

所以我的問題是:如何在opengl和c ++中設置旋轉中心

我已經找到了這篇文章: OpenGL圍繞一個點旋轉相機

顯示此解決方案的位置:

旋轉功能通常繞原點旋轉。 要繞另一個點P旋轉,您必須:平移(-P)旋轉平移(P)

而且我現在不知道如何實現

翻譯(P)

因為在旋轉之后,平移相對於旋轉

示例:我想旋轉(1,1,1)。 我翻譯(-1,-1,-1)。 現在,我將其繞x軸旋轉90度。 這是正確的。 但是現在我必須將其翻譯回去。 如果我平移(1,1,1),它將無法工作,因為它現在旋轉了,並且運動是相對於旋轉的矩陣而言的。

PS: 我不想使用gluLookAt()

所以我的代碼是:

//set rotation to zero
glRotatef(-yr,0.0f,1.0f,0.0f);  
glRotatef(-xr,1.0f,0.0f,0.0f);
//set points relative to camera
glTranslatef(cam_pos[0],cam_pos[1],cam_pos[2]);
//rotate back
glRotatef(yr,0.0f,1.0f,0.0f);   
glRotatef(xr,1.0f,0.0f,0.0f);
//rotate
glRotatef(angley,0.0f,1.0f,0.0f);   
glRotatef(anglex,1.0f,0.0f,0.0f);
//and now, how to translate back,
//movement is relative to the cam ?
//I would simply use :  
//glTranslatef(-cam_pos[0],-cam_pos[1],-cam_pos[2]);
//which wont work because its relative to the new rotation.

任何幫助是極大的贊賞 !

因此,我實際上發現運動不是相對於旋轉而言的,這使得一切都易於實現。 代碼:

glTranslatef(point_to_rotate_around_x,point_to_rotate_around_y,point_to_rotate_around_z);
glRotatef(anglex,1,0,0);
glRotatef(angley,0,1,0);
glTranslatef(-point_to_rotate_around_x,-point_to_rotate_around_y,-point_to_rotate_around_z);

感謝@derhass,他告訴我它倒退了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM