From 789934b8f5d76e92a46b9c0115a586f30bfe02bb Mon Sep 17 00:00:00 2001 From: andrew morton Date: Sun, 8 Jan 2017 14:35:31 -0700 Subject: [PATCH] Make normal coloring separate shader --- include/Resources.h | 2 ++ resources/direction_colored.frag | 12 ++++++------ resources/direction_colored.vert | 1 - resources/normal_colored.frag | 12 ++++++++++++ resources/normal_colored.vert | 16 ++++++++++++++++ xcode/Cityscape.xcodeproj/project.pbxproj | 12 ++++++++++-- 6 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 resources/normal_colored.frag create mode 100644 resources/normal_colored.vert diff --git a/include/Resources.h b/include/Resources.h index ab5d459..702c3b5 100644 --- a/include/Resources.h +++ b/include/Resources.h @@ -1,6 +1,8 @@ #pragma once #include "cinder/CinderResources.h" +#define RES_NORMAL_VERT CINDER_RESOURCE( ../resources/, normal_colored.vert, 127, GLSL ) +#define RES_NORMAL_FRAG CINDER_RESOURCE( ../resources/, normal_colored.frag, 128, GLSL ) #define RES_BUILDING_VERT CINDER_RESOURCE( ../resources/, direction_colored.vert, 129, GLSL ) #define RES_BUILDING_FRAG CINDER_RESOURCE( ../resources/, direction_colored.frag, 130, GLSL ) #define RES_TREE_VERT CINDER_RESOURCE( ../resources/, tree.vert, 131, GLSL ) diff --git a/resources/direction_colored.frag b/resources/direction_colored.frag index c1e7ddd..5e8ecc9 100644 --- a/resources/direction_colored.frag +++ b/resources/direction_colored.frag @@ -10,17 +10,17 @@ out vec4 oColor; void main() { vec3 eyespaceNormal = normalize(cross(dFdx(eyespacePosition), dFdy(eyespacePosition))); -// oColor = vec4(eyespaceNormal, 1.0); + // Stylized if (eyespaceNormal.y > 0.5) { oColor = vec4(darkColor, 1); - } - else if (eyespaceNormal.x > 0.25) { + } else if (eyespaceNormal.x > 0.25) { oColor = vec4(mediumColor, 1); - } - else { + } else { oColor = vec4(lightColor, 1); } + +// // Grayscale // float sum = (eyespaceNormal.x + eyespaceNormal.y + eyespaceNormal.z) / 3.0; // oColor = vec4(sum, sum, sum, 1.0); -} \ No newline at end of file +} diff --git a/resources/direction_colored.vert b/resources/direction_colored.vert index bfa9829..59b5a94 100644 --- a/resources/direction_colored.vert +++ b/resources/direction_colored.vert @@ -12,6 +12,5 @@ void main() { eyespacePosition = (ciModelView * ciPosition).xyz; -// gl_FrontColor = gl_Color * gl_Color; gl_Position = ciModelViewProjection * vInstanceModelMatrix * ciPosition; } diff --git a/resources/normal_colored.frag b/resources/normal_colored.frag new file mode 100644 index 0000000..d64d34a --- /dev/null +++ b/resources/normal_colored.frag @@ -0,0 +1,12 @@ +#version 150 + +in vec3 eyespacePosition; +out vec4 oColor; + +void main() +{ + vec3 eyespaceNormal = normalize(cross(dFdx(eyespacePosition), dFdy(eyespacePosition))); + + // Color based on normal + oColor = vec4(eyespaceNormal, 0.75); +} diff --git a/resources/normal_colored.vert b/resources/normal_colored.vert new file mode 100644 index 0000000..59b5a94 --- /dev/null +++ b/resources/normal_colored.vert @@ -0,0 +1,16 @@ +#version 150 + +uniform mat4 ciModelView; +uniform mat4 ciModelViewProjection; + +in vec4 ciPosition; +in mat4 vInstanceModelMatrix; // per-instance position variable +in vec4 vInstanceColor; // ignored +out vec3 eyespacePosition; + +void main() +{ + eyespacePosition = (ciModelView * ciPosition).xyz; + + gl_Position = ciModelViewProjection * vInstanceModelMatrix * ciPosition; +} diff --git a/xcode/Cityscape.xcodeproj/project.pbxproj b/xcode/Cityscape.xcodeproj/project.pbxproj index 22c7582..04faf3a 100644 --- a/xcode/Cityscape.xcodeproj/project.pbxproj +++ b/xcode/Cityscape.xcodeproj/project.pbxproj @@ -16,6 +16,8 @@ 494BE6AB1C8B866500E50477 /* RoadBuilder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 494BE6A91C8B866500E50477 /* RoadBuilder.cpp */; }; 494BE6AD1C8B9D9100E50477 /* CityData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 494BE6AC1C8B9D9100E50477 /* CityData.cpp */; }; 497EFE111C4A0D240053D720 /* GeometryHelpers.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 497EFE101C4A0D240053D720 /* GeometryHelpers.cpp */; }; + 498027E41E22E68400CB5CA4 /* normal_colored.frag in Resources */ = {isa = PBXBuildFile; fileRef = 498027E31E22E68400CB5CA4 /* normal_colored.frag */; }; + 498027E61E22E6A000CB5CA4 /* normal_colored.vert in Resources */ = {isa = PBXBuildFile; fileRef = 498027E51E22E6A000CB5CA4 /* normal_colored.vert */; }; 4983C3731C8954A200531A46 /* tree.frag in Resources */ = {isa = PBXBuildFile; fileRef = 4983C3711C8954A200531A46 /* tree.frag */; }; 4983C3741C8954A200531A46 /* tree.vert in Resources */ = {isa = PBXBuildFile; fileRef = 4983C3721C8954A200531A46 /* tree.vert */; }; 49A6B2E01ABBFD3E000EA750 /* TODO.md in Resources */ = {isa = PBXBuildFile; fileRef = 49A6B2DF1ABBFD3E000EA750 /* TODO.md */; }; @@ -100,6 +102,8 @@ 495222581C7ED78700FC88B7 /* CgalPolygon.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CgalPolygon.h; sourceTree = ""; }; 497EFE0F1C4A0D0D0053D720 /* GeometryHelpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GeometryHelpers.h; sourceTree = ""; }; 497EFE101C4A0D240053D720 /* GeometryHelpers.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = GeometryHelpers.cpp; path = ../src/GeometryHelpers.cpp; sourceTree = ""; }; + 498027E31E22E68400CB5CA4 /* normal_colored.frag */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = normal_colored.frag; path = ../resources/normal_colored.frag; sourceTree = ""; }; + 498027E51E22E6A000CB5CA4 /* normal_colored.vert */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = normal_colored.vert; path = ../resources/normal_colored.vert; sourceTree = ""; }; 4983C3711C8954A200531A46 /* tree.frag */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = tree.frag; path = ../resources/tree.frag; sourceTree = ""; }; 4983C3721C8954A200531A46 /* tree.vert */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = tree.vert; path = ../resources/tree.vert; sourceTree = ""; }; 49A6B2DF1ABBFD3E000EA750 /* TODO.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = TODO.md; path = ../TODO.md; sourceTree = ""; }; @@ -320,10 +324,12 @@ children = ( 5FD4C4991D87BF8000EF8E51 /* car_01.obj */, 5F27C11D1D84625700EC1F03 /* plane.obj */, - 4983C3721C8954A200531A46 /* tree.vert */, - 4983C3711C8954A200531A46 /* tree.frag */, 49E959211B23EC340093E4C8 /* direction_colored.vert */, 49E959231B23EC4D0093E4C8 /* direction_colored.frag */, + 498027E51E22E6A000CB5CA4 /* normal_colored.vert */, + 498027E31E22E68400CB5CA4 /* normal_colored.frag */, + 4983C3721C8954A200531A46 /* tree.vert */, + 4983C3711C8954A200531A46 /* tree.frag */, 0AE48136C1644371A1A5B3C8 /* CinderApp.icns */, ACB4D9448A764F68A540CED0 /* Info.plist */, ); @@ -462,12 +468,14 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 498027E61E22E6A000CB5CA4 /* normal_colored.vert in Resources */, 49A6B2E01ABBFD3E000EA750 /* TODO.md in Resources */, 5FD4C49A1D87BF8000EF8E51 /* car_01.obj in Resources */, 49E959241B23EC4D0093E4C8 /* direction_colored.frag in Resources */, 5F27C11E1D84625700EC1F03 /* plane.obj in Resources */, 4983C3741C8954A200531A46 /* tree.vert in Resources */, 49E959221B23EC340093E4C8 /* direction_colored.vert in Resources */, + 498027E41E22E68400CB5CA4 /* normal_colored.frag in Resources */, A27A70ECAE7549F98B411A74 /* CinderApp.icns in Resources */, 4983C3731C8954A200531A46 /* tree.frag in Resources */, );