点动画OpenGL的数组 [英] OpenGL array of points animation

查看:189
本文介绍了点动画OpenGL的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动画,我已经建立,因此在屏幕上移动并重新出现在这个随机点是我迄今所做的,但不work.the code没有的完整版本我有我只贴了我认为应该够这个问题,任何人都可以帮助我的动画点

 无效TimerFunc(int值)
{
  XPOS [0] = XPOS [0] 0.25;
  glutPostRedisplay();
  glutTimerFunc(25,TimerFunc,1);
}
结构点
{
  浮动rship;
};
的std ::矢量<点>分;
无效的显示(无效)
{

glClear(GL_COLOR_BUFFER_BIT); / *明确的窗口* /
glColor3f(1.0,1.0,1.0); / *白绘图对象* /
/ *定义对象被绘制为正方形多边形* /

//画点
glEnableClientState(GL_VERTEX_ARRAY);
的glVertexPointer(2,GL_FLOAT,的sizeof(点),放大器;点[0] .X);
glPointSize(1); // 1个像素点
(4,8 GL_POINTS,0,points.size());
glDisableClientState(GL_VERTEX_ARRAY);
//过glEnable(GL_POINT_SMOOTH); / *圆润流畅的点* /
glFlush(); / *缓冲区*执行绘图命令/


}
INT主(INT ARGC,字符** argv的)
的for(int i = 0; I< 250; ++ I)
{
    点角;
    pt.x = -100 +(RAND()%200);
    pt.y = -100 +(RAND()%200);
    points.push_back(PT);
    XPOS [我] = pt.x;
    ypos [我] = pt.y;

}
 

解决方案

快速'N脏的:

 的#include< GL / glut.h>
#包括<载体>
#包括< cstdlib>

结构点
{
    浮动的x,y;
    unsigned char型R,G,B,A,
};
的std ::矢量<点>分;

无效定时器(int值)
{
    //向左移动每一帧的所有点
    用于(为size_t I = 0; I< points.size(); ++ I)
    {
        点[I] .X  -  = 1;

        //包点,如果围绕它移出
        //我们的100×100区域的边缘
        如果(分[I] .X< -50)
        {
            点[I] .X = 100 +点[I] .X;
        }
    }

    glutPostRedisplay();
    glutTimerFunc(30,计时器,1);
}

无效的显示(无效)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

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

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // 绘制
    glColor3ub(255,255,255);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    的glVertexPointer(2,GL_FLOAT,的sizeof(点),放大器;点[0] .X);
    glColorPointer(4 GL_UNSIGNED_BYTE,的sizeof(点),放大器;点[0] .R);
    glPointSize(3.0);
    (4,8 GL_POINTS,0,points.size());
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);

    glFlush();
    glutSwapBuffers();
}

无效重塑(INT W,INT高)
{
    glViewport(0,0,W,H);
}

INT主(INT ARGC,字符** argv的)
{
    glutInit(安培; ARGC,ARGV);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

    glutInitWindowSize(640,480);
    glutCreateWindow(滚动点);

    glutDisplayFunc(显示);
    glutReshapeFunc(重塑);
    glutTimerFunc(30,计时器,1);

     //填充点
    用于(为size_t I = 0; I< 1000; ++ I)
    {
        点角;
        pt.x = -50 +(RAND()%100);
        pt.y = -50 +(RAND()%100);
        pt.r =兰特()%255;
        pt.g =兰特()%255;
        pt.b =兰特()%255;
        pt.a = 255;
        points.push_back(PT);
    }

    glutMainLoop();
    返回0;
}
 

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;

}

解决方案

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

这篇关于点动画OpenGL的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆