Advertisement
Kitomas

bresenhamAll & wireframe software renderer

May 5th, 2023
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.98 KB | None | 0 0
  1. #define IN_BOUNDS(_x,_y, _w,_h) (_x>=0 && _x<_w  &&  _y>=0 && _y<_h)
  2. void bresenhamAll(KIT_Color* pixels,int w,int h, int x0,int y0, int x1,int y1){
  3.   int dx=abs(x1-x0), dy=-abs(y1-y0);
  4.   int sx,sy, psy;
  5.   if(x0<x1){ sx=1;        } else { sx=-1;         }
  6.   if(y0<y1){ sy=1; psy=w; } else { sy=-1; psy=-w; }
  7.   int err=dx+dy, err2;
  8.   int pos=x0 + y0*w;
  9.   do {
  10.     if(IN_BOUNDS(x0,y0, w,h)) pixels[pos].value=0xffffff00; //cyan
  11.     if(x0==x1 && y0==y1) break;
  12.     err2=err<<1;
  13.     if(err2>=dy){
  14.       if(x0==x1) break;
  15.       err+=dy;
  16.       x0+=sx;
  17.       pos+=sx;
  18.     }
  19.     if(err2<=dx){
  20.       if(y0==y1) break;
  21.       err+=dx;
  22.       y0+=sy;
  23.       pos+=psy;
  24.     }
  25.   } while(1);
  26. }
  27. //draws wireframe triangle
  28. int notAShader2(KIT_TTM_Canvas3D* canvas, KIT_TTM* ttms,int ttms_len, void* data){
  29.   if(!canvas){ return 1; }  if(!ttms){ return 2; } if(!ttms_len){ return 3; }
  30.   float v_w=canvas->v_w, v_h=canvas->v_h; int w=canvas->w, h=canvas->h;
  31.   KIT_TTM_LockCanvas3D(canvas);
  32.   KIT_Color* pixels=canvas->pixels; int w_half=w/2, h_half=h/2;
  33.   for(uint32_t ttm_i=0; ttm_i<ttms_len; ++ttm_i){//for every ttm
  34.   uint32_t objects_len=ttms[ttm_i].objects_len;
  35.   for(uint32_t obj_i=0; obj_i<objects_len; ++obj_i){//for every object
  36.     KIT_TTM_Object* object=&ttms[ttm_i].objects[obj_i];
  37.       uint32_t numWorkTris=object->numWorkTris;
  38.       if(numWorkTris==0) continue;
  39.     KIT_TTM_Triangle* render=object->render;
  40.       memcpy(render,object->work,sizeof(KIT_TTM_Triangle)*numWorkTris);
  41.     for(uint32_t tri_i=0; tri_i<numWorkTris; ++tri_i){//for every triangle
  42.       KIT_TTM_Triangle tri=render[tri_i];
  43.       tri=KIT_TTM_ProjectTriangle(tri, w_half,h_half, v_w,v_h);
  44.       bresenhamAll(pixels,w,h, tri.a.pos.x,tri.a.pos.y, tri.b.pos.x,tri.b.pos.y);
  45.       bresenhamAll(pixels,w,h, tri.b.pos.x,tri.b.pos.y, tri.c.pos.x,tri.c.pos.y);
  46.       bresenhamAll(pixels,w,h, tri.c.pos.x,tri.c.pos.y, tri.a.pos.x,tri.a.pos.y);
  47.     }
  48.   }}
  49.   if(canvas->autoUnlock) KIT_TTM_UnlockCanvas3D(canvas);
  50.   return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement