简体   繁体   中英

Draw vertices with thread in opengl

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;
}

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM