sine wave spin

This commit is contained in:
corey 2024-07-04 12:22:22 -05:00
parent 8b50f2e6a2
commit e39280f5ac
2 changed files with 31 additions and 17 deletions

View File

@ -1,5 +1,5 @@
CFLAGS= -Wfatal-errors -Wall -Wextra $(shell pkgconf --cflags sdl2)
LDFLAGS= -s $(shell pkgconf --libs sdl2)
LDFLAGS= -s -lm $(shell pkgconf --libs sdl2)
all: perspective
perspective: main.o

46
main.c
View File

@ -4,6 +4,7 @@
*******/
#include<stdio.h>
#include<math.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdint.h>
@ -72,7 +73,7 @@ void draw_cube(SDL_Renderer*ren,Cam cam,Point3d r,int w)
{
Point2d p2[8];
Point3d p3[8];
float f=0.01;
float f=0.005;
if(!ren)return;
@ -106,6 +107,9 @@ int main(void)
bool running=true;
const int height=240;
const int width=320;
float xrat=0;
float yrat=0.75;
bool spin=false;
// Init SDL
SDL_Init(SDL_INIT_EVERYTHING);
@ -128,28 +132,38 @@ int main(void)
case SDL_KEYDOWN:
switch(evt.key.keysym.sym)
{
switch(evt.key.keysym.sym)
{
case SDLK_ESCAPE:
case SDLK_q:
running=false;
break;
case SDLK_ESCAPE:
case SDLK_q:
running=false;
break;
case SDLK_LEFT:--cam.x;break;
case SDLK_RIGHT:++cam.x;break;
case SDLK_UP:++cam.y;break;
case SDLK_DOWN:--cam.y;break;
case SDLK_w:cam.z+=0.1;break;
case SDLK_s:cam.z-=0.1;break;
case SDLK_SPACE:spin=!spin;break;
}
case SDLK_LEFT:--cam.x;break;
case SDLK_RIGHT:++cam.x;break;
case SDLK_UP:--cam.y;break;
case SDLK_DOWN:++cam.y;break;
case SDLK_w:cam.z+=0.1;break;
case SDLK_s:cam.z-=0.1;break;
break;
}
break;
}
}
if(spin)
{
cam.x=(0.5+sin(M_PI*2.0*xrat))*15;
cam.y=(0.5+sin(M_PI*2.0*(0.5-yrat)))*15;
xrat=fmod(xrat+0.01,1);
yrat=fmod(yrat+0.01,1);
}
// Render
SDL_SetRenderDrawColor(ren,0,0,0,0);
SDL_RenderClear(ren);
@ -160,7 +174,7 @@ int main(void)
// Draw cube
SDL_SetRenderDrawColor(ren,255,255,255,0);
draw_cube(ren,cam,(Point3d){.x=1,.y=1,.z=1},10);
draw_cube(ren,cam,(Point3d){.x=1,.y=1,.z=1},7);
SDL_RenderPresent(ren);
usleep(20000);