Skip to content

Commit bcd6067

Browse files
committed
First pass on sphere rendering
1 parent d202f5e commit bcd6067

File tree

6 files changed

+46
-18
lines changed

6 files changed

+46
-18
lines changed

FitdLib/osystem.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ extern "C"{
7979
void osystem_fillPoly(float* buffer, int numPoint, unsigned char color,u8 polyType);
8080
void osystem_draw3dLine(float x1, float y1, float z1, float x2, float y2, float z2, unsigned char color);
8181
void osystem_draw3dQuad(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, float x4, float y4, float z4, unsigned char color, int transparency);
82-
void osystem_drawSphere(float X, float Y, float Z, u8 color, float size);
83-
void osystem_drawPoint(float X, float Y, float Z, u8 color, float size);
82+
void osystem_drawSphere(float X, float Y, float Z, u8 color, u8 material, float size);
83+
void osystem_drawPoint(float X, float Y, float Z, u8 color, u8 material, float size);
8484
void osystem_flushPendingPrimitives();
8585

8686
void osystem_startBgPoly();

FitdLib/renderer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -917,19 +917,19 @@ void renderZixel(primEntryStruct* pEntry) // point
917917
float pointSize = 20.f;
918918
float transformedSize = ((pointSize * (float)cameraFovX) / (float)(pEntry->vertices[0].Z+cameraPerspective));
919919

920-
osystem_drawPoint(pEntry->vertices[0].X,pEntry->vertices[0].Y,pEntry->vertices[0].Z,pEntry->color,transformedSize);
920+
osystem_drawPoint(pEntry->vertices[0].X,pEntry->vertices[0].Y,pEntry->vertices[0].Z,pEntry->color,pEntry->material, transformedSize);
921921
}
922922

923923
void renderPoint(primEntryStruct* pEntry) // point
924924
{
925925
float pointSize = 0.3f; // TODO: better way to compute that?
926-
osystem_drawPoint(pEntry->vertices[0].X,pEntry->vertices[0].Y,pEntry->vertices[0].Z,pEntry->color, pointSize);
926+
osystem_drawPoint(pEntry->vertices[0].X,pEntry->vertices[0].Y,pEntry->vertices[0].Z,pEntry->color, pEntry->material, pointSize);
927927
}
928928

929929
void renderBigPoint(primEntryStruct* pEntry) // point
930930
{
931931
float bigPointSize = 2.f; // TODO: better way to compute that?
932-
osystem_drawPoint(pEntry->vertices[0].X,pEntry->vertices[0].Y,pEntry->vertices[0].Z,pEntry->color, bigPointSize);
932+
osystem_drawPoint(pEntry->vertices[0].X,pEntry->vertices[0].Y,pEntry->vertices[0].Z,pEntry->color, pEntry->material, bigPointSize);
933933
}
934934

935935
void renderSphere(primEntryStruct* pEntry) // sphere
@@ -938,7 +938,7 @@ void renderSphere(primEntryStruct* pEntry) // sphere
938938

939939
transformedSize = (((float)pEntry->size * (float)cameraFovX) / (float)(pEntry->vertices[0].Z+cameraPerspective));
940940

941-
osystem_drawSphere(pEntry->vertices[0].X,pEntry->vertices[0].Y,pEntry->vertices[0].Z,pEntry->color, transformedSize);
941+
osystem_drawSphere(pEntry->vertices[0].X,pEntry->vertices[0].Y,pEntry->vertices[0].Z,pEntry->color, pEntry->material, transformedSize);
942942
}
943943

944944

FitdLib/rendererBGFX.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,12 +1150,12 @@ void osystem_draw3dQuad(float x1, float y1, float z1, float x2, float y2, float
11501150

11511151
}
11521152

1153-
void osystem_drawSphere(float X, float Y, float Z, u8 color, float size)
1153+
void osystem_drawSphere(float X, float Y, float Z, u8 color, u8 material, float size)
11541154
{
1155-
osystem_drawPoint(X, Y, Z, color, size);
1155+
osystem_drawPoint(X, Y, Z, color, material, size);
11561156
}
11571157

1158-
void osystem_drawPoint(float X, float Y, float Z, u8 color, float size)
1158+
void osystem_drawPoint(float X, float Y, float Z, u8 color, u8 material, float size)
11591159
{
11601160
std::array<sphereVertex, 4> corners;
11611161
corners[0].X = X + size;
@@ -1193,6 +1193,7 @@ void osystem_drawPoint(float X, float Y, float Z, u8 color, float size)
11931193
pVertex->size = size;
11941194
pVertex->centerX = X;
11951195
pVertex->centerY = Y;
1196+
pVertex->material = material;
11961197
}
11971198
}
11981199

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
vec3 a_position : POSITION;
22
vec2 a_texcoord0 : TEXCOORD0;
3-
vec3 a_texcoord1 : TEXCOORD1;
3+
vec4 a_texcoord1 : TEXCOORD1;
44

55
vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
6-
vec3 v_sphereParams : TEXCOORD1 = vec3(0.0, 0.0, 0.0);
6+
vec4 v_sphereParams : TEXCOORD1 = vec4(0.0, 0.0, 0.0, 0.0);
77
vec3 v_screenSpacePosition : TEXCOORD2 = vec3(0.0, 0.0, 0.0);
88

FitdLib/shaders/sphere_ps.sc

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,42 @@ void main()
88
{
99
vec2 sphereCenter = v_sphereParams.xy;
1010
float sphereSize = v_sphereParams.z;
11-
float fDistanceToCenter = length(v_screenSpacePosition - sphereCenter);
11+
float fDistanceToCenter = length(v_screenSpacePosition.xy - sphereCenter);
1212
if(fDistanceToCenter > sphereSize)
1313
discard;
1414

1515
int color = int(v_texcoord0.x * 15.f);
1616
int bank = int(v_texcoord0.y * 15.f);
17+
int material = (int)v_sphereParams.w;
1718

18-
int colorOffset = (bank << 4) + color;
19+
if(material == 0) { // flat
20+
int colorOffset = (bank << 4) + color;
21+
gl_FragColor.r = (texelFetch(s_paletteTexture, ivec2(0, colorOffset), 0) / 255.f).r;
22+
gl_FragColor.g = (texelFetch(s_paletteTexture, ivec2(1, colorOffset), 0) / 255.f).r;
23+
gl_FragColor.b = (texelFetch(s_paletteTexture, ivec2(2, colorOffset), 0) / 255.f).r;
24+
gl_FragColor.w = 1;
25+
} else if(material == 2) { // transparent
26+
int colorOffset = (bank << 4) + color;
27+
gl_FragColor.r = (texelFetch(s_paletteTexture, ivec2(0, colorOffset), 0) / 255.f).r;
28+
gl_FragColor.g = (texelFetch(s_paletteTexture, ivec2(1, colorOffset), 0) / 255.f).r;
29+
gl_FragColor.b = (texelFetch(s_paletteTexture, ivec2(2, colorOffset), 0) / 255.f).r;
30+
gl_FragColor.w = 0.5;
31+
} else if(material == 3) { // marbre
32+
vec2 normalizedPosition = (v_screenSpacePosition.xy - sphereCenter.xy) / sphereSize;
33+
float angle = asin(normalizedPosition.y);
34+
float distanceToCircleOnScanline = cos(angle) - normalizedPosition.x; // value is in 0/2 range
35+
distanceToCircleOnScanline /= 2.f; // remap to 0 / 1
36+
color = (1.f - distanceToCircleOnScanline) * 15.f;
1937

20-
gl_FragColor.r = (texelFetch(s_paletteTexture, ivec2(0, colorOffset), 0) / 255.f).r;
21-
gl_FragColor.g = (texelFetch(s_paletteTexture, ivec2(1, colorOffset), 0) / 255.f).r;
22-
gl_FragColor.b = (texelFetch(s_paletteTexture, ivec2(2, colorOffset), 0) / 255.f).r;
23-
gl_FragColor.w = 1;
38+
int colorOffset = (bank << 4) + color;
39+
gl_FragColor.r = (texelFetch(s_paletteTexture, ivec2(0, colorOffset), 0) / 255.f).r;
40+
gl_FragColor.g = (texelFetch(s_paletteTexture, ivec2(1, colorOffset), 0) / 255.f).r;
41+
gl_FragColor.b = (texelFetch(s_paletteTexture, ivec2(2, colorOffset), 0) / 255.f).r;
42+
gl_FragColor.w = 1;
43+
} else {
44+
gl_FragColor.r = 1.f;
45+
gl_FragColor.g = 0;
46+
gl_FragColor.b = 0;
47+
gl_FragColor.w = 1;
48+
}
2449
}

FitdLib/shaders/sphere_vs.sc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ void main()
1010

1111
vec2 sphereCenter = a_texcoord1.xy;
1212
float sphereSize = a_texcoord1.z;
13+
float sphereMaterial = a_texcoord1.w;
1314

1415
v_screenSpacePosition = a_position.xyz;
1516
v_sphereParams.xy = sphereCenter.xy;
16-
v_sphereParams.z = sphereSize; // radius
17+
v_sphereParams.z = sphereSize;
18+
v_sphereParams.w = sphereMaterial;
1719
}

0 commit comments

Comments
 (0)