-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
155 lines (120 loc) · 4.26 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <GL/glew.h> // Include the GLEW header file
#include <GL/glut.h> // Include the GLUT header file
#include <iostream>
#include <math.h>
#include <algorithm>
#include <time.h>
#include "Voronoi.h"
#include "VPoint.h"
void display (void);
void onEF (int n);
void reshape (int width, int height);
vor::Voronoi * v;
vor::Vertices * ver; // vrcholy
vor::Vertices * dir; // smiry, kterymi se pohybuji
// tr. from Czech: "the directions in which I move"
vor::Edges * edg; // hrany diagramu
double w = 10000;
int main (int argc, char **argv)
{
using namespace vor;
v = new Voronoi();
ver = new Vertices();
dir = new Vertices();
srand ( time(NULL) );
for(int i=0; i<50; i++)
{
ver->push_back(new VPoint( w * (double)rand()/(double)RAND_MAX , w * (double)rand()/(double)RAND_MAX ));
dir->push_back(new VPoint( (double)rand()/(double)RAND_MAX - 0.5, (double)rand()/(double)RAND_MAX - 0.5));
}
edg = v->GetEdges(ver, w, w);
std::cout << "voronois done!\n";
for(vor::Edges::iterator i = edg->begin(); i!= edg->end(); ++i)
{
if( (*i)->start == 0 )
{
std::cout << "chybi zacatek hrany!\n";
continue;
}
if( (*i)->end == 0 )
{
std::cout << "chybi konec hrany!\n";
continue;
}
}
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode (GLUT_SINGLE); // Set up a basic display buffer (only single buffered for now)
glutInitWindowSize (600, 600); // Set the width and height of the window
glutInitWindowPosition (100, 100); // Set the position of the window
glutCreateWindow ("Your first OpenGL Window"); // Set the title for the window
glutTimerFunc(100, onEF, 0);
glutDisplayFunc(display); // Tell GLUT to use the method "display" for rendering
glutReshapeFunc(reshape); // Tell GLUT to use the method "reshape" for reshaping
//glutKeyboardFunc(keyPressed); // Tell GLUT to use the method "keyPressed" for key presses
//glutKeyboardUpFunc(keyUp); // Tell GLUT to use the method "keyUp" for key up events
glutMainLoop(); // Enter GLUT's main loop
return 0;
}
void drawVoronoi()
{
vor::Vertices::iterator j = dir->begin();
for(vor::Vertices::iterator i = ver->begin(); i != ver->end(); ++i)
{
(*i)->x += (*j)->x * w/50;
(*i)->y += (*j)->y * w/50;
if( (*i)->x > w ) (*j)->x *= -1;
if( (*i)->x < 0 ) (*j)->x *= -1;
if( (*i)->y > w ) (*j)->y *= -1;
if( (*i)->y < 0 ) (*j)->y *= -1;
++j;
}
edg = v->GetEdges(ver, w, w);
//std::cout << "voronoi done";
for(vor::Vertices::iterator i = ver->begin(); i!= ver->end(); ++i)
{
glBegin(GL_QUADS);
//std::cout << (*i)->x << "\n";
glVertex2f( -1+2*(*i)->x/w -0.01, -1+2*(*i)->y/w - 0.01);
glVertex2f( -1+2*(*i)->x/w +0.01, -1+2*(*i)->y/w - 0.01);
glVertex2f( -1+2*(*i)->x/w +0.01, -1+2*(*i)->y/w + 0.01);
glVertex2f( -1+2*(*i)->x/w -0.01, -1+2*(*i)->y/w + 0.01);
glEnd();
}
for(vor::Edges::iterator i = edg->begin(); i!= edg->end(); ++i)
{
/*
glBegin(GL_LINES);
glVertex2f( -1+2*(*i)->left->x/w, -1+2*(*i)->left->y/w);
glVertex2f( -1+2*(*i)->right->x/w, -1+2*(*i)->right->y/w);
glEnd();
*/
glBegin(GL_LINES);
glVertex2f( -1+2*(*i)->start->x/w, -1+2*(*i)->start->y/w);
glVertex2f( -1+2*(*i)->end->x/w, -1+2*(*i)->end->y/w);
glEnd();
}
}
void display (void)
{
std::cout << "display\n";
glLoadIdentity(); // Load the Identity Matrix to reset our drawing locations
glTranslatef(0.0f, 0.0f, -5.0f);
glFlush();
}
void onEF(int n)
{
glutTimerFunc(20, onEF, 0);
glClear(GL_COLOR_BUFFER_BIT);//Clear the screen
glClearColor(0.0f, 0.0f, 0.2f, 1.0f); // Clear the background of our window to red
drawVoronoi();
glutSwapBuffers();
//Draw everything to the screen
}
void reshape (int width, int height)
{
glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Set our viewport to the size of our window
glMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we can manipulate how our scene is viewed
glLoadIdentity(); // Reset the projection matrix to the identity matrix so that we don't get any artifacts (cleaning up)
gluPerspective(22.5, (GLfloat)width / (GLfloat)height, 1.0, 100.0); // Set the Field of view angle (in degrees), the aspect ratio of our window, and the new and far planes
glMatrixMode(GL_MODELVIEW); // Switch back to the model view matrix, so that we can start drawing shapes correctly
}