C++ opengl/glut中的renderDisplayFunc多次调用myfunc #包括“GL/glut.h” #包括“总账/总账h” #包括 #包括 使用名称空间std; #定义XWidth 700//剪裁窗口大小700*700 #定义YHeight 700 void renderFunction(){ /*清除上次绘制的信息 设置用于清除的当前清除颜色 RGBA模式下的颜色缓冲区。 */ glClearColor(0.0,0.0,0.0,0.0); glClear(GLU颜色缓冲位); //设置线宽 线宽(1); //(x,y)以像素为单位的坐标 glMatrixMode(GL_投影); glLoadIdentity(); 格洛托(0,XWidth,0,YHeight,-1,1); //设置线条颜色 GL3F(1.0,0.0,0.0); //随机数生成 对于(inti=0;i
,我在这里看到了几个问题 较小的一个是,在C++ opengl/glut中的renderDisplayFunc多次调用myfunc #包括“GL/glut.h” #包括“总账/总账h” #包括 #包括 使用名称空间std; #定义XWidth 700//剪裁窗口大小700*700 #定义YHeight 700 void renderFunction(){ /*清除上次绘制的信息 设置用于清除的当前清除颜色 RGBA模式下的颜色缓冲区。 */ glClearColor(0.0,0.0,0.0,0.0); glClear(GLU颜色缓冲位); //设置线宽 线宽(1); //(x,y)以像素为单位的坐标 glMatrixMode(GL_投影); glLoadIdentity(); 格洛托(0,XWidth,0,YHeight,-1,1); //设置线条颜色 GL3F(1.0,0.0,0.0); //随机数生成 对于(inti=0;i,c++,opengl,glut,C++,Opengl,Glut,,我在这里看到了几个问题 较小的一个是,在renderFunction中缩进for循环有点误导。该循环始终有4次迭代,每次打印一次随机变量 第二个问题是,您似乎误解了glutDisplayFunc(renderFunction);的含义。如前所述,这仅将glut的默认“显示”回调注册为renderFunction,并且: GLUT根据窗口的重新显示状态确定何时触发显示回调 将调用已注册的回调,直到程序完成。请注意,glutMainLoop永远不会返回。程序被未处理的信号或主窗口关闭中断。这意味着
renderFunction
中缩进for循环有点误导。该循环始终有4次迭代,每次打印一次随机变量
第二个问题是,您似乎误解了glutDisplayFunc(renderFunction);的含义。如前所述,这仅将glut的默认“显示”回调注册为renderFunction
,并且:
GLUT根据窗口的重新显示状态确定何时触发显示回调
将调用已注册的回调,直到程序完成。请注意,glutMainLoop
永远不会返回。程序被未处理的信号或主窗口关闭中断。这意味着,如果最小化并还原窗口,glut将希望重新绘制其内容,并将调用显示回调(renderFunction
)再次
由于您无法轻松控制,或者不应该尝试控制glut何时调用显示函数,因此我建议您确保显示函数只在屏幕上绘制。您可以生成(和打印)main
函数中的随机值,并且随机变量是全局的,以便可以从renderFunction
轻松访问。或者,通过条件语句保护采样和打印的执行,条件语句在第一次执行时修改标志:
#include "GL/glut.h"
#include "GL/gl.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
#define XWidth 700 // Clipping window size 700*700
#define YHeight 700
void renderFunction() {
/*Clear Information from last draw
Sets the current clearing color for use in clearing
color buffers in RGBA mode.
*/
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
//Set line width
glLineWidth(1);
//(x,y) coordinates as in pixels
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, XWidth, 0, YHeight, -1, 1);
//Set line color
glColor3f(1.0, 0.0, 0.0);
//random num generated
for(int i=0;i<4;i++){
int r1 = rand() % 1000;
int r2 = rand() % 1000;
int r3 = rand() % 1000;
int r4 = rand() % 1000;
//Begin LINE coordinates
glBegin(GL_LINES);
glVertex2d(r1, r2);
glVertex2d(r3,r4);
//End LINE coordinate
glEnd();
cout<<r1<<" "<<r2<<" "<<r3<<" "<<r4<<" i is "<<i<<endl;}
//Forces previously issued OpenGL commands to begin execution
glFlush();
}
// Driver program to test above functions
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
//Set Output Window Size
glutInitWindowSize(XWidth,YHeight);
//Set the position of Output window corresponding to Screen
glutInitWindowPosition(100,100);
//Create the Window
glutCreateWindow("OpenGL - Classify line among three classes");
//Set handler functions for drawing
glutDisplayFunc(renderFunction);
//Start the main loop
glutMainLoop();
return 0;
}
在任何一种情况下,您都需要存储所有16个随机变量,因为您将4次生成4个随机值(并且您将所有这些值都用于绘图/打印)。我建议将它们存储在int r[4][4];
以下是我的首选版本:
// at global scope (before renderFunction)
generated = false
declare random variables
...
// inside renderFunction
if (generated = false) {
generate RV
print RV
generated = true
}
display lines
#包括“GL/glut.h”
#包括“总账/总账h”
#包括
#包括
使用名称空间std;
#定义XWidth 700//剪裁窗口大小700*700
#定义YHeight 700
int r[4][4];
void renderFunction(){
/*清除上次绘制的信息
设置用于清除的当前清除颜色
RGBA模式下的颜色缓冲区。
*/
glClearColor(0.0,0.0,0.0,0.0);
glClear(GLU颜色缓冲位);
//设置线宽
线宽(1);
//(x,y)以像素为单位的坐标
glMatrixMode(GL_投影);
glLoadIdentity();
格洛托(0,XWidth,0,YHeight,-1,1);
//设置线条颜色
GL3F(1.0,0.0,0.0);
//随机数生成
对于(int i=0;iDoesrand()
连续8或12次为您提供相同的4个值集?是的,我得到了4个值集,但正如您所看到的,我也使用这些值绘制线,线是有限的(4或更少)。如果我必须生成10000多行,该怎么办?数组将需要大量内存。@VarunMalhotra此内存约束实际上不是最初的问题,但这是一个好问题。我将提供一个替代方案。
#include "GL/glut.h"
#include "GL/gl.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
#define XWidth 700 // Clipping window size 700*700
#define YHeight 700
int r[4][4];
void renderFunction() {
/*Clear Information from last draw
Sets the current clearing color for use in clearing
color buffers in RGBA mode.
*/
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
//Set line width
glLineWidth(1);
//(x,y) coordinates as in pixels
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, XWidth, 0, YHeight, -1, 1);
//Set line color
glColor3f(1.0, 0.0, 0.0);
//random num generated
for(int i=0;i<4;i++){
//Begin LINE coordinates
glBegin(GL_LINES);
glVertex2d(r[i][0], r[i][1]);
glVertex2d(r[i][2], r[i][3]);
//End LINE coordinate
glEnd();
}
//Forces previously issued OpenGL commands to begin execution
glFlush();
}
// Driver program to test above functions
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
//Set Output Window Size
glutInitWindowSize(XWidth,YHeight);
//Set the position of Output window corresponding to Screen
glutInitWindowPosition(100,100);
//Create the Window
glutCreateWindow("OpenGL - Classify line among three classes");
//Set handler functions for drawing
glutDisplayFunc(renderFunction);
//random num generated
for(int i=0;i<4;i++) {
r[i][0] = rand() % 1000;
r[i][1] = rand() % 1000;
r[i][2] = rand() % 1000;
r[i][3] = rand() % 1000;
cout<<r[i][0] << " " << r[i][1]<<" "<<r[i][2]<<" "<<r[i][3]<<" i is "<<i<<endl;
}
//Start the main loop
glutMainLoop();
return 0;
}
#include "GL/glut.h"
#include "GL/gl.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
#define XWidth 700 // Clipping window size 700*700
#define YHeight 700
bool printed;
int random_seed;
void renderFunction() {
/*Clear Information from last draw
Sets the current clearing color for use in clearing
color buffers in RGBA mode.
*/
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
//Set line width
glLineWidth(1);
//(x,y) coordinates as in pixels
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, XWidth, 0, YHeight, -1, 1);
//Set line color
glColor3f(1.0, 0.0, 0.0);
// =============================================
// set the seed of your random number generator
// ---------------------------------------------
srand(random_seed);
// =============================================
//random num generated
for(int i=0;i<4;i++) {
int r1 = rand() % 1000;
int r2 = rand() % 1000;
int r3 = rand() % 1000;
int r4 = rand() % 1000;
//Begin LINE coordinates
glBegin(GL_LINES);
glVertex2d(r1, r2);
glVertex2d(r3,r4);
//End LINE coordinate
glEnd();
// ***********************************************
// make sure the values are only printed the first
// time around
// ***********************************************
if (!printed) {
cout<<r1<<" "<<r2<<" "<<r3<<" "<<r4<<" i is "<<i<<endl;
}
}
printed = true;
//Forces previously issued OpenGL commands to begin execution
glFlush();
}
// Driver program to test above functions
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
//Set Output Window Size
glutInitWindowSize(XWidth,YHeight);
//Set the position of Output window corresponding to Screen
glutInitWindowPosition(100,100);
//Create the Window
glutCreateWindow("OpenGL - Classify line among three classes");
//Set handler functions for drawing
glutDisplayFunc(renderFunction);
// =======================================
// generate a random seed for the lines
// ---------------------------------------
srand(time(0));
random_seed = rand();
// =======================================
// ==========================================
// initialize the printing guard to "false",
// i.e. "did not print the random values yet"
// ------------------------------------------
printed = false;
// ==========================================
//Start the main loop
glutMainLoop();
return 0;
}