从相机旋转中隔离“指南针”openGL
Posted
技术标签:
【中文标题】从相机旋转中隔离“指南针”openGL【英文标题】:Isolating "Compass" from camera rotations openGL 【发布时间】:2018-09-05 08:49:10 【问题描述】:我即将完成太阳系动画的原型。 (它仍然没有达到应有的效率)。目前我有一个带有土星/天王星/海王星加上海卫一/变形虫(海王星的卫星)的太阳系。该程序允许用户在 awsd 围绕轴旋转相机时为这些行星设置动画。 (0,1,0 / 1, 0, 0 等)
我放入了一个原型指南针(字面意思是 3 行)来指示当前的相机方向。 Y = 白色。 X = 红色。 Z = 蓝色。然而,每当旋转相机时,指南针似乎在移动(尽管它并没有真正移动),从而使用户感到困惑。有没有办法可以锁定指南针位置,同时允许它模拟用户的相机旋转到位?
我知道指南针是 3D 空间中的一组 3D 线,并且在视觉上将其锁定在 2D 字段中没有意义,但我希望无论如何都有解决方案。谢谢!
此外,我还尝试为指南针设置动画,使其模拟相机旋转到位,以帮助用户了解他/她当前的相机方向。
在display()
中绘制/初始化的指南针
在“wasd”案例中声明的相机旋转
float triton = 0;
float proteus = 0;
float neptune = 0;
float saturn = 0;
float uranus = 0;
int sun = 0;
int angle = 0;
GLint buf, sbuf;
void init(void)
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
// Stars
glNewList(1, GL_COMPILE);
glBegin(GL_POINTS);
glColor3f(1.0, 1.0, 1.0);
for (int i = 0; i < 200; i++)
for (int j = 0; j < 300; j++)
if (((i + j) % 2) == 0)
glVertex3f(100 * i, 100 * j, 0.0);
glEnd();
glEndList();
// Material Specs
GLfloat mat_specular[] = 0.8, 0.8, 0.9, 0.1 ;
GLfloat mat_shininess[] = 128.0 ;
GLfloat lightDiffuse[] = 1.0, 1.0, 1.0, 0.0 ;
GLfloat lmodel_ambient[] = 0.1, 0.2, 0.7, 0.0 ;
// Light 0 Initialized.
GLfloat light0[] = 1.0, 1.0, 1.0, 0.1 ;
GLfloat light_position[] = 1.0, 0.5, 0.0, -100.0 ;
// Light 0
glLightfv(GL_LIGHT0, GL_SPECULAR, light0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
// Mat Specs Implmentations.
glMaterialfv(GL_FRONT, GL_DIFFUSE, lightDiffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
//Ambient surrounding light on object.
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
// Enable Lighting and Depth
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
// Enable AntiAliased Lines
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
glLineWidth(1.5);
// Orbit values are arbitrary
void orbit(void)
triton = triton - 0.11;
proteus = proteus - 0.08;
neptune = neptune - 0.04;
saturn = saturn - 0.02;
uranus = uranus - 0.037;
glutPostRedisplay();
void backorbit(void)
triton = triton + 0.11;
proteus = proteus + 0.08;
neptune = neptune + 0.04;
saturn = saturn + 0.02;
uranus = uranus + 0.037;
glutPostRedisplay();
void display(void)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_COLOR_MATERIAL);
glDisable(GL_LIGHTING);
// Compass
glPushMatrix();
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
// Y axis
glVertex3f(-15.0, 12.0, 0.0);
glVertex3f(-15.0, 17.0, 0.0);
glColor3f(1.0, 0.0, 0.0);
// X axis
glVertex3f(-12.5, 14.5, 0.0);
glVertex3f(-17.5, 14.5, 0.0);
glColor3f(0.0, 0.0, 1.0);
// Z axis
glVertex3f(-15.0, 14.5, 1.0);
glVertex3f(-15.0, 14.5, -1.5);
glEnd();
glPopMatrix();
glEnable(GL_LIGHTING);
// Sun
glPushMatrix();
glColor3f(1.0, 0.35, 0.1);
glutSolidSphere(2.0, 100, 100);
// Saturn
glPushMatrix();
glRotatef((GLfloat)saturn, 0.5, 0.6, 1.0);
glColor3f(0.7, 0.5, 0.3);
glTranslatef(5.0, 0.0, 0.0);
glutSolidSphere(0.38, 100, 100);
glRotatef(20.0, 1.0, 0.0, 0.0);
glutSolidTorus(.11, .60, 2, 50);
glPopMatrix();
// Uranus
glPushMatrix();
glRotatef((GLfloat)uranus, 0.0, 1.0, 0.0);
glColor3f(0.5, 0.85, 0.9);
glTranslatef(6.5, 0.0, 0.0);
glutSolidSphere(0.37, 100, 100);
glPopMatrix();
// End Uranus
// Neptune
glPushMatrix();
glRotatef((GLfloat)neptune, 0.3, 1.0, 0.8);
glTranslatef(8.0, 0.0, 0.0);
glColor3f(0.1, 0.1, 0.3);
glutSolidSphere(0.3, 100, 100);
// Neptune(Triton)
glPushMatrix();
glColor3f(0.85, 0.7, 0.8);
glRotatef((GLfloat)triton, 1.0, 1.0, 1.0);
glTranslatef(1.0, 0.0, 0.0);
glutSolidSphere(0.07, 100, 100);
glPopMatrix(); // Ends Triton
// Neptune(Proteus)
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glRotatef((GLfloat)proteus, 0.0, 1.0, 0.0);
glTranslatef(1.0, 0.0, 0.0);
glutSolidSphere(0.04, 100, 100);
glPopMatrix(); // Ends Proteus
glPopMatrix(); // Ends Neptune
glPopMatrix(); // Ends Sun
glEnable(GL_MULTISAMPLE);
glDisable(GL_LIGHTING);
// Stars
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
// Arbitrary ortho
glOrtho(0, 1000, 0, 1000, 0, 1000);
glCallList(1);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPopMatrix();
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glFlush();
glutSwapBuffers();
void reshape(int w, int h)
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(120.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -11.0); // Find location.
void keyboard(unsigned char key, int x, int y)
switch (key)
// Triton + Proteus Orbit.
case 'o':
glutIdleFunc(orbit);
break;
case 'p':
glutIdleFunc(backorbit);
break;
// Camera Rotations.
case 'd':
glRotatef(4, 1.0, 0.0, 0.0);
glutPostRedisplay();
break;
case 'a':
glRotatef(-4, 1.0, 0.0, 0.0);
glutPostRedisplay();
break;
case 'w':
angle -= 4.0;
glRotatef(-4.0, 0.0, 1.0, 0.0);
glutPostRedisplay();
break;
case 's':
glRotatef(4.0, 0.0, 1.0, 0.0);
glutPostRedisplay();
break;
// Stop Orbit.
case 't':
glutIdleFunc(NULL);
break;
// Reset to Origin (IP)
case '1':
angle = 0;
glutPostRedisplay();
break;
case ',':
glTranslatef(-0.3, 0.0, 0.0);
glutPostRedisplay();
break;
case '.':
glTranslatef(0.3, 0.0, 0.0);
glutPostRedisplay();
break;
// Exit
case 27:
exit(0);
break;
default:
break;
int main(int argc, char **argv)
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB | GLUT_MULTISAMPLE);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(100, 100);
glutCreateWindow("Solar System");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
【问题讨论】:
【参考方案1】:您必须将“指南针”绘制到视图的中心(0、0、0),然后您必须应用视图矩阵,最后您必须将“指南针”平移到其最终位置。
当前模型视图矩阵可以通过glGetFloatv( GL_MODELVIEW_MATRIX, view_mat )
获取:
glPushMatrix();
GLfloat view_mat[16];
glGetFloatv( GL_MODELVIEW_MATRIX, view_mat );
glLoadIdentity();
glTranslatef( -15.0, 14.5, 0.0 );
glMultMatrixf( view_mat );
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
// Y axis
glVertex3f(0.0, -2.5, 0.0);
glVertex3f(0.0, 2.5, 0.0);
glColor3f(1.0, 0.0, 0.0);
// X axis
glVertex3f(-2.5, 0.0, 0.0);
glVertex3f(2.5, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
// Z axis
glVertex3f(-0.0, 0.0, 1.0);
glVertex3f(-0.0, 0.0, -1.5);
glEnd();
glPopMatrix();
但请注意,由glBegin
/glEnd
序列绘制并使用固定函数管道矩阵堆栈已被弃用数年。
阅读Fixed Function Pipeline 并查看Vertex Specification 和Shader,了解最先进的渲染方式。
【讨论】:
我明白了,我知道重叠矩阵本身就是问题,但使用glGetFloatv( GL_MODELVIEW_MATRIX, view_mat);
对我来说是新的。非常感谢!祝福***。以上是关于从相机旋转中隔离“指南针”openGL的主要内容,如果未能解决你的问题,请参考以下文章
我试图统一创建一个 3d 指南针,但针无法与指南针的主体一起旋转
Android在GoogleMap(百度地图)实现自定义指南针旋转与回正功能
Android在GoogleMap(百度地图)实现自定义指南针旋转与回正功能