lunes, 9 de abril de 2007

PREVIO PRACTICA 5 RELOJ USO DE FUNCION TIMER

#include GL/glut.h
#include stdlib.h
#include stdio.h
#include windows.h
#include math.h

void text(GLuint x, GLuint y, GLfloat scale, char* format);
void onMouse(int button, int state, int x, int y);
void onMotion(int x, int y);
void auxiliar(void);
void TimerFunction(int value);

int h1,h2,m1,m2,s1,s2;
GLint incrementa = 20;
float alpha, beta;
int xo, yo,sigue;
s1=0;
m1=0;
h1=0;
s2=0;
m2=0;
h2=0;
sigue=0;
void init(void)
{
glClearColor (0.0, 0.0, 1.0, 0.0);
glShadeModel (GL_FLAT);
}

void text(GLuint x, GLuint y, GLfloat scale, char *format) {
va_list args;
char buffer[255], *p;
GLfloat font_scale = 119.05f + 33.33f;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, glutGet(GLUT_WINDOW_WIDTH), 0,
glutGet(GLUT_WINDOW_HEIGHT));
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glTranslatef(x, y, 0.0);
glScalef(scale/font_scale, scale/font_scale, scale/font_scale);
for(p = buffer; *p; p++)
glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
glutStrokeCharacter(GLUT_STROKE_ROMAN, h2+48);
glutStrokeCharacter(GLUT_STROKE_ROMAN, h1+48);
glutStrokeCharacter(GLUT_STROKE_ROMAN, 58);
glutStrokeCharacter(GLUT_STROKE_ROMAN, m2+48);
glutStrokeCharacter(GLUT_STROKE_ROMAN, m1+48);
glutStrokeCharacter(GLUT_STROKE_ROMAN, 58);
glutStrokeCharacter(GLUT_STROKE_ROMAN, s2+48);
glutStrokeCharacter(GLUT_STROKE_ROMAN, s1+48);
glPopAttrib();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
void TimerFunction(int value)
{
s1++;
if(s1==10){
s2++;
s1=0;
if(s2==6){
s2=0;
m1++;
if(m1==10){
m1=0;
m2++;
if(m2==6){
m2=0;
h1++;
if(h1==10){
h1=0;
if(h2==2){
m1=m2=s1=s2=h1=h2=0;
}
}
}
}
}
}
glutPostRedisplay();
if(sigue==1)
glutTimerFunc(1000,TimerFunction,1);
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
}

/* ARGSUSED1 */
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}
void onMouse(int button, int state, int x, int y)
{
if ( (button == GLUT_LEFT_BUTTON) & (state == GLUT_DOWN)) {
sigue=1;
auxiliar();
xo=x;
yo=y;
}

if ( (button == GLUT_RIGHT_BUTTON) & (state == GLUT_DOWN) ) {
sigue=0;

xo=x;
yo=y;
}

}

void onMotion(int x, int y) {
alpha = (alpha + (y - yo));
beta = (beta + (x - xo));
xo = x; yo = y;
glutPostRedisplay();
}

void display(void)
{
GLint command = 1;
glClear (GL_COLOR_BUFFER_BITGL_DEPTH_BUFFER_BIT);

glLoadIdentity (); /* clear the matrix */
/* viewing transformation */
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

if (command) {
glColor3f(1.0,1.,0.0) ;
text(250,200, incrementa, "%s","Reloj ");
}

glutSwapBuffers();
}


int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (0,0);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutMouseFunc(onMouse);
glutMotionFunc(onMotion);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

void auxiliar(void){
if(sigue==1)
glutTimerFunc(1000,TimerFunction,1);

}

No hay comentarios: