aboutsummaryrefslogtreecommitdiff
path: root/src/render_cpu.c
blob: 8f8d4399786f50bc209c55bcbe0382a417fc57f8 (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
/*
 * render.c
 *
 *  Created on: 15.01.2018
 *      Author: Superleo1810
 */

#include "render_cpu.h"
#define HAVE_STRUCT_TIMESPEC

void idle_cpu_dummy(void)
{
//	glBindTexture(GL_TEXTURE_2D, config_cpu->tex);
//	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, config_cpu->width,
//			config_cpu->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, config_cpu->arr);
//	glBindTexture(GL_TEXTURE_2D, 0);
	glutPostRedisplay();
}

void init_cpu(CpuConfig *config)
{
	config_cpu = config;
	x_min_s = -2.0;
	x_max_s = 1.0;
	y_min_s = -1.0;
	y_max_s = 1.0;
	x_min = x_min_s;
	x_max = x_max_s;
	y_min = y_min_s;
	y_max = y_max_s;

	calculate(x_min, y_min, x_max, y_max, config_cpu->set_func, config_cpu->arr);
	t_old_cpu = glutGet(GLUT_ELAPSED_TIME);
}

void render_cpu(void)
{
	glBindTexture(GL_TEXTURE_2D, config_cpu->tex);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, config_cpu->width,
				config_cpu->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, config_cpu->arr);
		glBindTexture(GL_TEXTURE_2D, 0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glBindTexture(GL_TEXTURE_2D, config_cpu->tex);
	glEnable(GL_TEXTURE_2D);

	glBegin(GL_QUADS);
	    glTexCoord2i(0, 0); glVertex2i(0, 0);
	    glTexCoord2i(0, 1); glVertex2i(0, config_cpu->height);
	    glTexCoord2i(1, 1); glVertex2i(config_cpu->width, config_cpu->height);
	    glTexCoord2i(1, 0); glVertex2i(config_cpu->width, 0);
	glEnd();

	glDisable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, 0);
	glutSwapBuffers();
}

void calculate(d64 x_min, d64 y_min, d64 x_max, d64 y_max, u32 (*sfunc) (d64, d64, u32), u32 *arr)
{
	pthread_t thread;
	ThreadArgs *args = (ThreadArgs *) malloc(config_cpu->threads * sizeof(ThreadArgs));
	for(u8 i = 0; i < config_cpu->threads; i++)
	{
		args[i] = (ThreadArgs) { .tc = config_cpu->threads, .tid = i, .x_min = x_min, .y_min = y_min, .x_max = x_max, .y_max = y_max, .sfunc = sfunc, .arr = arr };
		pthread_create(&thread, NULL, calculate_t, (void *)&args[i]);
	}
	pthread_join(thread, NULL);
	free(args);
}

void calculate_t(void *args)
{
	ThreadArgs *_args = (ThreadArgs *)args;
	d64 x_math, y_math;
	u32 iterations;
	for (u32 y = (config_cpu->height/_args->tc)*(_args->tid); y < config_cpu->height; y++)
	{
		for (u32 x = 0; x < config_cpu->width; x++)
		{
			x_math = _args->x_min + ((d64) x * (_args->x_max - _args->x_min)) / config_cpu->width;
			y_math = _args->y_min + ((d64) (config_cpu->height - y) * (_args->y_max - _args->y_min)) / config_cpu->height;
			iterations = _args->sfunc(x_math, y_math, config_cpu->iterations);
//			color[2].s0 = ((1.0f + native_cos(c)) * 0.5f) * 255;
//			color[2].s1 = ((1.0f + native_cos(2.0f * c + 2.0f * 3.1416f / 3.0f)) * 0.5f) * 255;
//			color[2].s2 = ((1.0f + native_cos(c - 2.0f * 3.1416f / 3.0f)) * 0.5f) * 255;
			_args->arr[COORDS(x, y, config_cpu->width)] = (((1<<24)-1)*iterations)/config_cpu->iterations;
		}
	}
}

void idle_cpu(void)
{
	int t = 0, delta = 0;
	do
	{
		t = glutGet(GLUT_ELAPSED_TIME);
		delta = t - t_old_cpu;
	}
	while(delta < 16); // TODO: Hardcoded FPS
	t_old_cpu = t;

	calculate(x_min, y_min, x_max, y_max, config_cpu->set_func, config_cpu->arr);
	//glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, config_cpu->tex);
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, config_cpu->width, config_cpu->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, config_cpu->arr);
	glBindTexture(GL_TEXTURE_2D, 0);
	ft+=(config_cpu->speed*(delta/1000.0));
	x_min = x_min_s + config_cpu->zoom_func(ft, (d64)2.0 + config_cpu->to_x);
	y_min = y_min_s + config_cpu->zoom_func(ft, (d64)1.0 + config_cpu->to_y);
	x_max = x_max_s - config_cpu->zoom_func(ft, (d64)1.0 - config_cpu->to_x);
	y_max = y_max_s - config_cpu->zoom_func(ft, (d64)1.0 - config_cpu->to_y);
	glutPostRedisplay();
}

void keyboard_cpu(unsigned char key, int mouseX, int mouseY)
{
	switch(key)
	{
	case 'i':
		config_cpu->iterations++;
		break;
	case 'd':
		config_cpu->iterations--;
		break;
	}
}

void mouse_cpu(int button, int state, int x, int y)
{
	if (state == GLUT_DOWN)
	{
		switch(button)
		{
		case GLUT_LEFT_BUTTON:
			config_cpu->to_x = x_min + ((d64) x * (x_max - x_min)) / config_cpu->width;
			config_cpu->to_y = y_min + ((d64) (config_cpu->height - y) * (y_max - y_min)) / config_cpu->height;
			t_old_cpu = glutGet(GLUT_ELAPSED_TIME);
			glutIdleFunc(idle_cpu);
			break;
		case GLUT_RIGHT_BUTTON:
			glutIdleFunc(idle_cpu_dummy);
			break;
		}
	}
}