What was the software rasterizer 3D engine called?


mjohansson

Supporter
Joined
Feb 10, 2011
Messages
409
Hey, I cant for the life of me remember what the engine was called and cant find it now searching for 3D engine on forum, but someone did a software rasterizer back in the days and I remember seeing a demo of cogwheels. Well the thing is Im starting to want a software rasterizer myself now for various reasons, I was just wondering if there was something usefull I could steal some code from and not having to do various boring work myself :p I dont remember if the engine Im talking about was open source or what, or if someone wanna help me out in a shared open source project please say so, I did start up something a while ago but didnt come far, the code probably need to be redone from scratch anyway but Ill post the code I did anyway, I had the line drawing and was gonna start filling surfaces. I think the engine was named after a bird.
[doublepost=1493717968,1493717697][/doublepost]
Code:
#include <iostream>
#include "SDL2/SDL.h"
#include "math.h"

#define sgSDL2
#include "sgkb.cpp"

#define sgDefineStandardMaxFPS 100.0f
#define sgDefineStandardDrawDistance 800.0f
int sgQuit=0;
int sgScreenWidth=1400;
int sgScreenHeight=800;
const int sgMinX=700,sgMinY=400;
int sgStereo3D=0;
float sgStereo3D_dTweak=0.03f;
int sgVideoOUT=1;
int sgWindowHUD=1,sgFullScreen=0,sgOldScreenWidth=0,sgOldScreenHeight=0;// DONT MANUALLY MESS WITH THESE!
int sgDesktopRefresh=0,sgDesktopWidth=0,sgDesktopHeight=0,sgVP[4]={0,0,1400,800};// DONT MANUALLY MESS WITH THESE!
float sgMaxFPS=sgDefineStandardMaxFPS;
float sgDrawDistance=sgDefineStandardDrawDistance;
float sgNearClip=0.1f;
float sgFov=90.0f;
float sgAspectRatio=(float)sgScreenWidth/(float)sgScreenHeight;
float sgPglX=2.0f/(float)sgScreenWidth,sgPglY=2.0f/(float)sgScreenHeight;
float sgDYNRES=(float)sgScreenWidth/(float)sgMinX;
float sgMp[16]={0.0f},sgMc[9]={0.0f},sg1=0.0f,sg2=0.0f,sg3=0.0f,sg4=0.0f,sg5=0.0f,sg6=0.0f;
float sgR1x=0.0f,sgR1y=0.0f,sgR1z=0.0f,sgR2x=0.0f,sgR2y=0.0f,sgR2z=0.0f,sgR3x=0.0f,sgR3y=0.0f,sgR3z=0.0f;
Uint32 sgOldTime=0,sgCurrentTime=0,sgNullTime=0;
float sgTimeUnit=0.1f,sgGameTime=1.0f,sgFrameRate=1000.0f/sgDefineStandardMaxFPS,sgTextureTime=0.0f;


const int sgMAXRES=6000;
static Uint8 *sgP[sgMAXRES][sgMAXRES];
static float sgFR[sgMAXRES][sgMAXRES],sgFG[sgMAXRES][sgMAXRES];
static float sgFB[sgMAXRES][sgMAXRES],sgFA[sgMAXRES][sgMAXRES];
SDL_Window *sgWindow;SDL_Surface* s=NULL;SDL_Event sgEvent;


// ADJUST THESE TO INCREASE/DECREASE CAPABILITY OF AN OBJECT.
const int sgNumberOfPolygons=9000;
const int sgNumberOfBones=45;
const int sgNumberOfTextures=8;
const int sgNumberOfTexturesAnimation=15;
const int sgNumberOfAnimations=100;
const int sgNumberOfFrames=10;

// FOR RENDERING. (DONT TOUCH!)
static float sgRv[sgNumberOfPolygons*9*2];// VERTICES.
static float sgRc[sgNumberOfPolygons*12*2];// COLOR.
static float sgRt[sgNumberOfPolygons*6*2];// TEXTURE COORDINATES.
#define sgLINES 0
#define sgTRIANGLES 1


void sgUPDATE_DISPLAY_ADRESSES(){
for(int i=0;i<sgScreenWidth;i++){for(int j=0;j<sgScreenHeight;j++){
sgP[i][j]=(Uint8*)s->pixels+j*s->pitch+i*s->format->BytesPerPixel;}}}

void sgCLEAR(){
  for(int i=0;i<sgScreenWidth;i++){
  for(int j=0;j<sgScreenHeight;j++){
  sgFR[i][j]=0.0f;sgFG[i][j]=0.0f;
  sgFB[i][j]=0.0f;sgFA[i][j]=0.0f;}}}

void sgFLIP(){
  for(int i=0;i<sgScreenWidth;i++){
  for(int j=0;j<sgScreenHeight;j++){
  *(Uint32*)sgP[i][j]=SDL_MapRGB(s->format,
  (float)(sgFR[i][j]*255.0f),
  (float)(sgFG[i][j]*255.0f),
  (float)(sgFB[i][j]*255.0f));}}
  SDL_UpdateWindowSurface(sgWindow);}

void sgINIT(){
SDL_InitSubSystem(SDL_INIT_VIDEO);
SDL_InitSubSystem(SDL_INIT_TIMER);
SDL_InitSubSystem(SDL_INIT_EVENTS);
sgWindow=SDL_CreateWindow(
    "SILKY GAMES 3D ENGINE",
    SDL_WINDOWPOS_CENTERED,
    SDL_WINDOWPOS_CENTERED,
    sgScreenWidth,
    sgScreenHeight,
    SDL_WINDOW_BORDERLESS);
s=SDL_GetWindowSurface(sgWindow);
sgUPDATE_DISPLAY_ADRESSES();}

// CALCULATE PERSPECTIVE MATRIX DATA.
void sgCALCULATE_PERSPECTIVE(float fov,float ar,float zn,float zf,
       float& ett,float& tva, float& tre, float& fyr, float& fem, float& sex){fov/=2.0f;
float xmin=0.0f,xmax=0.0f,ymin=0.0f,ymax=0.0f,left=0.0f,right=0.0f,top=0.0f,bottom=0.0f;
ymax=zn*tanf(fov*0.00872f);ymin=-ymax;xmin=ymin*ar;xmax=ymax*ar;left=xmin;right=xmax;bottom=ymin;
top=ymax;ett=(2.0f*zn)/(right-left);tva=(2.0f*zn)/(top-bottom);tre=(right+left)/(right-left);
fyr=(top+bottom)/(top-bottom);fem=-((zf+zn)/(zf-zn));sex=-((2.0f*zf*zn)/(zf-zn));}

// ROTATION MATRIX.
void sgRM(float rx,float ry,float rz,float& t1x,float& t1y,float& t1z,float& t2x,float& t2y,
          float& t2z,float& t3x,float& t3y,float& t3z){
    float sx=0.0f,cx=0.0f,sy=0.0f,cy=0.0f,sz=0.0f,cz=0.0f,theta=0.0f;
    theta=rx*0.017453f;sx=sinf(theta);cx=cosf(theta);
    theta=ry*0.017453f;sy=sinf(theta);cy=cosf(theta);
    theta=rz*0.017453f;sz=sinf(theta);cz=cosf(theta);
    t1x=cz*cy-sz*sx*sy;t1y=sz*cy+cz*sx*sy;t1z=-cx*sy;t2x=-sz*cx;
    t2y=cz*cx;t2z=sx;t3x=cz*sy+sz*sx*cy;t3y=sz*sy-cz*sx*cy;t3z=cx*cy;}

// xp + yp = POINT OF ROTATION. ux + uy = POINT TO ROTATE (will be updated) WITH "degree".
void sgROTATE(float xp,float yp,float& ux,float& uy,float degree){
 float bt=0.0f,lx=0.0f,ly=0.0f,rx=0.0f,ry=0.0f,nnx=0.0f,a=0.0f;if(degree!=0.0f){
 if(uy<=yp&&ux>=xp){a=atanf((yp-uy)/((ux-xp)))*57.295779f;}
 if(uy<=yp&&xp>ux){a=90.0f+(90.0f-acosf((xp-ux)/(sqrtf(((yp-uy)*(yp-uy))+((xp-ux)*(xp-ux)))))*57.295779f);}
 if(yp<uy&&xp>ux){a=270.0f-(90.0f-(atanf((uy-yp)/((xp-ux)))*57.295779f));}
 if(yp<uy&&ux>=xp){a=360.0f-(acosf((ux-xp)/(sqrtf(((uy-yp)*(uy-yp))+((ux-xp)*(ux-xp)))))*57.295779f);}
 a=(360.0f-a)+degree+45.0f;while(a>360.0f){a-=360.0f;}
 rx=cosf(a*0.017453f)+sinf(a*0.017453f);ry=-sinf(a*0.017453f)+cosf(a*0.017453f);
 nnx=sqrtf(rx*rx+ry*ry);lx=ux-xp;ly=yp-uy;bt=sqrtf(lx*lx+ly*ly);rx/=nnx;ry/=nnx;ux=xp+rx*bt;uy=yp-ry*bt;}}

int oldpx=0,oldpy=0,olda=0;
void sgDRAW(int mode,int offset,int number){
float tx1=0.0f,ty1=0.0f,tx2=0.0f,ty2=0.0f,tx3=0.0f,ty3=0.0f,x=0.0f,y=0.0f;int px=0,py=0,a=0;
float ptx1=0.0f,pty1=0.0f,ptz1=0.0f,ptx2=0.0f,pty2=0.0f,ptz2=0.0f,ptx3=0.0f,pty3=0.0f,ptz3=0.0f;
float cr=0.0f,cg=0.0f,cb=0.0f,ca=0.0f,tz1=0.0f,tz2=0.0f,tz3=0.0f,tw1=0.0f,tw2=0.0f,tw3=0.0f;
float v[9];int yu=0,ym=0,yl=0;
float pty4=0.0f,pty7=0.0f,s1x=0.0f,s1y=0.0f;

if(mode==sgTRIANGLES){for(int i=0;i<number;i++){

ptx1=sgRv[((offset+i)*9)+0];pty1=sgRv[((offset+i)*9)+1];ptz1=sgRv[((offset+i)*9)+2];
tx1=sgMp[0]*ptx1+sgMp[4]*pty1+sgMp[8]*ptz1;
ty1=sgMp[1]*ptx1+sgMp[5]*pty1+sgMp[9]*ptz1;
tz1=sgMp[2]*ptx1+sgMp[6]*pty1+sgMp[10]*ptz1;
tw1=sgMp[14];
ptx1=tx1;pty1=ty1;ptz1=tz1;//if(tw1!=1.0f){ptx1/=tw1;pty1/=tw1;ptz1/=tw1;}
ptx2=sgRv[((offset+i)*9)+3];pty2=sgRv[((offset+i)*9)+4];ptz2=sgRv[((offset+i)*9)+5];
tx1=sgMp[0]*ptx2+sgMp[4]*pty2+sgMp[8]*ptz2;
ty1=sgMp[1]*ptx2+sgMp[5]*pty2+sgMp[9]*ptz2;
tz1=sgMp[2]*ptx2+sgMp[6]*pty2+sgMp[10]*ptz2;
tw2=sgMp[14];
ptx2=tx1;pty2=ty1;ptz2=tz1;//if(tw2!=1.0f){ptx2/=tw2;pty2/=tw2;ptz2/=tw2;}
ptx3=sgRv[((offset+i)*9)+6];pty3=sgRv[((offset+i)*9)+7];ptz3=sgRv[((offset+i)*9)+8];
tx1=sgMp[0]*ptx3+sgMp[4]*pty3+sgMp[8]*ptz3;
ty1=sgMp[1]*ptx3+sgMp[5]*pty3+sgMp[9]*ptz3;
tz1=sgMp[2]*ptx3+sgMp[6]*pty3+sgMp[10]*ptz3;
tw3=sgMp[14];
ptx3=tx1;pty3=ty1;ptz3=tz1;//if(tw3!=1.0f){ptx3/=tw3;pty3/=tw3;ptz3/=tw3;}
if(ptz1>0.0f&&ptz2>0.0f&&ptz3>0.0f){
ptx1/=ptz1;pty1/=ptz1*-1.0f;ptx2/=ptz2;pty2/=ptz2*-1.0f;ptx3/=ptz3;pty3/=ptz3*-1.0f;
v[0]=ptx1;v[1]=pty1;v[2]=ptz1;
v[3]=ptx2;v[4]=pty2;v[5]=ptz2;
v[6]=ptx3;v[7]=pty3;v[8]=ptz3;

/*tz1=sgRv[((offset+i)*9)+1];px=1;
if(sgRv[((offset+i)*9)+4]>tz1){tz1=sgRv[((offset+i)*9)+4];px=4;}
if(sgRv[((offset+i)*9)+7]>tz1){tz1=sgRv[((offset+i)*9)+7];px=7;}
if(px!=1){tz3=sgRv[((offset+i)*9)+1];py=1;}else{tz3=sgRv[((offset+i)*9)+4];py=4;}
if(sgRv[((offset+i)*9)+4]<tz3){tz3=sgRv[((offset+i)*9)+4];py=4;}
if(sgRv[((offset+i)*9)+7]<tz3){tz3=sgRv[((offset+i)*9)+7];py=7;}
if(px==1&&py==4){a=7;}if(px==1&&py==7){a=4;}if(px==4&&py==1){a=7;}
if(px==4&&py==7){a=1;}if(px==7&&py==1){a=4;}if(px==7&&py==4){a=1;}*/

// yu = HIGHEST POINT, ym = MEDIUM POINT, yl = LOWEST POINT.
tz1=pty1;yu=1;if(pty4>tz1){tz1=pty4;yu=4;}if(pty7>tz1){tz1=pty7;yu=7;}
if(yu!=1){tz3=pty1;yl=1;}else{tz3=pty4;yl=4;}
if(pty4<tz3){tz3=pty4;yl=4;}if(pty7<tz3){tz3=pty7;yl=7;}
if(yu==1&&yl==4){ym=7;}if(yu==1&&yl==7){ym=4;}if(yu==4&&yl==1){ym=7;}
if(yu==4&&yl==7){ym=1;}if(yu==7&&yl==1){ym=4;}if(yu==7&&yl==4){ym=1;}

tx1=sgScreenWidth*((v[yu-1]+1.0f)*0.5f);tx2=sgScreenWidth*((v[ym-1]+1.0f)*0.5f);
ty1=sgScreenHeight*((v[yu]+1.0f)*0.5f);ty2=sgScreenHeight*((v[ym]+1.0f)*0.5f);
tx3=tx2-tx1;ty3=ty2-ty1;x=tx3;if(x<0.0f){x*=-1.0f;}y=ty3;if(y<0.0f){y*=-1.0f;}
if(x<=y){a=y;}else{a=x;}if(a!=0){tx3=(float)tx3/a;ty3=(float)ty3/a;

for(int j=0;j<a;j++){px=(float)(tx1+0.5f+(float)(tx3*j));py=(float)(ty1+0.5f+(float)(ty3*j));
tx2=(float)j/a;ty2=ptz1+(ptz2-ptz1)*tx2;if(ty2>0.1f&&ty2<=sgDrawDistance){
ptx1=(float)sgVP[2]/sgScreenWidth;pty1=(float)sgVP[3]/sgScreenHeight;
px=(float)(px*ptx1)+sgVP[0];py=(float)(py*pty1)+sgVP[1];s1x=px;s1y=py;


if(px>=sgVP[0]&&px<sgVP[2]&&py>=sgVP[1]&&py<sgVP[3]){

}}}}



//if(px!=oldpx||a!=olda||py!=oldpy){
//std::cout<<px<<"   "<<a<<"   "<<py<<std::endl;oldpx=px;olda=a;oldpy=py;}


}
}}

if(mode==sgLINES){for(int i=0;i<number;i++){
ptx1=sgRv[((offset+i)*6)+0];pty1=sgRv[((offset+i)*6)+1];ptz1=sgRv[((offset+i)*6)+2];
tx1=sgMp[0]*ptx1+sgMp[4]*pty1+sgMp[8]*ptz1;
ty1=sgMp[1]*ptx1+sgMp[5]*pty1+sgMp[9]*ptz1;
tz1=sgMp[2]*ptx1+sgMp[6]*pty1+sgMp[10]*ptz1+sgMp[11];
ptx1=tx1;pty1=ty1;ptz1=tz1;
ptx2=sgRv[((offset+i)*6)+3];pty2=sgRv[((offset+i)*6)+4];ptz2=sgRv[((offset+i)*6)+5];
tx1=sgMp[0]*ptx2+sgMp[4]*pty2+sgMp[8]*ptz2;
ty1=sgMp[1]*ptx2+sgMp[5]*pty2+sgMp[9]*ptz2;
tz1=sgMp[2]*ptx2+sgMp[6]*pty2+sgMp[10]*ptz2+sgMp[11];
ptx2=tx1;pty2=ty1;ptz2=tz1;
if(ptz1>0.0f&&ptz2>0.0f){
//if(ptz1<0.0f){ptz1*=-1.0f;}if(ptz2<0.0f){ptz2*=-1.0f;}
ptx1/=ptz1;pty1/=ptz1*-1.0f;ptx2/=ptz2;pty2/=ptz2*-1.0f;
tx1=sgScreenWidth*((ptx1+1.0f)/2.0f);tx2=sgScreenWidth*((ptx2+1.0f)/2.0f);
ty1=sgScreenHeight*((pty1+1.0f)/2.0f);ty2=sgScreenHeight*((pty2+1.0f)/2.0f);
tx3=tx2-tx1;ty3=ty2-ty1;x=tx3;if(x<0.0f){x*=-1.0f;}y=ty3;if(y<0.0f){y*=-1.0f;}
if(x<=y){a=y;}else{a=x;}if(a!=0){tx3=(float)tx3/a;ty3=(float)ty3/a;
for(int j=0;j<a;j++){px=(float)(tx1+0.5f+(float)(tx3*j));py=(float)(ty1+0.5f+(float)(ty3*j));
tx2=(float)j/a;ty2=ptz1+(ptz2-ptz1)*tx2;if(ty2>0.1f&&ty2<=sgDrawDistance){
ptx1=(float)sgVP[2]/sgScreenWidth;pty1=(float)sgVP[3]/sgScreenHeight;
px=(float)(px*ptx1)+sgVP[0];py=(float)(py*pty1)+sgVP[1];
if(px>=sgVP[0]&&px<sgVP[2]&&py>=sgVP[1]&&py<sgVP[3]){
cr=sgRc[((offset+i)*8)+0]+(sgRc[((offset+i)*8)+4]-sgRc[((offset+i)*8)+0])*tx2;
cg=sgRc[((offset+i)*8)+1]+(sgRc[((offset+i)*8)+5]-sgRc[((offset+i)*8)+1])*tx2;
cb=sgRc[((offset+i)*8)+2]+(sgRc[((offset+i)*8)+6]-sgRc[((offset+i)*8)+2])*tx2;
ca=sgRc[((offset+i)*8)+3]+(sgRc[((offset+i)*8)+7]-sgRc[((offset+i)*8)+3])*tx2;
//cr=1.0f;cg=1.0f;cb=1.0f;ca=1.0f;
sgFR[px][py]=cr;sgFG[px][py]=cg;sgFB[px][py]=cb;sgFA[px][py]=ca;}}}}}}}
}



void sgSET_PERSPECTIVE(){
  sgCALCULATE_PERSPECTIVE(
    sgFov,sgAspectRatio,sgNearClip,sgDrawDistance,sg1,sg2,sg3,sg4,sg5,sg6);
  sgMp[0]=sg1;sgMp[1]=0.0f;sgMp[2]=sg3;sgMp[3]=0.0f;
  sgMp[4]=0.0f;sgMp[5]=sg2;sgMp[6]=sg4;sgMp[7]=0.0f;
  sgMp[8]=0.0f;sgMp[9]=0.0f;sgMp[10]=sg5;sgMp[11]=sg6;
  sgMp[12]=0.0f;sgMp[13]=0.0f;sgMp[14]=-1.0f;sgMp[15]=1.0f;}

void sgViewport(int x1,int y1,int x2,int y2){
if(x2>x1&&y2>y1&&x2<=sgDesktopWidth&&y2<=sgDesktopHeight&&x1>=0&&y1>=0){
sgVP[0]=x1;sgVP[1]=y1;sgVP[2]=x2;sgVP[3]=y2;}}

// sgXXX FUNCTIONS RETURNS 0.0f ON FAILURE, OR 1.0f ON SUCCESS.

float sgSET_ASPECT_RATIO( float id = -1.0f ){int x=0;
if( id == 0.0f ){sgAspectRatio=(float)sgScreenWidth/sgScreenHeight;x=1;}
if( id > 0.0f ){sgAspectRatio=id;x=1;}
if( id < 0.0f ){return sgAspectRatio;}
if( x == 1 ){sgSET_PERSPECTIVE();return 1.0f;}return 0.0f;}

float sgSET_FOV( float id = -1.0f ){int x=0;
if( id == 0.0f ){sgFov=90.0f;x=1;}
if( id > 0.0f ){sgFov=id;x=1;}
if( id < 0.0f ){return sgFov;}
if( x == 1 ){sgSET_PERSPECTIVE();return 1.0f;}return 0.0f;}

float sgSET_DRAW_DISTANCE( float id = -1.0f ){int x=0;
if( id == 0.0f ){sgDrawDistance=sgDefineStandardDrawDistance;x=1;}
if( id > 0.0f ){sgDrawDistance=id;x=1;}
if( id < 0.0f ){return sgDrawDistance;}
if( x == 1 ){sgSET_PERSPECTIVE();return 1.0f;}return 0.0f;}

float sgSET_MAXFPS( float id = -1.0f ){int x=0;
if( id == 0.0f ){sgMaxFPS=sgDefineStandardMaxFPS;x=1;}
if( id > 0.0f ){sgMaxFPS=id;x=1;}
if( id < 0.0f ){return sgMaxFPS;}
if( x == 1 ){sgFrameRate=1000.0f/sgMaxFPS;return 1.0f;}return 0.0f;}

void sgGET_TIME(Uint32& utime){utime=SDL_GetTicks();}
void sgDELAY_TIME(Uint32 utime){SDL_Delay(utime);}
float sgGET_FPS(){if(sgTimeUnit>0.0f){return 1.0f/sgTimeUnit;}else{return 0.0f;}}

void sgUPDATE_TIME(){float x=0.0f;Uint32 td=0;sgGET_TIME(sgCurrentTime);
 if(sgOldTime>sgCurrentTime){sgCurrentTime=sgOldTime;}x=sgCurrentTime-sgOldTime;
 if(x<sgFrameRate){x=sgFrameRate-x;td=x;sgDELAY_TIME(td);sgCurrentTime+=td;}
 sgTimeUnit=(float)(sgCurrentTime-sgOldTime)/1000.0f;
 if(sgTimeUnit<0.0f){sgTimeUnit*=-1.0f;}sgOldTime=sgCurrentTime;}

void sgCHANGE_RESOLUTION(int x,int y){
if(x>=sgMinX&&x<=sgDesktopWidth&&y>=sgMinY&&y<=sgDesktopHeight){
sgOldScreenWidth=sgScreenWidth;sgOldScreenHeight=sgScreenHeight;
sgScreenWidth=x;sgScreenHeight=y;sgViewport(0,0,sgScreenWidth,sgScreenHeight);
sgAspectRatio=(float)sgScreenWidth/(float)sgScreenHeight;
sgPglX=2.0f/(float)sgScreenWidth;sgPglY=2.0f/(float)sgScreenHeight;
sgDYNRES=(float)sgScreenWidth/(float)sgMinX;sgSET_PERSPECTIVE();}}





void sgMOVE(float& xp,float& yp,float& zp,int iforward,int ibackward,int ileft, int iright,
               int iup,int idown,float angle_h,float angle_v,float speed,bool free_float){
float rx=0.0f,ry=0.0f,rad=0.017453f,offset=135.0f,nnx=0.0f,vf=0.0f;
while(angle_h>=360.0f){angle_h-=360.0f;}while(angle_h<0.0f){angle_h+=360.0f;}
while(angle_v>=360.0f){angle_v-=360.0f;}while(angle_v<0.0f){angle_v+=360.0f;}
if(angle_v>270.0f){vf=1.0f-((360.0f-angle_v)/90.0f);}else{vf=(90.0f-angle_v)/90.0f;}
if(iforward==1){
 rx=cosf((angle_h+offset)*rad)+sinf((angle_h+offset)*rad);
 ry=-sinf((angle_h+offset)*rad)+cosf((angle_h+offset)*rad);
 nnx=sqrtf((rx*rx)+(ry*ry));rx/=nnx;ry/=nnx;rx*=speed;ry*=speed;
 if(free_float==true){xp-=rx*vf;zp+=ry*vf;
  if(angle_v>=270.0f){yp+=speed*(1.0f-vf);}else{yp-=speed*(1.0f-vf);}}
 else{xp-=rx;zp+=ry;}}
if(ibackward==1){
 rx=cosf((angle_h+offset)*rad)+sinf((angle_h+offset)*rad);
 ry=-sinf((angle_h+offset)*rad)+cosf((angle_h+offset)*rad);
 nnx=sqrtf((rx*rx)+(ry*ry));rx/=nnx;ry/=nnx;rx*=speed;ry*=speed;
 if(free_float==true){xp+=rx*vf;zp-=ry*vf;
  if(angle_v>=270.0f){yp-=speed*(1.0f-vf);}else{yp+=speed*(1.0f-vf);}}
 else{xp+=rx;zp-=ry;}}
if(ileft==1){
 rx=cosf((angle_h+offset+90.0f)*rad)+sinf((angle_h+offset+90.0f)*rad);
 ry=-sinf((angle_h+offset+90.0f)*rad)+cosf((angle_h+offset+90.0f)*rad);
 nnx=sqrtf((rx*rx)+(ry*ry));rx/=nnx;ry/=nnx;rx*=speed;ry*=speed;xp+=rx;zp-=ry;}
if(iright==1){
 rx=cosf((angle_h+offset-90.0f)*rad)+sinf((angle_h+offset-90.0f)*rad);
 ry=-sinf((angle_h+offset-90.0f)*rad)+cosf((angle_h+offset-90.0f)*rad);
 nnx=sqrtf((rx*rx)+(ry*ry));rx/=nnx;ry/=nnx;rx*=speed;ry*=speed;xp+=rx;zp-=ry;}
if(free_float==true){if(iup==1&&angle_v>=270.0f){yp+=speed*(1.0f-vf);}else{yp-=speed*(1.0f-vf);}}
else{if(iup==1){yp+=speed;}}
if(free_float==true){if(idown==1&&angle_v>=270.0f){yp-=speed*(1.0f-vf);}else{yp+=speed*(1.0f-vf);}}
else{if(idown==1){yp-=speed;}}}
//sgMOVE(x,y,z,F,B,L,R,U,D,angle_H,angle_V,speed,free_float);

int main(int argc,char *argv[]){

sgINIT();

sgSET_PERSPECTIVE();

float t1x=-0.5f,t1y=-0.5f,t1z=-2.0f;
float t2x=0.5f,t2y=-0.5f,t2z=-2.0f;
float t3x=0.0f,t3y=0.5f,t3z=-2.0f;
float color[4]={0.2f,0.2f,0.6f,1.0f};
float s1x=0.0f,s1y=0.0f,s1z=0.0f;
float s2x=0.0f,s2y=0.0f,s2z=0.0f;
float s3x=0.0f,s3y=0.0f,s3z=0.0f;
float temp=0.0f,tx=0.0f,ty=0.0f,tz=0.0f;

int che=0;

float cposition[3]={0.0f,0.0f,0.0f},cangle[3]={0.0f,0.0f,0.0f};
int mF=0,mB=0,mL=0,mR=0,mU=0,mD=0;bool free_float=false;float speed=0.1f;

while(sgQuit==0){
while(SDL_PollEvent(&sgEvent)){
if(sgEvent.type == SDL_QUIT){sgQuit=1;}
sgUPDATE_KEYBOARD_STATE();}

if(key[k_ESCAPE]==1){sgQuit=1;}



sgCLEAR();

if(key[k_A]==1){sgFR[250][250]=1.0f;
//std::cout<<"1 = "<<s1x<<"   "<<s1y<<"   "<<s1z<<std::endl;
//std::cout<<"2 = "<<s2x<<"   "<<s2y<<"   "<<s2z<<std::endl;
//std::cout<<"3 = "<<s3x<<"   "<<s3y<<"   "<<s3z<<std::endl;
che=1;
}else{che=0;}
if(key[k_UP]==1){mF=1;}else{mF=0;}
if(key[k_DOWN]==1){mB=1;}else{mB=0;}
if(key[k_LEFT]==1){cangle[0]-=1.0;}
if(key[k_RIGHT]==1){cangle[0]+=1.0;}

sgMOVE(cposition[0],cposition[1],cposition[2],mF,mB,mL,mR,mU,mD,
cangle[0],cangle[1],speed,free_float);


s1x=t1x;s1y=t1y;s1z=t1z;
s2x=t2x;s2y=t2y;s2z=t2z;
s3x=t3x;s3y=t3y;s3z=t3z;
//temp+=0.1f;
sgROTATE(0.0f,0.0f,s1x,s1y,temp);
sgROTATE(0.0f,0.0f,s2x,s2y,temp);
sgROTATE(0.0f,0.0f,s3x,s3y,temp);

//sgROTATE(-2.0f,0.0f,s1z,s1y,temp);
//sgROTATE(-2.0f,0.0f,s2z,s2y,temp);
//sgROTATE(-2.0f,0.0f,s3z,s3y,temp);


sgRM(cangle[1],cangle[0],cangle[2],sgR1x,sgR1y,sgR1z,sgR2x,sgR2y,sgR2z,sgR3x,sgR3y,sgR3z);
tx=sgR1x*(s1x-cposition[0])+sgR2x*(s1y-cposition[1])+sgR3x*(s1z-cposition[2]);
ty=sgR1y*(s1x-cposition[0])+sgR2y*(s1y-cposition[1])+sgR3y*(s1z-cposition[2]);
tz=sgR1z*(s1x-cposition[0])+sgR2z*(s1y-cposition[1])+sgR3z*(s1z-cposition[2]);
s1x=tx;s1y=ty;s1z=tz;
tx=sgR1x*(s2x-cposition[0])+sgR2x*(s2y-cposition[1])+sgR3x*(s2z-cposition[2]);
ty=sgR1y*(s2x-cposition[0])+sgR2y*(s2y-cposition[1])+sgR3y*(s2z-cposition[2]);
tz=sgR1z*(s2x-cposition[0])+sgR2z*(s2y-cposition[1])+sgR3z*(s2z-cposition[2]);
s2x=tx;s2y=ty;s2z=tz;
tx=sgR1x*(s3x-cposition[0])+sgR2x*(s3y-cposition[1])+sgR3x*(s3z-cposition[2]);
ty=sgR1y*(s3x-cposition[0])+sgR2y*(s3y-cposition[1])+sgR3y*(s3z-cposition[2]);
tz=sgR1z*(s3x-cposition[0])+sgR2z*(s3y-cposition[1])+sgR3z*(s3z-cposition[2]);
s3x=tx;s3y=ty;s3z=tz;


sgRv[0]=s1x;sgRv[1]=s1y;sgRv[2]=s1z;
sgRv[3]=s2x;sgRv[4]=s2y;sgRv[5]=s2z;
sgRv[6]=s3x;sgRv[7]=s3y;sgRv[8]=s3z;
sgDRAW(sgTRIANGLES,0,1);



sgRv[0]=s1x;sgRv[1]=s1y;sgRv[2]=s1z;
sgRv[3]=s2x;sgRv[4]=s2y;sgRv[5]=s2z;
sgRc[0]=1.0f;sgRc[1]=0.0f;sgRc[2]=0.0f;sgRc[3]=1.0f;
sgRc[4]=0.0f;sgRc[5]=0.0f;sgRc[6]=1.0f;sgRc[7]=1.0f;

sgRv[6]=s1x;sgRv[7]=s1y;sgRv[8]=s1z;
sgRv[9]=s3x;sgRv[10]=s3y;sgRv[11]=s3z;
sgRc[8]=1.0f;sgRc[9]=0.0f;sgRc[10]=0.0f;sgRc[11]=1.0f;
sgRc[12]=0.0f;sgRc[13]=1.0f;sgRc[14]=0.0f;sgRc[15]=1.0f;

sgRv[12]=s3x;sgRv[13]=s3y;sgRv[14]=s3z;
sgRv[15]=s2x;sgRv[16]=s2y;sgRv[17]=s2z;
sgRc[16]=0.0f;sgRc[17]=1.0f;sgRc[18]=0.0f;sgRc[19]=1.0f;
sgRc[20]=0.0f;sgRc[21]=0.0f;sgRc[22]=1.0f;sgRc[23]=1.0f;

sgDRAW(sgLINES,0,3);

sgFLIP();
}

SDL_DestroyWindow(sgWindow);
SDL_Quit();
return 0;
}


this probably has rendering math error, Ill look at it later and see if I can update it all or start new and see if I can produce slightly less messy code.
 
and here is the sgkb.cpp

Code:
int key[256]={0};
#define k_A 0
#define k_B 1
#define k_C 2
#define k_D 3
#define k_E 4
#define k_F 5
#define k_G 6
#define k_H 7
#define k_I 8
#define k_J 9
#define k_K 10
#define k_L 11
#define k_M 12
#define k_N 13
#define k_O 14
#define k_P 15
#define k_Q 16
#define k_R 17
#define k_S 18
#define k_T 19
#define k_U 20
#define k_V 21
#define k_W 22
#define k_X 23
#define k_Y 24
#define k_Z 25
#define k_1 26
#define k_2 27
#define k_3 28
#define k_4 29
#define k_5 30
#define k_6 31
#define k_7 32
#define k_8 33
#define k_9 34
#define k_0 35
#define k_UP 36
#define k_DOWN 37
#define k_LEFT 38
#define k_RIGHT 39
#define k_ESCAPE 40
#define k_SPACE 41
#define k_BACKSPACE 42
#define k_RETURN 43
#define k_PLUS 44
#define k_MINUS 45
#define k_LSHIFT 46
#define k_RSHIFT 47
#define k_LCTRL 48
#define k_RCTRL 49
#define k_PAGEUP 50
#define k_PAGEDOWN 51
#define k_HOME 52
#define k_END 53
#define k_COMMA 54
#define k_DOT 55
#define k_LESS 56
#define k_ALT 57
#define k_ALT_GR 58
#define k_DELETE 59
#define k_INSERT 60
#define k_PAUSE 61
#define k_TAB 62
#define k_CAPSLOCK 63
#define k_STAR 64

void sgUPDATE_KEYBOARD_STATE(){const Uint8* currentKeyStates;
#ifdef sgSDL1
currentKeyStates = SDL_GetKeyState( NULL );
if( currentKeyStates[SDLK_a] ){key[k_A]=1;}else{key[k_A]=0;}
if( currentKeyStates[SDLK_b] ){key[k_B]=1;}else{key[k_B]=0;}
if( currentKeyStates[SDLK_c] ){key[k_C]=1;}else{key[k_C]=0;}
if( currentKeyStates[SDLK_d] ){key[k_D]=1;}else{key[k_D]=0;}
if( currentKeyStates[SDLK_e] ){key[k_E]=1;}else{key[k_E]=0;}
if( currentKeyStates[SDLK_f] ){key[k_F]=1;}else{key[k_F]=0;}
if( currentKeyStates[SDLK_g] ){key[k_G]=1;}else{key[k_G]=0;}
if( currentKeyStates[SDLK_h] ){key[k_H]=1;}else{key[k_H]=0;}
if( currentKeyStates[SDLK_i] ){key[k_I]=1;}else{key[k_I]=0;}
if( currentKeyStates[SDLK_j] ){key[k_J]=1;}else{key[k_J]=0;}
if( currentKeyStates[SDLK_k] ){key[k_K]=1;}else{key[k_K]=0;}
if( currentKeyStates[SDLK_l] ){key[k_L]=1;}else{key[k_L]=0;}
if( currentKeyStates[SDLK_m] ){key[k_M]=1;}else{key[k_M]=0;}
if( currentKeyStates[SDLK_n] ){key[k_N]=1;}else{key[k_N]=0;}
if( currentKeyStates[SDLK_o] ){key[k_O]=1;}else{key[k_O]=0;}
if( currentKeyStates[SDLK_p] ){key[k_P]=1;}else{key[k_P]=0;}
if( currentKeyStates[SDLK_q] ){key[k_Q]=1;}else{key[k_Q]=0;}
if( currentKeyStates[SDLK_r] ){key[k_R]=1;}else{key[k_R]=0;}
if( currentKeyStates[SDLK_s] ){key[k_S]=1;}else{key[k_S]=0;}
if( currentKeyStates[SDLK_t] ){key[k_T]=1;}else{key[k_T]=0;}
if( currentKeyStates[SDLK_u] ){key[k_U]=1;}else{key[k_U]=0;}
if( currentKeyStates[SDLK_v] ){key[k_V]=1;}else{key[k_V]=0;}
if( currentKeyStates[SDLK_w] ){key[k_W]=1;}else{key[k_W]=0;}
if( currentKeyStates[SDLK_x] ){key[k_X]=1;}else{key[k_X]=0;}
if( currentKeyStates[SDLK_y] ){key[k_Y]=1;}else{key[k_Y]=0;}
if( currentKeyStates[SDLK_z] ){key[k_Z]=1;}else{key[k_Z]=0;}
if( currentKeyStates[SDLK_1] ){key[k_1]=1;}else{key[k_1]=0;}
if( currentKeyStates[SDLK_2] ){key[k_2]=1;}else{key[k_2]=0;}
if( currentKeyStates[SDLK_3] ){key[k_3]=1;}else{key[k_3]=0;}
if( currentKeyStates[SDLK_4] ){key[k_4]=1;}else{key[k_4]=0;}
if( currentKeyStates[SDLK_5] ){key[k_5]=1;}else{key[k_5]=0;}
if( currentKeyStates[SDLK_6] ){key[k_6]=1;}else{key[k_6]=0;}
if( currentKeyStates[SDLK_7] ){key[k_7]=1;}else{key[k_7]=0;}
if( currentKeyStates[SDLK_8] ){key[k_8]=1;}else{key[k_8]=0;}
if( currentKeyStates[SDLK_9] ){key[k_9]=1;}else{key[k_9]=0;}
if( currentKeyStates[SDLK_0] ){key[k_0]=1;}else{key[k_0]=0;}
if( currentKeyStates[SDLK_UP] ){key[k_UP]=1;}else{key[k_UP]=0;}
if( currentKeyStates[SDLK_DOWN] ){key[k_DOWN]=1;}else{key[k_DOWN]=0;}
if( currentKeyStates[SDLK_LEFT] ){key[k_LEFT]=1;}else{key[k_LEFT]=0;}
if( currentKeyStates[SDLK_RIGHT] ){key[k_RIGHT]=1;}else{key[k_RIGHT]=0;}
if( currentKeyStates[SDLK_ESCAPE] ){key[k_ESCAPE]=1;}else{key[k_ESCAPE]=0;}
if( currentKeyStates[SDLK_SPACE] ){key[k_SPACE]=1;}else{key[k_SPACE]=0;}
if( currentKeyStates[SDLK_BACKSPACE] ){key[k_BACKSPACE]=1;}else{key[k_BACKSPACE]=0;}
if( currentKeyStates[SDLK_RETURN] ){key[k_RETURN]=1;}else{key[k_RETURN]=0;}
if( currentKeyStates[SDLK_PLUS] ){key[k_PLUS]=1;}else{key[k_PLUS]=0;}
if( currentKeyStates[SDLK_MINUS] ){key[k_MINUS]=1;}else{key[k_MINUS]=0;}
if( currentKeyStates[SDLK_LSHIFT] ){key[k_LSHIFT]=1;}else{key[k_LSHIFT]=0;}
if( currentKeyStates[SDLK_RSHIFT] ){key[k_RSHIFT]=1;}else{key[k_RSHIFT]=0;}
if( currentKeyStates[SDLK_LCTRL] ){key[k_LCTRL]=1;}else{key[k_LCTRL]=0;}
if( currentKeyStates[SDLK_RCTRL] ){key[k_RCTRL]=1;}else{key[k_RCTRL]=0;}
if( currentKeyStates[SDLK_PAGEUP] ){key[k_PAGEUP]=1;}else{key[k_PAGEUP]=0;}
if( currentKeyStates[SDLK_PAGEDOWN] ){key[k_PAGEDOWN]=1;}else{key[k_PAGEDOWN]=0;}
if( currentKeyStates[SDLK_HOME] ){key[k_HOME]=1;}else{key[k_HOME]=0;}
if( currentKeyStates[SDLK_END] ){key[k_END]=1;}else{key[k_END]=0;}
if( currentKeyStates[SDLK_COMMA] ){key[k_COMMA]=1;}else{key[k_COMMA]=0;}
if( currentKeyStates[SDLK_PERIOD] ){key[k_DOT]=1;}else{key[k_DOT]=0;}
if( currentKeyStates[SDLK_LESS] ){key[k_LESS]=1;}else{key[k_LESS]=0;}
if( currentKeyStates[SDLK_LALT] ){key[k_ALT]=1;}else{key[k_ALT]=0;}
if( currentKeyStates[SDLK_RALT] ){key[k_ALT_GR]=1;}else{key[k_ALT_GR]=0;}
if( currentKeyStates[SDLK_DELETE] ){key[k_DELETE]=1;}else{key[k_DELETE]=0;}
if( currentKeyStates[SDLK_INSERT] ){key[k_INSERT]=1;}else{key[k_INSERT]=0;}
if( currentKeyStates[SDLK_BREAK] ){key[k_PAUSE]=1;}else{key[k_PAUSE]=0;}
if( currentKeyStates[SDLK_TAB] ){key[k_TAB]=1;}else{key[k_TAB]=0;}
if( currentKeyStates[SDLK_CAPSLOCK] ){key[k_CAPSLOCK]=1;}else{key[k_CAPSLOCK]=0;}
if( currentKeyStates[SDLK_QUOTE] ){key[k_STAR]=1;}else{key[k_STAR]=0;}
#endif
#ifdef sgSDL2
currentKeyStates = SDL_GetKeyboardState( NULL );
if( currentKeyStates[SDL_SCANCODE_A] ){key[k_A]=1;}else{key[k_A]=0;}
if( currentKeyStates[SDL_SCANCODE_B] ){key[k_B]=1;}else{key[k_B]=0;}
if( currentKeyStates[SDL_SCANCODE_C] ){key[k_C]=1;}else{key[k_C]=0;}
if( currentKeyStates[SDL_SCANCODE_D] ){key[k_D]=1;}else{key[k_D]=0;}
if( currentKeyStates[SDL_SCANCODE_E] ){key[k_E]=1;}else{key[k_E]=0;}
if( currentKeyStates[SDL_SCANCODE_F] ){key[k_F]=1;}else{key[k_F]=0;}
if( currentKeyStates[SDL_SCANCODE_G] ){key[k_G]=1;}else{key[k_G]=0;}
if( currentKeyStates[SDL_SCANCODE_H] ){key[k_H]=1;}else{key[k_H]=0;}
if( currentKeyStates[SDL_SCANCODE_I] ){key[k_I]=1;}else{key[k_I]=0;}
if( currentKeyStates[SDL_SCANCODE_J] ){key[k_J]=1;}else{key[k_J]=0;}
if( currentKeyStates[SDL_SCANCODE_K] ){key[k_K]=1;}else{key[k_K]=0;}
if( currentKeyStates[SDL_SCANCODE_L] ){key[k_L]=1;}else{key[k_L]=0;}
if( currentKeyStates[SDL_SCANCODE_M] ){key[k_M]=1;}else{key[k_M]=0;}
if( currentKeyStates[SDL_SCANCODE_N] ){key[k_N]=1;}else{key[k_N]=0;}
if( currentKeyStates[SDL_SCANCODE_O] ){key[k_O]=1;}else{key[k_O]=0;}
if( currentKeyStates[SDL_SCANCODE_P] ){key[k_P]=1;}else{key[k_P]=0;}
if( currentKeyStates[SDL_SCANCODE_Q] ){key[k_Q]=1;}else{key[k_Q]=0;}
if( currentKeyStates[SDL_SCANCODE_R] ){key[k_R]=1;}else{key[k_R]=0;}
if( currentKeyStates[SDL_SCANCODE_S] ){key[k_S]=1;}else{key[k_S]=0;}
if( currentKeyStates[SDL_SCANCODE_T] ){key[k_T]=1;}else{key[k_T]=0;}
if( currentKeyStates[SDL_SCANCODE_U] ){key[k_U]=1;}else{key[k_U]=0;}
if( currentKeyStates[SDL_SCANCODE_V] ){key[k_V]=1;}else{key[k_V]=0;}
if( currentKeyStates[SDL_SCANCODE_W] ){key[k_W]=1;}else{key[k_W]=0;}
if( currentKeyStates[SDL_SCANCODE_X] ){key[k_X]=1;}else{key[k_X]=0;}
if( currentKeyStates[SDL_SCANCODE_Y] ){key[k_Y]=1;}else{key[k_Y]=0;}
if( currentKeyStates[SDL_SCANCODE_Z] ){key[k_Z]=1;}else{key[k_Z]=0;}
if( currentKeyStates[SDL_SCANCODE_1] ){key[k_1]=1;}else{key[k_1]=0;}
if( currentKeyStates[SDL_SCANCODE_2] ){key[k_2]=1;}else{key[k_2]=0;}
if( currentKeyStates[SDL_SCANCODE_3] ){key[k_3]=1;}else{key[k_3]=0;}
if( currentKeyStates[SDL_SCANCODE_4] ){key[k_4]=1;}else{key[k_4]=0;}
if( currentKeyStates[SDL_SCANCODE_5] ){key[k_5]=1;}else{key[k_5]=0;}
if( currentKeyStates[SDL_SCANCODE_6] ){key[k_6]=1;}else{key[k_6]=0;}
if( currentKeyStates[SDL_SCANCODE_7] ){key[k_7]=1;}else{key[k_7]=0;}
if( currentKeyStates[SDL_SCANCODE_8] ){key[k_8]=1;}else{key[k_8]=0;}
if( currentKeyStates[SDL_SCANCODE_9] ){key[k_9]=1;}else{key[k_9]=0;}
if( currentKeyStates[SDL_SCANCODE_0] ){key[k_0]=1;}else{key[k_0]=0;}
if( currentKeyStates[SDL_SCANCODE_UP] ){key[k_UP]=1;}else{key[k_UP]=0;}
if( currentKeyStates[SDL_SCANCODE_DOWN] ){key[k_DOWN]=1;}else{key[k_DOWN]=0;}
if( currentKeyStates[SDL_SCANCODE_LEFT] ){key[k_LEFT]=1;}else{key[k_LEFT]=0;}
if( currentKeyStates[SDL_SCANCODE_RIGHT] ){key[k_RIGHT]=1;}else{key[k_RIGHT]=0;}
if( currentKeyStates[SDL_SCANCODE_ESCAPE] ){key[k_ESCAPE]=1;}else{key[k_ESCAPE]=0;}
if( currentKeyStates[SDL_SCANCODE_SPACE] ){key[k_SPACE]=1;}else{key[k_SPACE]=0;}
if( currentKeyStates[SDL_SCANCODE_BACKSPACE] ){key[k_BACKSPACE]=1;}else{key[k_BACKSPACE]=0;}
if( currentKeyStates[SDL_SCANCODE_RETURN] ){key[k_RETURN]=1;}else{key[k_RETURN]=0;}
//if( currentKeyStates[SDL_SCANCODE_PLUS] ){key[k_PLUS]=1;}else{key[k_PLUS]=0;}
if( currentKeyStates[SDL_SCANCODE_MINUS] ){key[k_MINUS]=1;}else{key[k_MINUS]=0;}
if( currentKeyStates[SDL_SCANCODE_LSHIFT] ){key[k_LSHIFT]=1;}else{key[k_LSHIFT]=0;}
if( currentKeyStates[SDL_SCANCODE_RSHIFT] ){key[k_RSHIFT]=1;}else{key[k_RSHIFT]=0;}
if( currentKeyStates[SDL_SCANCODE_LCTRL] ){key[k_LCTRL]=1;}else{key[k_LCTRL]=0;}
if( currentKeyStates[SDL_SCANCODE_RCTRL] ){key[k_RCTRL]=1;}else{key[k_RCTRL]=0;}
if( currentKeyStates[SDL_SCANCODE_PAGEUP] ){key[k_PAGEUP]=1;}else{key[k_PAGEUP]=0;}
if( currentKeyStates[SDL_SCANCODE_PAGEDOWN] ){key[k_PAGEDOWN]=1;}else{key[k_PAGEDOWN]=0;}
if( currentKeyStates[SDL_SCANCODE_HOME] ){key[k_HOME]=1;}else{key[k_HOME]=0;}
if( currentKeyStates[SDL_SCANCODE_END] ){key[k_END]=1;}else{key[k_END]=0;}
if( currentKeyStates[SDL_SCANCODE_COMMA] ){key[k_COMMA]=1;}else{key[k_COMMA]=0;}
if( currentKeyStates[SDL_SCANCODE_PERIOD] ){key[k_DOT]=1;}else{key[k_DOT]=0;}
//if( currentKeyStates[SDL_SCANCODE_LESS] ){key[k_LESS]=1;}else{key[k_LESS]=0;}
if( currentKeyStates[SDL_SCANCODE_LALT] ){key[k_ALT]=1;}else{key[k_ALT]=0;}
if( currentKeyStates[SDL_SCANCODE_RALT] ){key[k_ALT_GR]=1;}else{key[k_ALT_GR]=0;}
if( currentKeyStates[SDL_SCANCODE_DELETE] ){key[k_DELETE]=1;}else{key[k_DELETE]=0;}
if( currentKeyStates[SDL_SCANCODE_INSERT] ){key[k_INSERT]=1;}else{key[k_INSERT]=0;}
//if( currentKeyStates[SDL_SCANCODE_BREAK] ){key[k_PAUSE]=1;}else{key[k_PAUSE]=0;}
if( currentKeyStates[SDL_SCANCODE_TAB] ){key[k_TAB]=1;}else{key[k_TAB]=0;}
if( currentKeyStates[SDL_SCANCODE_CAPSLOCK] ){key[k_CAPSLOCK]=1;}else{key[k_CAPSLOCK]=0;}
//if( currentKeyStates[SDL_SCANCODE_QUOTE] ){key[k_STAR]=1;}else{key[k_STAR]=0;}
#endif
}
 
Yes thank you!
[doublepost=1493720221,1493719257][/doublepost]Ok I have no idea what Im looking at, the Sparrow3D seems like a hughmongous project! And advanced...
This inspires me to try to make a simple rasterizer more easily handled by less experienced coders...
Ill start from scratch and comment everything as well as I can, open source and no licenses. I should make a new trhead for it when I have the start up and running, Ill call it SIMPLISTIC RASTERIZER.
 
Yes thank you!
[doublepost=1493720221,1493719257][/doublepost]Ok I have no idea what Im looking at, the Sparrow3D seems like a hughmongous project! And advanced...
This inspires me to try to make a simple rasterizer more easily handled by less experienced coders...
Ill start from scratch and comment everything as well as I can, open source and no licenses. I should make a new trhead for it when I have the start up and running, Ill call it SIMPLISTIC RASTERIZER.
Well, sparrow3D is very simple. XD
But for the sake of completeness: sparrow3D is LGPL2+. Feel free to do whatever you want with the code as long as you satisfy the license (which probably means you need to link against sparrow3d or if you really want to copy code you need to release your code as (L)GPL, too).
 
ok I made the triangle fill routine now after all, it was the thing I didnt want to do cos it had some surprisingly complex logic...
Im not all to happy with the code, its not the biggest mess Ive made, but it seem unecesary slow and the barycentric calculation dosnt seem right to me but I cant work out the math myself...
Heres a early demo, get the sgkb.cpp from above post. This code is hardcoded for 1000x1000 resolution and is 2d only, it dosnt have the perspective math and texturing, I might make a new demo later with a more ready full renderer, but its really the triangle fill routine thats of interest here so thats what I wanted to post in case anyone needs it for something then they might find it here somehow and not have to do that boring frustrating work yet again... Oh and if your gonna change something then beware so you dont try to render off screen with to large triangles or you get crash, but if anyone turns this into a full renderer then display check is just one of several things needed.

Code:
#include <iostream>
#define sgSDL2
#include "SDL2/SDL.h"

SDL_Window *srWindow;
SDL_Surface* srSurface;
SDL_Event srEvent;

#include "sgkb.cpp"
#include "math.h"
int srScreenWidth=1000;
int srScreenHeight=1000;

unsigned int sgOldTime=0,sgCurrentTime=0,sgNullTime=0;
float sgMaxFPS=100.0f,sgTimeUnit=0.1f,sgFrameRate=1000.0f/sgMaxFPS;

static unsigned char srColorBuffer[8096][8096][4];
static Uint8 *srP[2048][2048];


void srUPDATE_DISPLAY_ADRESSES(){
for(int i=0;i<srScreenWidth;i++){for(int j=0;j<srScreenHeight;j++){
srP[i][j]=(Uint8*)
srSurface->pixels+j*srSurface->pitch+i*srSurface->format->BytesPerPixel;}}}

void srCLEAR(){
  for(int i=0;i<srScreenWidth;i++){
  for(int j=0;j<srScreenHeight;j++){
  srColorBuffer[i][j][0]=0;
  srColorBuffer[i][j][1]=0;
  srColorBuffer[i][j][2]=0;}}}

void srFLIP(){int x=0,y=0;
  for(int i=0;i<srScreenWidth;i++){x=i*4;
  for(int j=0;j<srScreenHeight;j++){y=j*srScreenWidth*4;
  *(Uint32*)srP[i][j]=SDL_MapRGB(srSurface->format,
  srColorBuffer[i][j][0],
  srColorBuffer[i][j][1],
  srColorBuffer[i][j][2]);}}
  SDL_UpdateWindowSurface(srWindow);}

void srINIT(){
#ifdef sgSDL2
SDL_InitSubSystem(SDL_INIT_VIDEO);
SDL_InitSubSystem(SDL_INIT_TIMER);
SDL_InitSubSystem(SDL_INIT_EVENTS);
//SDL_Init(SDL_INIT_EVERYTHING);
srWindow=SDL_CreateWindow(
    "SIMPLISTIC RASTERIZER 3D ENGINE",
    SDL_WINDOWPOS_CENTERED,
    SDL_WINDOWPOS_CENTERED,
    srScreenWidth,
    srScreenHeight,
    SDL_WINDOW_BORDERLESS);
srSurface=SDL_GetWindowSurface(srWindow);
#endif
srUPDATE_DISPLAY_ADRESSES();
}

void sgGET_TIME(unsigned int& utime){utime=SDL_GetTicks();}
void sgDELAY_TIME(unsigned int utime){SDL_Delay(utime);}
float sgGET_FPS(){return 1.0f/sgTimeUnit;}

void sgUPDATE_TIME(){float x=0.0f,x2=0.0f;unsigned int td=0;sgGET_TIME(sgCurrentTime);
 if(sgOldTime>sgCurrentTime){sgCurrentTime=sgOldTime;}x=sgCurrentTime-sgOldTime;
 td=0;if(x<sgFrameRate){x2=sgFrameRate-x;td=x2;sgDELAY_TIME(td);sgCurrentTime+=td;}
 if(sgOldTime>sgCurrentTime){sgOldTime=sgCurrentTime;sgCurrentTime+=(int)x+td;}
 sgTimeUnit=((float)sgCurrentTime-(float)sgOldTime)/1000.0f;sgOldTime=sgCurrentTime;}

// xp + yp = POINT OF ROTATION. ux + uy = POINT TO ROTATE (will be updated) WITH "degree".
void sgROTATE(float xp,float yp,float& ux,float& uy,float degree){
 float bt=0.0f,lx=0.0f,ly=0.0f,rx=0.0f,ry=0.0f,a=0.0f;if(degree!=0.0f){
 if(xp!=ux||yp!=uy){if(uy<=yp&&ux>=xp){a=atanf((yp-uy)/((ux-xp)))*57.295779f;}
 if(uy<=yp&&xp>ux){a=90.0f+(90.0f-acosf((xp-ux)/(sqrtf(((yp-uy)*(yp-uy))+((xp-ux)*(xp-ux)))))*57.295779f);}
 if(yp<uy&&xp>ux){a=270.0f-(90.0f-(atanf((uy-yp)/((xp-ux)))*57.295779f));}
 if(yp<uy&&ux>=xp){a=360.0f-(acosf((ux-xp)/(sqrtf(((uy-yp)*(uy-yp))+((ux-xp)*(ux-xp)))))*57.295779f);}
 a=(360.0f-a)+degree+45.0f;while(a>=360.0f){a-=360.0f;}rx=cosf(a*0.017453f)+sinf(a*0.017453f);
 ry=-sinf(a*0.017453f)+cosf(a*0.017453f);bt=sqrtf(rx*rx+ry*ry);rx/=bt;ry/=bt;
 lx=ux-xp;ly=yp-uy;bt=sqrtf(lx*lx+ly*ly);ux=xp+rx*bt;uy=yp-ry*bt;}}}

// BIAS VALUE FOR VERTICE 1,2,3 XY. sgGET_BARYCENTRIC_BIAS(ax,ay,bx,by,cx,cy,x,y,ba,bb,bc);
void sgGET_BARYCENTRIC_BIAS(
  float ax,float ay,float bx,float by,float cx,float cy,
  float px,float py,float& ba,float& bb,float& bc){
float v0x=0.0f,v0y=0.0f,v1x=0.0f,v1y=0.0f,v2x=0.0f,v2y=0.0f,d=0.0f;
v0x=bx-ax;v0y=by-ay;v1x=cx-ax;v1y=cy-ay;v2x=px-ax;v2y=py-ay;d=v0x*v1y-v1x*v0y;
//if(d!=0.0f){bb=(v2x*v1y-v1x*v2y)/d;bc=(v0x*v2y-v2x*v0y)/d;ba=1.0f-bb-bc;}
//else{ba=0.333333f;bb=0.333333f;bc=0.333333f;}}
bb=(v2x*v1y-v1x*v2y)/d;bc=(v0x*v2y-v2x*v0y)/d;ba=1.0f-bb-bc;}

int checker1,checker2,checker3;


void sgDRAWLINE(int ox,int oy,int ex,int ey,float c1[4],float c2[4]){
float r1n,r2n,g1n,g2n,b1n,b2n,a1n,a2n,r,g,b,a,tc;
int x=0,y=0,z=0,q=0,w=0;unsigned char cr,cg,cb,ca;
 tc=c1[0];if(tc<0.0f){tc=0.0f;}if(tc>1.0f){tc=1.0f;}r1n=tc*255.0f;
 tc=c2[0];if(tc<0.0f){tc=0.0f;}if(tc>1.0f){tc=1.0f;}r2n=tc*255.0f;
 tc=c1[1];if(tc<0.0f){tc=0.0f;}if(tc>1.0f){tc=1.0f;}g1n=tc*255.0f;
 tc=c2[1];if(tc<0.0f){tc=0.0f;}if(tc>1.0f){tc=1.0f;}g2n=tc*255.0f;
 tc=c1[2];if(tc<0.0f){tc=0.0f;}if(tc>1.0f){tc=1.0f;}b1n=tc*255.0f;
 tc=c2[2];if(tc<0.0f){tc=0.0f;}if(tc>1.0f){tc=1.0f;}b2n=tc*255.0f;
 tc=c1[3];if(tc<0.0f){tc=0.0f;}if(tc>1.0f){tc=1.0f;}a1n=tc*255.0f;
 tc=c2[3];if(tc<0.0f){tc=0.0f;}if(tc>1.0f){tc=1.0f;}a2n=tc*255.0f;
if(ex>ox){
 if(ey>=oy&&ey-oy<=ex-ox){//::::::::::: VAR 1
  x=ex-ox;z=(2*(ey-oy))-x;y=0;
  r=(r2n-r1n)/(float)x;g=(g2n-g1n)/(float)x;
  b=(b2n-b1n)/(float)x;a=(a2n-a1n)/(float)x;
  for(int i=0;i<x;i++){q=ox+i;w=oy+y;
  cr=r*(float)i;cg=g*(float)i;
  cb=b*(float)i;ca=a*(float)i;
  srColorBuffer[q][w][0]=r1n+cr;
  srColorBuffer[q][w][1]=g1n+cg;
  srColorBuffer[q][w][2]=b1n+cb;
  srColorBuffer[q][w][3]=a1n+ca;
  if(z>0){y+=1;z-=2*x;}z+=2*(ey-oy);}}
 if(ey>=oy&&ey-oy>ex-ox){//:::::::::::: VAR 2
  x=ey-oy;z=(2*(ex-ox))-x;y=0;
  r=(r2n-r1n)/(float)x;g=(g2n-g1n)/(float)x;
  b=(b2n-b1n)/(float)x;a=(a2n-a1n)/(float)x;
  for(int i=0;i<x;i++){q=ox+y;w=oy+i;
  cr=r*(float)i;cg=g*(float)i;
  cb=b*(float)i;ca=a*(float)i;
  srColorBuffer[q][w][0]=r1n+cr;
  srColorBuffer[q][w][1]=g1n+cg;
  srColorBuffer[q][w][2]=b1n+cb;
  srColorBuffer[q][w][3]=a1n+ca;
  if(z>0){y+=1;z-=2*x;}z+=2*(ex-ox);}}
 if(ey<oy&&oy-ey<=ex-ox){//::::::::::: VAR 3
  x=ex-ox;z=(2*(oy-ey))-x;y=0;
  r=(r2n-r1n)/(float)x;g=(g2n-g1n)/(float)x;
  b=(b2n-b1n)/(float)x;a=(a2n-a1n)/(float)x;
  for(int i=0;i<x;i++){q=ox+i;w=oy-y;
  cr=r*(float)i;cg=g*(float)i;
  cb=b*(float)i;ca=a*(float)i;
  srColorBuffer[q][w][0]=r1n+cr;
  srColorBuffer[q][w][1]=g1n+cg;
  srColorBuffer[q][w][2]=b1n+cb;
  srColorBuffer[q][w][3]=a1n+ca;
  if(z>0){y+=1;z-=2*x;}z+=2*(oy-ey);}}
 if(ey<oy&&oy-ey>ex-ox){//:::::::::::: VAR 4
  x=oy-ey;z=(2*(ex-ox))-x;y=0;
  r=(r2n-r1n)/(float)x;g=(g2n-g1n)/(float)x;
  b=(b2n-b1n)/(float)x;a=(a2n-a1n)/(float)x;
  for(int i=0;i<x;i++){q=ox+y;w=oy-i;
  cr=r*(float)i;cg=g*(float)i;
  cb=b*(float)i;ca=a*(float)i;
  srColorBuffer[q][w][0]=r1n+cr;
  srColorBuffer[q][w][1]=g1n+cg;
  srColorBuffer[q][w][2]=b1n+cb;
  srColorBuffer[q][w][3]=a1n+ca;
  if(z>0){y+=1;z-=2*x;}z+=2*(ex-ox);}}
}else{
 if(ey>=oy&&ey-oy<=ox-ex){//::::::::::: VAR 5
  x=ox-ex;z=(2*(ey-oy))-x;y=0;
  r=(r2n-r1n)/(float)x;g=(g2n-g1n)/(float)x;
  b=(b2n-b1n)/(float)x;a=(a2n-a1n)/(float)x;
  for(int i=0;i<x;i++){q=ox-i;w=oy+y;
  cr=r*(float)i;cg=g*(float)i;
  cb=b*(float)i;ca=a*(float)i;
  srColorBuffer[q][w][0]=r1n+cr;
  srColorBuffer[q][w][1]=g1n+cg;
  srColorBuffer[q][w][2]=b1n+cb;
  srColorBuffer[q][w][3]=a1n+ca;
  if(z>0){y+=1;z-=2*x;}z+=2*(ey-oy);}}
 if(ey>=oy&&ey-oy>ox-ex){//:::::::::::: VAR 6
  x=ey-oy;z=(2*(ox-ex))-x;y=0;
  r=(r2n-r1n)/(float)x;g=(g2n-g1n)/(float)x;
  b=(b2n-b1n)/(float)x;a=(a2n-a1n)/(float)x;
  for(int i=0;i<x;i++){q=ox-y;w=oy+i;
  cr=r*(float)i;cg=g*(float)i;
  cb=b*(float)i;ca=a*(float)i;
  srColorBuffer[q][w][0]=r1n+cr;
  srColorBuffer[q][w][1]=g1n+cg;
  srColorBuffer[q][w][2]=b1n+cb;
  srColorBuffer[q][w][3]=a1n+ca;
  if(z>0){y+=1;z-=2*x;}z+=2*(ox-ex);}}
 if(ey<oy&&oy-ey<=ox-ex){//::::::::::: VAR 7
  x=ox-ex;z=(2*(oy-ey))-x;y=0;
  r=(r2n-r1n)/(float)x;g=(g2n-g1n)/(float)x;
  b=(b2n-b1n)/(float)x;a=(a2n-a1n)/(float)x;
  for(int i=0;i<x;i++){q=ox-i;w=oy-y;
  cr=r*(float)i;cg=g*(float)i;
  cb=b*(float)i;ca=a*(float)i;
  srColorBuffer[q][w][0]=r1n+cr;
  srColorBuffer[q][w][1]=g1n+cg;
  srColorBuffer[q][w][2]=b1n+cb;
  srColorBuffer[q][w][3]=a1n+ca;
  if(z>0){y+=1;z-=2*x;}z+=2*(oy-ey);}}
 if(ey<oy&&oy-ey>ox-ex){//:::::::::::: VAR 8
  x=oy-ey;z=(2*(ox-ex))-x;y=0;
  r=(r2n-r1n)/(float)x;g=(g2n-g1n)/(float)x;
  b=(b2n-b1n)/(float)x;a=(a2n-a1n)/(float)x;
  for(int i=0;i<x;i++){q=ox-y;w=oy-i;
  cr=r*(float)i;cg=g*(float)i;
  cb=b*(float)i;ca=a*(float)i;
  srColorBuffer[q][w][0]=r1n+cr;
  srColorBuffer[q][w][1]=g1n+cg;
  srColorBuffer[q][w][2]=b1n+cb;
  srColorBuffer[q][w][3]=a1n+ca;
  if(z>0){y+=1;z-=2*x;}z+=2*(ox-ex);}}}}

void sgDRAWTRIANGLE(int v[9],float c1[4],float c2[4],float c3[4]){
int lxw,lxh,lx,mxw,mxh,mx,rxw,rxh,rx,ox,oy;
int x1,x2,x3,y1,y2,y3,z1,z2,z3,tx,l1[1000],l2[1000],l3[1000];

checker1=-7;checker2=-7;checker3=-7;

lxw=v[3];lxh=v[4];lx=1;
if(v[0]<lxw){lxw=v[0];lxh=v[1];lx=0;}
if(v[6]<lxw){lxw=v[6];lxh=v[7];lx=2;}
rxw=v[3];rxh=v[4];rx=1;
if(v[0]>rxw){rxw=v[0];rxh=v[1];rx=0;}
if(v[6]>rxw){rxw=v[6];rxh=v[7];rx=2;}
if(lx==1&&rx==2){mx=0;}
if(lx==2&&rx==1){mx=0;}
if(lx==0&&rx==2){mx=1;}
if(lx==2&&rx==0){mx=1;}
if(lx==0&&rx==1){mx=2;}
if(lx==1&&rx==0){mx=2;}mxw=v[(mx*3)+0];mxh=v[(mx*3)+1];
ox=v[(lx*3)+0];oy=v[(lx*3)+1];
//checker1=lx;checker2=mx;checker3=rx;

if(mxh>=lxh&&mxh-lxh<=mxw-lxw){//:::::::::::: VAR 1
x1=mxw-lxw;z1=(2*(mxh-lxh))-x1;y1=0;tx=0;l1[0]=oy;
for(int i=0;i<x1;i++){l1[i]=oy+y1;
if(z1>0){y1+=1;z1-=2*x1;}z1+=2*(mxh-lxh);}}
if(rxh>=lxh&&rxh-lxh<=rxw-lxw){//:::::::::::: VAR 1
x2=rxw-lxw;z2=(2*(rxh-lxh))-x2;y2=0;tx=0;l2[0]=oy;
for(int i=0;i<x2;i++){l2[i]=oy+y2;
if(z2>0){y2+=1;z2-=2*x2;}z2+=2*(rxh-lxh);}}
if(rxh>=mxh&&rxh-mxh<=rxw-mxw){//:::::::::::: VAR 1
x3=rxw-mxw;z3=(2*(rxh-mxh))-x3;y3=0;tx=0;l3[0]=v[(mx*3)+1];
for(int i=0;i<x3;i++){l3[i]=v[(mx*3)+1]+y3;
if(z3>0){y3+=1;z3-=2*x3;}z3+=2*(rxh-mxh);}}
if(mxh>=lxh&&mxh-lxh>mxw-lxw){//:::::::::::: VAR 2
x1=mxh-lxh;z1=(2*(mxw-lxw))-x1;y1=0;tx=0;l1[0]=oy;
for(int i=0;i<x1;i++){l1[y1]=oy+i;
if(z1>0){y1+=1;z1-=2*x1;}z1+=2*(mxw-lxw);}}
if(rxh>=lxh&&rxh-lxh>rxw-lxw){//:::::::::::: VAR 2
x2=rxh-lxh;z2=(2*(rxw-lxw))-x2;y2=0;tx=0;l2[0]=oy;
for(int i=0;i<x2;i++){l2[y2]=oy+i;
if(z2>0){y2+=1;z2-=2*x2;}z2+=2*(rxw-lxw);}}
if(rxh>=mxh&&rxh-mxh>rxw-mxw){//:::::::::::: VAR 2
x3=rxh-mxh;z3=(2*(rxw-mxw))-x3;y3=0;tx=0;l3[0]=v[(mx*3)+1];
for(int i=0;i<x3;i++){l3[y3]=v[(mx*3)+1]+i;
if(z3>0){y3+=1;z3-=2*x3;}z3+=2*(rxw-mxw);}}
if(mxh<lxh&&lxh-mxh<=mxw-lxw){//:::::::::::: VAR 3
x1=mxw-lxw;z1=(2*(lxh-mxh))-x1;y1=0;tx=0;l1[0]=oy;
for(int i=0;i<x1;i++){l1[i]=oy-y1;
if(z1>0){y1+=1;z1-=2*x1;}z1+=2*(lxh-mxh);}}
if(rxh<lxh&&lxh-rxh<=rxw-lxw){//:::::::::::: VAR 3
x2=rxw-lxw;z2=(2*(lxh-rxh))-x2;y2=0;tx=0;l2[0]=oy;
for(int i=0;i<x2;i++){l2[i]=oy-y2;
if(z2>0){y2+=1;z2-=2*x2;}z2+=2*(lxh-rxh);}}
if(rxh<mxh&&mxh-rxh<=rxw-mxw){//:::::::::::: VAR 3
x3=rxw-mxw;z3=(2*(mxh-rxh))-x3;y3=0;tx=0;l3[0]=v[(mx*3)+1];
for(int i=0;i<x3;i++){l3[i]=v[(mx*3)+1]-y3;
if(z3>0){y3+=1;z3-=2*x3;}z3+=2*(mxh-rxh);}}
if(mxh<lxh&&lxh-mxh>mxw-lxw){//:::::::::::: VAR 4
x1=lxh-mxh;z1=(2*(mxw-lxw))-x1;y1=0;tx=0;l1[0]=oy;
for(int i=0;i<x1;i++){l1[y1]=oy-i;
if(z1>0){y1+=1;z1-=2*x1;}z1+=2*(mxw-lxw);}}
if(rxh<lxh&&lxh-rxh>rxw-lxw){//:::::::::::: VAR 4
x2=lxh-rxh;z2=(2*(rxw-lxw))-x2;y2=0;tx=0;l2[0]=oy;
for(int i=0;i<x2;i++){l2[y2]=oy-i;
if(z2>0){y2+=1;z2-=2*x2;}z2+=2*(rxw-lxw);}}
if(rxh<mxh&&mxh-rxh>rxw-mxw){//:::::::::::: VAR 4
x3=mxh-rxh;z3=(2*(rxw-mxw))-x3;y3=0;tx=0;l3[0]=v[(mx*3)+1];
for(int i=0;i<x3;i++){l3[y3]=v[(mx*3)+1]-i;
if(z3>0){y3+=1;z3-=2*x3;}z3+=2*(rxw-mxw);}}

float ba,bb,bc,tc1,tc2,tc3,r,g,b,px,py;
unsigned char cr,cg,cb,ca;

float v0x=0.0f,v0y=0.0f,v1x=0.0f,v1y=0.0f,v2x=0.0f,v2y=0.0f,d=0.0f;

v0x=v[3]-v[0];v0y=v[4]-v[1];
v1x=v[6]-v[0];v1y=v[7]-v[1];
d=v0x*v1y-v1x*v0y;

x1=mxw-lxw;x2=rxw-lxw;
if(mxh>=rxh){
for(int i=0;i<x1;i++){tx=l1[i]-l2[i];
for(int j=0;j<tx;j++){
px=ox+i;py=l1[i]-j;
v2x=px-v[0];v2y=py-v[1];
bb=(v2x*v1y-v1x*v2y)/d;bc=(v0x*v2y-v2x*v0y)/d;ba=1.0f-bb-bc;
srColorBuffer[ox+i][l1[i]-j][0]=((c1[0]*ba)+(c2[0]*bb)+(c3[0]*bc))*255.0f;
srColorBuffer[ox+i][l1[i]-j][1]=((c1[1]*ba)+(c2[1]*bb)+(c3[1]*bc))*255.0f;
srColorBuffer[ox+i][l1[i]-j][2]=((c1[2]*ba)+(c2[2]*bb)+(c3[2]*bc))*255.0f;
srColorBuffer[ox+i][l1[i]-j][3]=255;}}}
if(mxh<rxh){
for(int i=0;i<x1;i++){tx=l2[i]-l1[i];
for(int j=0;j<tx;j++){
px=ox+i;py=l1[i]+j;
v2x=px-v[0];v2y=py-v[1];
bb=(v2x*v1y-v1x*v2y)/d;bc=(v0x*v2y-v2x*v0y)/d;ba=1.0f-bb-bc;
srColorBuffer[ox+i][l1[i]+j][0]=((c1[0]*ba)+(c2[0]*bb)+(c3[0]*bc))*255.0f;
srColorBuffer[ox+i][l1[i]+j][1]=((c1[1]*ba)+(c2[1]*bb)+(c3[1]*bc))*255.0f;
srColorBuffer[ox+i][l1[i]+j][2]=((c1[2]*ba)+(c2[2]*bb)+(c3[2]*bc))*255.0f;
srColorBuffer[ox+i][l1[i]+j][3]=255;}}}

ox=v[(mx*3)+0];
if(mxh>=lxh){
for(int i=0;i<(x2-x1);i++){tx=l3[i]-l2[i+x1];
for(int j=0;j<tx;j++){
px=ox+i;py=l3[i]-j;
v2x=px-v[0];v2y=py-v[1];
bb=(v2x*v1y-v1x*v2y)/d;bc=(v0x*v2y-v2x*v0y)/d;ba=1.0f-bb-bc;
srColorBuffer[ox+i][l3[i]-j][0]=((c1[0]*ba)+(c2[0]*bb)+(c3[0]*bc))*255.0f;
srColorBuffer[ox+i][l3[i]-j][1]=((c1[1]*ba)+(c2[1]*bb)+(c3[1]*bc))*255.0f;
srColorBuffer[ox+i][l3[i]-j][2]=((c1[2]*ba)+(c2[2]*bb)+(c3[2]*bc))*255.0f;
srColorBuffer[ox+i][l3[i]-j][3]=255;}}}
if(mxh<lxh){
for(int i=0;i<(x2-x1);i++){tx=l2[i+x1]-l3[i];
for(int j=0;j<tx;j++){
px=ox+i;py=l3[i]+j;
v2x=px-v[0];v2y=py-v[1];
bb=(v2x*v1y-v1x*v2y)/d;bc=(v0x*v2y-v2x*v0y)/d;ba=1.0f-bb-bc;
srColorBuffer[ox+i][l3[i]+j][0]=((c1[0]*ba)+(c2[0]*bb)+(c3[0]*bc))*255.0f;
srColorBuffer[ox+i][l3[i]+j][1]=((c1[1]*ba)+(c2[1]*bb)+(c3[1]*bc))*255.0f;
srColorBuffer[ox+i][l3[i]+j][2]=((c1[2]*ba)+(c2[2]*bb)+(c3[2]*bc))*255.0f;
srColorBuffer[ox+i][l3[i]+j][3]=255;}}}

//checker1=l2[0];checker2=l2[200];checker3=l2[400-1];

sgDRAWLINE(v[0],v[1],v[3],v[4],c1,c2);
sgDRAWLINE(v[3],v[4],v[6],v[7],c2,c3);
sgDRAWLINE(v[6],v[7],v[0],v[1],c3,c1);

}




int main(int argc,char *argv[]){

srINIT();
srCLEAR();

int srQuit=0;

float color1[4]={1.0f,0.0f,0.0f,1.0f};
float color2[4]={0.0f,1.0f,0.0f,1.0f};
float color3[4]={0.0f,0.0f,1.0f,1.0f};

int vertice[9],verticeR[9];float verticeF[9];
verticeR[0]=300;verticeR[1]=700;
verticeR[3]=700;verticeR[4]=700;
verticeR[6]=500;verticeR[7]=300;

int vertice2[9],verticeR2[9];float verticeF2[9];
verticeR2[0]=500;verticeR2[1]=300;
verticeR2[3]=700;verticeR2[4]=700;
verticeR2[6]=700;verticeR2[7]=300;

float tx=0.0f;
float tangle=0.0f;


while(srQuit==0){
while(SDL_PollEvent(&srEvent)){
if(srEvent.type==SDL_QUIT){srQuit=1;}
sgUPDATE_KEYBOARD_STATE();}


if(key[k_ESCAPE]==1){srQuit=1;}
if(key[k_Q]==1){tangle-=0.1f;}
if(key[k_W]==1){tangle+=0.1f;}
if(key[k_E]==1){tangle=0.0f;}

for(int i=0;i<9;i++){verticeF[i]=verticeR[i];}
sgROTATE(500,500,verticeF[0],verticeF[1],tangle);
sgROTATE(500,500,verticeF[3],verticeF[4],tangle);
sgROTATE(500,500,verticeF[6],verticeF[7],tangle);
for(int i=0;i<9;i++){vertice[i]=verticeF[i];}

for(int i=0;i<9;i++){verticeF2[i]=verticeR2[i];}
sgROTATE(500,500,verticeF2[0],verticeF2[1],tangle);
sgROTATE(500,500,verticeF2[3],verticeF2[4],tangle);
sgROTATE(500,500,verticeF2[6],verticeF2[7],tangle);
for(int i=0;i<9;i++){vertice2[i]=verticeF2[i];}


//sgDRAWTRIANGLE(vertice,color1,color2,color3);


//sgDRAWLINE(vertice[0],vertice[1],vertice[3],vertice[4],color1,color2);
//sgDRAWLINE(vertice[3],vertice[4],vertice[6],vertice[7],color2,color3);
//sgDRAWLINE(vertice[6],vertice[7],vertice[0],vertice[1],color3,color1);

//sgDRAWLINE(vertice[0],vertice[1],vertice[6],vertice[7],color1,color3);

sgDRAWTRIANGLE(vertice,color1,color2,color3);
sgDRAWTRIANGLE(vertice2,color1,color2,color3);

srFLIP();
srCLEAR();
sgUPDATE_TIME();
tx=sgGET_FPS();
}
SDL_Quit();
std::cout<<"ok!   "<<tx<<"   "<<checker1<<"   "<<checker2<<"   "<<checker3<<std::endl;
return 0;
}
[doublepost=1494196652,1494196494][/doublepost]The line drawing is based on this integer logic:
https://en.wikipedia.org/wiki/Bresenham's_line_algorithm
And the triangle fill routine is based on the line drawing logic.
[doublepost=1494196982][/doublepost]oh and I call the drawline function inside the triangle fill funtion because the surface dosnt cover all area always, and I couldnt understand why but found that adding the line drawing would be enough to fill hole area... so with texturing the line drawing needs to be changed also...
 
Nice work, but I'd add some tabs and a few more comments to make it easier to follow :)

So, what made you want to do this? I like software rasterization too, lots of fun and portable to everything under the sun, including GPU-less platforms like the old GP2X.
 
I forgot about commenting. What made me want to do it was probably having problems with GLSL quite some time ago, and the fact you cant print out values in terminal from GPU cos thats how I normally "debug" my code, I know nothing of debuggers or such things. But also I am constantly on the quest of becoming completly independant, I know everyone would strongly advise against it, but I hate not understanding what Im doing and other things to I think, basically its because I suck at programming in general and especially compiling is my biggest area of unknown, so Im always trying to get away from reliance on libraries.

I always think when I m reading some tutorial or something I dont understand, that I could totally recreate the effect if I could code it myself, and so far that has always been the case for me, its just the advanced math that I dont know and feel would be a to big time sink to try to go back to the beginning and start studiying math again ( i have no education from school). What started me out in programming was reading somewere that raytracing was dead simple and I stumbled over a book that was written in a way that I could understand, and Im still waiting for the day we get those 1000 core CPUs so I can switch to glorious raytracing finally! :) Im also sad we didnt get the Larabee "GPU".

Also thanks for pointing to code and such, but what I have been looking for is a single file like I did above basically, mayby its different for everyone, but for me as selftaught on the internet I have never gotten into learning more about the language then I needed at the time and especially compiling have always till this day been a MAJOR headache for me, Im guessing there might be others like me out there looking for a single file thats easy to find what your looking for and see the complete implementation of it. Dealing with several files cant be easy for any beginner.
 
Dealing with several files cant be easy for any beginner.
I don't know, whilst I have earmarked several "single-file implemention" libraries in my github, I find them great to implement in a project, but not so great to learn from.

Personally, I consider seperate files as a massive bonus of modern day coding, because you can seperate out sections of logic, making it easier to find and read things that are specific to what you need.
The other bonus to me, is that you can leave your cursor in your current development block, and load up other files with releated code to check function names and parameters... or even if I had to add a new function, then I won't lose my place in the other source code.

Also, I guess it depends how much you intend to reuse any of the code. For example, say your 3d software renderer can render polygons by splitting it into triangles first. It might be handle to keep the poly->tri code in a seperate file to your software render so you can use it in other projects that might not require your renderer.
 
the fact you cant print out values in terminal from GPU cos thats how I normally "debug" my code
You can still start your work from a remote connection from your desktop/laptop. So the console is on your PC while the pandora screen show what it has to ;)

I know nothing of debuggers or such things
gdb is not that complex piece of software. All you have to remember : "gdb ./my_program" then "run" and when the crash occur "bt" ;)

Dealing with several files cant be easy for any beginner.
Err when you're writting your own software gles renderer, you cannot be seen as a beginner anymore ;)
 
Well its all a matter of opinion :)

I actually code in a few different files for my large projects also, but if its not a massive amounts of lines of code then I think one file is simpler to read for someone new to the code, but like I said everyone is of different opinion, but the important thing is there was several code projects with sophisticated code in several files, I didnt find any single file simple code but now there is, kind of, I should fix it up a bit and comment, but anyone interested in software rendering is probably mostly after the though bits like the triangle fill routine and can make their own system around that based on how they handle everything else, but I should add the perspective rendering also.

Yea after all these years I shouldnt call myself a beginner, but then I try to read some code from someone else and yea... Feeling quite small at times...
 
Back
Top