Fix shader problems
This commit is contained in:
parent
f27795b697
commit
f86fc26703
8 changed files with 39 additions and 160 deletions
|
@ -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));
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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);
|
||||
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
#include "glsl.h"
|
||||
#include "textures.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
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<char>(is), std::istreambuf_iterator<char>());
|
||||
}
|
||||
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);
|
||||
|
|
Loading…
Reference in a new issue