我怎么能停止闪烁?

时间:2018-02-17 07:53:03

标签: c++ opengl glut

我为屏幕周围的弹跳球制作了这个代码,因为事实证明它并没有像我说的那样工作。

我在使用过剩和c ++的OpenGL工作。

我的问题是:如何在运行中停止闪烁?

#include "stdafx.h"
#include <Windows.h>
#include <GL\glut.h>
#include <math.h>
#define PI 3.14159265f

GLfloat radius_Bolita= 1.0f;
GLfloat posX_Bolita = 0.0f;
GLfloat posY_Bolita = 0.0f;
GLfloat xSpeed_Bolita = 0.02f;
GLfloat ySpeed_Bolita = 0.007f;
GLfloat xmaxBound_Bolita, xminBound_Bolita, ymaxBound_Bolita, yminBound_Bolita;
int updateMS = 30;

GLdouble areaClip_Xleft, areaClip_Xright, areaClip_Ytop, areaClip_Ybottom;
void initOpenGL(){
glClearColor(1.0, 1.0, 0.0, 1.0); //color de fondo
}

void pantalla() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(posX_Bolita, posY_Bolita, 0.0f);

glBegin(GL_TRIANGLE_FAN);
glColor3f(1.0, 1.0, 1.0); //color de bolita
glVertex2d(0.0f, 0.0f);
int partes=100;
GLfloat angulo;
for (int i = 0; i < partes; i++) {
    angulo = i * 2.0f * PI / partes;
    glVertex2d(cos(angulo)*radius_Bolita,sin(angulo)*radius_Bolita);
}
glEnd;
glutSwapBuffers();

posX_Bolita += xSpeed_Bolita;
posY_Bolita += ySpeed_Bolita;

if (posX_Bolita > xmaxBound_Bolita) {
        posX_Bolita = xmaxBound_Bolita;
        xSpeed_Bolita = -xSpeed_Bolita;
}
else if(posX_Bolita < xminBound_Bolita){
            posX_Bolita = xminBound_Bolita;
            xSpeed_Bolita = -xSpeed_Bolita;
}
if (posY_Bolita > ymaxBound_Bolita) {
    posY_Bolita = ymaxBound_Bolita;
    ySpeed_Bolita = -ySpeed_Bolita;
}
else if (posY_Bolita < yminBound_Bolita) {
    posY_Bolita = yminBound_Bolita;
    ySpeed_Bolita = -ySpeed_Bolita;
}
}
void ajustar(GLsizei width, GLsizei height) {
if (height=0) {
    height = 1;
}
GLfloat look = (GLfloat)width / (GLfloat)height;

glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (width >= height) {
    areaClip_Xleft= -1.0 * look;
    areaClip_Xright= 1.0 * look;
    areaClip_Ybottom= -1.0;
    areaClip_Ytop= 1.0;
}
else {
    areaClip_Xleft = -1.0;
    areaClip_Xright = 1.0;
    areaClip_Ybottom = -1.0/look;
    areaClip_Ytop = 1.0/look;
}
gluOrtho2D(areaClip_Xleft, areaClip_Xright, areaClip_Ybottom, areaClip_Ytop);
xminBound_Bolita = areaClip_Xleft + radius_Bolita;
xmaxBound_Bolita= areaClip_Xright - radius_Bolita;
yminBound_Bolita= areaClip_Ybottom + radius_Bolita;
ymaxBound_Bolita= areaClip_Ytop - radius_Bolita;
}

void tempo(int value) {
glutPostRedisplay();
glutTimerFunc(updateMS, tempo, 0);
}

int window_Width=640;
int window_Height=480;
int window_Posx=50;
int window_Posy=50;

int main(int argc,char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(window_Width,window_Height);
glutInitWindowPosition(window_Posx, window_Posy);
glutCreateWindow("BouncyBall");
glutDisplayFunc(pantalla);
glutReshapeFunc(ajustar);
glutTimerFunc(0,tempo,0);
initOpenGL();
glutMainLoop();
return 0;

}

正如你所看到的,我尝试了doubleBuffer的东西,没有用。也许我做错了

glutSwapBuffers();

1 个答案:

答案 0 :(得分:1)

结果证明,由于我仍然是菜鸟,在这里我没有以正确的方式调试此代码,感谢@andreas和@genpfault我能够解决它。谢谢大家。
解决方案不是在文件末尾使用glEnd() ...是的,这是一个偷偷摸摸的错误。