Major optimizations. Also did some perf testing, which is contained in drawmodeperf.org

This commit is contained in:
InventorXtreme 2023-12-05 22:49:33 -05:00
parent 46678bd628
commit 717c45260d
3 changed files with 70 additions and 25 deletions

72
c3d.c
View file

@ -23,10 +23,6 @@ const float half_fov = fov / 2;
float proj; //HALFWIDTH / tan(half_fov) Inited later
/*
The point is now rendering, but it is in front of and behind us at the same time
*/
struct LocalCam {
Vector3 position;
@ -117,7 +113,7 @@ void FillTopFlatZbuffer(Zee zee[][1080], Tri2D* t) {
double curx1 = t->c.x;
double curx2 = t->c.x;
for(int scanline=t->c.y; scanline > t->a.y; scanline--){
for(int scanline=t->c.y; scanline >= t->a.y; scanline--){
if (0 <= scanline && scanline < RENDERHEIGHT){
for (int i = Max(curx1,0); i < Min(curx2,RENDERWIDTH); i++) {
zee[i][scanline].triangle = t;
@ -129,15 +125,15 @@ void FillTopFlatZbuffer(Zee zee[][1080], Tri2D* t) {
}
//Draws triangle with a flat top. Note B and C must be the bottom points with A being the top "spike"
//Draws triangle with a flat bottomp. Note B and C must be the bottom points with A being the top "spike"
void FillBottomFlatZbuffer(Zee zee[][1080], Tri2D* t) {
//Becasue we are trying to get the x values in terms of the y values, we need inverse slope
float atobslopeinv = (t->b.x - t->a.x) / (t->b.y - t->a.y); //dif in x from start to end with a and c
float atocslopeinv = (t->c.x - t->a.x) / (t->c.y - t->a.y); //dif in x from start to end with b and c
//start at bottom of triangle (point c) so that we do not need to determine which vertex is on which side and increment it with its proper slope
double curx1 = t->c.x;
double curx2 = t->c.x;
//start at top of triangle (point c) so that we do not need to determine which vertex is on which side and increment it with its proper slope
double curx1 = t->a.x;
double curx2 = t->a.x;
for(int scanline=t->a.y; scanline < t->c.y; scanline++){
if (0 <= scanline && scanline < RENDERHEIGHT){
@ -385,9 +381,14 @@ int main() {
printf("mh:%d mw:%d w:%d h:%d\n", mh, mw, w, h);
SetWindowPosition(a.x + (0.5 * mw) - (w / 2), a.y + (0.5 * mh) - (0.5 * h));
RenderTexture2D renderTexture = LoadRenderTexture(RENDERWIDTH,RENDERHEIGHT);
RenderTexture2D uiraylibtexture = LoadRenderTexture(RENDERWIDTH,RENDERHEIGHT);
RenderTexture2D render3dtexture = LoadRenderTexture(RENDERWIDTH,RENDERHEIGHT);
Texture2D directaccesstex;
// Init cube model
// TODO: Load from obj
@ -424,14 +425,18 @@ int main() {
Tri2D funners = (Tri2D){ (Vector2){50,50},(Vector2){500,50},(Vector2){500,500},GREEN};
Tri2D funners2 = (Tri2D){ (Vector2){600,0},(Vector2){600,500},(Vector2){1000,500},RED};
Tri2D fullscreentritop = (Tri2D){ (Vector2){0,0}, (Vector2){1920,0}, (Vector2){1920,1080}, BLUE};
Tri2D fullscreentribottom = (Tri2D){ (Vector2){0,0}, (Vector2){0,1080}, (Vector2){1920,1080}, RED};
Tri2D blank = (Tri2D) { (Vector2){-10,-10},(Vector2){-10,-10}, (Vector2){-10,-10}};
while (!WindowShouldClose()) {
float frametime = GetFrameTime();
CtrlLocalCam(&camera,frametime);
LocalCamApplyVelo(&camera,frametime);
BeginTextureMode(renderTexture);
//ClearBackground(BLACK);
;
/* Vector3 TransVector = TransformWithCam(point,&camera); */
/* if (TransVector.z < 0) { */
/* Vector2 MPos = Conv3Dto2D(TransVector); */
@ -451,16 +456,16 @@ int main() {
Tri2DArrayAppend(&Tri2Darr,ConvertTriToTri2D(&TransformedTris.arr[i]));
}
//memset(ZBuff,0,sizeof(ZBuff));
memset(ZBuff,0,sizeof(ZBuff));
memset(display,0,sizeof(display));
for (int y = 0; y < RENDERHEIGHT; y++){
for (int x = 0; x<RENDERWIDTH; x++){
ZBuff[x][y].triangle = &blank;
}
}
/* for (int y = 0; y < RENDERHEIGHT; y++){ */
/* for (int x = 0; x<RENDERWIDTH; x++){ */
/* ZBuff[x][y].triangle = &blank; */
/* } */
/* } */
/* for(int i = 0; i < Tri2Darr.length; i++) { */
/* for(int y = 0; y < RENDERHEIGHT; y++){ */
@ -474,8 +479,13 @@ int main() {
FillTopFlatZbuffer(ZBuff, &funners);
FillBottomFlatZbuffer(ZBuff,&funners2);
//FillTopFlatZbuffer(ZBuff, &funners);
//FillBottomFlatZbuffer(ZBuff,&funners2);
//FillTopFlatZbuffer(ZBuff, &fullscreentritop);
//FillBottomFlatZbuffer(ZBuff, &fullscreentribottom);
int index = 0;
for(int y = 0; y < RENDERHEIGHT; y++) {
@ -487,21 +497,35 @@ int main() {
display[index+2] = c->b;
display[index+3] = c->a;
*/
display[index] = ZBuff[x][y].triangle->color;
if (ZBuff[x][y].triangle != NULL) {
//DrawPixel(x,y,ZBuff[x][y].triangle->color);
display[index] = ZBuff[x][y].triangle->color;
}
index = index+1;
}
}
BeginTextureMode(uiraylibtexture);
//gui stuff
EndTextureMode();
//Copytexture to main display :0
BeginDrawing();
ClearBackground(BLACK);
UpdateTexture(renderTexture.texture, display);
DrawTexturePro(renderTexture.texture, (Rectangle){0,0,renderTexture.texture.width,-renderTexture.texture.height},(Rectangle){0,0,SCREENWIDTH,SCREENHEIGHT},(Vector2){0,0},0,WHITE);
UpdateTexture(render3dtexture.texture, display);
//Copies render3dtexture to screen
DrawTexturePro(render3dtexture.texture, (Rectangle){0,0,render3dtexture.texture.width, render3dtexture.texture.height}, (Rectangle){0,0,SCREENWIDTH,SCREENHEIGHT},(Vector2){0,0},0,WHITE);
//Copies uiraylibtexture to screen (not this is not the texture used for 3d stuff
DrawTexturePro(uiraylibtexture.texture, (Rectangle){0,0,uiraylibtexture.texture.width,-uiraylibtexture.texture.height},(Rectangle){0,0,SCREENWIDTH,SCREENHEIGHT},(Vector2){0,0},0,WHITE);
char fpstext[40];
sprintf(fpstext,"%d",GetFPS());