我为在屏幕周围的弹跳球编写了此代码,因为事实证明,它不起作用。
我正在使用OpenGL和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();
最佳答案
好吧,事实证明我仍然是菜鸟,我没有以正确的方式调试此代码,幸好我能解决@andreas和@genpfault。感谢大家。解决方案是在文件末尾没有使用glEnd()
...是的,这是一个偷偷摸摸的错误。
关于c++ - 如何停止闪烁?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48839179/