I am working on the 42 school project FdF that is a computer graphics related project that uses an API for the Xlib named minilibx (linked below if you want to check out) and need to make a 3D representation of a map given as an input. The map is given as a 2D matrix and each value is its height.
After we manage to update the scale of our map, center it on the created window and display the result of our projection in isometric view we can implement some other features like adding rotation.
My problem with this feature is that when I rotate on the x or y axis 90 degrees the image seems to flip as if my “camera” was following it on the other side instead of getting the down view of the map.
I leave bellow the code and some links that might help
The project repo if you want to see some other functions : https://github.com/Artur-2k/FdF-42
To install dependencies: (Linux) sudo apt-get install gcc make xorg libxext-dev libbsd-dev -y
Minilibx: https://github.com/42Paris/minilibx-linux
Good base-level understanding of minilibx: https://harm-smits.github.io/42docs/libs/minilibx
#include "fdf.h"
void rotate_x(t_data *data, double alpha)
{
int tmp_y;
int i;
int j;
double cos_alpha;
double sin_alpha;
sin_alpha = sin(alpha);
cos_alpha = cos(alpha);
i = 0;
while (i < data->rows)
{
j = 0;
while (j < data->cols)
{
tmp_y = data->matrix[i][j].proj_y;
data->matrix[i][j].proj_y = (int)(tmp_y * cos_alpha - data->matrix[i][j].proj_z * sin_alpha);
data->matrix[i][j].proj_z = (int)(tmp_y * sin_alpha + data->matrix[i][j].proj_z * cos_alpha);
j++;
}
i++;
}
}
void rotate_y(t_data *data, double beta)
{
int tmp_x;
int i;
int j;
double cos_beta;
double sin_beta;
sin_beta = sin(beta);
cos_beta = cos(beta);
i = 0;
while (i < data->rows)
{
j = 0;
while (j < data->cols)
{
tmp_x = data->matrix[i][j].proj_x;
data->matrix[i][j].proj_x = (int)(tmp_x * cos_beta + data->matrix[i][j].proj_z * sin_beta);
data->matrix[i][j].proj_z = (int)(-tmp_x * sin_beta + data->matrix[i][j].proj_z * cos_beta);
j++;
}
i++;
}
}
void rotate_z(t_data *data, double gama)
{
int tmp_x;
int i;
int j;
double cos_gama;
double sin_gama;
sin_gama = sin(gama);
cos_gama = cos(gama);
i = 0;
while (i < data->rows)
{
j = 0;
while (j < data->cols)
{
tmp_x = data->matrix[i][j].proj_x;
data->matrix[i][j].proj_x = (int)(tmp_x * cos_gama - data->matrix[i][j].proj_y * sin_gama);
data->matrix[i][j].proj_y = (int)(tmp_x * sin_gama + data->matrix[i][j].proj_y * cos_gama);
j++;
}
i++;
}
}
void manual_rotate(t_data *data, int key)
{
// each respective key press changes the angle accordingly
if (key == XK_w)
data->alpha += ANGLE_CHANGE;
else if (key == XK_s)
data->alpha -= ANGLE_CHANGE;
else if (key == XK_d)
data->beta += ANGLE_CHANGE;
else if (key == XK_a)
data->beta -= ANGLE_CHANGE;
else if (key == XK_q)
data->gama += ANGLE_CHANGE;
else if (key == XK_e)
data->gama -= ANGLE_CHANGE;
reset_projections_set_zoom(data); // resets xyz_proj to xyz and applies scale
// re-rotate
/*
#define DEG_TO_RAD(deg) (deg * M_PI / 180)
#define NORMALIZE_ANGLE(angle) (fmod(angle, 360.0))
*/
rotate_x(data,DEG_TO_RAD (NORMALIZE_ANGLE(data->alpha)));
rotate_y(data,DEG_TO_RAD (NORMALIZE_ANGLE(data->beta)));
rotate_z(data,DEG_TO_RAD (NORMALIZE_ANGLE(data->gama)));
// update offsets
get_bounderies(data); // makes it rotate on itself on not on the 0.0
get_offsets(data); //center the map with the same offsets
//* shitft the object offset + translation(if added any)
translate(data, data->xoffset + data->xtranslate, data->yoffset + data->ytranslate);
//* updates the image (could just 0 the memory of the image instead too)
update_img(data);
}
Artur Carvalho is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.