-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3367f3
commit f9f142f
Showing
12 changed files
with
359 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#version 430 core | ||
out vec4 FragColor; | ||
|
||
#define NUM_TEXTURE_LAYERS 8 | ||
|
||
uniform vec3 _LightPosition; | ||
uniform vec3 _LightColor; | ||
|
||
in float height; | ||
in float Distance; | ||
in vec3 FragPos; | ||
in vec3 Normal; | ||
in vec2 TexCoord; | ||
|
||
uniform sampler2D _DiffuseTextures[NUM_TEXTURE_LAYERS]; | ||
uniform vec3 _DiffuseTexturesHeights[NUM_TEXTURE_LAYERS]; | ||
uniform vec3 _DiffuseTexturesData[NUM_TEXTURE_LAYERS]; | ||
uniform float _SeaLevel; | ||
|
||
|
||
vec2 hash( vec2 p ) // replace this by something better | ||
{ | ||
p = vec2( dot(p,vec2(127.1,311.7)), dot(p,vec2(269.5,183.3)) ); | ||
return -1.0 + 2.0*fract(sin(p)*43758.5453123); | ||
} | ||
|
||
float noise( in vec2 p ) | ||
{ | ||
const float K1 = 0.366025404; // (sqrt(3)-1)/2; | ||
const float K2 = 0.211324865; // (3-sqrt(3))/6; | ||
|
||
vec2 i = floor( p + (p.x+p.y)*K1 ); | ||
vec2 a = p - i + (i.x+i.y)*K2; | ||
float m = step(a.y,a.x); | ||
vec2 o = vec2(m,1.0-m); | ||
vec2 b = a - o + K2; | ||
vec2 c = a - 1.0 + 2.0*K2; | ||
vec3 h = max( 0.5-vec3(dot(a,a), dot(b,b), dot(c,c) ), 0.0 ); | ||
vec3 n = h*h*h*h*vec3( dot(a,hash(i+0.0)), dot(b,hash(i+o)), dot(c,hash(i+1.0))); | ||
return dot( n, vec3(70.0) ); | ||
} | ||
|
||
|
||
float getTextureInfluence(int i, vec2 coord) { | ||
float h = height + noise(coord*_DiffuseTexturesData[i].x)*_DiffuseTexturesData[i].y; | ||
float midVal = (_DiffuseTexturesHeights[i].x + _DiffuseTexturesHeights[i].y)/2; | ||
float p = 0; | ||
if(h < midVal) | ||
p = _DiffuseTexturesHeights[i].x - height; | ||
if(h >= midVal) | ||
p =height - _DiffuseTexturesHeights[i].y; | ||
|
||
return pow(2.713, -_DiffuseTexturesData[i].z*p); | ||
} | ||
|
||
vec4 GetTextureColorBasedOnHeight(vec2 coord){ | ||
vec4 accum = vec4(0.0); | ||
float total_influence = 0.0; | ||
|
||
for(int i=0; i < NUM_TEXTURE_LAYERS ; i++){ | ||
float texture_influence = getTextureInfluence(i, coord*_DiffuseTexturesHeights[i].z); | ||
|
||
total_influence += texture_influence; | ||
accum += texture(_DiffuseTextures[i], coord*_DiffuseTexturesHeights[i].z) * texture_influence; | ||
} | ||
|
||
if(total_influence > 0) { | ||
accum /= total_influence ; | ||
} | ||
return accum; | ||
} | ||
|
||
|
||
void main() | ||
{ | ||
|
||
vec3 objectColor = vec3(1, 1, 1); | ||
objectColor = GetTextureColorBasedOnHeight(TexCoord).xyz; | ||
FragColor = vec4(objectColor, 1.0f); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#version 430 core | ||
|
||
layout(triangles) in; | ||
layout(triangle_strip, max_vertices = 3) out; | ||
|
||
uniform mat4 _PV; | ||
|
||
out float height; | ||
out float Distance; | ||
out vec3 FragPos; | ||
out vec3 Normal; | ||
out vec2 TexCoord; | ||
|
||
in DATA | ||
{ | ||
float height; | ||
vec3 FragPos; | ||
vec3 Normal; | ||
float distance; | ||
vec2 TexCoord; | ||
} data_in[]; | ||
|
||
const vec4 clipPlane = vec4(0, -1, 0, 5); | ||
|
||
void main() | ||
{ | ||
gl_Position = _PV * gl_in[0].gl_Position; | ||
//gl_ClipDistance[0] = dot(gl_Position, clipPlane); | ||
Normal = data_in[0].Normal; | ||
height = data_in[0].height; | ||
Distance = data_in[0].distance; | ||
TexCoord = data_in[0].TexCoord; | ||
FragPos = data_in[0].FragPos; | ||
EmitVertex(); | ||
|
||
gl_Position = _PV * gl_in[1].gl_Position; | ||
//gl_ClipDistance[0] = dot(gl_Position, clipPlane); | ||
height = data_in[1].height; | ||
Distance = data_in[1].distance; | ||
Normal = data_in[1].Normal; | ||
TexCoord = data_in[1].TexCoord; | ||
FragPos = data_in[1].FragPos; | ||
EmitVertex(); | ||
|
||
gl_Position = _PV * gl_in[2].gl_Position; | ||
//gl_ClipDistance[0] = dot(gl_Position, clipPlane); | ||
height = data_in[2].height; | ||
Distance = data_in[2].distance; | ||
Normal = data_in[2].Normal; | ||
TexCoord = data_in[2].TexCoord; | ||
FragPos = data_in[2].FragPos; | ||
EmitVertex(); | ||
|
||
EndPrimitive(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#version 430 core | ||
layout (location = 0) in vec3 aPos; | ||
layout (location = 1) in vec3 aNorm; | ||
layout (location = 2) in vec2 aTexCoord; | ||
|
||
uniform mat4 _Model; | ||
uniform mat4 _PV; | ||
|
||
out DATA | ||
{ | ||
float height; | ||
vec3 FragPos; | ||
vec3 Normal; | ||
float distance; | ||
vec2 TexCoord; | ||
} data_out; | ||
|
||
|
||
|
||
void main() | ||
{ | ||
gl_Position = vec4(0, aPos.x, aPos.z, 1.0); | ||
data_out.height = aPos.y; | ||
data_out.FragPos = vec3(aPos.x, aPos.y, aPos.z); | ||
data_out.Normal = vec3(aNorm.x, aNorm.y, aNorm.z); | ||
data_out.TexCoord = aTexCoord; | ||
// data_out.distance = sqrt(_CameraPos, gl_Position.xyz); | ||
data_out.distance = 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include "ExportTexture.h" | ||
|
||
#include <glad/glad.h> | ||
#include <stb/stb_image_write.h> | ||
|
||
void ExportTexture(int fbo, std::string path, int w, int h) | ||
{ | ||
if (path.size() < 3) | ||
return; | ||
|
||
if (path.find(".png") == std::string::npos) | ||
path += ".png"; | ||
|
||
unsigned char* data = new unsigned char[w * h * 3]; | ||
|
||
glReadBuffer(GL_COLOR_ATTACHMENT0); | ||
glBindFramebuffer(GL_FRAMEBUFFER, fbo); | ||
glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, data); | ||
glBindFramebuffer(GL_FRAMEBUFFER, 0); | ||
|
||
stbi_flip_vertically_on_write(true); | ||
|
||
stbi_write_png(path.c_str(), w, h, 3, data, w * 3); | ||
|
||
delete[] data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
void ExportTexture(int fbo, std::string path, int w, int h); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.