Bladeren bron

scene: start working on a postprocess mechanism.

Not all programs support this feature for now, but that’s because they
don’t support the scene framework either, and will need patching. Also
the default postprocess is deliberately exaggerated for now.
undefined
Sam Hocevar 8 jaren geleden
bovenliggende
commit
3a90766614
8 gewijzigde bestanden met toevoegingen van 107 en 17 verwijderingen
  1. +7
    -7
      doc/tutorial/08_fbo.cpp
  2. +1
    -0
      src/Makefile.am
  3. +1
    -3
      src/engine/ticker.cpp
  4. +1
    -1
      src/gpu/framebuffer.cpp
  5. +34
    -0
      src/gpu/postprocess.lolfx
  6. +1
    -0
      src/lolcore.vcxproj
  7. +56
    -3
      src/scene.cpp
  8. +6
    -3
      src/scene.h

+ 7
- 7
doc/tutorial/08_fbo.cpp Bestand weergeven

@@ -24,15 +24,15 @@ class FBO : public WorldEntity
{
public:
FBO()
: m_time(0.f),
: 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)
{
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);
}

virtual void TickGame(float seconds)


+ 1
- 0
src/Makefile.am Bestand weergeven

@@ -101,6 +101,7 @@ liblolcore_sources = \
gpu/tile.lolfx gpu/palette.lolfx gpu/line.lolfx \
gpu/emptymaterial.lolfx \
gpu/testmaterial.lolfx \
gpu/postprocess.lolfx \
\
gpu/lolfx.cpp \
\


+ 1
- 3
src/engine/ticker.cpp Bestand weergeven

@@ -468,9 +468,7 @@ void TickerData::DrawThreadTick()
}

/* Do the render step */
scene.RenderPrimitives();
scene.RenderTiles();
scene.RenderLines(data->deltatime);
scene.render(data->deltatime);

/* Disable display */
scene.DisableDisplay();


+ 1
- 1
src/gpu/framebuffer.cpp Bestand weergeven

@@ -303,7 +303,7 @@ Framebuffer::Framebuffer(ivec2 size, FramebufferFormat fbo_format)
GLenum format = fbo_format.GetFormat();
# endif
GLenum wrapmode = GL_CLAMP_TO_EDGE;
GLenum filtering = GL_NEAREST;
GLenum filtering = GL_LINEAR;

# if GL_VERSION_1_1 || GL_ES_VERSION_2_0
glGenFramebuffers(1, &m_data->m_fbo);


+ 34
- 0
src/gpu/postprocess.lolfx Bestand weergeven

@@ -0,0 +1,34 @@
[vert.glsl]

#version 120

attribute vec2 in_Position;

varying vec2 pass_Position;

void main()
{
pass_Position = in_Position;
gl_Position = vec4(in_Position, 0.0, 1.0);
}

[frag.glsl]

#version 120

#if defined GL_ES
precision highp float;
#endif

uniform sampler2D u_texture;

varying vec2 pass_Position;

void main(void)
{
vec2 texcoords = pass_Position * 0.5 + vec2(0.5, 0.5);
texcoords += 0.02 * sin(0.05 * gl_FragCoord.xy);
vec4 color = vec4(texture2D(u_texture, texcoords).rgb, 1.0);
gl_FragColor = color;
}


+ 1
- 0
src/lolcore.vcxproj Bestand weergeven

@@ -370,6 +370,7 @@
<LolFxCompile Include="gpu\emptymaterial.lolfx" />
<LolFxCompile Include="gpu\line.lolfx" />
<LolFxCompile Include="gpu\palette.lolfx" />
<LolFxCompile Include="gpu\postprocess.lolfx" />
<LolFxCompile Include="gpu\testmaterial.lolfx" />
<LolFxCompile Include="gpu\tile.lolfx" />
<LolFxCompile Include="gradient.lolfx" />


+ 56
- 3
src/scene.cpp Bestand weergeven

@@ -26,6 +26,7 @@
LOLFX_RESOURCE_DECLARE(tile);
LOLFX_RESOURCE_DECLARE(palette);
LOLFX_RESOURCE_DECLARE(line);
LOLFX_RESOURCE_DECLARE(postprocess);

namespace lol
{
@@ -127,6 +128,14 @@ private:
* the default one created by the app will be used */
SceneDisplay* m_display = nullptr;

/** Back buffer: where to render to. */
Framebuffer *m_backbuffer = nullptr;
Shader *m_pp_shader = nullptr;
VertexDeclaration *m_pp_vdecl;
ShaderUniform m_pp_texture;
ShaderAttrib m_pp_coord;
VertexBuffer *m_pp_vbo;

/* Sources are shared by all scenes.
* Renderers are scene-dependent. They get the primitive in the identical
* slot to render with the given scene.
@@ -179,6 +188,20 @@ mutex SceneData::m_prim_mutex;
Scene::Scene(ivec2 size)
: data(new SceneData())
{
data->m_backbuffer = new Framebuffer(size);
data->m_pp_shader = Shader::Create(LOLFX_RESOURCE_NAME(postprocess));
data->m_pp_coord = data->m_pp_shader->GetAttribLocation(VertexUsage::Position, 0);
data->m_pp_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
data->m_pp_texture = data->m_pp_shader->GetUniformLocation("u_texture");

array<vec2> quad { 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), };

data->m_pp_vbo = new VertexBuffer(quad.bytes());
void *vertices = data->m_pp_vbo->Lock(0, 0);
memcpy(vertices, &quad[0], quad.bytes());
data->m_pp_vbo->Unlock();

/* Create a default orthographic camera, in case the user doesn’t. */
data->m_default_cam = new Camera();
mat4 proj = mat4::ortho(0.f, (float)size.x, 0.f, (float)size.y, -1000.f, 1000.f);
@@ -576,8 +599,36 @@ void Scene::DisableDisplay()
data->m_display->Disable();
}

/* Render everything that the scene contains */
void Scene::render(float seconds)
{
/* First render into the offline buffer */
data->m_backbuffer->Bind();
{
RenderContext rc;
rc.SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
rc.SetClearDepth(1.f);
Renderer::Get()->Clear(ClearMask::Color | ClearMask::Depth);

// FIXME: get rid of the delta time argument
render_primitives();
render_tiles();
render_lines(seconds);
}
data->m_backbuffer->Unbind();

/* Now blit the offline buffer */
data->m_pp_shader->Bind();
data->m_pp_shader->SetUniform(data->m_pp_texture, data->m_backbuffer->GetTextureUniform(), 0);
data->m_pp_vdecl->SetStream(data->m_pp_vbo, data->m_pp_coord);
data->m_pp_vdecl->Bind();
data->m_pp_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
data->m_pp_vdecl->Unbind();
data->m_pp_shader->Unbind();
}

//-----------------------------------------------------------------------------
void Scene::RenderPrimitives()
void Scene::render_primitives()
{
ASSERT(!!data, "Trying to access a non-ready scene");

@@ -599,7 +650,7 @@ void Scene::RenderPrimitives()
}

//-----------------------------------------------------------------------------
void Scene::RenderTiles() // XXX: rename to Blit()
void Scene::render_tiles() // XXX: rename to Blit()
{
ASSERT(!!data, "Trying to access a non-ready scene");

@@ -723,7 +774,9 @@ void Scene::RenderTiles() // XXX: rename to Blit()
}

//-----------------------------------------------------------------------------
void Scene::RenderLines(float seconds) // XXX: rename to Blit()
// FIXME: get rid of the delta time argument
// XXX: rename to Blit()
void Scene::render_lines(float seconds)
{
ASSERT(!!data, "Trying to access a non-ready scene");



+ 6
- 3
src/scene.h Bestand weergeven

@@ -178,6 +178,7 @@ public:

private:
int HasPrimitiveRenderer(uintptr_t key);

void AddPrimitiveRenderer(uintptr_t key, class PrimitiveRenderer* renderer);
void SetPrimitiveRenderer(int index, uintptr_t key, class PrimitiveRenderer* renderer);
void ReleasePrimitiveRenderer(int index, uintptr_t key);
@@ -250,11 +251,13 @@ public:
void EnableDisplay();
void DisableDisplay();

void RenderPrimitives();
void RenderTiles();
void RenderLines(float seconds);
void render(float seconds);

private:
void render_primitives();
void render_tiles();
void render_lines(float seconds);

SceneData *data;
};



Laden…
Annuleren
Opslaan