OpenGL массив точек анимации
Я пытаюсь анимировать случайные точки, которые я создал, чтобы они перемещались по экрану и снова появлялись.код не является завершенной версией того, что у меня есть я опубликовал только то, что я думал, будет достаточно для этого вопроса может ли кто-нибудь помочь мне анимировать пункты
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 ответ:
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; }
Comments