Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Cdigo uno:

#include <windows.h>

#include <stdlib.h>

#include<conio.h>

#include <GL/glut.h>

#include <math.h>

#define GL_PI 3.1416f

// declaracion de variables

// GLfloat ...;

// theta[] me indica los ngulos iniciales en los 3 s

static GLfloat theta[] = {0.0,0.0,0.0};

// eje es el ngulo a rotar

static GLint eje = 2;

static GLfloat ang=0.0f, radio, x, y;

int refreshMillis = 40;

void sol(void){

GLfloat ang, radio = 0.4f, x, y;

glColor3f(1.0,1.0,0.0);

glBegin(GL_POLYGON);

for (ang = 0.0f; ang < 2 * GL_PI; ang += 2*GL_PI/100){

x = radio * sin(ang);

y = radio * cos(ang);

glVertex2f(x,y);

glEnd();

void luna(void){
GLfloat ang, radio = 0.15f, x, y;

glColor3f(1.0,1.0,1.0);

glBegin(GL_POLYGON);

for (ang = 0.0f; ang < 2 * GL_PI; ang += 2*GL_PI/100){

x = radio * sin(ang);

y = radio * cos(ang);

glVertex2f(x,y);

glEnd();

void elipse(GLfloat a, GLfloat b){

GLfloat ang, radio, x, y;

glColor3f(1.0,0.0,0.0);

glBegin(GL_LINE_LOOP);

for (ang = 0.0f; ang < 2 * GL_PI; ang += 2*GL_PI/100){

radio=1/(sqrt(((pow(cos(ang),2))/(pow(a,2)))+((pow(sin(ang),2))/(pow(b,2)))))
;

x = radio * sin(ang);

y = radio * cos(ang);

glVertex2f(x,y);

glEnd();

void giroLuna(){

GLfloat ang, radio, x, y;


glColor3f(1.0,0.0,0.0);

glBegin(GL_LINE_LOOP);

for (ang = 0.0f; ang < 2 * GL_PI; ang += 2*GL_PI/100){

radio=(1/(sqrt(((pow(cos(ang),2))/(pow(0.6,2)))+((pow(sin(ang),2))/(pow(1.2,2
))))))+5.0;

x = radio * sin(ang);

y = radio * cos(ang);

glTranslated(x,y,0.0);

luna();

glFlush();

// intercambiamos los buffers, el que se muestra y el que esta oculto

glutSwapBuffers();

void giroLuna2(){

radio=1/(sqrt(((pow(cos(ang),2))/(pow(0.6,2)))+((pow(sin(ang),2))/(pow(1.2,2)
))));

x = radio * sin(ang);

y = radio * cos(ang);

glTranslated(x,y,0.0);

luna();

ang += 2*GL_PI/100;

if(ang >= 2 * GL_PI){

ang=0.0f;

}
// dibujamos nuestra escena

void display(void){

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

// composicion de rotaciones

glRotatef(theta[0],1.0,0.0,0.0);

glRotatef(theta[1],0.0,1.0,0.0);

glRotatef(theta[2],0.0,0.0,1.0);

elipse(0.6,1.2);

sol();

giroLuna2();

glFlush();

// intercambiamos los buffers, el que se muestra y el que esta oculto

glutSwapBuffers();

// esta funcin controla el angulo de rotacin segn el eje de giro

void girar_objeto_geometrico (){

theta[eje] += 0.05;

if(theta[eje]>360){

theta[eje] -= 360;

display();

// control de ventana (recuerde el volumen de visualizacin)

// modifique dicho volumen segn su conveniencia

void myReshape(int w, int h){


glViewport(0,0,w,h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

if(w <=h){

glOrtho(-2.0,2.0,-
2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w, -10.0, 10.0);

else{

glOrtho(-2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h, -
2.0,2.0,-10.0,10.0);

glMatrixMode(GL_MODELVIEW);

void Timer(int value) {

glutPostRedisplay();

glutTimerFunc(refreshMillis, Timer, 0);

void teclado(unsigned char tecla,int x,int y){

switch(tecla){

case 'a' : eje = 0; break;

case 's' : eje = 1; break;

case 'd' : eje = 2; break;

case 'f' : exit(0) ; break;

int main(int argc, char **argv)

{
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glutInitWindowSize(1000,650);

glutCreateWindow("mi objeto bajo rotaciones");

glutReshapeFunc(myReshape);

// invocamos a display() para dibujar nuestra escena

glutDisplayFunc(display);

// esta funcion llama a girar_objeto_geomtrico() mientras no haya evento

//alguno ocasionado por el usuario

//glutIdleFunc(giroLuna2);

glutTimerFunc(0, Timer, 0);

//glutKeyboardFunc(teclado);

/*glutMouseFunc(mouse);*/

glEnable(GL_DEPTH_TEST);

glutMainLoop();

return 0;

Cdigo 2:

#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; //Posicion inicial en X

GLfloat yPos = 0.0f; //Posicion inicial en Y

GLfloat xPosMax, xPosMin, yPosMax, yPosMin;

GLdouble xLeft, xRight, yBottom, yTop;

GLfloat xSpeed = 0.002f; // velocidad a la que se movera en las X

GLfloat ySpeed = -.007f; // velocidad a la que se movera en las Y

int refreshMillis = 5;

void initGL() {

// color de fondo

glClearColor(0.0, 0.0, 0.1f, 1.0);

void display() {

glClear(GL_COLOR_BUFFER_BIT);

glLoadIdentity();

glTranslatef(xPos, yPos, 0.0f);

glBegin(GL_TRIANGLE_FAN);

glColor3f(1.0f, 0.3f, 0.6f);

glVertex2f(0.0f, 0.0f);

int numSegments = 100; //redondeo de la figura

GLfloat angle;

for (int i = 0; i <= numSegments; i++) {


angle = i * 2.0f * PI / numSegments;

glVertex2f(cos(angle) * ballRadius, sin(angle) * ballRadius);

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 Transicion(){

display();

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(1, 1, 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);

void raton(int boton, int estado, int x, int y) {

switch (boton){

case GLUT_LEFT_BUTTON:

if (estado = GLUT_DOWN){
xPos += xSpeed;

yPos += ySpeed;

if(x == xPos){

x= xPosMin;

xSpeed = -xSpeed;

xPosMin = xLeft + ballRadius;

else if (y== yPos){

y =yPosMin;

ySpeed= -ySpeed;

else{

glutPostRedisplay();

default:

break;

void teclado(unsigned char tecla, int x, int y){

switch (tecla){

case 27 : exit(0);

break;

default:

break;

}
}

int main(int argc, CHAR* argv[]) {

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_DOUBLE);

glutInitWindowSize(windowWidth, windowHeight);

glutInitWindowPosition(windowPosX, windowPosY);

glutCreateWindow(title);

//glutDisplayFunc(display);

glutDisplayFunc(Transicion);

glutReshapeFunc(reshape);

glutTimerFunc(0, Timer, 0);

glutKeyboardFunc(teclado);

glutMouseFunc(raton);

initGL();

glutMainLoop();

return 0;

You might also like