C++ OpenGL Glutsphere 位置问题

C++ OpenGL Glutsphere location issue

我的 OpenGL 代码有问题,我尝试使用

glTranslatef

只是为了更新我的 glutsolidsphere 位置,但无论我尝试更改什么..它都会让所有其他不必要的对象移动到 windows 屏幕设置的边缘,但结果是这样的.. “the image”。它移动了我所有的其他对象。我的代码有什么问题?

/* * hello.c * This is a simple, 
introductory OpenGL program. */ 
#include <GL/glut.h> 

void display(void) 
{
/* clear all pixels */ 
glClear (GL_COLOR_BUFFER_BIT); 
/* draw white polygon (rectangle) with corners at 
*(0.25, 0.25, 0.0) and (0.75, 0.75, 0.0) */ 
glColor3f (1.0, 0.0, 0.0); 
glBegin(GL_TRIANGLES); 
glVertex3f (0.50, 0.70, 0.0); 
glVertex3f (0.40, 0.60, 0.0); 
glVertex3f (0.60, 0.60, 0.0);  
glEnd();
glFlush ();

glColor3f (0.0, 1.0, 0.0); 
glBegin(GL_POLYGON); 
glVertex3f (0.40, 0.20, 0.0); 
glVertex3f (0.60, 0.20, 0.0); 
glVertex3f (0.60, 0.40, 0.0);
glVertex3f (0.40, 0.40, 0.0);  
glEnd();
glFlush ();

glColor3f (0.0, 1.0, 1.0); 
glBegin(GL_POLYGON); 
glVertex3f (0.30, 0.30, 0.0); 
glVertex3f (0.32, 0.30, 0.0); 
glVertex3f (0.40, 0.38, 0.0);
glVertex3f (0.40, 0.40, 0.0);
glVertex3f (0.30, 0.40, 0.0);
glEnd();
glFlush ();

glColor3f (0.0, 1.0, 1.0); 
glBegin(GL_POLYGON); 
glVertex3f (0.60, 0.38, 0.0); 
glVertex3f (0.68, 0.30, 0.0); 
glVertex3f (0.70, 0.30, 0.0);
glVertex3f (0.70, 0.40, 0.0);
glVertex3f (0.60, 0.40, 0.0);
glEnd();
glFlush ();

glColor3f (0.0, 0.0, 1.0); 
glBegin(GL_LINE_STRIP); 
glVertex3f (0.25, 0.24, 0.0);
glVertex3f (0.30, 0.29, 0.0);
glVertex3f (0.35, 0.24, 0.0); 
glVertex3f (0.40, 0.29, 0.0); 
glVertex3f (0.45, 0.24, 0.0);
glVertex3f (0.50, 0.29, 0.0);
glVertex3f (0.55, 0.24, 0.0);
glVertex3f (0.60, 0.29, 0.0); 
glVertex3f (0.65, 0.24, 0.0);
glVertex3f (0.70, 0.29, 0.0);
glVertex3f (0.75, 0.24, 0.0);
glEnd();
glFlush ();

glColor3f (0.0, 1.0, 1.0); 
glBegin(GL_LINE_STRIP); 
glVertex3f (0.25, 0.19, 0.0);
glVertex3f (0.30, 0.24, 0.0);
glVertex3f (0.35, 0.19, 0.0); 
glVertex3f (0.40, 0.24, 0.0); 
glVertex3f (0.45, 0.19, 0.0);
glVertex3f (0.50, 0.24, 0.0);
glVertex3f (0.55, 0.19, 0.0);
glVertex3f (0.60, 0.24, 0.0); 
glVertex3f (0.65, 0.19, 0.0);
glVertex3f (0.70, 0.24, 0.0);
glVertex3f (0.75, 0.19, 0.0);
glEnd();
glFlush ();

glColor3f (0.0, 0.0, 1.0); 
glBegin(GL_LINE_STRIP); 
glVertex3f (0.25, 0.14, 0.0);
glVertex3f (0.30, 0.19, 0.0);
glVertex3f (0.35, 0.14, 0.0); 
glVertex3f (0.40, 0.19, 0.0); 
glVertex3f (0.45, 0.14, 0.0);
glVertex3f (0.50, 0.19, 0.0);
glVertex3f (0.55, 0.14, 0.0);
glVertex3f (0.60, 0.19, 0.0); 
glVertex3f (0.65, 0.14, 0.0);
glVertex3f (0.70, 0.19, 0.0);
glVertex3f (0.75, 0.14, 0.0);
glEnd();
glFlush ();

glColor3f (1.0, 1.0, 0.0); 
glTranslatef(0.20,0.27,0.0);
glutSolidSphere(0.06, 20, 20); 
glFlush ();
/* don't wait! 
* start processing buffered OpenGL routines */ 
} 
void init (void) 
{ 
/* select clearing color */ 
glClearColor (1.0, 1.0, 1.0, 1.0);
/* initialize viewing values */ 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); 
}
/* 
* Declare initial window size, position, and display mode 
* (single buffer and RGBA). Open window with "hello" 
* in its title bar. Call initialization routines. 
* Register callback function to display graphics. 
* Enter main loop and process events. */ 
int main(int argc, char** argv) 
{ 
glutInit(&argc, argv); 
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 
glutInitWindowSize (900, 600); 
glutInitWindowPosition (50, 100); 
glutCreateWindow ("hello"); 
init (); 
glutDisplayFunc(display); 
glutMainLoop(); 
return 0; 
/* ANSI C requires main to return int. */ 
}

大部分 OpenGL 充当状态机,这意味着设置 "stick" 直到您再次更改它们。这包括转换状态。所以当你打电话时:

glTranslatef(0.20,0.27,0.0);

您在此点之后绘制的所有内容 都将被翻译。如果你多次调用glTranslatef(),转换会累积,这意味着你会翻译得越来越多。

如果这不是您想要的行为,您必须在绘制球体后将变换恢复到之前的状态。最简单的方法是使用 glPushMatrix()/glPopMatrix()。遗留 OpenGL 中的变换矩阵作为堆栈进行管理,pushing/popping 矩阵将恢复之前的状态。

绘制球体的代码将如下所示:

glPushMatrix();
glTranslatef(0.2f, 0.27f , 0.0f);
glutSolidSphere(0.06, 20, 20);
glPopMatrix();