在动画OpenGL场景中绘制

draw in animated opengl scene

本文关键字:绘制 动画 OpenGL      更新时间:2023-10-16
当我

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

我的代码:

#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 在更新的窗口中向我致敬?

在透视投影中绘制场景时,该函数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);
}