Quellcode durchsuchen

base: simplify code here and there.

undefined
Sam Hocevar vor 10 Jahren
Ursprung
Commit
ebfd692152
5 geänderte Dateien mit 84 neuen und 178 gelöschten Zeilen
  1. +4
    -4
      demos/tutorial/01_triangle.cpp
  2. +18
    -25
      demos/tutorial/02_cube.cpp
  3. +7
    -8
      demos/tutorial/03_noise.cpp
  4. +14
    -20
      demos/tutorial/04_texture.cpp
  5. +41
    -121
      src/image/kernel.cpp

+ 4
- 4
demos/tutorial/01_triangle.cpp Datei anzeigen

@@ -24,11 +24,11 @@ class Triangle : public WorldEntity
{
public:
Triangle()
: m_vertices({ vec2( 0.0, 0.8),
vec2(-0.8, -0.8),
vec2( 0.8, -0.8) }),
m_ready(false)
{
m_vertices << vec2( 0.0, 0.8);
m_vertices << vec2(-0.8, -0.8);
m_vertices << vec2( 0.8, -0.8);
m_ready = false;
}

virtual void TickDraw(float seconds, Scene &scene)


+ 18
- 25
demos/tutorial/02_cube.cpp Datei anzeigen

@@ -24,32 +24,25 @@ class Cube : public WorldEntity
{
public:
Cube()
: m_angle(0),
m_mesh({ /* Front vertices/colors */
{ vec3(-1.0, -1.0, 1.0), vec3(1.0, 0.0, 1.0) },
{ vec3( 1.0, -1.0, 1.0), vec3(0.0, 1.0, 0.0) },
{ vec3( 1.0, 1.0, 1.0), vec3(1.0, 0.5, 0.0) },
{ vec3(-1.0, 1.0, 1.0), vec3(1.0, 1.0, 0.0) },
/* Back */
{ vec3(-1.0, -1.0, -1.0), vec3(1.0, 0.0, 0.0) },
{ vec3( 1.0, -1.0, -1.0), vec3(0.0, 0.5, 0.0) },
{ vec3( 1.0, 1.0, -1.0), vec3(0.0, 0.5, 1.0) },
{ vec3(-1.0, 1.0, -1.0), vec3(0.0, 0.0, 1.0) } }),
m_lines_indices({ 0, 1, 1, 2, 2, 3, 3, 0,
4, 5, 5, 6, 6, 7, 7, 4,
0, 4, 1, 5, 2, 6, 3, 7, }),
m_faces_indices({ 0, 1, 2, 2, 3, 0, 1, 5, 6, 6, 2, 1,
7, 6, 5, 5, 4, 7, 4, 0, 3, 3, 7, 4,
4, 5, 1, 1, 0, 4, 3, 2, 6, 6, 7, 3, }),
m_ready(false)
{
m_angle = 0;

/* Front vertices/colors */
m_mesh.Push(vec3(-1.0, -1.0, 1.0), vec3(1.0, 0.0, 1.0));
m_mesh.Push(vec3( 1.0, -1.0, 1.0), vec3(0.0, 1.0, 0.0));
m_mesh.Push(vec3( 1.0, 1.0, 1.0), vec3(1.0, 0.5, 0.0));
m_mesh.Push(vec3(-1.0, 1.0, 1.0), vec3(1.0, 1.0, 0.0));
/* Back */
m_mesh.Push(vec3(-1.0, -1.0, -1.0), vec3(1.0, 0.0, 0.0));
m_mesh.Push(vec3( 1.0, -1.0, -1.0), vec3(0.0, 0.5, 0.0));
m_mesh.Push(vec3( 1.0, 1.0, -1.0), vec3(0.0, 0.5, 1.0));
m_mesh.Push(vec3(-1.0, 1.0, -1.0), vec3(0.0, 0.0, 1.0));

m_faces_indices << 0 << 1 << 2 << 2 << 3 << 0;
m_faces_indices << 1 << 5 << 6 << 6 << 2 << 1;
m_faces_indices << 7 << 6 << 5 << 5 << 4 << 7;
m_faces_indices << 4 << 0 << 3 << 3 << 7 << 4;
m_faces_indices << 4 << 5 << 1 << 1 << 0 << 4;
m_faces_indices << 3 << 2 << 6 << 6 << 7 << 3;

m_lines_indices << 0 << 1 << 1 << 2 << 2 << 3 << 3 << 0;
m_lines_indices << 4 << 5 << 5 << 6 << 6 << 7 << 7 << 4;
m_lines_indices << 0 << 4 << 1 << 5 << 2 << 6 << 3 << 7;

m_ready = false;
}

virtual void TickGame(float seconds)


+ 7
- 8
demos/tutorial/03_noise.cpp Datei anzeigen

@@ -23,17 +23,16 @@ LOLFX_RESOURCE_DECLARE(03_noise);
class NoiseDemo : public WorldEntity
{
public:
NoiseDemo() :
NoiseDemo()
: m_vertices({ vec2(-1.0, 1.0),
vec2(-1.0, -1.0),
vec2( 1.0, -1.0),
vec2(-1.0, 1.0),
vec2( 1.0, -1.0),
vec2( 1.0, 1.0), }),
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, Scene &scene)


+ 14
- 20
demos/tutorial/04_texture.cpp Datei anzeigen

@@ -25,23 +25,17 @@ LOLFX_RESOURCE_DECLARE(04_texture);
class TextureDemo : public WorldEntity
{
public:
TextureDemo() :
TextureDemo()
: m_vertices({ vec2(-1.0, 1.0),
vec2(-1.0, -1.0),
vec2( 1.0, -1.0),
vec2(-1.0, 1.0),
vec2( 1.0, -1.0),
vec2( 1.0, 1.0), }),
m_frames(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_heightmap = new uint8_t[TEXTURE_WIDTH * 1];
}

virtual ~TextureDemo()
{
delete m_heightmap;
m_heightmap.Resize(TEXTURE_WIDTH * 1);
}

virtual void TickGame(float seconds)
@@ -50,16 +44,16 @@ public:

/* Generate a new heightmap at the beginning */
if (m_frames == 0)
memset(m_heightmap, 255, TEXTURE_WIDTH);
memset(m_heightmap.Data(), 255, m_heightmap.Bytes());

/* Scroll left */
for (int i = 0; i < TEXTURE_WIDTH - 1; i++)
for (int i = 0; i < m_heightmap.Count() - 1; i++)
m_heightmap[i] = m_heightmap[i + 1];

int height = m_heightmap[TEXTURE_WIDTH - 1];
int height = m_heightmap.Last();
height = (height + 127 + 40 * lol::sin(m_frames * 0.03) + rand() % 97 - 38) / 2;
height = std::max(15, std::min(height, 240));
m_heightmap[TEXTURE_WIDTH - 1] = height;
m_heightmap.Last() = height;

/* Update frame counter */
++m_frames;
@@ -91,7 +85,7 @@ public:
}

/* Send new heightmap to GPU */
m_texture->SetData(m_heightmap);
m_texture->SetData(m_heightmap.Data());

m_shader->Bind();
m_shader->SetUniform(m_texture_uni, m_texture->GetTextureUniform(), 0);
@@ -109,7 +103,7 @@ private:
ShaderUniform m_texture_uni;
VertexDeclaration *m_vdecl;
VertexBuffer *m_vbo;
uint8_t *m_heightmap;
Array<uint8_t> m_heightmap;
int m_frames;
bool m_ready;
};


+ 41
- 121
src/image/kernel.cpp Datei anzeigen

@@ -57,8 +57,8 @@ array2d<float> Image::HalftoneKernel(ivec2 size)
for (int y = 0; y < size.y; y++)
for (int x = 0; x < size.x; x++)
{
float dx = 2.f * x / size.x - 0.5f;
float dy = 2.f * (y + 0.07f) / size.y - 0.5f;
float dx = 2.f * (x + 0.02f) / size.x - 0.5f;
float dy = 2.f * (y + 0.03f) / size.y - 0.5f;
bool flip = false;
if (dx > 0.5f)
{
@@ -207,7 +207,7 @@ array2d<float> Image::NormalizeKernel(array2d<float> const &kernel)
{
int x = tmp[n].x;
int y = tmp[n].y;
dst[x][y] = (float)(n + 1) * epsilon;
dst[x][y] = (n + 1.f) * epsilon;
}

return dst;
@@ -215,138 +215,58 @@ array2d<float> Image::NormalizeKernel(array2d<float> const &kernel)

array2d<float> Image::EdiffKernel(EdiffAlgorithm algorithm)
{
array2d<float> ret;

switch(algorithm)
switch (algorithm)
{
case EdiffAlgorithm::FloydSteinberg:
ret.SetSize(ivec2(3, 2));
{
static float const myker[] =
{
0., 1., 7./16,
3./16, 5./16, 1./16,
};
memcpy(&ret[0][0], myker, sizeof(myker));
}
return ret;
return { { 0.f, 1.f, 7.f/16, },
{ 3.f/16, 5.f/16, 1.f/16, }, };

case EdiffAlgorithm::JaJuNi:
ret.SetSize(ivec2(5, 3));
{
static float const myker[] =
{
0., 0., 1., 7./48, 5./48,
3./48, 5./48, 7./48, 5./48, 3./48,
1./48, 3./48, 5./48, 3./48, 1./48,
};
memcpy(&ret[0][0], myker, sizeof(myker));
}
return ret;
return { { 0.f, 0.f, 1.f, 7.f/48, 5.f/48, },
{ 3.f/48, 5.f/48, 7.f/48, 5.f/48, 3.f/48, },
{ 1.f/48, 3.f/48, 5.f/48, 3.f/48, 1.f/48, }, };

case EdiffAlgorithm::Atkinson:
ret.SetSize(ivec2(4, 3));
{
static float const myker[] =
{
0., 1., 1./8, 1./8,
1./8, 1./8, 1./8, 0.,
0., 1./8, 0., 0.,
};
memcpy(&ret[0][0], myker, sizeof(myker));
}
return ret;
return { { 0.f, 1.f, 1.f/8, 1.f/8, },
{ 1.f/8, 1.f/8, 1.f/8, 0.f, },
{ 0.f, 1.f/8, 0.f, 0.f, }, };

case EdiffAlgorithm::Fan:
ret.SetSize(ivec2(4, 2));
{
static float const myker[] =
{
0., 0., 1., 7./16,
1./16, 3./16, 5./16, 0.,
};
memcpy(&ret[0][0], myker, sizeof(myker));
}
return ret;
return { { 0.f, 0.f, 1.f, 7.f/16, },
{ 1.f/16, 3.f/16, 5.f/16, 0.f, }, };

case EdiffAlgorithm::ShiauFan:
ret.SetSize(ivec2(4, 2));
{
static float const myker[] =
{
0., 0., 1., 1./2,
1./8, 1./8, 1./4, 0.,
};
memcpy(&ret[0][0], myker, sizeof(myker));
}
return ret;
return { { 0.f, 0.f, 1.f, 1.f/2, },
{ 1.f/8, 1.f/8, 1.f/4, 0.f, }, };

case EdiffAlgorithm::ShiauFan2:
ret.SetSize(ivec2(5, 2));
{
static float const myker[] =
{
0., 0., 0., 1., 1./2,
1./16, 1./16, 1./8, 1./4, 0.,
};
memcpy(&ret[0][0], myker, sizeof(myker));
}
return ret;
return { { 0.f, 0.f, 0.f, 1.f, 1.f/2, },
{ 1.f/16, 1.f/16, 1.f/8, 1.f/4, 0.f, }, };

case EdiffAlgorithm::Stucki:
ret.SetSize(ivec2(5, 3));
{
static float const myker[] =
{
0., 0., 1., 8./42, 4./42,
2./42, 4./42, 8./42, 4./42, 2./42,
1./42, 2./42, 4./42, 2./42, 1./42,
};
memcpy(&ret[0][0], myker, sizeof(myker));
}
return ret;
return { { 0.f, 0.f, 1.f, 8.f/42, 4.f/42, },
{ 2.f/42, 4.f/42, 8.f/42, 4.f/42, 2.f/42, },
{ 1.f/42, 2.f/42, 4.f/42, 2.f/42, 1.f/42, }, };

case EdiffAlgorithm::Burkes:
ret.SetSize(ivec2(5, 2));
{
static float const myker[] =
{
0., 0., 1., 4./16, 2./16,
1./16, 2./16, 4./16, 2./16, 1./16,
};
memcpy(&ret[0][0], myker, sizeof(myker));
}
return ret;
return { { 0.f, 0.f, 1.f, 4.f/16, 2.f/16, },
{ 1.f/16, 2.f/16, 4.f/16, 2.f/16, 1.f/16, }, };

case EdiffAlgorithm::Sierra:
ret.SetSize(ivec2(5, 3));
{
static float const myker[] =
{
0., 0., 1., 5./32, 3./32,
2./32, 4./32, 5./32, 4./32, 2./32,
0., 2./32, 3./32, 2./32, 0.,
};
memcpy(&ret[0][0], myker, sizeof(myker));
}
return ret;
return { { 0.f, 0.f, 1.f, 5.f/32, 3.f/32, },
{ 2.f/32, 4.f/32, 5.f/32, 4.f/32, 2.f/32, },
{ 0.f, 2.f/32, 3.f/32, 2.f/32, 0.f, }, };

case EdiffAlgorithm::Sierra2:
ret.SetSize(ivec2(5, 2));
{
static float const myker[] =
{
0., 0., 1., 4./16, 3./16,
1./16, 2./16, 3./16, 2./16, 1./16,
};
memcpy(&ret[0][0], myker, sizeof(myker));
}
return ret;
return { { 0.f, 0.f, 1.f, 4.f/16, 3.f/16, },
{ 1.f/16, 2.f/16, 3.f/16, 2.f/16, 1.f/16, }, };

case EdiffAlgorithm::Lite:
ret.SetSize(ivec2(3, 2));
{
static float const myker[] =
{
0., 1., 1./2,
1./4, 1./4, 0.,
};
memcpy(&ret[0][0], myker, sizeof(myker));
}
return ret;
return { { 0.f, 1.f, 1.f/2, },
{ 1.f/4, 1.f/4, 0.f, }, };
}

return ret;
return { { 1.f } };
}

/* Any standard deviation below this value will be rounded up, in order


Laden…
Abbrechen
Speichern