From 282c24f7f940b3b7e0caf76bd6e20d2993bf42b6 Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 14 Feb 2024 21:34:17 -0300 Subject: donut rodando, falta adicionar sombra --- donut.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ donut.h | 29 ++++++++++++++++++++++ main.c | 31 ++++++++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 donut.c create mode 100644 donut.h create mode 100644 main.c diff --git a/donut.c b/donut.c new file mode 100644 index 0000000..3862056 --- /dev/null +++ b/donut.c @@ -0,0 +1,85 @@ +#include "donut.h" +#include + +//center nao deve ter passado pela funcao map antes +void draw_circle(point2d center, double radius){ + double angle = 0; + static double static_angle = 0; + + while(angle <= 2*PI){ + for(double i=0; i<2*PI; i+=0.01){ + double r = radius; + + //point3d tmpc = {center.x, center.y, 0}; + //tmpc = rotate_x(tmpc, angle); + //center.x = tmpc.x; center.y = tmpc.y; + + point2d aux = {r*cos(i), r*sin(i)}; + aux.x += center.x; aux.y += center.y; + + point3d tmp3 = {aux.x, aux.y, 0}; + tmp3 = rotate_y(tmp3, angle); + tmp3 = rotate_z(tmp3, static_angle); + tmp3 = rotate_x(tmp3, static_angle); + aux.x = tmp3.x; aux.y = tmp3.y; + + //point3d tmp = {aux.x, aux.y, 0}; + //tmp = rotate_y(tmp, angle); + //aux.x = tmp.x; aux.y = tmp.y; + +// point3d tmp2 = {aux.x, aux.y, 0}; +// tmp2 = rotate_x(tmp2, static_angle); +// aux.x = tmp2.x; aux.y = tmp2.y; + + aux = map(aux); + DrawPixel(aux.x, aux.y, GREEN); + } + angle += 0.005; + } + static_angle += 0.05; +} + +point3d rotate_z(point3d p, double a){ + double Rz[3][3] = {{1, 0, 0}, {0, cos(a), -sin(a)}, {0, sin(a), cos(a)}}; + point3d rp; + rp.x = Rz[0][0]*p.x + Rz[0][1]*p.y + Rz[0][2]*p.z; + rp.y = Rz[1][0]*p.x + Rz[1][1]*p.y + Rz[1][2]*p.z; + rp.z = Rz[2][0]*p.x + Rz[2][1]*p.y + Rz[2][2]*p.z; + return rp; +} + +point3d rotate_y(point3d p, double a){ + double Ry[3][3] = {{cos(a), 0, sin(a)}, {0, 1, 0}, {-sin(a), 0, cos(a)}}; + point3d rp; + rp.x = Ry[0][0]*p.x + Ry[0][1]*p.y + Ry[0][2]*p.z; + rp.y = Ry[1][0]*p.x + Ry[1][1]*p.y + Ry[1][2]*p.z; + rp.z = Ry[2][0]*p.x + Ry[2][1]*p.y + Ry[2][2]*p.z; + return rp; +} + +point3d rotate_x(point3d p, double a){ + const double Rx[3][3] = {{cos(a), -sin(a), 0}, {sin(a), cos(a), 0}, {0, 0, 1}}; + point3d rp; + rp.x = Rx[0][0]*p.x + Rx[0][1]*p.y + Rx[0][2]*p.z; + rp.y = Rx[1][0]*p.x + Rx[1][1]*p.y + Rx[1][2]*p.z; + rp.z = Rx[2][0]*p.x + Rx[2][1]*p.y + Rx[2][2]*p.z; + return rp; +} + +point2d map(point2d p){ + point2d r; + r.x = SCREEN_WIDTH/2 * (p.x + 1); + r.y = -1*SCREEN_HEIGHT/2 * (p.y + 1) + SCREEN_HEIGHT; + return r; +} + +point2d demap(point2d r){ + point2d p; + p.x = r.x * 2 / SCREEN_WIDTH - 1; + p.y = -1*(r.y - SCREEN_HEIGHT) * 2 / SCREEN_HEIGHT - 1; + return p; +} + +void debug_printp2(point2d p){ + printf("x: %lf y: %lf\n", p.x, p.y); +} diff --git a/donut.h b/donut.h new file mode 100644 index 0000000..568ae80 --- /dev/null +++ b/donut.h @@ -0,0 +1,29 @@ +#ifndef _DONUT +#define _DONUT + +#include +#include +#include + +#define SCREEN_WIDTH 640 +#define SCREEN_HEIGHT 640 + +typedef struct _Point3d{ + double x, y, z; +}point3d; + +typedef struct _Point2d{ + double x, y; +}point2d; + +void debug_printp2(point2d p); + +point2d map(point2d p); +point2d demap(point2d p); + +void draw_circle(point2d center, double radius); +point3d rotate_x(point3d p, double a); +point3d rotate_y(point3d p, double a); +point3d rotate_z(point3d p, double a); + +#endif \ No newline at end of file diff --git a/main.c b/main.c new file mode 100644 index 0000000..cadbf42 --- /dev/null +++ b/main.c @@ -0,0 +1,31 @@ +#include +#include "donut.h" + +int main(){ + const int screenWidth = SCREEN_WIDTH; + const int screenHeight = SCREEN_HEIGHT; + + InitWindow(screenWidth, screenHeight, "donut"); + SetTargetFPS(60); + + point2d p1 = {0, 0}; + p1 = map(p1); + point2d p2 = (point2d){0.5, 0.5}; + p2 = map(p2); + debug_printp2(p1); + debug_printp2(p2); + while(!WindowShouldClose()){ + + BeginDrawing(); + ClearBackground(BLACK); + DrawText("DONUT", 0, 0, 16, WHITE); + + //draw line 0 0 0.5 0.5 + DrawLine(p1.x, p1.y, p2.x, p2.y, RED); + draw_circle((point2d){0.50, 0.0}, 0.25); + EndDrawing(); + } + + CloseWindow(); + return 0; +} \ No newline at end of file -- cgit v1.2.3