3
votes

I am using glut library with opengl and draw circle with it. 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. I am using sleep() function but doesn't work with it.
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;
}
1
If I understand correctly you want to draw a circle then wait 2 seconds then draw again. If that is what you want, you have the sleep in the wrong place. By having the sleep inside the segment for loop you are sleeping between each vertex point, but if you want to sleep between successive draws of the circle, move the sleep to after the glEnd() call or into your main display method. - Jere

1 Answers

2
votes

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. So, having a sleep within a drawing function will delay the presentation. With the code that you've posted, you're sleeping for 2 seconds each iteration of the loop within drawCircle. 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). Likely, you should draw frames with the circle in one state for 2 seconds, and then draw the next state.

To achieve this, you should remove the sleep, and have a timer within your display function, which increases the segment parameter as time passes. 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();
}