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

View file

@ -1,5 +1,5 @@
CC = gcc CC = gcc
CFLAGS = -pg -g CFLAGS = -pg -g -O2
LDFLAGS = -pg -g LDFLAGS = -pg -g
LDLIBS = -lraylib -lm LDLIBS = -lraylib -lm
objects = c3d.o objects = c3d.o

70
c3d.c
View file

@ -23,10 +23,6 @@ const float half_fov = fov / 2;
float proj; //HALFWIDTH / tan(half_fov) Inited later 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 { struct LocalCam {
Vector3 position; Vector3 position;
@ -117,7 +113,7 @@ void FillTopFlatZbuffer(Zee zee[][1080], Tri2D* t) {
double curx1 = t->c.x; double curx1 = t->c.x;
double curx2 = 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){ if (0 <= scanline && scanline < RENDERHEIGHT){
for (int i = Max(curx1,0); i < Min(curx2,RENDERWIDTH); i++) { for (int i = Max(curx1,0); i < Min(curx2,RENDERWIDTH); i++) {
zee[i][scanline].triangle = t; 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) { 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 //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 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 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 //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->c.x; double curx1 = t->a.x;
double curx2 = t->c.x; double curx2 = t->a.x;
for(int scanline=t->a.y; scanline < t->c.y; scanline++){ for(int scanline=t->a.y; scanline < t->c.y; scanline++){
if (0 <= scanline && scanline < RENDERHEIGHT){ if (0 <= scanline && scanline < RENDERHEIGHT){
@ -385,7 +381,12 @@ int main() {
printf("mh:%d mw:%d w:%d h:%d\n", mh, mw, w, h); 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)); 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 // Init cube model
@ -424,14 +425,18 @@ int main() {
Tri2D funners = (Tri2D){ (Vector2){50,50},(Vector2){500,50},(Vector2){500,500},GREEN}; 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 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}}; Tri2D blank = (Tri2D) { (Vector2){-10,-10},(Vector2){-10,-10}, (Vector2){-10,-10}};
while (!WindowShouldClose()) { while (!WindowShouldClose()) {
float frametime = GetFrameTime(); float frametime = GetFrameTime();
CtrlLocalCam(&camera,frametime); CtrlLocalCam(&camera,frametime);
LocalCamApplyVelo(&camera,frametime); LocalCamApplyVelo(&camera,frametime);
BeginTextureMode(renderTexture);
//ClearBackground(BLACK); //ClearBackground(BLACK);
;
/* Vector3 TransVector = TransformWithCam(point,&camera); */ /* Vector3 TransVector = TransformWithCam(point,&camera); */
/* if (TransVector.z < 0) { */ /* if (TransVector.z < 0) { */
/* Vector2 MPos = Conv3Dto2D(TransVector); */ /* Vector2 MPos = Conv3Dto2D(TransVector); */
@ -451,16 +456,16 @@ int main() {
Tri2DArrayAppend(&Tri2Darr,ConvertTriToTri2D(&TransformedTris.arr[i])); Tri2DArrayAppend(&Tri2Darr,ConvertTriToTri2D(&TransformedTris.arr[i]));
} }
//memset(ZBuff,0,sizeof(ZBuff)); memset(ZBuff,0,sizeof(ZBuff));
memset(display,0,sizeof(display)); memset(display,0,sizeof(display));
for (int y = 0; y < RENDERHEIGHT; y++){ /* for (int y = 0; y < RENDERHEIGHT; y++){ */
for (int x = 0; x<RENDERWIDTH; x++){ /* for (int x = 0; x<RENDERWIDTH; x++){ */
ZBuff[x][y].triangle = &blank; /* ZBuff[x][y].triangle = &blank; */
} /* } */
} /* } */
/* for(int i = 0; i < Tri2Darr.length; i++) { */ /* for(int i = 0; i < Tri2Darr.length; i++) { */
/* for(int y = 0; y < RENDERHEIGHT; y++){ */ /* for(int y = 0; y < RENDERHEIGHT; y++){ */
@ -474,8 +479,13 @@ int main() {
FillTopFlatZbuffer(ZBuff, &funners); //FillTopFlatZbuffer(ZBuff, &funners);
FillBottomFlatZbuffer(ZBuff,&funners2); //FillBottomFlatZbuffer(ZBuff,&funners2);
//FillTopFlatZbuffer(ZBuff, &fullscreentritop);
//FillBottomFlatZbuffer(ZBuff, &fullscreentribottom);
int index = 0; int index = 0;
for(int y = 0; y < RENDERHEIGHT; y++) { for(int y = 0; y < RENDERHEIGHT; y++) {
@ -487,21 +497,35 @@ int main() {
display[index+2] = c->b; display[index+2] = c->b;
display[index+3] = c->a; 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; index = index+1;
} }
} }
BeginTextureMode(uiraylibtexture);
//gui stuff
EndTextureMode(); EndTextureMode();
//Copytexture to main display :0 //Copytexture to main display :0
BeginDrawing(); BeginDrawing();
ClearBackground(BLACK); ClearBackground(BLACK);
UpdateTexture(renderTexture.texture, display); UpdateTexture(render3dtexture.texture, display);
DrawTexturePro(renderTexture.texture, (Rectangle){0,0,renderTexture.texture.width,-renderTexture.texture.height},(Rectangle){0,0,SCREENWIDTH,SCREENHEIGHT},(Vector2){0,0},0,WHITE);
//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]; char fpstext[40];
sprintf(fpstext,"%d",GetFPS()); sprintf(fpstext,"%d",GetFPS());

21
drawmodeperf.org Normal file
View file

@ -0,0 +1,21 @@
* Performnace compars for C3D without any real rendering, only drawing blank screen and 2D tris
** GCC with profiling and line profiling enabled. No optimization
| RenderType | Displayed on screen | Fps |
| Fill Zbuff with blank tris, do not check for null | Blank Screen | 98-100 |
| '' | Two triangles, not filled screen | 90-92 |
| '' | Two triangles, full screen | 43-44 |
| '' | Four triangles, with the two that did not fill screen being obsured | 42-43 |
| Meset Zbuff to zero, check for null TRIs | Blank Screen | 141-142 |
| '' | Two triangles, not filled screen | 115-117 |
| '' | Two triangles, full screen | 45-46 |
| '' | Four triangles, with the two that did not fill screen being obsured | 42-43 |
** GCC with profiling and line profiling enabled. -O2 flag.
| RenderType | Displayed on screen | Fps |
| Fill Zbuff with blank tris, do not check for null | Blank Screen | 151-157 |
| '' | Two triangles, not filled screen | 151-154 |
| '' | Two triangles, full screen | 121-128 |
| '' | Four triangles, with the two that did not fill screen being obsured | 120-124 |
| Meset Zbuff to zero, check for null TRIs | Blank Screen | 299-301 |
| '' | Two triangles, not filled screen | 154-155 |
| '' | Two triangles, full screen | 120-123 |
| '' | Four triangles, with the two that did not fill screen being | 118-121 |