簡體   English   中英

在opengl中用線程繪制頂點

[英]Draw vertices with thread in opengl

我正在使用帶有opengl glut庫並用它繪制圓圈。 圓在框架上成功繪制,但我想在線程中編譯這些圓頂點。 例如我把圓圈圈,之后每個頂點平局2seconds完成比它出現在幀的頂點,其秒鍾傳遞運行時間。 我正在使用sleep()函數但不能使用它。
碼:

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

如果我正確理解了您的問題,您需要為圓形繪制設置動畫。 OpenGL中的繪制命令不會立即發出 - 您需要繪制,然后將結果呈現給窗口。 因此,在繪圖功能中進行sleep將延遲演示。 使用您發布的代碼,您將在drawCircle循環的每次迭代中休眠2秒。 由於您傳遞的是segment=360 ,因此渲染圓圈大約需要12分鍾(在此期間您的應用程序似乎會掛起)。 可能,您應該在一個狀態下繪制圓形框架2秒,然后繪制下一個狀態。

要實現這一點,您應該刪除sleep ,並在display功能中使用計時器,這會隨着時間的推移增加segment參數。 例如:

#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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM