使用 Ortho2D 移动当前图形在 GLUT 中不起作用
Moving current drawings using Ortho2D doesn't work in GLUT
我有一个用C++/OpenGL做的迷你项目,它允许用户使用鼠标在屏幕上绘制形状。
现在我想允许用户使用他的键盘左右移动/平移屏幕,假设屏幕上有 4 个形状在不同的地方,我希望它们在按下"A"键时全部向左移动。
我已经设法在以前的项目中做到了这一点,甚至放大/缩小,但在这个程序中有些东西不起作用,我设法做的最接近的事情是用户在按下"A"键后创建的所有新形状都在新位置而不是他的鼠标所在位置。
这是我的主要,初始化,显示和键盘代码:
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(W_WIDTH, W_HEIGHT);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpenGL Excercise");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutPassiveMotionFunc(My_mouse_routine);
createMenu();
glutMainLoop();
}
void init()
{
// Select a clear color
glClearColor(0.933, 0.933, 0.933, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
gluOrtho2D(0, W_WIDTH, W_HEIGHT, 0);
}
void display() {/* clear all pixels */
// draw text at the bottom
char* text_to_draw = "Press right mouse button for menu...";
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, W_WIDTH, 0, W_HEIGHT);
glColor3f(0.129, 0.129, 0.129);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glRasterPos2i(10, W_HEIGHT - 15); // move in 10 pixels from the left and bottom edges
for (int i = 0; i < (int)strlen(text_to_draw); ++i) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, text_to_draw[i]);
}
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glFlush();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
case 'A':
case 'a':
glMatrixMode(GL_PROJECTION); //start editing the projection matrix
glLoadIdentity(); //remove current projection
glPopMatrix();
gluOrtho2D(1,0,1,0); //create new one
glPushMatrix();
glMatrixMode(GL_MODELVIEW); //back to editing the modelview matrix
glFlush();
default:
break;
}
glutPostRedisplay();
}
最后,下面是一个矩形绘图的示例:
GLfloat colors[][3] = { { 0.957f, 0.263f, 0.212f },{ 0.298f, 0.686f, 0.314f },{ 0.129f, 0.588f, 0.953f }, };
static int col = 0;
glColor3f(colors[col][0], colors[col][1], colors[col][2]);
glBegin(GL_POLYGON);
glVertex3f(mouse_x - 50, mouse_y - 50, 0.0);
glVertex3f(mouse_x + 50, mouse_y - 50, 0.0);
glVertex3f(mouse_x + 50, mouse_y + 50, 0.0);
glVertex3f(mouse_x - 50, mouse_y + 50, 0.0);
glEnd(); /* don't wait!!! start processing buffered OpenGL routines */
col += 1;
if (col == sizeof(colors) / sizeof(colors[0])) {
col = 0;
}
glutPostRedisplay();
我知道在这个网站和数百个其他网站上有很多关于这个问题的问题,我几乎肯定它与矩阵有关,但我不知道我做错了什么。
-
为什么要在击键
A
更改投影矩阵?我假设你想使用 WSAD(这对像我这样的老派 OPQAM 人来说非常不舒服)来平移视图。
第一个问题是您在击键时更改
GL_PROJECTION
A
但在呈现时重置它display()
呈现击键代码而不做任何事情。为了解决这个问题,您应该在init()
函数中初始化GL_PROJECTION
。顺便说一句,您在那里缺少glMatrixMode
并将其从display()
中删除.另一个问题是您在
GL_PROJECTION
中使用相机/视图矩阵,而不是称为GL_MODELVIEW
:- GL_PROJECTION矩阵滥用。
当您使用高级照明和雾时,这将导致您出现严重问题(除非您切换到 GLSL)。
最后,当您通过正交设置视图时,您应该仅平移
glTranslate
这至少是我的观点。 -
您的场景存储在哪里?
从您的文本中,我认为这应该是一些简单的矢量绘画,例如能够在场景中添加/绘制形状的编辑器。因此,您需要某种能够存储场景所有信息的列表,例如:
- 对象的类型
- 控制点
- 颜色
在您的代码中没有看到此类信息,相反,我看到直接使用鼠标坐标,这对于预览最后添加的点很好,但对于更复杂的形状(如线条)会失败。
下面来看看:
- C++中的简单拖放示例
对于如何完成的一些想法。
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- 使用 Ortho2D 移动当前图形在 GLUT 中不起作用
- 在Visual Studio中使用GLUT库,没有错误,但输出控制台不起作用
- VS 2012内置的GLUT -不起作用