在同一窗口中绘制3d和2d无法正常工作openGl



我正试图在同一个窗口中绘制一些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();
}

没有使用GLUT,但这是我第一眼看到的。

  1. glClear(GL_DEPTH_BUFFER_BIT);

    为什么不把颜色也弄清楚?尝试glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  2. glutSwapBuffers();

    如果在显示功能结束时没有此调用,则渲染不会在需要时更新。

  3. 交换缓冲区前无glFlush/glFinish

    把它们放在最后是安全的。我看到你在一些渲染调用中得到了它们,但不是全部。

  4. int操作数用于glVertex2f

    既然所有坐标都是整数,为什么不使用glVertex2i呢。int to float转换减慢了速度。。。

看看我刚刚发现的一些简单的GLUT例子:

  • cube.c

在那里显示如下:

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

因此,相应地更改您的代码。。。

最重要的是,我可能会尝试使用混合云。。。

最新更新