#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
#include <unistd.h>
//Funciones de color extraidas de:
//https://stackoverflow.com/questions/46928277/trying-to-convert-integer-rangeto-rgb-color
//Fast inverse square root:
//https://en.wikipedia.org/wiki/Fast_inverse_square_root
//**Resulta que ya no es mas rapido debido a los avances en hardware :(
float zoom=-34;
GLfloat girax = 30, giray = -30;
int gx, gy;
GLboolean clic=GL_FALSE;
void ajusta(int ancho, int alto){
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(-11,11,-12,12,11,-11);
gluPerspective(45,1,10,100);
//glFrustum(-11,11,-12,12,11,-11);
//glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
glEnable(GL_DEPTH_TEST);
}
void dibuja(void){
float x,y,z,k;
//y = 12*( (sin(sqrt(x*x + z*z)))/sqrt(x*x + z*z) );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0,0,zoom);
glRotatef(girax,1,0,0);
glRotatef(giray,0,1,0);
for(x=-8;x<=8;x+=0.2){
glBegin(GL_LINE_STRIP);
for(z=-8;z<=8;z+=0.2){
if((x*x + z*z)==0)continue;
y = 12*( (sin(sqrt(x*x + z*z)))/sqrt(x*x + z*z) );
//printf("y: %.2f, x: %.2f, z: %.2f\n",y,x,z);
glColor3ub(255,0,255);
glVertex3f(x,y,z);
}
glEnd();
}
for(z=-8;z<=8;z+=0.2){
glBegin(GL_LINE_STRIP);
for(x=-8;x<=8;x+=0.2){
if((x*x + z*z)==0)continue;
y = 12*( (sin(sqrt(x*x + z*z)))/sqrt(x*x + z*z) );
//printf("y: %.2f, x: %.2f, z: %.2f\n",y,x,z);
glColor3ub(255,0,255);
glVertex3f(x,y,z);
}
glEnd();
}
glPopMatrix();
//glFlush();
glutSwapBuffers();
}
void teclado(unsigned char key, int x, int y) {
switch(key) {
case 27: exit (0);
case '+': zoom++; break;
case '-': zoom--; break;
default: break;
}
glutPostRedisplay();
}
void raton(int boton, int estado, int xv, int yv) {
clic= boton==GLUT_LEFT_BUTTON && estado==GLUT_DOWN;
if(clic){
gx = xv;
gy = yv;
}
if (boton==3 || boton==4) // scroll up => 3 scroll down=>4
zoom+=(boton==3)?0.1:-0.1;
glutPostRedisplay();
}
void girar(int xv, int yv){
if (clic){ // inicia circulos con centro
girax -= (gy - yv);
giray -= (gx - xv);
gx = xv;
gy = yv;
}
glutPostRedisplay();
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(640,480);
glutInitWindowPosition(100, 150);
glutCreateWindow("Ecuacion");
glutReshapeFunc(ajusta);
glutDisplayFunc(dibuja);
glutKeyboardFunc(teclado);
glutMouseFunc(raton);
glutMotionFunc(girar);
glutMainLoop();
return 0;
}