OpenGL 点数组动画

发布于 2024-12-13 22:40:11 字数 1079 浏览 1 评论 0原文

我正在尝试对我创建的随机点进行动画处理,以便它们在屏幕上移动并重新出现,这是我到目前为止所做的,但不起作用。代码不是我所拥有的完整版本,我只发布了内容我认为这个问题就足够了,有人可以帮我动画化这些点吗

void TimerFunc(int value)
{
  xpos[0]=xpos[0]+0.25;
  glutPostRedisplay(); 
  glutTimerFunc(25, TimerFunc, 1);
}
struct Point
{
  float rship;
};
std::vector< Point > points;
void display(void)
{

glClear (GL_COLOR_BUFFER_BIT);   /* clear window */
glColor3f(1.0, 1.0, 1.0);        /* white drawing objects */
/* define object to be drawn as a square polygon */   

//Draw points
glEnableClientState( GL_VERTEX_ARRAY );  
glVertexPointer( 2, GL_FLOAT, sizeof(Point), &points[0].x );   
glPointSize( 1 ); //1 pixel dot
glDrawArrays( GL_POINTS, 0, points.size() );
glDisableClientState( GL_VERTEX_ARRAY );
//glEnable( GL_POINT_SMOOTH );/*round smooth points*/
glFlush();     /* execute drawing commands in buffer */


}
int main(int argc, char** argv)
for( int i = 0; i <250; ++i )
{
    Point pt;
    pt.x = -100 + (rand() % 200);
    pt.y = -100 + (rand() % 200);
    points.push_back(pt);
    xpos[i] = pt.x;
    ypos[i] = pt.y;

}

I am trying to animate random point that i have created so they move across the screen and re-appear this is what i have done so far but does not work.the code is not the completed version of what i have i have only posted what i thought would be enough for this question could anyone help me animated the points

void TimerFunc(int value)
{
  xpos[0]=xpos[0]+0.25;
  glutPostRedisplay(); 
  glutTimerFunc(25, TimerFunc, 1);
}
struct Point
{
  float rship;
};
std::vector< Point > points;
void display(void)
{

glClear (GL_COLOR_BUFFER_BIT);   /* clear window */
glColor3f(1.0, 1.0, 1.0);        /* white drawing objects */
/* define object to be drawn as a square polygon */   

//Draw points
glEnableClientState( GL_VERTEX_ARRAY );  
glVertexPointer( 2, GL_FLOAT, sizeof(Point), &points[0].x );   
glPointSize( 1 ); //1 pixel dot
glDrawArrays( GL_POINTS, 0, points.size() );
glDisableClientState( GL_VERTEX_ARRAY );
//glEnable( GL_POINT_SMOOTH );/*round smooth points*/
glFlush();     /* execute drawing commands in buffer */


}
int main(int argc, char** argv)
for( int i = 0; i <250; ++i )
{
    Point pt;
    pt.x = -100 + (rand() % 200);
    pt.y = -100 + (rand() % 200);
    points.push_back(pt);
    xpos[i] = pt.x;
    ypos[i] = pt.y;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟若柳尘 2024-12-20 22:40:11

又快又脏:

#include <GL/glut.h>
#include <vector>
#include <cstdlib>

struct Point
{
    float x, y;
    unsigned char r, g, b, a;
};
std::vector< Point > points;

void timer( int value )
{
    // move all points left each frame
    for( size_t i = 0; i < points.size(); ++i )
    {
        points[i].x -= 1;

        // wrap point around if it's moved off
        // the edge of our 100x100 area
        if( points[i].x < -50 )
        {
            points[i].x = 100 + points[i].x;
        }
    }

    glutPostRedisplay();
    glutTimerFunc(30, timer, 1);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-50, 50, -50, 50, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // draw
    glColor3ub( 255, 255, 255 );
    glEnableClientState( GL_VERTEX_ARRAY );
    glEnableClientState( GL_COLOR_ARRAY );
    glVertexPointer( 2, GL_FLOAT, sizeof(Point), &points[0].x );
    glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(Point), &points[0].r );
    glPointSize( 3.0 );
    glDrawArrays( GL_POINTS, 0, points.size() );
    glDisableClientState( GL_VERTEX_ARRAY );
    glDisableClientState( GL_COLOR_ARRAY );

    glFlush();
    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

    glutInitWindowSize(640,480);
    glutCreateWindow("Scrolling Points");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutTimerFunc(30, timer, 1);

     // populate points
    for( size_t i = 0; i < 1000; ++i )
    {
        Point pt;
        pt.x = -50 + (rand() % 100);
        pt.y = -50 + (rand() % 100);
        pt.r = rand() % 255;
        pt.g = rand() % 255;
        pt.b = rand() % 255;
        pt.a = 255;
        points.push_back(pt);
    }    

    glutMainLoop();
    return 0;
}

Quick 'n dirty:

#include <GL/glut.h>
#include <vector>
#include <cstdlib>

struct Point
{
    float x, y;
    unsigned char r, g, b, a;
};
std::vector< Point > points;

void timer( int value )
{
    // move all points left each frame
    for( size_t i = 0; i < points.size(); ++i )
    {
        points[i].x -= 1;

        // wrap point around if it's moved off
        // the edge of our 100x100 area
        if( points[i].x < -50 )
        {
            points[i].x = 100 + points[i].x;
        }
    }

    glutPostRedisplay();
    glutTimerFunc(30, timer, 1);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-50, 50, -50, 50, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // draw
    glColor3ub( 255, 255, 255 );
    glEnableClientState( GL_VERTEX_ARRAY );
    glEnableClientState( GL_COLOR_ARRAY );
    glVertexPointer( 2, GL_FLOAT, sizeof(Point), &points[0].x );
    glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(Point), &points[0].r );
    glPointSize( 3.0 );
    glDrawArrays( GL_POINTS, 0, points.size() );
    glDisableClientState( GL_VERTEX_ARRAY );
    glDisableClientState( GL_COLOR_ARRAY );

    glFlush();
    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

    glutInitWindowSize(640,480);
    glutCreateWindow("Scrolling Points");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutTimerFunc(30, timer, 1);

     // populate points
    for( size_t i = 0; i < 1000; ++i )
    {
        Point pt;
        pt.x = -50 + (rand() % 100);
        pt.y = -50 + (rand() % 100);
        pt.r = rand() % 255;
        pt.g = rand() % 255;
        pt.b = rand() % 255;
        pt.a = 255;
        points.push_back(pt);
    }    

    glutMainLoop();
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文