#include <stdlib.h>
#include "glut.h"
#include <iostream>
using namespace std;
double n = 0;
double alpha = 0.0;
double betta = 0.0;
bool fl_alpha = false;
bool fl_betta = false;
#define PROJ_SIZE 8 // горизонтальный размер проекции
#define SPHERE_FACES 64
#define TIME_JITTER_STEPS 10 // кол-во шагов сдвига/поворота
#define FULL_ROT 0 // угол, на который за время сдвигов должна повернуться модель
#define SCALE_FACTOR 1.0 // фактор сжатия модели
void init()
{
GLfloat mat_ambient[] = { 0.8, 0.8, 0.8, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 0.0, 0.0, 10.0, 1.0 };
GLfloat lm_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, 50.0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lm_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// glEnable(GL_RESCALE_NORMAL);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearAccum(0.0, 0.0, 0.0, 0.0);
}
void displayObjects(GLfloat xoffset, GLfloat yrot)
{
GLfloat torus1_diffuse[] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat torus2_diffuse[] = { 1.0, 0.7, 0.7, 1.0 };
GLfloat sphere_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
glLoadIdentity();
if (fl_alpha == true) alpha = xoffset;
else alpha = 0.0;
if (fl_betta == true) betta = xoffset;
else betta = 0.0;
glTranslatef(alpha, betta, 0.0);
glRotatef(yrot, 0.0, 1.0, 0.0);
glScalef(SCALE_FACTOR, SCALE_FACTOR, SCALE_FACTOR);
glMaterialfv(GL_FRONT, GL_DIFFUSE, torus1_diffuse);
glutSolidTorus(0.3, 0.55, SPHERE_FACES, SPHERE_FACES);
glMaterialfv(GL_FRONT, GL_DIFFUSE, torus2_diffuse);
glutSolidTorus(0.35, 0.45, SPHERE_FACES, SPHERE_FACES);
glMaterialfv(GL_FRONT, GL_DIFFUSE, sphere_diffuse);
glutSolidSphere(0.4, SPHERE_FACES, SPHERE_FACES);
}
void display()
{
GLfloat correctedProSize = PROJ_SIZE - 3;
int jitter;
GLfloat oneStepTrans = correctedProSize / TIME_JITTER_STEPS;
GLfloat oneStepRot = FULL_ROT / TIME_JITTER_STEPS;
glClear(GL_COLOR_BUFFER_BIT | GL_ACCUM_BUFFER_BIT);
for (jitter = 0; jitter < TIME_JITTER_STEPS; jitter++)
{
glClear(GL_DEPTH_BUFFER_BIT);
displayObjects(-correctedProSize / 2 + oneStepTrans * jitter,
oneStepRot * jitter);
if (jitter != TIME_JITTER_STEPS)
glAccum(GL_LOAD, n);
else
glAccum(GL_LOAD, 1.0);
glAccum(GL_RETURN, 1.0);
}
glAccum(GL_RETURN, 1.0);
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
if (w <= h)
glOrtho(-PROJ_SIZE / 2, PROJ_SIZE / 2, -(PROJ_SIZE / 2)*h / w, PROJ_SIZE / 2 * h / w, -10.0, 10.0);
else
glOrtho(-PROJ_SIZE / 2 * w / h, PROJ_SIZE / 2 * w / h, -PROJ_SIZE / 2, PROJ_SIZE / 2, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case '1':
n = 0.1;
glutDisplayFunc(display);
glutPostRedisplay();
break;
case '2':
n = 0.2;
glutDisplayFunc(display);
glutPostRedisplay();
break;
case '3':
n = 0.3;
glutDisplayFunc(display);
glutPostRedisplay();
break;
case '4':
n = 0.4;
glutDisplayFunc(display);
glutPostRedisplay();
break;
case '5':
n = 0.5;
glutDisplayFunc(display);
glutPostRedisplay();
break;
case '6':
n = 0.6;
glutDisplayFunc(display);
glutPostRedisplay();
break;
case '7':
n = 0.7;
glutDisplayFunc(display);
glutPostRedisplay();
break;
case '8':
n = 0.8;
glutDisplayFunc(display);
glutPostRedisplay();
break;
case '9':
n = 0.9;
glutDisplayFunc(display);
glutPostRedisplay();
break;
case 'h':
fl_betta = false;
fl_alpha = true;
glutDisplayFunc(display);
glutPostRedisplay();
break;
case 'v':
fl_alpha = false;
fl_betta = true;
glutDisplayFunc(display);
glutPostRedisplay();
break;
case 'd':
fl_betta = true;
fl_alpha = true;
glutDisplayFunc(display);
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char** argv)
{
setlocale(LC_CTYPE, "Russian");
cout << "Выберите коэффециент ослабления" << endl;
cout << "'1' - 0.1 " << endl;
cout << "'2' - 0.2 " << endl;
cout << "'3' - 0.3 " << endl;
cout << "'4' - 0.4 " << endl;
cout << "'5' - 0.5 " << endl;
cout << "'6' - 0.6 " << endl;
cout << "'7' - 0.7 " << endl;
cout << "'8' - 0.8 " << endl;
cout << "'9' - 0.9 " << endl;
cout << "" << endl;
cout << "Измените направление движения " << endl;
cout << "'h' - горизонтальное " << endl;
cout << "'v' - вертикальное " << endl;
cout << "'d' - диагональное" << endl;
cout << "'esc' - завершение работы " << endl;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_ACCUM | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutInitWindowPosition(500, 300);
glutCreateWindow("Моделирование размытости границ движ. объекта");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}