Reputation: 49
I made this code for a bounce ball around the screen, as it turns out it doesn't work as I spected.
I am working in OpenGL with glut and c++.
My question is: How can I stop the flickering on the run?
#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;
}
As you can see, I tried the doubleBuffer stuff, didn't work. Maybe I did it wrong
glutSwapBuffers();
Upvotes: 0
Views: 135
Reputation: 49
Well it turns out as I'm still a noob here i did not debugged this code the right way, thankfully to @andreas and @genpfault I was able to solve it. Thanks to all of you.
The solution was not having used glEnd()
at the end of my file... so yeah kind of a sneaky error.
Upvotes: 1