Skip to content

Commit

Permalink
fixes/tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Bigfoot71 committed Sep 16, 2024
1 parent a5a53a0 commit 68be75f
Show file tree
Hide file tree
Showing 10 changed files with 380 additions and 264 deletions.
45 changes: 25 additions & 20 deletions src/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ PFcontext pfCreateContext(void* targetBuffer, PFsizei width, PFsizei height, PFp
{
/* Memory allocation for the context */

PFIctx *ctx = (PFIctx*)PF_CALLOC(sizeof(PFIctx), 1);
PFIctx *ctx = (PFIctx*)PF_CALLOC(1, sizeof(PFIctx));
if (!ctx) return NULL;

/* Initialization of the main framebuffer */
Expand Down Expand Up @@ -1678,9 +1678,7 @@ void pfVertex4fv(const PFfloat* v)
}
}

// NOTE: Used by `pfColor` to assign material colors
// when the `PF_COLOR_MATERIAL` state is enabled.
static inline void pfiSetCurrentColor(PFcolor color)
void pfColor(PFcolor color)
{
if (G_currentCtx->state & PF_COLOR_MATERIAL) {
PFImaterial *m1 = &G_currentCtx->faceMaterial[PF_FRONT];
Expand Down Expand Up @@ -1712,23 +1710,30 @@ static inline void pfiSetCurrentColor(PFcolor color)
}
}

void pfColor1ui(PFuint color)
{
pfColor(((union {
PFuint x; PFcolor r;
}) { .x = color }).r);
}

void pfColor3ub(PFubyte r, PFubyte g, PFubyte b)
{
pfiSetCurrentColor((PFcolor) {
pfColor((PFcolor) {
r, g, b, 255
});
}

void pfColor3ubv(const PFubyte* v)
{
pfiSetCurrentColor((PFcolor) {
pfColor((PFcolor) {
v[0], v[1], v[2], 255
});
}

void pfColor3us(PFushort r, PFushort g, PFushort b)
{
pfiSetCurrentColor((PFcolor) {
pfColor((PFcolor) {
(PFubyte)(r >> 8),
(PFubyte)(g >> 8),
(PFubyte)(b >> 8),
Expand All @@ -1738,7 +1743,7 @@ void pfColor3us(PFushort r, PFushort g, PFushort b)

void pfColor3usv(const PFushort* v)
{
pfiSetCurrentColor((PFcolor) {
pfColor((PFcolor) {
(PFubyte)(v[0] >> 8),
(PFubyte)(v[1] >> 8),
(PFubyte)(v[2] >> 8),
Expand All @@ -1748,7 +1753,7 @@ void pfColor3usv(const PFushort* v)

void pfColor3ui(PFuint r, PFuint g, PFuint b)
{
pfiSetCurrentColor((PFcolor) {
pfColor((PFcolor) {
(PFubyte)(r >> 24),
(PFubyte)(g >> 24),
(PFubyte)(b >> 24),
Expand All @@ -1758,7 +1763,7 @@ void pfColor3ui(PFuint r, PFuint g, PFuint b)

void pfColor3uiv(const PFuint* v)
{
pfiSetCurrentColor((PFcolor) {
pfColor((PFcolor) {
(PFubyte)(v[0] >> 24),
(PFubyte)(v[1] >> 24),
(PFubyte)(v[2] >> 24),
Expand All @@ -1768,7 +1773,7 @@ void pfColor3uiv(const PFuint* v)

void pfColor3f(PFfloat r, PFfloat g, PFfloat b)
{
pfiSetCurrentColor((PFcolor) {
pfColor((PFcolor) {
(PFubyte)(r*255),
(PFubyte)(g*255),
(PFubyte)(b*255),
Expand All @@ -1778,7 +1783,7 @@ void pfColor3f(PFfloat r, PFfloat g, PFfloat b)

void pfColor3fv(const PFfloat* v)
{
pfiSetCurrentColor((PFcolor) {
pfColor((PFcolor) {
(PFubyte)(v[0]*255),
(PFubyte)(v[1]*255),
(PFubyte)(v[2]*255),
Expand All @@ -1788,21 +1793,21 @@ void pfColor3fv(const PFfloat* v)

void pfColor4ub(PFubyte r, PFubyte g, PFubyte b, PFubyte a)
{
pfiSetCurrentColor((PFcolor) {
pfColor((PFcolor) {
r, g, b, a
});
}

void pfColor4ubv(const PFubyte* v)
{
pfiSetCurrentColor((PFcolor) {
pfColor((PFcolor) {
v[0], v[1], v[2], v[3]
});
}

void pfColor4us(PFushort r, PFushort g, PFushort b, PFushort a)
{
pfiSetCurrentColor((PFcolor) {
pfColor((PFcolor) {
(PFubyte)(r >> 8),
(PFubyte)(g >> 8),
(PFubyte)(b >> 8),
Expand All @@ -1812,7 +1817,7 @@ void pfColor4us(PFushort r, PFushort g, PFushort b, PFushort a)

void pfColor4usv(const PFushort* v)
{
pfiSetCurrentColor((PFcolor) {
pfColor((PFcolor) {
(PFubyte)(v[0] >> 8),
(PFubyte)(v[1] >> 8),
(PFubyte)(v[2] >> 8),
Expand All @@ -1822,7 +1827,7 @@ void pfColor4usv(const PFushort* v)

void pfColor4ui(PFuint r, PFuint g, PFuint b, PFuint a)
{
pfiSetCurrentColor((PFcolor) {
pfColor((PFcolor) {
(PFubyte)(r >> 24),
(PFubyte)(g >> 24),
(PFubyte)(b >> 24),
Expand All @@ -1832,7 +1837,7 @@ void pfColor4ui(PFuint r, PFuint g, PFuint b, PFuint a)

void pfColor4uiv(const PFuint* v)
{
pfiSetCurrentColor((PFcolor) {
pfColor((PFcolor) {
(PFubyte)(v[0] >> 24),
(PFubyte)(v[1] >> 24),
(PFubyte)(v[2] >> 24),
Expand All @@ -1842,7 +1847,7 @@ void pfColor4uiv(const PFuint* v)

void pfColor4f(PFfloat r, PFfloat g, PFfloat b, PFfloat a)
{
pfiSetCurrentColor((PFcolor) {
pfColor((PFcolor) {
(PFubyte)(r*255),
(PFubyte)(g*255),
(PFubyte)(b*255),
Expand All @@ -1852,7 +1857,7 @@ void pfColor4f(PFfloat r, PFfloat g, PFfloat b, PFfloat a)

void pfColor4fv(const PFfloat* v)
{
pfiSetCurrentColor((PFcolor) {
pfColor((PFcolor) {
(PFubyte)(v[0]*255),
(PFubyte)(v[1]*255),
(PFubyte)(v[2]*255),
Expand Down
37 changes: 33 additions & 4 deletions src/internal/context/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

/* Backup context functions */

void pfiMakeContextBackup(void)
void
pfiMakeContextBackup(void)
{
PFIctxbackup *bck = &G_currentCtx->ctxBackup;
memcpy(bck->faceMaterial, G_currentCtx->faceMaterial, 2 * sizeof(PFImaterial));
Expand All @@ -33,7 +34,8 @@ void pfiMakeContextBackup(void)
bck->state = G_currentCtx->state;
}

void pfiRestoreContext(void)
void
pfiRestoreContext(void)
{
PFIctxbackup *bck = &G_currentCtx->ctxBackup;
memcpy(G_currentCtx->faceMaterial, bck->faceMaterial, 2 * sizeof(PFImaterial));
Expand All @@ -46,7 +48,8 @@ void pfiRestoreContext(void)

/* Internal vertex processing function definitions */

void pfiHomogeneousToScreen(PFIvertex* v)
void
pfiHomogeneousToScreen(PFIvertex* v)
{
// NOTE: We add 0.5 to the screen coordinates to round them to the nearest integer
// when they are converted to integer coordinates. This adjustment was added because
Expand All @@ -61,9 +64,35 @@ void pfiHomogeneousToScreen(PFIvertex* v)
v->screen[1] = (G_currentCtx->vpPos[1] + (1.0f - v->homogeneous[1]) * 0.5f * G_currentCtx->vpDim[1]) + 0.5f;
}

PFIvertex
pfiLerpVertex(const PFIvertex* start, const PFIvertex* end, PFfloat t)
{
PFIvertex result = { 0 };

const PFubyte *startCol = (const PFubyte*)(&start->color);
const PFubyte *endCol = (const PFubyte*)(&end->color);
PFubyte *resultCol = (PFubyte*)(&result.color);
PFubyte uT = (PFubyte)(255 * t);

# ifdef _OPENMP
# pragma omp simd
# endif
for (int_fast8_t i = 0; i < 4; i++) {
result.homogeneous[i] = start->homogeneous[i] + t*(end->homogeneous[i] - start->homogeneous[i]);
result.position[i] = start->position[i] + t*(end->position[i] - start->position[i]);
resultCol[i] = startCol[i] + (uT*((PFint)endCol[i] - startCol[i]))/255;

if (i < 2) result.texcoord[i] = start->texcoord[i] + t*(end->texcoord[i] - start->texcoord[i]);
if (i < 3) result.normal[i] = start->normal[i] + t*(end->normal[i] - start->normal[i]);
}

return result;
}

/* Internal processing and rasterization function definitions */

void pfiProcessAndRasterize(void)
void
pfiProcessAndRasterize(void)
{
switch (G_currentCtx->currentDrawMode) {
case PF_POINTS:
Expand Down
2 changes: 2 additions & 0 deletions src/internal/context/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ void pfiMakeContextBackup(void);
void pfiRestoreContext(void);

void pfiHomogeneousToScreen(PFIvertex* v);
PFIvertex pfiLerpVertex(const PFIvertex* start, const PFIvertex* end, PFfloat t);

void pfiProcessAndRasterize(void);


Expand Down
85 changes: 0 additions & 85 deletions src/internal/helper.h

This file was deleted.

5 changes: 2 additions & 3 deletions src/internal/primitives/lines.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

#include "../context/context.h"
#include "../helper.h"
#include <stdlib.h>

/* Enums for internal use */
Expand Down Expand Up @@ -166,8 +165,8 @@ PFboolean Process_ClipLine2D(PFIvertex* restrict v1, PFIvertex* restrict v2)
if (code0 & code1) break;

if (code0 == CLIP_INSIDE) {
pfiSwapByte(&code0, &code1);
pfiSwapVertex(v1, v2);
PFubyte ctmp = code0; code0 = code1; code1 = ctmp;
PFIvertex vtmp = *v1; *v1 = *v2; *v2 = vtmp;
}

if (code0 & CLIP_LEFT) {
Expand Down
1 change: 0 additions & 1 deletion src/internal/primitives/triangles.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "../lighting/lighting.h"
#include "../context/context.h"
#include "../../pfm.h"
#include "../helper.h"
#include "../color.h"
#include "../blend.h"

Expand Down
Loading

0 comments on commit 68be75f

Please sign in to comment.