martes, 10 de abril de 2007

Previo Práctica 6 Laboratorio parte 2

Códigos para los programas de cronómetro, despliegue de nombre con stroke y con bitmap.

//Antonio Dávalos de los Ríos
//Este programa despliega mi nombre utilizando bitmap

#include
#include

//Aqui se define la fuente a utilizar, en un arreglo se definen las otras fuentes
void *font = GLUT_BITMAP_HELVETICA_18;
void *fonts[] =
{
GLUT_BITMAP_9_BY_15,
GLUT_BITMAP_TIMES_ROMAN_10,
GLUT_BITMAP_TIMES_ROMAN_24
};
//Este es el mensaje por default, es en caso de mandar sólo a desplegar el contenido del apuntador
char defaultMessage[] = "Dr. Abe";
char *message = defaultMessage;


//Funcion que despliega cada caracter
void
output(int x, int y, char *string)
{
int len, i;

glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len; i++) {
glutBitmapCharacter(font, string[i]);
}
}

void
display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
//Aqui cambio el color de la fuente
output(250, 270, "Antonio Davalos de los Rios");
glutSwapBuffers();
}

//Funcion que realiza los cambios con el mouse
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON)
{
font = GLUT_BITMAP_TIMES_ROMAN_10;
glutPostRedisplay();
}
else if (button == GLUT_RIGHT_BUTTON)
{
font = GLUT_BITMAP_TIMES_ROMAN_24;
glutPostRedisplay();
}
}

void
reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, h, 0);
glMatrixMode(GL_MODELVIEW);
}

int
main(int argc, char **argv)
{
int i, msg_submenu, color_submenu;

glutInit(&argc, argv);
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-mono")) {
font = GLUT_BITMAP_9_BY_15;
}
}
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition (50, 100);
glutCreateWindow("Despliega mi nombre con BitMap");
glClearColor(0.0, 0.0, 0.0, 1.0);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}

//Antonio Dávalos de los Ríos
//Programa que despliega un cronómetro y puede controlarse con clicks del mouse
#include
#include
#include
#include

//Variables globales para el programa


static int inicio_cuenta=1; n;
static char label[8];
static float inc=0.2;
//Estas variables son las que contienen las horas minutos y segundos
static int hor_cd=0;
static int hor_ci=0;
static int min_cd=0;
static int min_ci=0;
static int seg_cd=0;
static int seg_ci=0;

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}

//Funcion para el despliegue de texto utilizando stroke
void strokeText(char *s)
{
unsigned int i;
for (i=0; iglutStrokeCharacter(GLUT_STROKE_ROMAN,s[i]);
}
//Funcion que calcula el tiempo para desplegar los caracteres, cuenta con una serie de if anidados
void timer(int id)
{
if (inicio_cuenta==1)
{
if(seg_cd<=n)
{//Despliegue para los segundos y su cambio al llegar a 9
n=9;
seg_cd++;

if(seg_cd>n)
{//Despliegue de los dígitos de decenas en los segundos
seg_cd=0;
seg_ci++;

if(seg_ci>5)
{//Despliegue para los minutos al llegar a 59 segundos
seg_ci=0;
min_cd++;


}
}
}
}
}//Fin de las funciones if anidadas
glutTimerFunc(1000,timer,0);
glutPostRedisplay();
}
void entradaMouse(int boton, int situacion) {
if(situacion == GLUT_DOWN)
{
if(boton == GLUT_LEFT_BUTTON)
{
inicio_cuenta=1;
}
else{
if(boton == GLUT_RIGHT_BUTTON)
{
inicio_cuenta=0;
}
else
{
inicio_cuenta=1;
hor_cd=0;
hor_ci=0;
min_cd=0;
min_ci=0;
seg_cd=-1;//Se hace -1 para q se despliegue cero
seg_ci=0;
}
}
}
}
void display(void)
{
//COnversión de los caracteres para el correcto despliegue en la ventana
char sd=(char)(seg_cd+48);
char si=(char)(seg_ci+48);
char md=(char)(min_cd+48);
char mi=(char)(min_ci+48);
char hd=(char)(hor_cd+48);
char hi=(char)(hor_ci+48);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_MODELVIEW_MATRIX );
glLoadIdentity();
glColor3f(0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(200,200,0);
glScalef(inc,inc,inc);
//Diseño de los caracteres para el despliegue
sprintf(label,"%c%c:%c%c:%c%c",hi,hd,mi,md,si,sd);
strokeText(label);
glPopMatrix();
glFlush();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D(0, w, 0, h);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (600, 400);
glutInitWindowPosition (50, 50);
glutCreateWindow ("Click Izquierdo: Inicia - Click Derecho: Detiene");
init();
glutMouseFunc(entradaMouse);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(1000,timer,1);
glutMainLoop();
return 0;


//Antonio Dávalos de los Ríos
//Programa que despliega mi nom_stroke utilizando comando stroke
//Utiliza al ratón para aumentar y disminuir el tamaño

#include
#include
#include
#include
static float escala_stroke = 0.0015;

//Funcion que sirve para desplegar el texto

void texto_stroke(GLfloat x, GLfloat y, char *nom_stroke)
{
//Funciones para el manejo de stroke
va_list args;
char nom_strokebuff[200], *apuntanom_stroke;
va_start(args, nom_stroke);
vsprintf(nom_strokebuff, nom_stroke, args);
va_end(args);
glPushMatrix();
//Los valores de la escala son para poder modificar cuando se da click derecho o izquierdo
glTranslatef(x, y, 0);
glScalef((GLfloat)escala_stroke, (GLfloat)(escala_stroke*3), (GLfloat)escala_stroke);
//Impresión de los caracteres del nombre
for (apuntanom_stroke = nom_strokebuff; *apuntanom_stroke; apuntanom_stroke++)
{
glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN , *apuntanom_stroke);
}
glPopMatrix();
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(40.0, 1.0, 0.1, 20.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
gluLookAt(0.0, 0.0, 9.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.);
glPushMatrix();
texto_stroke(-3.0, 0, "Antonio Davalos de los Rios");
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glFlush();
}
//Funcion para la utilizacion del mouse
void mouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN)
{
if (button == GLUT_LEFT_BUTTON)
{
escala_stroke = escala_stroke - 0.00005;
glutPostRedisplay();
}
else if (button == GLUT_RIGHT_BUTTON)
{
escala_stroke = escala_stroke + 0.00005;
glutPostRedisplay();
}
}
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, 0, h);
glScalef(1, -1, 1);
glTranslatef(0, -h, 0);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition (50, 50);
glutCreateWindow("Click Derecho: Disminuye - Click Izquierdo: Aumenta");
glClearColor(0.0, 0.0, 0.0, 1.0);
//Defino el color azul para desplegar
glColor3f(0, 0, 1);
//Defino el ancho de la linea del texto
glLineWidth(3.0);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

No hay comentarios.: