C++ OpenGL Glutsphere 位置问题

C++ OpenGL Glutsphere location issue

本文关键字:问题 位置 Glutsphere OpenGL C++      更新时间:2023-10-16

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

gltranslatef

只是为了更新我的贪吃球体位置,但无论我试图改变什么......它都会使所有其他不必要的对象移动到Windows屏幕设置的边缘,但结果是这样的。图像" .it 移动了我所有其他对象..我的代码有什么问题?

/* * 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充当状态机,这意味着设置会"一直存在",直到您再次更改它们。这包括转换状态。因此,当您致电:

glTranslatef(0.20,0.27,0.0);

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

如果这不是您想要的行为,则必须在绘制球体后将变换恢复到以前的状态。最简单的方法是使用 glPushMatrix()/glPopMatrix() .旧版 OpenGL 中的转换矩阵作为堆栈进行管理,推送/弹出矩阵将恢复以前的状态。

然后,围绕绘制球体的代码将如下所示:

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