-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterrain.c
155 lines (138 loc) · 3.57 KB
/
terrain.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
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
#include "terrain.h"
/**
* main - entry point for program, contains main event loop: checks for key
* input and calls rendering routines.
* @argc: argument count
* @argv: pointer to command line arguments
*
* Return: EXIT_SUCCESS on success, EXIT_FAILURE if some error occurs
*/
int main(int argc, char *argv[])
{
size_t i, j, nrows, ncols;
int angle, tmp;
SDL_bool quit;
SDL_Event e;
SDL_Window *win;
SDL_Renderer *rend;
float3d_t **coords;
SDL_Point **grid;
if (argc != 2)
{
fprintf(stderr, "Usage: %s <altitudes>\n", argv[0]);
exit(EXIT_FAILURE);
}
coords = get_altitudes(argv[1], &nrows, &ncols);
grid = allocate_grid(nrows, ncols);
if (!grid)
{
free_coords(coords, nrows);
exit(EXIT_FAILURE);
}
init_xycoords(coords, nrows, ncols);
init_win_rend(&win, &rend);
reset_and_clear(rend);
project_to_grid(grid, coords, nrows, ncols, angle);
draw_grid(grid, nrows, ncols, rend);
while (!quit)
{
while (SDL_PollEvent(&e))
{
tmp = angle;
quit = get_key_press(&e, &angle);
}
if (tmp != angle)
{
reset_and_clear(rend);
project_to_grid(grid, coords, nrows, ncols, angle);
draw_grid(grid, nrows, ncols, rend);
}
}
exit(EXIT_SUCCESS);
}
/**
* init_xycoords - initialize a 2d grid of points such that the center of
* the grid is origin of the x/y axes. The values are in local unit length
* coordinates.
* @coords: 2d matrix of 3d points in which to place z coordinates
*/
void init_xycoords(float3d_t **coords, size_t nrows, size_t ncols)
{
size_t i, j;
for (i = 0; i < nrows; i++)
{
for (j = 0; j < ncols; j++)
{
coords[i][j].y = i;
coords[i][j].x = j;
}
}
}
/**
* project_to_grid - takes a matrix of points in 3d coordinates and maps them
* isometrically to 2d screen space and stores the result as integers using the
* SDL_Point struct.
* @grid: 2d matrix to represent points from `coords' in screen coordinates
* @coords: 2d matrix of 3d points to be transformed into screen coordinates
* @angle: angle between 0 and 359 degrees with which to rotate the points along
* the x/y plane.
*/
void project_to_grid(SDL_Point **grid,
float3d_t **coords,
size_t nrows,
size_t ncols,
int angle)
{
size_t i, j;
float a;
float cx, cy, wx, wy, Rx, Ry, xmin, xmax, ymax;
float3d_t *coord;
xmax = INCLINATION * ncols;
xmin = -xmax;
ymax = 0.3 * 2 * nrows;
for (i = 0; i < nrows; i++)
{
for (j = 0; j < ncols; j++)
{
cx = (ncols - 1) / 2.0;
cy = (nrows - 1) / 2.0;
coord = &(coords[i][j]);
a = angle * PI / 180.0;
Rx = (cx - coord->x) * cos(a) - (cy - coord->y) * sin(a) + cx;
Ry = (cx - coord->x) * sin(a) + (cy - coord->y) * cos(a) + cy;
wx = INCLINATION * (Rx - Ry);
wy = (1 - INCLINATION) * (Rx + Ry);
wx -= xmin;
wx *= 0.8 * SCR_W / (xmax - xmin);
wx += 0.1 * SCR_W;
wy *= 0.5 * SCR_H / ymax;
wy += 0.5 * SCR_H;
wy -= 0.5 * coord->z;
grid[i][j].x = wx;
grid[i][j].y = wy;
}
}
}
/**
* draw_grid - take transformed points in screen coordinates and draw lines
* between neighboring points to give the appearance of a mesh.
* @grid: 2d matrix of points to render to the screen
* @rend: back buffer rendering agent
*/
void draw_grid(SDL_Point **grid,
size_t nrows,
size_t ncols,
SDL_Renderer *rend)
{
size_t i, j;
for (i = 0; i < nrows - 1; i++)
{
SDL_RenderDrawLines(rend, grid[i], ncols);
for (j = 0; j < ncols; j++)
SDL_RenderDrawLine(rend,
grid[i][j].x, grid[i][j].y,
grid[i+1][j].x, grid[i+1][j].y);
}
SDL_RenderDrawLines(rend, grid[i], ncols);
SDL_RenderPresent(rend);
}