Browse Source

tutorial: make the FBO example display something at last, so we can port

it to DirectX.
legacy
Sam Hocevar sam 12 years ago
parent
commit
13e5b6b8c4
2 changed files with 66 additions and 10 deletions
  1. +41
    -6
      tutorial/08_fbo.cpp
  2. +25
    -4
      tutorial/08_fbo.lolfx

+ 41
- 6
tutorial/08_fbo.cpp View File

@@ -14,6 +14,7 @@

#include "core.h"
#include "loldebug.h"
#include "lolgl.h"

using namespace std;
using namespace lol;
@@ -29,12 +30,28 @@ class FBO : public WorldEntity
public:
FBO()
{
m_vertices << vec2( 0.0, 0.8);
m_vertices << vec2(-0.8, -0.8);
m_vertices << vec2( 0.8, -0.8);
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 TickGame(float seconds)
{
WorldEntity::TickGame(seconds);

m_time += seconds;
m_hotspot = 0.4f * vec3(lol::cos(m_time * 2.f) + lol::cos(m_time * 3.3f),
lol::sin(m_time * 4.4f) + lol::sin(m_time * 3.2f),
lol::sin(m_time * 5.f));
m_color = 0.25f * vec3(3.f + lol::sin(m_time * 4.5f + 1.f),
3.f + lol::sin(m_time * 2.8f + 1.3f),
3.f + lol::sin(m_time * 3.7f));
}

virtual void TickDraw(float seconds)
{
WorldEntity::TickDraw(seconds);
@@ -43,6 +60,10 @@ public:
{
m_shader = Shader::Create(lolfx_08_fbo);
m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
m_uni_point = m_shader->GetUniformLocation("in_Point");
m_uni_color = m_shader->GetUniformLocation("in_Color");
m_uni_flag = m_shader->GetUniformLocation("in_Flag");
m_uni_texture = m_shader->GetUniformLocation("in_Texture");

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

@@ -52,6 +73,11 @@ public:
m_vbo->Unlock();

m_fbo = new FrameBuffer(Video::GetSize());
m_fbo->Bind();
glClearColor(0.0, 0.0, 0.0, 1.0f);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_fbo->Unbind();

m_ready = true;

@@ -59,18 +85,25 @@ public:
}

m_fbo->Bind();
/* FIXME: we should just disable depth test in the shader */
glClear(GL_DEPTH_BUFFER_BIT);
m_shader->Bind();
m_shader->SetUniform(m_uni_flag, 0.f);
m_shader->SetUniform(m_uni_point, m_hotspot);
m_shader->SetUniform(m_uni_color, m_color);
m_vdecl->SetStream(m_vbo, m_coord);
m_vdecl->Bind();
m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 1);
m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 2);
m_vdecl->Unbind();
m_shader->Unbind();
m_fbo->Unbind();

m_shader->Bind();
m_shader->SetUniform(m_uni_flag, 1.f);
m_shader->SetTexture(m_uni_texture, m_fbo->GetTexture(), 0);
m_vdecl->SetStream(m_vbo, m_coord);
m_vdecl->Bind();
m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 1);
m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 2);
m_vdecl->Unbind();
m_shader->Unbind();
}
@@ -79,9 +112,12 @@ private:
Array<vec2> m_vertices;
Shader *m_shader;
ShaderAttrib m_coord;
ShaderUniform m_uni_flag, m_uni_point, m_uni_color, m_uni_texture;
VertexDeclaration *m_vdecl;
VertexBuffer *m_vbo;
FrameBuffer *m_fbo;
double m_time;
vec3 m_hotspot, m_color;
bool m_ready;
};

@@ -95,7 +131,6 @@ int main(int argc, char **argv)
_chdir("../..");
#endif

new DebugFps(5, 5);
new FBO();

app.Run();


+ 25
- 4
tutorial/08_fbo.lolfx View File

@@ -2,21 +2,42 @@

#version 120

attribute vec2 in_Position;

varying vec2 pass_Position;

void main()
{
gl_Position = gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0;
pass_Position = in_Position;
gl_Position = vec4(in_Position, 0.0, 1.0);
}

-- GLSL.Frag --

#version 120

uniform sampler2D texture;
uniform sampler2D in_Texture;
uniform float in_Flag;
uniform vec3 in_Point;
uniform vec3 in_Color;

varying vec2 pass_Position;

void main(void)
{
gl_FragColor = vec4(texture2D(texture, gl_TexCoord[0].xy).xyz, 1.0);
if (in_Flag == 0.0)
{
float s = 4.0 + 4.0 * in_Point.z;
vec2 p = pass_Position - in_Point.xy * 0.8;
float f = clamp(1.0 - dot(s * p, s * p), 0.0, 1.0);
f = sqrt(f);
gl_FragColor = vec4(f * in_Color, f + 0.1);
}
else
{
vec2 texcoords = pass_Position * 0.5 + vec2(0.5, 0.5);
gl_FragColor = vec4(texture2D(in_Texture, texcoords).xyz, 1.0);
}
}

-- HLSL.Vert --


Loading…
Cancel
Save