TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ PrepGeomShader()

void ShadingManager::PrepGeomShader ( )
private

Definition at line 489 of file ShadingManager.cpp.

490{
491 gsh->Clear();
492
493 gsh->AddTopLine(GLSLLine("layout(triangles) in;", "Take 3 vertices of a triangles as input"));
494 gsh->AddTopLine(GLSLLine("layout(triangle_strip, max_vertices = 3) out;"));
495 gsh->AddTopLine(GLSLLine("", "The data passed in from the vertex shader"));
496 gsh->AddTopLine(GLSLLine(R"(
497in DATA
498{
499 float height;
500 vec3 FragPos;
501 vec3 Normal;
502 vec2 TexCoord;
503 vec4 Extras1;
504} data_in[];
505)"));
506 gsh->AddTopLine(GLSLLine(""));
507 gsh->AddTopLine(GLSLLine("out float height;", "Pass the height to the fragment shader"));
508 gsh->AddTopLine(GLSLLine("out vec4 FragPos;", "Pass the position to the fragment shader"));
509 gsh->AddTopLine(GLSLLine("out vec4 Extras1;", "Pass the extras to the fragment shader"));
510 gsh->AddTopLine(GLSLLine("out vec3 Normal;", "Pass the normal to the fragment shader"));
511 gsh->AddTopLine(GLSLLine("out vec2 TexCoord;", "Pass the texture coordinates to the fragment shader"));
512 gsh->AddTopLine(GLSLLine("out mat3 TBN;", "Pass the Tangent Bitangent Normal matrix to the fragment shader"));
513
514 gsh->AddUniform(GLSLUniform("_PV", "mat4"));
515 gsh->AddUniform(GLSLUniform("_FlatShade", "float"));
516
517 GLSLFunction main("main");
518 main.AddLine(GLSLLine("vec3 a = (_PV * gl_in[0].gl_Position).xyz;", "Vertex a of triangle"));
519 main.AddLine(GLSLLine("vec3 b = (_PV * gl_in[1].gl_Position).xyz;", "Vertex b of triangle"));
520 main.AddLine(GLSLLine("vec3 c = (_PV * gl_in[2].gl_Position).xyz;", "Vertex c of triangle"));
521 main.AddLine(GLSLLine(""));
522 main.AddLine(GLSLLine("vec3 edge0 = b - c;"));
523 main.AddLine(GLSLLine("vec3 edge1 = c - a;"));
524 main.AddLine(GLSLLine(""));
525 main.AddLine(GLSLLine("vec2 deltaUV0 = data_in[1].TexCoord - data_in[0].TexCoord;"));
526 main.AddLine(GLSLLine("vec2 deltaUV1 = data_in[2].TexCoord - data_in[0].TexCoord;"));
527 main.AddLine(GLSLLine(""));
528 main.AddLine(GLSLLine("float invDet = 1.0f / (deltaUV0.x * deltaUV1.y - deltaUV1.x * deltaUV0.y);", "One over the determinant"));
529 main.AddLine(GLSLLine(""));
530 main.AddLine(GLSLLine("vec3 tangent = vec3(invDet * (deltaUV1.y * edge0 - deltaUV0.y * edge1));"));
531 main.AddLine(GLSLLine("vec3 bitangent = vec3(invDet * (-deltaUV1.x * edge0 + deltaUV0.x * edge1));"));
532 main.AddLine(GLSLLine("mat4 model = mat4(1);", "A default model matrix"));
533 main.AddLine(GLSLLine(""));
534 main.AddLine(GLSLLine("vec3 T = normalize(vec3(model * vec4(tangent, 0.0f)));"));
535 main.AddLine(GLSLLine("vec3 B = normalize(vec3(model * vec4(bitangent, 0.0f)));"));
536 main.AddLine(GLSLLine("vec3 N = normalize(vec3(model * vec4(cross(edge1, edge0), 0.0f)));"));
537 main.AddLine(GLSLLine(""));
538 main.AddLine(GLSLLine("TBN = mat3(T, B, N);"));
539 main.AddLine(GLSLLine(""));
540 main.AddLine(GLSLLine("vec3 n = normalize(cross(edge1, edge0));", "Calculate face normal for flat shading"));
541 main.AddLine(GLSLLine(""));
542
543 for(int i=0;i<3;i++)
544 {
545 main.AddLine(GLSLLine("gl_Position = _PV * gl_in[" + std::to_string(i) + "].gl_Position;"));
546 main.AddLine(GLSLLine("height = data_in[" + std::to_string(i) + "].height;"));
547 main.AddLine(GLSLLine("TexCoord = data_in[" + std::to_string(i) + "].TexCoord;"));
548 main.AddLine(GLSLLine("Extras1 = data_in[" + std::to_string(i) + "].Extras1;"));
549 main.AddLine(GLSLLine("FragPos = vec4(data_in[" + std::to_string(i) + "].FragPos, 1.0f);"));
550 main.AddLine(GLSLLine(R"(
551 if(_FlatShade > 0.5f)
552 Normal = n;
553 else
554 Normal = data_in[)" + std::to_string(i) + R"(].Normal;
555 )"));
556 main.AddLine(GLSLLine("EmitVertex();", "Emit the vertex"));
557}
558
559main.AddLine(GLSLLine("EndPrimitive();", "Emit the triangle"));
560
561
562gsh->AddFunction(main);
563
564}