From f86fc26703aed5bec3510273bc4a10f127776683 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Fri, 16 Nov 2018 12:47:55 +0100 Subject: [PATCH] Fix shader problems --- exercise1/include/Viewer.h | 2 +- exercise2/glsl/old_sky.frag | 21 ------------ exercise2/glsl/old_sky.vert | 19 ----------- exercise2/glsl/old_terrain.frag | 49 ---------------------------- exercise2/glsl/old_terrain.vert | 58 --------------------------------- exercise2/glsl/terrain.frag | 2 +- exercise2/glsl/terrain.vert | 6 ++-- exercise2/src/Viewer.cpp | 42 +++++++++++++++++++----- 8 files changed, 39 insertions(+), 160 deletions(-) delete mode 100644 exercise2/glsl/old_sky.frag delete mode 100644 exercise2/glsl/old_sky.vert delete mode 100644 exercise2/glsl/old_terrain.frag delete mode 100644 exercise2/glsl/old_terrain.vert diff --git a/exercise1/include/Viewer.h b/exercise1/include/Viewer.h index 8ee0c42..e9c4f25 100644 --- a/exercise1/include/Viewer.h +++ b/exercise1/include/Viewer.h @@ -26,7 +26,7 @@ private: nanogui::Slider* sldJuliaCY; nanogui::Slider* sldJuliaZoom; //Zoom factor for the Julia fractal - nanogui::Slider* point1X; + nanogui::Slider* point1X; nanogui::Slider* point1Y; nanogui::Slider* point2X; nanogui::Slider* point2Y; diff --git a/exercise2/glsl/old_sky.frag b/exercise2/glsl/old_sky.frag deleted file mode 100644 index 839a02e..0000000 --- a/exercise2/glsl/old_sky.frag +++ /dev/null @@ -1,21 +0,0 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! -// Copyright (C) CGV TU Dresden - All Rights Reserved -#version 330 - -out vec4 color; - -in vec4 clipPos; - -const vec4 horizon = vec4(0.85, 0.85, 0.8, 1.0); -const vec4 floor = vec4(0.1, 0.1, 0.1, 1.0); -const vec4 sky = vec4(0.5, 0.6, 0.8, 1.0); - -void main() -{ - float h = normalize(clipPos.xyz).y; - if(h < 0) - color = mix(horizon, floor, pow(-h, 0.5)); - else - color = mix(horizon, sky, pow(h, 0.9)); -} \ No newline at end of file diff --git a/exercise2/glsl/old_sky.vert b/exercise2/glsl/old_sky.vert deleted file mode 100644 index b154cce..0000000 --- a/exercise2/glsl/old_sky.vert +++ /dev/null @@ -1,19 +0,0 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! -// Copyright (C) CGV TU Dresden - All Rights Reserved -#version 330 - -uniform mat4 mvp; - -out vec4 clipPos; - -void main() -{ - clipPos = vec4( float((gl_VertexID << 1) & 2) - 1.0, - float((gl_VertexID + 1) & 2) - 1.0, - float( gl_VertexID & 2) - 1.0, - 1.0); - - gl_Position = mvp * clipPos; - gl_Position.z = 0.5 * gl_Position.w; -} \ No newline at end of file diff --git a/exercise2/glsl/old_terrain.frag b/exercise2/glsl/old_terrain.frag deleted file mode 100644 index 68e8105..0000000 --- a/exercise2/glsl/old_terrain.frag +++ /dev/null @@ -1,49 +0,0 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! -// Copyright (C) CGV TU Dresden - All Rights Reserved -#version 330 - - - -out vec4 color; - -uniform vec3 cameraPos; - - -uniform sampler2D background; -uniform vec2 screenSize; - -const vec3 dirToLight = normalize(vec3(1, 3, 1)); - -//Calculates the visible surface color based on the Blinn-Phong illumination model -vec4 calculateLighting(vec4 materialColor, float specularIntensity, vec3 normalizedNormal, vec3 directionToViewer) -{ - vec4 color = materialColor; - vec3 h = normalize(dirToLight + directionToViewer); - color.xyz *= 0.9 * max(dot(normalizedNormal, dirToLight), 0) + 0.1; - color.xyz += specularIntensity * pow(max(dot(h, normalizedNormal), 0), 50); - return color; -} - -vec4 getBackgroundColor() -{ - return texture(background, gl_FragCoord.xy / screenSize); -} - -void main() -{ - //surface geometry - vec3 n = vec3(0, 1, 0); - vec3 dirToViewer = vec3(0, 1, 0); - - //material properties - color = vec4(0.6, 0.6, 0.6, 1); - float specular = 0; - - - - //Calculate light - color = calculateLighting(color, specular, n, dirToViewer); - - -} \ No newline at end of file diff --git a/exercise2/glsl/old_terrain.vert b/exercise2/glsl/old_terrain.vert deleted file mode 100644 index 79f3139..0000000 --- a/exercise2/glsl/old_terrain.vert +++ /dev/null @@ -1,58 +0,0 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! -// Copyright (C) CGV TU Dresden - All Rights Reserved -#version 330 - -in vec4 position; - - - -uniform mat4 mvp; - -//Returns the height of the procedural terrain at a given xz position -float getTerrainHeight(vec2 p); - - - -void main() -{ - gl_Position = mvp * position; -} - -//source: https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83 -float rand(vec2 c) -{ - return 2 * fract(sin(dot(c.xy ,vec2(12.9898,78.233))) * 43758.5453) - 1; -} - -float perlinNoise(vec2 p ) -{ - vec2 ij = floor(p); - vec2 xy = p - ij; - //xy = 3.*xy*xy-2.*xy*xy*xy; - xy = .5*(1.-cos(3.1415926 * xy)); - float a = rand((ij+vec2(0.,0.))); - float b = rand((ij+vec2(1.,0.))); - float c = rand((ij+vec2(0.,1.))); - float d = rand((ij+vec2(1.,1.))); - float x1 = mix(a, b, xy.x); - float x2 = mix(c, d, xy.x); - return mix(x1, x2, xy.y); -} - -//based on https://www.seedofandromeda.com/blogs/58-procedural-heightmap-terrain-generation -float getTerrainHeight(vec2 p) -{ - float total = 0.0; - float maxAmplitude = 0.0; - float amplitude = 1.0; - float frequency = 0.02; - for (int i = 0; i < 11; i++) - { - total += ((1.0 - abs(perlinNoise(p * frequency))) * 2.0 - 1.0) * amplitude; - frequency *= 2.0; - maxAmplitude += amplitude; - amplitude *= 0.45; - } - return 15 * total / maxAmplitude; -} \ No newline at end of file diff --git a/exercise2/glsl/terrain.frag b/exercise2/glsl/terrain.frag index dc3227e..b994002 100644 --- a/exercise2/glsl/terrain.frag +++ b/exercise2/glsl/terrain.frag @@ -1,4 +1,4 @@ -#version 330 +#version 330 // This source code is property of the Computer Graphics and Visualization // chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved diff --git a/exercise2/glsl/terrain.vert b/exercise2/glsl/terrain.vert index 5eb2daa..fda93aa 100644 --- a/exercise2/glsl/terrain.vert +++ b/exercise2/glsl/terrain.vert @@ -1,6 +1,6 @@ -#version 330 -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +#version 330 +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved in vec4 position; diff --git a/exercise2/src/Viewer.cpp b/exercise2/src/Viewer.cpp index 7118611..a41ba9f 100644 --- a/exercise2/src/Viewer.cpp +++ b/exercise2/src/Viewer.cpp @@ -14,9 +14,11 @@ #include -#include "glsl.h" +#include "glsl.h" #include "textures.h" +#include + const uint32_t PATCH_SIZE = 256; //number of vertices along one side of the terrain patch Viewer::Viewer() @@ -55,10 +57,34 @@ bool Viewer::resizeEvent(const Eigen::Vector2i&) return false; } +inline std::string loadShaderText(const char *path) +{ + std::ifstream is(path); + std::string s_save; + + if (is.is_open()) + { + s_save.assign(std::istreambuf_iterator(is), std::istreambuf_iterator()); + } + else + { + std::cout << "could not open " << path << std::endl; + } + + is.close(); + return s_save; +} + void Viewer::LoadShaders() { - skyShader.init("Sky Shader", std::string((const char*)sky_vert, sky_vert_size), std::string((const char*)sky_frag, sky_frag_size)); - terrainShader.init("Terrain Shader", std::string((const char*)terrain_vert, terrain_vert_size), std::string((const char*)terrain_frag, terrain_frag_size)); + std::string sky_vs = loadShaderText("exercise2/glsl/sky.vert"); + std::string sky_fs = loadShaderText("exercise2/glsl/sky.frag"); + + std::string terrain_vs = loadShaderText("exercise2/glsl/terrain.vert"); + std::string terrain_fs = loadShaderText("exercise2/glsl/terrain.frag"); + + skyShader.init("Sky Shader", sky_vs, sky_fs); + terrainShader.init("Terrain Shader", terrain_vs, terrain_fs); } GLuint CreateTexture(const unsigned char* fileData, size_t fileLength, bool repeat = true) @@ -88,7 +114,7 @@ void Viewer::CreateGeometry() for (int i = 0; i < PATCH_SIZE; i++) { for (int j = 0; j < PATCH_SIZE; j++) { - positions.emplace_back((float)i, (float)j, 0.0f, 1.0f); + positions.emplace_back((float)i, 0.0f, (float)j, 1.0f); } } @@ -107,8 +133,8 @@ void Viewer::CreateGeometry() indices.emplace_back(k); indices.emplace_back(k + PATCH_SIZE); - indices.emplace_back(k + PATCH_SIZE + 1); - indices.emplace_back(k + 1); + //indices.emplace_back(k + 1); + //indices.emplace_back(k + 1 + PATCH_SIZE); } terrainShader.bind(); @@ -199,8 +225,8 @@ void Viewer::drawContents() terrainShader.setUniform("cameraPos", cameraPosition, false); /* Task: Render the terrain */ - glPrimitiveRestartIndex(256); - glDrawElements(GL_TRIANGLE_STRIP, (PATCH_SIZE-1) * (PATCH_SIZE-1), GL_UNSIGNED_INT, 0); + //glPrimitiveRestartIndex(255); + glDrawElements(GL_TRIANGLE_STRIP, (PATCH_SIZE-1) * (PATCH_SIZE-1) * 4, GL_UNSIGNED_INT, 0); //Render text nvgBeginFrame(mNVGContext, width(), height(), mPixelRatio);