fixed cube rendering to render rotated cubes properly

This commit is contained in:
Alexandre 2024-12-30 20:15:13 +01:00
parent 69a16c4920
commit ce8563cc94
11 changed files with 90 additions and 21 deletions

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -118,7 +118,7 @@ bool str_equal(char* s1, char* s2) {
// ------------------------------------------------------------------------------------------------ //
cube_0 create_cube_0(double x, double y, double z, double w, double h, double d, int r, int g, int b) {
cube_0 create_cube_0(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b) {
cube_0 cb ;
cb.red = r ;
cb.green = g ;
@ -129,14 +129,14 @@ cube_0 create_cube_0(double x, double y, double z, double w, double h, double d,
cb.w = w ;
cb.h = h ;
cb.d = d ;
cb.hz_angle = 0.0 ;
cb.vt_angle = 0.0 ;
cb.hz_angle = hz_a ;
cb.vt_angle = vt_a ;
return cb ;
}
cube create_cube(double x, double y, double z, double w, double h, double d, int r, int g, int b) {
cube create_cube(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b) {
cube cb = malloc(sizeof(cube_0));
*cb = create_cube_0(x, y, z, w, d, h, r, g, b) ;
*cb = create_cube_0(x, y, z, w, h, d, hz_a, vt_a, r, g, b) ;
return cb;
}

View File

@ -15,8 +15,8 @@ int line_count(char* filename);
int str_to_int(char* s);
bool str_equal(char* s1, char* s2);
cube_0 create_cube_0(double x, double y, double z, double w, double h, double d, int r, int g, int b);
cube create_cube(double x, double y, double z, double w, double h, double d, int r, int g, int b);
cube_0 create_cube_0(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b);
cube create_cube(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b);
void free_cube(cube c);
double convex_pt(double a, double b, double theta);
double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1);

View File

@ -214,25 +214,84 @@ void draw_segment(SDL_Renderer* renderer, double sx, double sy, double sz, doubl
} // else : not visible (behind camera)
}
void axialRotation_X0(double* y, double* z, double theta) {
double y0 = *y ;
*y = (*y)*cos(theta) - (*z)*sin(theta) ;
*z = (*z)*cos(theta) + y0*sin(theta) ;
}
void axialRotation_X(double* y, double* z, double theta, double cst_y, double cst_z) {
// project the space onto the (y, z) plane to get cst_y and cst_z
double y1 = *y - cst_y;
double z1 = *z - cst_z;
axialRotation_X0(&y1, &z1, theta);
*y = y1 + cst_y ;
*z = z1 + cst_z ;
}
void axialRotation_Y0(double* x, double* z, double theta) {
double x0 = *x ;
*x = (*x)*cos(theta) + (*z)*sin(theta) ;
*z = (*z)*cos(theta) - x0*sin(theta) ;
}
void axialRotation_Y(double* x, double* z, double theta, double cst_x, double cst_z) {
// project the space onto the (y, z) plane to get cst_y and cst_z
double x1 = *x - cst_x;
double z1 = *z - cst_z;
axialRotation_Y0(&x1, &z1, theta);
*x = x1 + cst_x ;
*z = z1 + cst_z ;
}
void axialRotation_Z0(double* x, double* y, double theta) {
double x0 = *x ;
*x = (*x)*cos(theta) - (*y)*sin(theta) ;
*y = (*y)*cos(theta) + x0*sin(theta) ;
}
void axialRotation_Z(double* x, double* y, double theta, double cst_x, double cst_y) {
// project the space onto the (y, z) plane to get cst_y and cst_z
double x1 = *x - cst_x;
double y1 = *y - cst_y;
axialRotation_Z0(&x1, &y1, theta);
*x = x1 + cst_x ;
*y = y1 + cst_y ;
}
void drawSegmentRotated(SDL_Renderer* renderer, double sx, double sy, double sz, double ex, double ey, double ez, double hz_angle, double vt_angle, double center_x, double center_y, double center_z) {
double psx = sx;
double psy = sy;
double psz = sz;
double pex = ex;
double pey = ey;
double pez = ez;
axialRotation_Y(&psx, &psz, hz_angle, center_x, center_z);
axialRotation_Y(&pex, &pez, hz_angle, center_x, center_z);
axialRotation_X(&psy, &psz, vt_angle, center_y, center_z);
axialRotation_X(&pey, &pez, vt_angle, center_y, center_z);
draw_segment(renderer, psx, psy, psz, pex, pey, pez);
}
void drawOutlineOfCube_0(SDL_Renderer* renderer, cube_0 c) {
SDL_SetRenderDrawColor(renderer, c.red, c.green, c.blue, 255) ;
// x = constant
draw_segment(renderer, c.x, c.y, c.z, c.x + c.w, c.y, c.z) ;
draw_segment(renderer, c.x, c.y + c.h, c.z, c.x + c.w, c.y + c.h, c.z) ;
draw_segment(renderer, c.x, c.y, c.z + c.d, c.x + c.w, c.y, c.z + c.d) ;
draw_segment(renderer, c.x, c.y + c.h, c.z + c.d, c.x + c.w, c.y + c.h, c.z + c.d) ;
drawSegmentRotated(renderer, c.x, c.y, c.z, c.x + c.w, c.y, c.z, c.hz_angle, c.vt_angle, c.x + c.w/2.0, c.y + c.h/2.0, c.z + c.d/2.0) ;
drawSegmentRotated(renderer, c.x, c.y + c.h, c.z, c.x + c.w, c.y + c.h, c.z, c.hz_angle, c.vt_angle, c.x + c.w/2.0, c.y + c.h/2.0, c.z + c.d/2.0) ;
drawSegmentRotated(renderer, c.x, c.y, c.z + c.d, c.x + c.w, c.y, c.z + c.d, c.hz_angle, c.vt_angle, c.x + c.w/2.0, c.y + c.h/2.0, c.z + c.d/2.0) ;
drawSegmentRotated(renderer, c.x, c.y + c.h, c.z + c.d, c.x + c.w, c.y + c.h, c.z + c.d, c.hz_angle, c.vt_angle, c.x + c.w/2.0, c.y + c.h/2.0, c.z + c.d/2.0) ;
// y = constant
draw_segment(renderer, c.x, c.y, c.z, c.x, c.y + c.h, c.z) ;
draw_segment(renderer, c.x + c.w, c.y, c.z, c.x + c.w, c.y + c.h, c.z) ;
draw_segment(renderer, c.x, c.y, c.z + c.d, c.x, c.y + c.h, c.z + c.d) ;
draw_segment(renderer, c.x + c.w, c.y, c.z + c.d, c.x + c.w, c.y + c.h, c.z + c.d) ;
drawSegmentRotated(renderer, c.x, c.y, c.z, c.x, c.y + c.h, c.z, c.hz_angle, c.vt_angle, c.x + c.w/2.0, c.y + c.h/2.0, c.z + c.d/2.0) ;
drawSegmentRotated(renderer, c.x + c.w, c.y, c.z, c.x + c.w, c.y + c.h, c.z, c.hz_angle, c.vt_angle, c.x + c.w/2.0, c.y + c.h/2.0, c.z + c.d/2.0) ;
drawSegmentRotated(renderer, c.x, c.y, c.z + c.d, c.x, c.y + c.h, c.z + c.d, c.hz_angle, c.vt_angle, c.x + c.w/2.0, c.y + c.h/2.0, c.z + c.d/2.0) ;
drawSegmentRotated(renderer, c.x + c.w, c.y, c.z + c.d, c.x + c.w, c.y + c.h, c.z + c.d, c.hz_angle, c.vt_angle, c.x + c.w/2.0, c.y + c.h/2.0, c.z + c.d/2.0) ;
// z = constant
draw_segment(renderer, c.x, c.y, c.z, c.x, c.y, c.z + c.d) ;
draw_segment(renderer, c.x + c.w, c.y, c.z, c.x + c.w, c.y, c.z + c.d) ;
draw_segment(renderer, c.x, c.y + c.h, c.z, c.x, c.y + c.h, c.z + c.d) ;
draw_segment(renderer, c.x + c.w, c.y + c.h, c.z, c.x + c.w, c.y + c.h, c.z + c.d) ;
drawSegmentRotated(renderer, c.x, c.y, c.z, c.x, c.y, c.z + c.d, c.hz_angle, c.vt_angle, c.x + c.w/2.0, c.y + c.h/2.0, c.z + c.d/2.0) ;
drawSegmentRotated(renderer, c.x + c.w, c.y, c.z, c.x + c.w, c.y, c.z + c.d, c.hz_angle, c.vt_angle, c.x + c.w/2.0, c.y + c.h/2.0, c.z + c.d/2.0) ;
drawSegmentRotated(renderer, c.x, c.y + c.h, c.z, c.x, c.y + c.h, c.z + c.d, c.hz_angle, c.vt_angle, c.x + c.w/2.0, c.y + c.h/2.0, c.z + c.d/2.0) ;
drawSegmentRotated(renderer, c.x + c.w, c.y + c.h, c.z, c.x + c.w, c.y + c.h, c.z + c.d, c.hz_angle, c.vt_angle, c.x + c.w/2.0, c.y + c.h/2.0, c.z + c.d/2.0) ;
}
// -------------------------------------------------------------------------------------------------------------------------------- //

View File

@ -19,6 +19,13 @@ void drawCharToRenderer(SDL_Renderer* renderer, imgs data, char c, int X, int Y,
void drawNumberToRenderer(SDL_Renderer* renderer, imgs data, int n, int X, int Y, int W, int H, int Woffset);
void drawStringToRenderer(SDL_Renderer* renderer, imgs data, char* s, int X, int Y, int W, int H);
void axialRotation_X0(double* y, double* z, double theta);
void axialRotation_X(double* y, double* z, double theta, double cst_y, double cst_z);
void axialRotation_Y0(double* x, double* z, double theta);
void axialRotation_Y(double* x, double* z, double theta, double cst_x, double cst_z);
void axialRotation_Z0(double* x, double* y, double theta);
void axialRotation_Z(double* x, double* z, double theta, double cst_x, double cst_z);
void project_to_camera(double x0, double y0, double z0, double* rx, double* ry, double* rz);
void draw_segment(SDL_Renderer* renderer, double sx, double sy, double sz, double ex, double ey, double ez);
void drawOutlineOfCube_0(SDL_Renderer* renderer, cube_0 c);

View File

@ -16,4 +16,5 @@
#include "base.h"
#include "generation.h"
// ------------------------------------------------------------------------ //
hashtbl visited ;

View File

@ -47,7 +47,8 @@ int main(int argc, char** argv) {
init_csts() ;
import_digits(rend) ;
import_letters(rend) ;
cube_0 cb = create_cube_0(1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 255, 255, 255) ;
cube_0 cb = create_cube_0(1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 0.0, 3.14159/4.0, 255, 255, 255) ;
cube_0 cb2 = create_cube_0(1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 0.0, 0.0, 255, 0, 255) ;
while(true) {
resetRenderer(rend) ;
@ -55,6 +56,7 @@ int main(int argc, char** argv) {
playerActions() ;
drawData(rend) ;
drawOutlineOfCube_0(rend, cb) ;
drawOutlineOfCube_0(rend, cb2) ;
//draw_segment(rend, 1.0, 1.0, 1.0, 10.0, 2.0, -1.0) ;
updateRenderer(rend) ;