Skip to content

Commit b171e80

Browse files
hoffstadt-nasahoffstadt
authored andcommitted
WIP
1 parent 26e9400 commit b171e80

File tree

9 files changed

+698
-417
lines changed

9 files changed

+698
-417
lines changed

examples/example_gfx_3.c

Lines changed: 140 additions & 55 deletions
Large diffs are not rendered by default.

examples/example_gfx_4.c

Lines changed: 308 additions & 330 deletions
Large diffs are not rendered by default.

examples/shaders/solid.frag

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
layout(set = 3, binding = 0) uniform PL_DYNAMIC_DATA
77
{
88
mat4 tCameraViewProjection;
9-
vec3 tCameraPos;
9+
vec4 tCameraPosHigh;
10+
vec4 tCameraPosLow;
1011
} tObjectInfo;
1112

1213
layout(location = 0) out vec4 outColor;
@@ -36,33 +37,26 @@ vec3 phongBRDF(vec3 lightDir, vec3 viewDir, vec3 normal, vec3 phongDiffuseCol, v
3637

3738
void main()
3839
{
39-
// vec3 sunlightColor = vec3(1.0, 1.0, 1.0);
40-
vec3 sunlightDirection = vec3(-1.0, -1.0, -1.0);
40+
// vec3 sunlightDirection = vec3(-1.0, -1.0, -1.0);
4141

42-
// vec3 diffuse = vec3(0.7);
43-
vec3 ambient = vec3(0);
42+
// vec3 ambient = vec3(0);
4443

45-
// outColor.rgb = diffuse * (max(0.0, dot(shadingNormal, w_i)) * sunlightColor + ambient) +
46-
// sunlightColor * (0.1 * pow(max(0.0, dot(shadingNormal, w_h)), 6));
44+
// vec3 lightDir = normalize(sunlightDirection);
45+
// vec3 viewDir = normalize(tShaderIn.tPosition - tObjectInfo.tCameraPosHigh.xyz);
46+
// // vec3 viewDir = normalize(-tShaderIn.tPosition);
47+
// vec3 n = normalize(tShaderIn.tPosition);
4748

48-
// outColor.a = 1.0;
49-
50-
// new
51-
52-
vec3 lightDir = normalize(-sunlightDirection);
53-
vec3 viewDir = normalize(-tObjectInfo.tCameraPos);
54-
vec3 n = normalize(tShaderIn.tPosition);
55-
56-
vec3 radiance = ambient;
49+
// vec3 radiance = ambient;
5750

58-
float irradiance = max(dot(lightDir, n), 0.0) * irradiPerp;
59-
if(irradiance > 0.0)
60-
{
61-
vec3 brdf = phongBRDF(lightDir, viewDir, n, diffuseColor.rgb, specularColor.rgb, shininess);
62-
radiance += brdf * irradiance * lightColor.rgb;
63-
}
64-
65-
radiance = pow(radiance, vec3(1.0 / 2.2) ); // gamma correction
66-
outColor.rgb = radiance;
51+
// float ddot = dot(lightDir, n);
52+
// float irradiance = max(ddot, 0.0) * irradiPerp;
53+
// if(irradiance > 0.0)
54+
// {
55+
// vec3 brdf = phongBRDF(lightDir, viewDir, n, diffuseColor.rgb, specularColor.rgb, shininess);
56+
// radiance += brdf * irradiance * lightColor.rgb;
57+
// }
58+
59+
// radiance = pow(radiance, vec3(1.0 / 2.2) ); // gamma correction
60+
outColor.rgb = vec3(1.0);
6761
outColor.a = 1.0;
6862
}

examples/shaders/solid.vert

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,31 @@
55

66
layout(set = 3, binding = 0) uniform PL_DYNAMIC_DATA
77
{
8-
98
mat4 tCameraViewProjection;
10-
vec3 tCameraPos;
9+
vec4 tCameraPosHigh;
10+
vec4 tCameraPosLow;
1111
} tObjectInfo;
1212

13-
layout(location = 0) in vec3 inPos;
13+
layout(location = 0) in vec3 inHighPos;
14+
layout(location = 1) in vec3 inLowPos;
1415

1516
layout(location = 0) out struct plShaderOut {
1617
vec3 tPosition;
1718
} tShaderIn;
1819

1920
void main()
2021
{
21-
tShaderIn.tPosition = inPos;
22-
gl_Position = tObjectInfo.tCameraViewProjection * vec4(inPos, 1.0);
22+
// vec3 highDifference = inHighPos - tObjectInfo.tCameraPosHigh.xyz;
23+
// vec3 lowDifference = inLowPos - tObjectInfo.tCameraPosLow.xyz;
24+
25+
vec3 t1 = inLowPos - tObjectInfo.tCameraPosLow.xyz;
26+
vec3 e = t1 - inLowPos;
27+
28+
vec3 t2 = ((-tObjectInfo.tCameraPosLow.xyz - e) + (inLowPos - (t1 - e))) + inHighPos - tObjectInfo.tCameraPosHigh.xyz;
29+
vec3 highDifference = t1 + t2;
30+
vec3 lowDifference = t2 - (highDifference - t1);
31+
32+
33+
tShaderIn.tPosition = lowDifference + highDifference;
34+
gl_Position = tObjectInfo.tCameraViewProjection * vec4(tShaderIn.tPosition, 1.0);
2335
}

examples/shaders/solid_single.frag

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#version 450
2+
#extension GL_ARB_separate_shader_objects : enable
3+
#extension GL_EXT_nonuniform_qualifier : enable
4+
5+
6+
layout(set = 3, binding = 0) uniform PL_DYNAMIC_DATA
7+
{
8+
mat4 tCameraViewProjection;
9+
vec3 tCameraPos;
10+
} tObjectInfo;
11+
12+
layout(location = 0) out vec4 outColor;
13+
14+
layout(location = 0) in struct plShaderOut {
15+
vec3 tPosition;
16+
} tShaderIn;
17+
18+
//-----------------------------------------------------------------------------
19+
// [SECTION] entry
20+
//-----------------------------------------------------------------------------
21+
22+
const vec4 ambientColor = vec4(0.01, 0.0, 0.0, 1.0);
23+
const vec4 diffuseColor = vec4(0.25, 0.0, 0.0, 1.0);
24+
const vec4 specularColor = vec4(1.0, 1.0, 1.0, 1.0);
25+
const float shininess = 20.0;
26+
const vec4 lightColor = vec4(1.0, 1.0, 1.0, 1.0);
27+
const float irradiPerp = 1.0;
28+
29+
vec3 phongBRDF(vec3 lightDir, vec3 viewDir, vec3 normal, vec3 phongDiffuseCol, vec3 phongSpecularCol, float phongShininess) {
30+
vec3 color = phongDiffuseCol;
31+
vec3 reflectDir = reflect(-lightDir, normal);
32+
float specDot = max(dot(reflectDir, viewDir), 0.0);
33+
color += pow(specDot, phongShininess) * phongSpecularCol;
34+
return color;
35+
}
36+
37+
void main()
38+
{
39+
// vec3 sunlightColor = vec3(1.0, 1.0, 1.0);
40+
vec3 sunlightDirection = vec3(-1.0, -1.0, -1.0);
41+
42+
// vec3 diffuse = vec3(0.7);
43+
vec3 ambient = vec3(0);
44+
45+
// outColor.rgb = diffuse * (max(0.0, dot(shadingNormal, w_i)) * sunlightColor + ambient) +
46+
// sunlightColor * (0.1 * pow(max(0.0, dot(shadingNormal, w_h)), 6));
47+
48+
// outColor.a = 1.0;
49+
50+
// new
51+
52+
vec3 lightDir = normalize(-sunlightDirection);
53+
vec3 viewDir = normalize(-tObjectInfo.tCameraPos);
54+
vec3 n = normalize(tShaderIn.tPosition);
55+
56+
vec3 radiance = ambient;
57+
58+
float irradiance = max(dot(lightDir, n), 0.0) * irradiPerp;
59+
if(irradiance > 0.0)
60+
{
61+
vec3 brdf = phongBRDF(lightDir, viewDir, n, diffuseColor.rgb, specularColor.rgb, shininess);
62+
radiance += brdf * irradiance * lightColor.rgb;
63+
}
64+
65+
radiance = pow(radiance, vec3(1.0 / 2.2) ); // gamma correction
66+
outColor.rgb = radiance;
67+
outColor.a = 1.0;
68+
}

examples/shaders/solid_single.vert

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#version 450
2+
#extension GL_ARB_separate_shader_objects : enable
3+
#extension GL_EXT_nonuniform_qualifier : enable
4+
5+
6+
layout(set = 3, binding = 0) uniform PL_DYNAMIC_DATA
7+
{
8+
9+
mat4 tCameraViewProjection;
10+
vec3 tCameraPos;
11+
} tObjectInfo;
12+
13+
layout(location = 0) in vec3 inPos;
14+
15+
layout(location = 0) out struct plShaderOut {
16+
vec3 tPosition;
17+
} tShaderIn;
18+
19+
void main()
20+
{
21+
tShaderIn.tPosition = inPos;
22+
gl_Position = tObjectInfo.tCameraViewProjection * vec4(inPos, 1.0);
23+
}

extensions/pl_mesh_ext.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ typedef struct _plMeshBuilder
7171
{
7272
plMeshBuilderOptions tOptions;
7373
plVec3* sbtVertices;
74+
plDVec3* sbtDVertices;
7475
plMeshBuilderTriangle* sbtTriangles;
7576
} plMeshBuilder;
7677

@@ -702,6 +703,100 @@ pl_mesh_builder_commit(plMeshBuilder* ptBuilder, uint32_t* puIndexBuffer, plVec3
702703
}
703704

704705

706+
void
707+
pl_mesh_builder_add_triangle_double(plMeshBuilder* ptBuilder, plDVec3 tA, plDVec3 tB, plDVec3 tC)
708+
{
709+
plMeshBuilderTriangle tTriangle;
710+
tTriangle.uIndex0 = UINT32_MAX;
711+
tTriangle.uIndex1 = UINT32_MAX;
712+
tTriangle.uIndex2 = UINT32_MAX;
713+
714+
const double fWeldRadiusSqr = (double)(ptBuilder->tOptions.fWeldRadius * ptBuilder->tOptions.fWeldRadius);
715+
716+
const uint32_t uVertexCount = pl_sb_size(ptBuilder->sbtDVertices);
717+
718+
for(uint32_t i = 0; i < uVertexCount; i++)
719+
{
720+
const plDVec3* ptVertex = &ptBuilder->sbtDVertices[i];
721+
722+
double fDist = pl_length_sqr_vec3_d(pl_sub_vec3_d(*ptVertex, tA));
723+
724+
if(fDist < fWeldRadiusSqr)
725+
{
726+
tTriangle.uIndex0 = i;
727+
break;
728+
}
729+
}
730+
731+
for(uint32_t i = 0; i < uVertexCount; i++)
732+
{
733+
const plDVec3* ptVertex = &ptBuilder->sbtDVertices[i];
734+
735+
double fDist = pl_length_sqr_vec3_d(pl_sub_vec3_d(*ptVertex, tB));
736+
737+
if(fDist < fWeldRadiusSqr)
738+
{
739+
tTriangle.uIndex1 = i;
740+
break;
741+
}
742+
}
743+
744+
for(uint32_t i = 0; i < uVertexCount; i++)
745+
{
746+
const plDVec3* ptVertex = &ptBuilder->sbtDVertices[i];
747+
748+
double fDist = pl_length_sqr_vec3_d(pl_sub_vec3_d(*ptVertex, tC));
749+
750+
if(fDist < fWeldRadiusSqr)
751+
{
752+
tTriangle.uIndex2 = i;
753+
break;
754+
}
755+
}
756+
757+
if(tTriangle.uIndex0 == UINT32_MAX)
758+
{
759+
tTriangle.uIndex0 = pl_sb_size(ptBuilder->sbtDVertices);
760+
pl_sb_push(ptBuilder->sbtDVertices, tA);
761+
}
762+
763+
if(tTriangle.uIndex1 == UINT32_MAX)
764+
{
765+
tTriangle.uIndex1 = pl_sb_size(ptBuilder->sbtDVertices);
766+
pl_sb_push(ptBuilder->sbtDVertices, tB);
767+
}
768+
769+
if(tTriangle.uIndex2 == UINT32_MAX)
770+
{
771+
tTriangle.uIndex2 = pl_sb_size(ptBuilder->sbtDVertices);
772+
pl_sb_push(ptBuilder->sbtDVertices, tC);
773+
}
774+
775+
pl_sb_push(ptBuilder->sbtTriangles, tTriangle);
776+
}
777+
778+
void
779+
pl_mesh_builder_commit_double(plMeshBuilder* ptBuilder, uint32_t* puIndexBuffer, plDVec3* ptVertexBuffer, uint32_t* puIndexBufferCountOut, uint32_t* puVertexBufferCountOut)
780+
{
781+
const uint32_t uVertexCount = pl_sb_size(ptBuilder->sbtDVertices);
782+
const uint32_t uTriangleCount = pl_sb_size(ptBuilder->sbtTriangles);
783+
784+
if(puVertexBufferCountOut)
785+
*puVertexBufferCountOut = uVertexCount;
786+
787+
if(puIndexBufferCountOut)
788+
*puIndexBufferCountOut = uTriangleCount * 3;
789+
790+
if(puIndexBuffer && ptVertexBuffer)
791+
{
792+
memcpy(puIndexBuffer, ptBuilder->sbtTriangles, uTriangleCount * 3 * sizeof(uint32_t));
793+
memcpy(ptVertexBuffer, ptBuilder->sbtDVertices, uVertexCount * sizeof(plDVec3));
794+
pl_sb_reset(ptBuilder->sbtTriangles);
795+
pl_sb_reset(ptBuilder->sbtDVertices);
796+
}
797+
}
798+
799+
705800
//-----------------------------------------------------------------------------
706801
// [SECTION] extension loading
707802
//-----------------------------------------------------------------------------
@@ -730,6 +825,14 @@ pl_load_mesh_ext(plApiRegistryI* ptApiRegistry, bool bReload)
730825
};
731826
pl_set_api(ptApiRegistry, plMeshBuilderI, &tApi2);
732827

828+
const plMeshBuilderDI tApi3 = {
829+
.create = pl_mesh_builder_create,
830+
.cleanup = pl_mesh_builder_cleanup,
831+
.add_triangle = pl_mesh_builder_add_triangle_double,
832+
.commit = pl_mesh_builder_commit_double,
833+
};
834+
pl_set_api(ptApiRegistry, plMeshBuilderDI, &tApi3);
835+
733836
gptECS = pl_get_api_latest(ptApiRegistry, plEcsI);
734837
gptMemory = pl_get_api_latest(ptApiRegistry, plMemoryI);
735838
gptLog = pl_get_api_latest(ptApiRegistry, plLogI);
@@ -759,4 +862,7 @@ pl_unload_mesh_ext(plApiRegistryI* ptApiRegistry, bool bReload)
759862

760863
const plMeshBuilderI* ptApi2 = pl_get_api_latest(ptApiRegistry, plMeshBuilderI);
761864
ptApiRegistry->remove_api(ptApi2);
865+
866+
const plMeshBuilderDI* ptApi3 = pl_get_api_latest(ptApiRegistry, plMeshBuilderDI);
867+
ptApiRegistry->remove_api(ptApi3);
762868
}

extensions/pl_mesh_ext.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Index of this file:
4141

4242
#define plMeshI_version {0, 1, 0}
4343
#define plMeshBuilderI_version {0, 1, 0}
44+
#define plMeshBuilderDI_version {0, 1, 0}
4445

4546
//-----------------------------------------------------------------------------
4647
// [SECTION] includes
@@ -106,6 +107,20 @@ typedef struct _plMeshBuilderI
106107

107108
} plMeshBuilderI;
108109

110+
typedef struct _plMeshBuilderDI
111+
{
112+
// setup/shutdown
113+
plMeshBuilder* (*create)(plMeshBuilderOptions);
114+
void (*cleanup)(plMeshBuilder*);
115+
116+
// adding
117+
void (*add_triangle)(plMeshBuilder*, plDVec3, plDVec3, plDVec3);
118+
119+
// commit
120+
void (*commit)(plMeshBuilder*, uint32_t* indexBuffer, plDVec3* vertexBuffer, uint32_t* indexBufferCountOut, uint32_t* vertexBufferCountOut);
121+
122+
} plMeshBuilderDI;
123+
109124
//-----------------------------------------------------------------------------
110125
// [SECTION] structs
111126
//-----------------------------------------------------------------------------

extensions/pl_starter_ext.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,7 @@ pl__starter_create_render_pass_with_depth(void)
14011401
.tStencilStoreOp = PL_STORE_OP_DONT_CARE,
14021402
.tCurrentUsage = PL_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT,
14031403
.tNextUsage = PL_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT,
1404-
.fClearZ = 1.0f
1404+
.fClearZ = 0.0f
14051405
},
14061406
.atColorTargets = {
14071407
{
@@ -1493,7 +1493,7 @@ pl__starter_create_render_pass_with_msaa_and_depth(void)
14931493
.tStencilStoreOp = PL_STORE_OP_DONT_CARE,
14941494
.tCurrentUsage = PL_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT,
14951495
.tNextUsage = PL_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT,
1496-
.fClearZ = 1.0f
1496+
.fClearZ = 0.0f
14971497
},
14981498
.tResolveTarget = { // swapchain image
14991499
.tLoadOp = PL_LOAD_OP_DONT_CARE,

0 commit comments

Comments
 (0)