我正在玩OpenGL渲染三角形,我遇到了一个有趣的问题。显然,当你沿着“向上看”时,方向,在我的情况下意味着Eye_z = Up_z(并且由于我在球坐标中工作,这意味着theta = 0),因此不显示对象(请参阅Google Fonts FAQs的最佳答案以获得数学解释)。 但是,当我将角度设置为360度而不是0时,对象显示没有任何问题。有谁知道为什么?我已经附上了我的代码片段以及截图:
triangle.cpp
void Triangle::render(Camera C){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glLoadIdentity();
float th = C.Getth();
float ph = C.Getph();
float Eye_x = r*sin(th*DEGtoRAD)*cos(ph*DEGtoRAD);
float Eye_y = r*sin(th*DEGtoRAD)*sin(ph*DEGtoRAD);
float Eye_z = r*cos(th*DEGtoRAD);
float Up_z = (th>=180.0f && th<360.0f)?-1.0f:1.0f;
gluLookAt(Eye_x,Eye_y,Eye_z,0.0,0.0,0.0,0.0,0.0,Up_z);
glRotatef(m_rotationAngle, 0.0f, 0.0f, 1.0f);
// Draw Triangle
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-0.5f,-0.5f,0.0f);
glColor3f(1.0f,1.0f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,0.5f,0.0f);
glEnd();
// Draw Axes
glBegin(GL_LINES);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,1.0f);
glEnd();
}
camera.cpp:
void Camera::Move(int dir){
switch(dir){
case 1: // +th
th += move_amt;
if(th>360.0f){
th-=360.0f;
}
break;
case -1: // -th
th -= move_amt;
if(th<=0.0f){
th+=360.0f;
}
break;
case 2: // ph
ph += move_amt;
if(ph>=360.0f){
ph-=360.0f;
}
break;
case -2: // -ph
ph -= move_amt;
if(ph<=0.0f){
ph+=360.0f;
}
break;
default:
break;
}
}
OpenGL is not displaying anything when looking along the y axis