繁体   English   中英

绘制动画的OpenGL场景

[英]draw in animated opengl scene

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

我的代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM