3D三角形绕Y轴旋转无法正常显示:代码问题排查与解决
Let's break down why your triangle isn't showing up or rotating, and fix each issue step by step:
1. Triangle Is Clipped by the Near Plane
Your projection setup uses glFrustum(-30, 30, -30, 30, 30, 90). Here's what that means:
- The near plane is 30 units away from the camera along the -Z axis (so at global Z = -30)
- The far plane is 90 units away (Z = -90)
Your triangle's vertices have Z values of -10, -20, -10 — these are closer to the camera than the near plane (Z = -30), so they get completely clipped and never rendered.
Fix: Move the triangle behind the near plane. You can either:
- Adjust the triangle's Z coordinates to be between
-30and-90, or - Use
glTranslatefto shift the entire scene back so the triangle falls within the frustum.
2. No Rotation Applied
Your glRotatef(0.0, 0.0f, 1.0f, 0.0f) uses a fixed angle of 0.0, so no rotation happens. To make it spin, you need to increment the angle over time.
Fix: Add a global rotation angle variable, update it each frame, and use it in the rotation call.
3. Missing Continuous Redraw
Without triggering regular redraws, the triangle won't animate. You need to tell GLUT to refresh the display continuously.
Fix: Add an idle function that calls glutPostRedisplay().
Corrected Full Code Example
Here's the fixed code with all the changes:
#include <GL/glut.h> // Global rotation angle float rotationAngle = 0.0f; void init() { glEnable(GL_DEPTH_TEST); glEnable(GL_COLOR_MATERIAL); glClearColor(0.7f, 0.9f, 1.0f, 1.0f); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-30, 30, -30, 30, 30, 90); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // Shift the entire scene back 50 units to place it within the frustum glTranslatef(0.0f, 0.0f, -50.0f); // Apply rotation around the Y-axis with increasing angle glRotatef(rotationAngle, 0.0f, 1.0f, 0.0f); glBegin(GL_TRIANGLES); // Triangle vertices (now positioned within the view frustum) glColor3f(1.0f, 0.0f, 0.0f); // Red color for visibility glVertex3f(20.0f, 10.0f, 0.0f); glVertex3f(50.0f, 15.0f, -10.0f); glVertex3f(32.0f, 30.0f, 0.0f); glEnd(); glutSwapBuffers(); // Update rotation angle (adjust speed by changing the increment value) rotationAngle += 0.5f; if (rotationAngle > 360.0f) { rotationAngle -= 360.0f; } } // Idle function to trigger continuous redraws void idle() { glutPostRedisplay(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(800, 600); glutCreateWindow("Rotating 3D Triangle"); init(); glutDisplayFunc(display); glutIdleFunc(idle); // Attach the idle function glutMainLoop(); return 0; }
Key Changes Explained:
- Global
rotationAngle: Tracks the current rotation amount, incremented each frame to create smooth spinning. glTranslatef(0.0f, 0.0f, -50.0f): Moves the triangle back 50 units, placing it between the near (Z=-30) and far (Z=-90) planes so it's visible.- Dynamic rotation: Uses the updating
rotationAngleinglRotatefto rotate the triangle around the Y-axis. - Idle function: Ensures the display refreshes continuously, so the rotation animates smoothly.
- Added color: Sets a red color for the triangle to make it stand out against the light blue background.
内容的提问来源于stack exchange,提问作者Chitra

