fixed spinning cubes

This commit is contained in:
Alexandre 2025-01-31 13:53:23 +01:00
parent 0cf97eb7b5
commit af20c1084e
8 changed files with 64 additions and 204 deletions

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -240,9 +240,9 @@ double square_z_distance_to_camera(
void rotate_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0* cb) { void rotate_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0* cb) {
// align pt to (0, 0, 0) // align pt to (0, 0, 0)
double x = x0 - (cb->x + cb->w/2) ; double x = x0 - (cb->x + cb->w/2.0) ;
double y = y0 - (cb->y + cb->h/2) ; double y = y0 - (cb->y + cb->h/2.0) ;
double z = z0 - (cb->z + cb->d/2) ; double z = z0 - (cb->z + cb->d/2.0) ;
// rotate (y) // rotate (y)
double xry = x*cos(cb->hz_angle) - z*sin(cb->hz_angle) ; double xry = x*cos(cb->hz_angle) - z*sin(cb->hz_angle) ;
@ -250,9 +250,9 @@ void rotate_cube(double x0, double y0, double z0, double* rx, double* ry, double
double zry = z*cos(cb->hz_angle) + x*sin(cb->hz_angle) ; double zry = z*cos(cb->hz_angle) + x*sin(cb->hz_angle) ;
// rotate (x) // rotate (x)
*rx = (cb->x + cb->w/2) + xry ; *rx = (cb->x + cb->w/2.0) + xry ;
*ry = (cb->y + cb->h/2) + yry*cos(cb->vt_angle) - zry*sin(cb->vt_angle) ; *ry = (cb->y + cb->h/2.0) + yry*cos(cb->vt_angle) - zry*sin(cb->vt_angle) ;
*rz = (cb->z + cb->d/2) + zry*cos(cb->vt_angle) + yry*sin(cb->vt_angle) ; *rz = (cb->z + cb->d/2.0) + zry*cos(cb->vt_angle) + yry*sin(cb->vt_angle) ;
} }
pt_2d pt_rotate_cube_3d(double x0, double y0, double z0, cube_0* cb) { pt_2d pt_rotate_cube_3d(double x0, double y0, double z0, cube_0* cb) {
@ -985,54 +985,54 @@ pt_2d gl_project(pt_2d p) {
void gl_renderSurface(unsigned int shaderProgram, unsigned int VAO, unsigned int VBO, cube_0* c, int sf) { void gl_renderSurface(unsigned int shaderProgram, unsigned int VAO, unsigned int VBO, cube_0* c, int sf) {
float vertices[] = { float vertices[] = {
(float)(c->x), (float)(c->y) , (float)(c->z), (float)(-c->w/2.0), (float)(-c->h/2.0), (float)(-c->d/2.0),
(float)(c->x), (float)(c->y + c->h), (float)(c->z), (float)(-c->w/2.0), (float)(c->h/2.0), (float)(-c->d/2.0),
(float)(c->x), (float)(c->y + c->h), (float)(c->z + c->d), (float)(-c->w/2.0), (float)(c->h/2.0), (float)(c->d/2.0),
(float)(c->x), (float)(c->y) , (float)(c->z), (float)(-c->w/2.0), (float)(-c->h/2.0), (float)(-c->d/2.0),
(float)(c->x), (float)(c->y) , (float)(c->z + c->d), (float)(-c->w/2.0), (float)(-c->h/2.0), (float)(c->d/2.0),
(float)(c->x), (float)(c->y + c->h), (float)(c->z + c->d), (float)(-c->w/2.0), (float)(c->h/2.0), (float)(c->d/2.0),
(float)(c->x + c->w), (float)(c->y) , (float)(c->z), (float)(c->w/2.0), (float)(-c->h/2.0), (float)(-c->d/2.0),
(float)(c->x + c->w), (float)(c->y + c->h), (float)(c->z), (float)(c->w/2.0), (float)(c->h/2.0), (float)(-c->d/2.0),
(float)(c->x + c->w), (float)(c->y + c->h), (float)(c->z + c->d), (float)(c->w/2.0), (float)(c->h/2.0), (float)(c->d/2.0),
(float)(c->x + c->w), (float)(c->y) , (float)(c->z), (float)(c->w/2.0), (float)(-c->h/2.0), (float)(-c->d/2.0),
(float)(c->x + c->w), (float)(c->y) , (float)(c->z + c->d), (float)(c->w/2.0), (float)(-c->h/2.0), (float)(c->d/2.0),
(float)(c->x + c->w), (float)(c->y + c->h), (float)(c->z + c->d), (float)(c->w/2.0), (float)(c->h/2.0), (float)(c->d/2.0),
(float)(c->x) , (float)(c->y), (float)(c->z), (float)(-c->w/2.0), (float)(-c->h/2.0), (float)(-c->d/2.0),
(float)(c->x + c->w), (float)(c->y), (float)(c->z), (float)(c->w/2.0), (float)(-c->h/2.0), (float)(-c->d/2.0),
(float)(c->x + c->w), (float)(c->y), (float)(c->z + c->d), (float)(c->w/2.0), (float)(-c->h/2.0), (float)(c->d/2.0),
(float)(c->x) , (float)(c->y), (float)(c->z), (float)(-c->w/2.0), (float)(-c->h/2.0), (float)(-c->d/2.0),
(float)(c->x) , (float)(c->y), (float)(c->z + c->d), (float)(-c->w/2.0), (float)(-c->h/2.0), (float)(c->d/2.0),
(float)(c->x + c->w), (float)(c->y), (float)(c->z + c->d), (float)(c->w/2.0), (float)(-c->h/2.0), (float)(c->d/2.0),
(float)(c->x) , (float)(c->y + c->h), (float)(c->z), (float)(-c->w/2.0), (float)(c->h/2.0), (float)(-c->d/2.0),
(float)(c->x + c->w), (float)(c->y + c->h), (float)(c->z), (float)(c->w/2.0), (float)(c->h/2.0), (float)(-c->d/2.0),
(float)(c->x + c->w), (float)(c->y + c->h), (float)(c->z + c->d), (float)(c->w/2.0), (float)(c->h/2.0), (float)(c->d/2.0),
(float)(c->x) , (float)(c->y + c->h), (float)(c->z), (float)(-c->w/2.0), (float)(c->h/2.0), (float)(-c->d/2.0),
(float)(c->x) , (float)(c->y + c->h), (float)(c->z + c->d), (float)(-c->w/2.0), (float)(c->h/2.0), (float)(c->d/2.0),
(float)(c->x + c->w), (float)(c->y + c->h), (float)(c->z + c->d), (float)(c->w/2.0), (float)(c->h/2.0), (float)(c->d/2.0),
(float)(c->x) , (float)(c->y) , (float)(c->z), (float)(-c->w/2.0), (float)(-c->h/2.0), (float)(-c->d/2.0),
(float)(c->x + c->w), (float)(c->y) , (float)(c->z), (float)(c->w/2.0), (float)(-c->h/2.0), (float)(-c->d/2.0),
(float)(c->x + c->w), (float)(c->y + c->h), (float)(c->z), (float)(c->w/2.0), (float)(c->h/2.0), (float)(-c->d/2.0),
(float)(c->x) , (float)(c->y) , (float)(c->z), (float)(-c->w/2.0), (float)(-c->h/2.0), (float)(-c->d/2.0),
(float)(c->x) , (float)(c->y + c->h), (float)(c->z), (float)(-c->w/2.0), (float)(c->h/2.0), (float)(-c->d/2.0),
(float)(c->x + c->w), (float)(c->y + c->h), (float)(c->z), (float)(c->w/2.0), (float)(c->h/2.0), (float)(-c->d/2.0),
(float)(c->x) , (float)(c->y) , (float)(c->z + c->d), (float)(-c->w/2.0), (float)(-c->h/2.0), (float)(c->d/2.0),
(float)(c->x + c->w), (float)(c->y) , (float)(c->z + c->d), (float)(c->w/2.0), (float)(-c->h/2.0), (float)(c->d/2.0),
(float)(c->x + c->w), (float)(c->y + c->h), (float)(c->z + c->d), (float)(c->w/2.0), (float)(c->h/2.0), (float)(c->d/2.0),
(float)(c->x) , (float)(c->y) , (float)(c->z + c->d), (float)(-c->w/2.0), (float)(-c->h/2.0), (float)(c->d/2.0),
(float)(c->x) , (float)(c->y + c->h), (float)(c->z + c->d), (float)(-c->w/2.0), (float)(c->h/2.0), (float)(c->d/2.0),
(float)(c->x + c->w), (float)(c->y + c->h), (float)(c->z + c->d), (float)(c->w/2.0), (float)(c->h/2.0), (float)(c->d/2.0),
}; };
mat4 model, view, projection; mat4 model, view, projection;
glm_mat4_identity(model); glm_mat4_identity(model);
//glm_rotate(model, (float)(c->hz_angle), (vec3){0.0f, 1.0f, 0.0f}); glm_translate(model, (vec3){(float)(c->x+c->w/2.0), (float)(c->y+c->h/2.0), (float)(c->z+c->d/2.0)});
//glm_rotate(model, (float)(c->vt_angle), (vec3){1.0f, 0.0f, 0.0f}); glm_rotate(model, (float)(-c->hz_angle), (vec3){0.0f, 1.0f, 0.0f});
//glm_translate(model, (vec3){(float)camx, (float)camy, (float)(-camz)}); glm_rotate(model, (float)(c->vt_angle), (vec3){1.0f, 0.0f, 0.0f});
//glm_translate(model, (vec3){0.0f, 0.0f, 0.0f});
vec3 dir0; vec3 dir0;
vec3 direction; vec3 direction;

View File

@ -41,7 +41,7 @@ void init_ent_generator(int n) {
hashtbl_entities[0].id = 0; hashtbl_entities[0].id = 0;
hashtbl_entities[0].name = "Coin"; // 0 = default hashtbl_entities[0].name = "Coin"; // 0 = default
hashtbl_entities[0].updatePos = &speen ; hashtbl_entities[0].updatePos = &speen2 ;
hashtbl_entities[0].onHit = &detectHit ; hashtbl_entities[0].onHit = &detectHit ;
hashtbl_entities[0].onDeath = NULL ; hashtbl_entities[0].onDeath = NULL ;

View File

@ -110,7 +110,7 @@ const char *vertexShaderSource = "#version 330 core\n"
const char *fragmentShaderSource = "#version 330 core\n" const char *fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n" "out vec4 FragColor;\n"
"void main() {\n" "void main() {\n"
" FragColor = vec4(1.0, 0.5, 0.2, 1.0);\n" " FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
"}\0"; "}\0";
int main_alt() { int main_alt() {
@ -205,6 +205,9 @@ int main_alt() {
init_proj(); init_proj();
parse_rooms(5); parse_rooms(5);
int fps = 60 ;
int interval = 1000000/fps ;
clock_t origin = clock(); clock_t origin = clock();
clock_t finish = clock(); clock_t finish = clock();
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
@ -216,7 +219,6 @@ int main_alt() {
generate_nearby_chunks(1); generate_nearby_chunks(1);
//gl_renderTriangle(shaderProgram, VAO, VBO, -0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.0, 0.5, 0.0, 1.0, 1.0, 1.0);
gl_renderAll(shaderProgram, VAO, VBO); gl_renderAll(shaderProgram, VAO, VBO);
//printf("01\n"); //printf("01\n");
@ -225,11 +227,15 @@ int main_alt() {
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// ------------------------------------------------------------------------------- // -------------------------------------------------------------------------------
finish = clock(); finish = clock();
processInput(window, ((float)origin - (float)finish)/CLOCKS_PER_SEC); processInput(window, ((float)finish - (float)origin)/CLOCKS_PER_SEC);
update_entities(((float)origin - (float)finish)/CLOCKS_PER_SEC); update_entities(((float)finish - (float)origin)/CLOCKS_PER_SEC);
updateAllProj(((float)origin - (float)finish)/CLOCKS_PER_SEC); updateAllProj(((float)finish - (float)origin)/CLOCKS_PER_SEC);
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
printf("%f\n", 1.0f/(((float)origin - (float)finish)/CLOCKS_PER_SEC));
usleep(interval);
} }
for(int k = 0; k < MAX_SIZE; k++) { for(int k = 0; k < MAX_SIZE; k++) {
@ -266,150 +272,4 @@ int main_alt() {
int main(int argc, char** argv) { int main(int argc, char** argv) {
srand(time(NULL)); srand(time(NULL));
return main_alt(); return main_alt();
//-------------------------------------------------------------------------------//
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf( "error initializing SDL: %s\n", SDL_GetError());
}
SDL_Window* win = SDL_CreateWindow("Game",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
1500, 1000, 0);
Uint32 render_flags = SDL_RENDERER_ACCELERATED;
SDL_Renderer* rend = SDL_CreateRenderer(win, -1, render_flags);
if(rend == NULL) {
printf( "ERROR : cannot initialize SDL renderer\n");
exit(1);
}
printf( "%d\n", SDL_SetRenderDrawBlendMode(rend, SDL_BLENDMODE_BLEND));
//-------------------------------------------------------------------------------//
if(SDL_Init(SDL_INIT_AUDIO)) {
fprintf(stderr, "cannot initialize audio");
exit(1);
}
SDL_SetRelativeMouseMode(true) ;
/* -------------------------------------------------------- */
int fps = 60;
int interval = 1000000/fps;
double intervalf = 1.0/((double)(fps));
bool debug_main = true ;
init_csts();
init_hashtbl();
init_draworder();
init_ent_generator(10);
trInit();
init_proj();
parse_rooms(5);
import_digits(rend) ;
import_letters(rend) ;
sim_time = 0.0 ;
clock_t origin = clock();
clock_t finish = clock();
clock_t entstart = clock();
clock_t entend = clock();
float delta;
while(!stop_evetything) {
resetRenderer(rend) ;
origin = clock();
SDL_SetRenderDrawColor(rend, 255, 255, 255, SDL_ALPHA_OPAQUE) ;
entend = clock();
//printf("00\n");
//printf("%s\n", SDL_GetError());
//fflush(stdout);
playerActions(((float)entend - (float)entstart)/CLOCKS_PER_SEC) ;
//printf("01\n");
//fflush(stdout);
generate_nearby_chunks(1);
//printf("02\n");
//fflush(stdout);
entend = clock();
update_entities(((float)entend - (float)entstart)/CLOCKS_PER_SEC);
updateAllProj(((float)entend - (float)entstart)/CLOCKS_PER_SEC);
//printf("03\n");
//fflush(stdout);
entstart = clock();
//printf("-->%d\n", triangles_i);
drawCurrentRoom(rend);
//printf("-->%d\n", triangles_i);
//printf("-->%d\n", triangles_i);
//printf("04\n");
//printf("%s\n", SDL_GetError());
//fflush(stdout);
drawData(rend) ;
//printf("05\n");
//fflush(stdout);
drawHPbar(rend);
//printf("06\n");
//fflush(stdout);
finish = clock();
fade_dmg = max(fade_dmg-5, 0);
delta = ((float)finish - (float)origin)/CLOCKS_PER_SEC;
//printf("07\n");
//fflush(stdout);
drawNumberToRenderer(rend, digits, (int)(1.0f/delta), 720, 60, 75/2, 105/2, 0);
drawNumberToRenderer(rend, digits, (int)(10*sim_time), 720, 110, 75/2, 105/2, 0);
drawNumberToRenderer(rend, digits, coins, 1500/2-55, 1000 - 70, 75/3, 105/3, 0);
//printf("08\n");
//fflush(stdout);
//printf("%s\n", SDL_GetError());
updateRenderer(rend) ;
sim_time += delta + intervalf ;
//printf("09\n");
//fflush(stdout);
if(player_hp <= 0) {
stop_evetything = true ;
}
usleep(interval) ;
}
//printf("GPNE\n");
//fflush(stdout);
free_digits(digits) ;
for(int k = 0; k < MAX_SIZE; k++) {
free(triangles_to_render[k]);
free(triangles_og_coords[k]);
}
free(drawOrder);
free(triangles_to_render);
free(triangles_og_coords);
free(reds);
free(greens);
free(blues);
free(triangles_order);
free(triangles_shr);
free(visited_tri);
free_proj();
//printf("10\n");
//fflush(stdout);
hashtbl_free(visited);
free_pool();
/* -------------------------------------------------------- */
SDL_DestroyRenderer(rend);
//printf("10\n");
//fflush(stdout);
SDL_DestroyWindow(win);
//printf("11\n");
//fflush(stdout);
SDL_Quit();
//printf("12\n");
//fflush(stdout);
/* -------------------------------------------------------- */
//printf("Done\n") ;
//fflush(stdout);
return 0;
} }

View File

@ -12,12 +12,12 @@ Teleporters :
[7.0, 0.0, -1.0, 2.0, 1.0, 2.0, 0.0, 0.0, 0, 0, 255; 1, 0] [7.0, 0.0, -1.0, 2.0, 1.0, 2.0, 0.0, 0.0, 0, 0, 255; 1, 0]
Entities : Entities :
[0.0, 3.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0] [1.0, 3.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
[0.0, 4.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0] [1.0, 4.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
[0.0, 5.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0] [1.0, 5.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
[0.0, 6.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0] [0.0, 6.0, 1.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
[0.0, 7.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0] [0.0, 7.0, 1.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
[0.0, 8.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0] [0.0, 8.0, 1.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
[0.0, 9.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0] [0.0, 9.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
[0.0, 10.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0] [0.0, 10.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]