diff options
author | chai <chaifix@163.com> | 2020-07-14 20:54:35 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2020-07-14 20:54:35 +0800 |
commit | b0235d7a0bac4994da7102887ec0ba1cd613769f (patch) | |
tree | d7315b8e99b8781e95cf9cbf016f10c989ce7afb | |
parent | efe7768e5b65022b2dd418feff017b97012a6b24 (diff) |
*misc
-rw-r--r-- | Release/SoftShadeRoom.exe | bin | 130560 -> 128512 bytes | |||
-rw-r--r-- | SoftShadeRoom/SoftShadeRoom.vcxproj | 11 | ||||
-rw-r--r-- | SoftShadeRoom/SoftShadeRoom.vcxproj.filters | 36 | ||||
-rw-r--r-- | src/core/rasterizer.c | 8 | ||||
-rw-r--r-- | src/core/shader.c | 2 | ||||
-rw-r--r-- | src/core/shader.h | 2 | ||||
-rw-r--r-- | src/example/04_bloom/postprocess.c | 2 | ||||
-rw-r--r-- | src/example/04_bloom/preprocess.c | 2 | ||||
-rw-r--r-- | src/extend/mesh.c | 149 | ||||
-rw-r--r-- | src/extend/mesh.h | 7 | ||||
-rw-r--r-- | src/gizmo/gizmo.c | 2 | ||||
-rw-r--r-- | src/shaders/default.c | 2 | ||||
-rw-r--r-- | src/shaders/pbr.c | 2 | ||||
-rw-r--r-- | src/shaders/unlit.c | 2 |
14 files changed, 203 insertions, 24 deletions
diff --git a/Release/SoftShadeRoom.exe b/Release/SoftShadeRoom.exe Binary files differindex 54a81e1..75efd3d 100644 --- a/Release/SoftShadeRoom.exe +++ b/Release/SoftShadeRoom.exe diff --git a/SoftShadeRoom/SoftShadeRoom.vcxproj b/SoftShadeRoom/SoftShadeRoom.vcxproj index 87b5a64..c0808e9 100644 --- a/SoftShadeRoom/SoftShadeRoom.vcxproj +++ b/SoftShadeRoom/SoftShadeRoom.vcxproj @@ -177,7 +177,10 @@ <ClCompile Include="..\src\math\vec2.c" /> <ClCompile Include="..\src\math\vec3.c" /> <ClCompile Include="..\src\math\vec4.c" /> - <ClCompile Include="..\src\shaders\common.c" /> + <ClCompile Include="..\src\shaders\common\core.c" /> + <ClCompile Include="..\src\shaders\common\light.c" /> + <ClCompile Include="..\src\shaders\common\mathlib.c" /> + <ClCompile Include="..\src\shaders\common\shadow.c" /> <ClCompile Include="..\src\shaders\default.c" /> <ClCompile Include="..\src\shaders\pbr.c" /> <ClCompile Include="..\src\shaders\unlit.c" /> @@ -211,8 +214,12 @@ <ClInclude Include="..\src\extern\wog.h" /> <ClInclude Include="..\src\gizmo\gizmo.h" /> <ClInclude Include="..\src\math\math.h" /> + <ClInclude Include="..\src\math\structs.h" /> <ClInclude Include="..\src\settings.h" /> - <ClInclude Include="..\src\shaders\common.h" /> + <ClInclude Include="..\src\shaders\common\core.h" /> + <ClInclude Include="..\src\shaders\common\light.h" /> + <ClInclude Include="..\src\shaders\common\mathlib.h" /> + <ClInclude Include="..\src\shaders\common\shadow.h" /> <ClInclude Include="..\src\ssr.h" /> <ClInclude Include="..\src\test\test.h" /> <ClInclude Include="..\src\util\assert.h" /> diff --git a/SoftShadeRoom/SoftShadeRoom.vcxproj.filters b/SoftShadeRoom/SoftShadeRoom.vcxproj.filters index 110c667..2c33500 100644 --- a/SoftShadeRoom/SoftShadeRoom.vcxproj.filters +++ b/SoftShadeRoom/SoftShadeRoom.vcxproj.filters @@ -46,6 +46,9 @@ <Filter Include="extend"> <UniqueIdentifier>{06f04c83-7b9f-4e48-8bbb-7427f537ff3b}</UniqueIdentifier> </Filter> + <Filter Include="shaders\common"> + <UniqueIdentifier>{5544216a-5490-445d-8d05-617c883d23e7}</UniqueIdentifier> + </Filter> </ItemGroup> <ItemGroup> <ClCompile Include="..\src\main.c" /> @@ -118,9 +121,6 @@ <ClCompile Include="..\src\gizmo\icon2d.c"> <Filter>gizmo</Filter> </ClCompile> - <ClCompile Include="..\src\shaders\common.c"> - <Filter>shaders</Filter> - </ClCompile> <ClCompile Include="..\src\shaders\pbr.c"> <Filter>shaders</Filter> </ClCompile> @@ -166,6 +166,18 @@ <ClCompile Include="..\src\extend\light.c"> <Filter>extend</Filter> </ClCompile> + <ClCompile Include="..\src\shaders\common\mathlib.c"> + <Filter>shaders\common</Filter> + </ClCompile> + <ClCompile Include="..\src\shaders\common\core.c"> + <Filter>shaders\common</Filter> + </ClCompile> + <ClCompile Include="..\src\shaders\common\light.c"> + <Filter>shaders\common</Filter> + </ClCompile> + <ClCompile Include="..\src\shaders\common\shadow.c"> + <Filter>shaders\common</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="..\src\math\math.h"> @@ -238,9 +250,6 @@ <ClInclude Include="..\src\core\framebuffer.h"> <Filter>core</Filter> </ClInclude> - <ClInclude Include="..\src\shaders\common.h"> - <Filter>shaders</Filter> - </ClInclude> <ClInclude Include="..\src\core\depth.h"> <Filter>core</Filter> </ClInclude> @@ -258,5 +267,20 @@ <Filter>extend</Filter> </ClInclude> <ClInclude Include="..\src\settings.h" /> + <ClInclude Include="..\src\shaders\common\mathlib.h"> + <Filter>shaders\common</Filter> + </ClInclude> + <ClInclude Include="..\src\shaders\common\core.h"> + <Filter>shaders\common</Filter> + </ClInclude> + <ClInclude Include="..\src\math\structs.h"> + <Filter>math</Filter> + </ClInclude> + <ClInclude Include="..\src\shaders\common\light.h"> + <Filter>shaders\common</Filter> + </ClInclude> + <ClInclude Include="..\src\shaders\common\shadow.h"> + <Filter>shaders\common</Filter> + </ClInclude> </ItemGroup> </Project>
\ No newline at end of file diff --git a/src/core/rasterizer.c b/src/core/rasterizer.c index 0c78893..28ae639 100644 --- a/src/core/rasterizer.c +++ b/src/core/rasterizer.c @@ -82,8 +82,6 @@ static void puttriangle(Vec2* A, Vec2* B, Vec2* C, Color c) { ssrR_putline(C->x, C->y, B->x, B->y, c); } -extern FragmentShaderIn ssr_frag_in; - void ssrR_triangle( Vec4* CA, Vec4* CB, Vec4* CC, uint IA, uint IB, uint IC, @@ -176,7 +174,7 @@ void ssrR_triangle( /*interpolate varying variables*/ \ ssrS_solveregsbcp(&bc, IA, IB, IC); \ /*enter fragment shader*/ \ - discard = !frag_shader(uniforms, &ssr_frag_in, out_color[0]); \ + discard = !frag_shader(uniforms, out_color[0]); \ discardif(discard); \ /*put point*/ \ ssr_blendandputpoint(p.x, p.y, blend); \ @@ -308,7 +306,7 @@ void ssrR_line( discardif(!pass_depth_test || !pass_stencil_test); /*solve registers with lerp*/ ssrS_solveregslerp(t, IA, IB); - discard = !frag_shader(uniforms, &ssr_frag_in, out_color[0]); + discard = !frag_shader(uniforms, out_color[0]); discardif(discard); /*put point*/ ssr_blendandputpoint(px, py, blend); @@ -354,7 +352,7 @@ void ssrR_point(Vec4* CA, uint IA, Program* program, UniformCollection* uniforms } discardif(!pass_depth_test || !pass_stencil_test); ssrS_solveregscopy(IA); - discard = !frag_shader(uniforms, &ssr_frag_in, out_color[0]); + discard = !frag_shader(uniforms, out_color[0]); discardif(discard); /*put point*/ ssr_blendandputpoint(px, py, blend); diff --git a/src/core/shader.c b/src/core/shader.c index b523df7..c0ca5d0 100644 --- a/src/core/shader.c +++ b/src/core/shader.c @@ -5,6 +5,8 @@ extern FragmentShaderIn ssr_frag_in; +// LUTs + Register registers[REG_TOTAL] = { /*4 float registers*/ { 0, REGTYPE_NUM, sizeof(float), NULL }, diff --git a/src/core/shader.h b/src/core/shader.h index 70b4ce9..71f26e4 100644 --- a/src/core/shader.h +++ b/src/core/shader.h @@ -51,7 +51,7 @@ typedef struct { Vec4 v4[REG_V4_COUNT]; } FragmentShaderIn; -typedef bool(*FragmentShader)(UniformCollection* uniforms, FragmentShaderIn* in, Color32* color); +typedef bool(*FragmentShader)(UniformCollection* uniforms, Color32* color); typedef struct { VertexShader vertexshader; diff --git a/src/example/04_bloom/postprocess.c b/src/example/04_bloom/postprocess.c index ca35594..c305726 100644 --- a/src/example/04_bloom/postprocess.c +++ b/src/example/04_bloom/postprocess.c @@ -15,7 +15,7 @@ static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* clipcoor *_texcoord = in->vertex->texcoord; } -static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color32* color) { +static bool frag(UniformCollection* uniforms, Color32* color) { float off = 1 / 600.f; //blur *color = tex2d(_frag_tex, _texcoord); diff --git a/src/example/04_bloom/preprocess.c b/src/example/04_bloom/preprocess.c index 9e5bb55..8dd2170 100644 --- a/src/example/04_bloom/preprocess.c +++ b/src/example/04_bloom/preprocess.c @@ -15,7 +15,7 @@ static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* clipcoor #define _frag_color out_color[0] #define _bright_color out_color[1] -static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color32* color) { +static bool frag(UniformCollection* uniforms, Color32* color) { //_frag_color->r = 1; //_frag_color->g = 1; //_frag_color->b = 1; diff --git a/src/extend/mesh.c b/src/extend/mesh.c index a825860..1f393db 100644 --- a/src/extend/mesh.c +++ b/src/extend/mesh.c @@ -1,8 +1,9 @@ #include "mesh.h" +#define GENERATE_TANGNET //#define GENERATE_NORMAL +//#define OPTIMIZE_MESH -#define GENERATE_TANGNET #define LINE_SIZE 1024 @@ -129,6 +130,9 @@ static Mesh *build_mesh( /* obj loader */ /************************************************************************/ +#define SIMPLE_OBJ_LOADER 0 + +#if SIMPLE_OBJ_LOADER Mesh* mesh_loadfromobj(const char *filename) { Vec3 *positions = NULL; Vec2 *texcoords = NULL; @@ -211,5 +215,148 @@ Mesh* mesh_loadfromobj(const char *filename) { return mesh; } +#else + +typedef struct { + int normal_index; + int uv_index; + int index; +} VertexInfo; + +typedef struct { + VertexInfo* vertices; // vertices list +} VertexListElement; + +typedef struct { + int posIndex, normalIndex, uvIndex; +} FaceVertex; + +typedef struct { + FaceVertex vertices[3]; +} FaceInfo; +Mesh* mesh_loadfromobj(const char* path) { + Vec3* position_list = NULL; + Vec3* normal_list = NULL; + Vec2* uv_list = NULL; + VertexListElement* vertex_list = NULL; + FaceInfo* face_list = NULL; + FILE *file; + file = fopen(path, "rb"); + assert(file != NULL); + char line[LINE_SIZE]; + + while (1) { + int items; + if (fgets(line, LINE_SIZE, file) == NULL) { + break; + } + else if (strncmp(line, "v ", 2) == 0) { /* position */ + Vec3 position; + items = sscanf(line, "v %f %f %f", + &position.x, &position.y, &position.z); + assert(items == 3); + darray_push(position_list, position); + // init vertex_list + VertexListElement element = { 0 }; + darray_push(vertex_list, element); + } + else if (strncmp(line, "vt ", 3) == 0) { /* texcoord */ + Vec2 texcoord; + items = sscanf(line, "vt %f %f", + &texcoord.x, &texcoord.y); + assert(items == 2); + darray_push(uv_list, texcoord); + } + else if (strncmp(line, "vn ", 3) == 0) { /* normal */ + Vec3 normal; + items = sscanf(line, "vn %f %f %f", + &normal.x, &normal.y, &normal.z); + assert(items == 3); + darray_push(normal_list, normal); + } + else if (strncmp(line, "f ", 2) == 0) { /* face */ + FaceInfo face; + items = sscanf(line, "f %d/%d/%d %d/%d/%d %d/%d/%d", // only support triangle + &face.vertices[0].posIndex, &face.vertices[0].uvIndex, &face.vertices[0].normalIndex, + &face.vertices[1].posIndex, &face.vertices[1].uvIndex, &face.vertices[1].normalIndex, + &face.vertices[2].posIndex, &face.vertices[2].uvIndex, &face.vertices[2].normalIndex); + assert(items == 9); + darray_push(face_list, face); + } + else + { + // skip + } + } + + int vert_counts = darray_size(vertex_list); + int faceCount = darray_size(face_list); + int normalCount = darray_size(normal_list); + int uvCount = darray_size(uv_list); + + int vert_count = 0; + + Mesh temp_mesh = {0}; + + for (int i = 0; i < darray_size(face_list); ++i) + { + FaceInfo* face = &face_list[i]; + for (int j = 0; j < 3; ++j) + { + FaceVertex face_vert = face->vertices[j]; + int pos_index = face_vert.posIndex - 1; + int normal_index = face_vert.normalIndex - 1; + int uv_index = face_vert.uvIndex - 1; + VertexListElement* vertex = &vertex_list[pos_index]; + if (vertex->vertices) + { + for (int j = 0; j < darray_size(vertex->vertices); ++j) + { + VertexInfo* vert = &vertex->vertices[j]; + if (vert->normal_index == normal_index && vert->uv_index == uv_index) + { + darray_push(temp_mesh.triangles, vert->index); + goto next; + } + } + } + + // create seam + + VertexInfo new_vert; + new_vert.normal_index = normal_index; + new_vert.uv_index = uv_index; + new_vert.index = vert_count; + + darray_push(vertex->vertices, new_vert); + + Vert vert; + vert.position = position_list[pos_index]; + vert.normal = normal_list[normal_index]; + vert.texcoord = uv_list[uv_index]; + vert.index = vert_count; + darray_push(temp_mesh.vertices, vert); + + darray_push(temp_mesh.triangles, vert_count); + + ++temp_mesh.vert_count; + + ++vert_count; + + next: continue; + } + + ++temp_mesh.tris_count; + } + + Mesh* mesh = malloc(sizeof(Mesh)); + mesh->triangles = temp_mesh.triangles; + mesh->vertices = temp_mesh.vertices; + mesh->tris_count = temp_mesh.tris_count; + mesh->vert_count = temp_mesh.vert_count; + + return mesh; +} +#endif
\ No newline at end of file diff --git a/src/extend/mesh.h b/src/extend/mesh.h index aa2a398..b9b07f8 100644 --- a/src/extend/mesh.h +++ b/src/extend/mesh.h @@ -3,10 +3,11 @@ #include "../core/vert.h" -// typedef struct Mesh { - Vert* vertices; uint vert_count; - uint* triangles; uint tris_count; + Vert* vertices; + uint vert_count; + uint* triangles; + uint tris_count; } Mesh; Mesh* mesh_loadfromobj(const char* path); diff --git a/src/gizmo/gizmo.c b/src/gizmo/gizmo.c index 3069cb4..20c28e1 100644 --- a/src/gizmo/gizmo.c +++ b/src/gizmo/gizmo.c @@ -52,7 +52,7 @@ static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* clipcoor object2clip(&p, clipcoord); color_tocolor32(in->vertex->color, _color); } -static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color32* color) { +static bool frag(UniformCollection* uniforms, Color32* color) { *color = *_color; return 1; } diff --git a/src/shaders/default.c b/src/shaders/default.c index d348cdf..e05d15d 100644 --- a/src/shaders/default.c +++ b/src/shaders/default.c @@ -25,7 +25,7 @@ static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* clipcoor *rough = 1 - internal_vec3_dot(&worldnormal, light); } -static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color32* color) { +static bool frag(UniformCollection* uniforms, Color32* color) { internal_vec3_normalize(light, light); float roughness = *rough; //(*color).r = 1; diff --git a/src/shaders/pbr.c b/src/shaders/pbr.c index b8ee4be..e4afbf4 100644 --- a/src/shaders/pbr.c +++ b/src/shaders/pbr.c @@ -31,7 +31,7 @@ static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* clipcoor _clip_pos->y = clipcoord->w; } -static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Vec4* color) { +static bool frag(UniformCollection* uniforms, Vec4* color) { //internal_vec3_normalize(light, light); //internal_vec3_normalize(vnormal, vnormal); //float roughness = *rough; diff --git a/src/shaders/unlit.c b/src/shaders/unlit.c index 5e1b1c1..0b92c8c 100644 --- a/src/shaders/unlit.c +++ b/src/shaders/unlit.c @@ -22,7 +22,7 @@ static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* clipcoor *reg_num_00 = 1 - internal_vec3_dot(&worldnormal, light); } -static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color32* color) { +static bool frag(UniformCollection* uniforms, Color32* color) { internal_vec3_normalize(light, light); return 1; } |