Skip to content

Commit

Permalink
Added Texture Export
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaysmito101 committed Nov 4, 2021
1 parent b3367f3 commit f9f142f
Show file tree
Hide file tree
Showing 12 changed files with 359 additions and 128 deletions.
80 changes: 80 additions & 0 deletions Binaries/Data/shaders/texture_bake/frag.glsl
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);
}
55 changes: 55 additions & 0 deletions Binaries/Data/shaders/texture_bake/geom.glsl
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();
}
29 changes: 29 additions & 0 deletions Binaries/Data/shaders/texture_bake/vert.glsl
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 modified Binaries/TerraForge3D.exe
Binary file not shown.
26 changes: 26 additions & 0 deletions src/Base/ExportTexture.cpp
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;
}
5 changes: 5 additions & 0 deletions src/Base/ExportTexture.h
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);
9 changes: 6 additions & 3 deletions src/Base/FrameBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

#include <glad/glad.h>

FrameBuffer::FrameBuffer()
FrameBuffer::FrameBuffer(int w, int h)
{
width = w;
height = h;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glGenTextures(1, &colorTexture);
glBindTexture(GL_TEXTURE_2D, colorTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTexture, 0);
glGenTextures(1, &depthTexture);
glBindTexture(GL_TEXTURE_2D, depthTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 800, 600, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, w, h, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
Expand All @@ -32,6 +34,7 @@ FrameBuffer::~FrameBuffer()

void FrameBuffer::Begin()
{
glViewport(0, 0, width, height);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Base/FrameBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class FrameBuffer {

public:

FrameBuffer();
FrameBuffer(int width = 800, int height = 600);
~FrameBuffer();

void Begin();
Expand All @@ -18,4 +18,5 @@ class FrameBuffer {

private:
uint32_t colorTexture, depthTexture, fbo;
int width, height;
};
Loading

0 comments on commit f9f142f

Please sign in to comment.