From a86eaf1bb2a89dad95682213811f10a9d4429834 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 27 Aug 2012 12:16:58 +0000 Subject: [PATCH] tutorial: start working on a small Perlin-like noise demo. --- .gitignore | 1 + build/vs2010/Lol.sln | 22 ++++++++ tutorial/03_noise.cpp | 102 ++++++++++++++++++++++++++++++++++ tutorial/03_noise.lolfx | 112 ++++++++++++++++++++++++++++++++++++++ tutorial/03_noise.vcxproj | 68 +++++++++++++++++++++++ tutorial/Makefile.am | 7 ++- 6 files changed, 311 insertions(+), 1 deletion(-) create mode 100644 tutorial/03_noise.cpp create mode 100644 tutorial/03_noise.lolfx create mode 100644 tutorial/03_noise.vcxproj diff --git a/.gitignore b/.gitignore index 4d7d5340..f073003c 100644 --- a/.gitignore +++ b/.gitignore @@ -72,6 +72,7 @@ test/xolotl/xolotl tools/make-font tutorial/01_triangle tutorial/02_cube +tutorial/03_noise tutorial/05_easymesh tutorial/08_fbo tutorial/11_fractal diff --git a/build/vs2010/Lol.sln b/build/vs2010/Lol.sln index 47e4c2f4..48572f3e 100644 --- a/build/vs2010/Lol.sln +++ b/build/vs2010/Lol.sln @@ -105,6 +105,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Peeweek", "Peeweek", "{E27F EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "demo-peeweek", "..\..\people\peeweek\demo-peeweek.vcxproj", "{C2E01551-B636-4324-8461-71811DF6FBB5}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03_noise", "..\..\tutorial\03_noise.vcxproj", "{B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|PS3 = Debug|PS3 @@ -445,6 +447,24 @@ Global {C2E01551-B636-4324-8461-71811DF6FBB5}.Release|Xbox 360.ActiveCfg = Release|Xbox 360 {C2E01551-B636-4324-8461-71811DF6FBB5}.Release|Xbox 360.Build.0 = Release|Xbox 360 {C2E01551-B636-4324-8461-71811DF6FBB5}.Release|Xbox 360.Deploy.0 = Release|Xbox 360 + {B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E}.Debug|PS3.ActiveCfg = Debug|PS3 + {B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E}.Debug|PS3.Build.0 = Debug|PS3 + {B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E}.Debug|Win32.ActiveCfg = Debug|Win32 + {B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E}.Debug|Win32.Build.0 = Debug|Win32 + {B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E}.Debug|x64.ActiveCfg = Debug|x64 + {B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E}.Debug|x64.Build.0 = Debug|x64 + {B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E}.Debug|Xbox 360.ActiveCfg = Debug|Xbox 360 + {B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E}.Debug|Xbox 360.Build.0 = Debug|Xbox 360 + {B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E}.Debug|Xbox 360.Deploy.0 = Debug|Xbox 360 + {B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E}.Release|PS3.ActiveCfg = Release|PS3 + {B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E}.Release|PS3.Build.0 = Release|PS3 + {B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E}.Release|Win32.ActiveCfg = Release|Win32 + {B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E}.Release|Win32.Build.0 = Release|Win32 + {B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E}.Release|x64.ActiveCfg = Release|x64 + {B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E}.Release|x64.Build.0 = Release|x64 + {B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E}.Release|Xbox 360.ActiveCfg = Release|Xbox 360 + {B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E}.Release|Xbox 360.Build.0 = Release|Xbox 360 + {B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E}.Release|Xbox 360.Deploy.0 = Release|Xbox 360 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -470,6 +490,8 @@ Global {7B083DA2-FE08-4F6D-BFDD-195D5C2783EB} = {E74CF679-CA2A-47E9-B1F4-3779D6AC6B04} {1C5B8702-290C-42DA-AA9E-671348F5B747} = {E74CF679-CA2A-47E9-B1F4-3779D6AC6B04} {6BF81B39-EDC2-4227-9992-C2D8ABEA95AF} = {E74CF679-CA2A-47E9-B1F4-3779D6AC6B04} + {BCEE0132-8E24-49BE-AFEB-96DAD14396BA} = {E74CF679-CA2A-47E9-B1F4-3779D6AC6B04} + {B0A53D75-CBB4-4FDF-93AC-2D12A79ADA0E} = {E74CF679-CA2A-47E9-B1F4-3779D6AC6B04} {32F3F8CF-D22E-45E4-BEB8-AD909E8C5515} = {33704AA4-F2B5-4138-A40D-E3E77F89ED46} {EE203B88-44CF-4859-9D42-7A5F40FECB52} = {8C77EAA8-1077-4EF7-AE53-97C6C60A3601} {B357514A-7881-422D-8358-161B689E7620} = {3D341D8A-E400-4B1D-BC05-B5C35487D9B5} diff --git a/tutorial/03_noise.cpp b/tutorial/03_noise.cpp new file mode 100644 index 00000000..053e1451 --- /dev/null +++ b/tutorial/03_noise.cpp @@ -0,0 +1,102 @@ +// +// Lol Engine - Noise tutorial +// +// Copyright: (c) 2012 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/projects/COPYING.WTFPL for more details. +// + +#if defined HAVE_CONFIG_H +# include "config.h" +#endif + +#include "core.h" +#include "loldebug.h" + +using namespace std; +using namespace lol; + +#if defined _WIN32 +# include +#endif + +extern char const *lolfx_03_noise; + +class NoiseDemo : public WorldEntity +{ +public: + NoiseDemo() : + m_time(0.0), + m_ready(false) + { + m_vertices << vec2(-1.0, 1.0); + m_vertices << vec2(-1.0, -1.0); + m_vertices << vec2( 1.0, -1.0); + m_vertices << vec2(-1.0, 1.0); + m_vertices << vec2( 1.0, -1.0); + m_vertices << vec2( 1.0, 1.0); + m_ready = false; + } + + virtual void TickDraw(float seconds) + { + WorldEntity::TickDraw(seconds); + + m_time += seconds; + + if (!m_ready) + { + m_shader = Shader::Create(lolfx_03_noise); + m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0); + m_time_uni = m_shader->GetUniformLocation("in_Time"); + + m_vdecl = new VertexDeclaration(VertexStream(VertexUsage::Position)); + + m_vbo = new VertexBuffer(m_vertices.Bytes()); + void *vertices = m_vbo->Lock(0, 0); + memcpy(vertices, &m_vertices[0], m_vertices.Bytes()); + m_vbo->Unlock(); + + m_ready = true; + + /* FIXME: this object never cleans up */ + } + + m_shader->Bind(); + m_shader->SetUniform(m_time_uni, m_time); + m_vdecl->SetStream(m_vbo, m_coord); + m_vdecl->Bind(); + m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 2); + m_vdecl->Unbind(); + } + +private: + Array m_vertices; + Shader *m_shader; + ShaderAttrib m_coord; + ShaderUniform m_time_uni; + VertexDeclaration *m_vdecl; + VertexBuffer *m_vbo; + float m_time; + bool m_ready; +}; + +int main(int argc, char **argv) +{ + Application app("Tutorial 3: Noise", ivec2(1280, 720), 60.0f); + +#if defined _MSC_VER && !defined _XBOX + _chdir(".."); +#elif defined _WIN32 && !defined _XBOX + _chdir("../.."); +#endif + + new DebugFps(5, 5); + new NoiseDemo(); + + app.Run(); + return EXIT_SUCCESS; +} + diff --git a/tutorial/03_noise.lolfx b/tutorial/03_noise.lolfx new file mode 100644 index 00000000..e5eea0f3 --- /dev/null +++ b/tutorial/03_noise.lolfx @@ -0,0 +1,112 @@ +-- GLSL.Vert -- + +#version 120 + +attribute vec2 in_Position; + +void main(void) +{ + gl_Position = vec4(in_Position, 0.0, 1.0); +} + +-- GLSL.Frag -- + +#version 120 + +uniform float in_Time; + +vec3 noise3(vec2 p) +{ + return vec3(fract(sin(dot(p, vec2(12.3453, -54.3353432))) * 8433.235443), + fract(sin(dot(p, vec2(34.23232, 98.03537))) * 8294.5345433), + fract(sin(dot(p, vec2(82.91234, -19.243533))) * 2912.454369)); +} + +vec3 noise3(vec3 p) +{ + return vec3(fract(sin(54.343909 + dot(p, vec3(12.3453, -54.3353432, 34.3490432))) * 8433.235443), + fract(sin(93.329945 - dot(p, vec3(34.23232, 98.0353743, 90.2390984))) * 8294.5345433), + fract(sin(32.039284 + dot(p, vec3(82.91234, -19.243533, 22.3294090))) * 2912.454369)); +} + +vec3 perlin3(vec2 p) +{ + vec2 a = floor(p); + vec2 d = p - a; + d = d * d * (3.0 - 2.0 * d); + + vec3 n1 = noise3(a); + vec3 n2 = noise3(a + vec2(1.0, 0.0)); + vec3 n3 = noise3(a + vec2(0.0, 1.0)); + vec3 n4 = noise3(a + vec2(1.0, 1.0)); + + vec3 q1 = n2 * d.x + n1 * (1.0 - d.x); + vec3 q2 = n4 * d.x + n3 * (1.0 - d.x); + + vec3 q3 = q2 * d.y + q1 * (1.0 - d.y); + + return q3; +} + +vec3 perlin3(vec3 p) +{ + vec3 a = floor(p); + vec3 d = p - a; + d = d * d * (3.0 - 2.0 * d); + + vec3 o1 = noise3(a); + vec3 o2 = noise3(a + vec3(1.0, 0.0, 0.0)); + vec3 o3 = noise3(a + vec3(0.0, 1.0, 0.0)); + vec3 o4 = noise3(a + vec3(1.0, 1.0, 0.0)); + vec3 o5 = noise3(a + vec3(0.0, 0.0, 1.0)); + vec3 o6 = noise3(a + vec3(1.0, 0.0, 1.0)); + vec3 o7 = noise3(a + vec3(0.0, 1.0, 1.0)); + vec3 o8 = noise3(a + vec3(1.0, 1.0, 1.0)); + + vec3 n1 = o2 * d.x + o1 * (1.0 - d.x); + vec3 n2 = o4 * d.x + o3 * (1.0 - d.x); + vec3 n3 = o6 * d.x + o5 * (1.0 - d.x); + vec3 n4 = o8 * d.x + o7 * (1.0 - d.x); + + vec3 q1 = n2 * d.y + n1 * (1.0 - d.y); + vec3 q2 = n4 * d.y + n3 * (1.0 - d.y); + + vec3 q3 = q2 * d.z + q1 * (1.0 - d.z); + + return q3; +} + +void main(void) +{ + //vec3 input = vec3(gl_FragCoord.xy, in_Time * 200.0); + vec3 input = vec3(in_Time * 20.0, gl_FragCoord.xy); + + vec3 p = perlin3(input / 160.0) / 2.0 + + perlin3(input / 80.0) / 4.0 + + perlin3(input / 40.0) / 8.0 + + perlin3(input / 20.0) / 16.0 + + perlin3(input / 10.0) / 32.0 + + perlin3(input / 5.0) / 64.0 + + perlin3(input / 2.5) / 128.0; + gl_FragColor = vec4(p.xxx, 1.0); + + //inputvec4(perlin3(input), 1.0); + //inputvec4(noise3(floor(input / 10.0)).xxx, 1.0); + //inputvec4(noise3(input), 1.0); +} + +-- HLSL.Vert -- + +void main(float2 in_Position : POSITION, + out float4 out_Position : POSITION) +{ + out_Position = float4(in_Position, 0.0, 1.0); +} + +-- HLSL.Frag -- + +void main(out float4 out_FragColor : COLOR) +{ + out_FragColor = float4(0.7, 0.2, 0.5, 1.0); +} + diff --git a/tutorial/03_noise.vcxproj b/tutorial/03_noise.vcxproj new file mode 100644 index 00000000..92052e0d --- /dev/null +++ b/tutorial/03_noise.vcxproj @@ -0,0 +1,68 @@ + + + + + Debug + PS3 + + + Debug + Win32 + + + Debug + x64 + + + Debug + Xbox 360 + + + Release + PS3 + + + Release + Win32 + + + Release + x64 + + + Release + Xbox 360 + + + + + + + + + + + {9e62f2fe-3408-4eae-8238-fd84238ceeda} + + + + {b0a53d75-cbb4-4fdf-93ac-2d12a79ada0e} + Application + Win32Proj + + + + + + + + + + + + + + + + + diff --git a/tutorial/Makefile.am b/tutorial/Makefile.am index a842e022..539ce0d4 100644 --- a/tutorial/Makefile.am +++ b/tutorial/Makefile.am @@ -14,7 +14,7 @@ SUFFIXES = .lolfx .lolfx.o: $(LOLFX_BUILD) -noinst_PROGRAMS = 01_triangle 02_cube 05_easymesh 08_fbo 11_fractal +noinst_PROGRAMS = 01_triangle 02_cube 03_noise 05_easymesh 08_fbo 11_fractal 01_triangle_SOURCES = 01_triangle.cpp 01_triangle.lolfx 01_triangle_CPPFLAGS = @LOL_CFLAGS@ @PIPI_CFLAGS@ @@ -26,6 +26,11 @@ noinst_PROGRAMS = 01_triangle 02_cube 05_easymesh 08_fbo 11_fractal 02_cube_LDFLAGS = $(top_builddir)/src/liblol.a @LOL_LIBS@ @PIPI_LIBS@ 02_cube_DEPENDENCIES = $(top_builddir)/src/liblol.a +03_noise_SOURCES = 03_noise.cpp 03_noise.lolfx +03_noise_CPPFLAGS = @LOL_CFLAGS@ @PIPI_CFLAGS@ +03_noise_LDFLAGS = $(top_builddir)/src/liblol.a @LOL_LIBS@ @PIPI_LIBS@ +03_noise_DEPENDENCIES = $(top_builddir)/src/liblol.a + 05_easymesh_SOURCES = 05_easymesh.cpp 05_easymesh_CPPFLAGS = @LOL_CFLAGS@ @PIPI_CFLAGS@ 05_easymesh_LDFLAGS = $(top_builddir)/src/liblol.a @LOL_LIBS@ @PIPI_LIBS@