C++ 在同一窗口中绘制三维和二维图形openGl无法正常工作

C++ 在同一窗口中绘制三维和二维图形openGl无法正常工作,c++,opengl,glut,opengl-compat,C++,Opengl,Glut,Opengl Compat,我正在尝试在同一个窗口中绘制一些3d(太阳)和2d(地面、云等)形状,它确实有效!但问题是,当我运行程序时,它不会显示平滑的屏幕,它会先显示一个形状,然后显示另一个形状(第一个形状消失),然后显示下一个形状,依此类推。。。 这是代码,比你强 #include<windows.h> #include<stdio.h> #include<GL/glut.h> #include<stdlib.h> #include<math.h> #incl

我正在尝试在同一个窗口中绘制一些3d(太阳)和2d(地面、云等)形状,它确实有效!但问题是,当我运行程序时,它不会显示平滑的屏幕,它会先显示一个形状,然后显示另一个形状(第一个形状消失),然后显示下一个形状,依此类推。。。 这是代码,比你强

#include<windows.h>
#include<stdio.h>
#include<GL/glut.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define CIRCLE_RADIUS  0.15f
# define PI 3.14159265358979323846
int eggs_caught = 0, missed_eggs = 0, level_count = 1, points = 0;


int egg_xc, egg_yc;
// for coordinates of egg
int basket_x, basket_y;
// for coordinates of basket
int a = 600, b = 650; // for default size of the screen
int s = 0;
// for menu option
int dropped_eggs = 0;
int speed_1 = 1, speed_2 = 1.5, speed_3 = 2, speed_4 = 2.5;
int w = 48, h = 48, t = 10, e = 9, g = 12;
void myinit();
void start_screen(int, int);
void cloud1();
void egg();
void basket(int, int);
void duck(int, int);
void print_score();
void egg_start();
void color();
void score();
void display(void);
void basket_set(int, int);
void myReshape(int, int);
void keys(unsigned char, int, int);
void menu(int);
void myinit()
{

}


void sun()
{
    GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
    GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
    GLfloat direction[] = { 1.0, 1.0, 1.0, 0.0 };

    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, yellow);
    glMaterialfv(GL_FRONT, GL_SPECULAR, yellow);
    glMaterialf(GL_FRONT, GL_SHININESS, 30);

    glLightfv(GL_LIGHT0, GL_AMBIENT, black);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, yellow);
    glLightfv(GL_LIGHT0, GL_SPECULAR, yellow);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();

    glPushMatrix();
    glTranslatef(0, 0.0, 0);
    glutSolidSphere(0.5, 30, 30);
    glPopMatrix();

    glPopMatrix();
    glFlush();

    glDisable(GL_LIGHTING);
    glDisable(GL_LIGHT0);
    glDisable(GL_DEPTH_TEST);

}
void cloud1()
{

    float theta;
    GLfloat angle;
    glLineWidth(1.5);
    glColor3f(1, 1, 1);
    glBegin(GL_POLYGON);
    for (int i = 0; i < 360; i++)
    {
        theta = i * PI * i / 180;
        glVertex2f(100 + 50 * cos(theta) / 2, 590 + 50 * sin(theta) / 2);
    }

    glEnd();
    glLineWidth(1.5);
    glColor3f(1, 1, 1);
    glBegin(GL_POLYGON);
    for (int i = 0; i < 360; i++)
    {
        theta = i * PI * i / 180;
        glVertex2f(130 + 50 * cos(theta) / 2, 580 + 50 * sin(theta) / 2);
    }
    glEnd();

    glLineWidth(1.5);
    glColor3f(1, 1, 1);
    glBegin(GL_POLYGON);
    for (int i = 0; i < 360; i++)
    {
        theta = i * PI * i / 180;
        glVertex2f(140 + 50 * cos(theta) / 2, 600 + 50 * sin(theta) / 2);
    }
    glEnd();

    glLineWidth(1.5);
    glColor3f(1, 1, 1);
    glBegin(GL_POLYGON);
    for (int i = 0; i < 360; i++)
    {
        theta = i * PI * i / 180;
        glVertex2f(170 + 50 * cos(theta) / 2, 590 + 50 * sin(theta) / 2);
    }

    glEnd();
    glFlush();
}

void backk(int i, int j)
{


    glColor3f(0, .5, 1);
    glBegin(GL_QUADS);
    glVertex2f(0.0 + i, 0.0 + j);
    glVertex2f(600.0 + i, 0.0 + j);
    glVertex2f(600.0 + i, -500 + j);
    glVertex2f(0.0 + i, -500 + j);
    glEnd();
    glFlush();


}
void ground(int i, int j)
{

    glBegin(GL_QUADS);
    glColor3f(0, 1.0, 0);
    glVertex2f(0.0 + i, 0.0 + j);
    glVertex2f(600.0 + i, 0.0 + j);
    glVertex2f(600.0 + i, -j);
    glVertex2f(0.0 + i, -j);
    glEnd();
}


void duck(int i, int j)
{
    int h;
    glColor3f(1.0, 1.0, 0.0);
    glBegin(GL_POLYGON);
    glVertex2f(45 + i, 45 + j);
    glVertex2f(70 + i, 20 + j);
    glVertex2f(95 + i, 20 + j);
    glVertex2f(120 + i, 45 + j);
    glVertex2f(95 + i, 70 + j);
    glVertex2f(70 + i, 70 + j);
    glVertex2f(95 + i, 95 + j);
    glVertex2f(82.5 + i, 107.5 + j);
    glVertex2f(32.5 + i, 57.5 + j);
    glEnd();
    glFlush();
    for (h = 0; h < 13; h += 4)
    {
        glBegin(GL_LINES);
        glColor3f(0.7, 0.4, 0);
        glVertex2f(57.5 + h + i, 52.5 + h + j);
        glVertex2f(100 + h + i, 30 + h + j);
        glEnd();
        glFlush();
    }
    glColor3f(0.0, 1.0, 0.0);
    glBegin(GL_POLYGON);
    glVertex2f(82.5 + i, 107.5 + j);
    glVertex2f(65 + i, 107.5 + j);
    glVertex2f(50 + i, 95 + j);
    glVertex2f(70 + i, 95 + j);
    glEnd();
    glFlush();
    glColor3f(0.0, 0.0, 0.0);
    glPointSize(5);
    glBegin(GL_POINTS);
    glVertex2f(76 + i, 101 + j);
    glEnd();
    glColor3f(0.0, 1.0, 0.0);
    glBegin(GL_LINE_LOOP);
    glVertex2f(72.5 + i, 107.5 + j);
    glVertex2f(67.5 + i, 112.5 + j);
    glVertex2f(72.5 + i, 110 + j);
    glVertex2f(77.5 + i, 112.5 + j);
    glEnd();
    glFlush();
}


void display(void)
{
    GLfloat aspect = GLfloat(a) / GLfloat(b);
    glOrtho(-2.5 * aspect, 2.5 * aspect, -2.5, 2.5, -10.0, 10.0);
    glClear(GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-2.5 * aspect, 2.5 * aspect, -2.5, 2.5, -10.0, 10.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    sun();

    glClear(GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, (GLdouble)a, 0.0, (GLdouble)b, 0, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    cloud1();
    ground(0, 650);
    backk(0, 650);
    duck(40, 375);
    duck(180, 375);
    duck(320, 375);
}

void myReshape(int w, int h)
{
}
void keys(unsigned char key, int x, int y)
{
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);

    glutInitWindowSize(a, b);
    glutCreateWindow("EGG GAME");
    myinit();
    glutInitWindowPosition(100, 100);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
    glutDisplayFunc(display);
    glutKeyboardFunc(keys);
    glutIdleFunc(display);
    glutReshapeFunc(myReshape);
    glutMainLoop();
}
#包括
#包括
#包括
#包括
#包括
#包括
#定义圆半径0.15f
#定义PI 3.14159265358979323846
int COGS_CATCH=0,missed_COGS=0,level_count=1,点数=0;
int egg_xc,egg_yc;
//鸡蛋的坐标
内篮x,篮y;
//关于篮子的坐标
int a=600,b=650;//显示屏幕的默认大小
int s=0;
//用于菜单选项
int=0;
整数速度_1=1,速度_2=1.5,速度_3=2,速度_4=2.5;
int w=48,h=48,t=10,e=9,g=12;
void myinit();
无效启动屏幕(int,int);
void cloud1();
空蛋();
空篮(int,int);
空鸭(int,int);
无效打印分数();
void egg_start();
无效颜色();
无效分数();
作废显示(作废);
空篮_集(int,int);
void myrestrape(int,int);
无效键(无符号字符、int、int);
无效菜单(int);
void myinit()
{
}
void sun()
{
GLfloat黑色[]={0.0,0.0,0.0,1.0};
GLfloat yellow[]={1.0,1.0,0.0,1.0};
GLfloat方向[]={1.0,1.0,1.0,0.0};
GLMATERIALV(GL_前部、GL_环境_和_漫反射,黄色);
GLMATERIALV(GL_前部,GL_镜面反射,黄色);
GLMATERALF(GL_正面,GL_反光度,30);
glLightfv(GL_LIGHT0,GL_环境光,黑色);
glLightfv(GL_LIGHT0,GL_漫反射,黄色);
glLightfv(GL_LIGHT0,GL_镜面反射,黄色);
glEnable(德国劳埃德大学照明);
glEnable(GL_LIGHT0);
glEnable(GLU深度试验);
glMatrixMode(GLU模型视图);
glPushMatrix();
glPushMatrix();
glTranslatef(0,0.0,0);
固体球(0.5,30,30);
glPopMatrix();
glPopMatrix();
glFlush();
glDisable(GLU照明);
glDisable(GL_LIGHT0);
glDisable(GLU深度测试);
}
void cloud1()
{
浮动θ;
GLfloat角;
glLineWidth(1.5);
gl3f(1,1,1);
glBegin(GL_多边形);
对于(int i=0;i<360;i++)
{
θ=i*PI*i/180;
glVertex2f(100+50*cos(θ)/2590+50*sin(θ)/2);
}
格伦德();
glLineWidth(1.5);
gl3f(1,1,1);
glBegin(GL_多边形);
对于(int i=0;i<360;i++)
{
θ=i*PI*i/180;
glVertex2f(130+50*cos(θ)/2580+50*sin(θ)/2);
}
格伦德();
glLineWidth(1.5);
gl3f(1,1,1);
glBegin(GL_多边形);
对于(int i=0;i<360;i++)
{
θ=i*PI*i/180;
glVertex2f(140+50*cos(θ)/2600+50*sin(θ)/2);
}
格伦德();
glLineWidth(1.5);
gl3f(1,1,1);
glBegin(GL_多边形);
对于(int i=0;i<360;i++)
{
θ=i*PI*i/180;
glVertex2f(170+50*cos(θ)/2590+50*sin(θ)/2);
}
格伦德();
glFlush();
}
无效回退(int i,int j)
{
gl3f(0,5,1);
glBegin(GL_QUADS);
glVertex2f(0.0+i,0.0+j);
glVertex2f(600.0+i,0.0+j);
glVertex2f(600.0+i,-500+j);
glVertex2f(0.0+i,-500+j);
格伦德();
glFlush();
}
无效地面(内部i、内部j)
{
glBegin(GL_QUADS);
GL3F(0,1.0,0);
glVertex2f(0.0+i,0.0+j);
glVertex2f(600.0+i,0.0+j);
glVertex2f(600.0+i,-j);
glVertex2f(0.0+i,-j);
格伦德();
}
空鸭(inti,intj)
{
int-h;
GL3F(1.0,1.0,0.0);
glBegin(GL_多边形);
glVertex2f(45+i,45+j);
glVertex2f(70+i,20+j);
glVertex2f(95+i,20+j);
glVertex2f(120+i,45+j);
glVertex2f(95+i,70+j);
glVertex2f(70+i,70+j);
glVertex2f(95+i,95+j);
glVertex2f(82.5+i,107.5+j);
glVertex2f(32.5+i,57.5+j);
格伦德();
glFlush();
对于(h=0;h<13;h+=4)
{
glBegin(GL_行);
GL3F(0.7,0.4,0);
glVertex2f(57.5+h+i,52.5+h+j);
glVertex2f(100+h+i,30+h+j);
格伦德();
glFlush();
}
GL3F(0.0,1.0,0.0);
glBegin(GL_多边形);
glVertex2f(82.5+i,107.5+j);
glVertex2f(65+i,107.5+j);
glVertex2f(50+i,95+j);
glVertex2f(70+i,95+j);
格伦德();
glFlush();
GL3F(0.0,0.0,0.0);
gl点大小(5);
glBegin(总分);
glVertex2f(76+i,101+j);
格伦德();
GL3F(0.0,1.0,0.0);
glBegin(GL_线_环);
glVertex2f(72.5+i,107.5+j);
glVertex2f(67.5+i,112.5+j);
glVertex2f(72.5+i,110+j);
glVertex2f(77.5+i,112.5+j);
格伦德();
glFlush();
}
作废显示(作废)
{
GLfloat方面=GLfloat(a)/GLfloat(b);
格洛托(-2.5*纵横比,2.5*纵横比,-2.5,2.5,-10.0,10.0);
glClear(GLU深度缓冲位);
glMatrixMode(GL_投影);
glLoadIdentity();
格洛托(-2.5*纵横比,2.5*纵横比,-2.5,2.5,-10.0,10.0);
glMatrixMode(GLU模型视图);
glLoadIdentity();
sun();
glClear(GLU深度缓冲位);
glMatrixMode(GL_投影);
glLoadIdentity();
glOrtho(0,(GLdouble)a,0.0,(GLdouble)b,0,1);
glMatrixMode(GLU模型视图);
glLoadIdentity();
cloud1();
地面(0650);
贝克(0650);
鸭(40375);
鸭(180375);
鸭(320375);
}
void myrestrape(int w,int h)
{
}
无效键(无符号字符键,整数x,整数y)
{
}
int main(int argc,字符**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_单个| GLUT_RGB | GLUT_深度);
glutInitWindowSize(a,b);
游戏窗口(“蛋游戏”);
myinit();
位置(100100);
谷胱甘肽功能表(GLUT_右键);
glutDisplayFunc(显示器);
键盘功能(按键);
glutIdleFunc(显示);
GLUTREFORMEFUNC(MyREFORMATE);
glutMainLoop();
}

不使用GLUT,但这是我第一眼看到的

  • glClear(GL\u深度\u缓冲\u位
    
    void display(void)
      {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      drawBox(); // here yoru rendering stuff instead
      glFlush(); // I usually add this before swapping buffers
      glutSwapBuffers();
      }