Working with parallax test

This commit is contained in:
Vladislav Khorev 2017-12-11 12:30:51 +03:00
parent f2e238ed5e
commit 915c031f52
11 changed files with 282 additions and 94 deletions

BIN
assets/5f.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

BIN
assets/6f.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

BIN
assets/7f.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

BIN
assets/owl-green-height.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

BIN
assets/owl-green-normal.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 MiB

BIN
assets/owl-green.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 819 KiB

68
assets/parallax_fragment.txt Executable file
View File

@ -0,0 +1,68 @@
precision mediump float;
uniform sampler2D Texture;
uniform sampler2D NormalMap;
uniform sampler2D HeightMap;
precision highp float;
varying vec2 frag_uv;
varying vec3 ts_light_pos;
varying vec3 ts_view_pos;
varying vec3 ts_frag_pos;
const float num_layers = 16.0;
const float depth_scale = 0.01;
vec2 parallax_uv(vec2 uv, vec3 view_dir)
{
float layer_depth = 1.0 / num_layers;
float cur_layer_depth = 0.0;
vec2 delta_uv = view_dir.xy * depth_scale / (view_dir.z * num_layers);
vec2 cur_uv = uv;
float depth_from_tex = texture2D(HeightMap, cur_uv).r;
for (int i = 0; i < 32; i++) {
cur_layer_depth += layer_depth;
cur_uv -= delta_uv;
depth_from_tex = texture2D(HeightMap, cur_uv).r;
if (depth_from_tex < cur_layer_depth) {
break;
}
}
// Parallax occlusion mapping
vec2 prev_uv = cur_uv + delta_uv;
float next = depth_from_tex - cur_layer_depth;
float prev = texture2D(HeightMap, prev_uv).r - cur_layer_depth
+ layer_depth;
float weight = next / (next - prev);
return mix(cur_uv, prev_uv, weight);
}
void main(void)
{
vec3 light_dir = normalize(ts_light_pos - ts_frag_pos);
vec3 view_dir = normalize(ts_view_pos - ts_frag_pos);
// Only perturb the texture coordinates if a parallax technique is selected
vec2 uv = parallax_uv(frag_uv, view_dir);
//vec2 uv = frag_uv;
vec3 albedo = texture2D(Texture, uv).rgb;
vec3 ambient = 0.3 * albedo;
// Normal mapping
vec3 norm = normalize(texture2D(NormalMap, uv).rgb * 2.0 - 1.0);
float diffuse = max(dot(light_dir, norm), 0.0);
gl_FragColor = vec4(diffuse * albedo + ambient, 1.0);
}

60
assets/parallax_vertex.txt Executable file
View File

@ -0,0 +1,60 @@
attribute vec3 vPosition;
attribute vec2 vTexCoord;
uniform vec3 eye;
uniform mat4 ModelViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 ProjectionMatrix1;
uniform mat3 ModelViewMatrix3x3;
varying vec2 frag_uv;
varying vec3 ts_light_pos; // Tangent space values
varying vec3 ts_view_pos; //
varying vec3 ts_frag_pos; //
mat3 transpose(in mat3 inMatrix)
{
vec3 i0 = inMatrix[0];
vec3 i1 = inMatrix[1];
vec3 i2 = inMatrix[2];
mat3 outMatrix = mat3(
vec3(i0.x, i1.x, i2.x),
vec3(i0.y, i1.y, i2.y),
vec3(i0.z, i1.z, i2.z)
);
return outMatrix;
}
void main()
{
vec3 vert_norm = vec3(0, 1, 0);
vec3 vert_tang = vec3(1, 0, 0);
vec3 vert_bitang = vec3(0, 0, 1);
mat4 mvp = ProjectionMatrix1 * ModelViewMatrix;
gl_Position = mvp * vec4(vPosition.xyz, 1.0);
ts_frag_pos = vec3(ModelViewMatrix * vec4(vPosition.xyz, 1.0));
vec3 t = normalize((NormalMatrix) * vert_tang);
vec3 b = normalize((NormalMatrix) * vert_bitang);
vec3 n = normalize((NormalMatrix) * vert_norm);
mat3 tbn = transpose(mat3(t, b, n));
ts_light_pos = tbn * vec3(1,2,0);
//ts_view_pos = tbn * eye;
ts_view_pos = tbn * vec3(0,0,0);
ts_frag_pos = tbn * ts_frag_pos;
frag_uv = vTexCoord;
}

View File

@ -12,6 +12,41 @@
TMyApplication* Application; TMyApplication* Application;
Matrix3f quatToMatrix(Vector4f q) {
Matrix3f result;
double sqw = q(3)*q(3);
double sqx = q(0)*q(0);
double sqy = q(1)*q(1);
double sqz = q(2)*q(2);
// invs (inverse square length) is only required if quaternion is not already normalised
double invs = 1 / (sqx + sqy + sqz + sqw);
result(0,0) = (sqx - sqy - sqz + sqw)*invs; // since sqw + sqx + sqy + sqz =1/invs*invs
result(1,1) = (-sqx + sqy - sqz + sqw)*invs;
result(2,2) = (-sqx - sqy + sqz + sqw)*invs;
double tmp1 = q(0)*q(1);
double tmp2 = q(2)*q(3);
result(1, 0) = 2.0 * (tmp1 + tmp2)*invs;
result(0, 1) = 2.0 * (tmp1 - tmp2)*invs;
tmp1 = q(0)*q(2);
tmp2 = q(1)*q(3);
result(2,0) = 2.0 * (tmp1 - tmp2)*invs;
result(0,2) = 2.0 * (tmp1 + tmp2)*invs;
tmp1 = q(1)*q(2);
tmp2 = q(0)*q(3);
result(2, 1) = 2.0 * (tmp1 + tmp2)*invs;
result(1, 2) = 2.0 * (tmp1 - tmp2)*invs;
return result;
}
void TMyApplication::InnerInit() void TMyApplication::InnerInit()
{ {
@ -39,34 +74,65 @@ void TMyApplication::InnerInit()
ResourceManager->ShaderManager.AddShader("FrameShader", "frameshader_vertex.txt", "frameshader_fragment.txt"); ResourceManager->ShaderManager.AddShader("FrameShader", "frameshader_vertex.txt", "frameshader_fragment.txt");
ResourceManager->ShaderManager.AddShader("ColorShader", "color_vertex.txt", "color_fragment.txt"); ResourceManager->ShaderManager.AddShader("ColorShader", "color_vertex.txt", "color_fragment.txt");
ResourceManager->ShaderManager.AddShader("SSAA_4X", "SSAA_4X.vertex", "SSAA_4X.frag"); ResourceManager->ShaderManager.AddShader("SSAA_4X", "SSAA_4X.vertex", "SSAA_4X.frag");
ResourceManager->ShaderManager.AddShader("ParallaxShader", "parallax_vertex.txt", "parallax_fragment.txt");
Renderer->PushShader("DefaultShader"); Renderer->PushShader("DefaultShader");
ResourceManager->TexList.AddTexture("console_bkg.bmp"); ResourceManager->TexList.AddTexture("console_bkg.bmp");
ResourceManager->TexList.AddTexture("owl-green.jpg");
ResourceManager->TexList.AddTexture("owl-green-height.png");
ResourceManager->TexList.AddTexture("owl-green-normal.png");
ResourceManager->TexList.AddTexture("5f.jpg");
ResourceManager->TexList.AddTexture("6f.jpg");
ResourceManager->TexList.AddTexture("7f.jpg");
ResourceManager->FrameManager.AddFrameRenderBuffer("LevelBuffer", 512, 512); ResourceManager->FrameManager.AddFrameRenderBuffer("LevelBuffer", 512, 512);
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 0, 0)); /*
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 512, 0)); pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(-511, -512, 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(-512, 512, 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 512, 0)); pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 512, 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 0, 0)); pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(-512, -512, 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 512, 0)); pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 512, 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 0, 0)); pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, -512, 0));
*/
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(-511, 0, -512));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(-512, 0, 512));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 0, 512));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(-512, 0, -512));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 0, 512));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 0, -512));
pair.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(0, 0));
pair.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(0, 1));
pair.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(1,1));
pair.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(0, 0));
pair.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(1,1));
pair.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(1, 0));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 1, 0, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 0, 1, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 0, 1, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 1, 0, 1));
pair.first.ShaderName = "ParallaxShader";
pair.first.SamplerMap[CONST_STRING_TEXTURE_UNIFORM] = "owl-green.jpg";
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1)); pair.first.SamplerMap["HeightMap"] = "owl-green-height.png";
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1)); pair.first.SamplerMap["NormalMap"] = "owl-green-normal.png";
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 1, 0, 1)); //pair.first.SamplerMap[CONST_STRING_TEXTURE_UNIFORM] = "6f.jpg";
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 0, 1, 1)); //pair.first.SamplerMap["HeightMap"] = "5f.jpg";
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 0, 1, 1)); //pair.first.SamplerMap["NormalMap"] = "7f.jpg";
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 1, 0, 1));
pair.first.ShaderName = "ColorShader";
pair.second.RefreshBuffer(); pair.second.RefreshBuffer();
/*
rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 0, 0)); rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 0, 0));
rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 512, 0)); rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 512, 0));
rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 512, 0)); rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 512, 0));
@ -82,11 +148,9 @@ void TMyApplication::InnerInit()
rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(0, 0)); rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(0, 0));
rect.second.RefreshBuffer(); rect.second.RefreshBuffer();*/
Renderer->SetOrthoProjection();
Renderer->SetFullScreenViewport();
Inited = true; Inited = true;
@ -122,72 +186,93 @@ void TMyApplication::InnerOnTapUpAfterMove(Vector2f p)
void TMyApplication::InnerOnMove(Vector2f p, Vector2f shift) void TMyApplication::InnerOnMove(Vector2f p, Vector2f shift)
{ {
phi += shift(1)*0.02f;
if (phi < pi/12)
{
phi = pi / 12;
}
if (phi > pi / 2)
{
phi = pi / 2;
}
alpha -= shift(0)*0.02f;
} }
void TMyApplication::OnFling(Vector2f v) void TMyApplication::OnFling(Vector2f v)
{ {
} }
void TMyApplication::OnMouseWheel(short int delta)
{
distance += delta;
if (distance > 2500)
{
distance = 2500;
}
if (distance < 100)
{
distance = 100;
}
}
void TMyApplication::InnerDraw() void TMyApplication::InnerDraw()
{ {
Renderer->SwitchToFrameBuffer("LevelBuffer"); Renderer->SetPerspectiveProjection(pi / 6, 10.f, 10000.f);
Renderer->SetProjectionMatrix(512.f, 512.f);
glClearColor(0.0f, 0.0f, 1.0f, 1.0f); Renderer->SetFullScreenViewport();
Renderer->PushMatrix();
Renderer->TranslateMatrix(Vector3f(0, 0, -distance));
Vector4f quat1 = Vector4f(sin(phi / 2), 0, 0, cos(phi / 2));
Vector4f quat2 = Vector4f(0, sin(alpha / 2), 0, cos(alpha / 2));
Renderer->RotateMatrix(quat1);
Renderer->RotateMatrix(quat2);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
CheckGlError(""); CheckGlError("");
auto mat1 = quatToMatrix(quat1);
auto mat2 = quatToMatrix(quat2);
Vector3f lightPos = {0.f, 1.f, 1.f};
Vector3f eye = mat2 * (mat1 * Vector3f(0.0f, 0.f, -distance));
{ {
TRenderParamsSetter params(pair.first); TRenderParamsSetter params(pair.first);
RenderUniform3fv("eye", eye.data());
RenderUniform3fv("lightPos", lightPos.data());
Matrix3f normMatrix = Renderer->GetModelviewMatrix().inverse().transpose().block<3,3>(0,0);
RenderUniformMatrix3fv("NormalMatrix", false, normMatrix.data());
RenderUniformMatrix4fv("ModelViewMatrix", false, Renderer->GetModelviewMatrix().data());
RenderUniformMatrix3fv("ModelViewMatrix3x3", false, Renderer->GetModelviewMatrix().block<3,3>(0,0).data());
Renderer->DrawTriangleList(pair.second); Renderer->DrawTriangleList(pair.second);
} }
CheckGlError(""); Renderer->PopMatrix();
Renderer->SwitchToScreen();
Renderer->SetFullScreenViewport();
const float cos30 = sqrt(3) / 2;
const float sin30 = 0.5f;
const float sampleRadiusX = 0.75 / 512;
const float sampleRadiusY = 0.75 / 512;
Matrix2f rotate30Matrix;
rotate30Matrix(0, 0) = cos30;
rotate30Matrix(0, 1) = sin30;
rotate30Matrix(1, 0) = -sin30;
rotate30Matrix(1, 1) = cos30;
Renderer->PushShader("SSAA_4X");
std::array<Vector2f, 4> sampleVector = {
Vector2f(sampleRadiusX, sampleRadiusY),
Vector2f(-sampleRadiusX, sampleRadiusY),
Vector2f(-sampleRadiusX, -sampleRadiusY),
Vector2f(sampleRadiusX, -sampleRadiusY)
};
for (size_t i = 0; i < 4; i++)
{
sampleVector[i] = rotate30Matrix * sampleVector[i];
RenderUniform2fv("samplesOffset[" + boost::lexical_cast<std::string>(i) + "]", &sampleVector[i][0]);
}
glBindTexture(GL_TEXTURE_2D, ResourceManager->FrameManager.GetFrameTexture("LevelBuffer"));
Renderer->DrawTriangleList(rect.second);
Renderer->PopShader();
CheckGlError(""); CheckGlError("");
} }

View File

@ -63,6 +63,14 @@ public:
virtual void OnFling(Vector2f v); virtual void OnFling(Vector2f v);
virtual void OnMouseWheel(short int delta);
float distance = 2000;
float alpha = 0;
float phi = pi / 6;
}; };

View File

@ -1,33 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Файлы исходного кода">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Заголовочные файлы">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Файлы ресурсов">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Файлы исходного кода</Filter>
</ClCompile>
<ClCompile Include="..\..\..\game\main_code.cpp">
<Filter>Файлы исходного кода</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="main.h">
<Filter>Заголовочные файлы</Filter>
</ClInclude>
<ClInclude Include="..\..\..\game\main_code.h">
<Filter>Заголовочные файлы</Filter>
</ClInclude>
</ItemGroup>
</Project>