aboutsummaryrefslogtreecommitdiff
path: root/gragl.c
blob: 430b581806174421f286d1e45189790bdea82c99 (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <stdarg.h>
#include <GL/freeglut.h>
#include <memory.h>
#include <stdio.h>
#include <math.h>

#include "gragl.h"

#define SUBWINDOWS 64

Plot2dGroup * target = NULL;

int subwindows[SUBWINDOWS];

static void enable_multi_sample() {
	glEnable(GL_MULTISAMPLE);
	glHint(GL_MULTISAMPLE_FILTER_HINT_NV, GL_NICEST);
}

static void draw_function(double from, double to, double step, Scalar(*fun)(double)) {
	glColor3f(0.9f, 0.5f, 0.2f);
	glLineWidth(2);
	glBegin(GL_LINES);

	for (double i = from; i < to; i += step) {
		Scalar current = fun(i);
		Scalar next = fun(i + step);

		if (current.valid) {
			glVertex2f(i, current.v);
			glVertex2f(i + step, next.v);
		}
	}

	glEnd();
}

void draw_axis() {
	glColor3f(1, 1, 1);
	glLineWidth(2);
	glBegin(GL_LINES);

	glVertex2f(0, 0);
	glVertex2f(1, 0);
	glVertex2f(0, 0);
	glVertex2f(0, 1);

	glVertex2f(1, 0);
	glVertex2f(0.9, 0.1);
	glVertex2f(1, 0);
	glVertex2f(0.9, -0.1);

	glVertex2f(0, 1);
	glVertex2f(0.1, 0.9);
	glVertex2f(0, 1);
	glVertex2f(-0.1, 0.9);

	glEnd();
}

void draw_grid(double from, double to) {
	glColor3f(0.2, 0.2, 0.2);

	double yl = (from + to) / 2 + 1;

	glLineWidth(2);
	glBegin(GL_LINES);

	for (double i = from; i < to; ++i) {
		glVertex2f(i, yl);
		glVertex2f(i, -yl);
	}

	for (double i = -yl; i < yl; ++i) {
		glVertex2f(from, i);
		glVertex2f(to, i);
	}

	glEnd();

	glColor3f(0.5, 0.5, 0.5);
	glBegin(GL_LINES);
	glVertex2f(from, 0);
	glVertex2f(to, 0);

	glVertex2f(0, yl);
	glVertex2f(0, -yl);
	glEnd();

	glColor3f(0.7, 0.7, 0.7);
	glBegin(GL_LINE_LOOP);
	glVertex2f(from, yl);
	glVertex2f(to, yl);
	glVertex2f(to, -yl);
	glVertex2f(from, -yl);
	glEnd();
}

static void update() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	int window = glutGetWindow();
	int plot_id = 0;

	while (subwindows[plot_id] != window) ++plot_id;

	Plot2d * plot = &(target->plots[plot_id]);

	double translate = -((plot->to + plot->from) / 2);
	double zoom = 2 / (plot->to - plot->from);
	glScalef(zoom, zoom, zoom);
	glTranslatef(translate, 0, 0);

	draw_grid(plot->from, plot->to);
	draw_axis();
	draw_function(plot->from, plot->to, plot->step, plot->func);

	glTranslatef(-translate, 0, 0);
	glScalef(1/zoom, 1/zoom, 1/zoom);

	glutSwapBuffers();
}

static void parent_update() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glutSwapBuffers();

	int width = glutGet(GLUT_WINDOW_WIDTH);
	int height = glutGet(GLUT_WINDOW_HEIGHT);
	int min = width > height ? height : width;

	unsigned n_plots = ((unsigned)sqrt(target->n));

	int plot_dim = min / (int)n_plots - (int)target->padding / 4;

	plot_dim = plot < 0 ? 1 : plot_dim;

	for (unsigned i = 0; i < SUBWINDOWS && subwindows[i] > 0; ++i) {
		glutSetWindow(subwindows[i]);
		glutReshapeWindow(
				plot_dim - target->padding / 2,
				plot_dim - target->padding / 2
		);
		glutPositionWindow(
				(i % n_plots) * plot_dim + target->padding / 2,
				(i / n_plots) * plot_dim + target->padding / 2
		);
	}
}

void plot(Plot2dGroup * group) {
	target = group;

	char * arg[1] = { "" };
	int argc = 1;

	glutInit(&argc, arg);
	glutInitWindowSize(500, 500);
	glutSetOption(GLUT_MULTISAMPLE, 8);
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
	int parent = glutCreateWindow("GRAGL");

	for (int i = 0; i < group->n; ++i) {
		int id = glutCreateSubWindow(parent, 1, 1, 1, 1);
		subwindows[i] = id;
		glutSetWindow(id);
		glutDisplayFunc(update);
	}

	glutSetWindow(parent);
	glutDisplayFunc(parent_update);

	enable_multi_sample();

	glutMainLoop();
}

Plot2dGroup * create_plot2d_group(int num_args, ...) {
	Plot2dGroup * group = (Plot2dGroup*)malloc(sizeof(Plot2dGroup));
	group->plots = (Plot2d*)malloc(sizeof(Plot2d) * num_args);

	group->n = num_args;
	group->padding = 20;

	va_list ap;
	va_start(ap, num_args);

	for (int i = 0; i < num_args; ++i) {
		group->plots[i] = va_arg(ap, Plot2d);
	}
	va_end(ap);

	return group;
}

void free_plot2d_group(Plot2dGroup * group) {
	free(group->plots);
	free(group);
}