[英]Draw vertices with thread in opengl
I am using glut library with opengl
and draw circle with it. 我正在使用带有
opengl
glut库并用它绘制圆圈。 Circle is draw successfully on frame but what i want compile these circle vertices in a thread. 圆在框架上成功绘制,但我想在线程中编译这些圆顶点。 For example i put in circle loop, Every vertex draw after
2seconds
is completed than on run time it appear that vertex on frame which seconds are passed. 例如我把圆圈圈,之后每个顶点平局
2seconds
完成比它出现在帧的顶点,其秒钟传递运行时间。 I am using sleep()
function but doesn't work with it. 我正在使用
sleep()
函数但不能使用它。
Code: 码:
#include <iostream>
#include <cstdlib>
#include <GL/glut.h>
#include<windows.h>
#include <cmath>
#define M_PI 3.14159265358979323846
using namespace std;
void init(void) {
glClearColor(0.0f,0.0f,0.0f,0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case '\x1B':
exit(EXIT_SUCCESS);
break;
}
}
void drawCircle(float pointX, float pointY, float Radius, int segment)
{
glBegin(GL_LINE_LOOP);
for (int i = 0; i < segment; i++)
{
float thetha= i * (2.0f * (float)M_PI / segment);
float x = Radius * cos(thetha);
float y = Radius * sin(thetha);
Sleep(2000);
glVertex2f(x + pointX, y + pointY);
}
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
drawCircle(0.0f, 0.0f, 0.80f, 360);
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(590, 590);
glutInitWindowPosition(50,50);
glutCreateWindow("Frame");
init();
glutKeyboardFunc(&keyboard);
glutDisplayFunc(&display);
glutMainLoop();
return EXIT_SUCCESS;
}
If I understand your question correctly, you want to animate the drawing of the circle. 如果我正确理解了您的问题,您需要为圆形绘制设置动画。 Draw commands in OpenGL are not issued immediately - you need to draw, and then present the results to the window.
OpenGL中的绘制命令不会立即发出 - 您需要绘制,然后将结果呈现给窗口。 So, having a
sleep
within a drawing function will delay the presentation. 因此,在绘图功能中进行
sleep
将延迟演示。 With the code that you've posted, you're sleeping for 2 seconds each iteration of the loop within drawCircle
. 使用您发布的代码,您将在
drawCircle
循环的每次迭代中休眠2秒。 Since you're passing in segment=360
, it will take about 12 minutes to render your circle (and your application will appear to hang during this time). 由于您传递的是
segment=360
,因此渲染圆圈大约需要12分钟(在此期间您的应用程序似乎会挂起)。 Likely, you should draw frames with the circle in one state for 2 seconds, and then draw the next state. 可能,您应该在一个状态下绘制圆形框架2秒,然后绘制下一个状态。
To achieve this, you should remove the sleep
, and have a timer within your display
function, which increases the segment
parameter as time passes. 要实现这一点,您应该删除
sleep
,并在display
功能中使用计时器,这会随着时间的推移增加segment
参数。 Eg: 例如:
#include <ctime>
// ...
void display()
{
static clock_t startTime = clock(); // NOTE: evaluated only once
clock_t currentTime = clock();
float timeLength = 2.0f * CLOCKS_PER_SEC;
float circlePercentage = (currentTime - startTime) / timeLength;
circlePercentage = circlePercentage >= 1.0f ? 1.0f : circlePercentage; //clamp
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
drawCircle(0.0f, 0.0f, 0.80f, static_cast<int>(circlePercentage * 360));
glFlush();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.