-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
126 lines (110 loc) · 2.35 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <windows.h>
#include <GL/glut.h>
#define sleep(x) Sleep(1000 * (x))
#define sizeX 1000
#define sizeY 500
#define wallSize 1
double grid[sizeY][sizeX];
double newGrid[sizeY][sizeX];
double filter[3][3];
int width = 1000;
int height = 500;
double activation(double x){
return x;
}
void CVFilter(){
int x,y;
double cv; //covolved value
for(y=1;y<sizeY-1;y++){
for(x=1;x<sizeX-1;x++){
cv = (grid[y][x]*filter[1][1]) + (grid[y][x-1]*filter[1][0]) + (grid[y][x+1]*filter[1][2])+
(grid[y+1][x]*filter[2][1]) + (grid[y+1][x-1]*filter[2][0]) + (grid[y+1][x+1]*filter[2][2])+
(grid[y-1][x]*filter[0][1]) + (grid[y-1][x-1]*filter[0][0]) + (grid[y-1][x+1]*filter[0][2]);
newGrid[y][x] = activation(cv);
}
}
}
void drawGrid(){
int x,y;
double rgb;
glPointSize(wallSize);
glBegin(GL_POINTS);
for(y=0;y<sizeY;y++){
for(x=0;x<sizeX;x++){
rgb = grid[y][x];
glColor3f(rgb,0,0);
glVertex2i((x*wallSize),(y*wallSize));
}
}
glEnd();
}
int randRange(int min, int max){
return min + (int) (rand() / (double) (RAND_MAX + 1) * (max - min + 1));
}
void fillGrid(){
int x,y;
for(y=0;y<sizeY;y++){
for(x=0;x<sizeX;x++){
grid[y][x] = randRange(-1,1);
}
}
}
void cloneGrids(){
int x,y;
for(y=0;y<sizeY;y++){
for(x=0;x<sizeX;x++){
grid[y][x] = newGrid[y][x];
}
}
}
void fillFilter(){
int x,y;
for(y=0;y<=3;y++){
for(x=0;x<=3;x++){
filter[y][x] = (double)rand() / (double)RAND_MAX;
}
}
}
void printFilter(){
int x,y;
for(y=0;y<3;y++){
for(x=0;x<3;x++){
printf("%f | ", filter[y][x]);
}
printf("\n");
printf("--------------------------------");
printf("\n");
}
}
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//---------------------Proccess--------------------
CVFilter();
cloneGrids();
//-----------------------Draw----------------------
drawGrid();
//-------------------------------------------------
glutSwapBuffers();
glutPostRedisplay();
}
void init(){
glClearColor(0.3,0.3,0.3,0);
gluOrtho2D(0,width,height,0);
}
void main(int argc, char** argv){
srand(time(NULL));
fillGrid();
fillFilter();
printFilter();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(width,height);
glutCreateWindow("OpenGL");
init();
glutDisplayFunc(display);
glutMainLoop();
}