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 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]
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.
[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.