OpenGL数组点动画

时间:2011-11-04 22:49:42

标签: arrays opengl animation 3d 2d

我正在尝试为我创建的随机点设置动画,以便它们在屏幕上移动并重新显示这是我到目前为止所做的但是不起作用。代码不是我拥有的完整版本只发布了我认为足以解决这个问题,任何人都可以帮我动画积分

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;

}

1 个答案:

答案 0 :(得分:2)

快点肮脏:

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