从相机旋转 openGL 中隔离 "Compass"
Isolating "Compass" from camera rotations openGL
我即将完成太阳系动画的原型。 (它仍然没有达到应有的效率)。目前我有一个 Saturn/Uranus/Neptune 加上 Triton/Proteus(海王星卫星)的太阳系。该程序允许用户在 awsd 围绕轴旋转相机的同时为这些行星设置动画。 ( 0,1,0 / 1, 0, 0 等)
我放入了一个原型指南针(实际上是 3 条线)来指示当前的相机方向。 Y = 白色。 X = 红色。 Z = 蓝色。然而,无论何时旋转相机,罗盘似乎都在移动(尽管它实际上并没有移动),从而使用户感到困惑。有没有一种方法可以锁定罗盘位置,同时允许它模仿用户的相机旋转到位?
我知道指南针是 3D 中的一组 3D 线 space 并且在视觉上将其锁定在 2D 字段中没有意义,但我希望无论如何都有解决方案。谢谢!
此外,我还尝试制作指南针的动画,以便它模仿相机在适当位置的旋转,以帮助用户了解 his/her 当前相机方向。
指南针 drawn/initialized 在 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;
}
您必须将 "compass " 绘制到视图的中心 (0, 0, 0),然后您必须应用视图矩阵,最后您必须将 "compass" 转换为它的最终位置。
当前模型视图矩阵可以通过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 and see Vertex Specification and Shader 了解最先进的渲染方式。
我即将完成太阳系动画的原型。 (它仍然没有达到应有的效率)。目前我有一个 Saturn/Uranus/Neptune 加上 Triton/Proteus(海王星卫星)的太阳系。该程序允许用户在 awsd 围绕轴旋转相机的同时为这些行星设置动画。 ( 0,1,0 / 1, 0, 0 等)
我放入了一个原型指南针(实际上是 3 条线)来指示当前的相机方向。 Y = 白色。 X = 红色。 Z = 蓝色。然而,无论何时旋转相机,罗盘似乎都在移动(尽管它实际上并没有移动),从而使用户感到困惑。有没有一种方法可以锁定罗盘位置,同时允许它模仿用户的相机旋转到位?
我知道指南针是 3D 中的一组 3D 线 space 并且在视觉上将其锁定在 2D 字段中没有意义,但我希望无论如何都有解决方案。谢谢!
此外,我还尝试制作指南针的动画,以便它模仿相机在适当位置的旋转,以帮助用户了解 his/her 当前相机方向。
指南针 drawn/initialized 在 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;
}
您必须将 "compass " 绘制到视图的中心 (0, 0, 0),然后您必须应用视图矩阵,最后您必须将 "compass" 转换为它的最终位置。
当前模型视图矩阵可以通过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 and see Vertex Specification and Shader 了解最先进的渲染方式。