From f27795b697ccdb2c75a0f74269febe543843e0f7 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Fri, 16 Nov 2018 09:27:11 +0100 Subject: [PATCH] Start first task --- .vscode/settings.json | 7 +++- 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/sky.frag | 4 +-- exercise2/glsl/sky.vert | 4 +-- exercise2/glsl/terrain.frag | 8 ++--- exercise2/glsl/terrain.vert | 6 ++-- exercise2/src/Viewer.cpp | 30 +++++++++++++++-- 10 files changed, 190 insertions(+), 16 deletions(-) create mode 100644 exercise2/glsl/old_sky.frag create mode 100644 exercise2/glsl/old_sky.vert create mode 100644 exercise2/glsl/old_terrain.frag create mode 100644 exercise2/glsl/old_terrain.vert diff --git a/.vscode/settings.json b/.vscode/settings.json index 9e26dfe..f09e1a3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1 +1,6 @@ -{} \ No newline at end of file +{ + "files.associations": { + "bitset": "cpp", + "chrono": "cpp" + } +} \ No newline at end of file diff --git a/exercise2/glsl/old_sky.frag b/exercise2/glsl/old_sky.frag new file mode 100644 index 0000000..839a02e --- /dev/null +++ b/exercise2/glsl/old_sky.frag @@ -0,0 +1,21 @@ +// 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 new file mode 100644 index 0000000..b154cce --- /dev/null +++ b/exercise2/glsl/old_sky.vert @@ -0,0 +1,19 @@ +// 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 new file mode 100644 index 0000000..68e8105 --- /dev/null +++ b/exercise2/glsl/old_terrain.frag @@ -0,0 +1,49 @@ +// 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 new file mode 100644 index 0000000..79f3139 --- /dev/null +++ b/exercise2/glsl/old_terrain.vert @@ -0,0 +1,58 @@ +// 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/sky.frag b/exercise2/glsl/sky.frag index 839a02e..cb1baed 100644 --- a/exercise2/glsl/sky.frag +++ b/exercise2/glsl/sky.frag @@ -1,7 +1,7 @@ +#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 -#version 330 out vec4 color; @@ -18,4 +18,4 @@ void main() 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/sky.vert b/exercise2/glsl/sky.vert index b154cce..883a534 100644 --- a/exercise2/glsl/sky.vert +++ b/exercise2/glsl/sky.vert @@ -1,7 +1,7 @@ +#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 -#version 330 uniform mat4 mvp; @@ -16,4 +16,4 @@ void main() gl_Position = mvp * clipPos; gl_Position.z = 0.5 * gl_Position.w; -} \ No newline at end of file +} diff --git a/exercise2/glsl/terrain.frag b/exercise2/glsl/terrain.frag index 68e8105..dc3227e 100644 --- a/exercise2/glsl/terrain.frag +++ b/exercise2/glsl/terrain.frag @@ -1,9 +1,7 @@ -// This source code is property of the Computer Graphics and Visualization +#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 -#version 330 - - out vec4 color; @@ -46,4 +44,4 @@ void main() color = calculateLighting(color, specular, n, dirToViewer); -} \ No newline at end of file +} diff --git a/exercise2/glsl/terrain.vert b/exercise2/glsl/terrain.vert index 79f3139..5eb2daa 100644 --- a/exercise2/glsl/terrain.vert +++ b/exercise2/glsl/terrain.vert @@ -1,7 +1,7 @@ -// This source code is property of the Computer Graphics and Visualization +#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 -#version 330 in vec4 position; @@ -55,4 +55,4 @@ float getTerrainHeight(vec2 p) amplitude *= 0.45; } return 15 * total / maxAmplitude; -} \ No newline at end of file +} diff --git a/exercise2/src/Viewer.cpp b/exercise2/src/Viewer.cpp index fb7accd..7118611 100644 --- a/exercise2/src/Viewer.cpp +++ b/exercise2/src/Viewer.cpp @@ -86,12 +86,35 @@ void Viewer::CreateGeometry() /*Generate positions and indices for a terrain patch with a single triangle strip */ + 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); + } + } + + // full x direction, we skip x = 255 inside the loop + // decrease y direction by 1 + int count = (PATCH_SIZE) * (PATCH_SIZE-1); + + for (int k = 0; k < count; k++) { + + // skip creating triangles on last x in row + int end_of_x = k % (PATCH_SIZE-1); + + if (end_of_x == 0) { + continue; + } + + indices.emplace_back(k); + indices.emplace_back(k + PATCH_SIZE); + indices.emplace_back(k + PATCH_SIZE + 1); + indices.emplace_back(k + 1); + } + terrainShader.bind(); terrainPositions.uploadData(positions).bindToAttribute("position"); terrainIndices.uploadData(indices.size() * sizeof(uint32_t), indices.data()); - - //textures grassTexture = CreateTexture((unsigned char*)grass_jpg, grass_jpg_size); rockTexture = CreateTexture((unsigned char*)rock_jpg, rock_jpg_size); @@ -176,7 +199,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); //Render text nvgBeginFrame(mNVGContext, width(), height(), mPixelRatio);