- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
#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;
}
当我执行上面的程序时,它工作正常。但问题是,当我打印随机生成的变量值 r1
r2
r3
r4
时,它们正在打印 8 次或有时 12 次。这意味着 glutDisplayFunc(renderFunction);
多次调用 renderFunction
,这不是必需的。
如何控制这种行为。我希望只调用一次 renderFunction
。
更新:我想创建 4 条线,正好形成 4 条线,但是当我打印坐标时,它们显示出我上面提到的意外行为。
最佳答案
我在这里看到了几个问题。
较小的一个是您在 renderFunction
中对 for 循环的缩进有点误导。该循环总是恰好有 4 次迭代,每次都打印一次随机变量。
第二个是您似乎误解了 glutDisplayFunc(renderFunction);
的含义。正如解释的那样 here ,这只会将 renderFunction
注册为过剩的默认“显示”回调,并且:
GLUT determines when the display callback should be triggered based on the window's redisplay state.
glutMainLoop
将调用已注册的回调,直到程序完成。请注意,glutMainLoop
永远不会返回。程序被未处理的信号或主窗口关闭中断。这意味着如果您最小化并恢复窗口,glut 将要重新绘制其内容并再次调用显示回调 (renderFunction
)。
由于在 glut 调用 display 函数时您无法轻易控制,或者不应尝试控制,因此我建议您确保 display 函数只会绘制到屏幕上。您可以在 main
函数中生成(并打印)随机值,并将随机变量设置为全局变量,以便从 renderFunction
轻松访问它们。或者,通过在第一次执行时修改标志的条件语句来保护采样和打印的执行:
// at global scope (before renderFunction)
generated = false
declare random variables
...
// inside renderFunction
if (generated = false) {
generate RV
print RV
generated = true
}
display lines
无论哪种情况,您都需要存储所有 16 个随机变量,因为您生成了 4 个随机值 4 次(并且您将它们全部用于绘图/打印)。我建议将它们存储在 int r[4][4];
这是我喜欢的版本:
#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;
}
编辑原始发帖人在评论部分提出了一个有趣的问题转折。我将在这里解释一下:
If you require a large (10000+) number of lines and memory is a huge concern, can we still get the originally desired behavior:random values printed only once and lines not changing position every time the screen gets repainted.
答案是:是的!保持您最初拥有的非常低的内存占用的一种方法是使用“if”保护技巧的组合,每次通过 re-seeding the random number generator 打印和生成(相同的)随机值。 .
#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;
}
请注意,此方法与原始问题中的代码非常相似,添加的代码使用注释明确分隔。好的一面是,无论您要绘制多少行,它都使用恒定的内存量。不利的一面是调用 rand()
有点昂贵,因此您通常需要在高速(前一个版本)和低内存消耗(后一个版本)之间进行权衡。
关于c++ - opengl/glut 中的 renderDisplayFunc 不止一次调用 myfunc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18485690/
我想开始使用OpenGl开发图形程序 为了开始,我正在关注 OpenGL 我遇到过使用 GLUT 和不使用 GLUT 进行编程,但作为 OpenGL 的新手,我更加困惑如何使用它? 最佳答案 GLUT
我的应用程序在使用 wglCreateContext 上下文的 Windows 上仅使用 gl/glu。 该应用程序有自己的 key 管理器,现在我喜欢为输入框和选择列表实现 GUI 框架。 我找到了
我正在用 C++ 进行一些 OpenGL 编程。 这是我的代码的一部分: #include #include #include #include #include Properties.
我正在使用 Win32 API 处理 OpenGL。因此我正在使用 wgl (Wiggle)。一切安好。除非我想使用 FREEGLUT 库中的一些形状。例如,茶壶。我正在查看 freeglut 的源代
我正在尝试在 Haskell 中制作图形,并且一直在使用 Haskell.org 的教程 (http://www.haskell.org/haskellwiki/OpenGLTutorial1)。但是
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
我正在用 C 语言开发一个带有 GLUT 的应用程序。我有一个基本的 GLUT Canvas ,我可以在其中绘制 OpenGL 内容。 当其他 X11 窗口与 GLUT Canvas 重叠然后被删除时
我最近重新设计了我的应用程序,以便在 Idle 函数中完成数据收集。该程序从 argv[1] 读取文件并从该文件中解析出命令。这部分似乎都可以正常工作,并且解析的数据是准确的。 问题出在显示功能上。命
我遇到了一个奇怪的问题,当我以零延迟调用它时,glutTimerFunc 似乎随机停止工作。 这是我的代码: #include #include #include int x = 0; void
我正在处理一项作业,要求我仅使用隐藏线渲染图像。我在网上找到了有关如何删除隐藏线的内容,但我一直未能成功地弄清楚如何反转代码以使其仅绘制隐藏线。 通常,当我访问 Stack Overflow 时,我喜
我想将一个对象(立方体)乘以一定的数字,在本例中假设为 25,我确实有一个立方体的代码,并且它可以工作,但我不知道如何制作更多。我是 GLUT 的新人。 #include #include #in
嘿, 我正在尝试使用 C++ 使用 OpenGL 和 CUDA 创建场景,但是当我尝试传递函数进行初始化时收到错误消息。这是我的标题: class SimpleScene { public:
我有一个我在 6 个多月前编写的程序,它使用了 GLUT 库。 6 个月前我写的时候它运行得非常好。从那以后我就没有使用过它,也没有以任何方式编辑过代码。 该程序有一些立方体以一些随机速度四处漂浮,它
我有一个过剩的问题,我想要一个回调来知道用户何时移动我的应用程序的窗口,但我没有在过剩中发现任何东西。 我的目标是只在 Windows 上制作一个应用程序,是否可以使用 MFC 保留我的过剩代码来做到
我正在尝试在 GLUT c++ 上编写“Classic Snake”,但遇到了一些问题。 例如,当我的蛇向右移动时,我将目的地更改为向上,同时将其更改为向右蛇进入自身内部(我禁止将目的地从右更改为左)
我正在尝试使用以下摄像机代码实现摄像机控制的场景:http://www.swiftless.com/tutorials/opengl/camera2.html 还有书中的风景代码,随机生成地形。通过使
上周,我尝试了几个在阅读 GLUT 教程时发现的示例,一切都运行良好。 现在,当我重试这些相同的示例时,我得到了一个奇怪的行为:我的 GLUT 窗口显示了窗口所在位置的桌面部分(所以如果 GLUT 窗
我正在尝试使用以下方法将纹理渲染到平面: unsigned char image[HEIGHT][WIDTH][3]; ... GLuint textureId; glGenTextures(1, &
我编写了模拟选择排序操作的程序。我添加了函数 myKeyboard 来退出程序,但是由于使用函数 sleep() 来模拟动画,myKeyboard 仅在排序完成后才起作用。有什么方法可以替换函数 sl
当我编译后尝试运行这段代码时,我得到的只是一个没有内容的窗口边框,那么错误在哪里? 注意:我用的是ubuntu和gcc编译 gcc -lglut Simple.c -o Simple The out
我是一名优秀的程序员,十分优秀!