Start first task

This commit is contained in:
hodasemi 2018-11-16 09:27:11 +01:00
parent 166e697630
commit f27795b697
10 changed files with 190 additions and 16 deletions

View file

@ -1 +1,6 @@
{}
{
"files.associations": {
"bitset": "cpp",
"chrono": "cpp"
}
}

View file

@ -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));
}

View file

@ -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;
}

View file

@ -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);
}

View file

@ -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;
}

View file

@ -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));
}
}

View file

@ -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;
}
}

View file

@ -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);
}
}

View file

@ -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;
}
}

View file

@ -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);