在动画的opengl场景中绘制

时间:2018-01-28 13:40:14

标签: c++ opengl jquery-animate glut

当我点击鼠标按钮时,我在动画场景中绘制烟花效果时出现问题。为什么不画画?

我的代码:

#include<GL/glut.h>  
struct Point {
GLint x;
GLint y;
};
Point p1, p2;

int ww=600,wh=400;  

int xi,yi,xf,yf,y1b,x1b,y2b,x2b;
float px, py, t; 
float x=0.,y=0.,x1=5.;


void update()
{
   x+=0.01;
   x1 -= 0.02;

   if (x>6)
   {
       x -= 6;
       x1 = 4;

   }
}

在那里,我创建了一个基于贝塞尔曲线绘制焰火效果的函数。如果我在静态窗口上绘图,它将是Okey。

// Bezier curve firework
void bezier(int xi, int yi, int xf, int yf)
{

    // Coordinates for additional points of bezier curve
    x1b = xi + rand()%15;
    y1b = yi + rand()%5;
    x2b = xf + rand()%15;
    y2b = xf + rand()%5;

    calculate and draw the curves
    for (t=0.;t<=1.;t+=0.001)
    {
        px=(1-t)*(1-t)*(1-t)*xi+3*t*(1-t)*(1-t)*x1b+3*t*t*(1-t)*x2b+t*t*t*xf; 
        py=(1-t)*(1-t)*(1-t)*yi+3*t*(1-t)*(1-t)*y1b+3*t*t*(1-t)*y2b+t*t*t*yf;
        glPointSize(2);
        glBegin(GL_POINTS);
            //glColor3f(1,0,0);
            glVertex2d(px,py);
        glEnd();
        glFlush();
    }

}  


void initRendering()
{
    glEnable(GL_DEPTH_TEST);
}

void reshaped(int w , int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, (double)w/(double)h,1,200);
}

// If pressed ESC -> exit
void keyPressed(unsigned char k, int x, int y)
{
    if(k==27)
    {
        exit(0);
    }
}

然后,如果我按下鼠标按钮,它应该调用上面的函数并绘制我需要的东西。但没什么(

// If pressed mouse button -> draw firework effect
void mousePressed(int button, int state, int x, int y)
{

    if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
    {


             xi=x;  
             yi=wh-y;  


             xf=x + 5.;  

             p1.x = xi; p1.y = yi;
             p2.x = xf; p2.y = yi;

             //drawLine(xi,yi,xf,yi);
             bezier(xi, yi,xf, yi);



    }
glutPostRedisplay();
}

在那里我创建了一个动画窗口。两条云在水平波浪中移动。

// Display a two moving clouds and the earth
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();
    glPushMatrix();
    glTranslatef(x1,y,-5.0);
    glBegin(GL_POLYGON);

        glColor3f(1.,0.,0.5);

        glVertex3f(-1.,1.,-5.);
        glVertex3f(0.,2.,-5.);
        glVertex3f(-2.,2.,-5.);
        glVertex3f(1.,1.,-5.);

    glEnd();
    glPopMatrix();

    glPushMatrix();

    glTranslatef(x,y,-5.);

    glBegin(GL_POLYGON);

        glColor3f(0.,0.5,0.5);

        glVertex3f(1.,0.7,-5.);

        glVertex3f(1.5,1.0,-5.0);

        glVertex3f(0.7,1.5,-5.0);

        glVertex3f(0.0,2.0,-5.0);

        glVertex3f(-0.7,1.5,-5.0);

        glVertex3f(-1.4,1.6,-5.0);

        glVertex3f(-1.7,1.0,-5.0);

        glVertex3f(-1.5,0.7,-5.0);

        glVertex3f(-1.0,0.5,-5.0);

    glEnd();

    glPopMatrix();

    glBegin(GL_POLYGON);

        glColor3f(1.,1.,1.5);

        glVertex3f(-2.,-2.,-5.);
        glVertex3f(-2.0,-2.0,-5.0);
        glVertex3f(-1.0,-1.5,-5.0);
        //glVertex3f(0.0,0.0,-5.0);
        glVertex3f(2.0,-2.0,-5.0);
        glVertex3f(1.2,-1.5,-5.0);

    glEnd();

    update();

    glutSwapBuffers();



    glFlush();  

}

void myinit()  
{  
   glViewport(0,0,ww,wh);  
   glMatrixMode(GL_MODELVIEW);  
   glLoadIdentity();  
   gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);  
   glMatrixMode(GL_MODELVIEW);  
}  

int main(int argc,char **argv)
{
    // Initialization
    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);

    glutInitWindowPosition(100,100);

    glutInitWindowSize(400,400);

    glutCreateWindow("Salute | Clouds");

    initRendering();



    // Registration

    glutDisplayFunc(display);

    glutIdleFunc(display);

    glutReshapeFunc(reshaped);

    // Handler of

    myinit();

    glutKeyboardFunc(keyPressed);
    glutMouseFunc(mousePressed);

    // Main Loop
    glutMainLoop();

    return(0);
 }

我认为问题如下: 我试图在更新的动画窗口中绘制烟花。每次我点击屏幕,它都会更新。最后,没有什么是可见的。 其实这个问题: 如何使函数glutMoseFunk在更新的窗口中得到我的敬礼?

1 个答案:

答案 0 :(得分:0)

当您的场景以透视投影绘制时,函数bezier适用于正交投影。这意味着您必须在调用bezier时更改投影矩阵。

此外,在主循环(display函数)中进行所有渲染。事件mousePressed只应用于更改参数(设置xiyi,...)。

显示功能可能如下所示:

int ww=400, wh=400;

void display()
{
    // clear the frame buffer
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    // setup perspective projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, (double)ww/(double)wh,1,200);

    // set model view matrix (identity matrix)
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();  

    // draw scene
    .....

    // setup ortihgraphic projection
    glMatrixMode(GL_PROJECTION);  
    glLoadIdentity();  
    gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);

    bezier(xi, yi,xf, yi);
    update();

    glutSwapBuffers();
    glutPostRedisplay(); 
}

reshaped函数应设置视口并仅注意窗口大小:

void reshaped(int w , int h)
{
    ww = w;
    wh = h;
    glViewport(0, 0, ww, wh);
}