我是过剩的新手,我无法解决这个问题。 我在鼠标点击上画了一个点,但总是只有最后一点是可见的。如何让旧点不会消失,每次点击只需再添加一个点? (不保存所有的点作为变量或什么,只是让它们在绘制新的时不会从窗口中消失) 非常感谢。
#include <iostream>
#include <math.h>
#include "gl/glut.h"
#include <windows.h>
using namespace std;
typedef struct {
float x;
float y;
} Point;
typedef struct {
int r;
int g;
int b;
} Color;
Point novy;
Color nova;
void Display(void){
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(8);
glBegin(GL_POINTS);
glColor3f(nova.r,nova.g,nova.b);
glVertex2f(novy.x,novy.y);
glEnd();
glFlush();
}
void onMouse(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
novy.x = x / (double) 300 * (1 - (-1)) + (-1);
novy.y = (1 - y / (double) 300) * (1 - (-1)) + (-1);
glutPostRedisplay();
}
}
int main(void){
cout << "suradnice \n";
cin >> novy.x;
cin >> novy.y;
cout << "rgb farba \n";
cin >> nova.r;
cin >> nova.g;
cin >> nova.b;
glutCreateWindow("This is the window title");
glutDisplayFunc(Display);
glutMouseFunc(onMouse);
glutMainLoop();
return 0;
}
答案 0 :(得分:0)
在opengl中,一次又一次地处理显示功能。在main函数的代码中,你没有定义类似的函数:
glutIdleFunc(display);
因此,当您单击鼠标时,您的代码会进入显示功能。当然,你的显示功能会占用鼠标的坐标并绘制这一点。(不要在你的代码中写下这一行,这只是opengl工作原理的一个例子)
显示功能可以独立地一次又一次地绘制每一帧。不要期望记住它以前的绘制。如果你想让你的其他点坐标在屏幕上,你应该将你以前的点击坐标保留在一个数组中并绘制每一个你的显示功能。