C++ OpenGL Glutsphere 位置问题
C++ OpenGL Glutsphere location issue
我对我的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();
相关文章:
- 警告处理为错误这里有什么问题
- 这个返回元素位置的基于循环的函数有什么问题?
- 将文件复制到自定义位置,存在字符串转换问题
- 0xC0000005:访问冲突读取位置 0x00000000. 重载 == 运算符的问题
- 关于重载 -> 运算符中 const 关键字的特定位置的问题
- 关于引用数组位置的问题
- 恢复Qt窗口大小和位置问题
- 使用 Arduino 将位置编码器值转换为 RPM 时出现问题
- 自旋控制位置问题(Windows)
- C++ OpenGL Glutsphere 位置问题
- TMX问题,在正确位置绘制瓷砖(c++)
- OpenCV:内存位置问题
- 我的字节数组到十六进制字符串转换器有什么问题,或者为什么它在写入文件时会在某个位置后剪切符号?
- 访问冲突写入位置0x00000000.内存集函数问题
- qmessagebox问题,可以't改变其位置
- 写入位置0x00000000时发生访问冲突.指针问题
- COUT只在内存中打印位置的c++问题
- 如何处理随机访问ifstream使用?-位置/偏移量/尺寸问题
- 我的函数更新恒星附近行星的位置有什么问题?
- boost::program_options和位置参数的问题