Browse Source

doc: update some tutorials to use the new entity init/release mechanism.

legacy
Sam Hocevar 5 years ago
parent
commit
96736217fe
5 changed files with 188 additions and 169 deletions
  1. +28
    -24
      doc/tutorial/01_triangle.cpp
  2. +43
    -38
      doc/tutorial/02_cube.cpp
  3. +33
    -31
      doc/tutorial/03_noise.cpp
  4. +35
    -29
      doc/tutorial/04_texture.cpp
  5. +49
    -47
      doc/tutorial/08_fbo.cpp

+ 28
- 24
doc/tutorial/01_triangle.cpp View File

@@ -26,34 +26,31 @@ LOLFX_RESOURCE_DECLARE(01_triangle);
class Triangle : public WorldEntity
{
public:
Triangle()
: m_vertices({ vec2( 0.0f, 0.8f),
vec2(-0.8f, -0.8f),
vec2( 0.8f, -0.8f) }),
m_ready(false)
virtual bool init_draw() override
{
}

virtual void tick_draw(float seconds, Scene &scene)
{
WorldEntity::tick_draw(seconds, scene);

if (!m_ready)
array<vec2> vertices
{
m_shader = Shader::Create(LOLFX_RESOURCE_NAME(01_triangle));
m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
vec2( 0.0f, 0.8f),
vec2(-0.8f, -0.8f),
vec2( 0.8f, -0.8f),
};

m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));
m_shader = Shader::Create(LOLFX_RESOURCE_NAME(01_triangle));
m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);

m_vbo = std::make_shared<VertexBuffer>(m_vertices.bytes());
void *vertices = m_vbo->Lock(0, 0);
memcpy(vertices, &m_vertices[0], m_vertices.bytes());
m_vbo->Unlock();
m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));

m_ready = true;
m_vbo = std::make_shared<VertexBuffer>(vertices.bytes());
void *data = m_vbo->Lock(0, 0);
memcpy(data, vertices.data(), vertices.bytes());
m_vbo->Unlock();

/* FIXME: this object never cleans up */
}
return true;
}

virtual void tick_draw(float seconds, Scene &scene) override
{
WorldEntity::tick_draw(seconds, scene);

m_shader->Bind();
m_vdecl->SetStream(m_vbo, m_coord);
@@ -62,13 +59,20 @@ public:
m_vdecl->Unbind();
}

virtual bool release_draw() override
{
m_shader.reset();
m_vdecl.reset();
m_vbo.reset();

return true;
}

private:
array<vec2> m_vertices;
std::shared_ptr<Shader> m_shader;
ShaderAttrib m_coord;
std::shared_ptr<VertexDeclaration> m_vdecl;
std::shared_ptr<VertexBuffer> m_vbo;
bool m_ready;
};

int main(int argc, char **argv)


+ 43
- 38
doc/tutorial/02_cube.cpp View File

@@ -41,8 +41,7 @@ public:
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)
4, 5, 1, 1, 0, 4, 3, 2, 6, 6, 7, 3, })
{
m_camera = new Camera();
m_camera->SetProjection(mat4::perspective(radians(30.f), 640.f, 480.f, .1f, 1000.f));
@@ -52,7 +51,6 @@ public:
Scene& scene = Scene::GetScene();
scene.PushCamera(m_camera);
Ticker::Ref(m_camera);

}

~Cube()
@@ -62,7 +60,37 @@ public:
Ticker::Unref(m_camera);
}

virtual void tick_game(float seconds)
virtual bool init_draw() override
{
m_shader = Shader::Create(LOLFX_RESOURCE_NAME(02_cube));

m_mvp = m_shader->GetUniformLocation("u_matrix");
m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
m_color = m_shader->GetAttribLocation(VertexUsage::Color, 0);

m_vdecl = std::make_shared<VertexDeclaration>(
VertexStream<vec3,vec3>(VertexUsage::Position,
VertexUsage::Color));

m_vbo = std::make_shared<VertexBuffer>(m_mesh.bytes());
void *mesh = m_vbo->Lock(0, 0);
memcpy(mesh, m_mesh.data(), m_mesh.bytes());
m_vbo->Unlock();

m_lines_ibo = std::make_shared<IndexBuffer>(m_lines_indices.bytes());
void *indices = m_lines_ibo->Lock(0, 0);
memcpy(indices, m_lines_indices.data(), m_lines_indices.bytes());
m_lines_ibo->Unlock();

m_faces_ibo = std::make_shared<IndexBuffer>(m_faces_indices.bytes());
indices = m_faces_ibo->Lock(0, 0);
memcpy(indices, m_faces_indices.data(), m_faces_indices.bytes());
m_faces_ibo->Unlock();

return true;
}

virtual void tick_game(float seconds) override
{
WorldEntity::tick_game(seconds);

@@ -95,41 +123,10 @@ public:
}
}

virtual void tick_draw(float seconds, Scene &scene)
virtual void tick_draw(float seconds, Scene &scene) override
{
WorldEntity::tick_draw(seconds, scene);

if (!m_ready)
{
m_shader = Shader::Create(LOLFX_RESOURCE_NAME(02_cube));

m_mvp = m_shader->GetUniformLocation("u_matrix");
m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
m_color = m_shader->GetAttribLocation(VertexUsage::Color, 0);

m_vdecl = std::make_shared<VertexDeclaration>(
VertexStream<vec3,vec3>(VertexUsage::Position,
VertexUsage::Color));

m_vbo = std::make_shared<VertexBuffer>(m_mesh.bytes());
void *mesh = m_vbo->Lock(0, 0);
memcpy(mesh, &m_mesh[0], m_mesh.bytes());
m_vbo->Unlock();

m_lines_ibo = std::make_shared<IndexBuffer>(m_lines_indices.bytes());
void *indices = m_lines_ibo->Lock(0, 0);
memcpy(indices, &m_lines_indices[0], m_lines_indices.bytes());
m_lines_ibo->Unlock();

m_faces_ibo = std::make_shared<IndexBuffer>(m_faces_indices.bytes());
indices = m_faces_ibo->Lock(0, 0);
memcpy(indices, &m_faces_indices[0], m_faces_indices.bytes());
m_faces_ibo->Unlock();

/* FIXME: this object never cleans up */
m_ready = true;
}

scene.get_renderer()->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));

m_shader->Bind();
@@ -149,6 +146,16 @@ public:
m_vdecl->Unbind();
}

virtual bool release_draw() override
{
m_shader.reset();
m_vdecl.reset();
m_vbo.reset();
m_lines_ibo.reset();
m_faces_ibo.reset();
return true;
}

private:
Camera* m_camera = nullptr;
float m_angle;
@@ -162,8 +169,6 @@ private:
std::shared_ptr<VertexDeclaration> m_vdecl;
std::shared_ptr<VertexBuffer> m_vbo;
std::shared_ptr<IndexBuffer> m_lines_ibo, m_faces_ibo;

bool m_ready;
};

int main(int argc, char **argv)


+ 33
- 31
doc/tutorial/03_noise.cpp View File

@@ -26,42 +26,38 @@ LOLFX_RESOURCE_DECLARE(03_noise);
class NoiseDemo : public WorldEntity
{
public:
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)
virtual bool init_draw() override
{
array<vec2> 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_shader = Shader::Create(LOLFX_RESOURCE_NAME(03_noise));
m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
m_time_uni = m_shader->GetUniformLocation("u_time");

m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));

m_vbo = std::make_shared<VertexBuffer>(vertices.bytes());
void *data = m_vbo->Lock(0, 0);
memcpy(data, vertices.data(), vertices.bytes());
m_vbo->Unlock();

return true;
}

virtual void tick_draw(float seconds, Scene &scene)
virtual void tick_draw(float seconds, Scene &scene) override
{
WorldEntity::tick_draw(seconds, scene);

m_time += seconds;

if (!m_ready)
{
m_shader = Shader::Create(LOLFX_RESOURCE_NAME(03_noise));
m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
m_time_uni = m_shader->GetUniformLocation("u_time");

m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));

m_vbo = std::make_shared<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);
@@ -70,15 +66,21 @@ public:
m_vdecl->Unbind();
}

virtual bool release_draw() override
{
m_shader.reset();
m_vdecl.reset();
m_vbo.reset();
return true;
}

private:
array<vec2> m_vertices;
std::shared_ptr<Shader> m_shader;
ShaderAttrib m_coord;
ShaderUniform m_time_uni;
std::shared_ptr<VertexDeclaration> m_vdecl;
std::shared_ptr<VertexBuffer> m_vbo;
float m_time;
bool m_ready;
float m_time = 0.0;
};

int main(int argc, char **argv)


+ 35
- 29
doc/tutorial/04_texture.cpp View File

@@ -27,19 +27,11 @@ class TextureDemo : public WorldEntity
{
public:
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_heightmap.resize(TEXTURE_WIDTH * 1);
}

virtual void tick_game(float seconds)
virtual void tick_game(float seconds) override
{
WorldEntity::tick_game(seconds);

@@ -60,30 +52,37 @@ public:
++m_frames;
}

virtual void tick_draw(float seconds, Scene &scene)
virtual bool init_draw() override
{
WorldEntity::tick_draw(seconds, scene);

/* Initialise GPU data */
if (!m_ready)
array<vec2> vertices
{
m_texture = std::make_shared<Texture>(ivec2(TEXTURE_WIDTH, 1), PixelFormat::Y_8);
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_shader = Shader::Create(LOLFX_RESOURCE_NAME(04_texture));
m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
m_texture_uni = m_shader->GetUniformLocation("u_texture");
m_texture = std::make_shared<Texture>(ivec2(TEXTURE_WIDTH, 1), PixelFormat::Y_8);

m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));
m_shader = Shader::Create(LOLFX_RESOURCE_NAME(04_texture));
m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
m_texture_uni = m_shader->GetUniformLocation("u_texture");

m_vbo = std::make_shared<VertexBuffer>(m_vertices.bytes());
void *vertices = m_vbo->Lock(0, 0);
memcpy(vertices, &m_vertices[0], m_vertices.bytes());
m_vbo->Unlock();
m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));

m_ready = true;
m_vbo = std::make_shared<VertexBuffer>(vertices.bytes());
void *data = m_vbo->Lock(0, 0);
memcpy(data, vertices.data(), vertices.bytes());
m_vbo->Unlock();

/* FIXME: this object never cleans up */
}
return true;
}

virtual void tick_draw(float seconds, Scene &scene)
{
WorldEntity::tick_draw(seconds, scene);

/* Send new heightmap to GPU */
m_texture->Bind();
@@ -97,8 +96,16 @@ public:
m_vdecl->Unbind();
}

virtual bool release_draw() override
{
m_texture.reset();
m_shader.reset();
m_vdecl.reset();
m_vbo.reset();
return true;
}

private:
array<vec2> m_vertices;
std::shared_ptr<Texture> m_texture;
std::shared_ptr<Shader> m_shader;
ShaderAttrib m_coord;
@@ -106,8 +113,7 @@ private:
std::shared_ptr<VertexDeclaration> m_vdecl;
std::shared_ptr<VertexBuffer> m_vbo;
array<uint8_t> m_heightmap;
int m_frames;
bool m_ready;
int m_frames = 0;
};

int main(int argc, char **argv)


+ 49
- 47
doc/tutorial/08_fbo.cpp View File

@@ -25,19 +25,7 @@ LOLFX_RESOURCE_DECLARE(08_fbo);
class FBO : public WorldEntity
{
public:
FBO()
: 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.f),
m_ready(false)
{
}

virtual void tick_game(float seconds)
virtual void tick_game(float seconds) override
{
WorldEntity::tick_game(seconds);

@@ -54,42 +42,49 @@ public:
m_color /= x;
}

virtual void tick_draw(float seconds, Scene &scene)
virtual bool init_draw() override
{
WorldEntity::tick_draw(seconds, scene);

if (!m_ready)
array<vec2> vertices
{
m_shader = Shader::Create(LOLFX_RESOURCE_NAME(08_fbo));
m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
m_uni_flag = m_shader->GetUniformLocation("u_flag");
m_uni_point = m_shader->GetUniformLocation("u_point");
m_uni_color = m_shader->GetUniformLocation("u_color");
m_uni_texture = m_shader->GetUniformLocation("u_texture");
m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));
m_vbo = std::make_shared<VertexBuffer>(m_vertices.bytes());
void *vertices = m_vbo->Lock(0, 0);
memcpy(vertices, &m_vertices[0], m_vertices.bytes());
m_vbo->Unlock();
m_fbo = std::make_shared<Framebuffer>(Video::GetSize());
m_fbo->Bind();
{
render_context rc(scene.get_renderer());
rc.clear_color(vec4(0.f, 0.f, 0.f, 1.f));
rc.clear_depth(1.f);
scene.get_renderer()->Clear(ClearMask::Color | ClearMask::Depth);
}
m_fbo->Unbind();
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_shader = Shader::Create(LOLFX_RESOURCE_NAME(08_fbo));
m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
m_uni_flag = m_shader->GetUniformLocation("u_flag");
m_uni_point = m_shader->GetUniformLocation("u_point");
m_uni_color = m_shader->GetUniformLocation("u_color");
m_uni_texture = m_shader->GetUniformLocation("u_texture");
m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));
m_vbo = std::make_shared<VertexBuffer>(vertices.bytes());
void *data = m_vbo->Lock(0, 0);
memcpy(data, vertices.data(), vertices.bytes());
m_vbo->Unlock();
m_fbo = std::make_shared<Framebuffer>(Video::GetSize());
return true;
}

m_ready = true;
virtual void tick_draw(float seconds, Scene &scene) override
{
WorldEntity::tick_draw(seconds, scene);

/* FIXME: this object never cleans up */
// FIXME: this should only be done once!
m_fbo->Bind();
{
render_context rc(scene.get_renderer());
rc.clear_color(vec4(0.f, 0.f, 0.f, 1.f));
rc.clear_depth(1.f);
scene.get_renderer()->Clear(ClearMask::Color | ClearMask::Depth);
}
m_fbo->Unbind();

/* FIXME: we should just disable depth test in the shader */
render_context rc(scene.get_renderer());
@@ -120,17 +115,24 @@ public:
m_shader->Unbind();
}

virtual bool release_draw() override
{
m_shader.reset();
m_vdecl.reset();
m_vbo.reset();
m_fbo.reset();
return true;
}

private:
array<vec2> m_vertices;
std::shared_ptr<Shader> m_shader;
ShaderAttrib m_coord;
ShaderUniform m_uni_flag, m_uni_point, m_uni_color, m_uni_texture;
std::shared_ptr<VertexDeclaration> m_vdecl;
std::shared_ptr<VertexBuffer> m_vbo;
std::shared_ptr<Framebuffer> m_fbo;
double m_time;
double m_time = 0.0f;
vec3 m_hotspot, m_color;
bool m_ready;
};

int main(int argc, char **argv)


Loading…
Cancel
Save