lunes, 18 de febrero de 2013

Circulo que rebota con glut C++



Círculo que rebota.

El siguiente código muestra un círculo rebotando en la pantalla, haciendo uso de la librería GLUT.





#include <gl/glut.h> 
#include <Math.h>    
#define PI 3.14159265f    

char title[] = "Pelota que Rebota 2D"; 
int windowWidth  = 640;    
int windowHeight = 480;    
int windowPosX   = 50;     
int windowPosY   = 50;     
  
GLfloat ballRadius = 0.2f;  
GLfloat xPos = 0.0f;       
GLfloat yPos = 0.0f;
GLfloat xPosMax, xPosMin, yPosMax, yPosMin;
GLdouble xLeft, xRight, yBottom, yTop;    
GLfloat xSpeed = 0.004f;
GLfloat ySpeed = -.007f;
int refreshMillis = 5;    
     

void initGL() {
   glClearColor(0.0, 0.0, 0.0, 1.0);
}
  
void display() {

   glClear(GL_COLOR_BUFFER_BIT);   
  
   glLoadIdentity();               
   glTranslatef(xPos, yPos, 0.0f); 
  
   glBegin(GL_TRIANGLE_FAN);
      glColor3f(1.0, 0.0, 0.0); 
      glVertex3f(0.0f, 0.0f,0.0);      
      int numSegments = 100;
      GLfloat angle;
      for (int i = 0; i <= numSegments; i++) {
         angle = i * 2.0f * PI / numSegments; 
         glVertex3f(cos(angle) * ballRadius, sin(angle) * ballRadius,1.0);
      }
   glEnd();
   glutSwapBuffers();  
   xPos += xSpeed;
   yPos += ySpeed;
  
   if (xPos > xPosMax) {
      xPos = xPosMax;
      xSpeed = -xSpeed;
   } else if (xPos < xPosMin) {
      xPos = xPosMin;
      xSpeed = -xSpeed;
   }
   if (yPos > yPosMax) {
      yPos = yPosMax;
      ySpeed = -ySpeed;
   } else if (yPos < yPosMin) {
      yPos = yPosMin;
      ySpeed = -ySpeed;
   }

} 
void reshape(GLsizei weight, GLsizei height) 
{
   if (height == 0) height = 1;              
   GLfloat aspect = (GLfloat)weight / height;  
  
   glViewport(0, 0, weight, height);
     
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();           
   if (weight <= height) {
      xLeft   = -1.0;
      xRight  = 1.0;
      yBottom = -1.0 / aspect;
      yTop    = 1.0 / aspect;
   } else {
      xLeft   = -1.0 * aspect;
      xRight  = 1.0 * aspect;
      yBottom = -1.0;
      yTop    = 1.0;
   }
   gluOrtho2D(xLeft, xRight, yBottom, yTop);
   xPosMin = xLeft + ballRadius;
   yPosMax = yTop - ballRadius;   
   xPosMax = xRight - ballRadius;
   yPosMin = yBottom + ballRadius;
  
  
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();          
}
void Timer(int value) 
{
   glutPostRedisplay();   
   glutTimerFunc(refreshMillis, Timer, 0);
}

int main(int argc, char** argv) 
{
   glutInit(&argc, argv);           
   glutInitDisplayMode(GLUT_DOUBLE);
   glutInitWindowSize(windowWidth, windowHeight); 
   glutInitWindowPosition(windowPosX, windowPosY);
   glutCreateWindow(title);     
   glutDisplayFunc(display);    
   glutReshapeFunc(reshape);    
   glutTimerFunc(0, Timer, 0);  
   initGL();                    
   glutMainLoop();              
   return 0;
} 

No hay comentarios:

Publicar un comentario